このコードは、1 つのビューに複数のグラフを追加する場合に役立ちます。次のように呼び出して、グラフを簡単に追加できます。
CGRect rect = CGRectMake(160, 99, 864, 223); plot1 = [self getGraphWithRect:rect 識別子:@"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