core plot的学习与应用

本文介绍了Core Plot框架的基础概念,强调了CPTGraph类在统计图表绘制中的核心地位,及其与其他关键类如CPTAnnotation、CPTLegend等的关联关系。通过设置这些类的参数,并将CPTGraph添加到视图,可以实现统计图的创建和显示。文中还以条形图为例,结合代码注释和界面截图,展示了如何使用Core Plot绘制图表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


基本概念说明

core-plot的UML类图如上

从上图可以看出CPTGraph类是比较核心的类,它与CPTAnnotation,CPTLegend,CPTPlotAreaFrame,CPTAxisSet,CPTPlot,CPTPlotSpace成关联关系,通过设置CPTGraph的各项参数并将其添加到视图中,即可实现统计图的绘制与显示。


关键类的功能与作用
CPTAnnotation
CPTAnnotation是抽象类,它的两个子类分别是CPTLayerAnnotation与CPTPlotSpaceAnnotation,分别用于为基本图层与绘图图层添加文字注释。
CPTLegend
用于添加图例。
CPTPlotAreaFrame
其内部的核心类为 CPTPlotArea,可以理解为plot的容器,通过设置可以定制plot以外的图形,如网格效果,背景填充颜色等,还定义了交互行为协议
CPTAxisSet
用于管理CPTAxis类与其子类,实现坐标轴的定制与显示。
CPTPlot
CPTPlot是抽象的绘图类,其有CPTBarPlot、CPTPieChart、CPTRangePlot、CPTScatterPlot、CPTTradingRangePlot五个子类,通过实现五个子类,可以分别实现条状图、圆饼图、幅度图、散点图、交易幅度图的现实。其设计思路与tableView十分相似,也是通过datasource实现数据的填充,delegate实现交互行为与特殊数据填充。
CPTPlotSpace
通过定义CPTPlotSpace实现图空间的定制,因为坐标轴中的图画通常是无限大的,因此需要配置相关的参数决定需要显示的部分。

因为,无论是绘制什么样的图,实际上就是针对性地设置以上五个类对应的对象的各项参数。



经典例程分析

条形图
结合代码注释以及最下面的界面说明图能基本了解如何绘制图片。
    //graph添加到指定的hostingView,普通的view无法添加graph
   
CPTGraph *graph = [[CPTXYGraphalloc]initWithFrame:bounds];
    [
selfaddGraph:graphtoHostingView:hostingView];
   
   
//设置主题,效果如手机设置主题。有如下主题kCPTDarkGradientThemekCPTPlainBlackThemekCPTPlainWhiteThemekCPTSlateThemekCPTStocksTheme
    [
selfapplyTheme:themetoGraph:graphwithDefault:[CPTThemethemeNamed:kCPTPlainWhiteTheme]];

   
//设置plotAreaFrame的大小,plotAreaFramegraph上的一张子图。设置padding参数是用来设置子图的四个边缘与父视图的距离
    graph.
plotAreaFrame.paddingLeft  += self.titleSize* CPTFloat(2.5);
    graph.
plotAreaFrame.paddingTop    += self.titleSize* CPTFloat(1.25);
    graph.
plotAreaFrame.paddingRight  += self.titleSize;
    graph.
plotAreaFrame.paddingBottom+= self.titleSize;
    graph.
plotAreaFrame.masksToBorder  = NO;

   
//创建grid line styles,定义了majorGridLineStyleminorGridLineStyle两种网格格式
   
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStylelineStyle];
    majorGridLineStyle.
lineWidth= 1.0;
    majorGridLineStyle.
lineColor= [[CPTColorblackColor]colorWithAlphaComponent:0.75];

   
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStylelineStyle];
    minorGridLineStyle.
lineWidth= 1.0;
    minorGridLineStyle.
lineColor= [[CPTColorblackColor]colorWithAlphaComponent:0.25];

   
//获取graph的坐标轴集合
   
CPTXYAxisSet *axisSet = (CPTXYAxisSet*)graph.axisSet;
   
   
//获取graph.axisSet中的x
   
CPTXYAxis *x          = axisSet.xAxis;
    {
       
//设置x轴方向的网格格式
        x.
majorGridLineStyle          = majorGridLineStyle;
        x.
minorGridLineStyle          = minorGridLineStyle;
       
       
//主要间隔长度
        x.
majorIntervalLength        =CPTDecimalFromInteger(1);
       
       
//间隔最少标签数
        x.
minorTicksPerInterval      =0;
       
       
//x正交坐标位置
        x.
orthogonalCoordinateDecimal= CPTDecimalFromInteger(0);

        x.
axisLineStyle              =nil;
        x.
majorTickLineStyle          =nil;
        x.
minorTickLineStyle          =nil;
        x.
labelFormatter              =nil;
    }

   
//获取graph.axisSet中的y
   
CPTXYAxis *y = axisSet.yAxis;
    {
       
//设置y轴方向的网格风格
        y.
majorGridLineStyle          = majorGridLineStyle;
        y.
minorGridLineStyle          = minorGridLineStyle;
       
       
//主要间隔长度
        y.
majorIntervalLength        =CPTDecimalFromInteger(1);
       
       
//间隔最少标签数
        y.
minorTicksPerInterval      =9;
       
       
//y轴自动布局约束
        y.
axisConstraints            = [CPTConstraintsconstraintWithLowerOffset:0.0];
       
       
//主轴的数字标签数量
        y.
preferredNumberOfMajorTicks= 16;

       
//轴标签偏移量
        y.
labelOffset                =self.titleSize* CPTFloat(0.375);
       
       
//轴每个数字标签旋转角度
        y.
labelRotation              =CPTFloat(M_PI_4);
       
       
//标签规则
        y.
labelingPolicy              =CPTAxisLabelingPolicyAutomatic;
       
        y.
axisLineStyle              =nil;
        y.
majorTickLineStyle          =nil;
        y.
minorTickLineStyle          =nil;

       
        y.
title      =@"Y Axis";
       
       
//标题
        y.
titleOffset= self.titleSize* CPTFloat(1.25);
    }

   
