1. Home
  2. Docs
  3. Graph Chart
  4. Filling the graph chart with data

Filling the graph chart with data

This articles describes the methods that manipulate the data on the graph chart. You can also see the Graph Chart Quick Start Tutorial

Clearing A category before appending data

In many cases you would like to clear a category from all it’s previous data before appending new data. You can do that in the following way:

Graph.DataSource.ClearCategory("Category");

How to add data to the graph chart

You can add data to the graph chart using one of the following methods : AddPointToCategory , AddPointToCategoryRealtime.

  • AddPointToCategory should be used when loading large amounts of data at once
  • AddPointToCategoryRealtime should be used when appending a point to a streaming chart

Here is a quick use case example of each of these methods:

AddPointToCategory("category",x, y);
float animationTime = 1f; // this will slide the point nicely when it is appended to the chart
AddPointToCategoryRealtime("category",x, y,animationTime );

To understand more , let’s take a look at the code in Tutorials/Stream Graph/Streaming Graph.cs

The streaming graph tutorial first loads several points at once to the chart , after that it appends streaming points to it.

void Start()
{

...
// clear the "Player 1" category. this category is defined using the GraphChart inspector
        Graph.DataSource.ClearCategory("Player 1"); 
// clear the "Player 2" category. this category is defined using the GraphChart inspector
        Graph.DataSource.ClearCategory("Player 2"); 

        for (int i = 0; i < TotalPoints; i++)  //add random points to the graph
        {
            Graph.DataSource.AddPointToCategory("Player 1", System.DateTime.Now - System.TimeSpan.FromSeconds(x), Random.value * 20f + 10f); 
            Graph.DataSource.AddPointToCategory("Player 2", System.DateTime.Now  - System.TimeSpan.FromSeconds(x), Random.value * 10f);
            x -= Random.value * 3f;
            lastX = x;
        }

...

}

As you can see , first we clear both categories using Graph.DataSource.ClearCategory. After that we add a group of points using AddPointToCategory .

After the inital set of points is loaded , the update method adds streaming points to the graph :

        float time = Time.time;
        if (lastTime + 2f < time)
        {
            lastTime = time;
            lastX += Random.value * 3f;
//            System.DateTime t = ChartDateUtility.ValueToDate(lastX);
            Graph.DataSource.AddPointToCategoryRealtime("Player 1", System.DateTime.Now, Random.value * 20f + 10f, 1f); 
            Graph.DataSource.AddPointToCategoryRealtime("Player 2", System.DateTime.Now, Random.value * 10f, 1f); 
        }

You can also notice that the x and y components of these methods can be either double or DateTime.

Use case examples

Let’s say you have an sql data table and would like to apply it to the graph chart . Here is a possible way you go about it:

    SqlDataReader reader = command.ExecuteReader();
        var graphChart = GetComponent<GraphChartBase>();
        while (reader.Read())
        {
            double x = reader.GetDouble(0);
            double y = reader.GetDouble(1);
            graphChart.DataSource.AddPointToCategory("MyCategory", x, y);
        }
 

As you can see we are making using use ADO .NET in order to bring data from an sql table. After that we simply apply it to the graph chart using AddPointToCategory.

Another example would be streaming data from a remote network interface. Here is an example of how something like this can be done. In the following example we use a NetworkStream the has been created and is connected to a remote source. It is assumed in this case that the remote application fills the buffer with points defined using two doubles (x and y).

    int bufferOffset = 0;
    // create a buffer that can hold one point(two doubles)
    byte[] buffer = new byte[sizeof(double) * 2]; 
    private void Update()
    {
        // we read from the stream until the buffer fills. 
        // notice that we never read past the length of the buffer
        bufferOffset += netwrokStream.Read(buffer, bufferOffset, buffer.Length - bufferOffset);
        if(bufferOffset == buffer.Length) // if we have enough data in the buffer
        {
            // we can extract the point values from the buffer
            double x = BitConverter.ToDouble(buffer, 0); 
            double y = BitConverter.ToDouble(buffer, sizeof(double);
            GraphChartBase graph = GetComponent<GraphChartBase>();
            // apply the point to the graph chart
            graph.DataSource.AddPointToCategoryRealtime("Category", x, y, 0.2f);
            bufferOffset = 0;
        }
    }

Further read