优化Legendformatter支持{h}通配符

This commit is contained in:
monitor1394
2022-12-12 13:15:29 +08:00
parent a3a25fe7c6
commit ce1691e1ce
3 changed files with 13 additions and 10 deletions

View File

@@ -65,6 +65,7 @@ slug: /changelog
## master
* (2022.12.12) 优化`Legend``formatter`支持`{h}`通配符
* (2022.12.12) 修复`Legend``formatter`设置为固定值时显示不正常的问题
* (2022.12.08) 增加`AreaStyle``toTop`参数可设置折线图渐变色是到顶部还是到实际位置
* (2022.12.07) 增加`Formatter`的文本通配符`{h}`支持设置当前颜色值

View File

@@ -156,7 +156,7 @@ namespace XCharts.Runtime
var content = legend.formatter.Replace("{name}", category);
content = content.Replace("{value}", category);
var serie = chart.GetSerie(0);
FormatterHelper.ReplaceContent(ref content, dataIndex, legend.numericFormatter, serie, chart);
FormatterHelper.ReplaceContent(ref content, dataIndex, legend.numericFormatter, serie, chart, category);
return content;
}
}

View File

@@ -25,7 +25,7 @@ namespace XCharts.Runtime
}
/// <summary>
/// 替换字符串中的通配符,支持的通配符有{.}、{a}、{b}、{c}、{d}、{e}、{f}、{g}。
/// 替换字符串中的通配符,支持的通配符有{.}、{a}、{b}、{c}、{d}、{e}、{f}、{g}、{h}
/// </summary>
/// <param name="content">要替换的字符串</param>
/// <param name="dataIndex">选中的数据项serieData索引</param>
@@ -34,10 +34,9 @@ namespace XCharts.Runtime
/// <param name="series">所有serie</param>
/// <param name="theme">用来获取指定index的颜色</param>
/// <param name="category">选中的类目,一般用在折线图和柱状图</param>
/// <param name="dataZoom">dataZoom</param>
/// <returns></returns>
public static bool ReplaceContent(ref string content, int dataIndex, string numericFormatter, Serie serie,
BaseChart chart, DataZoom dataZoom = null)
BaseChart chart, string colorName = null)
{
var foundDot = false;
var mc = s_Regex.Matches(content);
@@ -66,20 +65,23 @@ namespace XCharts.Runtime
if (serie == null) continue;
if (p == '.' || p == 'h' || p == 'H')
{
var bIndex = targetIndex;
var bIndex = dataIndex;
if (argsCount >= 2)
{
var args1Str = args[1].ToString();
if (s_RegexN.IsMatch(args1Str)) bIndex = int.Parse(args1Str);
}
var color = string.IsNullOrEmpty(colorName) ?
(Color) chart.GetMarkColor(serie, serie.GetSerieData(bIndex)) :
SeriesHelper.GetNameColor(chart, bIndex, colorName);
if (p == '.')
{
content = content.Replace(old, ChartCached.ColorToDotStr(chart.theme.GetColor(bIndex)));
content = content.Replace(old, ChartCached.ColorToDotStr(color));
foundDot = true;
}
else
{
content = content.Replace(old, "#" + ChartCached.ColorToStr(chart.theme.GetColor(bIndex)));
content = content.Replace(old, "#" + ChartCached.ColorToStr(color));
}
}
else if (p == 'a' || p == 'A')
@@ -100,12 +102,12 @@ namespace XCharts.Runtime
var needCategory = (p != 'e' && p != 'E') && (serie is Line || serie is Bar);
if (needCategory)
{
var category = chart.GetTooltipCategory(dataIndex, serie, dataZoom);
var category = chart.GetTooltipCategory(dataIndex, serie);
content = content.Replace(old, category);
}
else
{
var serieData = serie.GetSerieData(bIndex, dataZoom);
var serieData = serie.GetSerieData(bIndex);
content = content.Replace(old, serieData.name);
}
}
@@ -151,7 +153,7 @@ namespace XCharts.Runtime
{
numericFormatter = SerieHelper.GetNumericFormatter(serie, serie.GetSerieData(bIndex), "");
}
var value = serie.GetData(bIndex, dimensionIndex, dataZoom);
var value = serie.GetData(bIndex, dimensionIndex);
if (isPercent)
{
var total = serie.GetDataTotal(dimensionIndex, serie.GetSerieData(bIndex));