iOS Charts in Objective C Part 2

In Part 1 we discussed how to get your project ready to use Charts. In Part 2 we'll actually create some line and bar charts.

We'll be starting with the mostly blank Page View Controller presented in Part 1. The only code additions to the default application are the two import lines in ViewController.h.

Define the Data

NSArray *xaxis_data = [NSArray arrayWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",nil]; NSArray *yaxis_data = [NSArray arrayWithObjects:@"5",@"10",@"15",@"10",@"20",nil];

Line Chart

LineChartView* _chartView = [[LineChartView alloc] initWithFrame:CGRectMake(20, 50, 320, 300)]; [self.view addSubview:_chartView]; _chartView.delegate = self; _chartView.descriptionText = @""; _chartView.noDataTextDescription = @"You need to provide data for the chart."; [_chartView.legend setEnabled:NO]; [_chartView.rightAxis setEnabled: NO]; // y axis on right _chartView.xAxis.drawGridLinesEnabled = NO; // vertical grid lines _chartView.xAxis.labelPosition = XAxisLabelPositionBottom; _chartView.xAxis.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.0f]; LineChartDataSet *set1 = [[LineChartDataSet alloc] initWithYVals:y_qty2 label:@"DataSet 1"]; NSMutableArray *dataSets = [[NSMutableArray alloc] init]; [dataSets addObject:set1]; LineChartData *data = [[LineChartData alloc] initWithXVals:xaxis_data dataSets:dataSets]; = data;
Building and running this should produce the following:

Bar Chart

Combined Chart