简书链接:【原创】unityxchartbarringpie等预制体添加数据实现 文章字数:216,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 using System.Collections; using System.Collections.Generic; using UnityEngine; using XCharts.Runtime; public class TestChart : MonoBehaviour { public GameObject uiGameObject; // Start is called before the first frame update void Start() { //testMultiBarChart(); //testMultiLineChart(); testRingCHart(); } private void testPieChart() { GameObject prefabs = Resources.Load<GameObject>("Prefabs/Chart/piechart"); GameObject chartGameObject = Instantiate(prefabs); PieChart baseChart = chartGameObject.GetComponent<PieChart>(); PieChart pieChart = (PieChart)baseChart; pieChart.GetOrAddChartComponent<Legend>().show = true; pieChart.ClearData(); pieChart.AddData(0, 10, "它"); pieChart.AddData(0, 60, "我"); pieChart.AddData(0, 30, "你"); pieChart.RefreshChart(); chartGameObject.transform.parent = uiGameObject.transform; chartGameObject.GetComponent<RectTransform>().anchoredPosition = Vector3.zero; } private void testRingCHart() { GameObject prefabs = Resources.Load<GameObject>("Prefabs/Chart/ringchart"); GameObject chartGameObject = Instantiate(prefabs); RingChart baseChart = chartGameObject.GetComponent<RingChart>(); RingChart ringchart = (RingChart)baseChart; ringchart.GetOrAddChartComponent<Legend>().show = true; ringchart.ClearData(); //ringchart.AddData(0, 30, "描述"); ringchart.AddData(0, 51,100, "描述"); ringchart.RefreshChart(); chartGameObject.transform.parent = uiGameObject.transform; chartGameObject.GetComponent<RectTransform>().anchoredPosition = Vector3.zero; } private void testBarChart() { GameObject prefabs = Resources.Load<GameObject>("Prefabs/Chart/barchart"); GameObject chartGameObject = Instantiate(prefabs); BarChart baseChart = chartGameObject.GetComponent<BarChart>(); BarChart barChart = (BarChart)baseChart; barChart.GetOrAddChartComponent<Title>().show = true; barChart.GetOrAddChartComponent<Tooltip>().show = true; barChart.GetOrAddChartComponent<Legend>().show = false; var xAxis = barChart.GetOrAddChartComponent<XAxis>(); var yAxis = barChart.GetOrAddChartComponent<YAxis>(); xAxis.show = true; yAxis.show = true; /* xAxis.type = Axis.AxisType.Category; yAxis.type = Axis.AxisType.Value;*/ xAxis.splitNumber = 10; xAxis.boundaryGap = true; barChart.RemoveData(); yAxis.minMaxType = Axis.AxisMinMaxType.Default; barChart.RemoveData(); Bar serie = barChart.AddSerie<Bar>("Bar1"); for (int i = 0; i < 5; i++) { barChart.AddXAxisData("x" + (i + 1)); barChart.AddData(0, UnityEngine.Random.Range(30, 90)); } chartGameObject.transform.parent = uiGameObject.transform; chartGameObject.GetComponent<RectTransform>().anchoredPosition = Vector3.zero; } private void testMultiBarChart() { GameObject prefabs = Resources.Load<GameObject>("Prefabs/Chart/barchart"); GameObject chartGameObject = Instantiate(prefabs); BarChart baseChart = chartGameObject.GetComponent<BarChart>(); BarChart barChart = (BarChart)baseChart; barChart.GetOrAddChartComponent<Title>().show = true; barChart.GetOrAddChartComponent<Tooltip>().show = true; barChart.GetOrAddChartComponent<Legend>().show = true; var xAxis = barChart.GetOrAddChartComponent<XAxis>(); var yAxis = barChart.GetOrAddChartComponent<YAxis>(); xAxis.show = true; yAxis.show = true; /* xAxis.type = Axis.AxisType.Category; yAxis.type = Axis.AxisType.Value;*/ xAxis.splitNumber = 0; xAxis.boundaryGap = true; barChart.RemoveData(); yAxis.minMaxType = Axis.AxisMinMaxType.Default; Bar serie = barChart.AddSerie<Bar>("Bar1"); Bar serie1 = barChart.AddSerie<Bar>("Bar2"); Bar serie2 = barChart.AddSerie<Bar>("Bar3"); for (int i = 0; i < 10; i++) { barChart.AddXAxisData("a" + (i + 1),0); barChart.AddData(0, UnityEngine.Random.Range(30, 90)); barChart.AddData(1, UnityEngine.Random.Range(30, 90)); barChart.AddData(2, UnityEngine.Random.Range(30, 90)); } chartGameObject.transform.parent = uiGameObject.transform; chartGameObject.GetComponent<RectTransform>().anchoredPosition = Vector3.zero; } private void testMultiLineChart() { GameObject prefabs = Resources.Load<GameObject>("Prefabs/Chart/linechart"); GameObject chartGameObject = Instantiate(prefabs); BaseChart baseChart = chartGameObject.GetComponent<LineChart>(); LineChart lineChart = (LineChart)baseChart; lineChart.GetOrAddChartComponent<Title>().show = true; lineChart.GetOrAddChartComponent<Tooltip>().show = true; lineChart.GetOrAddChartComponent<Legend>().show = true; var xAxis = lineChart.GetOrAddChartComponent<XAxis>(); var yAxis = lineChart.GetOrAddChartComponent<YAxis>(); xAxis.show = true; yAxis.show = true; xAxis.splitNumber = 0; xAxis.boundaryGap = true; lineChart.RemoveData(); yAxis.minMaxType = Axis.AxisMinMaxType.Default; Line serie = lineChart.AddSerie<Line>("Line1"); Line serie1 = lineChart.AddSerie<Line>("Line2"); Line serie2 = lineChart.AddSerie<Line>("Line3"); for (int i = 0; i < 10; i++) { lineChart.AddXAxisData("a" + (i + 1),0); lineChart.AddData(0, UnityEngine.Random.Range(30, 90)); lineChart.AddData(1, UnityEngine.Random.Range(30, 90)); lineChart.AddData(2, UnityEngine.Random.Range(30, 90)); } chartGameObject.transform.parent = uiGameObject.transform; chartGameObject.GetComponent<RectTransform>().anchoredPosition = Vector3.zero; } // Update is called once per frame void Update() { } }
要实现折线图和 柱状图,则当前组件应该是 linechart,然后添加serice即可实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 if (chartType == IDataType.splinechart) { baseChart = chartGameObject.GetComponent<LineChart>(); LineChart barChart = (LineChart)baseChart; barChart.GetOrAddChartComponent<Title>().show = true; barChart.GetOrAddChartComponent<Tooltip>().show = true; barChart.GetOrAddChartComponent<Legend>().show = true; var xAxis = barChart.GetOrAddChartComponent<XAxis>(); var yAxis = barChart.GetOrAddChartComponent<YAxis>(); xAxis.show = true; yAxis.show = true; xAxis.splitNumber = 10; xAxis.boundaryGap = true; barChart.RemoveData(); JArray xtitle = JSONUtil.getJSONArrayValue(view.detail, "xtitle", null).returnvalue; JArray datas = JSONUtil.getJSONArrayValue(view.detail, "datas", null).returnvalue; if (xtitle != null && datas != null) { int bartypeCount = datas.Count; for (int j = 0; j < bartypeCount; j++) { JObject currentBar = datas[j] as JObject; int type = JSONUtil.getIntValue(currentBar, "type", 0).returnvalue; if (type == 0) { barChart.AddSerie<Bar>(currentBar["title"].ToString()); } else if (type == 1) { barChart.AddSerie<Line>(currentBar["title"].ToString()); } } for (int i = 0; i < xtitle.Count; i++) { String title = xtitle[i].ToString(); barChart.AddXAxisData(title, 0); for (int j = 0; j < bartypeCount; j++) { JObject currentBar = datas[j] as JObject; JArray values = currentBar["value"] as JArray; //currentBar["title"] barChart.AddData(j, values == null || values.Count - 1 < i ? 0 : ParseUtil.parseFloat(values[i])); } } }
json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 { "detail":{ "xtitle":["时段1","时段2","时段3","时段4","时段5"], "datas":[ { "title":"a产能", "value":[5,9,13,8,22,3] },{ "title":"b产能", "value":[15,4,8,7,44] },{ "title":"平均", "type":1, "value":[35,39,3,83,6] } ] }, "title": "时段产能", "type": "splinechart", "x": 0, "y": 2 }