我正在努力使用coreplot显示实时图表。我每2秒获取一次数据并将其传递给coreplot数据源,但我没有看到该行。我注意到x轴(时间轴)正在正确更新。有任何建议请
//listen to notification center and update the plot
- (id) init
{
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveDownloadDataNotification:)
name:@"Data"
object:nil];
NSLog(@" downalodData notification listening");
return self;
}
-(void)constructGraph1
{
// Create graph from a theme
graph1 = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [ CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph1 applyTheme:theme];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = graph1;
graph1.plotAreaFrame.masksToBorder = NO;
//create x-axis time format
NSTimeInterval refTimeInterval = [[NSDate date]
timeIntervalSinceReferenceDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss a"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph1.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(refTimeInterval-(5*oneMin)) length:CPTDecimalFromFloat(2*oneMin)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(100.0)];
plotSpace.globalYRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(100.0f)];
//Line Styles
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 1.0f;
lineStyle.lineColor = [CPTColor blueColor];
boundLinePlot.dataLineStyle = lineStyle;
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph1.axisSet;
// X-Axes formatting
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromInteger(30);
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x.minorTicksPerInterval = 0;
x.labelOffset=0;
x.labelFormatter = timeFormatter;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.title=@"Time Axis";
x.labelFormatter = timeFormatter;
//Y-Axes formatting
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInteger(10);
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(refTimeInterval-oneMin);
y.minorTicksPerInterval = 5;
y.labelOffset = 0;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.preferredNumberOfMajorTicks = 10;
y.minorTickLineStyle = nil;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(100)];
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y.title=@"Mbps";
// Create a plot that uses the data source method
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init] ;
dataSourceLinePlot.identifier = @"graph Plot";
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph1 addPlot:dataSourceLinePlot];
graphDatatoPlot = [[NSMutableArray alloc]init];
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
return graphDatatoPlot.count;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if ( [plot.identifier isEqual:@"graph Plot"] ){
NSDecimalNumber *num = [[graphDatatoPlot objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
return 0;
}
- (void) receiveDownloadDataNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"Data"])
{
NSLog(@"received item %@",[notification object]);
[graphDatatoPlot addObject:[notification object]];
[graph1 reloadData];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph1.defaultPlotSpace;
NSTimeInterval aTimeInterval = [[NSDate date]timeIntervalSinceReferenceDate];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(aTimeInterval-(5*oneMin)) length:CPTDecimalFromInt(2*oneMin)];
NSLog(@"plot count %i",graphDatatoPlot.count);
}
}
只是为了测试我向通知中心发送一个20的常量值。时间被解雇好了,x轴被更新了,但我没看到实际的线在20分。
-(void)testTimer:(NSTimer *)Timer {
NSTimeInterval timeNow= [NSDate timeIntervalSinceReferenceDate];
NSMutableDictionary *newData= [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSDecimalNumber numberWithInt:timeNow], [NSNumber numberWithInt:CPTScatterPlotFieldX],
[NSDecimalNumber numberWithInt:20], [NSNumber numberWithInt:CPTScatterPlotFieldX],nil
] ;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Data" object:newData];
NSLog(@"newdata is %@ %f",newData,timeNow);
}
答案 0 :(得分:1)
数据点位于xRange
之外。每个数据点都被赋予当前时间的x值,但xRange
被设置为在当前时间之前五分钟开始的范围,长度为两分钟,这意味着它在当前时间之前三分钟结束。