Unity3d Chart and Graph插件使用 (2D曲线图,饼图,条形图)

8 篇文章 2 订阅

1.基本使用

创建Canvas-->菜单栏快速创建Graph

2.创建完毕后找到Graph Chart脚本 里data,根据实际情况创建几个曲线

3.如果自定义垂直和水平的值,就不要点击Auto,

  graph = GetComponent<GraphChartBase>();
        graph.DataSource.AutomaticVerticallView = false;
        graph.DataSource.VerticalViewOrigin = 0;
        graph.DataSource.VerticalViewSize = 3000;

4.调整垂直和水平的刻度

5 如果感觉点击节点是显示的文字太小或者重写格式,可以找到LtemLabels脚本

6.测试

using UnityEngine;
using ChartAndGraph;
using System.Collections.Generic;
using System;
public class GraphBaseMainMuen : MonoBehaviour, IComparer<DoubleVector2>
{
    protected List<DoubleVector2> mData = new List<DoubleVector2>();
    protected double pageSize = 50f;
    protected double currentPagePosition = 0.0;
    protected GraphChartBase graph;
    protected VerticalAxis axis;
  

    // Use this for initialization

    protected virtual void Awake()
    {
        graph = GetComponent<GraphChartBase>();
       
    }
   
    // 如果您想知道当前显示的索引是什么。使用二分查找来找到它
    int FindClosestIndex(double position)
    {
        //NOTE :: 此方法假定您的数据已排序 !!!
        int res = mData.BinarySearch(new DoubleVector2(position, 0.0), this);
        if (res >= 0)
            return res;
        return ~res;
    }
    // 给定页面位置,在该页面的数据中找到最右和最左的索引
    void findPointsForPage(double position, out int start, out int end)
    {


        int index = FindClosestIndex(position);
        int i = index;
        double endPosition = position + pageSize;
        double startPosition = position - pageSize;
        //从当前索引开始,我们发现页面边界
        for (start = index; start > 0; start--)
        {
            // 把纸上的第一点拿出来。所以曲线在这条边不会断裂
            if (mData[i].x < startPosition)
                break;
        }
        for (end = index; end < mData.Count; end++)
        {
            if (mData[i].x > endPosition) // 把纸上的第一点拿出来
                break;
        }
    }
    protected string named;

    protected virtual void Update()
    {
        if (graph != null)
        {
            //检查图形的滚动位置。如果我们超过了视图大小,则loada新页面
            double pageStartThreshold = currentPagePosition - pageSize;
            double pageEndThreshold = currentPagePosition + pageSize -
            graph.DataSource.HorizontalViewSize;
            if (graph.HorizontalScrolling < pageStartThreshold || graph.HorizontalScrolling > pageEndThreshold)
            {
                LoadPage(graph.HorizontalScrolling,named);
            }
        }
    }
    protected void LoadPage(double pagePosition,string name)
    {
        if (graph != null)
        {
           // Debug.Log("Loading page :" + pagePosition);
            graph.DataSource.StartBatch(); // call start batch
            graph.DataSource.HorizontalViewOrigin = 0;
            int start, end;
            findPointsForPage(pagePosition, out start, out end); // get the page edges
            graph.DataSource.ClearCategory("Player 1"); // clear the cateogry
            for (int i = start; i < end; i++) // load the data
                graph.DataSource.AddPointToCategory(name, mData[i].x, mData[i].y);
            graph.DataSource.EndBatch();
            graph.HorizontalScrolling = pagePosition;
        }
        currentPagePosition = pagePosition;
    }
    public int Compare(DoubleVector2 x, DoubleVector2 y)
    {
        if (x.x < y.x)
            return -1;
        if (x.x < y.x)
            return 1;
        return 0;
    }
}
using ChartAndGraph;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using UnityEngine.UI;

public class Electro : GraphBaseMainMuen
{
    public static string Url= "/energy/rest/MnitorData/?retry=5&retryDelay=500&time__range=";
    public static string Arge = "&type=hour&pagesize=9999&order=time&EnergyCategory=5";
    protected static float NowTimeGross;
    protected static float OldTimeGross;
    protected override void Awake()
    {
        base.Awake();

        StartCoroutine(Energewww(NowTime + "," + NextTime, "Player 2"));
        StartCoroutine( ConstantUtils.B_invoke(() => { StartCoroutine(Energewww(OldTime + "," + NowTime, "Player 1")); },0.5f));
    }

    // Update is called once per frame
    protected override void Update () {
        base.Update();
	}
    IEnumerator Energewww(string time,string name)
    {
       
        WWW www = new WWW(ConstantUtils.ServerUrl+ Url + time + Arge);
        yield return www;
        if(www.isDone&&string.IsNullOrEmpty(www.error))
        {
            JsonData data = JsonMapper.ToObject(www.text);
            int count = int.Parse(data["count"].ToString());

            for (int i = 0; i < count; i++)
            {
                mData.Add(new DoubleVector2(i, float.Parse( data["results"][i]["D_value"].ToString())));
                if (name == "Player 2")
                    NowTimeGross += float.Parse(data["results"][i]["D_value"].ToString());
                else
                    OldTimeGross += float.Parse(data["results"][i]["D_value"].ToString());
            }
            LoadPage(currentPagePosition, name);
            if(name== "Player 2")
            {
                ConstantUtils.GetOrCreat<Text>("CanvasFirst/运维页面/ElectroBG/BG/今日用电量/Text").text = String.Format("{0:F}", NowTimeGross);
            }
            else
            {
                Debug.Log(Rate(NowTimeGross, OldTimeGross));
            }
            
        }
    }
}

