【原創(chuàng)】Core Plot自定義Axis Label
字體:
大
中
小 發(fā)表日期:2011-09-27 16:50 評論:0 點擊:4106
Core Plot是支持Mac OS X和iOS的開源圖形框架,可生成各類柱狀圖、線型圖、餅圖。默認(rèn)的坐標(biāo)顯示為數(shù)值,但可以使用自定義的NumberFormatter來顯示各種坐標(biāo)標(biāo)簽。
自定義NumberFormatter,初始化時輸入包含自定義標(biāo)簽的數(shù)組(這里是一組日期),重寫stringForObjectValue方法,這樣就可以根據(jù)索引值返回所需的自定義標(biāo)簽了。
#import
@interface ChartDateFormatter : NSNumberFormatter
{
NSArray *dates;
}
-(id) initWithDates: (NSArray *)inDates;
@end
@implementation ChartDateFormatter
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (id)initWithDates: (NSArray *)inDates
{
if(self = [super init])
{
dates = inDates;
}
return self;
}
- (NSString *)stringForObjectValue: (NSDecimalNumber *)coordinateValue {
return [dates objectAtIndex:coordinateValue.intValue];
}
@end
調(diào)用時,只要把相應(yīng)坐標(biāo)的labelFormatter設(shè)為自定義的NumberFormatter就可以了,坐標(biāo)的值是自定義標(biāo)簽數(shù)組的索引值。
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
CPTXYAxis *y = axisSet.yAxis;
//x.labelingPolicy = CPTAxisLabelingPolicyNone;
CPTConstraints x2Constraints = {CPTConstraintFixed, CPTConstraintFixed};
x.isFloatingAxis = YES;
x.constraints = x2Constraints;
x.majorIntervalLength = CPTDecimalFromString(@"10");
x.minorTicksPerInterval = 4;//每兩個大刻度中有多少個小刻度,5等分的話,這個值為4
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x.labelRotation = M_PI / 4;
x.majorTickLength = 5;
x.minorTickLength = 2;
ChartDateFormatter *myFormatter = [[[ChartDateFormatter alloc] initWithDates:dates] autorelease];
x.labelFormatter = myFormatter;
※ ※ ※ 本文純屬【s_1_586】個人意見,與【鋼之家鋼鐵博客】立場無關(guān).※ ※ ※
 該日志尚無評論! |