增加数据更新接口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,23 +6,23 @@ 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>();
var wid = rect.sizeDelta.x;
int childNum = xchart.childCount;
float hig = grid.padding.top + childNum * (grid.cellSize.y+ grid.spacing.y);
rect.sizeDelta = new Vector2(wid,hig);
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,11 +32,9 @@ 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)
if (checkTheme != theme)
{
checkTheme = theme;
UpdateTheme(theme);
@@ -46,7 +44,7 @@ public class Demo : MonoBehaviour
void UpdateTheme(Theme theme)
{
var charts = transform.Find("xchart").GetComponentsInChildren<BaseChart>();
foreach(var chart in charts)
foreach (var chart in charts)
{
chart.UpdateTheme(theme);
}

View File

@@ -28,7 +28,7 @@ namespace xcharts
protected override void DrawChart(VertexHelper vh)
{
base.DrawChart(vh);
if(yAxis.type == AxisType.category)
if (yAxis.type == AxisType.category)
{
int seriesCount = seriesList.Count;
float scaleWid = coordinateHig / (yAxis.splitNumber - 1);

View File

@@ -215,7 +215,7 @@ namespace xcharts
else
{
StringBuilder sb = new StringBuilder(tempAxis.data[index]);
for(int i=0; i<seriesList.Count;i++)
for (int i = 0; i < seriesList.Count; i++)
{
string strColor = ColorUtility.ToHtmlStringRGBA(themeInfo.GetColor(i));
string key = seriesList[i].name;
@@ -292,7 +292,7 @@ namespace xcharts
for (int i = 0; i < yAxis.splitNumber; i++)
{
Text txt = ChartUtils.AddTextObject(YSCALE_TEXT_PREFIX + i, transform, themeInfo.font,
themeInfo.textColor, TextAnchor.MiddleRight, Vector2.zero, Vector2.zero,
themeInfo.textColor, TextAnchor.MiddleRight, Vector2.zero, Vector2.zero,
new Vector2(1, 0.5f),
new Vector2(coordinate.left, 20));
txt.transform.localPosition = GetYScalePosition(i);
@@ -536,7 +536,7 @@ namespace xcharts
themeInfo.axisLineColor);
if (yAxis.showSplitLine)
{
DrawSplitLine(vh, true,yAxis.splitLineType, new Vector3(zeroX, pY),
DrawSplitLine(vh, true, yAxis.splitLineType, new Vector3(zeroX, pY),
new Vector3(zeroX + coordinateWid, pY));
}
}
@@ -548,20 +548,20 @@ namespace xcharts
themeInfo.axisLineColor);
if (xAxis.showSplitLine)
{
DrawSplitLine(vh, false,xAxis.splitLineType, new Vector3(pX, zeroY),
DrawSplitLine(vh, false, xAxis.splitLineType, new Vector3(pX, zeroY),
new Vector3(pX, zeroY + coordinateHig));
}
}
//draw x,y axis
ChartUtils.DrawLine(vh, new Vector3(zeroX, zeroY - coordinate.splitWidth),
new Vector3(zeroX, zeroY + coordinateHig + 2), coordinate.tickness,
new Vector3(zeroX, zeroY + coordinateHig + 2), coordinate.tickness,
themeInfo.axisLineColor);
ChartUtils.DrawLine(vh, new Vector3(zeroX - coordinate.splitWidth, zeroY),
new Vector3(zeroX + coordinateWid + 2, zeroY), coordinate.tickness,
new Vector3(zeroX + coordinateWid + 2, zeroY), coordinate.tickness,
themeInfo.axisLineColor);
}
private void DrawSplitLine(VertexHelper vh,bool isYAxis,SplitLineType type,Vector3 startPos,
private void DrawSplitLine(VertexHelper vh, bool isYAxis, SplitLineType type, Vector3 startPos,
Vector3 endPos)
{
switch (type)
@@ -571,7 +571,7 @@ namespace xcharts
var startX = startPos.x;
var startY = startPos.y;
var dashLen = type == SplitLineType.dashed ? 6 : 2.5f;
var count = isYAxis ? (endPos.x - startPos.x) / (dashLen * 2):
var count = isYAxis ? (endPos.x - startPos.x) / (dashLen * 2) :
(endPos.y - startPos.y) / (dashLen * 2);
for (int i = 0; i < count; i++)
{
@@ -589,7 +589,7 @@ namespace xcharts
coordinate.tickness, themeInfo.axisSplitLineColor);
startY += dashLen * 2;
}
}
break;
case SplitLineType.solid:

View File

@@ -76,12 +76,12 @@ namespace xcharts
}
}
public void SetShowData(int index,bool flag)
public void SetShowData(int index, bool flag)
{
dataShowList[index] = flag;
}
public void SetDataButton(int index,Button btn)
public void SetDataButton(int index, Button btn)
{
if (index < 0 || index > dataBtnList.Count - 1)
{
@@ -93,9 +93,9 @@ namespace xcharts
}
}
public void SetShowData(string name,bool flag)
public void SetShowData(string name, bool flag)
{
for(int i = 0; i < dataList.Count; i++)
for (int i = 0; i < dataList.Count; i++)
{
if (dataList[i].Equals(name))
{
@@ -212,12 +212,20 @@ namespace xcharts
public float GetData(int index)
{
if (index >=0 && index <= dataList.Count - 1)
if (index >= 0 && index <= dataList.Count - 1)
{
return dataList[index];
}
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;
@@ -355,7 +389,7 @@ namespace xcharts
Button btn = ChartUtils.AddButtonObject(LEGEND_TEXT + i, transform, themeInfo.font,
themeInfo.textColor, Vector2.zero, Vector2.zero, Vector2.zero,
new Vector2(legend.itemWidth, legend.itemHeight));
legend.SetDataButton(i,btn);
legend.SetDataButton(i, btn);
Color bcolor = themeInfo.GetColor(i);
btn.gameObject.SetActive(legend.show);
btn.transform.localPosition = GetLegendPosition(i);
@@ -363,8 +397,8 @@ namespace xcharts
btn.GetComponentInChildren<Text>().text = legend.dataList[i];
btn.onClick.AddListener(delegate ()
{
legend.SetShowData(i,!legend.IsShowSeries(i));
btn.GetComponent<Image>().color = legend.IsShowSeries(i) ?
legend.SetShowData(i, !legend.IsShowSeries(i));
btn.GetComponent<Image>().color = legend.IsShowSeries(i) ?
themeInfo.GetColor(i) : themeInfo.unableColor;
OnYMaxValueChanged();
OnLegendButtonClicked();
@@ -375,7 +409,7 @@ namespace xcharts
private void InitTooltip()
{
GameObject obj = ChartUtils.AddTooltipObject("tooltip", transform,themeInfo.font);
GameObject obj = ChartUtils.AddTooltipObject("tooltip", transform, themeInfo.font);
tooltip.SetObj(obj);
tooltip.SetBackgroundColor(themeInfo.tooltipBackgroundColor);
tooltip.SetTextColor(themeInfo.tooltipTextColor);
@@ -496,7 +530,7 @@ namespace xcharts
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
Input.mousePosition, null, out local))
return;
if (local.x < 0 || local.x > chartWid ||
local.y < 0 || local.y > chartHig)
return;
@@ -598,4 +632,3 @@ namespace xcharts
}
}
}

View File

@@ -97,7 +97,7 @@ namespace xcharts
{
float value = series.dataList[i];
Vector3 p = new Vector3(startX + i * scaleWid,
Vector3 p = new Vector3(startX + i * scaleWid,
zeroY + value * coordinateHig / max);
float pointWid = lineInfo.pointWid;
if (tooltip.show && i == tooltip.DataIndex - 1)
@@ -106,8 +106,8 @@ namespace xcharts
}
if (theme == Theme.Dark)
{
ChartUtils.DrawCricle(vh, p, pointWid, color,
ChartUtils.DrawCricle(vh, p, pointWid, color,
(int)lineInfo.pointWid * 5);
}
else

View File

@@ -52,11 +52,11 @@ namespace xcharts
float degree = totalDegree * value / dataTotal;
float toDegree = startDegree + degree;
float outSideRadius = pieInfo.outsideRadiusDynamic ?
pieInfo.insideRadius + (pieRadius - pieInfo.insideRadius) * value / dataMax :
float outSideRadius = pieInfo.outsideRadiusDynamic ?
pieInfo.insideRadius + (pieRadius - pieInfo.insideRadius) * value / dataMax :
pieRadius;
ChartUtils.DrawDoughnut(vh, new Vector3(pieCenterX, pieCenterY), pieInfo.insideRadius,
outSideRadius,startDegree, toDegree, themeInfo.GetColor(i));
ChartUtils.DrawDoughnut(vh, new Vector3(pieCenterX, pieCenterY), pieInfo.insideRadius,
outSideRadius, startDegree, toDegree, themeInfo.GetColor(i));
startDegree = toDegree;
}
}
@@ -70,7 +70,7 @@ namespace xcharts
private float GetDataTotal()
{
float total = 0;
for(int i = 0; i < seriesList.Count; i++)
for (int i = 0; i < seriesList.Count; i++)
{
if (legend.IsShowSeries(i))
{
@@ -83,9 +83,9 @@ namespace xcharts
private float GetDataMax()
{
float max = 0;
for(int i = 0; i < seriesList.Count; i++)
for (int i = 0; i < seriesList.Count; i++)
{
if(legend.IsShowSeries(i) && seriesList[i].GetData(0) > max)
if (legend.IsShowSeries(i) && seriesList[i].GetData(0) > max)
{
max = seriesList[i].GetData(0);
}
@@ -98,7 +98,7 @@ namespace xcharts
float diffX = chartWid - pieInfo.left - pieInfo.right;
float diffY = chartHig - pieInfo.top - pieInfo.bottom;
float diff = Mathf.Min(diffX, diffY);
if(pieInfo.outsideRadius <= 0)
if (pieInfo.outsideRadius <= 0)
{
pieRadius = diff / 3 * 2;
pieCenterX = pieInfo.left + pieRadius;

View File

@@ -16,7 +16,7 @@ namespace xcharts
{
public bool cricle;
public bool area;
public float radius = 100;
public int splitNumber = 5;
@@ -80,7 +80,7 @@ namespace xcharts
else if (diff > 1f)
{
anchor = TextAnchor.MiddleLeft;
pos = new Vector3(pos.x + txtWid + 5,pos.y);
pos = new Vector3(pos.x + txtWid + 5, pos.y);
}
else
{
@@ -100,7 +100,7 @@ namespace xcharts
private void CheckRadarInfoChanged()
{
if( checkRadarInfo.radius != radarInfo.radius ||
if (checkRadarInfo.radius != radarInfo.radius ||
checkRadarInfo.left != radarInfo.left ||
checkRadarInfo.right != radarInfo.right ||
checkRadarInfo.top != radarInfo.top ||
@@ -132,7 +132,7 @@ namespace xcharts
var x = radarCenterX + radarInfo.radius * Mathf.Sin(angle);
var y = radarCenterY + radarInfo.radius * Mathf.Cos(angle);
return new Vector3(x,y);
return new Vector3(x, y);
}
protected override void DrawChart(VertexHelper vh)
@@ -181,13 +181,13 @@ namespace xcharts
Vector3 startPoint = Vector3.zero;
Vector3 toPoint = Vector3.zero;
Vector3 firstPoint = Vector3.zero;
for (int i = 0; i < seriesList.Count; i++)
{
if (!legend.IsShowSeries(i)) continue;
var dataList = seriesList[i].dataList;
var color = themeInfo.GetColor(i);
var areaColor = new Color(color.r,color.g,color.b,color.a*0.7f);
var areaColor = new Color(color.r, color.g, color.b, color.a * 0.7f);
var max = radarInfo.indicatorList[i].max > 0 ?
radarInfo.indicatorList[i].max :
GetMaxValue();
@@ -233,11 +233,11 @@ namespace xcharts
float block = radarInfo.radius / radarInfo.splitNumber;
int indicatorNum = radarInfo.indicatorList.Count;
Vector3 p1, p2, p3, p4;
Vector3 p = new Vector3(radarCenterX,radarCenterY);
Vector3 p = new Vector3(radarCenterX, radarCenterY);
float angle = 2 * Mathf.PI / indicatorNum;
for (int i = 0; i < radarInfo.splitNumber; i++)
{
Color color = radarInfo.backgroundColorList[i% radarInfo.backgroundColorList.Count];
Color color = radarInfo.backgroundColorList[i % radarInfo.backgroundColorList.Count];
outsideRadius = insideRadius + block;
p1 = new Vector3(p.x + insideRadius * Mathf.Sin(0), p.y + insideRadius * Mathf.Cos(0));
p2 = new Vector3(p.x + outsideRadius * Mathf.Sin(0), p.y + outsideRadius * Mathf.Cos(0));
@@ -261,7 +261,7 @@ namespace xcharts
float currAngle = j * angle;
p3 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p.y + outsideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness/2, radarInfo.lineColor);
ChartUtils.DrawLine(vh, p, p3, radarInfo.lineTickness / 2, radarInfo.lineColor);
}
}
@@ -285,7 +285,7 @@ namespace xcharts
for (int j = 0; j <= indicatorNum; j++)
{
float currAngle = j * angle;
p1 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p1 = new Vector3(p.x + outsideRadius * Mathf.Sin(currAngle),
p.y + outsideRadius * Mathf.Cos(currAngle));
ChartUtils.DrawLine(vh, p, p1, radarInfo.lineTickness / 2, radarInfo.lineColor);
}

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