0

我有一个应用程序需要显示未知数量的图表,具体取决于连接到 iPad 的传感器数量。我有 X 个NSArray包含 XNSNumber的 s 正在实时更新;图表需要与数据一起更新(即实时)。

当我使用Core Plot时会出现我的问题。如何动态创建多个图表?我是 Objective-C 的新手,所以也许这只是我的一个问题。

如果我创建一个图表和添加数据所需的相应委托方法?有人可以概述如何执行此操作吗?

谢谢。

4

2 回答 2

2

您可以根据需要创建任意数量的图表。为每个人创建一个托管视图,然后根据需要进行布置。托管视图是 的子类,UIView因此您可以使用所有标准布局技术和容器视图。

根据您想要实现的外观,另一种方法是使用一个包含多个图的图表。正常创建每个并使用-addPlot:将它们添加到图表中。

您的所有地块都可以共享一个数据源,尽管它们不是必须的。将identifier每个图上的 设置为唯一值。在数据源方法中,检查identifier参数plot并使用它来决定返回什么数据。

于 2012-09-10T22:58:32.910 回答
0

如果您想将多个图表添加到一个视图中,此代码将很有帮助。您可以通过调用简单地添加图表:

CGRect 矩形 = CGRectMake(160, 99, 864, 223); plot1 = [self getGraphWithRect:rect identifier:@"1"];

//
//  ViewController.m
//  sense01
//
//  Created by Morten Ydefeldt on 8/16/12.
//  Copyright (c) 2012 ydefeldt. All rights reserved.
//

#import "ViewController.h"
#import "SenseManager.h"
#import "Sensor.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize statusIndicator;
@synthesize values;
@synthesize recordButton;
@synthesize plot;

-(void)senseIsConnected:(BOOL)status
{
    if(status)
    {
        statusIndicator.text = @"Connected";
        [recordButton setImage:[UIImage imageNamed:@"record_inactive.png"] forState:UIControlStateNormal];
    }
    else
    {
        statusIndicator.text = @"Disconnected";
        [recordButton setImage:[UIImage imageNamed:@"record_notactive.png"] forState:UIControlStateNormal];

    }

}

- (void)newDataFromSensor:(id)sensor{

    for(id key in sensor) {
        id value = [sensor objectForKey:key];

        //textView.text = [value description];

        [self pointReceived:[NSNumber numberWithInt:[[value getRealWorldData] intValue]]];
    }}



-(CGPoint)plotSpace:(CPTPlotSpace *)space willDisplaceBy:(CGPoint)proposedDisplacementVector
{

    return CGPointMake(proposedDisplacementVector.x, 0);
}

-(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate
{
    if (coordinate == CPTCoordinateY) {
        newRange = ((CPTXYPlotSpace*)space).yRange;
    }
    return newRange;


}

-(void)viewDidAppear:(BOOL)animated{

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1].CGColor, (id)[UIColor colorWithRed:0.05 green:0.05 blue:0.05 alpha:1].CGColor, nil];
    [self.view.layer insertSublayer:gradient atIndex:0];
}


- (void)viewDidLoad
{

    [super viewDidLoad];
    manager = [[SenseManager alloc] init];
    manager.delegate  = self;
    [manager openConnection];


    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = CGRectMake(0, 0, 1024, 99);
    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1].CGColor, (id)[UIColor colorWithRed:0.05 green:0.05 blue:0.05 alpha:1].CGColor, nil];
    [self.view.layer insertSublayer:gradient atIndex:0];

    CGRect rect = CGRectMake(160, 99, 864, 223);
    plot1 = [self getGraphWithRect:rect identifier:@"1"];

    rect = CGRectMake(160, 322, 864, 223);
    plot2 = [self getGraphWithRect:rect identifier:@"2"];

    rect = CGRectMake(160, 545, 864, 223);
    plot3 = [self getGraphWithRect:rect identifier:@"3"];


    NSMutableArray *ContentArray = [NSMutableArray arrayWithCapacity:200];
    self.values = ContentArray;
}

