优化Legend的显示和控制

This commit is contained in:
monitor1394
2019-07-19 21:55:22 +08:00
parent 456d1ff81b
commit 3c17d7763b
12 changed files with 3891 additions and 410 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,6 @@ namespace XCharts
drawRect.height = EditorGUIUtility.singleLineHeight; drawRect.height = EditorGUIUtility.singleLineHeight;
SerializedProperty show = prop.FindPropertyRelative("m_Show"); SerializedProperty show = prop.FindPropertyRelative("m_Show");
SerializedProperty type = prop.FindPropertyRelative("m_Type"); SerializedProperty type = prop.FindPropertyRelative("m_Type");
SerializedProperty trigger = prop.FindPropertyRelative("m_Trigger");
ChartEditorHelper.MakeFoldout(ref drawRect, ref m_TooltipModuleToggle, "Tooltip", show); ChartEditorHelper.MakeFoldout(ref drawRect, ref m_TooltipModuleToggle, "Tooltip", show);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

View File

@@ -59,7 +59,7 @@ namespace XCharts
private void DrawYBarSerie(VertexHelper vh, int serieIndex, int stackCount, private void DrawYBarSerie(VertexHelper vh, int serieIndex, int stackCount,
Serie serie, Color color, ref Dictionary<int, float> seriesHig) 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 xAxis = m_XAxises[serie.axisIndex];
var yAxis = m_YAxises[serie.axisIndex]; var yAxis = m_YAxises[serie.axisIndex];
if (!yAxis.show) yAxis = m_YAxises[(serie.axisIndex + 1) % m_YAxises.Count]; 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, private void DrawXBarSerie(VertexHelper vh, int serieIndex, int stackCount,
Serie serie, Color color, ref Dictionary<int, float> seriesHig) 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); List<float> showData = serie.GetYDataList(m_DataZoom);
var yAxis = m_YAxises[serie.axisIndex]; var yAxis = m_YAxises[serie.axisIndex];
var xAxis = m_XAxises[serie.axisIndex]; var xAxis = m_XAxises[serie.axisIndex];
@@ -147,7 +147,8 @@ namespace XCharts
{ {
var stackSeries = m_Series.GetStackSeries(); var stackSeries = m_Series.GetStackSeries();
int seriesCount = stackSeries.Count; int seriesCount = stackSeries.Count;
int serieCount = 0; HashSet<string> serieNameSet = new HashSet<string>();
int serieNameCount = -1;
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
var seriesHig = new Dictionary<int, float>(); var seriesHig = new Dictionary<int, float>();
@@ -155,12 +156,15 @@ namespace XCharts
for (int n = 0; n < serieList.Count; n++) for (int n = 0; n < serieList.Count; n++)
{ {
Serie serie = serieList[n]; Serie serie = serieList[n];
Color color = m_ThemeInfo.GetColor(serieCount); if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
DrawYBarSerie(vh, j, seriesCount, serie, color, ref seriesHig); else if (!serieNameSet.Contains(serie.name))
if (serie.show)
{ {
serieCount++; serieNameSet.Add(serie.name);
serieNameCount++;
} }
Color color = m_ThemeInfo.GetColor(serieNameCount);
DrawYBarSerie(vh, j, seriesCount, serie, color, ref seriesHig);
} }
} }
DrawYTooltipIndicator(vh); DrawYTooltipIndicator(vh);
@@ -169,7 +173,8 @@ namespace XCharts
{ {
var stackSeries = m_Series.GetStackSeries(); var stackSeries = m_Series.GetStackSeries();
int seriesCount = stackSeries.Count; int seriesCount = stackSeries.Count;
int serieCount = 0; HashSet<string> serieNameSet = new HashSet<string>();
int serieNameCount = -1;
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
var seriesHig = new Dictionary<int, float>(); var seriesHig = new Dictionary<int, float>();
@@ -177,12 +182,14 @@ namespace XCharts
for (int n = 0; n < serieList.Count; n++) for (int n = 0; n < serieList.Count; n++)
{ {
Serie serie = serieList[n]; Serie serie = serieList[n];
Color color = m_ThemeInfo.GetColor(serieCount); if (string.IsNullOrEmpty(serie.name)) serieNameCount++;
DrawXBarSerie(vh, j, seriesCount, serie, color, ref seriesHig); else if (!serieNameSet.Contains(serie.name))
if (serie.show)
{ {
serieCount++; serieNameSet.Add(serie.name);
serieNameCount++;
} }
Color color = m_ThemeInfo.GetColor(serieNameCount);
DrawXBarSerie(vh, j, seriesCount, serie, color, ref seriesHig);
} }
} }
DrawXTooltipIndicator(vh); DrawXTooltipIndicator(vh);

