美文网首页
【原创】unity xchart bar,ring ,pie等预

【原创】unity xchart bar,ring ,pie等预

作者: 吉凶以情迁 | 来源:发表于2023-02-22 17:09 被阅读0次
    
    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<bar>即可实现

     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

    {
    
                "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
            }
    

    相关文章

      网友评论

          本文标题:【原创】unity xchart bar,ring ,pie等预

          本文链接:https://www.haomeiwen.com/subject/kdhewrtx.html