增加Tooltipformatter{.}{c:0}{c1:1}的支持

This commit is contained in:
monitor1394
2020-04-23 13:07:49 +08:00
parent 6d64156db4
commit 6cdeb2516b
9 changed files with 343 additions and 61 deletions

View File

@@ -1,6 +1,8 @@
# 更新日志
* (2020.04.23) 优化`ScatterChart``Tooltip`显示效果
* (2020.04.23) 增加`Tooltip``formatter``{.}``{c:0}``{c1:1}`的支持
* (2020.04.19) 优化`LineChart`折线图的区域填充渐变效果
* (2020.04.19) 增加`AxisLabel``onZero`参数可将`Label`显示在`0`刻度上
* (2020.04.19) 增加`Serie``AxisLabel``showAsPositiveNumber`参数将负数数值显示为正数

View File

@@ -190,7 +190,7 @@
* `Shadow`:阴影指示器。
* `None`:无指示器。
* `Corss`十字准星指示器。坐标轴显示Label和交叉线。
* `formatter`:提示框内容字符串模版格式器。支持用 `\n``<br/>` 换行。当`formatter`不为空时,优先使用`formatter`,否则使用`itemFormatter`。示例:`{a}:{c}``{a1}:{c1:f1}`。其中变量 `{a}`, `{b}`, `{c}`, `{d}` 在不同图表类型下代表数据含义为:
* `formatter`:提示框内容字符串模版格式器。支持用 `\n``<br/>` 换行。当`formatter`不为空时,优先使用`formatter`,否则使用`itemFormatter`。示例:`{a}:{c}``{a1}:{c1:f1}`。其中`{.}`表示带动态颜色的圆点;`{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}`(无)。

View File

@@ -1163,7 +1163,12 @@ namespace XCharts
var serieData = GetDataList(dataZoom);
if (index < serieData.Count)
{
return serieData[index].data[1];
var value = serieData[index].data[1];
if (showAsPositiveNumber)
{
value = Mathf.Abs(value);
}
return value;
}
return 0;
}
@@ -1174,7 +1179,12 @@ namespace XCharts
var serieData = GetDataList(dataZoom);
if (index < serieData.Count)
{
return serieData[index].GetCurrData(1, animation.GetUpdateAnimationDuration());
var value = serieData[index].GetCurrData(1, animation.GetUpdateAnimationDuration());
if (showAsPositiveNumber)
{
value = Mathf.Abs(value);
}
return value;
}
return 0;
}
@@ -1195,6 +1205,10 @@ namespace XCharts
if (index < serieData.Count)
{
yData = serieData[index].data[1];
if (showAsPositiveNumber)
{
yData = Mathf.Abs(yData);
}
dataName = serieData[index].name;
}
}

View File

@@ -95,8 +95,10 @@ namespace XCharts
}
/// <summary>
/// 提示框总内容的字符串模版格式器。支持用 \n 或 "<br/>" 换行。当formatter不为空时优先使用formatter否则使用itemFormatter。
/// 模板变量有 {a}, {b}{c}{d}{e},分别表示系列名,数据名,数据值等。{a0},{b1},c{1}等可指定serie。
/// 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:
/// 模板变量有 {a}, {b}{c}{d}{i},分别表示系列名,数据名,数据值等。{a0},{b1},{c1}等可指定serie。
/// {.}表示带动态颜色的圆点。
/// {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>
@@ -207,7 +209,10 @@ namespace XCharts
lineStyle.ClearComponentDirty();
textStyle.ClearComponentDirty();
}
/// <summary>
/// 当前提示框所指示的Serie索引目前只对散点图有效
/// </summary>
public Dictionary<int, List<int>> runtimeSerieDataIndex { get; internal set; }
/// <summary>
/// The data index currently indicated by Tooltip.
/// 当前提示框所指示的数据项索引。
@@ -259,7 +264,8 @@ namespace XCharts
runtimeXValues = new float[2] { -1, -1 },
runtimeYValues = new float[2] { -1, -1 },
runtimeDataIndex = new List<int>() { -1, -1 },
lastDataIndex = new List<int>() { -1, -1 }
lastDataIndex = new List<int>() { -1, -1 },
runtimeSerieDataIndex = new Dictionary<int, List<int>>()
};
return tooltip;
}
@@ -451,5 +457,33 @@ namespace XCharts
{
return runtimeDataIndex[0] == index || runtimeDataIndex[1] == index;
}
public void ClearSerieDataIndex()
{
foreach (var kv in runtimeSerieDataIndex)
{
kv.Value.Clear();
}
}
public void AddSerieDataIndex(int serieIndex, int dataIndex)
{
if (!runtimeSerieDataIndex.ContainsKey(serieIndex))
{
runtimeSerieDataIndex[serieIndex] = new List<int>();
}
runtimeSerieDataIndex[serieIndex].Add(dataIndex);
}
public bool isAnySerieDataIndex()
{
foreach (var kv in runtimeSerieDataIndex)
{
if (kv.Value.Count > 0) return true;
}
return false;
}
}
}

