优化Tooltipformatter,支持{c1:1-1:f1}格式配置

This commit is contained in:
monitor1394
2020-06-02 12:37:14 +08:00
parent 5d5dc468c0
commit ee7dd6b367
5 changed files with 214 additions and 155 deletions

View File

@@ -1,6 +1,7 @@
# 更新日志
* (2020.06.02) 优化`Tooltip``formatter`,支持`{c1:1-1:f1}`格式配置
* (2020.05.31) 优化`Background`组件的生效条件,需要有单独的父节点(升级前需要自己处理旧的背景节点)
* (2020.05.30) 优化`PieChart`支持设置`ignoreValue`不显示指定数据
* (2020.05.30) 修复`RadarChart``Circle`时不绘制`SplitArea`的问题

View File

@@ -191,13 +191,20 @@
* `Shadow`:阴影指示器。
* `None`:无指示器。
* `Corss`十字准星指示器。坐标轴显示Label和交叉线。
* `formatter`:提示框内容字符串模版格式器。支持用 `\n``<br/>` 换行。当`formatter`不为空时,优先使用`formatter`,否则使用`itemFormatter`示例:`{a}:{c}``{a1}:{c1}`。其中`{.}`表示带动态颜色的圆点;`{c:0}``{c1:1}`表示索引为0、1的serie的数据项的第0、第1个数据其它变量 `{a}`, `{b}`, `{c}`, `{d}` 在不同图表类型下代表数据含义为:
* 折线区域图、柱状条形图、K线图 : `{a}`(系列名称),`{b}`(类目值),`{c}`(数值), `{d}`(无)
* 散点图(气泡)图 : `{a}`(系列名称),`{b}`(数据名称),`{c}`(数值数组), `{d}`(无)
* 地图 : `{a}`系列名称),`{b}`(区域名称),`{c}`(合并数值), `{d}`(无)
* 饼图、仪表盘、漏斗图: `{a}`(系列名称),`{b}`(数据项名称),`{c}`(数值), `{d}`(百分比)。
* `titleFormatter`:提示框标题内容的字符串模版格式器。支持用 `\n``<br/>` 换行。仅当`itemFormatter`生效时才有效
* `itemFormatter`:提示框单个`serie`或数据项内容的字符串模版格式器。支持用 `\n``<br/>` 换行。当`formatter`不为空时,优先使用`formatter`,否则使用`itemFormatter`
* `formatter`:提示框内容字符串模版格式器。支持用 `\n` 换行。当`formatter`不为空时,优先使用`formatter`,否则使用`itemFormatter`
* 模板变量有`{.}``{a}``{b}``{c}``{d}`
* `{.}`表示带动态颜色的圆点
* `{a}`系列名`serie``name`
* `{b}`为数据项名`serieData``name`,或者类目值(如折线图的`X`)。
* `{c}`为数值
* `{d}`为百分比值,注意不带`%`
* `{a1}``{b1}``{c1}`表示指定`index``1``serie``{a}`默认`index``0``serie`
* `{c1:2}`表示索引为`1``serie`的数据项的第`3`个数据(`index``2`)。
* `{c1:2-2}`表示索引为`1``serie`的第`3`个数据项的第`3`个数据。
* `{d1:2:f2}`表示单独指定了数值的格式化字符串为`f2`(不指定时用`numericFormatter`)。
* 示例:`"{a}:{c}"``"{a1}:{c1:f1}"``"{a1}:{c1:1f1}"`
* `titleFormatter`:提示框标题内容的字符串模版格式器。支持用 `\n` 换行。仅当`itemFormatter`生效时才有效。
* `itemFormatter`:提示框单个`serie`或数据项内容的字符串模版格式器。支持用 `\n` 换行。当`formatter`不为空时,优先使用`formatter`,否则使用`itemFormatter`
* `numericFormatter`:标准数字格式字符串。用于将数值格式化显示为字符串。使用`Axx`的形式:`A`是格式说明符的单字符,支持`C`货币、`D`十进制、`E`指数、`F`顶点数、`G`常规、`N`数字、`P`百分比、`R`往返过程、`X`十六进制等九种。`xx`是精度说明,从`0`-`99`
* `fixedWidth`:固定宽度。当同时设置 `fixedWidth``minWidth` 时,`fixedWidth``minWidth` 优先级高。
* `fixedHeight`:固定高度。当同时设置 `fixedHeight``minHeight` 时,`fixedHeight``minHeight` 优先级高。

