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

View File

@@ -156,7 +156,7 @@ namespace XCharts.Runtime
var content = legend.formatter.Replace("{name}", category); var content = legend.formatter.Replace("{name}", category);
content = content.Replace("{value}", category); content = content.Replace("{value}", category);
var serie = chart.GetSerie(0); 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; return content;
} }
} }

View File

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