您的位置:首页 > 移动开发 > IOS开发

利用Core Plot绘制柱状图

2015-11-09 17:00 525 查看
上次说到利用coreplot绘制饼状图,这次再来说说绘制柱状图

直接上代码,注释已标注

-(void)createView
{
///建立坐标
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
//建立主题
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[newGraph applyTheme:theme];
newGraph.plotAreaFrame.masksToBorder = NO;

//设置画布
CPTGraphHostingView *barChartView = [[CPTGraphHostingView alloc] initWithFrame:self.view.frame];
barChartView.hostedGraph = newGraph;
[self.view addSubview:barChartView];

//设置图表与边框的距离
newGraph.paddingLeft = 70.0;
newGraph.paddingTop = 20.0;
newGraph.paddingRight = 20.0;
newGraph.paddingBottom = 80.0;

//设置x、y轴的数值范围
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)newGraph.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@0 length:@300];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@0 length:@16];

//设置文字显示样式
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor orangeColor];
//设置线条的显示样式
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 1.0f;
lineStyle.lineColor = [CPTColor whiteColor];
//设置x、y轴显示样式
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
//标题文字显示样式
x.titleTextStyle = textStyle;
//设置x轴的显示样式
x.axisLineStyle = lineStyle;
x.majorTickLineStyle = lineStyle;
x.minorTickLineStyle = lineStyle;
//x轴主刻度,显示数字标签的量度间隔
x.majorIntervalLength = @5;
//x轴戏份刻度:每一个主刻度范围内显示细分刻度的个数
x.orthogonalPosition = @0;
//x的标题
x.title = @"X Axis";
//x标题的偏移量
x.titleLocation = @7.5;
//x之间的间隔
x.titleOffset = 55.0;
//数据标签的倾斜角
x.labelRotation = CPTFloat(M_PI_2);
x.labelingPolicy = CPTAxisLabelingPolicyNone;
//设置每个x轴上标签的位置
CPTNumberArray customTickLocations = @[@1,@5,@10,@15];
//设置x轴上每个标签的文字
CPTStringArray xAxisLabels = @[@"Label A",@"Label B",@"Label C",@"Label D"];
NSUInteger labelLocation = 0;
CPTMutableAxisLabelSet customLabels = [NSMutableSet setWithCapacity:[xAxisLabels count]];
//对x轴上的信息进行设置
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:xAxisLabels[labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = tickLocation;
newLabel.offset = x.labelOffset + x.majorTickLength;
newLabel.rotation = CPTFloat(M_PI_4);
[customLabels addObject:newLabel];
}

x.axisLabels = customLabels;

//设置y轴上的信息
CPTXYAxis *y = axisSet.yAxis;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
//设置y轴上的分段间隔
y.majorIntervalLength = @50;
y.orthogonalPosition = @0;
y.title = @"Y Axis";
y.titleOffset = 45.0;
//设置y轴标签对应的位置
y.titleLocation = @150;

//设置第一个柱状图的信息
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
//初始值
barPlot.baseValue = @0;
barPlot.dataSource = self;
barPlot.barOffset= @(-0.25);
barPlot.identifier = @"BarPlot 1";
[newGraph addPlot:barPlot toPlotSpace:plotSpace];

//设置第二个柱状图的信息
barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
barPlot.dataSource = self;
barPlot.baseValue = @0;
barPlot.barOffset = @0.25;
barPlot.barCornerRadius = 2.0;
barPlot.identifier = @"Bar Plot 2";
barPlot.delegate = self;
[newGraph addPlot:barPlot toPlotSpace:plotSpace];
}
设置数据源
#pragma mark DataScource
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return 16;
}
-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx
{
NSNumber *num = nil;
switch (fieldEnum) {
case CPTBarPlotFieldBarLocation:
if (idx == 4) {
num = @(NAN);
}
else
{
num = @(idx);
}
break;
case CPTBarPlotFieldBarTip:
if (idx == 8) {
num = @(NAN);
}
else
{
num = @( (idx + 1) * (idx + 1));
if ([plot.identifier isEqual:@"Bar Plot 2"]) {
num = @(num.integerValue - 10);
}
}
break;

default:
break;
}
return num;
}


设置点击某一个柱的代理方法
#pragma mark Delegate
-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)idx
{
NSLog(@"barWasSelectedAtRecordIndex :%lu",(unsigned long)idx);
}
最后是效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息