View File

@@ -171,6 +171,7 @@ namespace XCharts
/// 饼图数据项的偏移半径
/// </summary>
public float runtimePieOffsetRadius { get; internal set; }
public Vector3 runtimePosition { get; internal set; }
public Vector3 runtiemPieOffsetCenter { get; internal set; }
private List<float> m_PreviousData = new List<float>();
private List<float> m_DataUpdateTime = new List<float>();

View File

@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
@@ -12,6 +14,48 @@ 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 void InitScatterTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
{
if (!tooltip.runtimeSerieDataIndex.ContainsKey(serie.index)) return;
var dataIndexList = tooltip.runtimeSerieDataIndex[serie.index];
if (!string.IsNullOrEmpty(serie.name))
{
sb.Append(serie.name).Append(PH_NN);
}
for (int i = 0; i < dataIndexList.Count; i++)
{
var dataIndex = dataIndexList[i];
var serieData = serie.GetSerieData(dataIndex);
float xValue, yValue;
serie.GetXYData(dataIndex, null, out xValue, out yValue);
sb.Append("<color=#").Append(themeInfo.GetColorStr(serie.index)).Append(">● </color>");
if (!string.IsNullOrEmpty(serieData.name))
sb.Append(serieData.name).Append(": ");
sb.AppendFormat("({0},{1})", ChartCached.FloatToStr(xValue, 0, tooltip.forceENotation),
ChartCached.FloatToStr(yValue, 0, tooltip.forceENotation));
if (i != dataIndexList.Count - 1)
{
sb.Append("\n");
}
}
}
private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ThemeInfo themeInfo)
@@ -22,7 +66,7 @@ namespace XCharts
sb.Length = 0;
if (!string.IsNullOrEmpty(serie.name))
{
sb.Append(serie.name).Append("\n");
sb.Append(serie.name).Append(PH_NN);
}
sb.Append("<color=#").Append(themeInfo.GetColorStr(index)).Append(">● </color>");
if (!string.IsNullOrEmpty(key))
@@ -60,7 +104,7 @@ namespace XCharts
{
string key = radar.indicatorList[i].name;
float value = serieData.GetData(i);
if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append("\n");
if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append(PH_NN);
sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, 0, tooltip.forceENotation));
}
break;
@@ -110,9 +154,11 @@ namespace XCharts
{
case SerieType.Line:
case SerieType.Bar:
InitCoordinateTooltip(ref sb, tooltip, serie, index, themeInfo, isCartesian, dataZoom);
break;
case SerieType.Scatter:
case SerieType.EffectScatter:
InitCoordinateTooltip(ref sb, tooltip, serie, index, themeInfo, isCartesian, dataZoom);
InitScatterTooltip(ref sb, tooltip, serie, index, themeInfo);
break;
case SerieType.Radar:
break;
@@ -139,60 +185,112 @@ namespace XCharts
var formatTitle = !string.IsNullOrEmpty(title);
var needCategory = false;
var first = true;
var isScatter = false;
sb.Length = 0;
for (int i = 0; i < series.Count; i++)
{
var serie = series.GetSerie(i);
if (!serie.show) continue;
var serieData = serie.GetSerieData(dataIndex, dataZoom);
if (serieData == null) continue;
var itemFormatter = GetItemFormatter(tooltip, serie, serieData);
var percent = serieData.GetData(1) / serie.yTotal * 100;
needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar);
if (serie.show)
if (serie.type == SerieType.Scatter || serie.type == SerieType.EffectScatter)
{
if (string.IsNullOrEmpty(itemFormatter))
if (serie.show && IsSelectedSerie(tooltip, serie.index))
{
if (!first) sb.Append("\n");
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian);
first = false;
continue;
isScatter = true;
var itemFormatter = GetItemFormatter(tooltip, serie, null);
if (string.IsNullOrEmpty(itemFormatter))
{
if (!first) sb.Append(PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian);
first = false;
continue;
}
var itemTitle = title;
if (!string.IsNullOrEmpty(itemTitle))
{
Replace(ref itemTitle, PH_A, i, serie.name, true);
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), 0, tooltip.forceENotation);
Replace(ref content, GetPHCC(n), i, valueStr, true);
}
if (!foundDot)
{
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(serie.index)));
}
sb.Append(content).Append(PH_NN);
}
}
string content = itemFormatter;
content = content.Replace("{a}", serie.name);
content = content.Replace("{b}", needCategory ? category : serieData.name);
content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation));
content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1));
if (!first) sb.Append("\n");
sb.Append("<color=#").Append(themeInfo.GetColorStr(i)).Append(">● </color>");
sb.Append(content);
first = false;
}
if (formatTitle)
else
{
if (i == 0)
var serieData = serie.GetSerieData(dataIndex, dataZoom);
if (serieData == null) continue;
var itemFormatter = GetItemFormatter(tooltip, serie, serieData);
var percent = serieData.GetData(1) / serie.yTotal * 100;
needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar);
if (formatTitle)
{
title = title.Replace("{a}", serie.name);
title = title.Replace("{b}", needCategory ? category : serieData.name);
title = title.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation));
title = title.Replace("{d}", ChartCached.FloatToStr(percent, 1));
var valueStr = ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation);
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, 1), true);
}
if (serie.show)
{
if (string.IsNullOrEmpty(itemFormatter))
{
if (!first) sb.Append(PH_NN);
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian);
first = false;
continue;
}
string content = itemFormatter;
var valueStr = ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation);
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, 1), true);
for (int n = 0; n < serieData.data.Count; n++)
{
valueStr = ChartCached.FloatToStr(serieData.GetData(n), 0, tooltip.forceENotation);
Replace(ref content, GetPHCC(n), i, valueStr, true);
}
if (!first) sb.Append(PH_NN);
sb.Append(ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
sb.Append(content);
first = false;
}
title = title.Replace("{a" + i + "}", serie.name);
title = title.Replace("{b" + i + "}", needCategory ? category : serieData.name);
title = title.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation));
title = title.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1));
}
}
if (string.IsNullOrEmpty(title))
if (isScatter)
{
if (needCategory) return category + "\n" + sb.ToString();
else return sb.ToString();
return TrimAndReplaceLine(sb);
}
else if (string.IsNullOrEmpty(title))
{
if (needCategory) return category + PH_NN + TrimAndReplaceLine(sb);
else return TrimAndReplaceLine(sb);
}
else
{
title = title.Replace("\\n", "\n");
title = title.Replace("<br/>", "\n");
return title + "\n" + sb.ToString();
title = title.Replace(PH_ON, PH_NN);
title = title.Replace(PH_BR, PH_NN);
return title + PH_NN + TrimAndReplaceLine(sb);
}
}
else
@@ -206,25 +304,82 @@ namespace XCharts
var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
var serieData = serie.GetSerieData(dataIndex, dataZoom);
var percent = serieData.GetData(1) / serie.yTotal * 100;
if (i == 0)
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), 0, tooltip.forceENotation));
Replace(ref content, PH_D, i, ChartCached.FloatToStr(percent, 1));
Replace(ref content, PH_I, i, ChartCached.ColorToDotStr(themeInfo.GetColor(i)));
for (int n = 0; n < serieData.data.Count; n++)
{
content = content.Replace("{a}", serie.name);
content = content.Replace("{b}", needCategory ? category : serieData.name);
content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation));
content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1));
var valueStr = ChartCached.FloatToStr(serieData.GetData(n), 0, tooltip.forceENotation);
if (i == 0) Replace(ref content, GetPHCC(n), i, valueStr, true);
Replace(ref content, GetPHCC(i, n), i, valueStr, true);
}
content = content.Replace("{a" + i + "}", serie.name);
content = content.Replace("{b" + i + "}", needCategory ? category : serieData.name);
content = content.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation));
content = content.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1));
}
}
content = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
content = content.Replace(PH_ON, PH_NN);
content = content.Replace(PH_BR, PH_NN);
return content;
}
}
private static string TrimAndReplaceLine(StringBuilder sb)
{
return sb.ToString().Trim().Replace(PH_ON, PH_NN_BBB).Replace(PH_BR, PH_NN_BBB);
}
private static bool IsSelectedSerie(Tooltip tooltip, int serieIndex)
{
if (tooltip.runtimeSerieDataIndex.ContainsKey(serieIndex))
{
return tooltip.runtimeSerieDataIndex[serieIndex].Count > 0;
}
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);

