Skip to content

Commit

Permalink
support tooltip formatter for HeatmapChart
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed Aug 15, 2022
1 parent d94e304 commit 8e30a37
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
65 changes: 37 additions & 28 deletions Assets/XCharts/Runtime/HeatmapChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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<string>{"#313695", "#4575b4", "#74add1", "#abd9e9", "#e0f3f8", "#ffffbf",
"#fee090", "#fdae61", "#f46d43", "#d73027", "#a50026"};
var colors = new List<string>
{
"#313695",
"#4575b4",
"#74add1",
"#abd9e9",
"#e0f3f8",
"#ffffbf",
"#fee090",
"#fdae61",
"#f46d43",
"#d73027",
"#a50026"
};
visualMap.inRange.Clear();
foreach (var str in colors)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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("<color=#").Append(ChartCached.ColorToStr(color)).Append(">● </color>")
.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;
}
}
}
}
45 changes: 37 additions & 8 deletions Assets/XCharts/Runtime/Helper/TooltipHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ 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");
}
}
}


private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
ChartTheme theme)
{
Expand Down Expand Up @@ -72,7 +71,7 @@ private static void InitRingTooltip(ref StringBuilder sb, Tooltip tooltip, Serie
if (!string.IsNullOrEmpty(serieData.name))
{
sb.Append("<color=#").Append(theme.GetColorStr(index)).Append(">● </color>")
.Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, numericFormatter));
.Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, numericFormatter));
}
else
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("<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,
BaseChart chart, DataZoom dataZoom = null, bool isCartesian = false,
Radar radar = null)
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8e30a37

Please sign in to comment.