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);
}