- Home /
Moving graph using vectrosity
Hi developers,
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.
Do you use a circular buffer or queue to store/add the points?
I have 2 array. 1 is to store the X,Y coordinates in float[]coordinatePoints another is to store the line points in Vector2[] linePoints
Answer by robhuhn · Dec 12, 2013 at 11:56 AM
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;
}
}
}
Yes this is just an example-sin-based-function
point = new Vector3(x, $$anonymous$$athf.Sin(x) * Random.Range(1, 1.2f));
You would store your coordinates here.
I wonder why your position seems to be increasing because if the object and the line are in 3d space, the increment will move it to the left at the same distance when the new point is added. But a little change would move the points without the need to move the transformation e.g.:
Vector3[] bufferArray = buffer.ToArray();
for (int i = 0; i < bufferArray.Length; i++)
{
bufferArray[i].x = i * increment + yourOffsetX;
}
That will set the x value of all points to a new x position based on its index which will work because of the circular buffer. The movement of the object can be deleted then.
Wow you are great!! Thank you for your help! It is really useful :) I would like to check, if I want to plot the x coordinate according to every 60 seconds, and y coordinate based on the sensor data, can I just replace with point = new Vector3(xCoordinate, yCoordinate)? Sorry for this noob question.
Yes but also move all the code from FixedUpdate to a method which is only called every 60 seconds.
Hi,
I changed my code something like:
void Start(){
for (int i = 0; i < buffer.Count; i++) { x += increment;
point = new Vector3(0, 0);
buffer.Add(point);
}
line = new VectorLine("line", buffer.ToArray(), Color.red, null, 2.0f, LineType.Continuous);
line.Draw3DAuto();
}
void FixedUpdate()
{
x += increment;
//x +=1;
//add the points to the buffer (old points get dequeued)
point = new Vector3(x,0);
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();
}
but why is my straight line not always moving? It stop moving after a few seconds.. Pls advise, thank you
Hi, will I be able to do the moving of the graph for vector2? Because I tried to apply the code for my vector2, but it doesnt seem workable. Pls advise, thank you!
Your answer
Follow this Question
Related Questions
Vectrosity: Clear vector2 array points 1 Answer
vectrosity: Draw lines with value of float 0 Answers
Vectrosity Vector2 Plot real time(moving) graph 1 Answer
Vectrosity - destory line 1 Answer
Different views for graph plotting 0 Answers