0

目标:为每个数据生成固定宽度的月份列。
问题:每个#data 点的列宽不同。
因此,由于缺少特定月份的数据,一些标签会聚集在一起 (例如,'JanFeb' vs 'Jan....Feb')。

以下代码片段创建新标签/月,但无法控制在 x 轴上静态放置标签;因此,如果该特定月份的数据项很少(点数较少),则某些标签可能会合并:

    for (PerformanceDataItem *item in perfDataArray) {
    if (![item.month isEqualToString:currentMonth]) {
        currentMonth = item.month;
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:item.month textStyle:x.labelTextStyle];
        newLabel.tickLocation = [[NSDecimalNumber numberWithInt:index] decimalValue];
        newLabel.offset = 2;
        [customLabels addObject:newLabel];
        [newLabel release]; 
    }
    ++index;    
} // end for().

x.axisLineStyle = nil;
x.majorTickLineStyle = nil;
x.minorTickLineStyle = nil;
x.labelingPolicy =  CPTAxisLabelingPolicyNone;


NSSet* customlabelSet = [NSSet setWithArray:customLabels];
x.axisLabels = customlabelSet; 

问题:如何创建自定义的固定宽度列集来绘制数据?

...仅将 x.labelingPolicy 设置为

CPTAxisLabelingPolicyFixedInterval
与使用我自己的 NSString 标签相比,使用大量数字 (1.0 - x.0) 会使 x 轴混乱。

还尝试过: a)更改 plotSpace.xRange;b) 更改 x.majorIntervalLength。

偏好:自定义标签:CPTAxisLabelingPolicyFixedInterval

PS我假设加宽细列(数据更少)只会推断下个月的下一个数据点;无需填写任何缺失的数据点以等于相邻月份的一组点。

==============================

基于评论的解决方案:

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) 长度:CPTDecimalFromUnsignedInteger(100)];

axisFixedInterval.majorIntervalLength = CPTDecimalFromDouble(25.0); NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

// Set begining date to April 1, 2012:
[dateComponents setMonth:4];
[dateComponents setDay:1];
[dateComponents setYear:2012];
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *refDate = [gregorian dateFromComponents:dateComponents];
[dateComponents release];
[gregorian release];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterMediumStyle;
[dateFormatter setDateFormat:@"MMM"];


CPTCalendarFormatter *calFormatter = [[[CPTCalendarFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
calFormatter.referenceDate = refDate;
calFormatter.referenceCalendarUnit = kCFCalendarUnitMonth;

axisFixedInterval.labelFormatter = calFormatter;

[dateFormatter release];

...这会给我一个 3 个字符月的固定分布:[Apr, May, Jun, Jul, Aug]; 对于 5 个刻度位置。

...来自 plot_gallery_ios 示例。

4

1 回答 1

0

You could use the CPTAxisLabelingPolicyFixedInterval labeling policy and a labelFormatter that creates month labels. Core Plot provides two different date formatters that might help: CPTCalendarFormatter and CPTTimeFormatter. Which one you choose depends on how you're coding the date values in your dataset.

于 2012-09-29T12:09:47.393 回答