Adding Level Lines

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example implements creating and displaying of chart level lines. The development environment console displays the following information on the first level line:

Clicking the chart area removes the first level line and changes the second line color, the repeated click removes all the lines.

Source Code

Executing the example requires to place the following code instead of the executeExample class of the ViewController method (see the Creating a Line Chart section):

// Function for example execution
-(void) executeExample{
    // Set maximum number of legend columns
    [[chart legend] setMaximumColumnCount:2];
    // Set order by maximum number of columns
    [[chart legend] setOrderByColumn:YES];
    // Determine level line
    LevelLineHolder *line1 = [[LevelLineHolder new] autorelease];
    [line1 setIsHorisontal:YES]; // Line position
    [line1 setColor:[UIColor orangeColor]]; // Line color
    [line1 setThickness:8]; // Line thickness
    [line1 setValue:9]; // Level line value
    [line1 setVisible:YES]; // Indicates whether line is visible
    [line1 setName:@"Line1"]; // Line name
    [line1 setLabelVisible:YES]; // Indicates whether line label is visible
    [line1 setChartView:chart]; // The chart, to which line belongs
    // Save line parameters
    NSDictionary *line1Dict = [line1 saveState];
    // Determine level line
    LevelLineHolder *line2 = [[LevelLineHolder new] autorelease];
    // Initialize level line by specified parameters
    [line2 initWithDict:line1Dict];
    [line2 setName: @"Line2"];
    [line2 setValue:5];
    [line2 setColor:[UIColor blueColor]];
    [line2 setChartView:chart];
    [line2 setIsHorisontal:NO];
    // Add created lines to chart
    [chart addLevelLine:line1];
    [chart addLevelLine:line2];
};
// Handle chart area touch event
- (void)chartTouchesTapInView:(UIView *)v withPoint:(CGPoint)point
{
    // Execute the following code if the number of chart level lines equals to 2
    if([chart levelLines].count == 2)
    {
        // Get the first chart level line
        LevelLineHolder *line1 = [chart levelLines][0];
        // Determine point with specified coordinates
        CGPoint p = CGPointMake(248, 158);
        // Display whether the specified point belongs to level line
        NSLog(@"%@ (%f, %f) level lines: %hhd", @"Indicates whether point with coordinates ",
              p.x, p.y, [line1 findLevelLineHit:p]);
        // Display level line position
        NSLog(@"%@ %f", @"Level line position on chart:", line1.position);
        // Display level line position
        NSLog(@"%@ %f", @"Absolute position of level line:", line1.x);
        // Display line type
        switch ([line1 isHorisontal]) {
            case YES:
                NSLog(@"Line type: horizontal");
                break;
            case NO:
                NSLog(@"Line type: vertical");
            default:
                break;
        }
        // Get second chart level line
        LevelLineHolder *line2 = [chart levelLines][1];
        // Save line parameters
        NSDictionary *line2Dict = [line2 saveState];
        // Change color parameter value
        [line2Dict setValue:[UIColor purpleColor] forKey:@"color"];
        // Load line parameters
        [line2 loadState:line2Dict];
        // If chart contains the specified line, remove it
        if([chart hasLevelLine:line1])
        {
            [chart deleteLevelLine:line1];
        }
        // Redraw chart
        [chart redrawChart];
    }
    else
    {
        // Remove all chart level lines
        [chart deleteAllLevelLines];
        // Redraw chart
        [chart redrawChart];
    }
}

After executing the example two chart level lines are created and displayed:

Click the chart area.

As a result, the development environment console displays information on the first level line:

Indicates whether the point with the coordinates (248.000000, 158.000000) belongs to level line: 1

Level line position on a chart: 154.000000

Absolute position of level line: 243.775000

Line type: horizontal

After that this level line is removed, the second line changed its color:

The repeated click on the chart area all level lines are removed.

See also:

Examples of Component Use