-(CPTXYGraph *)getGraphWithRect:(CGRect)rect identifier:(NSString*)identifier{


    plot = [[CPTXYGraph alloc] initWithFrame:rect];
    CPTGraphHostingView *layerHostingView = [[CPTGraphHostingView alloc] initWithFrame:rect];
    layerHostingView.collapsesLayers = NO;
    layerHostingView.hostedGraph = plot;


    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace* )plot.defaultPlotSpace;
    plotSpace.allowsUserInteraction = YES;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(0.0) length: CPTDecimalFromDouble(60.0)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation: CPTDecimalFromDouble(0.0) length: CPTDecimalFromDouble(50.0)];

    CPTScatterPlot *line = [[CPTScatterPlot alloc]init];
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];

    //Fill colors
    float c= 0.2;
    plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:c green:c blue:c alpha:1]];
    plot.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:c green:c blue:c alpha:1]];
    plot.plotAreaFrame.borderLineStyle = nil;

    //Plot border
    lineStyle.lineColor = [CPTColor grayColor];
    lineStyle.lineWidth = 1.2;
    plot.borderLineStyle = lineStyle;

    //Plot padding
    plot.paddingBottom = 5;
    plot.paddingLeft = 5;
    plot.paddingRight = 5;
    plot.paddingTop = 5;

    //axis range
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)plot.axisSet;
    CPTXYAxis *y = axisSet.yAxis;
    CPTXYAxis *x = axisSet.xAxis;
    x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
    y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");

    y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;

    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:15.0];
    y.axisConstraints = [CPTConstraints constraintWithLowerOffset:25.0];

    y.minorTickLength = 0;
    y.majorTickLength = 0;
    x.minorTickLength = 0;
    x.majorTickLength = 0;

    //axis theming
    c = 0.4;
    lineStyle.lineColor = [CPTColor colorWithComponentRed:c green:c blue:c alpha:1];
    lineStyle.lineWidth = 0.7f;
    y.axisLineStyle = lineStyle;
    x.axisLineStyle = lineStyle;


    //label theming
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color = [CPTColor whiteColor];
    textStyle.fontName = @"Helvetica-Bold";
    x.labelTextStyle = textStyle;
    y.labelTextStyle = textStyle;

    //grid theming
    c = 0.3;
    lineStyle.lineColor = [CPTColor colorWithComponentRed:c green:c blue:c alpha:1];
    lineStyle.lineWidth = 0.1f;
    x.minorGridLineStyle = lineStyle;
    y.minorGridLineStyle = lineStyle;


    //curve theming
    lineStyle.miterLimit = 1.0f;
    lineStyle.lineWidth = 1.5f;
    lineStyle.lineColor = [CPTColor whiteColor];
    line.dataLineStyle = lineStyle;
    line.identifier = identifier;
    line.dataSource = self;


    //plotsymbol
    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    CPTGradient *blueGradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithComponentRed:62.0f/255.0f green:112.0/255.0f blue:184.0f/255.0f alpha:1] endingColor:[CPTColor colorWithComponentRed:43.0f/255.0f green:63.0/255.0f blue:153.0f/255.0f alpha:1]];
    lineStyle.lineColor = [CPTColor whiteColor];
    lineStyle.lineWidth = 1.0f;
    plotSymbol.lineStyle = lineStyle;
    plotSymbol.fill = [CPTFill fillWithGradient:blueGradient];
    plotSymbol.size = CGSizeMake(11.0, 11.0);
    line.plotSymbol = plotSymbol;

    [plot addPlot:line];

    [self.view addSubview:layerHostingView];

    return plot;



}

#pragma mark

-(void)pointReceived:(NSNumber *) point{

    [self.values addObject:point];

    self.statusIndicator.text = [NSString stringWithFormat:@"%@", point];
    self.statusIndicator.text = [NSString stringWithFormat:@"%d", [self.values count]];

    CPTPlot *first = [plot plotWithIdentifier:@"2"];
    [first insertDataAtIndex:self.values.count-1 numberOfRecords:1];

    CPTPlot *secondPlot = [plot plotWithIdentifier:@"3"];
    [secondPlot insertDataAtIndex:self.values.count-1 numberOfRecords:1];

    NSLog(@"LOL");

}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{

    return [values count];

}


-(NSNumber *)numberForPlot:(CPTPlot*)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
    if (fieldEnum == CPTScatterPlotFieldX){
        return [NSNumber numberWithInteger:index];

    }else{
        return [self yValueForIndex:index];
    }

}

-(NSNumber *) yValueForIndex:(NSUInteger)index{

    return [self.values objectAtIndex:index];

}



- (void)viewDidUnload
{
    statusIndicator = nil;
    recordButton = nil;
    [self setRecordButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void) portStatusChanged
{

}

- (void) readBytesAvailable:(UInt32)length
{



//    myString = [NSMutableString stringWithString:@""];
//    int bytesRead = [rscMgr read:rxBuffer Length:length];
//    for (int i = 0; i < bytesRead; i++) {
//        [myString appendString:[NSString stringWithFormat:@"%c",((char *)rxBuffer)[i]]];
//    }
//    textView.text = myString;
//    //textView.text = [NSString stringWithFormat:@"%@%@",textView.text,myString];
//    //    NSArray *arrayValues = [myString componentsSeparatedByString:@","];
//    //    textView.text = [arrayValues objectAtIndex:1];
//    //    statusIndicator.text = [NSString stringWithFormat:@"%i",[arrayValues count]];
//    //  arrayValues = nil;

}

-(IBAction)Write:(id)sender{

    [self pointReceived:[NSNumber numberWithInt:20]];

}

- (IBAction)recordButton:(id)sender {



    if (record) {
        record = NO;
        [recordButton setImage:[UIImage imageNamed:@"record_inactive.png"] forState:UIControlStateNormal];
        [manager activateDataFromSense:NO];
    }
    else{

        [recordButton setImage:[UIImage imageNamed:@"record_active.png"] forState:UIControlStateNormal];
        [manager activateDataFromSense:YES];
        record = YES;
    }

}


@end
于 2012-09-11T21:19:42.823 回答