mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 22:40:10 +00:00
优化Tooltip支持设置不同的类目轴数据 #129
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
|
||||
## Latest
|
||||
|
||||
* (2021.03.30) Optimized `Tooltip` to support setting different category axis data #129
|
||||
* (2021.03.29) Optimized the custom draw callback API
|
||||
* (2021.03.25) Added `Ganttchart`
|
||||
* (2021.03.22) Added `Theme` `Unbind` button to unbind theme when copying chart #118
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
## Latest
|
||||
|
||||
* (2021.03.30) 优化`Tooltip`支持设置不同的类目轴数据 #129
|
||||
* (2021.03.29) 优化自定义绘制回调接口,增加`onCustomDrawBeforeSerie`、`onCustomDrawAfterSerie`和`onCustomDrawTop`
|
||||
* (2021.03.25) 增加`GanttChart`甘特图
|
||||
* (2021.03.22) 增加`Theme`的`Unbind`按钮用于解绑复制图表时的主题 #118
|
||||
|
||||
@@ -38,8 +38,8 @@ namespace XCharts
|
||||
/// <param name="category">选中的类目,一般用在折线图和柱状图</param>
|
||||
/// <param name="dataZoom">dataZoom</param>
|
||||
/// <returns></returns>
|
||||
public static bool ReplaceContent(ref string content, int dataIndex, string numericFormatter, Serie serie, Series series,
|
||||
ChartTheme theme, string category = null, DataZoom dataZoom = null)
|
||||
public static bool ReplaceContent(ref string content, int dataIndex, string numericFormatter, Serie serie,
|
||||
BaseChart chart, DataZoom dataZoom = null)
|
||||
{
|
||||
var foundDot = false;
|
||||
var mc = s_Regex.Matches(content);
|
||||
@@ -53,7 +53,7 @@ namespace XCharts
|
||||
char p = GetSerieIndex(args[0].ToString(), ref targetIndex);
|
||||
if (targetIndex >= 0)
|
||||
{
|
||||
serie = series.GetSerie(targetIndex);
|
||||
serie = chart.series.GetSerie(targetIndex);
|
||||
if (serie == null) continue;
|
||||
}
|
||||
else if (serie != null)
|
||||
@@ -62,7 +62,7 @@ namespace XCharts
|
||||
}
|
||||
else
|
||||
{
|
||||
serie = series.GetSerie(0);
|
||||
serie = chart.series.GetSerie(0);
|
||||
targetIndex = 0;
|
||||
}
|
||||
if (serie == null) continue;
|
||||
@@ -74,7 +74,7 @@ namespace XCharts
|
||||
var args1Str = args[1].ToString();
|
||||
if (s_RegexN.IsMatch(args1Str)) bIndex = int.Parse(args1Str);
|
||||
}
|
||||
content = content.Replace(old, ChartCached.ColorToDotStr(theme.GetColor(bIndex)));
|
||||
content = content.Replace(old, ChartCached.ColorToDotStr(chart.theme.GetColor(bIndex)));
|
||||
foundDot = true;
|
||||
}
|
||||
else if (p == 'a' || p == 'A')
|
||||
@@ -95,6 +95,7 @@ namespace XCharts
|
||||
var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar;
|
||||
if (needCategory)
|
||||
{
|
||||
var category = (chart as CoordinateChart).GetTooltipCategory(dataIndex, serie, dataZoom);
|
||||
content = content.Replace(old, category);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -390,8 +390,7 @@ namespace XCharts
|
||||
UpdateSerieGridIndex();
|
||||
RefreshSeriePainterByGridIndex(grid.index);
|
||||
var category = tempAxis.GetData(index, dataZoom);
|
||||
var content = TooltipHelper.GetFormatterContent(tooltip, index, m_Series, m_Theme, category,
|
||||
dataZoom, isCartesian);
|
||||
var content = TooltipHelper.GetFormatterContent(tooltip, index, this, dataZoom, isCartesian);
|
||||
TooltipHelper.SetContentAndPosition(tooltip, content, chartRect);
|
||||
tooltip.SetActive(true);
|
||||
|
||||
@@ -405,6 +404,51 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
internal string GetTooltipCategory(int dataIndex, DataZoom dataZoom = null)
|
||||
{
|
||||
bool isCartesian = IsValue();
|
||||
var index = -1;
|
||||
Axis tempAxis;
|
||||
if (isCartesian)
|
||||
{
|
||||
index = tooltip.runtimeDataIndex[0];
|
||||
tempAxis = m_XAxes[0];
|
||||
}
|
||||
else if (m_XAxes[0].type == Axis.AxisType.Value)
|
||||
{
|
||||
index = (int)tooltip.runtimeYValues[0];
|
||||
tempAxis = m_YAxes[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
index = (int)tooltip.runtimeXValues[0];
|
||||
tempAxis = m_XAxes[0];
|
||||
}
|
||||
return tempAxis.GetData(index, dataZoom);
|
||||
}
|
||||
internal string GetTooltipCategory(int dataIndex, Serie serie, DataZoom dataZoom = null)
|
||||
{
|
||||
bool isCartesian = IsValue();
|
||||
var index = -1;
|
||||
Axis tempAxis;
|
||||
if (isCartesian)
|
||||
{
|
||||
index = tooltip.runtimeDataIndex[0];
|
||||
tempAxis = GetXAxis(serie.xAxisIndex);
|
||||
}
|
||||
else if (m_XAxes[0].type == Axis.AxisType.Value)
|
||||
{
|
||||
index = (int)tooltip.runtimeYValues[0];
|
||||
tempAxis = GetYAxis(serie.yAxisIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
index = (int)tooltip.runtimeXValues[0];
|
||||
tempAxis = GetXAxis(serie.xAxisIndex);
|
||||
}
|
||||
return tempAxis == null ? "" : tempAxis.GetData(index, dataZoom);
|
||||
}
|
||||
|
||||
protected void UpdateAxisTooltipLabel(int axisIndex, Axis axis)
|
||||
{
|
||||
var showTooltipLabel = axis.show && tooltip.type == Tooltip.Type.Corss;
|
||||
@@ -608,7 +652,7 @@ namespace XCharts
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
var serie = m_Series.GetSerie(i);
|
||||
if(serie.yAxisIndex != axis.index) continue;
|
||||
if (serie.yAxisIndex != axis.index) continue;
|
||||
for (int j = serie.data.Count - 1; j >= 0; j--)
|
||||
{
|
||||
axis.runtimeData.Add(serie.data[j].name);
|
||||
|
||||
@@ -400,7 +400,7 @@ namespace XCharts
|
||||
chart.tooltip.SetActive(false);
|
||||
return;
|
||||
}
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, index, chart.series, chart.theme);
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, index, chart);
|
||||
TooltipHelper.SetContentAndPosition(chart.tooltip, content.TrimStart(), chart.chartRect);
|
||||
chart.tooltip.SetActive(true);
|
||||
}
|
||||
|
||||
@@ -599,7 +599,7 @@ namespace XCharts
|
||||
int index = chart.tooltip.runtimeDataIndex[serie.index];
|
||||
if (index < 0) continue;
|
||||
showTooltip = true;
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, index, chart.series, chart.theme);
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, index, chart);
|
||||
TooltipHelper.SetContentAndPosition(chart.tooltip, content.TrimStart(), chart.chartRect);
|
||||
}
|
||||
chart.tooltip.SetActive(showTooltip);
|
||||
|
||||
@@ -740,8 +740,7 @@ namespace XCharts
|
||||
var serie = chart.series.GetSerie(serieIndex);
|
||||
var radar = chart.radars[serie.radarIndex];
|
||||
var dataIndex = chart.tooltip.runtimeDataIndex[1];
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, dataIndex, chart.series, chart.theme,
|
||||
null, null, false, radar);
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, dataIndex, chart, null, false, radar);
|
||||
TooltipHelper.SetContentAndPosition(chart.tooltip, content, chart.chartRect);
|
||||
}
|
||||
|
||||
|
||||
@@ -352,7 +352,7 @@ namespace XCharts
|
||||
int index = chart.tooltip.runtimeDataIndex[serie.index];
|
||||
if (index < 0) continue;
|
||||
showTooltip = true;
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, index, chart.series, chart.theme);
|
||||
var content = TooltipHelper.GetFormatterContent(chart.tooltip, index, chart);
|
||||
TooltipHelper.SetContentAndPosition(chart.tooltip, content.TrimStart(), chart.chartRect);
|
||||
}
|
||||
chart.tooltip.SetActive(showTooltip);
|
||||
|
||||
@@ -233,7 +233,7 @@ namespace XCharts
|
||||
}
|
||||
|
||||
private static void InitGanttTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
||||
ChartTheme theme, string category)
|
||||
ChartTheme theme)
|
||||
{
|
||||
//if (tooltip.runtimeGridIndex >= 0) return;
|
||||
//if (serie.index != index || serie.type != SerieType.Gantt) return;
|
||||
@@ -241,7 +241,7 @@ namespace XCharts
|
||||
}
|
||||
|
||||
private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
||||
string category, ChartTheme theme = null, DataZoom dataZoom = null, bool isCartesian = false,
|
||||
ChartTheme theme = null, DataZoom dataZoom = null, bool isCartesian = false,
|
||||
Radar radar = null)
|
||||
{
|
||||
switch (serie.type)
|
||||
@@ -270,7 +270,7 @@ namespace XCharts
|
||||
InitGaugeTooltip(ref sb, tooltip, serie, index, theme);
|
||||
break;
|
||||
case SerieType.Gantt:
|
||||
InitGanttTooltip(ref sb, tooltip, serie, index, theme, category);
|
||||
InitGanttTooltip(ref sb, tooltip, serie, index, theme);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -290,7 +290,7 @@ namespace XCharts
|
||||
tooltip.UpdateContentPos(pos);
|
||||
}
|
||||
|
||||
public static string GetPolarFormatterContent(Tooltip tooltip, Series series, ChartTheme theme, AngleAxis angleAxis)
|
||||
public static string GetPolarFormatterContent(Tooltip tooltip, BaseChart chart, AngleAxis angleAxis)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tooltip.formatter))
|
||||
{
|
||||
@@ -308,13 +308,13 @@ namespace XCharts
|
||||
var angle = angleAxis.clockwise ? tooltip.runtimeAngle : 360 - tooltip.runtimeAngle;
|
||||
title = ChartCached.FloatToStr(angle);
|
||||
}
|
||||
foreach (var serie in series.list)
|
||||
foreach (var serie in chart.series.list)
|
||||
{
|
||||
if (serie.show && IsSelectedSerie(tooltip, serie.index))
|
||||
{
|
||||
if (formatTitle)
|
||||
{
|
||||
FormatterHelper.ReplaceContent(ref title, 0, tooltip.numericFormatter, serie, series, theme, null, null);
|
||||
FormatterHelper.ReplaceContent(ref title, 0, tooltip.numericFormatter, serie, chart, null);
|
||||
}
|
||||
var dataIndexList = tooltip.runtimeSerieIndex[serie.index];
|
||||
|
||||
@@ -328,7 +328,7 @@ namespace XCharts
|
||||
serie.GetXYData(dataIndex, null, out xValue, out yValue);
|
||||
if (string.IsNullOrEmpty(itemFormatter))
|
||||
{
|
||||
sb.Append("<color=#").Append(theme.GetColorStr(serie.index)).Append(">● </color>");
|
||||
sb.Append("<color=#").Append(chart.theme.GetColorStr(serie.index)).Append(">● </color>");
|
||||
if (!string.IsNullOrEmpty(serie.name))
|
||||
sb.Append(serie.name).Append(": ");
|
||||
sb.AppendFormat("{0}", ChartCached.FloatToStr(xValue, numericFormatter));
|
||||
@@ -340,9 +340,10 @@ namespace XCharts
|
||||
else
|
||||
{
|
||||
string content = itemFormatter;
|
||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, series, theme, null, null);
|
||||
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar || serie.type == SerieType.Ring ? dataIndex : serie.index;
|
||||
sb.Append(ChartCached.ColorToDotStr(theme.GetColor(dotColorIndex)));
|
||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, chart, null);
|
||||
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar
|
||||
|| serie.type == SerieType.Ring ? dataIndex : serie.index;
|
||||
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(dotColorIndex)));
|
||||
sb.Append(content);
|
||||
}
|
||||
}
|
||||
@@ -362,13 +363,13 @@ namespace XCharts
|
||||
else
|
||||
{
|
||||
string content = tooltip.formatter;
|
||||
FormatterHelper.ReplaceContent(ref content, 0, tooltip.numericFormatter, null, series, theme, null, null);
|
||||
FormatterHelper.ReplaceContent(ref content, 0, tooltip.numericFormatter, null, chart, null);
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetFormatterContent(Tooltip tooltip, int dataIndex, Series series, ChartTheme theme,
|
||||
string category = null, DataZoom dataZoom = null, bool isCartesian = false, Radar radar = null)
|
||||
public static string GetFormatterContent(Tooltip tooltip, int dataIndex, BaseChart chart, DataZoom dataZoom = null,
|
||||
bool isCartesian = false, Radar radar = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tooltip.formatter))
|
||||
{
|
||||
@@ -386,9 +387,9 @@ namespace XCharts
|
||||
formatTitle = false;
|
||||
titleIsIgnroe = true;
|
||||
}
|
||||
for (int i = 0; i < series.Count; i++)
|
||||
for (int i = 0; i < chart.series.Count; i++)
|
||||
{
|
||||
var serie = series.GetSerie(i);
|
||||
var serie = chart.series.GetSerie(i);
|
||||
if (tooltip.runtimeGridIndex >= 0 && serie.runtimeGridIndex != tooltip.runtimeGridIndex) continue;
|
||||
if (serie.type == SerieType.Scatter || serie.type == SerieType.EffectScatter)
|
||||
{
|
||||
@@ -399,24 +400,24 @@ namespace XCharts
|
||||
if (string.IsNullOrEmpty(itemFormatter))
|
||||
{
|
||||
if (!first) sb.Append(FormatterHelper.PH_NN);
|
||||
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, theme, dataZoom, isCartesian, radar);
|
||||
InitDefaultContent(ref sb, tooltip, serie, dataIndex, chart.theme, dataZoom, isCartesian, radar);
|
||||
first = false;
|
||||
continue;
|
||||
}
|
||||
var itemTitle = title;
|
||||
if (!string.IsNullOrEmpty(itemTitle))
|
||||
{
|
||||
FormatterHelper.ReplaceContent(ref itemTitle, dataIndex, tooltip.numericFormatter, serie, series, theme, category, dataZoom);
|
||||
FormatterHelper.ReplaceContent(ref itemTitle, dataIndex, tooltip.numericFormatter, serie, chart, dataZoom);
|
||||
sb.Append(itemTitle).Append(FormatterHelper.PH_NN);
|
||||
}
|
||||
var dataIndexList = tooltip.runtimeSerieIndex[serie.index];
|
||||
foreach (var tempIndex in dataIndexList)
|
||||
{
|
||||
string content = itemFormatter;
|
||||
var foundDot = FormatterHelper.ReplaceContent(ref content, tempIndex, tooltip.numericFormatter, serie, series, theme, category, dataZoom);
|
||||
var foundDot = FormatterHelper.ReplaceContent(ref content, tempIndex, tooltip.numericFormatter, serie, chart, dataZoom);
|
||||
if (!foundDot)
|
||||
{
|
||||
sb.Append(ChartCached.ColorToDotStr(theme.GetColor(serie.index)));
|
||||
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(serie.index)));
|
||||
}
|
||||
sb.Append(content).Append(FormatterHelper.PH_NN);
|
||||
}
|
||||
@@ -440,22 +441,23 @@ namespace XCharts
|
||||
needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar);
|
||||
if (formatTitle)
|
||||
{
|
||||
FormatterHelper.ReplaceContent(ref title, dataIndex, tooltip.numericFormatter, serie, series, theme, category, dataZoom);
|
||||
FormatterHelper.ReplaceContent(ref title, dataIndex, tooltip.numericFormatter, serie, chart, dataZoom);
|
||||
}
|
||||
if (serie.show)
|
||||
{
|
||||
if (string.IsNullOrEmpty(itemFormatter) || serie.type == SerieType.Radar)
|
||||
{
|
||||
if (!first) sb.Append(FormatterHelper.PH_NN);
|
||||
InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, theme, dataZoom, isCartesian, radar);
|
||||
InitDefaultContent(ref sb, tooltip, serie, dataIndex, chart.theme, dataZoom, isCartesian, radar);
|
||||
first = false;
|
||||
continue;
|
||||
}
|
||||
string content = itemFormatter;
|
||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, series, theme, category, dataZoom);
|
||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, chart, dataZoom);
|
||||
if (!first) sb.Append(FormatterHelper.PH_NN);
|
||||
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar || serie.type == SerieType.Ring ? dataIndex : i;
|
||||
sb.Append(ChartCached.ColorToDotStr(theme.GetColor(dotColorIndex)));
|
||||
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar
|
||||
|| serie.type == SerieType.Ring ? dataIndex : i;
|
||||
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(dotColorIndex)));
|
||||
sb.Append(content);
|
||||
first = false;
|
||||
}
|
||||
@@ -468,7 +470,10 @@ namespace XCharts
|
||||
else if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
if (needCategory && !titleIsIgnroe)
|
||||
{
|
||||
var category = (chart as CoordinateChart).GetTooltipCategory(dataIndex, dataZoom);
|
||||
return category + FormatterHelper.PH_NN + FormatterHelper.TrimAndReplaceLine(sb);
|
||||
}
|
||||
else
|
||||
return FormatterHelper.TrimAndReplaceLine(sb);
|
||||
}
|
||||
@@ -481,7 +486,7 @@ namespace XCharts
|
||||
else
|
||||
{
|
||||
string content = tooltip.formatter;
|
||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, null, series, theme, category, dataZoom);
|
||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, null, chart, dataZoom);
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -676,7 +676,7 @@ namespace XCharts
|
||||
if (showTooltip)
|
||||
{
|
||||
var m_AngleAxis = GetAngleAxis(tooltip.runtimePolarIndex);
|
||||
var content = TooltipHelper.GetPolarFormatterContent(tooltip, m_Series, m_Theme, m_AngleAxis);
|
||||
var content = TooltipHelper.GetPolarFormatterContent(tooltip, this, m_AngleAxis);
|
||||
TooltipHelper.SetContentAndPosition(tooltip, content, chartRect);
|
||||
UdpateTooltipLabel();
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace XCharts
|
||||
base.UpdateTooltip();
|
||||
if (tooltip.isAnySerieDataIndex())
|
||||
{
|
||||
var content = TooltipHelper.GetFormatterContent(tooltip, 0, m_Series, m_Theme);
|
||||
var content = TooltipHelper.GetFormatterContent(tooltip, 0, this);
|
||||
TooltipHelper.SetContentAndPosition(tooltip, content, chartRect);
|
||||
tooltip.SetActive(true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user