Charts > Using the Charts Library > Using Charts to Display Real-Time Data > Improving Performance When Adding Data Points to a Chart
 
Improving Performance When Adding Data Points to a Chart
By default, each time a data point is added to a data set, the chart is updated immediately. The area of the chart where the graphical representation of the new data point should be displayed is first invalidated and then redrawn by the holder of the chart. The holder is an instance of the IlvGraphicHolder class and can be retrieved for a given graphic object by the getHolder method.
If you look at our example, the redrawing of the chart that is required by adding the data points will be performed three times (one time for each data set that is modified) since we add a data point to three data sets at each period of the timer.
There are several methods that you can use to improve performance when adding data points to a chart:
*Use the initReDraws and reDrawViews methods of the holder of the chart.
This allows you to perform the redraw operation for all the data sets at one time instead of performing the redraw operation for each data set to which a data point is added.
*Use the fast scroll mode of the chart.
This allows you to draw the graphical representation of the newly added data point directly in the drawing port without going through the holder of the chart.
*Batch the modifications when adding new data points to a data set.
*Use an IlvChartCyclicPointSet data set.
Note: Batching modifications can be used alone or can be used in conjunction with the use of the fast scroll mode.
Using initReDraws and reDrawViews Methods
The initReDraws and reDrawViews methods allow you to specify that the redraw operation required by adding data points should be performed only one time. To use these methods, you need to do the following:
1. Add a call to the initReDraws method of the chart holder before the loop that adds a data point to each data set.
2. Add a call to the reDrawViews method of the chart holder after the loop that adds a data point to each data set.
In our scrolling example, each time a data point is added to a data set, the area of the chart that must be redrawn is simply invalidated and the redrawing required for the newly added data points for a given period of the timer will be performed one time when the reDrawViews method is called.
In our example, the method that is called at each period of the timer is now:
static void
AddPoints(IlvTimer* timer, IlAny arg)
// Timer callback to add points.
{
IlvChartGraphic* chart = (IlvChartGraphic*)arg;
chart->getHolder()->initReDraws();
 
IlvDisplay* display = timer->getDisplay();
IlUInt count;
IlvDoublePoint previousPoint;
IlvDoublePoint nextPoint;
IlvChartDataSet* dataSet;
// Add one point on each dataSet.
for (IlvUInt i = 0; i < chart->getDataSetsCount(); i++) {
dataSet = chart->getDataSet(i);
count = dataSet->getDataCount();
// Get the previous data point to have a smooth random creation.
if (count != 0) {
dataSet->getPoint(count-1, previousPoint);
 
// Create a new data point.
GeneratePoint(previousPoint, nextPoint);
}
else {
nextPoint.x(0);
nextPoint.y(i+0.5);
}
 
// Add the data point to the data set.
dataSet->addPoint(nextPoint);
}
 
chart->getHolder()->reDrawViews();
}
The complete source code can be found in the scroll2.cpp file located in the $ILVHOME/samples/charts/userman/src directory.
Using the Fast Scroll Mode
Using the fast scroll mode is another way to improve performance. This is done by calling the method IlvChartGraphic::enableFastScroll with IlvTrue as a parameter. Each time a data point is added to a data set, the graphical representation of the newly added data point is drawn directly in the drawing port without going through the holder of the chart.
Although fast scroll mode speeds the drawing operations, it has several limitations:
*There must be no other graphic objects underneath the chart object.
*Using displayers that display filled polygonal representations, such as polygon and stair displayers, should be avoided since an additional vertical line is drawn for each polygonal representation of an added data point.
Note: The fast scroll mode can be used only with the IlvScrollModeShift scroll mode.
Batching Modifications
You can drastically improve performance by using a batch operation when adding new data points to a data set. This is especially useful when it is not necessary to see the updates resulting from adding the new data points one by one as they occur. To batch the modifications, do the following:
1. Add a call to the IlvChartDataSet::startBatch method on the data set before a set of additions of data points is performed on the data set.
Once the IlvChartDataSet::startBatch method has been called, the updates resulting from adding new data points are no longer performed.
2. Add a call to the IlvChartDataSet::endBatch method on the data set after a set of additions of data points has been performed on the data set.
When the IlvChartDataSet::endBatch method is called, all the data points that have been added since the call to the IlvChartDataSet::startBatch method are processed at one time. The update resulting from the addition of all these data points is performed at one time. The newly added data points are drawn all at one time, instead of being drawn one by one as is the case when the modifications are not batched.
In our example, the method that is called at each period of the timer is now:
static void
AddPoints(IlvTimer* timer, IlAny arg)
// Timer callback to add points.
{
IlvChartGraphic* chart = (IlvChartGraphic*)arg;
 
IlvDisplay* display = timer->getDisplay();
IlUInt count;
IlvDoublePoint previousPoint;
IlvDoublePoint nextPoint;
IlvChartDataSet* dataSet;
// Add one point on each dataSet.
for (IlUInt i = 0; i < chart->getDataSetsCount(); i++) {
dataSet = chart->getDataSet(i);
 
dataSet->startBatch();
 
count = dataSet->getDataCount();
for (IlUInt k = 0; k < NbAddedPts; k++, count++) {
// Get the previous data point to have a smooth random creation.
if (count != 0) {
dataSet->getPoint(count-1, previousPoint);
 
// Create a new data point.
GeneratePoint(previousPoint, nextPoint);
}
else {
nextPoint.x(0);
nextPoint.y(i+0.5);
}
 
// Add the data point to the data set.
dataSet->addPoint(nextPoint);
}
 
dataSet->endBatch();
}
}
The complete source code can be found in the scroll3.cpp file located in the $ILVHOME/samples/charts/userman/src directory. In this source code, we use both the fast scroll mode and the batching modifications method when adding new data points.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.