mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 14:30:10 +00:00
优化Legend的显示和控制
This commit is contained in:
@@ -38,8 +38,9 @@ namespace XCharts
|
||||
[NonSerialized] private Legend m_CheckLegend = Legend.defaultLegend;
|
||||
[NonSerialized] private float m_CheckWidth = 0;
|
||||
[NonSerialized] private float m_CheckHeight = 0;
|
||||
[NonSerialized] private float m_CheckSerieCount = 0;
|
||||
[NonSerialized] private List<string> m_CheckSerieName = new List<string>();
|
||||
[NonSerialized] private bool m_RefreshChart = false;
|
||||
[NonSerialized] protected List<Text> m_LegendTextList = new List<Text>();
|
||||
|
||||
protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } }
|
||||
protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } }
|
||||
@@ -200,8 +201,11 @@ namespace XCharts
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(string serieName, bool active)
|
||||
{
|
||||
m_Legend.SetActive(serieName, active);
|
||||
m_Series.SetActive(serieName, active);
|
||||
var serie = m_Series.GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
SetActive(serie.index, active);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -211,8 +215,13 @@ namespace XCharts
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(int serieIndex, bool active)
|
||||
{
|
||||
m_Legend.SetActive(serieIndex, active);
|
||||
m_Series.SetActive(serieIndex, active);
|
||||
var serie = m_Series.GetSerie(serieIndex);
|
||||
if (serie != null && !string.IsNullOrEmpty(serie.name))
|
||||
{
|
||||
var bgColor1 = active ? m_ThemeInfo.GetColor(serie.index) : m_ThemeInfo.legendUnableColor;
|
||||
m_Legend.UpdateButtonColor(serie.name, bgColor1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -222,7 +231,7 @@ namespace XCharts
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(string serieName)
|
||||
{
|
||||
return m_Legend.IsActive(serieName) || m_Series.IsActive(serieName);
|
||||
return m_Series.IsActive(serieName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -232,7 +241,7 @@ namespace XCharts
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(int serieIndex)
|
||||
{
|
||||
return m_Legend.IsActive(serieIndex) && m_Series.IsActive(serieIndex);
|
||||
return m_Series.IsActive(serieIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -371,23 +380,40 @@ namespace XCharts
|
||||
legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight);
|
||||
ChartHelper.HideAllObject(legendObject, s_LegendObjectName);
|
||||
|
||||
for (int i = 0; i < m_Legend.data.Count; i++)
|
||||
var serieNameList = m_Series.GetSerieNameList();
|
||||
List<string> datas;
|
||||
if (m_Legend.data.Count > 0)
|
||||
{
|
||||
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i, legendObject.transform,
|
||||
datas = new List<string>();
|
||||
for (int i = 0; i < m_Legend.data.Count; i++)
|
||||
{
|
||||
var category = m_Legend.data[i];
|
||||
if (serieNameList.Contains(category)) datas.Add(category);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
datas = serieNameList;
|
||||
}
|
||||
m_Legend.RemoveButton();
|
||||
for (int i = 0; i < datas.Count; i++)
|
||||
{
|
||||
string legendName = datas[i];
|
||||
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + legendName, legendObject.transform,
|
||||
m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor,
|
||||
anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight));
|
||||
|
||||
m_Legend.SetButton(i, btn);
|
||||
m_Legend.SetActive(i, IsActive(i));
|
||||
m_Legend.UpdateButtonColor(i, m_ThemeInfo.GetColor(i), m_ThemeInfo.legendUnableColor);
|
||||
btn.GetComponentInChildren<Text>().text = m_Legend.data[i];
|
||||
var bgColor = IsActive(legendName) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor;
|
||||
m_Legend.SetButton(legendName, btn, datas.Count);
|
||||
m_Legend.UpdateButtonColor(legendName, bgColor);
|
||||
btn.GetComponentInChildren<Text>().text = legendName;
|
||||
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) =>
|
||||
{
|
||||
int count = (data as PointerEventData).clickCount;
|
||||
int index = int.Parse(data.selectedObject.name.Split('_')[1]);
|
||||
SetActive(index, !m_Legend.IsActive(index));
|
||||
m_Legend.UpdateButtonColor(index, m_ThemeInfo.GetColor(index),
|
||||
m_ThemeInfo.legendUnableColor);
|
||||
if (data.selectedObject == null) return;
|
||||
string selectedName = data.selectedObject.name.Split('_')[1];
|
||||
foreach (var serie in m_Series.GetSeries(selectedName))
|
||||
{
|
||||
SetActive(serie.index, !serie.show);
|
||||
}
|
||||
OnYMaxValueChanged();
|
||||
OnLegendButtonClicked();
|
||||
RefreshChart();
|
||||
@@ -459,6 +485,23 @@ namespace XCharts
|
||||
m_CheckLegend.Copy(m_Legend);
|
||||
OnLegendChanged();
|
||||
}
|
||||
else if (m_Legend.show)
|
||||
{
|
||||
if (m_CheckSerieCount != m_Series.Count)
|
||||
{
|
||||
m_CheckSerieCount = m_Series.Count;
|
||||
m_CheckSerieName.Clear();
|
||||
var serieNames = m_Series.GetSerieNameList();
|
||||
foreach (var name in serieNames) m_CheckSerieName.Add(name);
|
||||
OnLegendChanged();
|
||||
}
|
||||
else if (!ChartHelper.IsValueEqualsList(m_CheckSerieName, m_Series.GetSerieNameList()))
|
||||
{
|
||||
var serieNames = m_Series.GetSerieNameList();
|
||||
foreach (var name in serieNames) m_CheckSerieName.Add(name);
|
||||
OnLegendChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckTooltip()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
@@ -28,8 +27,6 @@ namespace XCharts
|
||||
private List<YAxis> m_CheckYAxises = new List<YAxis>();
|
||||
private Coordinate m_CheckCoordinate = Coordinate.defaultCoordinate;
|
||||
|
||||
// public float coordinateX { get { return coordinateX; } }
|
||||
// public float coordinateY { get { return coordinateY; } }
|
||||
public float coordinateX { get { return m_Coordinate.left; } }
|
||||
public float coordinateY { get { return m_Coordinate.bottom; } }
|
||||
public float coordinateWid { get { return chartWidth - m_Coordinate.left - m_Coordinate.right; } }
|
||||
@@ -89,9 +86,9 @@ namespace XCharts
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether is Cartesian coordinates
|
||||
/// Whether is Cartesian coordinates, reutrn true when all the show axis is `Value` type.
|
||||
/// </summary>
|
||||
/// <returns>reutrn true when all show axis is value type</returns>
|
||||
/// <returns></returns>
|
||||
public bool IsCartesian()
|
||||
{
|
||||
foreach (var axis in m_XAxises)
|
||||
@@ -159,7 +156,7 @@ namespace XCharts
|
||||
{
|
||||
var xAxis = m_XAxises[i];
|
||||
var yAxis = m_YAxises[i];
|
||||
if(!xAxis.show && !yAxis.show) continue;
|
||||
if (!xAxis.show && !yAxis.show) continue;
|
||||
if (isCartesian && xAxis.show && yAxis.show)
|
||||
{
|
||||
var yRate = (yAxis.maxValue - yAxis.minValue) / coordinateHig;
|
||||
@@ -234,7 +231,9 @@ namespace XCharts
|
||||
m_Tooltip.UpdateLastDataIndex();
|
||||
RefreshChart();
|
||||
}
|
||||
}else{
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Tooltip.SetActive(false);
|
||||
}
|
||||
}
|
||||
@@ -278,7 +277,7 @@ namespace XCharts
|
||||
{
|
||||
string key = serie.name;
|
||||
//if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(i);
|
||||
if(!string.IsNullOrEmpty(key)) key += ":";
|
||||
if (!string.IsNullOrEmpty(key)) key += " : ";
|
||||
float xValue, yValue;
|
||||
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
|
||||
if (isCartesian)
|
||||
@@ -722,7 +721,7 @@ namespace XCharts
|
||||
m_Series.GetYMinMaxValue(m_DataZoom, axisIndex, out tempMinValue, out tempMaxValue);
|
||||
}
|
||||
axis.AdjustMinMaxValue(ref tempMinValue, ref tempMaxValue);
|
||||
|
||||
|
||||
if (tempMinValue != axis.minValue || tempMaxValue != axis.maxValue)
|
||||
{
|
||||
axis.minValue = tempMinValue;
|
||||
@@ -891,7 +890,7 @@ namespace XCharts
|
||||
if (xAxis.IsValue() && xAxisIndex > 0) lineY += coordinateHig;
|
||||
var left = new Vector3(coordinateX - m_Coordinate.tickness, lineY);
|
||||
var top = new Vector3(coordinateX + coordinateWid + m_Coordinate.tickness, lineY);
|
||||
ChartHelper.DrawLine(vh, left,top, m_Coordinate.tickness, m_ThemeInfo.axisLineColor);
|
||||
ChartHelper.DrawLine(vh, left, top, m_Coordinate.tickness, m_ThemeInfo.axisLineColor);
|
||||
if (xAxis.axisLine.symbol)
|
||||
{
|
||||
var axisLine = xAxis.axisLine;
|
||||
@@ -1140,7 +1139,12 @@ namespace XCharts
|
||||
|
||||
public override void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
var pos = transform.InverseTransformPoint(eventData.position);
|
||||
Vector2 pos;
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
||||
eventData.position, canvas.worldCamera, out pos))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_DataZoom.IsInStartZoom(pos, coordinateX, coordinateWid))
|
||||
{
|
||||
m_DataZoom.isDraging = true;
|
||||
@@ -1160,7 +1164,6 @@ namespace XCharts
|
||||
|
||||
public override void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
//Debug.LogError("drag");
|
||||
float deltaX = eventData.delta.x;
|
||||
float deltaPercent = deltaX / coordinateWid * 100;
|
||||
if (m_DataZoomStartDrag)
|
||||
@@ -1257,7 +1260,12 @@ namespace XCharts
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
var localPos = transform.InverseTransformPoint(eventData.position);
|
||||
Vector2 localPos;
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
||||
eventData.position, canvas.worldCamera, out localPos))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_DataZoom.IsInStartZoom(localPos, coordinateX, coordinateWid) ||
|
||||
m_DataZoom.IsInEndZoom(localPos, coordinateX, coordinateWid))
|
||||
{
|
||||
|
||||
@@ -17,8 +17,7 @@ namespace XCharts
|
||||
[SerializeField] private int m_ItemFontSize = 18;
|
||||
[SerializeField] private List<string> m_Data = new List<string>();
|
||||
|
||||
[NonSerialized] private List<bool> m_DataActiveList = new List<bool>();
|
||||
[NonSerialized] private List<Button> m_DataBtnList = new List<Button>();
|
||||
private Dictionary<string, Button> m_DataBtnList = new Dictionary<string, Button>();
|
||||
|
||||
public bool show { get { return m_Show; } set { m_Show = value; } }
|
||||
|
||||
@@ -126,24 +125,6 @@ namespace XCharts
|
||||
return base.GetHashCode();
|
||||
}
|
||||
|
||||
public bool IsActive(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name)) return true;
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
if (data[i].Equals(name)) return m_DataActiveList[i];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool IsActive(int seriesIndex)
|
||||
{
|
||||
if (seriesIndex < 0 || seriesIndex >= m_DataActiveList.Count)
|
||||
return true;
|
||||
else
|
||||
return m_DataActiveList[seriesIndex];
|
||||
}
|
||||
|
||||
public void ClearData()
|
||||
{
|
||||
m_Data.Clear();
|
||||
@@ -179,48 +160,25 @@ namespace XCharts
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SetActive(int index, bool flag)
|
||||
public void RemoveButton()
|
||||
{
|
||||
m_DataActiveList[index] = flag;
|
||||
m_DataBtnList.Clear();
|
||||
}
|
||||
|
||||
public void SetActive(string name, bool flag)
|
||||
public void SetButton(string name, Button btn, int total)
|
||||
{
|
||||
for (int i = 0; i < data.Count; i++)
|
||||
{
|
||||
if (data[i].Equals(name))
|
||||
{
|
||||
m_DataActiveList[i] = flag;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetButton(int index, Button btn)
|
||||
{
|
||||
btn.transform.localPosition = GetButtonLocationPosition(index);
|
||||
if (index < 0 || index > m_DataBtnList.Count - 1)
|
||||
{
|
||||
m_DataBtnList.Add(btn);
|
||||
m_DataActiveList.Add(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DataBtnList[index] = btn;
|
||||
}
|
||||
int index = m_DataBtnList.Values.Count;
|
||||
btn.transform.localPosition = GetButtonLocationPosition(total,index);
|
||||
m_DataBtnList[name] = btn;
|
||||
btn.gameObject.SetActive(show);
|
||||
btn.GetComponentInChildren<Text>().text = data[index];
|
||||
btn.GetComponentInChildren<Text>().text = name;
|
||||
}
|
||||
|
||||
public void UpdateButtonColor(int index, Color ableColor, Color unableColor)
|
||||
public void UpdateButtonColor(string name, Color color)
|
||||
{
|
||||
if (IsActive(index))
|
||||
if (m_DataBtnList.ContainsKey(name))
|
||||
{
|
||||
m_DataBtnList[index].GetComponent<Image>().color = ableColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_DataBtnList[index].GetComponent<Image>().color = unableColor;
|
||||
m_DataBtnList[name].GetComponent<Image>().color = color;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,9 +187,8 @@ namespace XCharts
|
||||
m_Location.OnChanged();
|
||||
}
|
||||
|
||||
private Vector2 GetButtonLocationPosition(int index)
|
||||
private Vector2 GetButtonLocationPosition(int size,int index)
|
||||
{
|
||||
int size = m_Data.Count;
|
||||
switch (m_Orient)
|
||||
{
|
||||
case Orient.Vertical:
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace XCharts
|
||||
{
|
||||
public enum SerieType
|
||||
{
|
||||
None,
|
||||
Line,
|
||||
Bar,
|
||||
Pie,
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace XCharts
|
||||
{
|
||||
m_Series = new List<Serie>(){new Serie(){
|
||||
show = true,
|
||||
name = "serie1",
|
||||
index = 0
|
||||
}}
|
||||
};
|
||||
@@ -53,12 +54,24 @@ namespace XCharts
|
||||
{
|
||||
if (name.Equals(m_Series[i].name))
|
||||
{
|
||||
m_Series[i].index = i;
|
||||
return m_Series[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Serie> GetSeries(string name)
|
||||
{
|
||||
var list = new List<Serie>();
|
||||
if (name == null) return list;
|
||||
foreach (var serie in m_Series)
|
||||
{
|
||||
if (name.Equals(serie.name)) list.Add(serie);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public Serie GetSerie(int index)
|
||||
{
|
||||
if (index >= 0 && index < m_Series.Count)
|
||||
@@ -107,16 +120,15 @@ namespace XCharts
|
||||
if (serie == null)
|
||||
{
|
||||
serie = new Serie();
|
||||
serie.index = m_Series.Count;
|
||||
serie.type = type;
|
||||
serie.show = show;
|
||||
serie.name = serieName;
|
||||
serie.index = m_Series.Count;
|
||||
serie.yData = new List<float>();
|
||||
m_Series.Add(serie);
|
||||
}
|
||||
else
|
||||
{
|
||||
serie.type = type;
|
||||
serie.show = show;
|
||||
}
|
||||
return serie;
|
||||
@@ -124,19 +136,7 @@ namespace XCharts
|
||||
|
||||
public Serie AddData(string name, float value, int maxDataNumber = 0)
|
||||
{
|
||||
if (m_Series == null)
|
||||
{
|
||||
m_Series = new List<Serie>();
|
||||
}
|
||||
var serie = GetSerie(name);
|
||||
if (serie == null)
|
||||
{
|
||||
serie = new Serie();
|
||||
serie.index = m_Series.Count;
|
||||
serie.name = name;
|
||||
serie.yData = new List<float>();
|
||||
m_Series.Add(serie);
|
||||
}
|
||||
var serie = AddSerie(name, SerieType.None);
|
||||
serie.AddYData(value, maxDataNumber);
|
||||
return serie;
|
||||
}
|
||||
@@ -416,6 +416,19 @@ namespace XCharts
|
||||
return stackSeries;
|
||||
}
|
||||
|
||||
public List<string> GetSerieNameList()
|
||||
{
|
||||
var list = new List<string>();
|
||||
foreach (var serie in m_Series)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(serie.name) && !list.Contains(serie.name))
|
||||
{
|
||||
list.Add(serie.name);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public override void ParseJsonData(string jsonData)
|
||||
{
|
||||
//TODO:
|
||||
|
||||
Reference in New Issue
Block a user