增加数据更新接口UpdateData

This commit is contained in:
monitor1394
2019-03-16 07:49:36 +08:00
parent 9edaf02ccb
commit 9e57dc3d09
8 changed files with 101 additions and 80 deletions

View File

@@ -6,15 +6,13 @@ public class Demo : MonoBehaviour
{
public Theme theme = Theme.Dark;
private LineChart lineChart;
private BaseChart chart;
private float time;
private int count;
private Theme checkTheme = Theme.Dark;
void Awake()
{
//lineChart = transform.Find("xchart/line_chart").GetComponent<LineChart>();
var xchart = transform.Find("xchart");
GridLayoutGroup grid = xchart.GetComponent<GridLayoutGroup>();
RectTransform rect = transform.GetComponent<RectTransform>();
@@ -23,6 +21,8 @@ public class Demo : MonoBehaviour
float hig = grid.padding.top + childNum * (grid.cellSize.y + grid.spacing.y);
rect.sizeDelta = new Vector2(wid, hig);
xchart.GetComponent<RectTransform>().sizeDelta = new Vector2(wid, hig);
chart = xchart.gameObject.GetComponentInChildren<BaseChart>();
}
void Update()
@@ -32,9 +32,7 @@ public class Demo : MonoBehaviour
{
time = 0;
count++;
//lineChart.AddXAxisCategory("key" + count);
//lineChart.AddData("line1", "key"+count, Random.Range(24.0f, 60.0f));
//lineChart.AddData("line2", "key"+count, Random.Range(24.0f, 60.0f));
chart.UpdateData(0, Random.RandomRange(60, 150));
}
if (checkTheme != theme)
{

View File

@@ -218,6 +218,14 @@ namespace xcharts
}
return 0;
}
public void UpdataData(int index, float value)
{
if (index >= 0 && index <= dataList.Count - 1)
{
dataList[index] = value;
}
}
}
public class BaseChart : MaskableGraphic
@@ -277,7 +285,7 @@ namespace xcharts
{
for (int i = 0; i < seriesList.Count; i++)
{
if (seriesList[i].name == legend)
if (seriesList[i].name.Equals(legend))
{
seriesList[i].AddData(value);
break;
@@ -286,6 +294,32 @@ namespace xcharts
RefreshChart();
}
public void UpdateData(string legend, float value, int dataIndex = 0)
{
for (int i = 0; i < seriesList.Count; i++)
{
if (seriesList[i].name.Equals(legend))
{
seriesList[i].UpdataData(dataIndex, value);
break;
}
}
RefreshChart();
}
public void UpdateData(int legendIndex, float value, int dataIndex = 0)
{
for (int i = 0; i < seriesList.Count; i++)
{
if (i == legendIndex)
{
seriesList[i].UpdataData(dataIndex, value);
break;
}
}
RefreshChart();
}
public void UpdateTheme(Theme theme)
{
this.theme = theme;
@@ -598,4 +632,3 @@ namespace xcharts
}
}
}

View File

@@ -593,8 +593,8 @@ RectTransform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 505350089}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 599, y: 459.5, z: 0}
m_LocalScale: {x: 0.9359375, y: 0.9359375, z: 0.9359375}
m_LocalPosition: {x: 600.5, y: 459.5, z: 0}
m_LocalScale: {x: 0.93828124, y: 0.93828124, z: 0.93828124}
m_Children:
- {fileID: 2051892027}
m_Father: {fileID: 0}
@@ -830,27 +830,24 @@ MonoBehaviour:
top: 0
bottom: 0
dataList:
- show: 1
key: pie1
text: "\u6570\u636E1"
- show: 1
key: pie2
text: "\u6570\u636E2"
- show: 1
key: pie3
text: "\u6570\u636E3"
- "H\x01"
- "O\x01"
- "V\x01"
tooltip:
show: 0
seriesList:
- legendKey:
- name: H
showDataNumber: 0
dataList: []
- legendKey:
dataList:
- 60
- name: O
showDataNumber: 0
dataList: []
- legendKey:
dataList:
- 30
- name: V
showDataNumber: 0
dataList: []
dataList:
- 20
pieInfo:
name: Pie
insideRadius: 0
@@ -861,13 +858,6 @@ MonoBehaviour:
right: 0
top: 0
bottom: 0
dataList:
- text: pie1
value: 10
- text: pie2
value: 50
- text: pie3
value: 40
--- !u!222 &872320148
CanvasRenderer:
m_ObjectHideFlags: 0
@@ -1126,7 +1116,7 @@ MonoBehaviour:
m_HorizontalOverflow: 1
m_VerticalOverflow: 1
m_LineSpacing: 1
m_Text: "\u6570\u636E1"
m_Text: "H\x01"
--- !u!222 &1679032979
CanvasRenderer:
m_ObjectHideFlags: 0
@@ -1200,7 +1190,7 @@ MonoBehaviour:
m_HorizontalOverflow: 1
m_VerticalOverflow: 1
m_LineSpacing: 1
m_Text: "\u6570\u636E3"
m_Text: "V\x01"
--- !u!222 &1790728300
CanvasRenderer:
m_ObjectHideFlags: 0
@@ -1484,7 +1474,7 @@ MonoBehaviour:
m_HorizontalOverflow: 1
m_VerticalOverflow: 1
m_LineSpacing: 1
m_Text: "\u6570\u636E2"
m_Text: "O\x01"
--- !u!222 &2099261119
CanvasRenderer:
m_ObjectHideFlags: 0