从昨天开始我就在这个问题上打破了我的头,无法让它工作!我得到的只是一条普通的 x 轴线、y 轴线(甚至没有刻度线或标签!)和图表标题!请帮忙!看似简单,却又如此难以捉摸!
我正在绘制服务器内存使用的最后 5 个值。这些值大约是每分钟(有时是两分钟一次)。例如,我可能想要显示的一组最后 5 个值是这样的:
日期(与)内存使用情况(字节)
2013-05-02 11:50:33 +0000 - 157888512.000000
2013-05-02 11:51:33 +0000 - 157839360.000000
2013-05-02 11:52:14 +0000 - 157888512.000000
2013-05-02 11:56:36 +0000 - 157863936.000000
2013-05-02 11:57:48 +0000 - 157888512.000000
我每分钟刷新一次这个图表。
'numberForPlot'、'numberOfRecordsForPlot' 似乎被调用了,但图中没有看到/可见的绘图。
我的图表是用这种方法初始化的:
-(void) initializeGraph {
NSLog(@"View_Visualizer.initializeGraph");
if ((self.graph == nil) || (self.graph == NULL)) {
self.graphHostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostView.bounds];
graph.paddingLeft = 10.0;
graph.paddingRight = 10.0;
graph.paddingTop = 10.0;
graph.paddingBottom = 10.0;
graph.title = @"Processor Usage";
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
NSTimeInterval xLow = 0.0f;
NSTimeInterval oneMin = 60.0f;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow)
length:CPTDecimalFromFloat(oneMin*6.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(6)];
// plotting style is set to line plots
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
NSTimeInterval oneMin = 60.0f;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.graph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneMin);
axisSet.xAxis.minorTicksPerInterval = 0;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorTickLength = 7.0f;
//axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneMin);
//axisSet.xAxis.majorTickLineStyle = lineStyle;
//axisSet.xAxis.minorTickLineStyle = lineStyle;
//axisSet.xAxis.axisLineStyle = lineStyle;
//axisSet.xAxis.minorTickLength = 5.0f;
//axisSet.xAxis.majorTickLength = 5.0f;
//axisSet.xAxis.labelOffset = 3.0f;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Example date format : 2013-05-02 11:23:16 +0000
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
//dateFormatter.dateStyle = kCFDateFormatterShortStyle;
//dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = [NSDate dateWithTimeIntervalSince1970:([[NSDate date] timeIntervalSince1970] - (6*oneMin))];
axisSet.xAxis.labelFormatter = timeFormatter;
//axisSet.yAxis.majorIntervalLength = [NSDecimal decimalNumberWithString:@"5"];
//axisSet.yAxis.minorTicksPerInterval = 4;
//axisSet.yAxis.majorTickLineStyle = lineStyle;
//axisSet.yAxis.minorTickLineStyle = lineStyle;
//axisSet.yAxis.axisLineStyle = lineStyle;
//axisSet.yAxis.minorTickLength = 5.0f;
//axisSet.yAxis.majorTickLength = 5.0f;
//axisSet.yAxis.labelOffset = 3.0f;
CPTScatterPlot *xSquaredPlot = [[CPTScatterPlot alloc]
initWithFrame:self.graph.bounds];
xSquaredPlot.identifier = @"X Squared Plot";
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle = dataLineStyle;
xSquaredPlot.dataSource = self;
[self.graph addPlot:xSquaredPlot];
self.graphHostView.allowPinchScaling = NO;
self.graphHostView.hostedGraph = self.graph;
[self.view addSubview:self.graphHostView];
}
}
#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [dictHealth count];
}
-(NSNumber *) numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index {
Health_MemUse *healthData = (Health_MemUse *) [dictHealth objectForKey: [self.arrayHealthKeys objectAtIndex:index]];
if ( fieldEnum == CPTScatterPlotFieldX ) {
return [NSDecimalNumber numberWithFloat:[healthData.currDate timeIntervalSince1970]];
/*
return [NSNumber numberWithDouble:([healthData.currDate timeIntervalSince1970] + [[NSTimeZone localTimeZone] secondsFromGMT])];
*/
} else {
NSLog(@"Plot memuse %f on date %@ for index %d",
healthData.memUse, healthData.currDate, index);
return [NSNumber numberWithFloat:(healthData.memUse/(1024*1024*1024))]; // convert bytes to GB
}
}
请帮忙!我意识到在设置轴的范围时我可能大多是错误的,但我尝试了 Googl'ing、Stackoverflow'ing 并尝试了几种变体,但都是徒劳的。