mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-17 05:50:09 +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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user