【发布时间】:2012-09-27 23:24:41
【问题描述】:
目标:为每个数据生成固定宽度的月份列。
问题:列宽因#data 点而异。因此,一些标签聚集在一起
(例如,'JanFeb' vs 'Jan....Feb'),因为缺少特定月份的数据。
以下代码 sn-p 创建新标签/月,但无法控制在 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
附:我假设扩大细列(数据更少)只会对下个月的下一个数据点进行推断;无需填写任何缺失的数据点以等于相邻月份的一组点。
=============================
基于评论的解决方案:
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 示例。
【问题讨论】: