I am using vectrosity to plot a graph whenever a new data comes in. I add in the X and Y coordinates into an array and use VectorLine.Resize to update the lines.
However, it does not work like how I want as I want the graph to move to the left every time a new data comes in.
How can I achieve this? Any advice will be appreciated, thank you.
If you use a circular buffer, you would just need to add the new points to the buffer and scroll the line object to the opposite direction. I made a quick example:
using UnityEngine;
using System.Collections;
using Vectrosity;
public class Graph : MonoBehaviour
{
//the buffer contains 100 points
private CircularBuffer<Vector3> buffer = new CircularBuffer<Vector3>(100);
private VectorLine line;
private Vector3 point;
private float x = -5f;
private float increment = .1f;
void Start()
{
//initial points
for (int i = 0; i < buffer.Count; i++)
{
x += increment;
point = new Vector3(x, Mathf.Sin(x));
buffer.Add(point);
}
line = new VectorLine("MyLine", buffer.ToArray(), Color.red, null, 2.0f, LineType.Continuous);
line.Draw3DAuto();
}
void FixedUpdate()
{
x += increment;
//add the points to the buffer (old points get dequeued)
point = new Vector3(x, Mathf.Sin(x) * Random.Range(1, 1.2f));
buffer.Add(point);
//move the line object
Vector3 pos = line.vectorObject.transform.position;
pos.x -= increment;
line.vectorObject.transform.position = pos;
//update the current points
line.points3 = buffer.ToArray();
}
}
This is the circular buffer class:
using System;
using System.Collections;
using System.Collections.Generic;
public class CircularBuffer<T>
{
private Queue<T> queue;
private int count;
public CircularBuffer(int count)
{
queue = new Queue<T>(count);
this.count = count;
}
public void Add(T obj)
{
if (queue.Count == count)
{
queue.Dequeue();
queue.Enqueue(obj);
}
else
queue.Enqueue(obj);
}
public T Read()
{
return queue.Dequeue();
}
public T Peek()
{
return queue.Peek();
}
public T[] ToArray()
{
return queue.ToArray();
}
public int Count
{
get
{
return count;
}
}
}