如何创建定制的固定宽度色谱柱/ x-标签?
目标:生成每个数据固定宽度的月份列。
问题:列宽根据#data点数而不同。因此,由于缺乏特定月份的数据,一些标签聚集在一起
(例如,'JanFeb'对'Jan ...... Feb')。如何创建定制的固定宽度色谱柱/ x-标签?
以下代码片段会创建新的标签/月份,但不能控制静态放置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设置为
CPTAxisLabelingPolicyFixedIntervalx轴上有很多数字(1.0 - x.0),而使用我自己的NSString标签。
也试过: a)改变plotSpace.xRange;和 b)改变x.majorIntervalLength。
偏好:自定义标签:CPTAxisLabelingPolicyFixedInterval
附:我假设扩大精简栏(用较少的数据)只会绘制一个推断到下一个月的下一个数据点;而无需填写任何缺失的数据点以等于邻近月份的点数。
============================
解决方案基于评论:
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length: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个字元个月的固定分配:四月,五月,六月,七月,八月]。为5个滴答地点。
... from the plot_gallery_ios example。
您可以使用CPTAxisLabelingPolicyFixedInterval
标签政策和labelFormatter
创建月份标签。 Core Plot提供了两种不同的日期格式化器,可能有所帮助:CPTCalendarFormatter和CPTTimeFormatter。您选择哪一个取决于您如何编码数据集中的日期值。
这就是我所做的: –