在iPad上使用Core Plot的多个图表

时间:2012-09-10 15:27:03

标签: iphone objective-c xcode ipad core-plot

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

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

如果我创建图表以及添加数据所需的相应委托方法?有人可以概述一下如何做到这一点吗?

感谢。

2 个答案:

答案 0 :(得分:2)

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

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

您的所有图表都可以共享数据源,尽管他们不需要。将每个绘图上的identifier设置为唯一值。在数据源方法中,检查identifier参数上的plot并使用它来决定要返回的数据。

答案 1 :(得分:0)

  

如果要将多个图表添加到一个图表中,此代码将非常有用   视图。您可以通过调用:

简单地添加图表      

CGRect rect = 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