mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-19 15:00:08 +00:00
增加PieChart多饼图支持
This commit is contained in:
@@ -12,7 +12,7 @@ namespace XCharts
|
||||
Vertical
|
||||
}
|
||||
|
||||
public class BaseChart : Graphic, IPointerDownHandler, IPointerUpHandler,
|
||||
public partial class BaseChart : Graphic, IPointerDownHandler, IPointerUpHandler,
|
||||
IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler,
|
||||
IDragHandler, IEndDragHandler, IScrollHandler
|
||||
{
|
||||
@@ -26,7 +26,6 @@ namespace XCharts
|
||||
[SerializeField] protected Legend m_Legend = Legend.defaultLegend;
|
||||
[SerializeField] protected Tooltip m_Tooltip = Tooltip.defaultTooltip;
|
||||
[SerializeField] protected Series m_Series = Series.defaultSeries;
|
||||
|
||||
[SerializeField] protected float m_Large = 1;
|
||||
[SerializeField] protected int m_MinShowDataNumber;
|
||||
[SerializeField] protected int m_MaxShowDataNumber;
|
||||
@@ -45,278 +44,6 @@ namespace XCharts
|
||||
protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } }
|
||||
protected Vector2 chartPivot { get { return rectTransform.pivot; } }
|
||||
|
||||
public Title title { get { return m_Title; } }
|
||||
public Legend legend { get { return m_Legend; } }
|
||||
public Tooltip tooltip { get { return m_Tooltip; } }
|
||||
public Series series { get { return m_Series; } }
|
||||
|
||||
public float chartWidth { get { return m_ChartWidth; } }
|
||||
public float chartHeight { get { return m_ChartHeight; } }
|
||||
|
||||
/// <summary>
|
||||
/// The min number of data to show in chart.
|
||||
/// </summary>
|
||||
public int minShowDataNumber
|
||||
{
|
||||
get { return m_MinShowDataNumber; }
|
||||
set { m_MinShowDataNumber = value; if (m_MinShowDataNumber < 0) m_MinShowDataNumber = 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The max number of data to show in chart.
|
||||
/// </summary>
|
||||
public int maxShowDataNumber
|
||||
{
|
||||
get { return m_MaxShowDataNumber; }
|
||||
set { m_MaxShowDataNumber = value; if (m_MaxShowDataNumber < 0) m_MaxShowDataNumber = 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The max number of serie and axis data cache.
|
||||
/// The first data will be remove when the size of serie and axis data is larger then maxCacheDataNumber.
|
||||
/// default:0,unlimited.
|
||||
/// </summary>
|
||||
public int maxCacheDataNumber
|
||||
{
|
||||
get { return m_MaxCacheDataNumber; }
|
||||
set { m_MaxCacheDataNumber = value; if (m_MaxCacheDataNumber < 0) m_MaxCacheDataNumber = 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the size of chart.
|
||||
/// </summary>
|
||||
/// <param name="width">width</param>
|
||||
/// <param name="height">height</param>
|
||||
public virtual void SetSize(float width, float height)
|
||||
{
|
||||
m_ChartWidth = width;
|
||||
m_ChartHeight = height;
|
||||
m_CheckWidth = width;
|
||||
m_CheckHeight = height;
|
||||
rectTransform.sizeDelta = new Vector2(m_ChartWidth, m_ChartHeight);
|
||||
OnSizeChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all series and legend data.
|
||||
/// It just emptying all of serie's data without emptying the list of series.
|
||||
/// </summary>
|
||||
public virtual void ClearData()
|
||||
{
|
||||
m_Series.ClearData();
|
||||
m_Legend.ClearData();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove legend and serie by name.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
public virtual void RemoveData(string serieName)
|
||||
{
|
||||
m_Series.Remove(serieName);
|
||||
m_Legend.RemoveData(serieName);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all data from series and legend.
|
||||
/// The series list is also cleared.
|
||||
/// </summary>
|
||||
public virtual void RemoveData()
|
||||
{
|
||||
m_Legend.ClearData();
|
||||
m_Series.RemoveAll();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a serie to serie list.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="type">the type of serie</param>
|
||||
/// <param name="show">whether to show this serie</param>
|
||||
/// <returns>the added serie</returns>
|
||||
public virtual Serie AddSerie(string serieName, SerieType type, bool show = true)
|
||||
{
|
||||
m_Legend.AddData(serieName);
|
||||
return m_Series.AddSerie(serieName, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a data to serie.
|
||||
/// If serieName doesn't exist in legend,will be add to legend.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="value">the data to add</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, float value)
|
||||
{
|
||||
m_Legend.AddData(serieName);
|
||||
var success = m_Series.AddData(serieName, value, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a data to serie.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="value">the data to add</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, float value)
|
||||
{
|
||||
var success = m_Series.AddData(serieIndex, value, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an arbitray dimension data to serie,such as (x,y,z,...).
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="multidimensionalData">the (x,y,z,...) data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, List<float> multidimensionalData)
|
||||
{
|
||||
var success = m_Series.AddData(serieName, multidimensionalData, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an arbitray dimension data to serie,such as (x,y,z,...).
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie,index starts at 0</param>
|
||||
/// <param name="multidimensionalData">the (x,y,z,...) data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, List<float> multidimensionalData)
|
||||
{
|
||||
var success = m_Series.AddData(serieIndex, multidimensionalData, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a (x,y) data to serie.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="xValue">x data</param>
|
||||
/// <param name="yValue">y data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, float xValue, float yValue)
|
||||
{
|
||||
var success = m_Series.AddXYData(serieName, xValue, yValue, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a (x,y) data to serie.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="xValue">x data</param>
|
||||
/// <param name="yValue">y data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, float xValue, float yValue)
|
||||
{
|
||||
var success = m_Series.AddXYData(serieIndex, xValue, yValue, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
/// <summary>
|
||||
/// Update serie data by serie name.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="value">the data will be update</param>
|
||||
/// <param name="dataIndex">the index of data</param>
|
||||
public virtual void UpdateData(string serieName, float value, int dataIndex = 0)
|
||||
{
|
||||
m_Series.UpdateData(serieName, value, dataIndex);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data by serie index.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="value">the data will be update</param>
|
||||
/// <param name="dataIndex">the index of data</param>
|
||||
public virtual void UpdateData(int serieIndex, float value, int dataIndex = 0)
|
||||
{
|
||||
m_Series.UpdateData(serieIndex, value, dataIndex);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show serie and legend.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(string serieName, bool active)
|
||||
{
|
||||
var serie = m_Series.GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
SetActive(serie.index, active);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show serie and legend.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(int serieIndex, bool 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>
|
||||
/// Whether serie is activated.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(string serieName)
|
||||
{
|
||||
return m_Series.IsActive(serieName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether serie is activated.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(int serieIndex)
|
||||
{
|
||||
return m_Series.IsActive(serieIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redraw chart next frame.
|
||||
/// </summary>
|
||||
public void RefreshChart()
|
||||
{
|
||||
m_RefreshChart = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update chart theme
|
||||
/// </summary>
|
||||
/// <param name="theme">theme</param>
|
||||
public void UpdateTheme(Theme theme)
|
||||
{
|
||||
m_ThemeInfo.theme = theme;
|
||||
OnThemeChanged();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
if (m_ThemeInfo == null)
|
||||
@@ -338,6 +65,10 @@ namespace XCharts
|
||||
TransferOldVersionData();
|
||||
}
|
||||
|
||||
protected override void Start() {
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
CheckSize();
|
||||
@@ -390,12 +121,22 @@ namespace XCharts
|
||||
{
|
||||
foreach (var serie in m_Series.series)
|
||||
{
|
||||
if (serie.yData.Count > 0 && serie.data.Count == 0)
|
||||
if (serie.yData.Count <= 0) continue;
|
||||
bool needTransfer = true;
|
||||
foreach (var sd in serie.data)
|
||||
{
|
||||
foreach (var value in sd.data)
|
||||
{
|
||||
if (value != 0) needTransfer = false;
|
||||
}
|
||||
}
|
||||
if (needTransfer)
|
||||
{
|
||||
serie.data.Clear();
|
||||
for (int i = 0; i < serie.yData.Count; i++)
|
||||
{
|
||||
float xvalue, yvalue;
|
||||
serie.GetXYData(i, null, out xvalue, out yvalue);
|
||||
float xvalue = i < serie.xData.Count ? serie.xData[i] : i;
|
||||
float yvalue = serie.yData[i];
|
||||
var serieData = new SerieData();
|
||||
serieData.data = new List<float>() { xvalue, yvalue };
|
||||
serie.data.Add(serieData);
|
||||
@@ -472,24 +213,37 @@ namespace XCharts
|
||||
for (int i = 0; i < datas.Count; i++)
|
||||
{
|
||||
string legendName = datas[i];
|
||||
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + legendName, legendObject.transform,
|
||||
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i + "_" + legendName, legendObject.transform,
|
||||
m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor,
|
||||
anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight));
|
||||
var bgColor = IsActive(legendName) ? m_ThemeInfo.GetColor(i) : m_ThemeInfo.legendUnableColor;
|
||||
var bgColor = IsLegendActive(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.ClearEventListener(btn.gameObject);
|
||||
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) =>
|
||||
{
|
||||
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();
|
||||
var temp = data.selectedObject.name.Split('_');
|
||||
string selectedName = temp[2];
|
||||
int index = int.Parse(temp[1]);
|
||||
OnLegendButtonClick(index, selectedName);
|
||||
});
|
||||
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerEnter, (data) =>
|
||||
{
|
||||
if (btn == null) return;
|
||||
var temp = btn.name.Split('_');
|
||||
string selectedName = temp[2];
|
||||
int index = int.Parse(temp[1]);
|
||||
OnLegendButtonEnter(index, selectedName);
|
||||
});
|
||||
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerExit, (data) =>
|
||||
{
|
||||
if (btn == null) return;
|
||||
var temp = btn.name.Split('_');
|
||||
string selectedName = temp[2];
|
||||
int index = int.Parse(temp[1]);
|
||||
OnLegendButtonExit(index, selectedName);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -590,22 +344,25 @@ namespace XCharts
|
||||
}
|
||||
return;
|
||||
}
|
||||
m_Tooltip.dataIndex[0] = m_Tooltip.dataIndex[1] = -1;
|
||||
for (int i = 0; i < m_Tooltip.dataIndex.Count; i++)
|
||||
{
|
||||
m_Tooltip.dataIndex[i] = -1;
|
||||
}
|
||||
Vector2 local;
|
||||
if (canvas == null) return;
|
||||
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
||||
Input.mousePosition, canvas.worldCamera, out local))
|
||||
{
|
||||
if(m_Tooltip.IsActive()) RefreshChart();
|
||||
m_Tooltip.SetActive(false);
|
||||
RefreshChart();
|
||||
return;
|
||||
}
|
||||
if (local.x < 0 || local.x > chartWidth ||
|
||||
local.y < 0 || local.y > chartHeight)
|
||||
{
|
||||
if(m_Tooltip.IsActive()) RefreshChart();
|
||||
m_Tooltip.SetActive(false);
|
||||
RefreshChart();
|
||||
return;
|
||||
}
|
||||
m_Tooltip.pointerPos = local;
|
||||
@@ -667,8 +424,26 @@ namespace XCharts
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnLegendButtonClicked()
|
||||
protected virtual void OnLegendButtonClick(int index, string legendName)
|
||||
{
|
||||
foreach (var serie in m_Series.GetSeries(legendName))
|
||||
{
|
||||
SetActive(serie.index, !serie.show);
|
||||
}
|
||||
OnYMaxValueChanged();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
protected virtual void OnLegendButtonEnter(int index, string legendName)
|
||||
{
|
||||
var serie = m_Series.GetSerie(index);
|
||||
serie.highlighted = true;
|
||||
}
|
||||
|
||||
protected virtual void OnLegendButtonExit(int index, string legendName)
|
||||
{
|
||||
var serie = m_Series.GetSerie(index);
|
||||
serie.highlighted = false;
|
||||
}
|
||||
|
||||
protected virtual void RefreshTooltip()
|
||||
@@ -701,7 +476,8 @@ namespace XCharts
|
||||
ChartHelper.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.backgroundColor);
|
||||
}
|
||||
|
||||
protected void DrawSymbol(VertexHelper vh, SerieSymbolType type, float symbolSize, float tickness, Vector3 pos, Color color)
|
||||
protected void DrawSymbol(VertexHelper vh, SerieSymbolType type, float symbolSize,
|
||||
float tickness, Vector3 pos, Color color)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
|
||||
292
Scripts/UI/Internal/BaseChart_API.cs
Normal file
292
Scripts/UI/Internal/BaseChart_API.cs
Normal file
@@ -0,0 +1,292 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public partial class BaseChart
|
||||
{
|
||||
public Title title { get { return m_Title; } }
|
||||
public Legend legend { get { return m_Legend; } }
|
||||
public Tooltip tooltip { get { return m_Tooltip; } }
|
||||
public Series series { get { return m_Series; } }
|
||||
|
||||
public float chartWidth { get { return m_ChartWidth; } }
|
||||
public float chartHeight { get { return m_ChartHeight; } }
|
||||
|
||||
/// <summary>
|
||||
/// The min number of data to show in chart.
|
||||
/// </summary>
|
||||
public int minShowDataNumber
|
||||
{
|
||||
get { return m_MinShowDataNumber; }
|
||||
set { m_MinShowDataNumber = value; if (m_MinShowDataNumber < 0) m_MinShowDataNumber = 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The max number of data to show in chart.
|
||||
/// </summary>
|
||||
public int maxShowDataNumber
|
||||
{
|
||||
get { return m_MaxShowDataNumber; }
|
||||
set { m_MaxShowDataNumber = value; if (m_MaxShowDataNumber < 0) m_MaxShowDataNumber = 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The max number of serie and axis data cache.
|
||||
/// The first data will be remove when the size of serie and axis data is larger then maxCacheDataNumber.
|
||||
/// default:0,unlimited.
|
||||
/// </summary>
|
||||
public int maxCacheDataNumber
|
||||
{
|
||||
get { return m_MaxCacheDataNumber; }
|
||||
set { m_MaxCacheDataNumber = value; if (m_MaxCacheDataNumber < 0) m_MaxCacheDataNumber = 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the size of chart.
|
||||
/// </summary>
|
||||
/// <param name="width">width</param>
|
||||
/// <param name="height">height</param>
|
||||
public virtual void SetSize(float width, float height)
|
||||
{
|
||||
m_ChartWidth = width;
|
||||
m_ChartHeight = height;
|
||||
m_CheckWidth = width;
|
||||
m_CheckHeight = height;
|
||||
rectTransform.sizeDelta = new Vector2(m_ChartWidth, m_ChartHeight);
|
||||
OnSizeChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all series and legend data.
|
||||
/// It just emptying all of serie's data without emptying the list of series.
|
||||
/// </summary>
|
||||
public virtual void ClearData()
|
||||
{
|
||||
m_Series.ClearData();
|
||||
m_Legend.ClearData();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove legend and serie by name.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
public virtual void RemoveData(string serieName)
|
||||
{
|
||||
m_Series.Remove(serieName);
|
||||
m_Legend.RemoveData(serieName);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all data from series and legend.
|
||||
/// The series list is also cleared.
|
||||
/// </summary>
|
||||
public virtual void RemoveData()
|
||||
{
|
||||
m_Legend.ClearData();
|
||||
m_Series.RemoveAll();
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a serie to serie list.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="type">the type of serie</param>
|
||||
/// <param name="show">whether to show this serie</param>
|
||||
/// <returns>the added serie</returns>
|
||||
public virtual Serie AddSerie(string serieName, SerieType type, bool show = true)
|
||||
{
|
||||
m_Legend.AddData(serieName);
|
||||
return m_Series.AddSerie(serieName, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a data to serie.
|
||||
/// If serieName doesn't exist in legend,will be add to legend.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="data">the data to add</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, float data, string dataName = null)
|
||||
{
|
||||
m_Legend.AddData(serieName);
|
||||
var success = m_Series.AddData(serieName, data, dataName, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a data to serie.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="data">the data to add</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, float data, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieIndex, data, dataName, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an arbitray dimension data to serie,such as (x,y,z,...).
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="multidimensionalData">the (x,y,z,...) data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, List<float> multidimensionalData, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieName, multidimensionalData, dataName, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an arbitray dimension data to serie,such as (x,y,z,...).
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie,index starts at 0</param>
|
||||
/// <param name="multidimensionalData">the (x,y,z,...) data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, List<float> multidimensionalData, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddData(serieIndex, multidimensionalData, dataName, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a (x,y) data to serie.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="xValue">x data</param>
|
||||
/// <param name="yValue">y data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(string serieName, float xValue, float yValue, string dataName)
|
||||
{
|
||||
var success = m_Series.AddXYData(serieName, xValue, yValue, dataName, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a (x,y) data to serie.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="xValue">x data</param>
|
||||
/// <param name="yValue">y data</param>
|
||||
/// <param name="dataName">the name of data</param>
|
||||
/// <returns>Returns True on success</returns>
|
||||
public virtual bool AddData(int serieIndex, float xValue, float yValue, string dataName = null)
|
||||
{
|
||||
var success = m_Series.AddXYData(serieIndex, xValue, yValue, dataName, m_MaxCacheDataNumber);
|
||||
if (success) RefreshChart();
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data by serie name.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="value">the data will be update</param>
|
||||
/// <param name="dataIndex">the index of data</param>
|
||||
public virtual void UpdateData(string serieName, float value, int dataIndex = 0)
|
||||
{
|
||||
m_Series.UpdateData(serieName, value, dataIndex);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update serie data by serie index.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="value">the data will be update</param>
|
||||
/// <param name="dataIndex">the index of data</param>
|
||||
public virtual void UpdateData(int serieIndex, float value, int dataIndex = 0)
|
||||
{
|
||||
m_Series.UpdateData(serieIndex, value, dataIndex);
|
||||
RefreshChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show serie and legend.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(string serieName, bool active)
|
||||
{
|
||||
var serie = m_Series.GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
SetActive(serie.index, active);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show serie and legend.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <param name="active">Active or not</param>
|
||||
public virtual void SetActive(int serieIndex, bool 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>
|
||||
/// Whether serie is activated.
|
||||
/// </summary>
|
||||
/// <param name="serieName">the name of serie</param>
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(string serieName)
|
||||
{
|
||||
return m_Series.IsActive(serieName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether serie is activated.
|
||||
/// </summary>
|
||||
/// <param name="serieIndex">the index of serie</param>
|
||||
/// <returns>True when activated</returns>
|
||||
public virtual bool IsActive(int serieIndex)
|
||||
{
|
||||
return m_Series.IsActive(serieIndex);
|
||||
}
|
||||
|
||||
public virtual bool IsLegendActive(string legendName)
|
||||
{
|
||||
return IsActive(legendName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Redraw chart next frame.
|
||||
/// </summary>
|
||||
public void RefreshChart()
|
||||
{
|
||||
m_RefreshChart = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update chart theme
|
||||
/// </summary>
|
||||
/// <param name="theme">theme</param>
|
||||
public void UpdateTheme(Theme theme)
|
||||
{
|
||||
m_ThemeInfo.theme = theme;
|
||||
OnThemeChanged();
|
||||
RefreshChart();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/Internal/BaseChart_API.cs.meta
Normal file
11
Scripts/UI/Internal/BaseChart_API.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55240c555461146d28a72b071eb77a4d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -171,18 +171,21 @@ namespace XCharts
|
||||
for (int j = 0; j < m_Series.Count; j++)
|
||||
{
|
||||
var serie = m_Series.GetSerie(j);
|
||||
serie.selected = false;
|
||||
for (int n = 0; n < serie.xData.Count; n++)
|
||||
for (int n = 0; n < serie.data.Count; n++)
|
||||
{
|
||||
var xdata = serie.xData[n];
|
||||
var ydata = serie.yData[n];
|
||||
var serieData = serie.GetSerieData(n);
|
||||
var serieData = serie.data[n];
|
||||
var xdata = serieData.data[0];
|
||||
var ydata = serieData.data[1];
|
||||
var symbolSize = serie.symbol.GetSize(serieData == null ? null : serieData.data);
|
||||
if (Mathf.Abs(xValue - xdata) / xRate < symbolSize
|
||||
&& Mathf.Abs(yValue - ydata) / yRate < symbolSize)
|
||||
{
|
||||
m_Tooltip.dataIndex[i] = n;
|
||||
serie.selected = true;
|
||||
serieData.highlighted = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
serieData.highlighted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -285,7 +288,8 @@ namespace XCharts
|
||||
serie.GetXYData(index, m_DataZoom, out xValue, out yValue);
|
||||
if (isCartesian)
|
||||
{
|
||||
if (serie.selected)
|
||||
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)).Append(",")
|
||||
@@ -295,7 +299,7 @@ namespace XCharts
|
||||
else
|
||||
{
|
||||
sb.Append("\n")
|
||||
.Append("<color=").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
|
||||
.Append("<color=#").Append(m_ThemeInfo.GetColorStr(i)).Append(">● </color>")
|
||||
.Append(key).Append(!string.IsNullOrEmpty(key) ? " : " : "")
|
||||
.Append(ChartCached.FloatToStr(yValue));
|
||||
}
|
||||
|
||||
@@ -156,6 +156,11 @@ namespace XCharts
|
||||
return null;
|
||||
}
|
||||
|
||||
public int GetIndex(string legendName)
|
||||
{
|
||||
return m_Data.IndexOf(legendName);
|
||||
}
|
||||
|
||||
public void RemoveButton()
|
||||
{
|
||||
m_DataBtnList.Clear();
|
||||
@@ -164,7 +169,7 @@ namespace XCharts
|
||||
public void SetButton(string name, Button btn, int total)
|
||||
{
|
||||
int index = m_DataBtnList.Values.Count;
|
||||
btn.transform.localPosition = GetButtonLocationPosition(total,index);
|
||||
btn.transform.localPosition = GetButtonLocationPosition(total, index);
|
||||
m_DataBtnList[name] = btn;
|
||||
btn.gameObject.SetActive(show);
|
||||
btn.GetComponentInChildren<Text>().text = name;
|
||||
@@ -183,7 +188,7 @@ namespace XCharts
|
||||
m_Location.OnChanged();
|
||||
}
|
||||
|
||||
private Vector2 GetButtonLocationPosition(int size,int index)
|
||||
private Vector2 GetButtonLocationPosition(int size, int index)
|
||||
{
|
||||
switch (m_Orient)
|
||||
{
|
||||
|
||||
@@ -5,33 +5,10 @@ namespace XCharts
|
||||
[System.Serializable]
|
||||
public class Pie
|
||||
{
|
||||
[SerializeField] private string m_Name;
|
||||
[SerializeField] private float m_InsideRadius;
|
||||
[SerializeField] private float m_OutsideRadius;
|
||||
[SerializeField] private float m_TooltipExtraRadius;
|
||||
[SerializeField] private bool m_Selected;
|
||||
[SerializeField] private int m_SelectedIndex;
|
||||
[SerializeField] private float m_SelectedOffset;
|
||||
[SerializeField] private bool m_Rose;
|
||||
[SerializeField] private float m_Space;
|
||||
[SerializeField] private float m_Left;
|
||||
[SerializeField] private float m_Right;
|
||||
[SerializeField] private float m_Top;
|
||||
[SerializeField] private float m_Bottom;
|
||||
|
||||
public string name { get { return m_Name; } set { m_Name = value; } }
|
||||
public float insideRadius { get { return m_InsideRadius; } set { m_InsideRadius = value; } }
|
||||
public float outsideRadius { get { return m_OutsideRadius; } set { m_OutsideRadius = value; } }
|
||||
public float tooltipExtraRadius { get { return m_TooltipExtraRadius; } set { m_TooltipExtraRadius = value; } }
|
||||
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
|
||||
public int selectedIndex { get { return m_SelectedIndex; } set { m_SelectedIndex = value; } }
|
||||
public float selectedOffset { get { return m_SelectedOffset; } set { m_SelectedOffset = value; } }
|
||||
public bool rose { get { return m_Rose; } set { m_Rose = value; } }
|
||||
public float space { get { return m_Space; } set { m_Space = value; } }
|
||||
public float left { get { return m_Left; } set { m_Left = value; } }
|
||||
public float right { get { return m_Right; } set { m_Right = value; } }
|
||||
public float top { get { return m_Top; } set { m_Top = value; } }
|
||||
public float bottom { get { return m_Bottom; } set { m_Bottom = value; } }
|
||||
|
||||
public static Pie defaultPie
|
||||
{
|
||||
@@ -39,13 +16,8 @@ namespace XCharts
|
||||
{
|
||||
var pie = new Pie
|
||||
{
|
||||
m_Name = "Pie",
|
||||
m_InsideRadius = 0f,
|
||||
m_OutsideRadius = 80f,
|
||||
m_TooltipExtraRadius = 10f,
|
||||
m_Rose = false,
|
||||
m_Selected = false,
|
||||
m_SelectedOffset = 10,
|
||||
m_SelectedOffset = 10f,
|
||||
};
|
||||
return pie;
|
||||
}
|
||||
|
||||
@@ -16,149 +16,57 @@ namespace XCharts
|
||||
EffectScatter
|
||||
}
|
||||
|
||||
public enum SerieSymbolType
|
||||
public enum RoseType
|
||||
{
|
||||
EmptyCircle,
|
||||
Circle,
|
||||
Rect,
|
||||
Triangle,
|
||||
Diamond,
|
||||
None,
|
||||
Radius,
|
||||
Area
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The way to get serie symbol size.
|
||||
/// <para> `Custom`:Specify constant for symbol size. </para>
|
||||
/// <para> `FromData`:Specify the dataIndex and dataScale to calculate symbol size,the formula:data[dataIndex]*dataScale. </para>
|
||||
/// <para> `Callback`:Specify callback function for symbol size. </para>
|
||||
/// </summary>
|
||||
public enum SerieSymbolSizeType
|
||||
{
|
||||
/// <summary>
|
||||
/// Specify constant for symbol size.
|
||||
/// </summary>
|
||||
Custom,
|
||||
/// <summary>
|
||||
/// Specify the dataIndex and dataScale to calculate symbol size
|
||||
/// </summary>
|
||||
FromData,
|
||||
/// <summary>
|
||||
/// Specify callback function for symbol size
|
||||
/// </summary>
|
||||
Callback,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class SerieData
|
||||
{
|
||||
[SerializeField] private string m_Name;
|
||||
[SerializeField] private List<float> m_Data = new List<float>();
|
||||
|
||||
public string name { get { return m_Name; } set { m_Name = value; } }
|
||||
public List<float> data { get { return m_Data; } set { m_Data = value; } }
|
||||
}
|
||||
|
||||
public delegate float SymbolSizeCallback(List<float> data);
|
||||
|
||||
[System.Serializable]
|
||||
public class SerieSymbol
|
||||
{
|
||||
[SerializeField] private SerieSymbolType m_Type = SerieSymbolType.EmptyCircle;
|
||||
[SerializeField] private SerieSymbolSizeType m_SizeType = SerieSymbolSizeType.Custom;
|
||||
[SerializeField] private float m_Size = 20f;
|
||||
[SerializeField] private float m_SelectedSize = 30f;
|
||||
[SerializeField] private int m_DataIndex = 1;
|
||||
[SerializeField] private float m_DataScale = 1;
|
||||
[SerializeField] private float m_SelectedDataScale = 1.5f;
|
||||
[SerializeField] private SymbolSizeCallback m_SizeCallback;
|
||||
[SerializeField] private SymbolSizeCallback m_SelectedSizeCallback;
|
||||
|
||||
public SerieSymbolType type { get { return m_Type; } set { m_Type = value; } }
|
||||
public float size { get { return m_Size; } set { m_Size = value; } }
|
||||
public float selectedSize { get { return m_SelectedSize; } set { m_SelectedSize = value; } }
|
||||
public int dataIndex { get { return m_DataIndex; } set { m_DataIndex = value; } }
|
||||
public float dataScale { get { return m_DataScale; } set { m_DataScale = value; } }
|
||||
public float selectedDataScale { get { return m_SelectedDataScale; } set { m_SelectedDataScale = value; } }
|
||||
public SymbolSizeCallback sizeCallback { get { return m_SizeCallback; } set { m_SizeCallback = value; } }
|
||||
public SymbolSizeCallback selectedSizeCallback { get { return m_SelectedSizeCallback; } set { m_SelectedSizeCallback = value; } }
|
||||
|
||||
private List<float> m_AnimationSize = new List<float>() { 0, 5, 10 };
|
||||
public List<float> animationSize { get { return m_AnimationSize; } }
|
||||
public Color animationColor { get; set; }
|
||||
|
||||
public float GetSize(List<float> data)
|
||||
{
|
||||
if (data == null) return size;
|
||||
switch (m_SizeType)
|
||||
{
|
||||
case SerieSymbolSizeType.Custom:
|
||||
return size;
|
||||
case SerieSymbolSizeType.FromData:
|
||||
if (dataIndex >= 0 && dataIndex < data.Count)
|
||||
{
|
||||
return data[dataIndex] * m_DataScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
return size;
|
||||
}
|
||||
case SerieSymbolSizeType.Callback:
|
||||
if (sizeCallback != null) return sizeCallback(data);
|
||||
else return size;
|
||||
default: return size;
|
||||
}
|
||||
}
|
||||
|
||||
public float GetSelectedSize(List<float> data)
|
||||
{
|
||||
if (data == null) return selectedSize;
|
||||
switch (m_SizeType)
|
||||
{
|
||||
case SerieSymbolSizeType.Custom:
|
||||
return selectedSize;
|
||||
case SerieSymbolSizeType.FromData:
|
||||
if (dataIndex >= 0 && dataIndex < data.Count)
|
||||
{
|
||||
return data[dataIndex] * m_SelectedDataScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
return selectedSize;
|
||||
}
|
||||
case SerieSymbolSizeType.Callback:
|
||||
if (selectedSizeCallback != null) return selectedSizeCallback(data);
|
||||
else return selectedSize;
|
||||
default: return selectedSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public class Serie : JsonDataSupport
|
||||
{
|
||||
[SerializeField] [DefaultValue("true")] private bool m_Show = true;
|
||||
[SerializeField] private SerieType m_Type;
|
||||
[SerializeField] private bool m_Selected;
|
||||
[SerializeField] private string m_Name;
|
||||
[SerializeField] private string m_Stack;
|
||||
[SerializeField][Range(0,1)] private int m_AxisIndex;
|
||||
[SerializeField] [Range(0, 1)] private int m_AxisIndex;
|
||||
[SerializeField] private SerieSymbol m_Symbol = new SerieSymbol();
|
||||
#region PieChart
|
||||
[SerializeField] private bool m_ClickOffset = true;
|
||||
[SerializeField] private RoseType m_RoseType = RoseType.None;
|
||||
[SerializeField] private float m_Space;
|
||||
[SerializeField] private float[] m_Center = new float[2] { 0.5f, 0.5f };
|
||||
[SerializeField] private float[] m_Radius = new float[2] { 0, 80 };
|
||||
#endregion
|
||||
|
||||
[SerializeField][Range(1,6)] private int m_ShowDataDimension;
|
||||
[SerializeField] [Range(1, 6)] private int m_ShowDataDimension;
|
||||
[SerializeField] private bool m_ShowDataName;
|
||||
[FormerlySerializedAs("m_Data")]
|
||||
[SerializeField] private List<float> m_YData = new List<float>();
|
||||
[SerializeField] private List<float> m_XData = new List<float>();
|
||||
[SerializeField] private List<SerieData> m_Data = new List<SerieData>();
|
||||
|
||||
public int index { get; set; }
|
||||
public int dataCount { get { return m_Data.Count; } }
|
||||
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
|
||||
public bool show { get { return m_Show; } set { m_Show = value; } }
|
||||
public SerieType type { get { return m_Type; } set { m_Type = value; } }
|
||||
public string name { get { return m_Name; } set { m_Name = value; } }
|
||||
public string stack { get { return m_Stack; } set { m_Stack = value; } }
|
||||
public int axisIndex { get { return m_AxisIndex; } set { m_AxisIndex = value; } }
|
||||
public SerieSymbol symbol { get { return m_Symbol; } set { m_Symbol = value; } }
|
||||
public bool clickOffset { get { return m_ClickOffset; } set { m_ClickOffset = value; } }
|
||||
public RoseType roseType { get { return m_RoseType; } set { m_RoseType = value; } }
|
||||
public float space { get { return m_Space; } set { m_Space = value; } }
|
||||
public float[] center { get { return m_Center; } set { m_Center = value; } }
|
||||
public float[] radius { get { return m_Radius; } set { m_Radius = value; } }
|
||||
|
||||
public int index { get; set; }
|
||||
/// <summary>
|
||||
/// Whether the serie is highlighted.
|
||||
/// 该系列是否高亮,一般由图例悬停触发。
|
||||
/// </summary>
|
||||
public bool highlighted { get; set; }
|
||||
public int dataCount { get { return m_Data.Count; } }
|
||||
|
||||
public List<float> yData { get { return m_YData; } }
|
||||
public List<float> xData { get { return m_XData; } }
|
||||
public List<SerieData> data { get { return m_Data; } }
|
||||
@@ -175,11 +83,11 @@ namespace XCharts
|
||||
get
|
||||
{
|
||||
float max = int.MinValue;
|
||||
foreach (var data in yData)
|
||||
foreach (var sdata in data)
|
||||
{
|
||||
if (data > max)
|
||||
if (sdata.show && sdata.data[1] > max)
|
||||
{
|
||||
max = data;
|
||||
max = sdata.data[1];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
@@ -191,11 +99,11 @@ namespace XCharts
|
||||
get
|
||||
{
|
||||
float max = int.MinValue;
|
||||
foreach (var data in xData)
|
||||
foreach (var sdata in data)
|
||||
{
|
||||
if (data > max)
|
||||
if (sdata.show && sdata.data[0] > max)
|
||||
{
|
||||
max = data;
|
||||
max = sdata.data[0];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
@@ -207,11 +115,11 @@ namespace XCharts
|
||||
get
|
||||
{
|
||||
float min = int.MaxValue;
|
||||
foreach (var data in yData)
|
||||
foreach (var sdata in data)
|
||||
{
|
||||
if (data < min)
|
||||
if (sdata.show && sdata.data[1] < min)
|
||||
{
|
||||
min = data;
|
||||
min = sdata.data[1];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
@@ -223,11 +131,11 @@ namespace XCharts
|
||||
get
|
||||
{
|
||||
float min = int.MaxValue;
|
||||
foreach (var data in xData)
|
||||
foreach (var sdata in data)
|
||||
{
|
||||
if (data < min)
|
||||
if (sdata.show && sdata.data[0] < min)
|
||||
{
|
||||
min = data;
|
||||
min = sdata.data[0];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
@@ -239,9 +147,10 @@ namespace XCharts
|
||||
get
|
||||
{
|
||||
float total = 0;
|
||||
foreach (var data in yData)
|
||||
foreach (var sdata in data)
|
||||
{
|
||||
total += data;
|
||||
if (sdata.show)
|
||||
total += sdata.data[1];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
@@ -252,9 +161,10 @@ namespace XCharts
|
||||
get
|
||||
{
|
||||
float total = 0;
|
||||
foreach (var data in xData)
|
||||
foreach (var sdata in data)
|
||||
{
|
||||
total += data;
|
||||
if (sdata.show)
|
||||
total += sdata.data[0];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
@@ -274,7 +184,7 @@ namespace XCharts
|
||||
m_Data.RemoveAt(index);
|
||||
}
|
||||
|
||||
public void AddYData(float value, int maxDataNumber = 0, string dataName = null)
|
||||
public void AddYData(float value, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
if (maxDataNumber > 0)
|
||||
{
|
||||
@@ -288,7 +198,7 @@ namespace XCharts
|
||||
m_Data.Add(new SerieData() { data = new List<float>() { xValue, value }, name = dataName });
|
||||
}
|
||||
|
||||
public void AddXYData(float xValue, float yValue, int maxDataNumber = 0, string dataName = null)
|
||||
public void AddXYData(float xValue, float yValue, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
if (maxDataNumber > 0)
|
||||
{
|
||||
@@ -301,16 +211,16 @@ namespace XCharts
|
||||
m_Data.Add(new SerieData() { data = new List<float>() { xValue, yValue }, name = dataName });
|
||||
}
|
||||
|
||||
public void AddData(List<float> valueList, int maxDataNumber = 0, string dataName = null)
|
||||
public void AddData(List<float> valueList, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
if (valueList == null || valueList.Count == 0) return;
|
||||
if (valueList.Count == 1)
|
||||
{
|
||||
AddYData(valueList[0], maxDataNumber, dataName);
|
||||
AddYData(valueList[0], dataName, maxDataNumber);
|
||||
}
|
||||
else if (valueList.Count == 2)
|
||||
{
|
||||
AddXYData(valueList[0], valueList[1], maxDataNumber, dataName);
|
||||
AddXYData(valueList[0], valueList[1], dataName, maxDataNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -335,22 +245,27 @@ namespace XCharts
|
||||
public float GetYData(int index, DataZoom dataZoom = null)
|
||||
{
|
||||
if (index < 0) return 0;
|
||||
var showData = GetYDataList(dataZoom);
|
||||
if (index < showData.Count)
|
||||
var serieData = GetDataList(dataZoom);
|
||||
if (index < serieData.Count)
|
||||
{
|
||||
return showData[index];
|
||||
}
|
||||
else
|
||||
{
|
||||
var serieData = GetDataList(dataZoom);
|
||||
if (index < serieData.Count)
|
||||
{
|
||||
return serieData[index].data[1];
|
||||
}
|
||||
return serieData[index].data[1];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void GetYData(int index, out float yData, out string dataName, DataZoom dataZoom = null)
|
||||
{
|
||||
yData = 0;
|
||||
dataName = null;
|
||||
if (index < 0) return;
|
||||
var serieData = GetDataList(dataZoom);
|
||||
if (index < serieData.Count)
|
||||
{
|
||||
yData = serieData[index].data[1];
|
||||
dataName = serieData[index].name;
|
||||
}
|
||||
}
|
||||
|
||||
public SerieData GetSerieData(int index, DataZoom dataZoom = null)
|
||||
{
|
||||
var data = GetDataList(dataZoom);
|
||||
@@ -366,22 +281,12 @@ namespace XCharts
|
||||
xValue = 0;
|
||||
yVlaue = 0;
|
||||
if (index < 0) return;
|
||||
var xShowData = GetXDataList(dataZoom);
|
||||
var yShowData = GetYDataList(dataZoom);
|
||||
if (index < xShowData.Count && index < yShowData.Count)
|
||||
var showData = GetDataList(dataZoom);
|
||||
if (index < showData.Count)
|
||||
{
|
||||
xValue = xShowData[index];
|
||||
yVlaue = yShowData[index];
|
||||
}
|
||||
else
|
||||
{
|
||||
var showData = GetDataList(dataZoom);
|
||||
if (index < showData.Count)
|
||||
{
|
||||
var serieData = showData[index];
|
||||
xValue = serieData.data[0];
|
||||
yVlaue = serieData.data[1];
|
||||
}
|
||||
var serieData = showData[index];
|
||||
xValue = serieData.data[0];
|
||||
yVlaue = serieData.data[1];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,8 +347,6 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void UpdateFilterData(DataZoom dataZoom)
|
||||
{
|
||||
if (dataZoom != null && dataZoom.show)
|
||||
@@ -517,6 +420,24 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearHighlight()
|
||||
{
|
||||
highlighted = false;
|
||||
foreach (var sd in m_Data)
|
||||
{
|
||||
sd.highlighted = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetHighlight(int index)
|
||||
{
|
||||
if (index <= 0) return;
|
||||
for (int i = 0; i < m_Data.Count; i++)
|
||||
{
|
||||
m_Data[i].highlighted = index == i;
|
||||
}
|
||||
}
|
||||
|
||||
public override void ParseJsonData(string jsonData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(jsonData) || !m_DataFromJson) return;
|
||||
@@ -555,6 +476,34 @@ namespace XCharts
|
||||
m_Data.Add(serieData);
|
||||
}
|
||||
}
|
||||
else if (temp.IndexOf("value") > -1 && temp.IndexOf("name") > -1)
|
||||
{
|
||||
string[] datas = temp.Split(new string[] { "},", "} ,", "}" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
for (int i = 0; i < datas.Length; i++)
|
||||
{
|
||||
var arr = datas[i].Replace("{", "").Split(',');
|
||||
var serieData = new SerieData();
|
||||
foreach (var a in arr)
|
||||
{
|
||||
if (a.StartsWith("value:"))
|
||||
{
|
||||
float value = float.Parse(a.Substring(6, a.Length - 6));
|
||||
serieData.data = new List<float>() { i, value };
|
||||
}
|
||||
else if (a.StartsWith("name:"))
|
||||
{
|
||||
string name = a.Substring(6, a.Length - 6 - 1);
|
||||
serieData.name = name;
|
||||
}
|
||||
else if (a.StartsWith("selected:"))
|
||||
{
|
||||
string selected = a.Substring(9, a.Length - 9);
|
||||
serieData.selected = bool.Parse(selected);
|
||||
}
|
||||
}
|
||||
m_Data.Add(serieData);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] datas = temp.Split(',');
|
||||
@@ -565,10 +514,8 @@ namespace XCharts
|
||||
if (flag)
|
||||
{
|
||||
var serieData = new SerieData();
|
||||
|
||||
serieData.data.Add(value);
|
||||
serieData.data = new List<float>() { i, value };
|
||||
m_Data.Add(serieData);
|
||||
m_XData.Add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
40
Scripts/UI/Internal/SerieData.cs
Normal file
40
Scripts/UI/Internal/SerieData.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
[System.Serializable]
|
||||
/// <summary>
|
||||
/// A data item of serie.系列中的一个数据项。
|
||||
/// </summary>
|
||||
public class SerieData
|
||||
{
|
||||
[SerializeField] private string m_Name;
|
||||
[SerializeField] private bool m_Selected;
|
||||
[SerializeField] private List<float> m_Data = new List<float>();
|
||||
|
||||
private bool m_Show = true;
|
||||
|
||||
/// <summary>
|
||||
/// the name of data item.数据项名称。
|
||||
/// </summary>
|
||||
public string name { get { return m_Name; } set { m_Name = value; } }
|
||||
/// <summary>
|
||||
/// Whether the data item is selected.该数据项是否被选中。
|
||||
/// </summary>
|
||||
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
|
||||
/// <summary>
|
||||
/// An arbitrary dimension data list of data item.可指定任意维数的数值列表。
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public List<float> data { get { return m_Data; } set { m_Data = value; } }
|
||||
/// <summary>
|
||||
/// Whether the data item is showed.该数据项是否要显示。
|
||||
/// </summary>
|
||||
public bool show { get { return m_Show; } set { m_Show = value; } }
|
||||
/// <summary>
|
||||
/// Whether the data item is highlighted.该数据项是否被高亮,一般由鼠标悬停或图例悬停触发高亮。
|
||||
/// </summary>
|
||||
public bool highlighted { get; set; }
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/Internal/SerieData.cs.meta
Normal file
11
Scripts/UI/Internal/SerieData.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d61959eca953d46639084862f812b408
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
112
Scripts/UI/Internal/SerieSymbol.cs
Normal file
112
Scripts/UI/Internal/SerieSymbol.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XCharts
|
||||
{
|
||||
public enum SerieSymbolType
|
||||
{
|
||||
EmptyCircle,
|
||||
Circle,
|
||||
Rect,
|
||||
Triangle,
|
||||
Diamond,
|
||||
None,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The way to get serie symbol size.
|
||||
/// <para> `Custom`:Specify constant for symbol size. </para>
|
||||
/// <para> `FromData`:Specify the dataIndex and dataScale to calculate symbol size,the formula:data[dataIndex]*dataScale. </para>
|
||||
/// <para> `Callback`:Specify callback function for symbol size. </para>
|
||||
/// </summary>
|
||||
public enum SerieSymbolSizeType
|
||||
{
|
||||
/// <summary>
|
||||
/// Specify constant for symbol size.
|
||||
/// </summary>
|
||||
Custom,
|
||||
/// <summary>
|
||||
/// Specify the dataIndex and dataScale to calculate symbol size
|
||||
/// </summary>
|
||||
FromData,
|
||||
/// <summary>
|
||||
/// Specify callback function for symbol size
|
||||
/// </summary>
|
||||
Callback,
|
||||
}
|
||||
|
||||
public delegate float SymbolSizeCallback(List<float> data);
|
||||
|
||||
[System.Serializable]
|
||||
public class SerieSymbol
|
||||
{
|
||||
[SerializeField] private SerieSymbolType m_Type = SerieSymbolType.EmptyCircle;
|
||||
[SerializeField] private SerieSymbolSizeType m_SizeType = SerieSymbolSizeType.Custom;
|
||||
[SerializeField] private float m_Size = 20f;
|
||||
[SerializeField] private float m_SelectedSize = 30f;
|
||||
[SerializeField] private int m_DataIndex = 1;
|
||||
[SerializeField] private float m_DataScale = 1;
|
||||
[SerializeField] private float m_SelectedDataScale = 1.5f;
|
||||
[SerializeField] private SymbolSizeCallback m_SizeCallback;
|
||||
[SerializeField] private SymbolSizeCallback m_SelectedSizeCallback;
|
||||
|
||||
public SerieSymbolType type { get { return m_Type; } set { m_Type = value; } }
|
||||
public float size { get { return m_Size; } set { m_Size = value; } }
|
||||
public float selectedSize { get { return m_SelectedSize; } set { m_SelectedSize = value; } }
|
||||
public int dataIndex { get { return m_DataIndex; } set { m_DataIndex = value; } }
|
||||
public float dataScale { get { return m_DataScale; } set { m_DataScale = value; } }
|
||||
public float selectedDataScale { get { return m_SelectedDataScale; } set { m_SelectedDataScale = value; } }
|
||||
public SymbolSizeCallback sizeCallback { get { return m_SizeCallback; } set { m_SizeCallback = value; } }
|
||||
public SymbolSizeCallback selectedSizeCallback { get { return m_SelectedSizeCallback; } set { m_SelectedSizeCallback = value; } }
|
||||
|
||||
private List<float> m_AnimationSize = new List<float>() { 0, 5, 10 };
|
||||
public List<float> animationSize { get { return m_AnimationSize; } }
|
||||
public Color animationColor { get; set; }
|
||||
|
||||
public float GetSize(List<float> data)
|
||||
{
|
||||
if (data == null) return size;
|
||||
switch (m_SizeType)
|
||||
{
|
||||
case SerieSymbolSizeType.Custom:
|
||||
return size;
|
||||
case SerieSymbolSizeType.FromData:
|
||||
if (dataIndex >= 0 && dataIndex < data.Count)
|
||||
{
|
||||
return data[dataIndex] * m_DataScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
return size;
|
||||
}
|
||||
case SerieSymbolSizeType.Callback:
|
||||
if (sizeCallback != null) return sizeCallback(data);
|
||||
else return size;
|
||||
default: return size;
|
||||
}
|
||||
}
|
||||
|
||||
public float GetSelectedSize(List<float> data)
|
||||
{
|
||||
if (data == null) return selectedSize;
|
||||
switch (m_SizeType)
|
||||
{
|
||||
case SerieSymbolSizeType.Custom:
|
||||
return selectedSize;
|
||||
case SerieSymbolSizeType.FromData:
|
||||
if (dataIndex >= 0 && dataIndex < data.Count)
|
||||
{
|
||||
return data[dataIndex] * m_SelectedDataScale;
|
||||
}
|
||||
else
|
||||
{
|
||||
return selectedSize;
|
||||
}
|
||||
case SerieSymbolSizeType.Callback:
|
||||
if (selectedSizeCallback != null) return selectedSizeCallback(data);
|
||||
else return selectedSize;
|
||||
default: return selectedSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Scripts/UI/Internal/SerieSymbol.cs.meta
Normal file
11
Scripts/UI/Internal/SerieSymbol.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09616736d585e45b1940795215733660
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -150,67 +150,67 @@ namespace XCharts
|
||||
return serie;
|
||||
}
|
||||
|
||||
public bool AddData(string serieName, float value, int maxDataNumber = 0)
|
||||
public bool AddData(string serieName, float value, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
var serie = GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.AddYData(value, maxDataNumber);
|
||||
serie.AddYData(value, dataName, maxDataNumber);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddData(int index, float value, int maxDataNumber = 0)
|
||||
public bool AddData(int index, float value, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
var serie = GetSerie(index);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.AddYData(value, maxDataNumber);
|
||||
serie.AddYData(value, dataName, maxDataNumber);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddData(string serieName, List<float> multidimensionalData, int maxDataNumber = 0)
|
||||
public bool AddData(string serieName, List<float> multidimensionalData, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
var serie = GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.AddData(multidimensionalData, maxDataNumber);
|
||||
serie.AddData(multidimensionalData, dataName, maxDataNumber);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddData(int serieIndex, List<float> multidimensionalData, int maxDataNumber = 0)
|
||||
public bool AddData(int serieIndex, List<float> multidimensionalData, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
var serie = GetSerie(serieIndex);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.AddData(multidimensionalData, maxDataNumber);
|
||||
serie.AddData(multidimensionalData, dataName, maxDataNumber);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddXYData(string serieName, float xValue, float yValue, int maxDataNumber = 0)
|
||||
public bool AddXYData(string serieName, float xValue, float yValue, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
var serie = GetSerie(serieName);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.AddXYData(xValue, yValue, maxDataNumber);
|
||||
serie.AddXYData(xValue, yValue, dataName, maxDataNumber);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddXYData(int index, float xValue, float yValue, int maxDataNumber = 0)
|
||||
public bool AddXYData(int index, float xValue, float yValue, string dataName = null, int maxDataNumber = 0)
|
||||
{
|
||||
var serie = GetSerie(index);
|
||||
if (serie != null)
|
||||
{
|
||||
serie.AddXYData(xValue, yValue, maxDataNumber);
|
||||
serie.AddXYData(xValue, yValue, dataName, maxDataNumber);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -305,7 +305,7 @@ namespace XCharts
|
||||
public bool IsTooltipSelected(int serieIndex)
|
||||
{
|
||||
var serie = GetSerie(serieIndex);
|
||||
if (serie != null) return serie.selected;
|
||||
if (serie != null) return serie.highlighted;
|
||||
else return false;
|
||||
}
|
||||
|
||||
@@ -520,17 +520,31 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
private List<string> serieNameList = new List<string>();
|
||||
public List<string> GetSerieNameList()
|
||||
{
|
||||
var list = new List<string>();
|
||||
serieNameList.Clear();
|
||||
foreach (var serie in m_Series)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(serie.name) && !list.Contains(serie.name))
|
||||
if (serie.type == SerieType.Pie)
|
||||
{
|
||||
list.Add(serie.name);
|
||||
foreach (var data in serie.data)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(data.name) && !serieNameList.Contains(data.name))
|
||||
{
|
||||
serieNameList.Add(data.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.IsNullOrEmpty(serie.name) && !serieNameList.Contains(serie.name))
|
||||
{
|
||||
serieNameList.Add(serie.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
return serieNameList;
|
||||
}
|
||||
|
||||
public void SetSerieSymbolSizeCallback(SymbolSizeCallback size, SymbolSizeCallback selectedSize)
|
||||
|
||||
@@ -65,8 +65,8 @@ namespace XCharts
|
||||
/// <summary>
|
||||
/// The data index currently indicated by Tooltip.
|
||||
/// </summary>
|
||||
public int[] dataIndex { get; set; }
|
||||
public int[] lastDataIndex { get; set; }
|
||||
public List<int> dataIndex { get; set; }
|
||||
public List<int> lastDataIndex { get; set; }
|
||||
public float[] xValues { get; set; }
|
||||
public float[] yValues { get; set; }
|
||||
|
||||
@@ -85,8 +85,8 @@ namespace XCharts
|
||||
m_Show = true,
|
||||
xValues = new float[2],
|
||||
yValues = new float[2],
|
||||
dataIndex = new int[2],
|
||||
lastDataIndex = new int[2]
|
||||
dataIndex = new List<int>() { -1, -1 },
|
||||
lastDataIndex = new List<int>() { -1, -1 }
|
||||
};
|
||||
return tooltip;
|
||||
}
|
||||
@@ -141,6 +141,11 @@ namespace XCharts
|
||||
yValues[0] = yValues[1] = -1;
|
||||
}
|
||||
|
||||
public bool IsActive()
|
||||
{
|
||||
return m_GameObject != null && m_GameObject.activeInHierarchy;
|
||||
}
|
||||
|
||||
public void SetActive(bool flag)
|
||||
{
|
||||
lastDataIndex[0] = lastDataIndex[1] = -1;
|
||||
|
||||
Reference in New Issue
Block a user