PowerPlotPercolation logo

Example — Interactive scientific plot

This is an interactive version of the scientific data example. The user can scroll and zoom and it is shown how to react to the user selecting data poitns.

This page describes the class “FormFactorInteractive” in the PowerPlot demo app.

Interactive scientific plot

The first part – creating the chart – is the same as before. Then the scrolling and zooming behavior is set up and activated:

    // Now configure the zooming and scrolling behavior.
    [formFactorChart setAllAxisPreserveOnChangeX:kPreserveRelative];
    [formFactorChart setAllAxisPreserveOnChangeY:kPreserveRelative];
    WSPlotController *aCtrl = [formFactorChart lastPlot];
    [aCtrl setMaximumZoomScaleXD:2.
    NAFloat scaleX = NARangeLen(aCtrl.zoomRangeXD);
    NAFloat scaleY = NARangeLen(aCtrl.zoomRangeYD);
    aCtrl.zoomRangeXD = NARangeMake(scaleX/3, scaleX*1.25);
    aCtrl.zoomRangeYD = NARangeMake(scaleY/3, scaleY*1.25);
    aCtrl.zoomEnabled = YES;
    aCtrl.scrollEnabled = YES;

The allAxisPreserveOnChange[X|Y]: methods have discussed in the animation cascade example before. They ensure that the coordinate axis stay at the same positions in the window, even if the device is rotated or the view is zoomed or scrolled.

The method for setting the zoom scales will set both the zoom and the scroll ranges. If the user is allowed to scroll a larger region than she can maximally zoom out to, the ranges need to be adjusted. This is what the next four lines do. The final two lines will activate the standard handling of both zooming and scrolling.

In the following we also set up a handler for responding to single taps on data points. In this case we configure the plot controller to call the controller:singleTapAtDatum: in the view controller with the datum identified. Hit testing is done by taking the smallest distance in the X-Y plane, see also the interactive bar chart for a discussion of this topic.

    // Configure the single tap feature.
    aCtrl.tapEnabled = YES;
    aCtrl.delegate = self;
    aCtrl.hitTestMethod = (kHitTestX | kHitTestY);
    aCtrl.hitResponseMethod = kHitResponseDatum;

Finally, we need to implement the call back once the user has tapped on the screen:

#pragma mark - WSControllerGestureDelegate

- (void)controller:(WSPlotController *)controller
  singleTapAtDatum:(NSInteger)i {
    NSLog(@"Datum hit: %d.", i);

In this example the callback will write a log message with the number of the datum that the user has selected. It is easy to extend this to a custom highlighting of the data point or to respond differently to the selection by extending the controller:singleTapAtDatum: method.