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?

yes. I used a vector2 array.

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

1 Answer

1

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

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, 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!

This works great guys! Thanks so much