From 0e22dcbcdb56415f65d8910c53aaa487ab52afa0 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sat, 21 Mar 2020 11:26:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`ItemStyle`=E7=9A=84`tooltipF?= =?UTF-8?q?ormatter`=E5=8F=82=E6=95=B0=E5=8F=AF=E5=8D=95=E7=8B=AC=E9=85=8D?= =?UTF-8?q?=E7=BD=AE`Serie`=E7=9A=84`Tooltip`=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/XCharts/CHANGELOG.md | 1 + .../Documentation/XCharts配置项手册.md | 3 +- .../Editor/PropertyDrawers/ItemStyleDrawer.cs | 5 +- .../XCharts/Runtime/Component/Main/Tooltip.cs | 125 --------- .../Runtime/Component/Sub/ItemStyle.cs | 9 + .../XCharts/Runtime/Helper/TooltipHelper.cs | 250 ++++++++++++++++++ .../Runtime/Helper/TooltipHelper.cs.meta | 11 + .../Runtime/Internal/CoordinateChart.cs | 66 +---- Assets/XCharts/Runtime/PieChart.cs | 22 +- Assets/XCharts/Runtime/RadarChart.cs | 25 +- Assets/XCharts/Runtime/RingChart.cs | 22 +- 11 files changed, 292 insertions(+), 247 deletions(-) create mode 100644 Assets/XCharts/Runtime/Helper/TooltipHelper.cs create mode 100644 Assets/XCharts/Runtime/Helper/TooltipHelper.cs.meta diff --git a/Assets/XCharts/CHANGELOG.md b/Assets/XCharts/CHANGELOG.md index ce1d7a87..ac3a2181 100644 --- a/Assets/XCharts/CHANGELOG.md +++ b/Assets/XCharts/CHANGELOG.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.03.21) 增加`ItemStyle`的`tooltipFormatter`参数可单独配置`Serie`的`Tooltip`显示 * (2020.03.20) 修复`X Axis 1`和`Y Axis 1`配置变更时不会自动刷新的问题 * (2020.03.20) 增加`AxisTick`的`width`参数可单独设置坐标轴刻度的宽度 * (2020.03.20) 增加`Serie`的`radarType`参数设置`多圈`和`单圈`雷达图 diff --git a/Assets/XCharts/Documentation/XCharts配置项手册.md b/Assets/XCharts/Documentation/XCharts配置项手册.md index 1a7c4c7a..caa9c4c4 100644 --- a/Assets/XCharts/Documentation/XCharts配置项手册.md +++ b/Assets/XCharts/Documentation/XCharts配置项手册.md @@ -36,7 +36,7 @@ * [AxisSplitArea 坐标轴分割区域](#AxisSplitArea) * [AxisTick 坐标轴刻度](#AxisTick) * [Emphasis 高亮样式](#Emphasis) -* [ItemStyle 图形样式](#ItemStyle) +* [ItemStyle 数据项样式](#ItemStyle) * [LineArrow 折线图箭头](#LineArrow) * [LineStyle 折线图样式](#LineStyle) * [Location 位置](#Location) @@ -740,6 +740,7 @@ * `borderColor`:边框的颜色。 * `borderWidth`:边框宽。 * `opacity`:透明度。 +* `tooltipFormatter`:提示框单项的字符串模版格式器。具体配置参考`Tooltip`的`formatter`。 ## `LineArrow` diff --git a/Assets/XCharts/Editor/PropertyDrawers/ItemStyleDrawer.cs b/Assets/XCharts/Editor/PropertyDrawers/ItemStyleDrawer.cs index 0976aafd..be3eafa3 100644 --- a/Assets/XCharts/Editor/PropertyDrawers/ItemStyleDrawer.cs +++ b/Assets/XCharts/Editor/PropertyDrawers/ItemStyleDrawer.cs @@ -31,6 +31,7 @@ namespace XCharts SerializedProperty m_BorderWidth = prop.FindPropertyRelative("m_BorderWidth"); SerializedProperty m_BorderColor = prop.FindPropertyRelative("m_BorderColor"); SerializedProperty m_Opacity = prop.FindPropertyRelative("m_Opacity"); + SerializedProperty m_TooltipFormatter = prop.FindPropertyRelative("m_TooltipFormatter"); ChartEditorHelper.MakeFoldout(ref drawRect, ref m_ItemStyleToggle, prop, "Item Style", show, false); drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; if (ChartEditorHelper.IsToggle(m_ItemStyleToggle, prop)) @@ -56,6 +57,8 @@ namespace XCharts drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(drawRect, m_Opacity); drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + EditorGUI.PropertyField(drawRect, m_TooltipFormatter); + drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; --EditorGUI.indentLevel; } } @@ -65,7 +68,7 @@ namespace XCharts float height = 0; if (ChartEditorHelper.IsToggle(m_ItemStyleToggle, prop)) { - height += 11 * EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing; + height += 12 * EditorGUIUtility.singleLineHeight + 11 * EditorGUIUtility.standardVerticalSpacing; } else { diff --git a/Assets/XCharts/Runtime/Component/Main/Tooltip.cs b/Assets/XCharts/Runtime/Component/Main/Tooltip.cs index 72600a0c..b19f0e65 100644 --- a/Assets/XCharts/Runtime/Component/Main/Tooltip.cs +++ b/Assets/XCharts/Runtime/Component/Main/Tooltip.cs @@ -450,130 +450,5 @@ namespace XCharts { return runtimeDataIndex[0] == index || runtimeDataIndex[1] == index; } - - public bool IsNoFormatter() - { - return string.IsNullOrEmpty(m_Formatter) && string.IsNullOrEmpty(m_ItemFormatter); - } - - internal string GetFormatterContent(int dataIndex, Series series, string category, ThemeInfo themeInfo = null, DataZoom dataZoom = null) - { - if (string.IsNullOrEmpty(m_Formatter)) - { - if (string.IsNullOrEmpty(m_ItemFormatter)) return ""; - else - { - var sb = ChartHelper.sb; - var title = m_TitleFormatter; - var formatTitle = !string.IsNullOrEmpty(title); - var needCategory = false; - var first = true; - sb.Length = 0; - for (int i = 0; i < series.Count; i++) - { - var serie = series.GetSerie(i); - var serieData = serie.GetSerieData(dataIndex, dataZoom); - var percent = serieData.GetData(1) / serie.yTotal * 100; - needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar); - if (serie.show) - { - string content = m_ItemFormatter; - content = content.Replace("{a}", serie.name); - content = content.Replace("{b}", needCategory ? category : serieData.name); - content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation)); - content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1)); - if (!first) sb.Append("\n"); - sb.Append("● "); - sb.Append(content); - first = false; - } - if (formatTitle) - { - if (i == 0) - { - title = title.Replace("{a}", serie.name); - title = title.Replace("{b}", needCategory ? category : serieData.name); - title = title.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation)); - title = title.Replace("{d}", ChartCached.FloatToStr(percent, 1)); - } - title = title.Replace("{a" + i + "}", serie.name); - title = title.Replace("{b" + i + "}", needCategory ? category : serieData.name); - title = title.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation)); - title = title.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1)); - } - } - if (string.IsNullOrEmpty(title)) - { - if (needCategory) return category + "\n" + sb.ToString(); - else return sb.ToString(); - } - else - { - title = title.Replace("\\n", "\n"); - title = title.Replace("
", "\n"); - return title + "\n" + sb.ToString(); - } - } - } - else - { - string content = m_Formatter; - for (int i = 0; i < series.Count; i++) - { - var serie = series.GetSerie(i); - if (serie.show) - { - var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar; - var serieData = serie.GetSerieData(dataIndex, dataZoom); - var percent = serieData.GetData(1) / serie.yTotal * 100; - if (i == 0) - { - content = content.Replace("{a}", serie.name); - content = content.Replace("{b}", needCategory ? category : serieData.name); - content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation)); - content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1)); - } - content = content.Replace("{a" + i + "}", serie.name); - content = content.Replace("{b" + i + "}", needCategory ? category : serieData.name); - content = content.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation)); - content = content.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1)); - } - } - content = content.Replace("\\n", "\n"); - content = content.Replace("
", "\n"); - return content; - } - } - - internal string GetFormatterContent(string serieName, string dataName, float dataValue) - { - if (string.IsNullOrEmpty(m_Formatter)) - return ChartCached.FloatToStr(dataValue, 0, m_ForceENotation); - else - { - var content = m_Formatter.Replace("{a}", serieName); - content = content.Replace("{b}", dataName); - content = content.Replace("{c}", ChartCached.FloatToStr(dataValue, 0, m_ForceENotation)); - content = content.Replace("\\n", "\n"); - content = content.Replace("
", "\n"); - return content; - } - } - - internal Color GetLineColor(ThemeInfo theme) - { - if (lineStyle.color != Color.clear) - { - var color = lineStyle.color; - color.a *= lineStyle.opacity; - return color; - } - else - { - var color = (Color)theme.tooltipLineColor; - color.a *= lineStyle.opacity; - return color; - } - } } } \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Component/Sub/ItemStyle.cs b/Assets/XCharts/Runtime/Component/Sub/ItemStyle.cs index 64919f52..86d220ac 100644 --- a/Assets/XCharts/Runtime/Component/Sub/ItemStyle.cs +++ b/Assets/XCharts/Runtime/Component/Sub/ItemStyle.cs @@ -45,6 +45,7 @@ namespace XCharts [SerializeField] private float m_BorderWidth = 0; [SerializeField] private Color m_BorderColor; [SerializeField] [Range(0, 1)] private float m_Opacity = 1; + [SerializeField] private string m_TooltipFormatter; /// /// 是否启用。 @@ -136,6 +137,14 @@ namespace XCharts set { if (PropertyUtility.SetStruct(ref m_Opacity, value)) SetVerticesDirty(); } } /// + /// 提示框单项的字符串模版格式器。具体配置参考`Tooltip`的`formatter` + /// + public string tooltipFormatter + { + get { return m_TooltipFormatter; } + set { if (PropertyUtility.SetClass(ref m_TooltipFormatter, value)) SetVerticesDirty(); } + } + /// /// 实际边框宽。边框不显示时为0。 /// public float runtimeBorderWidth { get { return NeedShowBorder() ? borderWidth : 0; } } diff --git a/Assets/XCharts/Runtime/Helper/TooltipHelper.cs b/Assets/XCharts/Runtime/Helper/TooltipHelper.cs new file mode 100644 index 00000000..ca3ae720 --- /dev/null +++ b/Assets/XCharts/Runtime/Helper/TooltipHelper.cs @@ -0,0 +1,250 @@ +/******************************************/ +/* */ +/* Copyright (c) 2018 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/******************************************/ +using System.Text; +using UnityEngine; +using UnityEngine.UI; + +namespace XCharts +{ + internal static class TooltipHelper + { + + private static void InitPieTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index, + ThemeInfo themeInfo) + { + string key = serie.data[index].name; + + float value = serie.data[index].data[1]; + sb.Length = 0; + if (!string.IsNullOrEmpty(serie.name)) + { + sb.Append(serie.name).Append("\n"); + } + sb.Append("● "); + if (!string.IsNullOrEmpty(key)) + sb.Append(key).Append(": "); + sb.Append(ChartCached.FloatToStr(value, 0, tooltip.forceENotation)); + } + + private static void InitRingTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index, + ThemeInfo themeInfo) + { + var serieData = serie.GetSerieData(index); + float value = serieData.GetFirstData(); + sb.Length = 0; + if (!string.IsNullOrEmpty(serieData.name)) + { + sb.Append("● ") + .Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, 0, tooltip.forceENotation)); + } + else + { + sb.Append(ChartCached.FloatToStr(value, 0, tooltip.forceENotation)); + } + } + + public static void InitRadarTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, Radar radar, + ThemeInfo themeInfo) + { + var dataIndex = tooltip.runtimeDataIndex[1]; + var serieData = serie.GetSerieData(dataIndex); + switch (serie.radarType) + { + case RadarType.Multiple: + sb.Append(serieData.name); + for (int i = 0; i < radar.indicatorList.Count; i++) + { + string key = radar.indicatorList[i].name; + float value = serieData.GetData(i); + if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append("\n"); + sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, 0, tooltip.forceENotation)); + } + break; + case RadarType.Single: + string key2 = serieData.name; + float value2 = serieData.GetData(1); + if (string.IsNullOrEmpty(key2)) + { + key2 = radar.indicatorList[dataIndex].name; + } + sb.AppendFormat("{0}: {1}", key2, ChartCached.FloatToStr(value2, 0, tooltip.forceENotation)); + break; + } + } + + private static void InitCoordinateTooltip(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index, + ThemeInfo themeInfo, bool isCartesian, DataZoom dataZoom = null) + { + string key = serie.name; + float xValue, yValue; + serie.GetXYData(index, dataZoom, out xValue, out yValue); + var isIngore = serie.IsIngorePoint(index); + if (isCartesian) + { + var serieData = serie.GetSerieData(index, dataZoom); + if (serieData != null && serieData.highlighted) + { + sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : ""); + sb.Append("[").Append(ChartCached.FloatToStr(xValue, 0, tooltip.forceENotation)).Append(",") + .Append(ChartCached.FloatToStr(yValue, 0, tooltip.forceENotation)).Append("]\n"); + } + } + else + { + var valueTxt = isIngore ? tooltip.ignoreDataDefaultContent : + ChartCached.FloatToStr(yValue, 0, tooltip.forceENotation); + sb.Append("\n") + .Append("● ") + .Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "") + .Append(valueTxt); + } + } + + private static void InitDefaultContent(ref StringBuilder sb, Tooltip tooltip, Serie serie, int index, + string category, ThemeInfo themeInfo = null, DataZoom dataZoom = null, bool isCartesian = false) + { + switch (serie.type) + { + case SerieType.Line: + case SerieType.Bar: + case SerieType.Scatter: + case SerieType.EffectScatter: + InitCoordinateTooltip(ref sb, tooltip, serie, index, themeInfo, isCartesian, dataZoom); + break; + case SerieType.Radar: + break; + case SerieType.Pie: + InitPieTooltip(ref sb, tooltip, serie, index, themeInfo); + break; + case SerieType.Ring: + InitRingTooltip(ref sb, tooltip, serie, index, themeInfo); + break; + case SerieType.Heatmap: + break; + case SerieType.Gauge: + break; + } + } + + public static string GetFormatterContent(Tooltip tooltip, int dataIndex, Series series, ThemeInfo themeInfo, + string category = null, DataZoom dataZoom = null, bool isCartesian = false) + { + if (string.IsNullOrEmpty(tooltip.formatter)) + { + var sb = ChartHelper.sb; + var title = tooltip.titleFormatter; + var formatTitle = !string.IsNullOrEmpty(title); + var needCategory = false; + var first = true; + sb.Length = 0; + for (int i = 0; i < series.Count; i++) + { + var serie = series.GetSerie(i); + if (!serie.show) continue; + var serieData = serie.GetSerieData(dataIndex, dataZoom); + var itemFormatter = GetItemFormatter(tooltip, serie, serieData); + if (string.IsNullOrEmpty(itemFormatter)) + { + InitDefaultContent(ref sb, tooltip, serie, dataIndex, category, themeInfo, dataZoom, isCartesian); + continue; + } + var percent = serieData.GetData(1) / serie.yTotal * 100; + needCategory = needCategory || (serie.type == SerieType.Line || serie.type == SerieType.Bar); + if (serie.show) + { + string content = itemFormatter; + content = content.Replace("{a}", serie.name); + content = content.Replace("{b}", needCategory ? category : serieData.name); + content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation)); + content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1)); + if (!first) sb.Append("\n"); + sb.Append("● "); + sb.Append(content); + first = false; + } + if (formatTitle) + { + if (i == 0) + { + title = title.Replace("{a}", serie.name); + title = title.Replace("{b}", needCategory ? category : serieData.name); + title = title.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation)); + title = title.Replace("{d}", ChartCached.FloatToStr(percent, 1)); + } + title = title.Replace("{a" + i + "}", serie.name); + title = title.Replace("{b" + i + "}", needCategory ? category : serieData.name); + title = title.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation)); + title = title.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1)); + } + } + if (string.IsNullOrEmpty(title)) + { + if (needCategory) return category + "\n" + sb.ToString(); + else return sb.ToString(); + } + else + { + title = title.Replace("\\n", "\n"); + title = title.Replace("
", "\n"); + return title + "\n" + sb.ToString(); + } + } + else + { + string content = tooltip.formatter; + for (int i = 0; i < series.Count; i++) + { + var serie = series.GetSerie(i); + if (serie.show) + { + var needCategory = serie.type == SerieType.Line || serie.type == SerieType.Bar; + var serieData = serie.GetSerieData(dataIndex, dataZoom); + var percent = serieData.GetData(1) / serie.yTotal * 100; + if (i == 0) + { + content = content.Replace("{a}", serie.name); + content = content.Replace("{b}", needCategory ? category : serieData.name); + content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation)); + content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1)); + } + content = content.Replace("{a" + i + "}", serie.name); + content = content.Replace("{b" + i + "}", needCategory ? category : serieData.name); + content = content.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, tooltip.forceENotation)); + content = content.Replace("{d" + i + "}", ChartCached.FloatToStr(percent, 1)); + } + } + content = content.Replace("\\n", "\n"); + content = content.Replace("
", "\n"); + return content; + } + } + + private static string GetItemFormatter(Tooltip tooltip, Serie serie, SerieData serieData) + { + var itemStyle = SerieHelper.GetItemStyle(serie, serieData); + if (!string.IsNullOrEmpty(itemStyle.tooltipFormatter)) return itemStyle.tooltipFormatter; + else return tooltip.itemFormatter; + } + + public static Color GetLineColor(Tooltip tooltip, ThemeInfo theme) + { + var lineStyle = tooltip.lineStyle; + if (lineStyle.color != Color.clear) + { + var color = lineStyle.color; + color.a *= lineStyle.opacity; + return color; + } + else + { + var color = (Color)theme.tooltipLineColor; + color.a *= lineStyle.opacity; + return color; + } + } + } +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Helper/TooltipHelper.cs.meta b/Assets/XCharts/Runtime/Helper/TooltipHelper.cs.meta new file mode 100644 index 00000000..bb7eab8b --- /dev/null +++ b/Assets/XCharts/Runtime/Helper/TooltipHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: de1595793312142b4bbe2d1081eecf68 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/XCharts/Runtime/Internal/CoordinateChart.cs b/Assets/XCharts/Runtime/Internal/CoordinateChart.cs index f4487236..1a8f3987 100644 --- a/Assets/XCharts/Runtime/Internal/CoordinateChart.cs +++ b/Assets/XCharts/Runtime/Internal/CoordinateChart.cs @@ -404,57 +404,10 @@ namespace XCharts } return; } - - if (tooltip.IsNoFormatter()) - { - sb.Length = 0; - if (!isCartesian) - { - var category = tempAxis.GetData(index, m_DataZoom); - if (!string.IsNullOrEmpty(category)) sb.Append(category); - else - { - m_Tooltip.SetActive(false); - return; - } - } - for (int i = 0; i < m_Series.Count; i++) - { - var serie = m_Series.GetSerie(i); - if (serie.show) - { - string key = serie.name; - float xValue, yValue; - serie.GetXYData(index, m_DataZoom, out xValue, out yValue); - var isIngore = serie.IsIngorePoint(index); - if (isCartesian) - { - var serieData = serie.GetSerieData(index, m_DataZoom); - if (serieData != null && serieData.highlighted) - { - sb.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : ""); - sb.Append("[").Append(ChartCached.FloatToStr(xValue, 0, m_Tooltip.forceENotation)).Append(",") - .Append(ChartCached.FloatToStr(yValue, 0, m_Tooltip.forceENotation)).Append("]\n"); - } - } - else - { - var valueTxt = isIngore ? m_Tooltip.ignoreDataDefaultContent : - ChartCached.FloatToStr(yValue, 0, m_Tooltip.forceENotation); - sb.Append("\n") - .Append("● ") - .Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "") - .Append(valueTxt); - } - } - } - m_Tooltip.UpdateContentText(sb.ToString().Trim()); - } - else - { - var category = tempAxis.GetData(index, m_DataZoom); - m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series, category, m_ThemeInfo, m_DataZoom)); - } + var category = tempAxis.GetData(index, m_DataZoom); + var content = TooltipHelper.GetFormatterContent(m_Tooltip, index, m_Series, m_ThemeInfo, category, + m_DataZoom, isCartesian); + m_Tooltip.UpdateContentText(content); var pos = m_Tooltip.GetContentPos(); if (pos.x + m_Tooltip.runtimeWidth > chartWidth) { @@ -1255,12 +1208,13 @@ namespace XCharts if (xAxis.IsValue()) pX = m_Tooltip.runtimePointerPos.x; Vector2 sp = new Vector2(pX, coordinateY); Vector2 ep = new Vector2(pX, coordinateY + coordinateHeight); - DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, m_Tooltip.GetLineColor(m_ThemeInfo)); + var lineColor = TooltipHelper.GetLineColor(tooltip, m_ThemeInfo); + DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, lineColor); if (m_Tooltip.type == Tooltip.Type.Corss) { sp = new Vector2(coordinateX, m_Tooltip.runtimePointerPos.y); ep = new Vector2(coordinateX + coordinateWidth, m_Tooltip.runtimePointerPos.y); - DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, m_Tooltip.GetLineColor(m_ThemeInfo)); + DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, lineColor); } break; case Tooltip.Type.Shadow: @@ -1294,16 +1248,16 @@ namespace XCharts { case Tooltip.Type.Corss: case Tooltip.Type.Line: - float pY = coordinateY + m_Tooltip.runtimeYValues[i] * splitWidth + (yAxis.boundaryGap ? splitWidth / 2 : 0); Vector2 sp = new Vector2(coordinateX, pY); Vector2 ep = new Vector2(coordinateX + coordinateWidth, pY); - DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, m_Tooltip.GetLineColor(m_ThemeInfo)); + var lineColor = TooltipHelper.GetLineColor(tooltip, m_ThemeInfo); + DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, lineColor); if (m_Tooltip.type == Tooltip.Type.Corss) { sp = new Vector2(coordinateX, m_Tooltip.runtimePointerPos.y); ep = new Vector2(coordinateX + coordinateWidth, m_Tooltip.runtimePointerPos.y); - DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, m_Tooltip.GetLineColor(m_ThemeInfo)); + DrawLineStyle(vh, m_Tooltip.lineStyle, sp, ep, lineColor); } break; case Tooltip.Type.Shadow: diff --git a/Assets/XCharts/Runtime/PieChart.cs b/Assets/XCharts/Runtime/PieChart.cs index 3bc71aad..2b5ce511 100644 --- a/Assets/XCharts/Runtime/PieChart.cs +++ b/Assets/XCharts/Runtime/PieChart.cs @@ -580,26 +580,8 @@ namespace XCharts int index = m_Tooltip.runtimeDataIndex[serie.index]; if (index < 0) continue; showTooltip = true; - if (tooltip.IsNoFormatter()) - { - string key = serie.data[index].name; - if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(index); - - float value = serie.data[index].data[1]; - sb.Length = 0; - if (!string.IsNullOrEmpty(serie.name)) - { - sb.Append(serie.name).Append("\n"); - } - sb.Append("● ") - .Append(key).Append(": ").Append(ChartCached.FloatToStr(value, 0, m_Tooltip.forceENotation)); - m_Tooltip.UpdateContentText(sb.ToString()); - } - else - { - m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series, null, m_ThemeInfo)); - } - + var content = TooltipHelper.GetFormatterContent(m_Tooltip, index, m_Series, m_ThemeInfo); + m_Tooltip.UpdateContentText(content); var pos = m_Tooltip.GetContentPos(); if (pos.x + m_Tooltip.runtimeWidth > chartWidth) { diff --git a/Assets/XCharts/Runtime/RadarChart.cs b/Assets/XCharts/Runtime/RadarChart.cs index 8374177b..a97054a8 100644 --- a/Assets/XCharts/Runtime/RadarChart.cs +++ b/Assets/XCharts/Runtime/RadarChart.cs @@ -707,31 +707,8 @@ namespace XCharts m_Tooltip.SetActive(true); var serie = m_Series.GetSerie(serieIndex); var radar = m_Radars[serie.radarIndex]; - var dataIndex = m_Tooltip.runtimeDataIndex[1]; - var serieData = serie.GetSerieData(dataIndex); StringBuilder sb = new StringBuilder(); - switch (serie.radarType) - { - case RadarType.Multiple: - sb.Append(serieData.name); - for (int i = 0; i < radar.indicatorList.Count; i++) - { - string key = radar.indicatorList[i].name; - float value = serieData.GetData(i); - if ((i == 0 && !string.IsNullOrEmpty(serieData.name)) || i > 0) sb.Append("\n"); - sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, 0, m_Tooltip.forceENotation)); - } - break; - case RadarType.Single: - string key2 = serieData.name; - float value2 = serieData.GetData(1); - if (string.IsNullOrEmpty(key2)) - { - key2 = radar.indicatorList[dataIndex].name; - } - sb.AppendFormat("{0}: {1}", key2, ChartCached.FloatToStr(value2, 0, m_Tooltip.forceENotation)); - break; - } + TooltipHelper.InitRadarTooltip(ref sb, tooltip, serie, radar, themeInfo); m_Tooltip.UpdateContentText(sb.ToString()); var pos = m_Tooltip.GetContentPos(); if (pos.x + m_Tooltip.runtimeWidth > chartWidth) diff --git a/Assets/XCharts/Runtime/RingChart.cs b/Assets/XCharts/Runtime/RingChart.cs index a35173d8..1cd0966d 100644 --- a/Assets/XCharts/Runtime/RingChart.cs +++ b/Assets/XCharts/Runtime/RingChart.cs @@ -330,26 +330,8 @@ namespace XCharts int index = m_Tooltip.runtimeDataIndex[serie.index]; if (index < 0) continue; showTooltip = true; - if (tooltip.IsNoFormatter()) - { - var serieData = serie.GetSerieData(index); - float value = serieData.GetFirstData(); - sb.Length = 0; - if (!string.IsNullOrEmpty(serieData.name)) - { - sb.Append("● ") - .Append(serieData.name).Append(": ").Append(ChartCached.FloatToStr(value, 0, m_Tooltip.forceENotation)); - } - else - { - sb.Append(ChartCached.FloatToStr(value, 0, m_Tooltip.forceENotation)); - } - m_Tooltip.UpdateContentText(sb.ToString()); - } - else - { - m_Tooltip.UpdateContentText(m_Tooltip.GetFormatterContent(index, m_Series, null, m_ThemeInfo)); - } + var content = TooltipHelper.GetFormatterContent(m_Tooltip, index, m_Series, m_ThemeInfo); + m_Tooltip.UpdateContentText(content); var pos = m_Tooltip.GetContentPos(); if (pos.x + m_Tooltip.runtimeWidth > chartWidth)