代码整理,接口优化

This commit is contained in:
monitor1394
2019-07-15 00:24:04 +08:00
parent 0297702565
commit 8fadbca405
18 changed files with 1282 additions and 6841 deletions

View File

@@ -0,0 +1,39 @@
using UnityEngine;
using XCharts;
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class Demo10_LineSimple : MonoBehaviour
{
void Awake()
{
var chart = gameObject.GetComponent<LineChart>();
if (chart == null) return;
chart.title.show = true;
chart.title.text = "Line Simple";
chart.tooltip.show = true;
chart.legend.show = false;
chart.xAxises[0].show = true;
chart.xAxises[1].show = false;
chart.yAxises[0].show = true;
chart.yAxises[1].show = false;
chart.xAxises[0].type = Axis.AxisType.Category;
chart.yAxises[0].type = Axis.AxisType.Value;
int dataCount = 10;
chart.xAxises[0].splitNumber = dataCount;
chart.xAxises[0].boundaryGap = true;
chart.RemoveData();
chart.AddSerie("test", SerieType.Line);
for (int i = 0; i < dataCount; i++)
{
chart.AddXAxisData("x" + i);
chart.AddData(0, Random.Range(10, 20));
}
}
}

View File

@@ -1,7 +1,5 @@
fileFormatVersion: 2
guid: 79b834e804f7aa844bc2ad80cf2bda9f
timeCreated: 1557366438
licenseType: Free
guid: 75496d488607d421fbf2c190f80ed0a7
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -21,8 +21,8 @@ public class Demo_Dynamic : MonoBehaviour
void Awake()
{
chart = gameObject.GetComponentInChildren<CoordinateChart>();
chart.ClearAxisData();
chart.series.ClearData();
chart.RemoveData();
chart.AddSerie("data", SerieType.Line);
chart.maxCacheDataNumber = maxCacheDataNumber;
timeNow = DateTime.Now;
timeNow = timeNow.AddSeconds(-maxCacheDataNumber);
@@ -30,14 +30,16 @@ public class Demo_Dynamic : MonoBehaviour
void Update()
{
if (initCount< maxCacheDataNumber)
if (initCount < maxCacheDataNumber)
{
int count = (int)(maxCacheDataNumber / initDataTime * Time.deltaTime);
for (int i = 0; i < count; i++)
{
timeNow = timeNow.AddSeconds(1);
chart.AddXAxisData(timeNow.ToString("hh:mm:ss"));
chart.AddData(0, UnityEngine.Random.Range(60, 150));
string category = timeNow.ToString("hh:mm:ss");
float value = UnityEngine.Random.Range(60, 150);
chart.AddXAxisData(category);
chart.AddData(0, value);
initCount++;
if (initCount > maxCacheDataNumber) break;
}
@@ -48,8 +50,10 @@ public class Demo_Dynamic : MonoBehaviour
{
updateTime = 0;
count++;
chart.AddXAxisData(DateTime.Now.ToString("hh:mm:ss"));
chart.AddData(0, UnityEngine.Random.Range(60, 150));
string category = DateTime.Now.ToString("hh:mm:ss");
float value = UnityEngine.Random.Range(60, 150);
chart.AddXAxisData(category);
chart.AddData(0, value);
chart.RefreshChart();
}
}

View File

@@ -37,34 +37,6 @@ public class Demo_LargeData : MonoBehaviour
initCount++;
if (initCount > maxCacheDataNumber) break;
}
chart.RefreshChart();
}
}
void GenerateData(int count, CoordinateChart chart)
{
var baseValue = Random.Range(0, 1000);
var time = new System.DateTime(2011, 1, 1);
var smallBaseValue = 0;
chart.xAxises[0].ClearData();
for (var i = 0; i < count; i++)
{
chart.AddXAxisData(time.ToString("hh:mm:ss"));
smallBaseValue = i % 30 == 0
? Random.Range(0, 700)
: (smallBaseValue + Random.Range(0, 500) - 250);
baseValue += Random.Range(0, 20) - 10;
float value = Mathf.Max(
0,
Mathf.Round(baseValue + smallBaseValue) + 3000
);
//var index = i % 100;
//var value = (Mathf.Sin(index / 5) * (index / 5 - 10) + index / 6) * 5;
value = Mathf.Abs(value);
chart.AddData(0, value);
time = time.AddSeconds(1);
}
}
}

View File

