Chart Parameters

Description: Creating a line chart using the set data source.

The ViewController.h File

#import "DataSource.h"
#import <Charting/ChartTouchesDelegate.h>
#import <Charting/Chart.h>
#import <Charting/TrendLine.h>
@interface ViewController : UIViewController<ChartTouchesDelegate>{
    Chart *chart;
    float max;
    float min;
    NSMutableDictionary *m_numberFormat;
    int axisType;
    BOOL hideAxis;
    int chartType;
    NSArray *m_colors;
    DataSource *datasource;
    SolidColorBrush *solidBrush;
    TrendLine *line;
    LevelLineHolder *levelLine;
}
- (ChartLabel *)createDefaultLabel;
- (void) setChartSettings;
- (void) drawAllSeries;
@end

The ViewController.m File

#import &quot;ViewController.h&quot;
#define ColorWithRGBA(_r, _g, _b, _a) \
[UIColor colorWithRed:(_r) / 255.0f green:(_g) / 255.0f blue:(_b) / 255.0f alpha:(_a) / 255.0f]
@implementation ViewController
- (void)dealloc
{
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    m_numberFormat = [[NSMutableDictionary dictionaryWithObjectsAndKeys:
                       [NSNumber numberWithBool:NO], @&quot;asInSource&quot;,
                       [NSNumber numberWithInt:2], @&quot;decNumbers&quot;,
                       [NSNumber numberWithBool:YES], @&quot;separator&quot;,
                       @&quot;&quot;, @&quot;prefix&quot;,
                       @&quot;&quot;, @&quot;suffix&quot;,
                       [NSNumber numberWithBool:NO], @&quot;negativeRed&quot;,
                       nil] retain];
    m_colors = [[NSArray arrayWithObjects:
                 ColorWithRGBA(19, 88, 138, 255),
                 ColorWithRGBA(109, 167, 192, 255),
                 ColorWithRGBA(41, 171, 226, 255),
                 ColorWithRGBA(0, 169, 157, 255),
                 ColorWithRGBA(103, 124, 139, 255),
                 ColorWithRGBA(221, 241, 250, 255),
                 ColorWithRGBA(169, 221, 243, 255),
                 ColorWithRGBA(102, 203, 196, 255),
                 ColorWithRGBA(208, 222, 232, 255),
                 ColorWithRGBA(190, 160, 186, 255),
                 nil] retain];
    // Create a chart and set size
    chart = [[Chart alloc] initWithName:@&quot;CHart≥&quot;];
    chart.frame=CGRectMake(0, 0, 600, 550);
    chart.backgroundColor = [UIColor whiteColor];
    chart.plotArea.zoomEnabled = NO;
    chart.usingRightToLeft = NO;
    chart.minimalHitRadius = 4;
    // Create a brush for chart element fill
    SolidColorBrush *br = [SolidColorBrush new];
    br.color = [UIColor lightGrayColor];
    br.opacity = 0.1;
    // Set legend parameters
    chart.legend.background = [br autorelease];
    chart.legend.borderRadius = 2;
    chart.legend.borderThickness = 0;
    chart.legend.borderColor = [UIColor blackColor];
    chart.legend.font = [UIFont fontWithName:@&quot;Arial&quot; size:12];
    chart.legend.legendOrientation = LegendOrientationBottom;
    chart.legend.textColor = [UIColor blackColor];
    chart.legend.contentRotation = LegendContentRotationNone;
    // Set chart area parameters
    chart.chartArea.borderColor = [UIColor whiteColor];
    chart.chartArea.background = [SolidColorBrush solidColorBrushWithColor:[UIColor whiteColor]];
    // Create chart data source
    datasource = [[DataSource alloc] init];
    [self setChartSettings];
    [self drawAllSeries];
    // Set chart data source
    [chart setDataSource:datasource];
    [chart setDelegate:self];
    // Set minimum and maximum values of data source
    [datasource prepare];
    // Build chart
    for (ChartSeries *series in chart.seriesList) {
        [series.points removeAllObjects];
        [series forceDataUpdate];
    }
    // Execute custom example
    [self executeExample];
    // Display chart
    [self.view addSubview:chart];
}
// Function for example execution
-(void) executeExample{
    
}
- (void)drawAllSeries
{
    [chart deleteAllSeries]; // Remove all data series
    // Create a trend line
    line = [[[TrendLine alloc] init] autorelease];
    line.dataIndex = [NSString stringWithFormat:@&quot;%d&quot;, 2];
    [line setDataIndex:@&quot;3&quot;];
    line.color = [UIColor redColor];
    line.thickness = 3;
    line.dataSource = datasource;
    line.chartView = chart;
    line.autoName = NO;
    line.name = @&quot;Trend line&quot;;
    line.legendVisible = YES;
    line.visible = NO;
    [line updateData];
    // Add trend line to chart
    [chart addTrendLine:line];
    
    // Add series to chart
    int seriesCount = datasource.countSeries;
    for (int i = 0; i &lt; seriesCount; ++i)
    {
        UIColor *color = [m_colors objectAtIndex:i % [m_colors count]];
        axisType = ValueAxisAbsolute;
        hideAxis = NO;
        ColumnSeries *ser = [[ColumnSeries new] autorelease];
        ser.background = [SolidColorBrush solidColorBrushWithColor:color];
        ser.shadowOffset  = CGSizeMake(0, 0);
        ser.showShadow = NO;
        ser.dataIndex = i;
        ser.fillRatio = 0.7;
        ser.borderThickness = 2.0;
        ser.borderColor = [UIColor blackColor];
        [chart addSeries:ser];
    }
    [chart forceDataUpdate];
    // Create a level line
    levelLine = [[LevelLineHolder alloc] init];
    levelLine.chartView = chart;
    levelLine.thickness = 2;
    levelLine.value = 12;
    levelLine.name = @&quot;Level line&quot;;
    levelLine.color = [UIColor blueColor];
    levelLine.legendVisible = YES;
    levelLine.visible = YES;
    levelLine.labelVisible = YES;
    // Add level line to chart
    [chart addLevelLine:levelLine];
    // Build axes
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).valueAxisType = axisType;
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).valueAxisType = axisType;
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).visible = !hideAxis;
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).visible = !hideAxis;
    [chart.plotArea redrawChart];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)setChartSettings
{
    // Set X  axis parameters
    ValueAxis *axisX = (ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]];
    axisX.axisThickness = 1;
    axisX.majorTicks.thickness = 0;
    axisX.minorTicks.thickness = 0;
    axisX.axisLabelsVertical = NO;
    axisX.axisLabelsVisible = YES;
    // Set Y axis parameters
    ValueAxis *axisY = (ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]];
    axisY.axisLabelsVisible = YES;
    axisY.axisThickness = 1;
    axisY.majorTicks.thickness = 1;
    axisY.majorTicks.size = 3;
    axisY.minorTicks.thickness = 0;
    axisY.showGrid = YES;
    axisY.gridThickness = 1;
    axisY.gridColor = ColorWithRGBA(200, 200, 200, 255);
    axisY.caption.visible = YES;
    axisY.caption.text = @&quot;axisY&quot;;
    axisY.caption.font = [UIFont fontWithName:@&quot;Arial&quot; size:20.0];
    axisY.axisLabelsVertical = NO;
    // Set margins for chart legend and area
    Thickness th = {10,0,0,20};
    chart.legend.margin = th;
    th.bottom = 0;
    th.left = th.right = th.top = 10;
    chart.chartArea.padding = th;
}
- (void)chartDataReady:(id)sender
{
    NSLog(@&quot;Data was load&quot;);
}
@end

See also:

Creating of Level Lines and Trend on Chart