diff --git a/Demo/Scripts/Demo11_AddSinCurve.cs b/Demo/Scripts/Demo11_AddSinCurve.cs index 73eaa6d1..c695cec4 100644 --- a/Demo/Scripts/Demo11_AddSinCurve.cs +++ b/Demo/Scripts/Demo11_AddSinCurve.cs @@ -46,7 +46,7 @@ public class Demo11_AddSinCurve : MonoBehaviour { float xvalue = Mathf.PI / 180 * angle; float yvalue = Mathf.Sin(xvalue); - chart.AddXYData(0, xvalue, yvalue); + chart.AddData(0, xvalue, yvalue); } } @@ -56,6 +56,6 @@ public class Demo11_AddSinCurve : MonoBehaviour angle++; float xvalue = Mathf.PI / 180 * angle; float yvalue = Mathf.Sin(xvalue); - chart.AddXYData(0, xvalue, yvalue); + chart.AddData(0, xvalue, yvalue); } } diff --git a/Scripts/UI/Internal/BaseChart.cs b/Scripts/UI/Internal/BaseChart.cs index b2d111c0..ca3140b8 100644 --- a/Scripts/UI/Internal/BaseChart.cs +++ b/Scripts/UI/Internal/BaseChart.cs @@ -145,7 +145,6 @@ namespace XCharts /// /// Add a data to serie. - /// When serie doesn't exist, the data is ignored. /// If serieName doesn't exist in legend,will be add to legend. /// /// the name of serie @@ -161,7 +160,6 @@ namespace XCharts /// /// Add a data to serie. - /// When serie doesn't exist, the data is ignored. /// /// the index of serie /// the data to add @@ -173,15 +171,40 @@ namespace XCharts return success; } + /// + /// Add an arbitray dimension data to serie,such as (x,y,z,...). + /// + /// the name of serie + /// the (x,y,z,...) data + /// Returns True on success + public virtual bool AddData(string serieName, List multidimensionalData) + { + var success = m_Series.AddData(serieName, multidimensionalData, m_MaxCacheDataNumber); + if (success) RefreshChart(); + return success; + } + + /// + /// Add an arbitray dimension data to serie,such as (x,y,z,...). + /// + /// the index of serie,index starts at 0 + /// the (x,y,z,...) data + /// Returns True on success + public virtual bool AddData(int serieIndex, List multidimensionalData) + { + var success = m_Series.AddData(serieIndex, multidimensionalData, m_MaxCacheDataNumber); + if (success) RefreshChart(); + return success; + } + /// /// Add a (x,y) data to serie. - /// When serie doesn't exist, the data is ignored. /// /// the name of serie /// x data /// y data /// Returns True on success - public virtual bool AddXYData(string serieName, float xValue, float yValue) + public virtual bool AddData(string serieName, float xValue, float yValue) { var success = m_Series.AddXYData(serieName, xValue, yValue, m_MaxCacheDataNumber); if (success) RefreshChart(); @@ -190,13 +213,12 @@ namespace XCharts /// /// Add a (x,y) data to serie. - /// When serie doesn't exist, the data is ignored. /// /// the index of serie /// x data /// y data /// Returns True on success - public virtual bool AddXYData(int serieIndex, float xValue, float yValue) + public virtual bool AddData(int serieIndex, float xValue, float yValue) { var success = m_Series.AddXYData(serieIndex, xValue, yValue, m_MaxCacheDataNumber); if (success) RefreshChart(); @@ -313,6 +335,7 @@ namespace XCharts InitTitle(); InitLegend(); InitTooltip(); + TransferOldVersionData(); } protected virtual void Update() @@ -363,6 +386,24 @@ namespace XCharts } } + private void TransferOldVersionData() + { + foreach (var serie in m_Series.series) + { + if (serie.yData.Count > 0 && serie.data.Count == 0) + { + for (int i = 0; i < serie.yData.Count; i++) + { + float xvalue, yvalue; + serie.GetXYData(i, null, out xvalue, out yvalue); + var serieData = new SerieData(); + serieData.data = new List() { xvalue, yvalue }; + serie.data.Add(serieData); + } + } + } + } + private void InitTitle() { m_Title.OnChanged(); diff --git a/Scripts/UI/Internal/Legend.cs b/Scripts/UI/Internal/Legend.cs index 15017fd1..00a6b058 100644 --- a/Scripts/UI/Internal/Legend.cs +++ b/Scripts/UI/Internal/Legend.cs @@ -141,7 +141,7 @@ namespace XCharts public void AddData(string name) { - if (!m_Data.Contains(name)) + if (!m_Data.Contains(name) && !string.IsNullOrEmpty(name)) { m_Data.Add(name); } diff --git a/Scripts/UI/Internal/Serie.cs b/Scripts/UI/Internal/Serie.cs index 289bba28..36f774b9 100644 --- a/Scripts/UI/Internal/Serie.cs +++ b/Scripts/UI/Internal/Serie.cs @@ -301,13 +301,53 @@ namespace XCharts m_Data.Add(new SerieData() { data = new List() { xValue, yValue }, name = dataName }); } + public void AddData(List valueList, int maxDataNumber = 0, string dataName = null) + { + if (valueList == null || valueList.Count == 0) return; + if (valueList.Count == 1) + { + AddYData(valueList[0], maxDataNumber, dataName); + } + else if (valueList.Count == 2) + { + AddXYData(valueList[0], valueList[1], maxDataNumber, dataName); + } + else + { + if (maxDataNumber > 0) + { + while (m_XData.Count > maxDataNumber) m_XData.RemoveAt(0); + while (m_YData.Count > maxDataNumber) m_YData.RemoveAt(0); + while (m_Data.Count > maxDataNumber) m_Data.RemoveAt(0); + } + var serieData = new SerieData(); + serieData.name = dataName; + for (int i = 0; i < valueList.Count; i++) + { + if (i == 0) m_XData.Add(valueList[i]); + else if (i == 1) m_YData.Add(valueList[i]); + serieData.data.Add(valueList[0]); + } + m_Data.Add(serieData); + } + } + public float GetYData(int index, DataZoom dataZoom = null) { + if (index < 0) return 0; var showData = GetYDataList(dataZoom); - if (index >= 0 && index <= showData.Count - 1) + if (index < showData.Count) { return showData[index]; } + else + { + var serieData = GetDataList(dataZoom); + if (index < serieData.Count) + { + return serieData[index].data[1]; + } + } return 0; } @@ -325,10 +365,24 @@ namespace XCharts { xValue = 0; yVlaue = 0; + if (index < 0) return; var xShowData = GetXDataList(dataZoom); - if (index >= 0 && index < xShowData.Count) xValue = xShowData[index]; var yShowData = GetYDataList(dataZoom); - if (index >= 0 && index < yShowData.Count) yVlaue = yShowData[index]; + if (index < xShowData.Count && index < yShowData.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]; + } + } } public List GetYDataList(DataZoom dataZoom) @@ -437,21 +491,29 @@ namespace XCharts public void UpdateYData(int index, float value) { - if (index >= 0 && index <= m_YData.Count - 1) - { - m_YData[index] = value; - } + UpdateData(index, 2, value); } public void UpdateXYData(int index, float xValue, float yValue) { - if (index >= 0 && index <= m_YData.Count - 1) + UpdateData(index, 1, xValue); + UpdateData(index, 2, yValue); + } + + public void UpdateData(int index, int dimension, float value) + { + if (index < 0) return; + if (dimension == 1) { - m_YData[index] = yValue; + if (index < m_XData.Count) m_XData[index] = value; } - if (index >= 0 && index <= m_XData.Count - 1) + else if (dimension == 2) { - m_XData[index] = xValue; + if (index < m_YData.Count) m_YData[index] = value; + } + if (index < m_Data.Count && dimension < m_Data[index].data.Count) + { + m_Data[index].data[dimension] = value; } } @@ -463,7 +525,8 @@ namespace XCharts jsonData = jsonData.Replace("\n", ""); int startIndex = jsonData.IndexOf("["); int endIndex = jsonData.LastIndexOf("]"); - if (startIndex == -1 || endIndex == -1){ + if (startIndex == -1 || endIndex == -1) + { Debug.LogError("json data need include in [ ]"); return; } diff --git a/Scripts/UI/Internal/Series.cs b/Scripts/UI/Internal/Series.cs index d2a0085b..58949dd7 100644 --- a/Scripts/UI/Internal/Series.cs +++ b/Scripts/UI/Internal/Series.cs @@ -172,6 +172,28 @@ namespace XCharts return false; } + public bool AddData(string serieName, List multidimensionalData, int maxDataNumber = 0) + { + var serie = GetSerie(serieName); + if (serie != null) + { + serie.AddData(multidimensionalData, maxDataNumber); + return true; + } + return false; + } + + public bool AddData(int serieIndex, List multidimensionalData, int maxDataNumber = 0) + { + var serie = GetSerie(serieIndex); + if (serie != null) + { + serie.AddData(multidimensionalData, maxDataNumber); + return true; + } + return false; + } + public bool AddXYData(string serieName, float xValue, float yValue, int maxDataNumber = 0) { var serie = GetSerie(serieName); diff --git a/Scripts/UI/ScatterChart.cs b/Scripts/UI/ScatterChart.cs index 7f2c6d78..8eb85972 100644 --- a/Scripts/UI/ScatterChart.cs +++ b/Scripts/UI/ScatterChart.cs @@ -33,7 +33,7 @@ namespace XCharts AddSerie("serie1", SerieType.Scatter); for (int i = 0; i < 10; i++) { - AddXYData(0, Random.Range(10, 100), Random.Range(10, 100)); + AddData(0, Random.Range(10, 100), Random.Range(10, 100)); } } #endif