mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-30 05:08:48 +00:00
support tooltip formatter for HeatmapChart
This commit is contained in:
@@ -35,8 +35,8 @@ namespace XCharts
|
|||||||
m_YAxes[0].splitNumber = 10;
|
m_YAxes[0].splitNumber = 10;
|
||||||
RemoveData();
|
RemoveData();
|
||||||
var heatmapGridWid = 10f;
|
var heatmapGridWid = 10f;
|
||||||
int xSplitNumber = (int)(grid.runtimeWidth / heatmapGridWid);
|
int xSplitNumber = (int) (grid.runtimeWidth / heatmapGridWid);
|
||||||
int ySplitNumber = (int)(grid.runtimeHeight / heatmapGridWid);
|
int ySplitNumber = (int) (grid.runtimeHeight / heatmapGridWid);
|
||||||
|
|
||||||
SerieTemplate.AddDefaultHeatmapSerie(this, "serie1");
|
SerieTemplate.AddDefaultHeatmapSerie(this, "serie1");
|
||||||
|
|
||||||
@@ -49,8 +49,20 @@ namespace XCharts
|
|||||||
visualMap.location.align = Location.Align.BottomLeft;
|
visualMap.location.align = Location.Align.BottomLeft;
|
||||||
visualMap.location.bottom = 100;
|
visualMap.location.bottom = 100;
|
||||||
visualMap.location.left = 30;
|
visualMap.location.left = 30;
|
||||||
var colors = new List<string>{"#313695", "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#ffffbf",
|
var colors = new List<string>
|
||||||
"#fee090", "#fdae61", "#f46d43", "#d73027", "#a50026"};
|
{
|
||||||
|
"#313695",
|
||||||
|
"#4575b4",
|
||||||
|
"#74add1",
|
||||||
|
"#abd9e9",
|
||||||
|
"#e0f3f8",
|
||||||
|
"#ffffbf",
|
||||||
|
"#fee090",
|
||||||
|
"#fdae61",
|
||||||
|
"#f46d43",
|
||||||
|
"#d73027",
|
||||||
|
"#a50026"
|
||||||
|
};
|
||||||
visualMap.inRange.Clear();
|
visualMap.inRange.Clear();
|
||||||
foreach (var str in colors)
|
foreach (var str in colors)
|
||||||
{
|
{
|
||||||
@@ -80,11 +92,27 @@ namespace XCharts
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected override void UpdateTooltip()
|
protected override void UpdateTooltip()
|
||||||
|
{
|
||||||
|
var dataIndex = GetDataIndex();
|
||||||
|
if (dataIndex < 0) return;
|
||||||
|
var content = TooltipHelper.GetFormatterContent(tooltip, dataIndex, this);
|
||||||
|
TooltipHelper.SetContentAndPosition(tooltip, content, chartRect);
|
||||||
|
tooltip.SetActive(true);
|
||||||
|
for (int i = 0; i < m_XAxes.Count; i++)
|
||||||
|
{
|
||||||
|
UpdateAxisTooltipLabel(i, m_XAxes[i]);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < m_YAxes.Count; i++)
|
||||||
|
{
|
||||||
|
UpdateAxisTooltipLabel(i, m_YAxes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetDataIndex()
|
||||||
{
|
{
|
||||||
var xData = tooltip.runtimeXValues[0];
|
var xData = tooltip.runtimeXValues[0];
|
||||||
var yData = tooltip.runtimeYValues[0];
|
var yData = tooltip.runtimeYValues[0];
|
||||||
if (IsCategory() && (xData < 0 || yData < 0)) return;
|
if (IsCategory() && (xData < 0 || yData < 0)) return -1;
|
||||||
sb.Length = 0;
|
|
||||||
for (int i = 0; i < m_Series.Count; i++)
|
for (int i = 0; i < m_Series.Count; i++)
|
||||||
{
|
{
|
||||||
var serie = m_Series.GetSerie(i);
|
var serie = m_Series.GetSerie(i);
|
||||||
@@ -96,30 +124,11 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
if (IsCategory())
|
if (IsCategory())
|
||||||
{
|
{
|
||||||
string key = serie.name;
|
return (int) xData * yCount + (int) yData;
|
||||||
var serieData = serie.data[(int)xData * yCount + (int)yData];
|
|
||||||
var value = serieData.data[2];
|
|
||||||
var color = visualMap.enable ? visualMap.GetColor(value) :
|
|
||||||
m_Theme.GetColor(serie.index);
|
|
||||||
sb.Append("\n")
|
|
||||||
.Append(key).Append(!string.IsNullOrEmpty(key) ? "\n" : "")
|
|
||||||
.Append("<color=#").Append(ChartCached.ColorToStr(color)).Append(">● </color>")
|
|
||||||
.Append(xAxis.data[(int)xData]).Append(": ")
|
|
||||||
.Append(ChartCached.FloatToStr(value, string.Empty));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TooltipHelper.SetContentAndPosition(tooltip, sb.ToString().Trim(), chartRect);
|
return -1;
|
||||||
tooltip.SetActive(true);
|
|
||||||
|
|
||||||
for (int i = 0; i < m_XAxes.Count; i++)
|
|
||||||
{
|
|
||||||
UpdateAxisTooltipLabel(i, m_XAxes[i]);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < m_YAxes.Count; i++)
|
|
||||||
{
|
|
||||||
UpdateAxisTooltipLabel(i, m_YAxes[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,6 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
||||||
ChartTheme theme)
|
ChartTheme theme)
|
||||||
{
|
{
|
||||||
@@ -196,7 +195,7 @@ namespace XCharts
|
|||||||
double xValue, yValue;
|
double xValue, yValue;
|
||||||
serie.GetXYData(index, dataZoom, out xValue, out yValue);
|
serie.GetXYData(index, dataZoom, out xValue, out yValue);
|
||||||
var isIngore = serie.IsIgnorePoint(index);
|
var isIngore = serie.IsIgnorePoint(index);
|
||||||
if(isIngore) return;
|
if (isIngore) return;
|
||||||
var serieData = serie.GetSerieData(index, dataZoom);
|
var serieData = serie.GetSerieData(index, dataZoom);
|
||||||
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
|
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
|
||||||
if (isCartesian)
|
if (isCartesian)
|
||||||
@@ -234,6 +233,35 @@ namespace XCharts
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void InitHeatmapTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
||||||
|
CoordinateChart chart)
|
||||||
|
{
|
||||||
|
if (serie.type != SerieType.Heatmap) return;
|
||||||
|
var xData = tooltip.runtimeXValues[0];
|
||||||
|
var yData = tooltip.runtimeYValues[0];
|
||||||
|
if (chart.IsCategory() && (xData < 0 || yData < 0)) return;
|
||||||
|
sb.Length = 0;
|
||||||
|
var xAxis = chart.GetXAxis(serie.xAxisIndex);
|
||||||
|
var yAxis = chart.GetYAxis(serie.yAxisIndex);
|
||||||
|
var xCount = xAxis.data.Count;
|
||||||
|
var yCount = yAxis.data.Count;
|
||||||
|
var visualMap = chart.visualMap;
|
||||||
|
if (chart.IsCategory())
|
||||||
|
{
|
||||||
|
string key = serie.name;
|
||||||
|
var serieData = serie.data[(int) xData * yCount + (int) yData];
|
||||||
|
var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData);
|
||||||
|
var value = serieData.data[2];
|
||||||
|
var color = visualMap.enable ? visualMap.GetColor(value) :
|
||||||
|
chart.theme.GetColor(serie.index);
|
||||||
|
sb.Append("\n")
|
||||||
|
.Append(key).Append(!string.IsNullOrEmpty(key) ? "\n" : "")
|
||||||
|
.Append("<color=#").Append(ChartCached.ColorToStr(color)).Append(">● </color>")
|
||||||
|
.Append(xAxis.data[(int) xData]).Append(": ")
|
||||||
|
.Append(ChartCached.FloatToStr(value, numericFormatter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
||||||
BaseChart chart, DataZoom dataZoom = null, bool isCartesian = false,
|
BaseChart chart, DataZoom dataZoom = null, bool isCartesian = false,
|
||||||
Radar radar = null)
|
Radar radar = null)
|
||||||
@@ -259,6 +287,7 @@ namespace XCharts
|
|||||||
InitRingTooltip(ref sb, tooltip, serie, index, chart.theme);
|
InitRingTooltip(ref sb, tooltip, serie, index, chart.theme);
|
||||||
break;
|
break;
|
||||||
case SerieType.Heatmap:
|
case SerieType.Heatmap:
|
||||||
|
InitHeatmapTooltip(ref sb, tooltip, serie, index, chart as CoordinateChart);
|
||||||
break;
|
break;
|
||||||
case SerieType.Gauge:
|
case SerieType.Gauge:
|
||||||
InitGaugeTooltip(ref sb, tooltip, serie, index, chart.theme);
|
InitGaugeTooltip(ref sb, tooltip, serie, index, chart.theme);
|
||||||
@@ -335,8 +364,8 @@ namespace XCharts
|
|||||||
{
|
{
|
||||||
string content = itemFormatter;
|
string content = itemFormatter;
|
||||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, chart, null);
|
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, chart, null);
|
||||||
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar
|
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar ||
|
||||||
|| serie.type == SerieType.Ring ? dataIndex : serie.index;
|
serie.type == SerieType.Ring ? dataIndex : serie.index;
|
||||||
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(dotColorIndex)));
|
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(dotColorIndex)));
|
||||||
sb.Append(content);
|
sb.Append(content);
|
||||||
}
|
}
|
||||||
@@ -449,8 +478,8 @@ namespace XCharts
|
|||||||
string content = itemFormatter;
|
string content = itemFormatter;
|
||||||
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, chart, dataZoom);
|
FormatterHelper.ReplaceContent(ref content, dataIndex, tooltip.numericFormatter, serie, chart, dataZoom);
|
||||||
if (!first) sb.Append(FormatterHelper.PH_NN);
|
if (!first) sb.Append(FormatterHelper.PH_NN);
|
||||||
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar
|
var dotColorIndex = serie.type == SerieType.Pie || serie.type == SerieType.Radar ||
|
||||||
|| serie.type == SerieType.Ring ? dataIndex : i;
|
serie.type == SerieType.Ring ? dataIndex : i;
|
||||||
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(dotColorIndex)));
|
sb.Append(ChartCached.ColorToDotStr(chart.theme.GetColor(dotColorIndex)));
|
||||||
sb.Append(content);
|
sb.Append(content);
|
||||||
first = false;
|
first = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user