View File

@@ -1196,6 +1196,32 @@ namespace XCharts
}
}
/// <summary>
/// 获得指定index指定维数的数据
/// </summary>
/// <param name="index"></param>
/// <param name="dimension"></param>
/// <param name="dataZoom"></param>
/// <returns></returns>
public float GetData(int index, int dimension, DataZoom dataZoom = null)
{
if (index < 0 || dimension < 0) return 0;
var serieData = GetSerieData(index, dataZoom);
if (serieData != null && dimension < serieData.data.Count)
{
var value = serieData.GetData(dimension);
if (showAsPositiveNumber)
{
value = Mathf.Abs(value);
}
return value;
}
else
{
return 0;
}
}
/// <summary>
/// 获得维度Y索引对应的数据
/// </summary>

View File

@@ -94,44 +94,47 @@ namespace XCharts
set { if (PropertyUtility.SetStruct(ref m_Type, value)) SetAllDirty(); }
}
/// <summary>
/// 提示框总内容的字符串模版格式器。支持用 \n 或 "<br/>" 换行。当formatter不为空时优先使用formatter否则使用itemFormatter。
/// 模板变量有 {a}, {b}{c}{d}{i},分别表示系列名,数据名,数据值等。{a0},{b1},{c1}等可指定serie
/// 提示框总内容的字符串模版格式器。支持用 \n 换行。当formatter不为空时优先使用formatter否则使用itemFormatter。
/// 模板变量有{.}、{a}{b}{c}{d}。
/// {.}表示带动态颜色的圆点。
/// {c1:0}、{c1:1}表示索引为1的serie的数据项的第0、第1个数据
/// 其它变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:
/// <list type="bullet">
/// <item><description>折线区域图、柱状条形图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)。</description></item>
/// <item><description>散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)。</description></item>
/// <item><description>地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)。</description></item>
/// <item><description>饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)。</description></item>
/// </list>
/// 示例:"{a}:{c}","{a1}:{c1:f1}"
/// {a}为系列名serie的name
/// {b}为数据项名serieData的name或者类目值如折线图的X轴
/// {c}为数值。
/// {d}为百分比值,注意不带%号。
/// {a1}、{b1}、{c1}表示指定index为1的serie{a}默认index为0的serie。
/// {c1:2}表示索引为1的serie的数据项的第3个数据index为2
/// {c1:2-2}表示索引为1的serie的第3个数据项的第3个数据。
/// {d1:2:f2}表示单独指定了数值的格式化字符串为f2不指定时用numericFormatter
/// 示例:"{a}:{c}""{a1}:{c1:f1}"、"{a1}:{c1:1f1}"
/// </summary>
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
/// <summary>
/// 提示框标题内容的字符串模版格式器。支持用 \n 或 "<br/>" 换行。仅当itemFormatter生效时才有效。
/// 模板变量有 {a}, {b}{c}{d}{e},分别表示系列名,数据名,数据值等。{a0},{b1},c{1}等可指定serie
/// 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:
/// <list type="bullet">
/// <item><description>折线区域图、柱状条形图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)。</description></item>
/// <item><description>散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}数值数组), {d}(无)。</description></item>
/// <item><description>地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)。</description></item>
/// <item><description>饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)。</description></item>
/// </list>
/// 示例:"{a}:{c}","{a1}:{c1:f1}"
/// 提示框标题内容的字符串模版格式器。支持用 \n 换行。仅当itemFormatter生效时才有效。
/// 模板变量有{.}、{a}{b}{c}{d}。
/// {.}表示带动态颜色的圆点。
/// {a}为系列名serie的name。
/// {b}为数据项名serieData的name或者类目值如折线图的X轴
/// {c}数值
/// {d}为百分比值,注意不带%号。
/// {a1}、{b1}、{c1}表示指定index为1的serie{a}默认index为0的serie。
/// {c1:2}表示索引为1的serie的数据项的第3个数据index为2
/// {c1:2-2}表示索引为1的serie的第3个数据项的第3个数据。
/// {d1:2:f2}表示单独指定了数值的格式化字符串为f2不指定时用numericFormatter
/// 示例:"{a}:{c}"、"{a1}:{c1:f1}"、"{a1}:{c1:1f1}"
/// </summary>
public string titleFormatter { get { return m_TitleFormatter; } set { m_TitleFormatter = value; } }
/// <summary>
/// 提示框单个serie或数据项内容的字符串模版格式器。支持用 \n 或 "<br/>" 换行。当formatter不为空时优先使用formatter否则使用itemFormatter。
/// 模板变量有 {a}, {b}{c}{d}{e},分别表示系列名,数据名,数据值等
/// 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:
/// <list type="bullet">
/// <item><description>折线区域图、柱状条形图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)。</description></item>
/// <item><description>散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}数值数组), {d}(无)。</description></item>
/// <item><description>地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)。</description></item>
/// <item><description>饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)。</description></item>
/// </list>
/// 示例:"{a}:{c}","{a}:{c:f1}"
/// 提示框单个serie或数据项内容的字符串模版格式器。支持用 \n 换行。当formatter不为空时优先使用formatter否则使用itemFormatter。
/// 模板变量有{.}、{a}{b}{c}{d}。
/// {.}表示带动态颜色的圆点。
/// {a}为系列名serie的name。
/// {b}为数据项名serieData的name或者类目值如折线图的X轴
/// {c}数值
/// {d}为百分比值,注意不带%号。
/// {c:2}表示索引为1的serie的数据项的第3个数据index为2
/// {c:2-2}表示索引为1的serie的第3个数据项的第3个数据。
/// {d:f2}表示单独指定了数值的格式化字符串为f2不指定时用numericFormatter
/// 示例:"{a}:{c}"、"{a1}:{c1:f1}"、"{a1}:{c1:1f1}"
/// </summary>
public string itemFormatter { get { return m_ItemFormatter; } set { m_ItemFormatter = value; } }