@@ -1,30 +0,0 @@
using UnityEngine;
using XCharts;
[DisallowMultipleComponent]
[ExecuteInEditMode]
//[RequireComponent(typeof(CoordinateChart))]
public class Demo_Test : MonoBehaviour
{
private CoordinateChart chart;
private float time;
private int count;
void Awake()
{
chart = gameObject.GetComponentInChildren<CoordinateChart>();
}
void Update()
{
time += Time.deltaTime;
if (time >= 1)
{
time = 0;
count++;
chart.UpdateData(0, Random.Range(60, 150));
chart.AddXAxisData("time" + count);
chart.AddData(0, Random.Range(60, 150));
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,7 @@ using UnityEngine.UI;
namespace XCharts
{
[AddComponentMenu("XCharts/BarChart", 13)]
[AddComponentMenu("XCharts/BarChart", 14)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
@@ -20,12 +20,37 @@ namespace XCharts
public bool inSameBar { get { return m_InSameBar; } set { m_InSameBar = value; } }
public float barWidth { get { return m_BarWidth; } set { m_BarWidth = value; } }
public float space { get { return m_Space; } set { m_Space = value; } }
public static Bar defaultBar{
get{
return new Bar(){
m_InSameBar = false,
m_BarWidth = 0.7f,
m_Space = 10
};
}
}
}
[SerializeField] private Bar m_Bar = new Bar();
[SerializeField] private Bar m_Bar = Bar.defaultBar;
public Bar bar { get { return m_Bar; } }
#if UNITY_EDITOR
protected override void Reset()
{
base.Reset();
m_Bar = Bar.defaultBar;
RemoveData();
AddSerie("bar1", SerieType.Line);
for (int i = 0; i < 5; i++)
{
AddXAxisData("x" + (i + 1));
AddData(0, Random.Range(10, 90));
}
}
#endif
private void DrawYBarSerie(VertexHelper vh, int serieIndex, int stackCount,
Serie serie, Color color, ref Dictionary<int, float> seriesHig)
{
@@ -71,7 +96,7 @@ namespace XCharts
Serie serie, Color color, ref Dictionary<int, float> seriesHig)
{
if (!m_Legend.IsActive(serie.name)) return;
List<float> showData = serie.GetYData(m_DataZoom);
List<float> showData = serie.GetYDataList(m_DataZoom);
var yAxis = m_YAxises[serie.axisIndex];
var xAxis = m_XAxises[serie.axisIndex];
if (!xAxis.show) xAxis = m_XAxises[(serie.axisIndex + 1) % m_XAxises.Count];
@@ -90,7 +115,7 @@ namespace XCharts
seriesHig[i] = 0;
}
float value = showData[i];
float pX = zeroX + i * scaleWid;
float pX = coordinateX + i * scaleWid;
float zeroY = coordinateY + yAxis.zeroYOffset;
if (!xAxis.boundaryGap) pX -= scaleWid / 2;
float pY = seriesHig[i] + zeroY + m_Coordinate.tickness;
@@ -145,7 +170,7 @@ namespace XCharts
Vector3 ep = new Vector2(m_Tooltip.pointerPos.x, coordinateY + coordinateHig);
DrawSplitLine(vh, false, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
float splitWidth = yAxis.GetSplitWidth(coordinateHig, m_DataZoom);
float pY = zeroY + (m_Tooltip.yValues[i] - 1) * splitWidth +
float pY = coordinateY + (m_Tooltip.yValues[i] - 1) * splitWidth +
(yAxis.boundaryGap ? splitWidth / 2 : 0);
sp = new Vector2(coordinateX, pY);
ep = new Vector2(coordinateX + coordinateWid, pY);

View File

@@ -123,14 +123,14 @@ namespace XCharts
public string GetData(int index, DataZoom dataZoom)
{
var showData = GetData(dataZoom);
var showData = GetDataList(dataZoom);
if (index >= 0 && index < showData.Count)
return showData[index];
else
return "";
}
public List<string> GetData(DataZoom dataZoom)
public List<string> GetDataList(DataZoom dataZoom)
{
if (dataZoom != null && dataZoom.show)
{
@@ -179,7 +179,7 @@ namespace XCharts
public int GetSplitNumber(DataZoom dataZoom)
{
if (type == AxisType.Value) return m_SplitNumber;
int dataCount = GetData(dataZoom).Count;
int dataCount = GetDataList(dataZoom).Count;
if (dataCount > 2 * m_SplitNumber || dataCount <= 0)
return m_SplitNumber;
else
@@ -193,7 +193,7 @@ namespace XCharts
public int GetDataNumber(DataZoom dataZoom)
{
return GetData(dataZoom).Count;
return GetDataList(dataZoom).Count;
}
public float GetDataWidth(float coordinateWidth, DataZoom dataZoom)
@@ -202,7 +202,7 @@ namespace XCharts
return coordinateWidth / (m_BoundaryGap ? dataCount : dataCount - 1);
}
public string GetScaleName(int index, float minValue, float maxValue, DataZoom dataZoom)
public string GetLabelName(int index, float minValue, float maxValue, DataZoom dataZoom)
{
if (m_Type == AxisType.Value)
{
@@ -212,7 +212,7 @@ namespace XCharts
else
return (value).ToString("f1");
}
var showData = GetData(dataZoom);
var showData = GetDataList(dataZoom);
int dataCount = showData.Count;
if (dataCount <= 0) return "";
@@ -239,7 +239,7 @@ namespace XCharts
}
else
{
var showData = GetData(dataZoom);
var showData = GetDataList(dataZoom);
int dataCount = showData.Count;
if (dataCount > 2 * splitNumber || dataCount <= 0)
return m_BoundaryGap ? m_SplitNumber + 1 : m_SplitNumber;
@@ -259,7 +259,10 @@ namespace XCharts
{
for (int i = 0; i < axisLabelTextList.Count; i++)
{
axisLabelTextList[i].text = GetScaleName(i, minValue, maxValue, dataZoom);
if (axisLabelTextList[i] != null)
{
axisLabelTextList[i].text = GetLabelName(i, minValue, maxValue, dataZoom);
}
}
}

View File

@@ -50,23 +50,192 @@ namespace XCharts
public Tooltip tooltip { get { return m_Tooltip; } }
public Series series { get { return m_Series; } }
public bool large { get { return m_Large; } set { m_Large = value; } }
/// <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>
/// 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 serie doesn't exist,will be add to series.
/// 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>
public virtual void AddData(string serieName, float value)
{
m_Legend.AddData(serieName);
m_Series.AddData(serieName, value, m_MaxCacheDataNumber);
RefreshChart();
}
/// <summary>
/// Add a data to serie.
/// If serie doesn't exist, the data is ignored.
/// </summary>
/// <param name="serieIndex">the index of serie</param>
/// <param name="value">the data to add</param>
public virtual void AddData(int serieIndex, float value)
{
m_Series.AddData(serieIndex, value, m_MaxCacheDataNumber);
RefreshChart();
}
/// <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)
{
m_Legend.SetActive(serieName, active);
m_Series.SetActive(serieName, 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_Legend.SetActive(serieIndex, active);
m_Series.SetActive(serieIndex, active);
}
/// <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_Legend.IsActive(serieName) || 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_Legend.IsActive(serieIndex) || 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)
{
this.m_Theme = theme;
OnThemeChanged();
RefreshChart();
}
protected override void Awake()
{
if (m_ThemeInfo == null)
@@ -116,72 +285,6 @@ namespace XCharts
}
}
public void ClearData()
{
m_Series.ClearData();
m_Legend.ClearData();
}
public virtual void AddData(string legend, float value)
{
m_Legend.AddData(legend);
m_Series.AddData(legend, value, m_MaxCacheDataNumber);
RefreshChart();
}
public virtual void AddData(int legend, float value)
{
m_Series.AddData(legend, value, m_MaxCacheDataNumber);
}
public virtual void UpdateData(string legend, float value, int dataIndex = 0)
{
m_Series.UpdateData(legend, value, dataIndex);
RefreshChart();
}
public virtual void UpdateData(int legendIndex, float value, int dataIndex = 0)
{
m_Series.UpdateData(legendIndex, value, dataIndex);
RefreshChart();
}
public virtual void SetActive(string legend, bool active)
{
m_Legend.SetActive(legend, active);
m_Series.SetActive(legend, active);
}
public virtual void SetActive(int index, bool active)
{
m_Legend.SetActive(index, active);
m_Series.SetActive(index, active);
}
public virtual bool IsActive(string name)
{
return m_Legend.IsActive(name) || m_Series.IsActive(name);
}
public virtual bool IsActive(int index)
{
return m_Legend.IsActive(index) || m_Series.IsActive(index);
}
public virtual void RemoveData(string legend)
{
m_Legend.RemoveData(legend);
m_Series.RemoveData(legend);
RefreshChart();
}
public void UpdateTheme(Theme theme)
{
this.m_Theme = theme;
OnThemeChanged();
SetAllDirty();
}
private void InitTitle()
{
m_Title.OnChanged();
@@ -411,11 +514,6 @@ namespace XCharts
{
}
public void RefreshChart()
{
m_RefreshChart = true;
}
protected virtual void RefreshTooltip()
{
}

View File

@@ -26,10 +26,10 @@ namespace XCharts
{
var coordinate = new Coordinate
{
m_Left = 40f,
m_Right = 80f,
m_Top = 50f,
m_Bottom = 30f,
m_Left = 50,
m_Right = 30,
m_Top = 60,
m_Bottom = 30,
m_Tickness = 0.6f,
m_FontSize = 16,
};

View File

@@ -27,8 +27,8 @@ namespace XCharts
private List<YAxis> m_CheckYAxises = new List<YAxis>();
private Coordinate m_CheckCoordinate = Coordinate.defaultCoordinate;
public float zeroX { get { return coordinateX; } }
public float zeroY { get { return coordinateY; } }
// public float coordinateX { get { return coordinateX; } }
// public float coordinateY { get { return coordinateY; } }
public float coordinateX { get { return m_Coordinate.left; } }
public float coordinateY { get { return m_Coordinate.bottom; } }
public float coordinateWid { get { return chartWidth - m_Coordinate.left - m_Coordinate.right; } }
@@ -36,6 +36,52 @@ namespace XCharts
public List<XAxis> xAxises { get { return m_XAxises; } }
public List<YAxis> yAxises { get { return m_YAxises; } }
/// <summary>
/// Remove all data from series,legend and axis.
/// It just emptying all of serie's data without emptying the list of series.
/// </summary>
public override void ClearData()
{
base.ClearData();
ClearAxisData();
}
/// <summary>
/// Remove all data from series,legend and axis.
/// The series list is also cleared.
/// </summary>
public override void RemoveData()
{
base.RemoveData();
ClearAxisData();
}
/// <summary>
/// Remove all data of axises.
/// </summary>
public void ClearAxisData()
{
foreach (var item in m_XAxises) item.data.Clear();
foreach (var item in m_YAxises) item.data.Clear();
}
/// <summary>
///
/// </summary>
/// <param name="category"></param>
/// <param name="xAxisIndex"></param>
public void AddXAxisData(string category, int xAxisIndex = 0)
{
m_XAxises[xAxisIndex].AddData(category, m_MaxCacheDataNumber);
OnXAxisChanged();
}
public void AddYAxisData(string category, int yAxisIndex = 0)
{
m_YAxises[yAxisIndex].AddData(category, m_MaxCacheDataNumber);
OnYAxisChanged();
}
protected override void Awake()
{
base.Awake();
@@ -64,9 +110,7 @@ namespace XCharts
m_Coordinate = Coordinate.defaultCoordinate;
m_XAxises.Clear();
m_YAxises.Clear();
InitDefaultAxises();
InitAxisX();
InitAxisY();
Awake();
}
#endif
@@ -270,39 +314,24 @@ namespace XCharts
InitAxisY();
}
public void ClearAxisData()
{
foreach (var item in m_XAxises) item.data.Clear();
foreach (var item in m_YAxises) item.data.Clear();
}
public void AddXAxisData(string category, int xAxisIndex = 0)
{
m_XAxises[xAxisIndex].AddData(category, m_MaxCacheDataNumber);
OnXAxisChanged();
}
public void AddYAxisData(string category, int yAxisIndex = 0)
{
m_YAxises[yAxisIndex].AddData(category, m_MaxCacheDataNumber);
OnYAxisChanged();
}
private void InitDefaultAxises()
{
if (m_XAxises.Count <= 0)
{
var axis1 = new XAxis();
axis1.Copy(axis1);
var axis1 = XAxis.defaultXAxis;
var axis2 = XAxis.defaultXAxis;
axis1.show = true;
axis2.show = false;
m_XAxises.Add(axis1);
m_XAxises.Add(axis2);
}
if (m_YAxises.Count <= 0)
{
var axis1 = new YAxis();
axis1.Copy(axis1);
var axis1 = YAxis.defaultYAxis;
var axis2 = YAxis.defaultYAxis;
axis1.show = true;
axis1.splitNumber = 6;
axis1.boundaryGap = false;
axis2.show = false;
m_YAxises.Add(axis1);
m_YAxises.Add(axis2);
@@ -353,7 +382,7 @@ namespace XCharts
}
txt.transform.localPosition = GetLabelYPosition(labelWidth, i, yAxisIndex, yAxis);
txt.text = yAxis.GetScaleName(i, yAxis.minValue, yAxis.maxValue, m_DataZoom);
txt.text = yAxis.GetLabelName(i, yAxis.minValue, yAxis.maxValue, m_DataZoom);
txt.gameObject.SetActive(yAxis.show &&
(yAxis.axisLabel.interval == 0 || i % (yAxis.axisLabel.interval + 1) == 0));
yAxis.axisLabelTextList.Add(txt);
@@ -397,12 +426,15 @@ namespace XCharts
}
}
//init tooltip label
Vector2 privot = yAxisIndex > 0 ? new Vector2(0, 0.5f) : new Vector2(1, 0.5f);
var labelParent = m_Tooltip.gameObject.transform;
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
yAxis.SetTooltipLabel(labelObj);
yAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
yAxis.SetTooltipLabelActive(yAxis.show && m_Tooltip.show && m_Tooltip.crossLabel);
if (m_Tooltip.gameObject)
{
Vector2 privot = yAxisIndex > 0 ? new Vector2(0, 0.5f) : new Vector2(1, 0.5f);
var labelParent = m_Tooltip.gameObject.transform;
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
yAxis.SetTooltipLabel(labelObj);
yAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
yAxis.SetTooltipLabelActive(yAxis.show && m_Tooltip.show && m_Tooltip.crossLabel);
}
}
private void InitAxisX()
@@ -438,7 +470,7 @@ namespace XCharts
xAxis.axisLabel.fontSize, xAxis.axisLabel.rotate, xAxis.axisLabel.fontStyle);
txt.transform.localPosition = GetLabelXPosition(labelWidth, i, xAxisIndex, xAxis);
txt.text = xAxis.GetScaleName(i, xAxis.minValue, xAxis.maxValue, m_DataZoom);
txt.text = xAxis.GetLabelName(i, xAxis.minValue, xAxis.maxValue, m_DataZoom);
txt.gameObject.SetActive(xAxis.show &&
(xAxis.axisLabel.interval == 0 || i % (xAxis.axisLabel.interval + 1) == 0));
xAxis.axisLabelTextList.Add(txt);
@@ -481,12 +513,15 @@ namespace XCharts
break;
}
}
Vector2 privot = xAxisIndex > 0 ? new Vector2(0.5f, 1) : new Vector2(0.5f, 1);
var labelParent = m_Tooltip.gameObject.transform;
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
xAxis.SetTooltipLabel(labelObj);
xAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
xAxis.SetTooltipLabelActive(xAxis.show && m_Tooltip.show && m_Tooltip.crossLabel);
if (m_Tooltip.gameObject)
{
Vector2 privot = xAxisIndex > 0 ? new Vector2(0.5f, 1) : new Vector2(0.5f, 1);
var labelParent = m_Tooltip.gameObject.transform;
GameObject labelObj = ChartHelper.AddTooltipLabel(objName + "_label", labelParent, m_ThemeInfo.font, privot);
xAxis.SetTooltipLabel(labelObj);
xAxis.SetTooltipLabelColor(m_ThemeInfo.tooltipBackgroundColor, m_ThemeInfo.tooltipTextColor);
xAxis.SetTooltipLabelActive(xAxis.show && m_Tooltip.show && m_Tooltip.crossLabel);
}
}
private void InitDataZoom()

View File

@@ -44,7 +44,7 @@ namespace XCharts
{
m_Show = true,
m_Orient = Orient.Horizonal,
m_Location = Location.defaultRight,
m_Location = Location.defaultTop,
m_ItemWidth = 60.0f,
m_ItemHeight = 20.0f,
m_ItemGap = 5,
@@ -54,6 +54,7 @@ namespace XCharts
"Legend"
}
};
legend.location.top = 30;
return legend;
}
}

