mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-18 06:20:15 +00:00
优化Legend的显示和控制
This commit is contained in:
@@ -59,7 +59,7 @@ namespace XCharts
|
||||
private void DrawYBarSerie(VertexHelper vh, int serieIndex, int stackCount,
|
||||
Serie serie, Color color, ref Dictionary<int, float> seriesHig)
|
||||
{
|
||||
if (!m_Legend.IsActive(serie.name)) return;
|
||||
if (!IsActive(serie.name)) return;
|
||||
var xAxis = m_XAxises[serie.axisIndex];
|
||||
var yAxis = m_YAxises[serie.axisIndex];
|
||||
if (!yAxis.show) yAxis = m_YAxises[(serie.axisIndex + 1) % m_YAxises.Count];
|
||||
@@ -100,7 +100,7 @@ namespace XCharts
|
||||
private void DrawXBarSerie(VertexHelper vh, int serieIndex, int stackCount,
|
||||
Serie serie, Color color, ref Dictionary<int, float> seriesHig)
|
||||
{
|
||||
if (!m_Legend.IsActive(serie.name)) return;
|
||||
if (!IsActive(serie.name)) return;
|
||||
List<float> showData = serie.GetYDataList(m_DataZoom);
|
||||
var yAxis = m_YAxises[serie.axisIndex];
|
||||
var xAxis = m_XAxises[serie.axisIndex];
|
||||
@@ -147,7 +147,8 @@ namespace XCharts
|
||||
{
|
||||
var stackSeries = m_Series.GetStackSeries();
|
||||
int seriesCount = stackSeries.Count;
|
||||
int serieCount = 0;
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int j = 0; j < seriesCount; j++)
|
||||
{
|
||||
var seriesHig = new Dictionary<int, float>();
|
||||
@@ -155,12 +156,15 @@ namespace XCharts
|
||||
for (int n = 0; n < serieList.Count; n++)
|
||||
{
|
||||
Serie serie = serieList[n];
|
||||
Color color = m_ThemeInfo.GetColor(serieCount);
|
||||
DrawYBarSerie(vh, j, seriesCount, serie, color, ref seriesHig);
|
||||
if (serie.show)
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
serieCount++;
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
Color color = m_ThemeInfo.GetColor(serieNameCount);
|
||||
DrawYBarSerie(vh, j, seriesCount, serie, color, ref seriesHig);
|
||||
|
||||
}
|
||||
}
|
||||
DrawYTooltipIndicator(vh);
|
||||
@@ -169,7 +173,8 @@ namespace XCharts
|
||||
{
|
||||
var stackSeries = m_Series.GetStackSeries();
|
||||
int seriesCount = stackSeries.Count;
|
||||
int serieCount = 0;
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int j = 0; j < seriesCount; j++)
|
||||
{
|
||||
var seriesHig = new Dictionary<int, float>();
|
||||
@@ -177,12 +182,14 @@ namespace XCharts
|
||||
for (int n = 0; n < serieList.Count; n++)
|
||||
{
|
||||
Serie serie = serieList[n];
|
||||
Color color = m_ThemeInfo.GetColor(serieCount);
|
||||
DrawXBarSerie(vh, j, seriesCount, serie, color, ref seriesHig);
|
||||
if (serie.show)
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
serieCount++;
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
Color color = m_ThemeInfo.GetColor(serieNameCount);
|
||||
DrawXBarSerie(vh, j, seriesCount, serie, color, ref seriesHig);
|
||||
}
|
||||
}
|
||||
DrawXTooltipIndicator(vh);
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -54,6 +54,8 @@ namespace XCharts
|
||||
List<Vector3> points = new List<Vector3>();
|
||||
List<Color> colors = new List<Color>();
|
||||
int dataCount = 0;
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int j = 0; j < seriesCount; j++)
|
||||
{
|
||||
var seriesCurrHig = new Dictionary<int, float>();
|
||||
@@ -61,13 +63,20 @@ namespace XCharts
|
||||
for (int n = 0; n < serieList.Count; n++)
|
||||
{
|
||||
Serie serie = serieList[n];
|
||||
DrawXLineSerie(vh, serieCount, serie, ref dataCount, ref points, ref colors, ref seriesCurrHig);
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
Color color = m_ThemeInfo.GetColor(serieNameCount);
|
||||
DrawXLineSerie(vh, serieCount, color, serie, ref dataCount, ref points, ref colors, ref seriesCurrHig);
|
||||
if (serie.show)
|
||||
{
|
||||
serieCount++;
|
||||
}
|
||||
}
|
||||
DrawPoint(vh, dataCount, points, colors);
|
||||
DrawLinePoint(vh, dataCount, points, colors);
|
||||
}
|
||||
DrawXTooltipIndicator(vh);
|
||||
}
|
||||
@@ -80,6 +89,8 @@ namespace XCharts
|
||||
List<Vector3> points = new List<Vector3>();
|
||||
List<Color> colors = new List<Color>();
|
||||
int dataCount = 0;
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int j = 0; j < seriesCount; j++)
|
||||
{
|
||||
var seriesHig = new Dictionary<int, float>();
|
||||
@@ -87,18 +98,25 @@ namespace XCharts
|
||||
for (int n = 0; n < serieList.Count; n++)
|
||||
{
|
||||
Serie serie = serieList[n];
|
||||
DrawYLineSerie(vh, serieCount, serie, ref dataCount, ref points, ref colors, ref seriesHig);
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
Color color = m_ThemeInfo.GetColor(serieNameCount);
|
||||
DrawYLineSerie(vh, serieCount, color, serie, ref dataCount, ref points, ref colors, ref seriesHig);
|
||||
if (serie.show)
|
||||
{
|
||||
serieCount++;
|
||||
}
|
||||
}
|
||||
DrawPoint(vh, dataCount, points, colors);
|
||||
DrawLinePoint(vh, dataCount, points, colors);
|
||||
}
|
||||
DrawYTooltipIndicator(vh);
|
||||
}
|
||||
|
||||
private void DrawPoint(VertexHelper vh, int dataCount, List<Vector3> points, List<Color> colors)
|
||||
private void DrawLinePoint(VertexHelper vh, int dataCount, List<Vector3> points, List<Color> colors)
|
||||
{
|
||||
if (m_Line.point)
|
||||
{
|
||||
@@ -133,7 +151,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawXLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref int dataCount,
|
||||
private void DrawXLineSerie(VertexHelper vh, int serieIndex, Color color, Serie serie, ref int dataCount,
|
||||
ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig)
|
||||
{
|
||||
if (!IsActive(serie.index)) return;
|
||||
@@ -143,7 +161,6 @@ namespace XCharts
|
||||
List<float> yData = serie.GetYDataList(m_DataZoom);
|
||||
List<float> xData = serie.GetXDataList(m_DataZoom);
|
||||
|
||||
Color color = m_ThemeInfo.GetColor(serieIndex);
|
||||
Vector3 lp = Vector3.zero;
|
||||
Vector3 np = Vector3.zero;
|
||||
var yAxis = m_YAxises[serie.axisIndex];
|
||||
@@ -330,7 +347,7 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawYLineSerie(VertexHelper vh, int serieIndex, Serie serie, ref int dataCount,
|
||||
private void DrawYLineSerie(VertexHelper vh, int serieIndex, Color color, Serie serie, ref int dataCount,
|
||||
ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig)
|
||||
{
|
||||
if (!IsActive(serie.index)) return;
|
||||
@@ -338,7 +355,6 @@ namespace XCharts
|
||||
List<Vector3> lastSmoothPoints = new List<Vector3>();
|
||||
List<Vector3> smoothPoints = new List<Vector3>();
|
||||
|
||||
Color color = m_ThemeInfo.GetColor(serieIndex);
|
||||
Vector3 lp = Vector3.zero;
|
||||
Vector3 np = Vector3.zero;
|
||||
var xAxis = m_XAxises[serie.axisIndex];
|
||||
@@ -380,7 +396,6 @@ namespace XCharts
|
||||
float pX = seriesHig[i] + coordinateX + m_Coordinate.tickness;
|
||||
float dataHig = (value - xAxis.minValue) / (xAxis.maxValue - xAxis.minValue) * coordinateWid;
|
||||
np = new Vector3(pX + dataHig, pY);
|
||||
//Debug.LogError(gameObject.name +","+(pX + dataHig)+","+startY+","+scaleWid+","+xAxis.maxValue+","+xAxis.minValue);
|
||||
if (i > 0)
|
||||
{
|
||||
if (m_Line.step)
|
||||
@@ -473,18 +488,18 @@ namespace XCharts
|
||||
ChartHelper.DrawLine(vh, lp, np, m_Line.tickness, color);
|
||||
if (m_Line.area)
|
||||
{
|
||||
Vector3 alp = new Vector3(lp.x, lp.y - m_Line.tickness);
|
||||
Vector3 anp = new Vector3(np.x, np.y - m_Line.tickness);
|
||||
Vector3 alp = new Vector3(lp.x, lp.y);
|
||||
Vector3 anp = new Vector3(np.x, np.y);
|
||||
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
|
||||
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(coordinateX, coordinateY),
|
||||
new Vector3(coordinateX, coordinateY + coordinateHig));
|
||||
if (cross == Vector3.zero)
|
||||
{
|
||||
Vector3 tnp = serieIndex > 0 ?
|
||||
new Vector3(lastPoints[i].x, lastPoints[i].y + m_Line.tickness) :
|
||||
new Vector3(lastPoints[i].x + m_Coordinate.tickness, lastPoints[i].y) :
|
||||
new Vector3(coordinateX + m_Coordinate.tickness, np.y);
|
||||
Vector3 tlp = serieIndex > 0 ?
|
||||
new Vector3(lastPoints[i - 1].x, lastPoints[i - 1].y + m_Line.tickness) :
|
||||
new Vector3(lastPoints[i - 1].x + m_Coordinate.tickness, lastPoints[i - 1].y) :
|
||||
new Vector3(coordinateX + m_Coordinate.tickness, lp.y);
|
||||
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
|
||||
}
|
||||
|
||||
@@ -24,18 +24,15 @@ namespace XCharts
|
||||
/// <summary>
|
||||
/// Add a data to pie.
|
||||
/// </summary>
|
||||
/// <param name="legend">the name of data</param>
|
||||
/// <param name="serieName">the name of data</param>
|
||||
/// <param name="value">the data</param>
|
||||
public override void AddData(string legend, float value)
|
||||
public override void AddData(string serieName, float value)
|
||||
{
|
||||
m_Legend.AddData(legend);
|
||||
var serie = m_Series.AddData(legend, value);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.ClearData();
|
||||
serie.AddYData(value);
|
||||
RefreshChart();
|
||||
}
|
||||
m_Legend.AddData(serieName);
|
||||
var serie = m_Series.AddSerie(serieName, SerieType.Pie);
|
||||
serie.ClearData();
|
||||
serie.AddYData(value);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,17 +87,22 @@ namespace XCharts
|
||||
float dataTotal = GetDataTotal();
|
||||
float dataMax = GetDataMax();
|
||||
m_AngleList.Clear();
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
if (!IsActive(i))
|
||||
var serie = m_Series.series[i];
|
||||
serie.index = i;
|
||||
var data = serie.yData;
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
m_AngleList.Add(0);
|
||||
continue;
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
var data = m_Series.series[i].yData;
|
||||
if (data.Count <= 0)
|
||||
if (data.Count <= 0 || !serie.show)
|
||||
{
|
||||
m_AngleList.Add(0);
|
||||
m_AngleList.Add(i > 0 ? m_AngleList[i - 1] : 0);
|
||||
continue;
|
||||
}
|
||||
float value = data[0];
|
||||
@@ -125,12 +127,12 @@ namespace XCharts
|
||||
var offestCenter = new Vector3(m_PieCenter.x + offset * Mathf.Sin(currAngle),
|
||||
m_PieCenter.y + offset * Mathf.Cos(currAngle));
|
||||
ChartHelper.DrawDoughnut(vh, offestCenter, m_Pie.insideRadius, outSideRadius,
|
||||
startDegree, toDegree, m_ThemeInfo.GetColor(i));
|
||||
startDegree, toDegree, m_ThemeInfo.GetColor(serieNameCount));
|
||||
}
|
||||
else
|
||||
{
|
||||
ChartHelper.DrawDoughnut(vh, m_PieCenter, m_Pie.insideRadius, outSideRadius,
|
||||
startDegree, toDegree, m_ThemeInfo.GetColor(i));
|
||||
startDegree, toDegree, m_ThemeInfo.GetColor(serieNameCount));
|
||||
}
|
||||
m_AngleList.Add(toDegree);
|
||||
startDegree = toDegree;
|
||||
@@ -223,11 +225,11 @@ namespace XCharts
|
||||
float angle = VectorAngle(Vector2.up, dir);
|
||||
for (int i = m_AngleList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (i == 0 && angle < m_AngleList[i])
|
||||
if (i == 0)
|
||||
{
|
||||
return m_Tooltip.dataIndex[0] = 0;
|
||||
if (angle <= m_AngleList[i]) return m_Tooltip.dataIndex[0] = 0;
|
||||
}
|
||||
else if (angle < m_AngleList[i] && angle > m_AngleList[i - 1])
|
||||
else if (angle <= m_AngleList[i] && angle > m_AngleList[i - 1])
|
||||
{
|
||||
return m_Tooltip.dataIndex[0] = i;
|
||||
}
|
||||
@@ -282,7 +284,12 @@ namespace XCharts
|
||||
|
||||
public override void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
var local = transform.InverseTransformPoint(eventData.position);
|
||||
Vector2 local;
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
||||
eventData.position, canvas.worldCamera, out local))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var selectedIndex = GetPosPieIndex(local);
|
||||
if (selectedIndex != m_Pie.selectedIndex)
|
||||
{
|
||||
|
||||
@@ -170,8 +170,17 @@ namespace XCharts
|
||||
Vector3 firstPoint = Vector3.zero;
|
||||
dataPosList.Clear();
|
||||
dataPosList.Capacity = m_Series.Count;
|
||||
HashSet<string> serieNameSet = new HashSet<string>();
|
||||
int serieNameCount = -1;
|
||||
for (int i = 0; i < m_Series.Count; i++)
|
||||
{
|
||||
var serie = m_Series.series[i];
|
||||
if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
|
||||
else if (!serieNameSet.Contains(serie.name))
|
||||
{
|
||||
serieNameSet.Add(serie.name);
|
||||
serieNameCount++;
|
||||
}
|
||||
if (!IsActive(i))
|
||||
{
|
||||
dataPosList.Add(new List<Vector3>());
|
||||
|
||||
Reference in New Issue
Block a user