如何使用Core Plot绘制直方图和折线图

本文详细介绍了如何使用CorePlot库在iOS应用中绘制折线图和直方图,包括数据点的配置、图表主题设定、坐标轴定制及图形样式设置等关键步骤。

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


Core Plot提供了散点图(CPScatterPlot)绘制,包括折线图和直方图,如下图所示:

下面的代码包括了折线图和直方图的实现:

1、.h文件:

#import<UIKit/UIKit.h>

#import<CorePlot/CorePlot.h>

//散点图的数据点数:20

#define num20

@interfaceBarChartViewController : UIViewController <CPPlotDataSource>

{

@private

CPXYGraph*graph;

doublex[num] ;//散点的x坐标

doubley1[num] ;//1个散点图的y坐标

doubley2[num];//2个散点图的y坐标

}

@end

2、.m文件:

#import"BarChartViewController.h"

@implementationBarChartViewController

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

returnYES;

}

#pragma mark -

#pragma mark Initialization and teardown

-(void)viewDidAppear:(BOOL)animated

{

//CPGraph指定主题

graph= [[CPXYGraphalloc]initWithFrame:CGRectZero];

CPTheme*theme = [CPThemethemeNamed:kCPDarkGradientTheme];

[graphapplyTheme:theme];

//self.viewUIView转变为CPGraphHostingView,因为UIView无法加载CPGraph

self.view=[[CPGraphHostingViewalloc ] initWithFrame :[UIScreenmainScreen ]. bounds ];

CPGraphHostingView*hostingView = (CPGraphHostingView*)self.view;

[hostingView setHostedGraph :graph];

// CPGraph边框:无

graph.plotAreaFrame.borderLineStyle=nil;

graph.plotAreaFrame.cornerRadius=0.0f;

//绘图空间plot space

CPXYPlotSpace*plotSpace = (CPXYPlotSpace*)graph.defaultPlotSpace;

//绘图空间大小:Y0-300x0-16

plotSpace.yRange= [CPPlotRangeplotRangeWithLocation:CPDecimalFromFloat(0.0f)length:CPDecimalFromFloat(200.0f)];

plotSpace.xRange= [CPPlotRangeplotRangeWithLocation:CPDecimalFromFloat(0.0f)length:CPDecimalFromInt(num)];

// CPGraph四边不留白

graph.paddingLeft=0.0f;

graph.paddingRight=0.0f;

graph.paddingTop=0.0f;

graph.paddingBottom=0.0f;

//绘图区4边留白

graph.plotAreaFrame.paddingLeft=45.0;

graph.plotAreaFrame.paddingTop=40.0;

graph.plotAreaFrame.paddingRight=5.0;

graph.plotAreaFrame.paddingBottom=80.0;

//坐标系

CPXYAxisSet*axisSet = (CPXYAxisSet*)graph.axisSet;

//x轴:为坐标系的x

CPXYAxis*X = axisSet.xAxis;

//清除默认的轴标签,使用自定义的轴标签

X.labelingPolicy=CPAxisLabelingPolicyNone;

//构造MutableArray,用于存放自定义的轴标签

NSMutableArray*customLabels = [NSMutableArrayarrayWithCapacity:num];

//构造一个TextStyle

staticCPTextStyle* labelTextStyle=nil;

labelTextStyle=[[CPTextStylealloc]init];

labelTextStyle.color=[CPColorwhiteColor];

labelTextStyle.fontSize=10.0f;

//每个数据点一个轴标签

for(inti=0;i<num;i++) {

CPAxisLabel*newLabel = [[CPAxisLabelalloc]initWithText: [NSStringstringWithFormat:@"%d个数据点",(i+1)]textStyle:labelTextStyle];

newLabel.tickLocation=CPDecimalFromInt(i);

newLabel.offset= X.labelOffset+ X.majorTickLength;

newLabel.rotation=M_PI/2;

[customLabelsaddObject:newLabel];

[newLabelrelease];

}

X.axisLabels=[NSSetsetWithArray:customLabels];

//y

CPXYAxis*y = axisSet.yAxis;

//y轴:不显示小刻度线

y.minorTickLineStyle=nil;

//大刻度线间距:50单位

y.majorIntervalLength=CPDecimalFromString(@"50");

//坐标原点:0

y.orthogonalCoordinateDecimal=CPDecimalFromString(@"0");

y.titleOffset=45.0f;

y.titleLocation=CPDecimalFromFloat(150.0f);

//1个散点图:蓝色

CPScatterPlot*boundLinePlot = [[[CPScatterPlotalloc]init]autorelease];

//id,用于识别该散点图

boundLinePlot.identifier=@"Blue Plot";

//线型设置

CPLineStyle* lineStyle = [[[CPLineStylealloc]init]autorelease];

lineStyle.lineWidth=1.0f;

lineStyle.lineColor= [CPColorblueColor];

boundLinePlot.dataLineStyle= lineStyle;

//设置数据源,必须实现CPPlotDataSource协议

boundLinePlot.dataSource=self;

[graphaddPlot:boundLinePlot];

//在图形上添加一些小圆点符号(节点)

CPLineStyle*symbolLineStyle = [[CPLineStylealloc ]init];

//描边:黑色

symbolLineStyle.lineColor= [CPColorblackColor];

//符号类型:椭圆

CPPlotSymbol*plotSymbol = [CPPlotSymbolellipsePlotSymbol];

//填充色:蓝色

plotSymbol.fill= [CPFillfillWithColor:[CPColorblueColor]];

//描边

plotSymbol.lineStyle= symbolLineStyle;

//符号大小:10*10

plotSymbol.size=CGSizeMake(6.0,6.0);

//向图形上加入符号

boundLinePlot.plotSymbol= plotSymbol;

//创建渐变区

//渐变色1

CPColor*areaColor = [CPColorcolorWithComponentRed:0.0green:0.0blue:1.0alpha:1.0];

//创建一个颜色渐变:从建变色1渐变到无色

CPGradient*areaGradient = [CPGradientgradientWithBeginningColor:areaColorendingColor:[CPColorclearColor]];

//渐变角度:-90度(顺时针旋转)

areaGradient.angle= -90.0f;

//创建一个颜色填充:以颜色渐变进行填充

CPFill*areaGradientFill = [CPFillfillWithGradient:areaGradient];

//为图形1设置渐变区

boundLinePlot.areaFill= areaGradientFill;

//渐变区起始值,小于这个值的图形区域不再填充渐变色

boundLinePlot.areaBaseValue=CPDecimalFromString(@"0.0");

//interpolation值为CPScatterPlotInterpolation枚举类型,该枚举有3个值:

//CPScatterPlotInterpolationLinear,线性插补——折线图.

//CPScatterPlotInterpolationStepped,在后方进行插补——直方图

//CPScatterPlotInterpolationHistogram,以散点为中心进行插补——直方图

boundLinePlot.interpolation=CPScatterPlotInterpolationHistogram;

//2个散点图:绿色

CPScatterPlot*dataSourceLinePlot = [[[CPScatterPlotalloc]init]autorelease];

dataSourceLinePlot.identifier=@"Green Plot";

//线型设置

lineStyle = [[[CPLineStylealloc]init]autorelease];

lineStyle.lineWidth=1.0f;

lineStyle.lineColor= [CPColorgreenColor];

dataSourceLinePlot.dataLineStyle= lineStyle;

//设置数据源,必须实现CPPlotDataSource协议

dataSourceLinePlot.dataSource=self;

[graphaddPlot:dataSourceLinePlot] ;

//随机产生散点数据

NSUIntegeri;

for( i =0; i <num; i++ ) {

x[i] = i ;

y1[i] = (num*10)*(rand()/(float)RAND_MAX);

y2[i] = (num*10)*(rand()/(float)RAND_MAX);

}

}

