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);
}
}