利用Core Plot绘制柱状图

上次说到利用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);
}
最后是效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值