Scripting a Chart Series
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.
- Configure a chart, add a series, and for the Data source property select Script as the source.
- Switch to the Script View and enable scripting for this screen.
- In the script for the screen, add a
GetCustomSeriesDatamethod below the//add custom methods herecomment in thepublic class ScreenClassclass.
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;
}
- The above method has a parameter with a data type of 'Series', which is included in the
Canvas.Shared.Modelsnamespace. So, at the top of the script you need to add a statement to indicate this:
Copy
using Canvas.Shared.Models;
- 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.
- 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
- The AddData method on a series object is used to add a value at the supplied time. AddData has three parameters:
- DateTime — Type of "DateTime"
- Value — Type of "Double"
- Color — Type of "String". This is an optional parameter. See the next note about bar charts and color.
Here is an example of adding a point to the chart.
Copy
AddData(DateTime.Now, 150)
- When adding a Bar Series type, you must set the color at the point level. This allows for points to be colored differently from each other. Color is an optional parameter on the AddData method; but it must be supplied for a bar chart.
Copy
AddData(DateTime.Now, 150, "Blue")
- When adding a scripted data source, set the Delay loading property to True to give the script time to set the delegates before data retrieval begins. The chart needs to be refreshed after the delegates are connected. See example above.
See Configuring a Chart for more general information.