7.上测试图,目前还可以

饼图

public class PieBase : MonoBehaviour {
    protected BillboardText oldtext;
    protected double value;
    protected CanvasPieChart pieChart;
    [HideInInspector]
    public bool isActive;
    // Use this for initialization
    public virtual void Start () {
        pieChart = GetComponent<CanvasPieChart>();
        if (isActive)
        {
            pieChart.PieHovered.AddListener(Active);
            pieChart.NonHovered.AddListener(hide);
        }
        
	}

    private void hide()
    {
        oldtext.UIText.GetComponent<Text>().text = value.ToString();
    }

    private void Active(PieChart.PieEventArgs arg0)
    {
      
      foreach (var i in pieChart.mPies)
        {
           if(i.Key== arg0.Category)
            {
                oldtext = i.Value.ItemLabel;
                value = arg0.Value;
                i.Value.ItemLabel.UIText.GetComponent<Text>().text = arg0.Category+"\n" + value;
            }
        }
    }

    // Update is called once per frame
    void Update () {
		
	}
}

条形图

使用叠加模式需要点上CanvasBarChart脚本中stacked参数

using ChartAndGraph;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BarBase : MonoBehaviour {
    public BarChart barChart;
    // Use this for initialization
   public virtual void Start () {
        barChart = GetComponent<BarChart>();
       
    }
   
	// Update is called once per frame
	void Update () {
		
	}
    public void BasebarInfo(string category,Material material,float value, string Group = "All" )
    {
        barChart.DataSource.StartBatch();
        barChart.DataSource.AddCategory(category, material);
    
        barChart.DataSource.SetValue(category, Group, value);
        //barChart.DataSource.SlideValue(category, Group, value, 4f);
        barChart.DataSource.EndBatch();
    }
    public void Setval(string category, float value, string Group = "All")
    {
        barChart.DataSource.SetValue(category, Group, value);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BarMain : BarBase
{
    public List<double> paix = new List<double>();
    public override void Start()
    {
        base.Start();

        for (int i = 0; i < 4; i++)
        {

            for (int j = 0; j < 3; j++)
            {
                if (j == 0)
                {
                    int x = Random.Range(30, 50);
                    paix.Add(x);
                    barChart.DataSource.SetValue(barChart.DataSource.GetCategoryName(j), barChart.DataSource.GetGroupName(i), x);
                }
                else
                {
                    double x=  Random.Range(10, 40);
                    paix.Add(x);
                    barChart.DataSource.SetValue(barChart.DataSource.GetCategoryName(j), barChart.DataSource.GetGroupName(i), barChart.DataSource.GetValue(barChart.DataSource.GetCategoryName(j - 1), barChart.DataSource.GetGroupName(i)) +x);
                   
                }
            }



            //  barChart.DataSource.SetCategoryIndex(i + "1标", i);
        }

        barChart.isok = true;
       
    }
    public void M_ItemRotate()
    {
        int c = 0;
        if (barChart.mBars == null)
            return;
        foreach (var i in barChart.mBars)
        {
           
          i.Value.ItemLabel.UIText.GetComponent<Text>().text= paix[c].ToString();
            c++;
        }
        Transform ds = transform.Find("New Game Object/textController");
      
        for (int i=0;i< ds.childCount;i++)
        {
           ds.GetChild(i).localEulerAngles = new Vector3(0, 0, 90);
        }
    }

 

  • 5
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
Graph and Chart 1.81是Unity3D的一款图表插件,它提供了丰富的数据可视化功能和自定义选项。这个插件可以帮助开发者在游戏或应用程序中轻松创建各种类型的图表,以展示和分析数据。 Graph and Chart 1.81支持多种类型的图表,包括线形图、柱状图、饼图、雷达图和面积图等。开发者可以根据自己的需求选择合适的图表类型,并通过简单易用的编辑器工具来配置图表的样式和属性。例如,可以设置坐标轴的标签、标题和颜色,调整图表的大小和位置,甚至可以自定义图表中数据点的样式和动画效果。 该插件还支持将数据通过多种格式导入图表中,包括数组、CSV文件甚至数据库。这使得开发者可以方便地将实时数据或历史数据应用到图表中,以便更好地展示和比较数据。同时,插件还提供了丰富的数据处理功能,如排序、聚合和过滤等,以方便开发者在图表中进行数据分析和呈现。 除了基础的图表功能外,Graph and Chart 1.81还提供了一些高级功能,如交互式图表和动态更新。通过使用这些功能,开发者可以实现用户与图表的交互,例如点击柱状图获取详细信息,或者动态地更新图表以展示实时数据的变化。 总之,Graph and Chart 1.81是一款非常实用的图表插件,它为Unity3D开发者提供了强大的数据可视化工具。无论是在游戏开发中还是应用程序开发中,使用插件可以轻松创建出美观、交互性强的图表,从而更好地展示和分析数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值