Using the Charts Library > Data Handling > Modifying Data and Updating Charts > Updating Charts Automatically
 
Updating Charts Automatically
Charts are automatically updated when modifications are made to the data they display. This automatic update is performed by listeners that are set on the data.
Notification Mechanism Based on Listeners
A mechanism based on the use of listeners has been implemented to propagate automatically all modifications made to the data to the objects using these data. A listener is a dedicated object that catches the modifications made to the object on which the listener is set and then notifies other objects about these modifications.
Modifications can be made to data at the level of the data sets and at the level of the chart data objects (see Types of Modifications). Therefore, listeners are also set at the level of the data sets and at the level of the chart data objects. Figure 6.2 illustrates how listeners are set at the data set level and the chart data object level.
Figure 6.2    Listeners at the Data-Set Level and at the Chart-Data-Object Level
Listeners are set on each data set to notify the chart data objects that use the data set about the modifications made to the data set. Notice that two listeners are set on the data set Data set 2 since this data set is used by two chart data objects, Chart data 0 and Chart data 1. These listeners are automatically set on the data sets when the data sets are set on the chart data objects.
In the same way, listeners are set on each chart data object to notify the chart objects that use the chart data object about the modifications made to the chart data object. Notice that two listeners are set on the chart data object Chart data 1 since this chart data object is used by two chart objects, Chart 1 and Chart 2. These listeners are automatically set on the chart data objects when the chart data objects are set on the chart objects.
How the Update is Performed
By default, the listeners are enabled for all data sets. This means that listeners will be used to propagate the modifications made to the data sets. If the listeners are not enabled, they will not be considered and thus the modifications will not be transmitted to the listeners.
The method areListenersEnabled indicates whether the listeners are enabled for a given data set. The method enableListeners is used to specify whether the listeners should be enabled for a given data set.
Figure 6.3 shows how the listeners propagate a modification made to a data set to all the objects using the data set, thus allowing these objects to be updated. This figure uses the data set Data set 2 from Figure 6.2 as an example.
Figure 6.3    Propagation of a Modification Made to a Data Set
If the listeners are enabled, the following events occur as soon as a modification is made to a data set:
1. First, the modification is propagated to all the listeners set on the data set.
2. Each data set listener then notifies each chart data object with which it is associated about the modification.
3. The modification is then propagated to all the listeners set on the chart data objects.
4. Each chart data listener then notifies each chart object with which it is associated about the modification.
5. The charts are finally updated to reflect the modification.
If the listeners are not enabled, the updates needed because of a modification made to a data set will not be performed automatically since the modifications will not be transmitted to the listeners. The updates will have to be made by hand.
Batching the Modifications
By default, the notification of a modification and the update to the chart resulting from the modification is made as soon as the modification occurs. However, if it is not necessary to see the updates one by one as they occur, you can batch modifications when adding new data items to a data set. Using a batch operation when adding new data items increases performance and can be very useful, especially when adding real-time data.
To trigger the batch operations for adding new data items on a given data set, you have to call the startBatch method on the data set. Once the startBatch method has been called, the updates resulting from the addition of new data items are no longer performed. You have to call the endBatch method on the data set to specify that you want to stop the batch operations and perform the global update one time.
The startBatch method can be called several times in succession on a given data set. When this is done, the endBatch method has to be called as many times as the startBatch method. When the last call to the endBatch method is performed, all the data items that have been added since the first call of the startBatch method will be considered all at one time. The update resulting from the addition of all these data items will be performed at one time. The added data items will be drawn all at one time, instead of being drawn one by one as is done when the modifications are not batched.

Version 5.6
Copyright © 2012, Rogue Wave Software, Inc. All Rights Reserved.