1. 初始化的动作
// create the graph
CPTGraphHostingView *chartLayout=[[CPTGraphHostingView alloc] initWithFrame:CGRectMake(320, 0, self.frame.size.width, self.frame.size.height-BUTTON_LAYOUT_HEIGHT)];
chartLayout.backgroundColor=[UIColor whiteColor];
graph=[[CPTXYGraph alloc] initWithFrame:chartLayout.bounds];
graph.plotAreaFrame.masksToBorder=NO;
chartLayout.hostedGraph=graph;
//configure the graph
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
// graph在hostingView中的偏移
graph.paddingBottom=5.0f;
graph.paddingLeft=5.0f;
graph.paddingRight=5.0f;
graph.paddingTop=5.0f;
graph.plotAreaFrame.borderLineStyle=nil;
graph.plotAreaFrame.cornerRadius=0.0f;// hide frame
// 绘图区4边留白
graph.plotAreaFrame.paddingTop=5.0;
graph.plotAreaFrame.paddingRight=5.0;
graph.plotAreaFrame.paddingLeft=50.0;
graph.plotAreaFrame.paddingBottom=30.0;
//设置title
// set up the plots
CPTBarPlot *plot=[CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
// set up line style
CPTMutableLineStyle *barLineStyle=[[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor=[CPTColor blackColor];
barLineStyle.lineWidth=1.0;
// set up text style
CPTMutableTextStyle *textLineStyle=[CPTMutableTextStyle textStyle];
textLineStyle.color=[CPTColor blackColor];
//set up plot space
CGFloat xMin=0.1f;
CGFloat xMax=7.9f;
CGFloat yMin=0.1f;
CGFloat yMax=7500.0f;
CPTXYPlotSpace *plotSpace=(CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
plotSpace.yRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];
// add plots to graph
plot.dataSource=self;
plot.delegate=self; // 如果不需要柱状图的选择,这条语句是没必要的
plot.baseValue=CPTDecimalFromInt(0); // 设定基值,大于该值的从此点向上画,小于该值的反向绘制,即向下画
plot.barWidth=CPTDecimalFromDouble(0.5); // 设定柱图的宽度(0.0~1.0)
plot.barOffset=CPTDecimalFromDouble(-0.0); // 柱图每个柱子开始绘制的偏移位置,我们让它绘制在刻度线中间,所以不偏移
plot.lineStyle=barLineStyle;
// set axis
CPTXYAxisSet *axisSet=(CPTXYAxisSet *)graph.axisSet;
// xAxis
CPTXYAxis *xAxis=axisSet.xAxis;
CPTMutableLineStyle *xLineStyle=[[CPTMutableLineStyle alloc] init];
xLineStyle.lineColor=[CPTColor blackColor];
xLineStyle.lineWidth=1.0f;
xAxis.axisLineStyle=xLineStyle;
xAxis.labelingPolicy=CPTAxisLabelingPolicyNone;
xAxis.axisConstraints=[CPTConstraints constraintWithLowerOffset:0.0];// 加上这两句才能显示label
xAxis.majorTickLineStyle=xLineStyle; //X轴大刻度线,线型设置
xAxis.majorTickLength=5; // 刻度线的长度
xAxis.majorIntervalLength=CPTDecimalFromInt(1); // 间隔单位,和xMin~xMax对应
// 小刻度线minor...
xAxis.minorTickLineStyle=nil;
xAxis.orthogonalCoordinateDecimal=CPTDecimalFromInt(0);
// yAxis
CPTXYAxis *yAxis=axisSet.yAxis;
yAxis.axisLineStyle=xLineStyle;
yAxis.majorTickLineStyle=xLineStyle; //X轴大刻度线,线型设置
yAxis.majorTickLength=5; // 刻度线的长度
yAxis.majorIntervalLength=CPTDecimalFromInt(300); // 间隔单位,和yMin~yMax对应
// 小刻度线minor...
yAxis.minorTickLineStyle=nil; // 不显示小刻度线
yAxis.orthogonalCoordinateDecimal=CPTDecimalFromInt(0);
// 设置X轴label
NSMutableArray *labelArray=[NSMutableArray arrayWithCapacity:7];
NSArray *conArray=[NSArray arrayWithObjects:@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",@"星期日", nil];
int labelLocation=1;
for(NSString *label in conArray){
CPTAxisLabel *newLabel=[[CPTAxisLabel alloc] initWithText:label textStyle:xAxis.labelTextStyle];
newLabel.tickLocation=[[NSNumber numberWithInt:labelLocation] decimalValue];
newLabel.offset=xAxis.labelOffset+xAxis.majorTickLength;
newLabel.rotation=M_PI/6;
[labelArray addObject:newLabel];
labelLocation++;
[newLabel release];
}
xAxis.axisLabels=[NSSet setWithArray:labelArray];
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace]; // 将plot添加到默认的空间中
[self addSubview:chartLayout];
2. 实现CPTBarPlotDataSource协议方法
#pragma mark - CPTBarPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
return 7;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num=nil;
if ([plot isKindOfClass:[CPTBarPlot class]]) {
NSMutableDictionary *tempDic=[self.mArray objectAtIndex:index];
switch (fieldEnum) {
case CPTBarPlotFieldBarLocation:
num=[NSNumber numberWithInt:index+1]; // X轴上的数值表示
break;
case CPTBarPlotFieldBarTip:
num=[NSDecimalNumber numberWithInt:[[mArray objectAtIndex:index] intValue]];
break;
default:
break;
}
}
return num;
}
//-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index{
// return nil;
//}
// 在柱子上面显示对应的值
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index{
CPTMutableTextStyle *textLineStyle=[CPTMutableTextStyle textStyle];
textLineStyle.fontSize=14;
textLineStyle.color=[CPTColor blackColor];
NSMutableDictionary *tempDic=[self.mArray objectAtIndex:index];
CPTTextLayer *label=[[CPTTextLayer alloc] initWithText:[mArray objectAtIndex:index] style:textLineStyle];
return label;
}
三,效果图
注:此图数据和我具体的项目有关,所以如果你单纯的复制上面的代码产生不了此图,我只是给出其中最核心的代码,如果有任何方面的疑问和交流,请留言
-------------对 软件456 的回复
需要注意的几个地方:
1. plotSpace.yRange:这个地方你要设置正确,然后才能显示完整。
如依据上图,我的设置为:
CGFloat yMin=-1000.0f;
CGFloat yMax=1500.0f;
plotSpace.yRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin)
length:CPTDecimalFromFloat(yMax-yMin)];
2. 轴上的数据显示
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index{
CPTMutableTextStyle
*textLineStyle=[CPTMutableTextStyle textStyle];
textLineStyle.fontSize=14;
textLineStyle.color=[CPTColor blackColor];
// NSMutableDictionary *tempDic=[self.mArray
objectAtIndex:index];
// CPTTextLayer *label=[[CPTTextLayer alloc] initWithText:[tempDic
objectForKey:KEY] style:textLineStyle];
CPTTextLayer *label=[[CPTTextLayer alloc] initWithText:
[NSString stringWithFormat:@"%d",-600+300*index] style:textLineStyle];
return [label
autorelease];
}