// Create a bar line style
   
CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStylealloc]init];
    barLineStyle.
lineWidth= 1.0;                  //bar 线框宽度
    barLineStyle.
lineColor= [CPTColorwhiteColor];//bar 线框颜色

   
// Create bar plot
   
CPTBarPlot *barPlot = [[CPTBarPlotalloc]init];
    barPlot.
lineStyle        = barLineStyle;
    barPlot.
barWidth          =CPTDecimalFromFloat(0.75f);// bar is 75% of the available space
    barPlot.
barCornerRadius  =10.0;
    barPlot.
barsAreHorizontal= NO;
    barPlot.
dataSource        =self;
    barPlot.
identifier        =@"Bar Plot 1";

    [graph
addPlot:barPlot];

   
//setting Plot space:设置显示的范围
   
   
//获取x轴的范围并额外添加1.05的长度
   
CPTMutablePlotRange *barRange = [[barPlotplotRangeEnclosingBars]mutableCopy];
    [barRange
expandRangeByFactor:CPTDecimalFromDouble(1.05)];

   
CPTXYPlotSpace *barPlotSpace = (CPTXYPlotSpace*)graph.defaultPlotSpace;
    barPlotSpace.
xRange= barRange;
    barPlotSpace.
yRange= [CPTPlotRangeplotRangeWithLocation:CPTDecimalFromFloat(0.0f)length:CPTDecimalFromFloat(16.0f)];

   
// Add legend:添加图例
   
CPTLegend *theLegend = [CPTLegendlegendWithGraph:graph];
   
   
//设置填充颜色
    theLegend.
fill            = [CPTFillfillWithColor:[CPTColorcolorWithGenericGray:CPTFloat(0.15)]];
   
   
//设置边框风格
    theLegend.
borderLineStyle= barLineStyle;
   
    theLegend.
cornerRadius    =2.0;
   
   
//设置文本风格
   
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyletextStyle];
    whiteTextStyle.
color  = [CPTColorwhiteColor];
    theLegend.
textStyle    = whiteTextStyle;
    theLegend.
numberOfRows= 2;

    graph.
legend            = theLegend;
   
   
//设置图例放置的位置
    graph.
legendAnchor      =CPTRectAnchorTop;
    graph.
legendDisplacement= CGPointMake( 0.0, self.titleSize* CPTFloat(-2.625) );
}

#pragma mark -
#pragma mark Plot Data Source Methods

-(
NSUInteger)numberOfRecordsForPlot:(CPTPlot*)plot
{
   
//返回plot的数量,本例中有八个
   
return self.plotData.count;
}


-(
NSArray*)numbersForPlot:(CPTPlot*)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange
{
   
NSArray *nums = nil;

   
//通过fieldEnum分别返回每个bar的位置数组与数据数组
   
switch ( fieldEnum ) {
           
//返回bar plot的位置图
       
case CPTBarPlotFieldBarLocation:
            nums = [
NSMutableArrayarrayWithCapacity:indexRange.length];
           
for ( NSUInteger i = indexRange.location; i <NSMaxRange(indexRange); i++ ) {
                [(
NSMutableArray*)nums addObject : @(i)];
            }
           
break;

           
//获取bar plot的数据
       
case CPTBarPlotFieldBarTip:
            nums = [
self.plotDataobjectsAtIndexes:[NSIndexSetindexSetWithIndexesInRange:indexRange]];
           
break;

       
default:
           
break;
    }

   
return nums;
}

-(
CPTFill*)barFillForBarPlot:(CPTBarPlot*)barPlot recordIndex:(NSUInteger)index
{
   
   
CPTColor *color = nil;

   
switch ( index ) {
       
case 0:
            color = [
CPTColorredColor];
           
break;

       
case 1:
            color = [
CPTColorgreenColor];
           
break;

       
case 2:
            color = [
CPTColorblueColor];
           
break;

       
case 3:
            color = [
CPTColoryellowColor];
           
break;

       
case 4:
            color = [
CPTColorpurpleColor];
           
break;

       
case 5:
            color = [
CPTColorcyanColor];
           
break;

       
case 6:
            color = [
CPTColororangeColor];
           
break;

       
case 7:
            color = [
CPTColormagentaColor];
           
break;

       
default:
           
break;
    }

   
//设置渐变填充风格对象,渐变风格由{以上代码设置的color}渐变到{黑色}结束。
   
CPTGradient *fillGradient = [CPTGradientgradientWithBeginningColor:colorendingColor:[CPTColorblackColor]];

   
//返回设置填充效果的对象
   
return [CPTFillfillWithGradient:fillGradient];

}

-(
NSString*)legendTitleForBarPlot:(CPTBarPlot*)barPlot recordIndex:(NSUInteger)index
{
   
//设置图例中每个单元的名称
   
return [NSStringstringWithFormat:@"Bar %lu", (unsignedlong)(index +1)];
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值