mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-20 07:20:08 +00:00
support tooltip formatter for HeatmapChart
This commit is contained in:
@@ -35,8 +35,8 @@ namespace XCharts
|
||||
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 @@ namespace XCharts
|
||||
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)
|
||||
{
|
||||
@@ -80,11 +92,27 @@ namespace XCharts
|
||||
#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 @@ namespace XCharts
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ namespace XCharts
|
||||
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 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index,
|
||||
ChartTheme theme)
|
||||
{
|
||||
@@ -72,7 +71,7 @@ namespace XCharts
|
||||
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
|
||||
{
|
||||
@@ -196,7 +195,7 @@ namespace XCharts
|
||||
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 @@ 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,
|
||||
BaseChart chart, DataZoom dataZoom = null, bool isCartesian = false,
|
||||
Radar radar = null)
|
||||
@@ -259,6 +287,7 @@ namespace XCharts
|
||||
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 @@ namespace XCharts
|
||||
{
|
||||
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 @@ namespace XCharts
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user