View File

@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
@@ -7,26 +5,21 @@ using System.Collections.Generic;
/* */
/******************************************/
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
namespace XCharts
{
internal static class TooltipHelper
{
private const string PH_A = "{a}";
private const string PH_B = "{b}";
private const string PH_C = "{c}";
private const string PH_D = "{d}";
private const string PH_I = "{.}";
private const string PH_Y = "{j}";
private const string PH_ON = "\\n";
private const string PH_NN = "\n";
private const string PH_NN_BBB = "\n";
private const string PH_BR = "<br/>";
private static Dictionary<string, Dictionary<int, string>> s_PHDic = new Dictionary<string, Dictionary<int, string>>();
private static Dictionary<int, string> s_PHCCDic = new Dictionary<int, string>();
private static Dictionary<int, Dictionary<int, string>> s_PHSerieCCDic = new Dictionary<int, Dictionary<int, string>>();
private static Regex s_Regex = new Regex(@"{([a-d|.]\d*)(:\d+(-\d+)?)?(:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
private static Regex s_RegexSub = new Regex(@"(\w?-?\d+)|(\w)|(\.)", RegexOptions.IgnoreCase);
private static Regex s_RegexN = new Regex(@"^\d+", RegexOptions.IgnoreCase);
private static Regex s_RegexN_N = new Regex(@"\d+-\d+", RegexOptions.IgnoreCase);
private static Regex s_RegexFn = new Regex(@"[c-g|x|p|r]\d*", RegexOptions.IgnoreCase);
private static Regex s_RegexNewLine = new Regex(@"[\\|/]+n", RegexOptions.IgnoreCase);
private static void InitScatterTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
@@ -228,27 +221,14 @@ namespace XCharts
var itemTitle = title;
if (!string.IsNullOrEmpty(itemTitle))
{
Replace(ref itemTitle, PH_A, i, serie.name, true);
ReplaceContent(ref itemTitle, dataIndex, tooltip, serie, null, themeInfo, category, dataZoom, isCartesian);
sb.Append(itemTitle).Append(PH_NN);
}
var dataIndexList = tooltip.runtimeSerieDataIndex[serie.index];
foreach (var tempIndex in dataIndexList)
{
var foundDot = false;
var serieData = serie.GetSerieData(tempIndex);
string content = itemFormatter;
Replace(ref content, PH_A, i, serie.name, true);
Replace(ref content, PH_B, i, needCategory ? category : serieData.name, true);
if (itemFormatter.IndexOf(PH_I) >= 0)
{
foundDot = true;
Replace(ref content, PH_I, i, ChartCached.ColorToDotStr(themeInfo.GetColor(serie.index)), true);
}
for (int n = 0; n < serieData.data.Count; n++)
{
var valueStr = ChartCached.FloatToStr(serieData.GetData(n), numericFormatter);
Replace(ref content, GetPHCC(n), i, valueStr, true);
}
var foundDot = ReplaceContent(ref content, tempIndex, tooltip, serie, null, themeInfo, category, dataZoom, isCartesian);
if (!foundDot)
{
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(serie.index)));
@@ -262,16 +242,10 @@ namespace XCharts
var serieData = serie.GetSerieData(dataIndex, dataZoom);
if (serieData == null) continue;
var itemFormatter = GetItemFormatter(tooltip, serie, serieData);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
var percent = serieData.GetData(1) / serie.yTotal * 100;
needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar);
if (formatTitle)
{
var valueStr = ChartCached.FloatToStr(serieData.GetData(1), numericFormatter);
Replace(ref title, PH_A, i, serie.name, true);
Replace(ref title, PH_B, i, needCategory ? category : serieData.name, true);
Replace(ref title, PH_C, i, valueStr, true);
Replace(ref title, PH_D, i, ChartCached.FloatToStr(percent, string.Empty, 1), true);
ReplaceContent(ref title, dataIndex, tooltip, null, series, themeInfo, category, dataZoom, isCartesian);
}
if (serie.show)
{
@@ -283,16 +257,7 @@ namespace XCharts
continue;
}
string content = itemFormatter;
var valueStr = ChartCached.FloatToStr(serieData.GetData(1), numericFormatter);
Replace(ref content, PH_A, i, serie.name, true);
Replace(ref content, PH_B, i, needCategory ? category : serieData.name, true);
Replace(ref content, PH_C, i, valueStr, true);
Replace(ref content, PH_D, i, ChartCached.FloatToStr(percent, string.Empty, 1), true);
for (int n = 0; n < serieData.data.Count; n++)
{
valueStr = ChartCached.FloatToStr(serieData.GetData(n), numericFormatter);
Replace(ref content, GetPHCC(n), i, valueStr, true);
}
ReplaceContent(ref content, dataIndex, tooltip, serie, null, themeInfo, category, dataZoom, isCartesian);
if (!first) sb.Append(PH_NN);
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
sb.Append(content);
@@ -311,45 +276,145 @@ namespace XCharts
}
else
{
title = title.Replace(PH_ON, PH_NN);
title = title.Replace(PH_BR, PH_NN);
title = s_RegexNewLine.Replace(title, PH_NN);
return title + PH_NN + TrimAndReplaceLine(sb);
}
}
else
{
string content = tooltip.formatter;
for (int i = 0; i < series.Count; i++)
{
var serie = series.GetSerie(i);
if (serie.show)
{
var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
var serieData = serie.GetSerieData(dataIndex, dataZoom);
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
var percent = serieData.GetData(1) / serie.yTotal * 100;
Replace(ref content, PH_A, i, serie.name);
Replace(ref content, PH_B, i, needCategory ? category : serieData.name);
Replace(ref content, PH_C, i, ChartCached.FloatToStr(serieData.GetData(1), numericFormatter));
Replace(ref content, PH_D, i, ChartCached.FloatToStr(percent, string.Empty, 1));
Replace(ref content, PH_I, i, ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
for (int n = 0; n < serieData.data.Count; n++)
{
var valueStr = ChartCached.FloatToStr(serieData.GetData(n), numericFormatter);
if (i == 0) Replace(ref content, GetPHCC(n), i, valueStr, true);
Replace(ref content, GetPHCC(i, n), i, valueStr, true);
}
}
}
content = content.Replace(PH_ON, PH_NN);
content = content.Replace(PH_BR, PH_NN);
ReplaceContent(ref content, dataIndex, tooltip, null, series, themeInfo, category, dataZoom, isCartesian);
return content;
}
}
public static bool ReplaceContent(ref string content, int dataIndex, Tooltip tooltip, Serie serie, Series series,
ThemeInfo themeInfo, string category = null, DataZoom dataZoom = null, bool isCartesian = false)
{
var foundDot = false;
var mc = s_Regex.Matches(content);
foreach (var m in mc)
{
var old = m.ToString();
var args = s_RegexSub.Matches(m.ToString());
var argsCount = args.Count;
if (argsCount <= 0) continue;
int targetIndex = 0;
char p = GetSerieIndex(args[0].ToString(), ref targetIndex);
if (serie == null)
{
if (targetIndex == -1) continue;
serie = series.GetSerie(targetIndex);
if (serie == null) continue;
}
else
{
targetIndex = serie.index;
}
if (p == '.')
{
if (argsCount == 1)
{
content = content.Replace(old, ChartCached.ColorToDotStr(themeInfo.GetColor(targetIndex)));
foundDot = true;
}
}
else if (p == 'a' || p == 'A')
{
if (argsCount == 1)
{
content = content.Replace(old, serie.name);
}
}
else if (p == 'b' || p == 'B')
{
var bIndex = dataIndex;
if (argsCount >= 2)
{
var args1Str = args[1].ToString();
if (s_RegexN.IsMatch(args1Str)) bIndex = int.Parse(args1Str);
}
var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
if (needCategory)
{
content = content.Replace(old, category);
}
else
{
var serieData = serie.GetSerieData(bIndex, dataZoom);
content = content.Replace(old, serieData.name);
}
}
else if (p == 'c' || p == 'C' || p == 'd' || p == 'D')
{
var isPercent = p == 'd' || p == 'D';
var bIndex = dataIndex;
var dimensionIndex = -1;
var numericFormatter = string.Empty;
if (argsCount >= 2)
{
var args1Str = args[1].ToString();
if (s_RegexFn.IsMatch(args1Str))
{
numericFormatter = args1Str;
}
else if (s_RegexN_N.IsMatch(args1Str))
{
var temp = args1Str.Split('-');
bIndex = int.Parse(temp[0]);
dimensionIndex = int.Parse(temp[1]);
}
else if (s_RegexN.IsMatch(args1Str))
{
dimensionIndex = int.Parse(args1Str);
}
else
{
Debug.LogError("unmatch:" + args1Str);
continue;
}
}
if (argsCount >= 3)
{
numericFormatter = args[2].ToString();
}
if (dimensionIndex == -1) dimensionIndex = 1;
if (numericFormatter == string.Empty)
{
numericFormatter = GetItemNumericFormatter(tooltip, serie, serie.GetSerieData(bIndex));
}
var value = serie.GetData(bIndex, dimensionIndex, dataZoom);
if (isPercent)
{
var percent = value / serie.yTotal * 100;
content = content.Replace(old, ChartCached.FloatToStr(percent, numericFormatter));
}
else
{
content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
}
}
}
content = s_RegexNewLine.Replace(content, PH_NN);
return foundDot;
}
private static char GetSerieIndex(string strType, ref int index)
{
index = 0;
if (strType.Length > 1)
{
if (!int.TryParse(strType.Substring(1), out index))
{
index = -1;
}
}
return strType.ElementAt(0);
}
private static string TrimAndReplaceLine(StringBuilder sb)
{
return sb.ToString().Trim().Replace(PH_ON, PH_NN_BBB).Replace(PH_BR, PH_NN_BBB);
return s_RegexNewLine.Replace(sb.ToString().Trim(), PH_NN);
}
private static bool IsSelectedSerie(Tooltip tooltip, int serieIndex)
@@ -361,49 +426,6 @@ namespace XCharts
return false;
}
private static void Replace(ref string content, string placeHolder, int index, string newStr, bool all = false)
{
if ((all || index == 0) && content.IndexOf(placeHolder) >= 0)
{
content = content.Replace(placeHolder, newStr);
}
if (!s_PHDic.ContainsKey(placeHolder))
{
s_PHDic[placeHolder] = new Dictionary<int, string>();
}
if (!s_PHDic[placeHolder].ContainsKey(index))
{
s_PHDic[placeHolder][index] = placeHolder.Insert(2, index.ToString());
}
var holder = s_PHDic[placeHolder][index];
if (content.IndexOf(holder) >= 0)
{
content = content.Replace(holder, newStr);
}
}
private static string GetPHCC(int index)
{
if (!s_PHCCDic.ContainsKey(index))
{
s_PHCCDic[index] = "{c:" + index + "}";
}
return s_PHCCDic[index];
}
private static string GetPHCC(int serieIndex, int dataIndex)
{
if (!s_PHSerieCCDic.ContainsKey(serieIndex))
{
s_PHSerieCCDic[serieIndex] = new Dictionary<int, string>();
}
if (!s_PHSerieCCDic[serieIndex].ContainsKey(dataIndex))
{
s_PHSerieCCDic[serieIndex][dataIndex] = "{c" + serieIndex + ":" + dataIndex + "}";
}
return s_PHSerieCCDic[serieIndex][dataIndex];
}
private static string GetItemFormatter(Tooltip tooltip, Serie serie, SerieData serieData)
{
var itemStyle = SerieHelper.GetItemStyle(serie, serieData);