Application needs to create an instance of CPTGraphHostingView which is hosting the graph view. In this class, there is hostedGraph which is an instance of CPTGraph which is a generic interface. In this case, we can have the CPTXYGraph instance which is Bar chart kind of graph.
CPTXYGraph *barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
barGraph.hostedGraph = barChart;
barGraph.allowPinchScaling = NO;
barChart.paddingLeft = 35.0;
barChart.paddingTop = 20.0;
barChart.paddingRight = 20.0;
We can also apply a theme for the graph using the below statements
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainBlackTheme];
[barChart applyTheme:theme];
barChart.plotAreaFrame.masksToBorder = NO;
barChart.plotAreaFrame.borderLineStyle = nil;
The Axis style can be set like below
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.1;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] col
We can give the Axis labels like this below
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[months objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
[customLabels addObject:newLabel];
x.axisLabels = [NSSet setWithArray:customLabels];
Now we can draw each of the bar like in the below code
CPTBarPlot *barPlot = [[CPTBarPlot alloc] init];
barPlot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:87/255.0 green:142/255.0 blue:242/255.0 alpha:1.0]];
barPlot.dataSource = self;
barPlot.barCornerRadius = 2.0f;
barPlot.delegate = self;
barPlot.lineStyle = barLineStyle;
barPlot.baseValue = CPTDecimalFromFloat(0.0f);
Application needs to implement the methods of CPTPlotDataSource so that the bar is supplied with the data
The only mandatory method is
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;
Other methods such as below are optional in the API
-(NSArray *)numbersForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange;
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx;
-(double *)doublesForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange;
-(double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx;
-(CPTNumericData *)dataForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange;
-(CPTNumericData *)dataForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange;
-(NSArray *)dataLabelsForPlot:(CPTPlot *)plot recordIndexRange:(NSRange)indexRange;
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx;
references:
http://www.raywenderlich.com/13269/how-to-draw-graphs-with-core-plot-part-1
references:
http://www.raywenderlich.com/13269/how-to-draw-graphs-with-core-plot-part-1
No comments:
Post a Comment