Updating chart data or properties
Every property or series data value change will cause the LightningChart control to be redrawn. Every redraw will cause CPU and display adapter overhead. If more than one property is programmatically changed at the same time, the property changes should be made between BeginUpdate() and EndUpdate() method calls, as a batch. BeginUpdate() will stop drawing the control until EndUpdate() is called. There is an internal counter for pending BeginUpdate() calls, and when an equal amount of EndUpdate() calls have been reached, EndUpdate() redraws the control. The following example demonstrates how to update chart with minimal load to the computer.
chart.BeginUpdate(); //Disable redraws
//Add data to series
chart.ViewXY.SampleDataSeries[0].AddSamples(multiChannelSampleStream[0], false);
chart.ViewXY.SampleDataSeries[1].AddSamples(multiChannelSampleStream[1], false);
chart.ViewXY.SampleDataSeries[2].AddSamples(multiChannelSampleStream[2], false);
//Update point counter bar
chart.ViewXY.BarSeries[0].SetValue(0,1,(double)totalPointsCollected,””, false);
// Point counter label
chart.Title.Text = totalPointsCollected.ToString();
// Set monitoring scroll position to latest x
newestX = firstSampleTimeStamp + (double)(pointsLen - 1) / genSampFreq;
chart.ViewXY.XAxes[0].ScrollPosition = newestX;
chart.EndUpdate(); // Enable redraws and redraw
The internal counter allows using nesting updates as follows:
void MainMethod()
{
chart.BeginUpdate();
chart.Title.Text = “My title”; chart.ViewXY.XAxes[0].AxisColor = Colors.Red;
UpdateSeriesColors();
chart.EndUpdate();
// Repaints only once.
}
private void buttonCreate_Click(object sender, EventArgs e)
{
UpdateSeriesColors(); // Repaints only once
}
void UpdateSeriesColors()
{
chart.BeginUpdate();
foreach(PointLineSeries series in chart.ViewXY.PointLineSeries)
{
series.LineStyle.Color = Color.Yellow;
}
chart.EndUpdate();
}
Updating series data depends on how the data is stored. For array series, InvalidateData() method has to be called after updating the array contents, otherwise the UI doesn’t get notified about the changes.
ObservableCollection, useful for data binding in WPF bindable chart, will update itself with every point or point's field because of automatic notifications. Thus, InvalidateData() is not needed. However, ObservableCollections cause slightly reduced performance compared to arrays or lists, which is noticeable especially when having a large amount of data points (see also here).