【问题标题】:Consistently show a label at the origin using a date/time and Core Plot使用日期/时间和核心图始终在原点显示标签
【发布时间】:2012-02-08 18:49:31
【问题描述】:

我正在使用核心图在 iPhone/iPad 上显示最近的服务器指标。我已经正确地绘制了它,并且看起来大约 95% 是我想要的。我唯一的抱怨是日期和时间显示为 x 轴间隔,而且我似乎无法控制该间隔的开始点。

例如,我目前的主要间隔是每半小时打印一次日期/时间。这意味着我有时间在 x 轴上打印如下: 2/8 12:00pm 2/8 12:30pm 2/8 1:00pm 我想让图表的原点(当前时间)成为这个间隔的开始 (例如 2/8 11:37am 2/8 12:07pm 2/8 12:37pm)。这可能吗?我可以定义某种区间起点吗?

下面是相关代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M/d h:mm a"];
if([[NSTimeZone systemTimeZone] isDaylightSavingTime]) {
    NSLog(@"DLS");
}

[NSTimeZone resetSystemTimeZone];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
axisSet.xAxis.labelFormatter = timeFormatter;

CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;

axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"50"] decimalValue];
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;



axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"50"] decimalValue];
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;

CPTScatterPlot *PowerPlot = [[CPTScatterPlot alloc] initWithFrame:self.view.bounds];
PowerPlot.identifier = @"PowerPlot";

CPTMutableLineStyle *PowerLine = [[CPTMutableLineStyle alloc] init];
[PowerLine setLineColor:[CPTColor redColor]];
[PowerLine setLineWidth:1.0f];
[PowerPlot setDataLineStyle:PowerLine];

PowerPlot.dataSource = self;
[MainGraph addPlot:PowerPlot];

CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
PowerPlot.plotSymbol = greenCirclePlotSymbol;  


 (void)RefreshGraph {
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)MainGraph.defaultPlotSpace;
NSRange YRange = [self getActiveGraphYRange];
NSInteger YIncrement = YRange.length/10;



long TimeInterval = 0;
switch (CurrentPeriod) {
    case PERIOD_HOUR:
        TimeInterval = OneHour;
        break;
    case PERIOD_DAY:
        TimeInterval = OneDay;
        break;
    case PERIOD_WEEK:
        TimeInterval = OneWeek;
        break;
    case PERIOD_MONTH:
        TimeInterval = OneMonth;
        break;
    default:
        break;
}

long StartTime =  [[NSDate date] timeIntervalSinceReferenceDate];
if([DataPoints count] > 0) {
StartTime = [[(DataPoint *)[DataPoints objectAtIndex:0] Timestamp] timeIntervalSinceReferenceDate];
}
NSInteger XIncrement = TimeInterval/10;
NSInteger XMajorIncrement = XIncrement*5;

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)MainGraph.axisSet;
axisSet.xAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",XMajorIncrement]] decimalValue];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(StartTime - XIncrement- (XIncrement/3)) length:CPTDecimalFromFloat(TimeInterval + (2*XIncrement))];
}
else{
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(StartTime - XIncrement) length:CPTDecimalFromFloat(TimeInterval + (2*XIncrement))];
}
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(StartTime - XIncrement/5);

//axisSet.yAxis.

if([TypeControl selectedSegmentIndex] == 0) { //Power
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-40) length:CPTDecimalFromFloat(YRange.length+40)];
        axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(YRange.length+15)];
        axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",YIncrement]] decimalValue];

    }
    else{
        plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-15) length:CPTDecimalFromFloat(YRange.length+15)];
        axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(YRange.length+15)];
        axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",YIncrement]] decimalValue];
    }
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);

} else { //Temp
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        plotSpace.yRange = 
        [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-4) length:CPTDecimalFromFloat(35)];

        axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(35)];
        axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"4"] decimalValue];
    }
    else{
        plotSpace.yRange = 
        [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2) length:CPTDecimalFromFloat(35)];

        axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(35)];
        axisSet.yAxis.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
    }
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);
}    

[MainGraph reloadData];    
}

【问题讨论】:

    标签: ios datetime label core-plot


    【解决方案1】:

    由于您使用的是默认标签策略 (CPTAxisLabelingPolicyFixedInterval),因此您可以使用轴上的 labelingOrigin 属性来控制标签的开始位置。将其设置为绘图范围的起始location

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多