View File

@@ -38,8 +38,9 @@ namespace XCharts
[NonSerialized] private Legend m_CheckLegend = Legend.defaultLegend; [NonSerialized] private Legend m_CheckLegend = Legend.defaultLegend;
[NonSerialized] private float m_CheckWidth = 0; [NonSerialized] private float m_CheckWidth = 0;
[NonSerialized] private float m_CheckHeight = 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] private bool m_RefreshChart = false;
[NonSerialized] protected List<Text> m_LegendTextList = new List<Text>();
protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } } protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } }
protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } } protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } }
@@ -200,8 +201,11 @@ namespace XCharts
/// <param name="active">Active or not</param> /// <param name="active">Active or not</param>
public virtual void SetActive(string serieName, bool active) public virtual void SetActive(string serieName, bool active)
{ {
m_Legend.SetActive(serieName, active); var serie = m_Series.GetSerie(serieName);
m_Series.SetActive(serieName, active); if (serie != null)
{
SetActive(serie.index, active);
}
} }
/// <summary> /// <summary>
@@ -211,8 +215,13 @@ namespace XCharts
/// <param name="active">Active or not</param> /// <param name="active">Active or not</param>
public virtual void SetActive(int serieIndex, bool active) public virtual void SetActive(int serieIndex, bool active)
{ {
m_Legend.SetActive(serieIndex, active);
m_Series.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> /// <summary>
@@ -222,7 +231,7 @@ namespace XCharts
/// <returns>True when activated</returns> /// <returns>True when activated</returns>
public virtual bool IsActive(string serieName) public virtual bool IsActive(string serieName)
{ {
return m_Legend.IsActive(serieName) || m_Series.IsActive(serieName); return m_Series.IsActive(serieName);
} }
/// <summary> /// <summary>
@@ -232,7 +241,7 @@ namespace XCharts
/// <returns>True when activated</returns> /// <returns>True when activated</returns>
public virtual bool IsActive(int serieIndex) public virtual bool IsActive(int serieIndex)
{ {
return m_Legend.IsActive(serieIndex) && m_Series.IsActive(serieIndex); return m_Series.IsActive(serieIndex);
} }
/// <summary> /// <summary>
@@ -371,23 +380,40 @@ namespace XCharts
legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight); legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight);
ChartHelper.HideAllObject(legendObject, s_LegendObjectName); 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, m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor,
anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight)); anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight));
var bgColor = IsActive(legendName) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor;
m_Legend.SetButton(i, btn); m_Legend.SetButton(legendName, btn, datas.Count);
m_Legend.SetActive(i, IsActive(i)); m_Legend.UpdateButtonColor(legendName, bgColor);
m_Legend.UpdateButtonColor(i, m_ThemeInfo.GetColor(i), m_ThemeInfo.legendUnableColor); btn.GetComponentInChildren<Text>().text = legendName;
btn.GetComponentInChildren<Text>().text = m_Legend.data[i];
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) => ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) =>
{ {
int count = (data as PointerEventData).clickCount; if (data.selectedObject == null) return;
int index = int.Parse(data.selectedObject.name.Split('_')[1]); string selectedName = data.selectedObject.name.Split('_')[1];
SetActive(index, !m_Legend.IsActive(index)); foreach (var serie in m_Series.GetSeries(selectedName))
m_Legend.UpdateButtonColor(index, m_ThemeInfo.GetColor(index), {
m_ThemeInfo.legendUnableColor); SetActive(serie.index, !serie.show);
}
OnYMaxValueChanged(); OnYMaxValueChanged();
OnLegendButtonClicked(); OnLegendButtonClicked();
RefreshChart(); RefreshChart();
@@ -459,6 +485,23 @@ namespace XCharts
m_CheckLegend.Copy(m_Legend); m_CheckLegend.Copy(m_Legend);
OnLegendChanged(); 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() private void CheckTooltip()

View File

@@ -1,5 +1,4 @@
using System; using UnityEngine;
using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -28,8 +27,6 @@ namespace XCharts
private List<YAxis> m_CheckYAxises = new List<YAxis>(); private List<YAxis> m_CheckYAxises = new List<YAxis>();
private Coordinate m_CheckCoordinate = Coordinate.defaultCoordinate; 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 coordinateX { get { return m_Coordinate.left; } }
public float coordinateY { get { return m_Coordinate.bottom; } } public float coordinateY { get { return m_Coordinate.bottom; } }
public float coordinateWid { get { return chartWidth - m_Coordinate.left - m_Coordinate.right; } } public float coordinateWid { get { return chartWidth - m_Coordinate.left - m_Coordinate.right; } }
@@ -89,9 +86,9 @@ namespace XCharts
} }
/// <summary> /// <summary>
/// Whether is Cartesian coordinates /// Whether is Cartesian coordinates, reutrn true when all the show axis is `Value` type.
/// </summary> /// </summary>
/// <returns>reutrn true when all show axis is value type</returns> /// <returns></returns>
public bool IsCartesian() public bool IsCartesian()
{ {
foreach (var axis in m_XAxises) foreach (var axis in m_XAxises)
@@ -159,7 +156,7 @@ namespace XCharts
{ {
var xAxis = m_XAxises[i]; var xAxis = m_XAxises[i];
var yAxis = m_YAxises[i]; var yAxis = m_YAxises[i];
if(!xAxis.show && !yAxis.show) continue; if (!xAxis.show && !yAxis.show) continue;
if (isCartesian && xAxis.show && yAxis.show) if (isCartesian && xAxis.show && yAxis.show)
{ {
var yRate = (yAxis.maxValue - yAxis.minValue) / coordinateHig; var yRate = (yAxis.maxValue - yAxis.minValue) / coordinateHig;
@@ -234,7 +231,9 @@ namespace XCharts
m_Tooltip.UpdateLastDataIndex(); m_Tooltip.UpdateLastDataIndex();
RefreshChart(); RefreshChart();
} }
}else{ }
else
{
m_Tooltip.SetActive(false); m_Tooltip.SetActive(false);
} }
} }
@@ -278,7 +277,7 @@ namespace XCharts
{ {
string key = serie.name; string key = serie.name;
//if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(i); //if (string.IsNullOrEmpty(key)) key = m_Legend.GetData(i);
if(!string.IsNullOrEmpty(key)) key += ":"; if (!string.IsNullOrEmpty(key)) key += " : ";
float xValue, yValue; float xValue, yValue;
serie.GetXYData(index, m_DataZoom, out xValue, out yValue); serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
if (isCartesian) if (isCartesian)
@@ -891,7 +890,7 @@ namespace XCharts
if (xAxis.IsValue() && xAxisIndex > 0) lineY += coordinateHig; if (xAxis.IsValue() && xAxisIndex > 0) lineY += coordinateHig;
var left = new Vector3(coordinateX - m_Coordinate.tickness, lineY); var left = new Vector3(coordinateX - m_Coordinate.tickness, lineY);
var top = new Vector3(coordinateX + coordinateWid + 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) if (xAxis.axisLine.symbol)
{ {
var axisLine = xAxis.axisLine; var axisLine = xAxis.axisLine;
@@ -1140,7 +1139,12 @@ namespace XCharts
public override void OnBeginDrag(PointerEventData eventData) 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)) if (m_DataZoom.IsInStartZoom(pos, coordinateX, coordinateWid))
{ {
m_DataZoom.isDraging = true; m_DataZoom.isDraging = true;
@@ -1160,7 +1164,6 @@ namespace XCharts
public override void OnDrag(PointerEventData eventData) public override void OnDrag(PointerEventData eventData)
{ {
//Debug.LogError("drag");
float deltaX = eventData.delta.x; float deltaX = eventData.delta.x;
float deltaPercent = deltaX / coordinateWid * 100; float deltaPercent = deltaX / coordinateWid * 100;
if (m_DataZoomStartDrag) if (m_DataZoomStartDrag)
@@ -1257,7 +1260,12 @@ namespace XCharts
public override void OnPointerDown(PointerEventData eventData) 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) || if (m_DataZoom.IsInStartZoom(localPos, coordinateX, coordinateWid) ||
m_DataZoom.IsInEndZoom(localPos, coordinateX, coordinateWid)) m_DataZoom.IsInEndZoom(localPos, coordinateX, coordinateWid))
{ {

View File

@@ -17,8 +17,7 @@ namespace XCharts
[SerializeField] private int m_ItemFontSize = 18; [SerializeField] private int m_ItemFontSize = 18;
[SerializeField] private List<string> m_Data = new List<string>(); [SerializeField] private List<string> m_Data = new List<string>();
[NonSerialized] private List<bool> m_DataActiveList = new List<bool>(); private Dictionary<string, Button> m_DataBtnList = new Dictionary<string, Button>();
[NonSerialized] private List<Button> m_DataBtnList = new List<Button>();
public bool show { get { return m_Show; } set { m_Show = value; } } public bool show { get { return m_Show; } set { m_Show = value; } }
@@ -126,24 +125,6 @@ namespace XCharts
return base.GetHashCode(); 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() public void ClearData()
{ {
m_Data.Clear(); m_Data.Clear();
@@ -179,48 +160,25 @@ namespace XCharts
return null; 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++) int index = m_DataBtnList.Values.Count;
{ btn.transform.localPosition = GetButtonLocationPosition(total,index);
if (data[i].Equals(name)) m_DataBtnList[name] = btn;
{
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;
}
btn.gameObject.SetActive(show); 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; m_DataBtnList[name].GetComponent<Image>().color = color;
}
else
{
m_DataBtnList[index].GetComponent<Image>().color = unableColor;
} }
} }
@@ -229,9 +187,8 @@ namespace XCharts
m_Location.OnChanged(); m_Location.OnChanged();
} }
private Vector2 GetButtonLocationPosition(int index) private Vector2 GetButtonLocationPosition(int size,int index)
{ {
int size = m_Data.Count;
switch (m_Orient) switch (m_Orient)
{ {
case Orient.Vertical: case Orient.Vertical:

View File

@@ -7,6 +7,7 @@ namespace XCharts
{ {
public enum SerieType public enum SerieType
{ {
None,
Line, Line,
Bar, Bar,
Pie, Pie,

View File

@@ -20,6 +20,7 @@ namespace XCharts
{ {
m_Series = new List<Serie>(){new Serie(){ m_Series = new List<Serie>(){new Serie(){
show = true, show = true,
name = "serie1",
index = 0 index = 0
}} }}
}; };
@@ -53,12 +54,24 @@ namespace XCharts
{ {
if (name.Equals(m_Series[i].name)) if (name.Equals(m_Series[i].name))
{ {
m_Series[i].index = i;
return m_Series[i]; return m_Series[i];
} }
} }
return null; 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) public Serie GetSerie(int index)
{ {
if (index >= 0 && index < m_Series.Count) if (index >= 0 && index < m_Series.Count)
@@ -107,16 +120,15 @@ namespace XCharts
if (serie == null) if (serie == null)
{ {
serie = new Serie(); serie = new Serie();
serie.index = m_Series.Count;
serie.type = type; serie.type = type;
serie.show = show; serie.show = show;
serie.name = serieName; serie.name = serieName;
serie.index = m_Series.Count;
serie.yData = new List<float>(); serie.yData = new List<float>();
m_Series.Add(serie); m_Series.Add(serie);
} }
else else
{ {
serie.type = type;
serie.show = show; serie.show = show;
} }
return serie; return serie;
@@ -124,19 +136,7 @@ namespace XCharts
public Serie AddData(string name, float value, int maxDataNumber = 0) public Serie AddData(string name, float value, int maxDataNumber = 0)
{ {
if (m_Series == null) var serie = AddSerie(name, SerieType.None);
{
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);
}
serie.AddYData(value, maxDataNumber); serie.AddYData(value, maxDataNumber);
return serie; return serie;
} }
@@ -416,6 +416,19 @@ namespace XCharts
return stackSeries; 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) public override void ParseJsonData(string jsonData)
{ {
//TODO: //TODO:

View File

@@ -54,6 +54,8 @@ namespace XCharts
List<Vector3> points = new List<Vector3>(); List<Vector3> points = new List<Vector3>();
List<Color> colors = new List<Color>(); List<Color> colors = new List<Color>();
int dataCount = 0; int dataCount = 0;
HashSet<string> serieNameSet = new HashSet<string>();
int serieNameCount = -1;
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
var seriesCurrHig = new Dictionary<int, float>(); var seriesCurrHig = new Dictionary<int, float>();
@@ -61,13 +63,20 @@ namespace XCharts
for (int n = 0; n < serieList.Count; n++) for (int n = 0; n < serieList.Count; n++)
{ {
Serie serie = serieList[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) if (serie.show)
{ {
serieCount++; serieCount++;
} }
} }
DrawPoint(vh, dataCount, points, colors); DrawLinePoint(vh, dataCount, points, colors);
} }
DrawXTooltipIndicator(vh); DrawXTooltipIndicator(vh);
} }
@@ -80,6 +89,8 @@ namespace XCharts
List<Vector3> points = new List<Vector3>(); List<Vector3> points = new List<Vector3>();
List<Color> colors = new List<Color>(); List<Color> colors = new List<Color>();
int dataCount = 0; int dataCount = 0;
HashSet<string> serieNameSet = new HashSet<string>();
int serieNameCount = -1;
for (int j = 0; j < seriesCount; j++) for (int j = 0; j < seriesCount; j++)
{ {
var seriesHig = new Dictionary<int, float>(); var seriesHig = new Dictionary<int, float>();
@@ -87,18 +98,25 @@ namespace XCharts
for (int n = 0; n < serieList.Count; n++) for (int n = 0; n < serieList.Count; n++)
{ {
Serie serie = serieList[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) if (serie.show)
{ {
serieCount++; serieCount++;
} }
} }
DrawPoint(vh, dataCount, points, colors); DrawLinePoint(vh, dataCount, points, colors);
} }
DrawYTooltipIndicator(vh); 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) 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) ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig)
{ {
if (!IsActive(serie.index)) return; if (!IsActive(serie.index)) return;
@@ -143,7 +161,6 @@ namespace XCharts
List<float> yData = serie.GetYDataList(m_DataZoom); List<float> yData = serie.GetYDataList(m_DataZoom);
List<float> xData = serie.GetXDataList(m_DataZoom); List<float> xData = serie.GetXDataList(m_DataZoom);
Color color = m_ThemeInfo.GetColor(serieIndex);
Vector3 lp = Vector3.zero; Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero; Vector3 np = Vector3.zero;
var yAxis = m_YAxises[serie.axisIndex]; 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) ref List<Vector3> points, ref List<Color> colors, ref Dictionary<int, float> seriesHig)
{ {
if (!IsActive(serie.index)) return; if (!IsActive(serie.index)) return;
@@ -338,7 +355,6 @@ namespace XCharts
List<Vector3> lastSmoothPoints = new List<Vector3>(); List<Vector3> lastSmoothPoints = new List<Vector3>();
List<Vector3> smoothPoints = new List<Vector3>(); List<Vector3> smoothPoints = new List<Vector3>();
Color color = m_ThemeInfo.GetColor(serieIndex);
Vector3 lp = Vector3.zero; Vector3 lp = Vector3.zero;
Vector3 np = Vector3.zero; Vector3 np = Vector3.zero;
var xAxis = m_XAxises[serie.axisIndex]; var xAxis = m_XAxises[serie.axisIndex];
@@ -380,7 +396,6 @@ namespace XCharts
float pX = seriesHig[i] + coordinateX + m_Coordinate.tickness; float pX = seriesHig[i] + coordinateX + m_Coordinate.tickness;
float dataHig = (value - xAxis.minValue) / (xAxis.maxValue - xAxis.minValue) * coordinateWid; float dataHig = (value - xAxis.minValue) / (xAxis.maxValue - xAxis.minValue) * coordinateWid;
np = new Vector3(pX + dataHig, pY); np = new Vector3(pX + dataHig, pY);
//Debug.LogError(gameObject.name +","+(pX + dataHig)+","+startY+","+scaleWid+","+xAxis.maxValue+","+xAxis.minValue);
if (i > 0) if (i > 0)
{ {
if (m_Line.step) if (m_Line.step)
@@ -473,18 +488,18 @@ namespace XCharts
ChartHelper.DrawLine(vh, lp, np, m_Line.tickness, color); ChartHelper.DrawLine(vh, lp, np, m_Line.tickness, color);
if (m_Line.area) if (m_Line.area)
{ {
Vector3 alp = new Vector3(lp.x, lp.y - m_Line.tickness); Vector3 alp = new Vector3(lp.x, lp.y);
Vector3 anp = new Vector3(np.x, np.y - m_Line.tickness); Vector3 anp = new Vector3(np.x, np.y);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f); Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(coordinateX, coordinateY), var cross = ChartHelper.GetIntersection(lp, np, new Vector3(coordinateX, coordinateY),
new Vector3(coordinateX, coordinateY + coordinateHig)); new Vector3(coordinateX, coordinateY + coordinateHig));
if (cross == Vector3.zero) if (cross == Vector3.zero)
{ {
Vector3 tnp = serieIndex > 0 ? 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); new Vector3(coordinateX + m_Coordinate.tickness, np.y);
Vector3 tlp = serieIndex > 0 ? 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); new Vector3(coordinateX + m_Coordinate.tickness, lp.y);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor); ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
} }

View File

@@ -24,18 +24,15 @@ namespace XCharts
/// <summary> /// <summary>
/// Add a data to pie. /// Add a data to pie.
/// </summary> /// </summary>
/// <param name="legend">the name of data</param> /// <param name="serieName">the name of data</param>
/// <param name="value">the 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); m_Legend.AddData(serieName);
var serie = m_Series.AddData(legend, value); var serie = m_Series.AddSerie(serieName, SerieType.Pie);
if (serie != null) serie.ClearData();
{ serie.AddYData(value);
serie.ClearData(); RefreshChart();
serie.AddYData(value);
RefreshChart();
}
} }
/// <summary> /// <summary>
@@ -90,17 +87,22 @@ namespace XCharts
float dataTotal = GetDataTotal(); float dataTotal = GetDataTotal();
float dataMax = GetDataMax(); float dataMax = GetDataMax();
m_AngleList.Clear(); m_AngleList.Clear();
HashSet<string> serieNameSet = new HashSet<string>();
int serieNameCount = -1;
for (int i = 0; i < m_Series.Count; i++) 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); serieNameSet.Add(serie.name);
continue; serieNameCount++;
} }
var data = m_Series.series[i].yData; if (data.Count <= 0 || !serie.show)
if (data.Count <= 0)
{ {
m_AngleList.Add(0); m_AngleList.Add(i > 0 ? m_AngleList[i - 1] : 0);
continue; continue;
} }
float value = data[0]; float value = data[0];
@@ -125,12 +127,12 @@ namespace XCharts
var offestCenter = new Vector3(m_PieCenter.x + offset * Mathf.Sin(currAngle), var offestCenter = new Vector3(m_PieCenter.x + offset * Mathf.Sin(currAngle),
m_PieCenter.y + offset * Mathf.Cos(currAngle)); m_PieCenter.y + offset * Mathf.Cos(currAngle));
ChartHelper.DrawDoughnut(vh, offestCenter, m_Pie.insideRadius, outSideRadius, ChartHelper.DrawDoughnut(vh, offestCenter, m_Pie.insideRadius, outSideRadius,
startDegree, toDegree, m_ThemeInfo.GetColor(i)); startDegree, toDegree, m_ThemeInfo.GetColor(serieNameCount));
} }
else else
{ {
ChartHelper.DrawDoughnut(vh, m_PieCenter, m_Pie.insideRadius, outSideRadius, 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); m_AngleList.Add(toDegree);
startDegree = toDegree; startDegree = toDegree;
@@ -223,11 +225,11 @@ namespace XCharts
float angle = VectorAngle(Vector2.up, dir); float angle = VectorAngle(Vector2.up, dir);
for (int i = m_AngleList.Count - 1; i >= 0; i--) 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; return m_Tooltip.dataIndex[0] = i;
} }
@@ -282,7 +284,12 @@ namespace XCharts
public override void OnPointerDown(PointerEventData eventData) 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); var selectedIndex = GetPosPieIndex(local);
if (selectedIndex != m_Pie.selectedIndex) if (selectedIndex != m_Pie.selectedIndex)
{ {

View File

@@ -170,8 +170,17 @@ namespace XCharts
Vector3 firstPoint = Vector3.zero; Vector3 firstPoint = Vector3.zero;
dataPosList.Clear(); dataPosList.Clear();
dataPosList.Capacity = m_Series.Count; dataPosList.Capacity = m_Series.Count;
HashSet<string> serieNameSet = new HashSet<string>();
int serieNameCount = -1;
for (int i = 0; i < m_Series.Count; i++) 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)) if (!IsActive(i))
{ {
dataPosList.Add(new List<Vector3>()); dataPosList.Add(new List<Vector3>());

View File

@@ -23,6 +23,7 @@ QQ交流群XCharts交流群202030963
## 更新日志 ## 更新日志
* 2019.07.19)优化`Legend`的显示和控制
* 2019.07.18)优化抗锯齿,曲线更平滑 * 2019.07.18)优化抗锯齿,曲线更平滑
* 2019.07.18)增加`Tooltip`指示器类型,优化显示控制 * 2019.07.18)增加`Tooltip`指示器类型,优化显示控制
* 2019.07.15)增加`Size`设置图表尺寸 * 2019.07.15)增加`Size`设置图表尺寸