View File

@@ -15,6 +15,7 @@ namespace XCharts
protected void DrawScatterSerie(VertexHelper vh, int colorIndex, Serie serie)
{
if (serie.animation.HasFadeOut()) return;
if (!serie.show) return;
var yAxis = m_YAxises[serie.axisIndex];
var xAxis = m_XAxises[serie.axisIndex];
int maxCount = serie.maxShow > 0 ?
@@ -41,6 +42,7 @@ namespace XCharts
float yDataHig = (yValue - yAxis.runtimeMinValue) / (yAxis.runtimeMaxValue - yAxis.runtimeMinValue) * coordinateHeight;
var pos = new Vector3(pX + xDataHig, pY + yDataHig);
serie.dataPoints.Add(pos);
serieData.runtimePosition = pos;
var datas = serie.data[n].data;
float symbolSize = 0;
if (serie.highlighted || serieData.highlighted)

View File

@@ -64,5 +64,71 @@ namespace XCharts
RefreshChart();
}
}
protected override void CheckTootipArea(Vector2 local)
{
base.CheckTootipArea(local);
m_Tooltip.ClearSerieDataIndex();
bool selected = false;
var localv3 = new Vector3(local.x, local.y);
foreach (var serie in m_Series.list)
{
if (!serie.show) continue;
if (serie.type != SerieType.Scatter && serie.type != SerieType.EffectScatter) continue;
bool refresh = false;
for (int j = 0; j < serie.data.Count; j++)
{
var serieData = serie.data[j];
var dist = Vector3.Distance(local, serieData.runtimePosition);
if (dist <= serie.symbol.size)
{
serieData.selected = true;
m_Tooltip.AddSerieDataIndex(serie.index, j);
selected = true;
}
else
{
serieData.selected = false;
}
}
if (refresh) RefreshChart();
}
if (selected)
{
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
UpdateTooltip();
}
else if (m_Tooltip.IsActive())
{
m_Tooltip.SetActive(false);
RefreshChart();
}
}
protected override void UpdateTooltip()
{
base.UpdateTooltip();
if (m_Tooltip.isAnySerieDataIndex())
{
var content = TooltipHelper.GetFormatterContent(m_Tooltip, 0, m_Series, m_ThemeInfo);
m_Tooltip.UpdateContentText(content);
var pos = m_Tooltip.GetContentPos();
if (pos.x + m_Tooltip.runtimeWidth > chartWidth)
{
pos.x = chartWidth - m_Tooltip.runtimeWidth;
}
if (pos.y - m_Tooltip.runtimeHeight < 0)
{
pos.y = m_Tooltip.runtimeHeight;
}
m_Tooltip.UpdateContentPos(pos);
m_Tooltip.SetActive(true);
}
else
{
m_Tooltip.SetActive(false);
}
}
}
}

View File

@@ -22,11 +22,10 @@ namespace XCharts
private static Dictionary<float, string> s_ValueToEStr = new Dictionary<float, string>(1000);
private static Dictionary<int, string> s_IntToStr = new Dictionary<int, string>(1000);
private static Dictionary<int, string> s_IntToFn = new Dictionary<int, string>(20);
private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(1000);
private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(100);
private static Dictionary<int, string> s_SerieLabelName = new Dictionary<int, string>(1000);
private static Dictionary<int, string> s_AxisLabelName = new Dictionary<int, string>(1000);
private static Dictionary<Color, string> s_ColorDotStr = new Dictionary<Color, string>(100);
public static string FloatToStr(float value, int f = 0, bool forceE = false)
{
@@ -88,6 +87,15 @@ namespace XCharts
}
}
public static string ColorToDotStr(Color color)
{
if (!s_ColorDotStr.ContainsKey(color))
{
s_ColorDotStr[color] = "<color=#" + ColorToStr(color) + ">● </color>";
}
return s_ColorDotStr[color];
}
internal static string GetSerieLabelName(string prefix, int i, int j)
{
int key = i * 10000000 + j;