0

我正在努力使用 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);

}
4

1 回答 1

1

数据点在xRange. 每个数据点都有一个当前时间的 x 值,但xRange它设置为从当前时间前五分钟开始的范围,长度为两分钟,这意味着它在当前时间前三分钟结束。

于 2013-10-19T01:05:57.760 回答