Handling Chart Data Source Events

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes a method of handling chart data source events. After starting the example the following operations are executed:

Required Files

It is required to add the following file to the base example Displaying of Express Report:

Source Code

Executing the example requires to create a chart data source delegate class, in which chart event handling is implemented, and also to add class header file into the source code of the ViewController class (see the Displaying of Express Report section). The it is required to place the following code in the body of the executeExample method of the ViewController class:

// Get array of data view controller
NSArray *controllers =[m_controller dataViewControllers];
// Parse data view controllers in cycle
for(NSObject *controller in controllers)
{
    // Check if the current controller is controller of chart data view
    if([controller isMemberOfClass:[MAChartDataViewController class]])
    {
        // Get chart data view controller object
        MAChartDataViewController *chartController = (MAChartDataViewController *)controller;
        // Get chart data source
        ChartDataViewDatasource *chart = [[chartController dataSource] datasource];
        // Get text description of data sample
        NSString *chartDataSelectionTitle = [chart dataSelectionTitle];
        // Get text description of data sample for specified time
        NSString *chartDataSelectionTitleWithTimeIndex = [chart dataSelectionTitleWithTimeaxisIndex:1];
        // Display obtained information in the development environment console
        NSLog(@"Data sample description: %@", chartDataSelectionTitle);
        NSLog(@"Data sample description corresponding to timeline position with the 1 index: %@", chartDataSelectionTitleWithTimeIndex);
        // Remove all chart data source subscribers
        [chart removeEventObservers];
        // Create an observer object of chart data source events
        DataViewDatasourceExample *exampleDelegate = [[DataViewDatasourceExample alloc] initWithName: @"chartDatasource"];
        // Subscribe observer object for data source events
        [chart addEventObserver:exampleDelegate];
        // Call data update event
        [chart pivotTableUpdated];
        // Call custom event
        [chart sendEvent:2];
        // Remove observer from the list of chart data source event observers
        [chart removeEventObserver: exampleDelegate];
        // Call custom event
        [chart sendEvent:2];
        break;
    }
}

After executing the example the development environment console displays information about data source, and also messages about chart data source events:

Data sample description: GDP per capita, PPP (constant 2005 international $)

Data sample description corresponding to timeline position with the 1 index: 1

chartDatasource: Data update event, event code: 1

chartDatasource: Custom event, event code: 2

 

The repeated call of the custom event does not result in executing observer object code because it was removed from the list of chart data source event observers by means of the DataViewDatasource.removeEventObserver: method. Identical result is obtained if the string is replaced in the source code:

[chart removeEventObserver: exampleDelegate];

with the following code:

[chart removeEventObservers];

See also:

Example of Component Use