#pragma mark -

#pragma mark Plot Data Source Methods

//返回散点数

-(NSUInteger)numberOfRecordsForPlot:(CPPlot*)plot

{

returnnum;

}

//根据参数返回数据(一个C数组)

- (double*)doublesForPlot:(CPPlot*)plot field:(NSUInteger)fieldEnum recordIndexRange:(NSRange)indexRange

{

//返回类型:一个double指针(数组)

double*values;

NSString* identifier=(NSString*)[plot identifier];

switch(fieldEnum) {

//如果请求的数据是散点x坐标,直接返回x坐标(两个图形是一样的),否则还要进一步判断是那个图形

caseCPScatterPlotFieldX:

values=x;

break;

caseCPScatterPlotFieldY:

//如果请求的数据是散点y坐标,则对于图形1,使用y1数组,对于图形2,使用y2数组

if([identifierisEqualToString:@"Blue Plot"]) {

values=y1;

}else

values=y2;

break;

}

//数组指针右移个indexRage.location单位,则数组截去indexRage.location个元素

returnvalues + indexRange.location;

}

//添加数据标签

-(CPLayer*)dataLabelForPlot:(CPPlot*)plot recordIndex:(NSUInteger)index

{

//定义一个白色的TextStyle

staticCPTextStyle*whiteText =nil;

if( !whiteText ) {

whiteText = [[CPTextStylealloc]init];

whiteText.color= [CPColorwhiteColor];

}

//定义一个TextLayer

CPTextLayer*newLayer =nil;

NSString* identifier=(NSString*)[plot identifier];

if([identifierisEqualToString:@"Blue Plot"]) {

newLayer = [[[CPTextLayeralloc]initWithText:[NSStringstringWithFormat:@"%.0f",y1[index]]style:whiteText]autorelease];

}

returnnewLayer;

}

@end


原文地址:http://blog.youkuaiyun.com/kmyhy/article/details/6217683

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值