在项目需求中。要画一个点线图。横坐标是 288个数值。代表:每5分钟一个值。每天产生 288个值。
在图表中,只需要显示 4,6,8,10,12,14,16,18,20,22,24 ,这几个整数的label,并且要转换成 文字描述。
参考文章
注意 这句话:
x.labelingPolicy = CPTAxisLabelingPolicyNone;
First, you set the axis labeling policy to CPTAxisLabelingPolicyNone
to let the framework know you will be providing custom labels, then you create an array of labels with their corresponding locations, and finally you assign that array of labels to the axisLabels
property on the X axis.
// 设置X轴label
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSMutableArray *labelArray=[NSMutableArray arrayWithCapacity:288];
for ( int i = 1 ; i<=288 ;i++)
{
CPTAxisLabel *newLabel ;
if(i == 48)
{
newLabel=[[CPTAxisLabel alloc] initWithText:@"4点" textStyle:x.labelTextStyle];
}else if (i== 72){
newLabel=[[CPTAxisLabel alloc] initWithText:@"6点" textStyle:x.labelTextStyle];
}else if (i== 96){
newLabel=[[CPTAxisLabel alloc] initWithText:@"8点" textStyle:x.labelTextStyle];
}else if (i== 120){
newLabel=[[CPTAxisLabel alloc] initWithText:@"10点" textStyle:x.labelTextStyle];
}else if (i== 144){
newLabel=[[CPTAxisLabel alloc] initWithText:@"12点" textStyle:x.labelTextStyle];
}else if (i== 168){
newLabel=[[CPTAxisLabel alloc] initWithText:@"14点" textStyle:x.labelTextStyle];
}else if (i== 192){
newLabel=[[CPTAxisLabel alloc] initWithText:@"16点" textStyle:x.labelTextStyle];
}else if (i== 216){
newLabel=[[CPTAxisLabel alloc] initWithText:@"18点" textStyle:x.labelTextStyle];
}else if (i== 240){
newLabel=[[CPTAxisLabel alloc] initWithText:@"20点" textStyle:x.labelTextStyle];
}else if (i== 264){
newLabel=[[CPTAxisLabel alloc] initWithText:@"22点" textStyle:x.labelTextStyle];
}else if (i== 288){
newLabel=[[CPTAxisLabel alloc] initWithText:@"24点" textStyle:x.labelTextStyle];
}
else{
newLabel=[[CPTAxisLabel alloc] initWithText:@"" textStyle:x.labelTextStyle];
}
newLabel.tickLocation=[[NSNumber numberWithInt:i] decimalValue];
newLabel.offset=x.labelOffset+x.majorTickLength;
[labelArray addObject:newLabel];
[newLabel release];
}
x.title = @"时间";
x.axisLabels=[NSSet setWithArray:labelArray];