核心图:在x上显示年度标签

对于我的Core Plot图表中的绘图值,我在x轴上显示基于NSDate的值。 这些价值包含许多年,并且每天都有绘制。

我想每年或每月在x轴上显示标签,但不是每天都显示。 这就是为什么我不能使用Core Plot示例应用程序DatePlot显示的标签方法。

目前我正在手动创建标签(如下面的代码片段所示)。 不幸的是,这个解决方案导致下面的屏幕截图显示的问题(2001年的标签太接近2002年)。

[sortedArray enumerateObjectsUsingBlock:  
   ^(Price *priceItem, NSUInteger idx, BOOL *stop) {

NSDateComponents *yearComponents = 
   [gregorian components:NSYearCalendarUnit 
   fromDate:price.dateTime];
NSDateComponents *monthComponents =
   [gregorian components:NSMonthCalendarUnit 
   fromDate:price.dateTime];       
NSInteger year = [yearComponents year];
NSInteger month = [monthComponents month];
if (month != previousMonth) {
   [minorTickLocations addObject:
   [NSDecimalNumber numberWithUnsignedInteger:idx]];
   previousMonth = month;
}
if (year != previousYear) {
   CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] 
   initWithText: [NSString stringWithFormat:@"%u", year]
   textStyle: axisTitleTextStyle];
   newLabel.tickLocation = CPTDecimalFromUnsignedInteger(idx);
   newLabel.offset = x.labelOffset + x.majorTickLength / 2.0;
   [xAxisLabels addObject:newLabel];
   [majorTickLocations addObject:
       [NSDecimalNumber numberWithUnsignedInteger:idx]];
   previousYear = year;
   }
}];

我想知道是否有更好的方式来使用标准的核心绘图方法对其进行编码。

如果日期格式化程序的行为仅用于显示年( YYYY ),月和年( MMM-YYYY )或仅显示月( MMM ),那就太好了。

编辑

正如Eric在他的文章中所建议的那样,我将日期转换为年份以便能够使用CPTDateFormatter 。 这就是我设置图表的方式(最初,我只想绘制每个日历年的记号):

x.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];
CPTCalendarFormatter *calendarFormatter = [[CPTCalendarFormatter alloc]
   initWithDateFormatter:dateFormatter];
calendarFormatter.referenceDate = [sortedPrices[0] 
   valueForKey:@"dateTime"];
calendarFormatter.referenceCalendarUnit = NSYearCalendarUnit;
x.labelFormatter = calendarFormatter;

这就是我如何将基于初始整数的索引转换为基于“年份”的索引:

- (NSArray*)indexArrayOfDatesArray:(NSArray*)dates
{
NSCalendar *gregorian = [[NSCalendar alloc] 
   initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *startDate = [sortedCommonSharePrices[0] valueForKey:@"dateTime"];
NSDateComponents *yearComponents = [gregorian components: NSYearCalendarUnit
   fromDate:startDate];
NSUInteger startYear = yearComponents.year;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:dates.count];
[dates enumerateObjectsUsingBlock:^(NSDate *date, NSUInteger idx, BOOL *stop) {
   NSDateComponents *yearComponents = [gregorian 
        components:NSYearCalendarUnit fromDate:date];
   NSInteger years = [yearComponents year];
   NSUInteger numberOfDaysSinceStartOfYear =
        [gregorian ordinalityOfUnit:NSDayCalendarUnit
         inUnit:NSYearCalendarUnit forDate:date];

   NSUInteger totalNumberOfDaysInYear = 365;

   double fractionOfYear = (double)numberOfDaysSinceStartOfYear /
      (double)totalNumberOfDaysInYear + (double)years - (double)startYear;

   [array addObject:[NSNumber numberWithDouble:fractionOfYear]];
}];    
return array;
}

这似乎工作正常,但似乎是很多代码(关于改进代码的任何想法?)。

缩放和平移时(使用plotSpace:willChangePlotRangeTo )我想将x轴标签设置为新的日期范围(根据新绘图范围中的天数)但是,我得到的刻度数和错误标签数不正确请参考下面的截图)。

我将如何设置标签参数来获得标签的权利?

谢谢你的帮助!

来自plotSpace:willChangePlotRangeTo代码片段plotSpace:willChangePlotRangeTo

NSDateComponents *components = [gregorian components:NSDayCalendarUnit 
   fromDate:newStartDate toDate:newEndDate options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
CPTCalendarFormatter *calendarFormatter = 
   [[CPTCalendarFormatter alloc] initWithDateFormatter:dateFormatter];
calendarFormatter.referenceDate = [sortedPrices[0] valueForKey:@"dateTime"];

if (components.day < 365) {
   dateFormatter.dateFormat = @"ddd-MMM-yyyy";
   calendarFormatter.referenceCalendarUnit = NSDayCalendarUnit;
} else if (components.day < 365 * 2) {
   dateFormatter.dateFormat = @"MMM yyyy";
   calendarFormatter.referenceCalendarUnit = NSYearCalendarUnit;
} else if (components.day < 365 * 4) {
   dateFormatter.dateFormat = @"qqq yyyy";
   calendarFormatter.referenceCalendarUnit = NSYearCalendarUnit;
} else {
   dateFormatter.dateFormat = @"yyyy";
   calendarFormatter.referenceCalendarUnit = NSYearCalendarUnit;
}
CPTXYAxis *x = axisSet.xAxis;
x.preferredNumberOfMajorTicks = 10;
x.majorIntervalLength = CPTDecimalFromDouble(0.1);
x.minorTicksPerInterval = 1;
x.labelFormatter = calendarFormatter;

错误的蜱和标签


提供您自己的标签政策,如下所示:

...
CPTXYAxis *x = axisSet.xAxis;
int totalNumberOfYearsToShow = 3;
int startYear = 2010;
int i = 0;
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:totalRecords];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:totalRecords];

while (i < totalNumberOfYearsToShow)
{
    NSNumber *xData = [NSNumber numberWithInt:(i + startYear)];
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[xData stringValue] textStyle:x.labelTextStyle];
    label.tickLocation = CPTDecimalFromInt([xData intValue]);
    label.offset = x.majorTickLength;

    if (label)
    {
        [xLabels addObject:label];
        [xLocations addObject:xData];
    }

    i ++;
}


x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
...

希望有所帮助。


我无法使用Core Plot示例应用程序DatePlot中显示的标签方法。

为什么不? 使用固定间隔标签策略,只需将majorIntervalLength设置为等同于一年,然后使用CPTCalendarFormatterCPTTimeFormatter格式化标签。

链接地址: http://www.djcxy.com/p/61035.html

上一篇: Core Plot: Show yearly labels on the x

下一篇: Calculating Date Time Axis minimum, maximum and major minor tick interval