View File

@@ -92,10 +92,16 @@ namespace XCharts
m_LineColor = Color.grey,
m_Indicator = true,
m_BackgroundColorList = new List<Color> {
new Color32(194, 53, 49, 255),
new Color32(47, 69, 84, 255)
new Color32(246, 246, 246, 255),
new Color32(231, 231, 231, 255)
},
m_IndicatorList = new List<Indicator>(5)
m_IndicatorList = new List<Indicator>(5){
new Indicator(){name="radar1",max = 100},
new Indicator(){name="radar2",max = 100},
new Indicator(){name="radar3",max = 100},
new Indicator(){name="radar4",max = 100},
new Indicator(){name="radar5",max = 100},
}
};
return radar;
}

View File

@@ -149,13 +149,24 @@ namespace XCharts
while (m_XData.Count > maxDataNumber) m_XData.RemoveAt(0);
while (m_YData.Count > maxDataNumber) m_YData.RemoveAt(0);
}
m_YData.Add(value);
m_XData.Add(m_XData.Count);
m_YData.Add(value);
}
public void AddXYData(float xValue, float yValue, int maxDataNumber = 0)
{
if (maxDataNumber > 0)
{
while (m_XData.Count > maxDataNumber) m_XData.RemoveAt(0);
while (m_YData.Count > maxDataNumber) m_YData.RemoveAt(0);
}
m_XData.Add(xValue);
m_YData.Add(yValue);
}
public float GetYData(int index, DataZoom dataZoom = null)
{
var showData = GetYData(dataZoom);
var showData = GetYDataList(dataZoom);
if (index >= 0 && index <= showData.Count - 1)
{
return showData[index];
@@ -163,7 +174,17 @@ namespace XCharts
return 0;
}
public List<float> GetYData(DataZoom dataZoom)
public void GetXYData(int index, DataZoom dataZoom, out float xValue, out float yVlaue)
{
xValue = 0;
yVlaue = 0;
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];
}
public List<float> GetYDataList(DataZoom dataZoom)
{
if (dataZoom != null && dataZoom.show)
{
@@ -182,7 +203,7 @@ namespace XCharts
}
}
public List<float> GetXData(DataZoom dataZoom)
public List<float> GetXDataList(DataZoom dataZoom)
{
if (dataZoom != null && dataZoom.show)
{
@@ -246,6 +267,18 @@ namespace XCharts
}
}
public void UpdateXYData(int index, float xValue, float yValue)
{
if (index >= 0 && index <= m_YData.Count - 1)
{
m_YData[index] = yValue;
}
if (index >= 0 && index <= m_XData.Count - 1)
{
m_XData[index] = xValue;
}
}
public override void ParseJsonData(string jsonData)
{
if (string.IsNullOrEmpty(jsonData) || !m_DataFromJson) return;

View File

@@ -79,15 +79,47 @@ namespace XCharts
return false;
}
public void RemoveData(string name)
/// <summary>
/// Remove serie from series.
/// </summary>
/// <param name="serieName">the name of serie</param>
public void Remove(string serieName)
{
var serie = GetSerie(name);
var serie = GetSerie(serieName);
if (serie != null)
{
m_Series.Remove(serie);
}
}
/// <summary>
/// Remove all serie from series.
/// </summary>
public void RemoveAll()
{
m_Series.Clear();
}
public Serie AddSerie(string serieName, SerieType type, bool show = true)
{
var serie = GetSerie(serieName);
if (serie == null)
{
serie = new Serie();
serie.type = type;
serie.show = show;
serie.name = serieName;
serie.yData = new List<float>();
m_Series.Add(serie);
}
else
{
serie.type = type;
serie.show = show;
}
return serie;
}
public Serie AddData(string name, float value, int maxDataNumber = 0)
{
if (m_Series == null)
@@ -116,6 +148,16 @@ namespace XCharts
return serie;
}
public Serie AddXYData(int index, float xValue, float yValue, int maxDataNumber = 0)
{
var serie = GetSerie(index);
if (serie != null)
{
serie.AddXYData(xValue, yValue, maxDataNumber);
}
return serie;
}
public void UpdateData(string name, float value, int dataIndex = 0)
{
var serie = GetSerie(name);
@@ -125,6 +167,15 @@ namespace XCharts
}
}
public void UpdateXYData(string name, float xValue, float yValue, int dataIndex = 0)
{
var serie = GetSerie(name);
if (serie != null)
{
serie.UpdateXYData(dataIndex, xValue, yValue);
}
}
public void UpdateData(int index, float value, int dataIndex = 0)
{
var serie = GetSerie(index);
@@ -134,6 +185,15 @@ namespace XCharts
}
}
public void UpdateXYData(int index, float xValue, float yValue, int dataIndex = 0)
{
var serie = GetSerie(index);
if (serie != null)
{
serie.UpdateXYData(dataIndex, xValue, yValue);
}
}
public void UpdateFilterData(DataZoom dataZoom)
{
if (dataZoom != null && dataZoom.show)
@@ -186,12 +246,12 @@ namespace XCharts
public void GetXMinMaxValue(DataZoom dataZoom, int axisIndex, out int minVaule, out int maxValue)
{
GetMinMaxValue(dataZoom,axisIndex,false,out minVaule,out maxValue);
GetMinMaxValue(dataZoom, axisIndex, false, out minVaule, out maxValue);
}
public void GetYMinMaxValue(DataZoom dataZoom, int axisIndex, out int minVaule, out int maxValue)
{
GetMinMaxValue(dataZoom,axisIndex,true,out minVaule,out maxValue);
GetMinMaxValue(dataZoom, axisIndex, true, out minVaule, out maxValue);
}
public void GetMinMaxValue(DataZoom dataZoom, int axisIndex, bool yValue, out int minVaule, out int maxValue)
@@ -208,7 +268,7 @@ namespace XCharts
{
var serie = ss.Value[i];
if (serie.axisIndex != axisIndex) continue;
var showData = yValue ? serie.GetYData(dataZoom) : serie.GetXData(dataZoom);
var showData = yValue ? serie.GetYDataList(dataZoom) : serie.GetXDataList(dataZoom);
for (int j = 0; j < showData.Count; j++)
{
if (!seriesTotalValue.ContainsKey(j))
@@ -234,7 +294,7 @@ namespace XCharts
if (m_Series[i].axisIndex != axisIndex) continue;
if (IsActive(i))
{
var showData = yValue ? m_Series[i].GetYData(dataZoom) : m_Series[i].GetXData(dataZoom);
var showData = yValue ? m_Series[i].GetYDataList(dataZoom) : m_Series[i].GetXDataList(dataZoom);
foreach (var data in showData)
{
if (data > max) max = data;
@@ -246,7 +306,7 @@ namespace XCharts
if (max == int.MinValue && min == int.MaxValue)
{
minVaule = 0;
maxValue = 100;
maxValue = 90;
}
else
{

View File

@@ -4,6 +4,10 @@ using UnityEngine.UI;
namespace XCharts
{
[AddComponentMenu("XCharts/LineChart", 13)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class LineChart : CoordinateChart
{
[SerializeField] private Line m_Line = Line.defaultLine;
@@ -15,6 +19,13 @@ namespace XCharts
{
base.Reset();
m_Line = Line.defaultLine;
RemoveData();
AddSerie("line1", SerieType.Line);
for (int i = 0; i < 5; i++)
{
AddXAxisData("x" + (i + 1));
AddData(0, Random.Range(10, 90));
}
}
#endif
@@ -91,15 +102,15 @@ namespace XCharts
var axis = m_XAxises[i];
if (!axis.show) continue;
float splitWidth = axis.GetSplitWidth(coordinateWid, m_DataZoom);
float px = zeroX + (m_Tooltip.xValues[i] - 1) * splitWidth
float px = coordinateX + (m_Tooltip.xValues[i] - 1) * splitWidth
+ (axis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(px, coordinateY);
Vector2 ep = new Vector2(px, coordinateY + coordinateHig);
ChartHelper.DrawLine(vh, sp, ep, m_Coordinate.tickness, m_ThemeInfo.tooltipLineColor);
if (m_Tooltip.crossLabel)
{
sp = new Vector2(zeroX, m_Tooltip.pointerPos.y);
ep = new Vector2(zeroX + coordinateWid, m_Tooltip.pointerPos.y);
sp = new Vector2(coordinateX, m_Tooltip.pointerPos.y);
ep = new Vector2(coordinateX + coordinateWid, m_Tooltip.pointerPos.y);
DrawSplitLine(vh, true, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
}
}
@@ -159,14 +170,14 @@ namespace XCharts
var axis = m_YAxises[i];
if (!axis.show) continue;
float splitWidth = axis.GetSplitWidth(coordinateHig, m_DataZoom);
float pY = zeroY + (m_Tooltip.yValues[i] - 1) * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0);
float pY = coordinateY + (m_Tooltip.yValues[i] - 1) * splitWidth + (axis.boundaryGap ? splitWidth / 2 : 0);
Vector2 sp = new Vector2(coordinateX, pY);
Vector2 ep = new Vector2(coordinateX + coordinateWid, pY);
ChartHelper.DrawLine(vh, sp, ep, m_Coordinate.tickness, m_ThemeInfo.tooltipFlagAreaColor);
if (m_Tooltip.crossLabel)
{
sp = new Vector2(m_Tooltip.pointerPos.x, zeroY);
ep = new Vector2(m_Tooltip.pointerPos.x, zeroY + coordinateHig);
sp = new Vector2(m_Tooltip.pointerPos.x, coordinateY);
ep = new Vector2(m_Tooltip.pointerPos.x, coordinateY + coordinateHig);
DrawSplitLine(vh, false, Axis.SplitLineType.Dashed, sp, ep, m_ThemeInfo.tooltipLineColor);
}
}
@@ -180,8 +191,8 @@ namespace XCharts
List<Vector3> lastPoints = new List<Vector3>();
List<Vector3> lastSmoothPoints = new List<Vector3>();
List<Vector3> smoothPoints = new List<Vector3>();
List<float> yData = serie.GetYData(m_DataZoom);
List<float> xData = serie.GetXData(m_DataZoom);
List<float> yData = serie.GetYDataList(m_DataZoom);
List<float> xData = serie.GetXDataList(m_DataZoom);
Color color = m_ThemeInfo.GetColor(serieIndex);
Vector3 lp = Vector3.zero;
@@ -190,7 +201,7 @@ namespace XCharts
var xAxis = m_XAxises[serie.axisIndex];
if (!xAxis.show) xAxis = m_XAxises[(serie.axisIndex + 1) % m_XAxises.Count];
float scaleWid = xAxis.GetDataWidth(coordinateWid, m_DataZoom);
float startX = zeroX + (xAxis.boundaryGap ? scaleWid / 2 : 0);
float startX = coordinateX + (xAxis.boundaryGap ? scaleWid / 2 : 0);
int maxCount = maxShowDataNumber > 0 ?
(maxShowDataNumber > yData.Count ? yData.Count : maxShowDataNumber)
: yData.Count;
@@ -254,8 +265,8 @@ namespace XCharts
if (m_Line.area)
{
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, new Vector2(middle1.x, zeroY), middle1, np,
new Vector2(np.x, zeroY), areaColor);
ChartHelper.DrawPolygon(vh, new Vector2(middle1.x, coordinateY), middle1, np,
new Vector2(np.x, coordinateY), areaColor);
}
break;
case Line.StepType.Middle:
@@ -268,11 +279,11 @@ namespace XCharts
if (m_Line.area)
{
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, new Vector2(lp.x, zeroY), lp, middle1,
new Vector2(middle1.x, zeroY), areaColor);
ChartHelper.DrawPolygon(vh, new Vector2(middle2.x + 2 * m_Line.tickness, zeroY),
ChartHelper.DrawPolygon(vh, new Vector2(lp.x, coordinateY), lp, middle1,
new Vector2(middle1.x, coordinateY), areaColor);
ChartHelper.DrawPolygon(vh, new Vector2(middle2.x + 2 * m_Line.tickness, coordinateY),
new Vector2(middle2.x + 2 * m_Line.tickness, middle2.y), np,
new Vector2(np.x, zeroY), areaColor);
new Vector2(np.x, coordinateY), areaColor);
}
break;
case Line.StepType.End:
@@ -283,9 +294,9 @@ namespace XCharts
if (m_Line.area)
{
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, new Vector2(lp.x, zeroY), lp,
ChartHelper.DrawPolygon(vh, new Vector2(lp.x, coordinateY), lp,
new Vector2(middle1.x - m_Line.tickness, middle1.y),
new Vector2(middle1.x - m_Line.tickness, zeroY), areaColor);
new Vector2(middle1.x - m_Line.tickness, coordinateY), areaColor);
}
break;
}
@@ -313,14 +324,14 @@ namespace XCharts
lastSmoothPoints[lastSmoothPoints.Count - 1].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount].x,
lastSmoothPoints[smoothPointCount].y + m_Line.tickness)) :
new Vector3(to.x, zeroY + m_Coordinate.tickness);
new Vector3(to.x, coordinateY + m_Coordinate.tickness);
Vector3 tlp = serieIndex > 0 ?
(smoothPointCount > lastSmoothPoints.Count - 1 ?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 2].x,
lastSmoothPoints[lastSmoothPoints.Count - 2].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount - 1].x,
lastSmoothPoints[smoothPointCount - 1].y + m_Line.tickness)) :
new Vector3(start.x, zeroY + m_Coordinate.tickness);
new Vector3(start.x, coordinateY + m_Coordinate.tickness);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
@@ -336,24 +347,24 @@ namespace XCharts
Vector3 alp = new Vector3(lp.x, lp.y - m_Line.tickness);
Vector3 anp = new Vector3(np.x, np.y - m_Line.tickness);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(zeroX, zeroY),
new Vector3(zeroX + coordinateWid, zeroY));
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(coordinateX, coordinateY),
new Vector3(coordinateX + coordinateWid, coordinateY));
if (cross == Vector3.zero)
{
Vector3 tnp = serieIndex > 0 ?
new Vector3(lastPoints[i].x, lastPoints[i].y + m_Line.tickness) :
new Vector3(np.x, zeroY + m_Coordinate.tickness);
new Vector3(np.x, coordinateY + m_Coordinate.tickness);
Vector3 tlp = serieIndex > 0 ?
new Vector3(lastPoints[i - 1].x, lastPoints[i - 1].y + m_Line.tickness) :
new Vector3(lp.x, zeroY + m_Coordinate.tickness);
new Vector3(lp.x, coordinateY + m_Coordinate.tickness);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
else
{
Vector3 cross1 = new Vector3(cross.x, cross.y + (alp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 cross2 = new Vector3(cross.x, cross.y + (anp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 xp1 = new Vector3(alp.x, zeroY + (alp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 xp2 = new Vector3(anp.x, zeroY + (anp.y > zeroY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 cross1 = new Vector3(cross.x, cross.y + (alp.y > coordinateY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 cross2 = new Vector3(cross.x, cross.y + (anp.y > coordinateY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 xp1 = new Vector3(alp.x, coordinateY + (alp.y > coordinateY ? m_Coordinate.tickness : -m_Coordinate.tickness));
Vector3 xp2 = new Vector3(anp.x, coordinateY + (anp.y > coordinateY ? m_Coordinate.tickness : -m_Coordinate.tickness));
ChartHelper.DrawTriangle(vh, alp, cross1, xp1, areaColor);
ChartHelper.DrawTriangle(vh, anp, cross2, xp2, areaColor);
}
@@ -385,7 +396,7 @@ namespace XCharts
var yAxis = m_YAxises[serie.axisIndex];
if (!yAxis.show) yAxis = m_YAxises[(serie.axisIndex + 1) % m_YAxises.Count];
float scaleWid = yAxis.GetDataWidth(coordinateHig, m_DataZoom);
float startY = zeroY + (yAxis.boundaryGap ? scaleWid / 2 : 0);
float startY = coordinateY + (yAxis.boundaryGap ? scaleWid / 2 : 0);
int maxCount = maxShowDataNumber > 0 ?
(maxShowDataNumber > serie.yData.Count ? serie.yData.Count : maxShowDataNumber)
: serie.yData.Count;
@@ -436,8 +447,8 @@ namespace XCharts
if (m_Line.area)
{
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, new Vector2(zeroX, middle1.y), middle1, np,
new Vector2(zeroX, np.y), areaColor);
ChartHelper.DrawPolygon(vh, new Vector2(coordinateX, middle1.y), middle1, np,
new Vector2(coordinateX, np.y), areaColor);
}
break;
case Line.StepType.Middle:
@@ -450,11 +461,11 @@ namespace XCharts
if (m_Line.area)
{
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, new Vector2(zeroX, lp.y), lp, middle1,
new Vector2(zeroX, middle1.y), areaColor);
ChartHelper.DrawPolygon(vh, new Vector2(zeroX, middle2.y + 2 * m_Line.tickness),
ChartHelper.DrawPolygon(vh, new Vector2(coordinateX, lp.y), lp, middle1,
new Vector2(coordinateX, middle1.y), areaColor);
ChartHelper.DrawPolygon(vh, new Vector2(coordinateX, middle2.y + 2 * m_Line.tickness),
new Vector2(middle2.x, middle2.y + 2 * m_Line.tickness), np,
new Vector2(zeroX, np.y), areaColor);
new Vector2(coordinateX, np.y), areaColor);
}
break;
case Line.StepType.End:
@@ -465,9 +476,9 @@ namespace XCharts
if (m_Line.area)
{
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, new Vector2(zeroX, lp.y), middle1,
ChartHelper.DrawPolygon(vh, new Vector2(coordinateX, lp.y), middle1,
new Vector2(np.x, np.y),
new Vector2(zeroX, np.y), areaColor);
new Vector2(coordinateX, np.y), areaColor);
}
break;
}
@@ -493,14 +504,14 @@ namespace XCharts
lastSmoothPoints[lastSmoothPoints.Count - 1].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount].x,
lastSmoothPoints[smoothPointCount].y + m_Line.tickness)) :
new Vector3(zeroX + m_Coordinate.tickness, to.y);
new Vector3(coordinateX + m_Coordinate.tickness, to.y);
Vector3 tlp = serieIndex > 0 ?
(smoothPointCount > lastSmoothPoints.Count - 1 ?
new Vector3(lastSmoothPoints[lastSmoothPoints.Count - 2].x,
lastSmoothPoints[lastSmoothPoints.Count - 2].y + m_Line.tickness) :
new Vector3(lastSmoothPoints[smoothPointCount - 1].x,
lastSmoothPoints[smoothPointCount - 1].y + m_Line.tickness)) :
new Vector3(zeroX + m_Coordinate.tickness, start.y);
new Vector3(coordinateX + m_Coordinate.tickness, start.y);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
@@ -516,24 +527,24 @@ namespace XCharts
Vector3 alp = new Vector3(lp.x, lp.y - m_Line.tickness);
Vector3 anp = new Vector3(np.x, np.y - m_Line.tickness);
Color areaColor = new Color(color.r, color.g, color.b, color.a * 0.75f);
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(zeroX, zeroY),
new Vector3(zeroX, zeroY + coordinateHig));
var cross = ChartHelper.GetIntersection(lp, np, new Vector3(coordinateX, coordinateY),
new Vector3(coordinateX, coordinateY + coordinateHig));
if (cross == Vector3.zero)
{
Vector3 tnp = serieIndex > 0 ?
new Vector3(lastPoints[i].x, lastPoints[i].y + m_Line.tickness) :
new Vector3(zeroX + m_Coordinate.tickness, np.y);
new Vector3(coordinateX + m_Coordinate.tickness, np.y);
Vector3 tlp = serieIndex > 0 ?
new Vector3(lastPoints[i - 1].x, lastPoints[i - 1].y + m_Line.tickness) :
new Vector3(zeroX + m_Coordinate.tickness, lp.y);
new Vector3(coordinateX + m_Coordinate.tickness, lp.y);
ChartHelper.DrawPolygon(vh, alp, anp, tnp, tlp, areaColor);
}
else
{
Vector3 cross1 = new Vector3(cross.x + (alp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), cross.y);
Vector3 cross2 = new Vector3(cross.x + (anp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), cross.y);
Vector3 xp1 = new Vector3(zeroX + (alp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), alp.y);
Vector3 xp2 = new Vector3(zeroX + (anp.x > zeroX ? m_Coordinate.tickness : -m_Coordinate.tickness), anp.y);
Vector3 cross1 = new Vector3(cross.x + (alp.x > coordinateX ? m_Coordinate.tickness : -m_Coordinate.tickness), cross.y);
Vector3 cross2 = new Vector3(cross.x + (anp.x > coordinateX ? m_Coordinate.tickness : -m_Coordinate.tickness), cross.y);
Vector3 xp1 = new Vector3(coordinateX + (alp.x > coordinateX ? m_Coordinate.tickness : -m_Coordinate.tickness), alp.y);
Vector3 xp2 = new Vector3(coordinateX + (anp.x > coordinateX ? m_Coordinate.tickness : -m_Coordinate.tickness), anp.y);
ChartHelper.DrawTriangle(vh, alp, cross1, xp1, areaColor);
ChartHelper.DrawTriangle(vh, anp, cross2, xp2, areaColor);
}

View File

@@ -5,6 +5,10 @@ using UnityEngine.UI;
namespace XCharts
{
[AddComponentMenu("XCharts/PieChart", 15)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class PieChart : BaseChart
{
[SerializeField] private Pie m_Pie = Pie.defaultPie;
@@ -17,6 +21,39 @@ namespace XCharts
public Pie pie { get { return m_Pie; } }
/// <summary>
/// Add a data to pie.
/// </summary>
/// <param name="legend">the name of data</param>
/// <param name="value">the data</param>
public override void AddData(string legend, float value)
{
m_Legend.AddData(legend);
var serie = m_Series.AddData(legend, value);
if (serie != null)
{
serie.ClearData();
serie.AddYData(value);
RefreshChart();
}
}
/// <summary>
/// Update the data for the specified name.
/// </summary>
/// <param name="legend">the name of data</param>
/// <param name="value">the data</param>
/// <param name="dataIndex">is not used in this function</param>
public override void UpdateData(string legend, float value, int dataIndex = 0)
{
var serie = m_Series.GetSerie(legend);
if (serie != null)
{
serie.UpdateYData(0, value);
RefreshChart();
}
}
protected override void Awake()
{
raycastTarget = m_Pie.selected;
@@ -32,27 +69,16 @@ namespace XCharts
}
}
public override void AddData(string legend, float value)
#if UNITY_EDITOR
protected override void Reset()
{
m_Legend.AddData(legend);
var serie = m_Series.AddData(legend, value);
if (serie != null)
{
serie.ClearData();
serie.AddYData(value);
RefreshChart();
}
}
public override void UpdateData(string legend, float value, int dataIndex = 0)
{
var serie = m_Series.GetSerie(legend);
if (serie != null)
{
serie.UpdateYData(0, value);
RefreshChart();
}
base.Reset();
m_Pie = Pie.defaultPie;
RemoveData();
AddData("Pie1", 80);
AddData("Pie2", 20);
}
#endif
protected override void DrawChart(VertexHelper vh)
{

View File

@@ -5,6 +5,10 @@ using UnityEngine.UI;
namespace XCharts
{
[AddComponentMenu("XCharts/RadarChart", 16)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class RadarChart : BaseChart
{
private const string INDICATOR_TEXT = "indicator";
@@ -20,10 +24,16 @@ namespace XCharts
public Radar radar { get { return m_Radar; } }
public override void RemoveData()
{
base.RemoveData();
m_Radar.indicatorList.Clear();
}
protected override void Awake()
{
base.Awake();
UpdateRadarCenter();
InitIndicator();
}
protected override void Update()
@@ -32,6 +42,21 @@ namespace XCharts
CheckRadarInfoChanged();
}
#if UNITY_EDITOR
protected override void Reset()
{
base.Reset();
RemoveData();
m_Radar = Radar.defaultRadar;
AddSerie("Radar", SerieType.Radar);
for (int i = 0; i < 5; i++)
{
AddData(0, Random.Range(20, 90));
}
}
#endif
private void InitIndicator()
{
indicatorTextList.Clear();
@@ -161,7 +186,7 @@ namespace XCharts
for (int j = 0; j < dataList.Count; j++)
{
var max = m_Radar.GetIndicatorMax(j) > 0 ?
m_Radar.GetIndicatorMax(j) :
m_Radar.GetIndicatorMax(j) :
GetMaxValue(j);
var radius = max < 0 ? m_Radar.radius - m_Radar.radius * dataList[j] / max
: m_Radar.radius * dataList[j] / max;