From 8e30a370b575bb406671f4fb635f32d7244a770f Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Mon, 15 Aug 2022 22:11:36 +0800 Subject: [PATCH] support tooltip formatter for HeatmapChart --- Assets/XCharts/Runtime/HeatmapChart.cs | 65 +++++++++++-------- .../XCharts/Runtime/Helper/TooltipHelper.cs | 45 ++++++++++--- 2 files changed, 74 insertions(+), 36 deletions(-) diff --git a/Assets/XCharts/Runtime/HeatmapChart.cs b/Assets/XCharts/Runtime/HeatmapChart.cs index b59318d0..05f1a71c 100644 --- a/Assets/XCharts/Runtime/HeatmapChart.cs +++ b/Assets/XCharts/Runtime/HeatmapChart.cs @@ -35,8 +35,8 @@ protected override void Reset() m_YAxes[0].splitNumber = 10; RemoveData(); var heatmapGridWid = 10f; - int xSplitNumber = (int)(grid.runtimeWidth / heatmapGridWid); - int ySplitNumber = (int)(grid.runtimeHeight / heatmapGridWid); + int xSplitNumber = (int) (grid.runtimeWidth / heatmapGridWid); + int ySplitNumber = (int) (grid.runtimeHeight / heatmapGridWid); SerieTemplate.AddDefaultHeatmapSerie(this, "serie1"); @@ -49,8 +49,20 @@ protected override void Reset() visualMap.location.align = Location.Align.BottomLeft; visualMap.location.bottom = 100; visualMap.location.left = 30; - var colors = new List{"#313695", "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#ffffbf", - "#fee090", "#fdae61", "#f46d43", "#d73027", "#a50026"}; + var colors = new List + { + "#313695", + "#4575b4", + "#74add1", + "#abd9e9", + "#e0f3f8", + "#ffffbf", + "#fee090", + "#fdae61", + "#f46d43", + "#d73027", + "#a50026" + }; visualMap.inRange.Clear(); foreach (var str in colors) { @@ -80,11 +92,27 @@ protected override void Reset() #endif 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 yData = tooltip.runtimeYValues[0]; - if (IsCategory() && (xData < 0 || yData < 0)) return; - sb.Length = 0; + if (IsCategory() && (xData < 0 || yData < 0)) return -1; for (int i = 0; i < m_Series.Count; i++) { var serie = m_Series.GetSerie(i); @@ -96,30 +124,11 @@ protected override void UpdateTooltip() { if (IsCategory()) { - string key = serie.name; - 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("● ") - .Append(xAxis.data[(int)xData]).Append(": ") - .Append(ChartCached.FloatToStr(value, string.Empty)); + return (int) xData * yCount + (int) yData; } } } - TooltipHelper.SetContentAndPosition(tooltip, sb.ToString().Trim(), 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]); - } + return -1; } } -} +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Helper/TooltipHelper.cs b/Assets/XCharts/Runtime/Helper/TooltipHelper.cs index 8646bdfa..c15174e8 100644 --- a/Assets/XCharts/Runtime/Helper/TooltipHelper.cs +++ b/Assets/XCharts/Runtime/Helper/TooltipHelper.cs @@ -33,7 +33,7 @@ private static void InitScatterTooltip(ref StringBuilder sb, Tooltip tooltip, Se if (!string.IsNullOrEmpty(serieData.name)) sb.Append(serieData.name).Append(": "); sb.AppendFormat("({0},{1})", ChartCached.FloatToStr(xValue, numericFormatter), - ChartCached.FloatToStr(yValue, numericFormatter)); + ChartCached.FloatToStr(yValue, numericFormatter)); if (i != dataIndexList.Count - 1) { sb.Append("\n"); @@ -41,7 +41,6 @@ private static void InitScatterTooltip(ref StringBuilder sb, Tooltip tooltip, Se } } - private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index, ChartTheme theme) { @@ -72,7 +71,7 @@ private static void InitRingTooltip(ref StringBuilder sb, Tooltip tooltip, Serie if (!string.IsNullOrEmpty(serieData.name)) { sb.Append("● ") - .Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, numericFormatter)); + .Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, numericFormatter)); } else { @@ -196,7 +195,7 @@ public static void InitCoordinateTooltip(ref StringBuilder sb, Tooltip tooltip, double xValue, yValue; serie.GetXYData(index, dataZoom, out xValue, out yValue); var isIngore = serie.IsIgnorePoint(index); - if(isIngore) return; + if (isIngore) return; var serieData = serie.GetSerieData(index, dataZoom); var numericFormatter = GetItemNumericFormatter(tooltip, serie, serieData); if (isCartesian) @@ -234,6 +233,35 @@ public static void InitCoordinateTooltip(ref StringBuilder sb, Tooltip tooltip, } } + 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("● ") + .Append(xAxis.data[(int) xData]).Append(": ") + .Append(ChartCached.FloatToStr(value, numericFormatter)); + } + } + private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index, BaseChart chart, DataZoom dataZoom = null, bool isCartesian = false, Radar radar = null) @@ -259,6 +287,7 @@ private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Se InitRingTooltip(ref sb, tooltip, serie, index, chart.theme); break; case SerieType.Heatmap: + InitHeatmapTooltip(ref sb, tooltip, serie, index, chart as CoordinateChart); break; case SerieType.Gauge: InitGaugeTooltip(ref sb, tooltip, serie, index, chart.theme); @@ -335,8 +364,8 @@ public static string GetPolarFormatterContent(Tooltip tooltip, BaseChart chart, { string content = itemFormatter; 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; + 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); } @@ -449,8 +478,8 @@ public static string GetFormatterContent(Tooltip tooltip, int dataIndex, BaseCha string content = itemFormatter; 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; + 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;