1. Home
  2. Docs
  3. Quick Start Tutorials
  4. Streaming Graph Chart

Streaming Graph Chart

scroll down for the written tutorial

The resulting scene and script from this tutorial can be found at Assets\Chart and Graph\Demos\Tutorials\Stream Graph

Start by setting up a graph chart in a similar way to the previous tutorial.

In order to add real time points , use the method AddPointToCategoryRealtime instead of AddPointToCategory. Realtime points can only be added at the end of the current data (their x value must be higher then previous data points). Add the following script to your graph feed object :

Note:Do not use StartBatch And EndBatch to batch realtime calls , this will only slow things down.

public class StreamingGraph : MonoBehaviour
{

    public GraphChart Graph;
    public int TotalPoints = 5;
    float lastTime = 0f;
    float lastX = 0f;

    void Start()
    {
        if (Graph == null) // the ChartGraph info is obtained via the inspector
            return;
        float x = 0f;
     /////   Graph.DataSource.StartBatch(); // do not call StartBatch for realtime calls , it will only slow down performance.
 
        Graph.DataSource.ClearCategory("Player 1"); // clear the "Player 1" category. this category is defined using the GraphChart inspector
        Graph.DataSource.ClearCategory("Player 2"); // clear the "Player 2" category. this category is defined using the GraphChart inspector

        for (int i = 0; i < TotalPoints; i++)  //add random points to the graph
        {
            Graph.DataSource.AddPointToCategoryRealtime("Player 1", x, Random.value * 20f + 10f); // each time we call AddPointToCategory 
            Graph.DataSource.AddPointToCategoryRealtime("Player 2", x, Random.value * 10f); // each time we call AddPointToCategory 
            x += Random.value * 3f;
            lastX = x;

        }
      ////  Graph.DataSource.EndBatch(); // do not batch reatlime calls
}

    void Update()
    {
        float time = Time.time;
        if (lastTime + 2f < time)
        {
            lastTime = time;
            lastX += Random.value * 3f;
            Graph.DataSource.AddPointToCategoryRealtime("Player 1", lastX, Random.value * 20f + 10f, 1f); // each time we call AddPointToCategory 
            Graph.DataSource.AddPointToCategoryRealtime("Player 2", lastX, Random.value * 10f, 1f); // each time we call AddPointToCategory
        }

    }

This code adds a few points in the Start method , and then times realtime point adds in the Update method. Notice that you can specify animation time to AddPointToCategoryRealtime calls .

Learn more about the Graph Chart