diff --git a/Scripts/BaseChart.cs b/Scripts/BaseChart.cs index 35694adb..3e1d03c7 100644 --- a/Scripts/BaseChart.cs +++ b/Scripts/BaseChart.cs @@ -86,6 +86,7 @@ namespace xcharts if (index < 0 || index > dataBtnList.Count - 1) { dataBtnList.Add(btn); + dataShowList.Add(true); } else { @@ -386,7 +387,7 @@ namespace xcharts for (int i = 0; i < legend.dataList.Count; i++) { //LegendData data = legend.dataList[i]; - Button btn = ChartUtils.AddButtonObject(LEGEND_TEXT + i, transform, themeInfo.font, + Button btn = ChartUtils.AddButtonObject(LEGEND_TEXT +"_"+ i, transform, themeInfo.font, themeInfo.textColor, Vector2.zero, Vector2.zero, Vector2.zero, new Vector2(legend.itemWidth, legend.itemHeight)); legend.SetDataButton(i, btn); @@ -397,9 +398,10 @@ namespace xcharts btn.GetComponentInChildren().text = legend.dataList[i]; btn.onClick.AddListener(delegate () { - legend.SetShowData(i, !legend.IsShowSeries(i)); - btn.GetComponent().color = legend.IsShowSeries(i) ? - themeInfo.GetColor(i) : themeInfo.unableColor; + int index = int.Parse(btn.name.Split('_')[1]); + legend.SetShowData(index, !legend.IsShowSeries(index)); + btn.GetComponent().color = legend.IsShowSeries(index) ? + themeInfo.GetColor(index) : themeInfo.unableColor; OnYMaxValueChanged(); OnLegendButtonClicked(); RefreshChart(); diff --git a/Scripts/PieChart.cs b/Scripts/PieChart.cs index df658c83..cc9f7488 100644 --- a/Scripts/PieChart.cs +++ b/Scripts/PieChart.cs @@ -138,7 +138,7 @@ namespace xcharts if (dist > pieRadius) { tooltip.DataIndex = 0; - RefreshTooltip(); + tooltip.SetActive(false); } else { diff --git a/Scripts/RadarChart.cs b/Scripts/RadarChart.cs index a4878105..f7bd0159 100644 --- a/Scripts/RadarChart.cs +++ b/Scripts/RadarChart.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Text; using UnityEngine; using UnityEngine.UI; @@ -47,6 +48,8 @@ namespace xcharts private float radarCenterY = 0f; private float radarRadius = 0; private List indicatorTextList = new List(); + private List> dataPosList = new List>(); + protected override void Awake() { @@ -181,10 +184,14 @@ namespace xcharts Vector3 startPoint = Vector3.zero; Vector3 toPoint = Vector3.zero; Vector3 firstPoint = Vector3.zero; - + dataPosList.Clear(); for (int i = 0; i < seriesList.Count; i++) { - if (!legend.IsShowSeries(i)) continue; + if (!legend.IsShowSeries(i)) + { + dataPosList.Add(new List()); + continue; + } var dataList = seriesList[i].dataList; var color = themeInfo.GetColor(i); var areaColor = new Color(color.r, color.g, color.b, color.a * 0.7f); @@ -192,6 +199,7 @@ namespace xcharts radarInfo.indicatorList[i].max : GetMaxValue(); List pointList = new List(); + dataPosList.Add(pointList); for (int j = 0; j < dataList.Count; j++) { var radius = radarInfo.radius * dataList[j] / max; @@ -313,5 +321,69 @@ namespace xcharts if (radarInfo.bottom > 0) radarCenterY = radarInfo.bottom + radarRadius; } } + + protected override void CheckTootipArea(Vector2 local) + { + if (dataPosList.Count <= 0) return; + tooltip.DataIndex = 0; + for (int i = 0; i < seriesList.Count; i++) + { + if (!legend.IsShowSeries(i)) continue; + for (int j = 0; j < dataPosList[i].Count; j++) + { + if (Vector3.Distance(local, dataPosList[i][j]) <= radarInfo.linePointSize * 1.2f) + { + tooltip.DataIndex = i + 1; + break; + } + } + } + if (tooltip.DataIndex > 0) + { + tooltip.UpdatePos(new Vector2(local.x + 18, local.y - 25)); + RefreshTooltip(); + if (tooltip.LastDataIndex != tooltip.DataIndex) + { + RefreshChart(); + } + tooltip.LastDataIndex = tooltip.DataIndex; + } + else + { + tooltip.SetActive(false); + } + } + + protected override void RefreshTooltip() + { + base.RefreshTooltip(); + int index = tooltip.DataIndex - 1; + if (index < 0) + { + tooltip.SetActive(false); + return; + } + tooltip.SetActive(true); + StringBuilder sb = new StringBuilder(legend.dataList[index]); + for (int i = 0; i < radarInfo.indicatorList.Count; i++) + { + string key = radarInfo.indicatorList[i].name; + float value = seriesList[index].dataList[i]; + sb.Append("\n"); + sb.AppendFormat("{0}: {1}", key, value); + } + tooltip.UpdateTooltipText(sb.ToString()); + + var pos = tooltip.GetPos(); + if (pos.x + tooltip.Width > chartWid) + { + pos.x = chartWid - tooltip.Width; + } + if (pos.y - tooltip.Height < 0) + { + pos.y = tooltip.Height; + } + tooltip.UpdatePos(pos); + } } }