【发布时间】: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