Scripting a Chart Series

Scripting is not supported by TWC

The following example guides you though creating a scripted chart series, adding delegates to show random data and auto update the series, and some important notes.

  1. Configure a chart, add a series, and for the Data source property select Script as the source.
  2. Switch to the Script View and enable scripting for this screen.
  3. In the script for the screen, add a GetCustomSeriesData method below the //add custom methods here comment in the public class ScreenClass class.

See the following example. This example also includes a GetCustomSeriesAutoUpdateData method to handle an auto updating series. Also see the comments showing the ability to handle multiple scripted series.

Copy
Scripting a Chart Series
private bool GetCustomSeriesData(Series series)
{
    var date = Objects.Chart1.DateStart;
    Random rand = new Random();
    
    //if there are multiple scripted series, use the series name to distinguish between them
    if(series.Name == "Custom 1")
    {
        //simple loop to populate series with random value
        while (date <= Objects.Chart1.DateEnd)
        {
            //create random number
            double value = rand.NextDouble() * 100;
            
            //add to series
            //if the series is a bar type, the color needs to be supplied in AddData
            series.AddData(date, value);
            
            //increment the date
            date += TimeSpan.FromSeconds(60);
        }
    }

    if(series.Name == "Custom 2")
    {
        //simple loop to populate series with random value
        while (date <= Objects.Chart1.DateEnd)
        {
            //create random number
            double value = rand.NextDouble() + 1000;
            
            //add to series
            //if the series is a bar type, the color needs to be supplied in AddData
            series.AddData(date, value);
            
            //increment the date
            date += TimeSpan.FromSeconds(60);
        }
    }
    return true;
}

public bool GetCustomSeriesAutoUpdateData(Series series, DateTime start, DateTime end)
{
    //start - the datetime at the beginning of the update interval
    //end - the datetime at the end of the update interval
    
    //insert custom series auto update code here
    
    return true;
}
  1. The above method has a parameter with a data type of 'Series', which is included in the Canvas.Shared.Models namespace. So, at the top of the script you need to add a statement to indicate this:
Copy
using Canvas.Shared.Models;
  1. Next you need to add the delegates, which are used, for example, to reference the custom scripted data or instruct the series to auto update.
  2. Go to the Events tab and enable the Initialize event on the screen. This is where you hook the delegates:
Copy
Hook the delegates
private void Screen_Initialize(object sender, EventArgs args)
{
 
    //hook up a GetCustomSeriesData delegate on the chart for the scripted series
    Objects.Chart1.GetCustomSeriesData = GetCustomSeriesData;
     
    //hook up a GetCustomSeriesAutoUpdateData delegate on the chart for the scripted series
    Objects.Chart1.GetCustomSeriesAutoUpdateData = GetCustomSeriesAutoUpdateData;
     
    //after the delegates are connected, the chart data needs to be refreshed
    Objects.Chart1.RefreshChart(true);
 
}

Other Script Notes

Here is an example of adding a point to the chart.

Copy
AddData(DateTime.Now, 150)
Copy
AddData(DateTime.Now, 150, "Blue")

See Configuring a Chart for more general information.