Description: Creating a line chart using the set data source.
#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
#import "ViewController.h" #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], @"asInSource", [NSNumber numberWithInt:2], @"decNumbers", [NSNumber numberWithBool:YES], @"separator", @"", @"prefix", @"", @"suffix", [NSNumber numberWithBool:NO], @"negativeRed", 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:@"CHart≥"]; 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:@"Arial" 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:@"%d", 2]; [line setDataIndex:@"3"]; line.color = [UIColor redColor]; line.thickness = 3; line.dataSource = datasource; line.chartView = chart; line.autoName = NO; line.name = @"Trend line"; 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 < 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 = @"Level line"; 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 = @"axisY"; axisY.caption.font = [UIFont fontWithName:@"Arial" 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(@"Data was load"); } @end
See also: