Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by thling90 · Dec 12, 2013 at 03:10 AM · c#vectrosity

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robhuhn · Dec 12, 2013 at 08:43 AM 0
Share

Do you use a circular buffer or queue to store/add the points?

avatar image thling90 · Dec 12, 2013 at 09:22 AM 0
Share

yes. I used a vector2 array.

avatar image thling90 · Dec 12, 2013 at 09:24 AM 0
Share

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 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
         }
     }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robhuhn · Dec 13, 2013 at 01:35 PM 0
Share

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.

avatar image thling90 · Dec 13, 2013 at 02:27 PM 0
Share

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.

avatar image robhuhn · Dec 13, 2013 at 02:31 PM 0
Share

Yes but also move all the code from FixedUpdate to a method which is only called every 60 seconds.

avatar image thling90 · Dec 14, 2013 at 01:36 AM 0
Share

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

avatar image thling90 · Dec 16, 2013 at 05:22 AM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges