Chart Parameters

Description: a chart is built by parameters specified in the menu.

The ViewController.h File

#import "DataSource.h"
#import "Menu.h"
#import <Charting/ChartTouchesDelegate.h>
#import <Charting/Chart.h>
#import <Charting/ColumnSeries.h>
#import <Charting/AreaSeries.h>
#import <Charting/BarSeries.h>
#import <Charting/PieSeries.h>
#import <Charting/LineSeries.h>
#import <Charting/LinePoint.h>
#import <Charting/ChartTouchesDelegate.h>
#import <Charting/ChartLabel.h>
typedef enum
{
    kColumnAbsolute,
    kColumnAdditional,
    kColumnPercent,
    kBarAbsolute,
    kBarAdditional,
    kBarPercent,
    kLineAbsolute,
    kLineAdditional,
    kLinePercent,
    kAreaAbsolute,
    kAreaAdditional,
    kAreaPercent,
    kPie,
    kDoughnut,
} ChartType;
@interface ViewController : UIViewController<ChartTouchesDelegate,SelectedCellAction> {
    Chart *chart;
    float max;
    float min;
    NSMutableDictionary *m_numberFormat;
    int axisType;
    BOOL hideAxis;
    int chartType;
    NSArray *m_colors;
    DataSource *datasource;
    Menu *menu;
}
- (ChartLabel *)createDefaultLabel;
- (void) setChartSettings;
- (ChartSeries *)createSeriesWithType:(ChartType)type;
- (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];
    chartType = 0;
    menu = [[Menu alloc] init];
    menu.delegate = self;
    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];
    // Create a data source
    datasource = [[DataSource alloc] init];
    [self setChartSettings];
    [self drawAllSeries];
    // Set link to data source
    [chart setDataSource :datasource];
    [chart setDelegate:self];
    // Set maximum and minimum border values along Y axis for data source
    [datasource prepare];
    [chart forceDataUpdate];
    chart.frame = CGRectMake(0, 40, chart.frame.size.width, chart.frame.size.height - 40);
    // Display chart
    [self.view addSubview:chart];
    UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 600, 40)] autorelease];
    [self.view addSubview:toolBar];
    [toolBar setBarStyle:UIBarStyleDefault];
    UIBarButtonItem *showMenuButton = [[[UIBarButtonItem alloc] initWithTitle:@&quot;Chart type&quot; style:UIBarButtonItemStyleDone target:self action:@selector(showMenu)] autorelease];
    toolBar.items = [NSArray arrayWithObjects:showMenuButton, nil];
}
- (void) showMenu
{
    [self.view addSubview:menu.view];
}
- (void)drawAllSeries
{
    [chart deleteAllSeries];
    int seriesCount = datasource.countSeries;
    for (int i = 0; i &lt; seriesCount; ++i)
    {
        ChartSeries *series = [self createSeriesWithType:chartType];
        series.dataIndex = i;
        UIColor *color = [m_colors objectAtIndex:i % [m_colors count]];
        if ([series respondsToSelector:@selector(setColor:)])
        {
            [(LineSeries *)series setColor:color];
        }
        else if ([series respondsToSelector:@selector(setBorderColor:)] &amp;&amp;
                 [series respondsToSelector:@selector(setBackground:)])
        {
            SolidColorBrush *brush = [[SolidColorBrush new] autorelease];
            brush.color = [m_colors objectAtIndex:i % [m_colors count]];
            [(AreaSeries *)series setBackground:brush];
        }
        series.defaultLabel = [self createDefaultLabel];
        [chart addSeries:series];
    }
    [chart forceDataUpdate];
    ((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;
}
- (ChartSeries *)createSeriesWithType:(ChartType)type
{
    ChartSeries *series = nil;
    axisType = ValueAxisAbsolute;
    hideAxis = NO;
    switch (type)
    {
        case kColumnAbsolute:
            series = [[ColumnSeries new] autorelease];
            ((ColumnSeries *)series).fillRatio = 0.7;
            ((ColumnSeries *)series).borderThickness = 0;
            break;
        case kColumnAdditional:
            series = [[ColumnSeries new] autorelease];
            ((ColumnSeries *)series).fillRatio = 0.7;
            ((ColumnSeries *)series).borderThickness = 0;
            ((ColumnSeries *)series).borderRadius = 0.5;
            ((ColumnSeries *)series).borderColor = [UIColor whiteColor];
            axisType = ValueAxisRelative;
            break;
        case kColumnPercent:
            series = [[ColumnSeries new] autorelease];
            ((ColumnSeries *)series).fillRatio = 0.7;
            ((ColumnSeries *)series).borderThickness = 0;
            axisType = ValueAxisPercent;
            break;
        case kAreaAbsolute:
            series = [[AreaSeries new] autorelease];
            ((AreaSeries *)series).borderThickness = 1;
            ((AreaSeries *)series).borderColor = [UIColor whiteColor];
            series.alpha = 0.4;
            break;
        case kAreaAdditional:
            series = [[AreaSeries new] autorelease];
            ((AreaSeries *)series).borderThickness = 0;
            axisType = ValueAxisRelative;
            series.alpha = 0.4;
            break;
        case kAreaPercent:
            series = [[AreaSeries new] autorelease];
            ((AreaSeries *)series).borderThickness = 0;
            axisType = ValueAxisPercent;
            series.alpha = 0.4;
            break;
        case kLineAbsolute:
            series = [[LineSeries new] autorelease];
            break;
        case kLineAdditional:
            series = [[LineSeries new] autorelease];
            axisType = ValueAxisRelative;
            break;
        case kLinePercent:
            series = [[LineSeries new] autorelease];
            axisType = ValueAxisPercent;
            break;
        case kBarAbsolute:
            series = [[BarSeries new] autorelease];
            ((ColumnSeries *)series).fillRatio = 0.7;
            ((BarSeries *)series).borderThickness = 0;
            break;
        case kBarAdditional:
            series = [[BarSeries new] autorelease];
            ((ColumnSeries *)series).fillRatio = 0.7;
            ((BarSeries *)series).borderThickness = 0;
            axisType = ValueAxisRelative;
            break;
        case kBarPercent:
            series = [[BarSeries new] autorelease];
            ((ColumnSeries *)series).fillRatio = 0.7;
            ((BarSeries *)series).borderThickness = 0;
            axisType = ValueAxisPercent;
            break;
        case kPie:
            series = [[PieSeries new] autorelease];
            ((PieSeries *)series).borderThickness = 1;
            ((PieSeries *)series).borderColor = [UIColor whiteColor];
            ((PieSeries *)series).cutoutLength = 10;
            ((PieSeries *)series).radiusPadding = 100;
            hideAxis = YES;
            break;
        case kDoughnut:
            series = [[PieSeries new] autorelease];
            ((PieSeries *)series).borderThickness = 1;
            ((PieSeries *)series).borderColor = [UIColor whiteColor];
            ((PieSeries *)series).radiusPadding = 100;
            ((PieSeries *)series).holeRadius = 0.3;
            hideAxis = YES;
            break;
    }
    return series;
}
- (void )viewDidUnload
{
    [super viewDidUnload];
}
- (BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void )chartTouchesTapInView:(UIView *)v withPoint:(CGPoint)point
{
    UIView *sender = [v hitTest:point withEvent:nil];
    if ([sender isKindOfClass:[ChartPoint class]])
    {
        ChartPoint *pt = (ChartPoint *)sender;
        if (pt.chartLabel)
        {
            pt.chartLabelVisible = !pt.chartLabelVisible;
            if (pt.chartLabelVisible)
                [pt moveAboveAllByZOrder];
            else
                [pt restoreZOrder];
        }
        [chart setNeedsLayout];
    }
}
- (void)setChartSettings
{
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).axisThickness = 1;  // X axis thickness
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).majorTicks.thickness = 0; // Thickness of major tick marks on X axis
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineX]]).minorTicks.thickness = 0; // Thickness of minor tick marks on X axis
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).axisThickness = 1; // Y axis thickness
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).majorTicks.thickness = 1;
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).majorTicks.size = 3; //Tick mark length
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).minorTicks.thickness = 0;
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).showGrid = YES; // Display grid
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).gridThickness = 1; //Grid thickness
    ((ValueAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisLineY]]).gridColor = ColorWithRGBA(200, 200, 200, 255);
    ((TimeAxis *)[chart.axes objectForKey:[NSNumber numberWithInt:AxisTypeTimeline]]).visible = NO; //Hide timeline
    Thickness th = {10,0,0,20}; // Margins
    chart.legend.margin = th; // Legend margins
    th.bottom = 0;
    th.left = th.right = th.top = 10;
    chart.chartArea.padding = th; // Chart area margins
}
- (ChartLabel *)createDefaultLabel
{
    ChartLabel *result = [[[ChartLabel alloc] initWithFrame:CGRectZero] autorelease]; // Create a label
    result.background = [[SolidColorBrush new] autorelease]; // Set color
    result.borderColor = [UIColor blackColor]; // Border color
    result.borderThickness = 1;
    result.borderRadius = 10;
    result.font = [UIFont fontWithName:@&quot;Arial&quot; size:18]; // Font
    result.textAlignment = UITextAlignmentCenter;
    result.textColor = [UIColor blackColor];
    result.mask = [NSString stringWithFormat:@&quot;%@#FVAL%@&quot;,
                   (NSString *)[m_numberFormat objectForKey:@&quot;prefix&quot;],
                   (NSString *)[m_numberFormat objectForKey:@&quot;suffix&quot;]];
    result.countOfDecimalNumbers = [((NSNumber *)[m_numberFormat objectForKey:@&quot;decNumbers&quot;]) intValue];
    result.displayNegativesInRed = [(NSNumber *)[m_numberFormat objectForKey:@&quot;negativeRed&quot;] boolValue];
    result.hasHorizontalNoteLine = result.hasVerticalNoteLine = result.hasFootNoteLine = NO;
    result.minWidth = 0;
    return result;
}
- (void)selectedItemWithIndex:(NSInteger)index
{
    chartType = index; // Set chart type selected in menu
    [self drawAllSeries]; // Redraw chart
}
@end

See also:

Dynamic Change of Chart Type and Parameters