iOSç¨ã°ã©ãæç»ã©ã¤ãã©ãªCorePlotè¶ å ¥é
移転ãã¾ãã â
"); // ãªãã¤ã¬ã¯ã setTimeout(function redirect(){ location.href = url; } , 5000); // canonical ã®æ¸ãæã var link = document.getElementsByTagName("link")[0]; link.href = url; -->iOSï¼ã¨ãOSXï¼ã§ã°ã©ããæ¸ããCorePlotãããã¼ããªãã·ã§ã³ã¨ããããããã£ã¦ç ©éã ãã©ããã¯ãã¦ããã¦ç°¡æ½ã«ã¾ã¨ãã¦ã¿ããã¨æãã¾ãã
ã¾ãã¯å°å ¥ç·¨
CocoaPods使ãã¾ãããã
pod 'CorePlot'
ããã®$ pod install
ã§ããã¾ãã
æ£å¸å³ãæãç·ã°ã©ã
ãµã³ãã«ã¨ãã¦ã°ã©ãã¯2ã¤æ¸ãã¾ãããAããªãã·ã§ã³ãã§ããã ãçç¥ããã»ããBããªãã·ã§ã³ãããã¤ãä»ãã¦ã¿ãã»ãã§ããé åã«20åã®ã©ã³ãã ãªæ´æ°å¤ãå ¥ããé åã®ã¤ã³ããã¯ã¹ã横軸ããã®å¤ã縦軸ã«ã¨ã£ãæãç·ã®ã°ã©ããæç»ãã¾ããã
ã¡ããã¨ä½¿ãã«ã¯ã°ã©ãç¨ã®UIViewã¯ã©ã¹ãä½ã£ã¦ã©ããããã»ããããã¨æãã¾ãããä»åã¯ãããé¢åãªã®ã§ViewControllerã«ç´æ¥æ¸ãã¾ãï¼
æ£å¸å³ãè¼ãããViewControllerãä½ã£ã¦ãããã¼ã«ã¯ãããã³ã«ã®æå®ã¨ããããç¨ãã¼ã¿ç½®ãå ´ãæ¸ãå ãã¾ãããObjective-Cã®Arrayã¯ã¡ãã£ã¨é¢åãªã®ã§ããããªããC++ã®ã³ã¼ããªã®ã§ViewControllerã®æ¡å¼µåã¯.mm
ã«ãã¦ããå¿
è¦ãããã¾ãã
#import <UIKit/UIKit.h> #import <CorePlot/CorePlot-CocoaTouch.h> #import <vector> @interface ScatterViewController : UIViewController<CPTPlotDataSource> { std::vector<int> _dataA; std::vector<int> _dataB; } @end
次ã«å®è£ ãã¡ã¤ã«ã®ã»ãã
ã°ã©ãé åããªãã·ã§ã³ãªã©ã®åæè¨å®ã¯viewDidLoad
ã§è¡ãã¾ããã
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // CorePlotã®ã»ããã¢ãã [self setupPlotA]; [self setupPlotB]; }
ã°ã©ãA
次ã«ã°ã©ãAç¨ã®ã»ããã¢ããã§ãã
- (void)setupPlotA { CGRect rect = CGRectMake(0, 100, 320, 100); CPTGraphHostingView* hostingView = [[CPTGraphHostingView alloc] initWithFrame:rect]; CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:rect]; hostingView.hostedGraph = graph; CPTScatterPlot* plot = [[CPTScatterPlot alloc] init]; plot.identifier = @"A"; plot.dataSource = self; [graph addPlot:plot]; CPTXYPlotSpace* space = (CPTXYPlotSpace*)graph.defaultPlotSpace; space.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)]; space.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(19)]; [self.view addSubview:hostingView]; // ããããç¨ãã¼ã¿ã®ã»ããã¢ãã _dataA.clear(); for(int i=0; i<20; ++i){ _dataA.push_back(rand()%100); } }
ããã¤ãCorePlotã®ã¯ã©ã¹ãç»å ´ãã¾ããã
- CPTGraphHostingView
ã°ã©ããæç»ãããUIViewã§è¦ªã®Viewã«addSubview
ããã¾ã - CPTGraph
ã°ã©ãã®ã¡ã¿ãã¼ã¿ï¼çãªãã®ã§ããã°ã©ãå ¨ä½ã管çãã¦ããã¤ã¡ã¼ã¸ - CPTScatterPlot
ãã¼ã¿ãã©ã®ããã«å¦çãããï¼æç»ãããï¼ã¨ãããã¨ãæ±ã£ã¦ããã¯ã©ã¹ï¼ã¨ããã¤ã¡ã¼ã¸ï¼
大éæã«è¨ãã¨ãCPTScatterPlot
ãCPTGraph
ã«ã»ãããã¦ãCPTGraph
ãCPTGraphHostingView
ã«ã»ããããã®CPTGraphHostingVIew
ãaddSubview
ããã¨ããæµãã§ãã
ã°ã©ãB
ã°ã©ãBã§ã¯ãªãã·ã§ã³ãããã¤ãè¨å®ãã¾ãããæ¬å½ã¯ãã£ã¨ããããã®ãªãã·ã§ã³ãããã®ã§ãããææ¡ããããªãããé©å½ã«ããã¤ãé¸ãã§ããã£ã¦ãã¾ãã
- (void)setupPlotB { CGRect rect = CGRectMake(0, 210, 320, 200); CPTGraphHostingView* hostingView = [[CPTGraphHostingView alloc] initWithFrame:rect]; CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:rect]; hostingView.hostedGraph = graph; CPTScatterPlot* plot = [[CPTScatterPlot alloc] init]; plot.identifier = @"B"; plot.dataSource = self; [graph addPlot:plot]; CPTXYPlotSpace* space = (CPTXYPlotSpace*)graph.defaultPlotSpace; space.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)]; space.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(19)]; [self.view addSubview:hostingView]; // ããããç¨ãã¼ã¿ã®ã»ããã¢ãã _dataB.clear(); for(int i=0; i<20; ++i){ _dataB.push_back(rand()%50); } // ããããè¨å® graph.plotAreaFrame.paddingBottom = 50; graph.plotAreaFrame.paddingLeft = 50; graph.plotAreaFrame.paddingTop = 10; graph.plotAreaFrame.paddingRight = 10; // 軸ã®è¨å® CPTXYAxisSet* axisSet = (CPTXYAxisSet*)graph.axisSet; CPTMutableLineStyle* style = [CPTMutableLineStyle lineStyle]; style.lineColor = [CPTColor orangeColor]; style.lineWidth = 2; axisSet.xAxis.axisLineStyle = style; axisSet.yAxis.axisLineStyle = style; CPTXYAxis* xAxis = axisSet.xAxis; xAxis.majorIntervalLength = CPTDecimalFromInt(5); NSNumberFormatter* tickFormatter = [[NSNumberFormatter alloc] init]; [tickFormatter setGeneratesDecimalNumbers:NO]; [tickFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; xAxis.labelFormatter = tickFormatter; xAxis.title = @"X軸"; CPTXYAxis* yAxis = axisSet.yAxis; yAxis.majorIntervalLength = CPTDecimalFromInt(20); yAxis.title = @"Y軸"; // ããããã®è¨å® plot.dataLineStyle = nil; plot.plotSymbol = [CPTPlotSymbol diamondPlotSymbol]; }
ãããé·ããªã£ã¦ãã¾ãã®ããããã¾ãããæ£ç´ãããã¯çµæ§é¢åã§ãã
DataSourceã®ã¡ã½ãã
ããã»ã©ã¾ã§ã®ã³ã¼ãã§ã¯ãèå¿ã®ã°ã©ãã«è¼ããããã®ãã¼ã¿ã¯ã»ã¨ãã©ç»å ´ãã¾ãããããããã©ã³ãã ãªå¤ãvectorã«è¿½å ããããããCorePlotã§ã¯ãã¼ã¿ãè¿ãã¡ã½ããã使ãã¾ããTableViewã¿ãããªæãã§ã
numberForPlot
ããããç¨ã®å¤ã§ãScatterPlotã®å ´åã¯idxã«å¯¾å¿ããXã®å¤ã¾ãã¯Yã®å¤ãè¿ãã¾ãnumberOfRecordsForPlat
ããããã®é ç®æ°ãè¿ãã¦ããã¾ã
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx { NSNumber* num; switch (fieldEnum) { case CPTScatterPlotFieldX: num = [NSNumber numberWithInt:idx]; break; case CPTScatterPlotFieldY: // plot.identifierã§åå²ãããã¨ã§ã2種é¡ä»¥ä¸ã®ã°ã©ããåãDataSourceã¯ã©ã¹ã§æ¸ããã¨ãã§ãã if([(NSString*)plot.identifier isEqualToString:@"A"]){ num = [NSNumber numberWithInt:_dataA[idx]]; }else{ num = [NSNumber numberWithInt:_dataB[idx]]; } break; } return num; } - (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { if([(NSString*)plot.identifier isEqualToString:@"A"]){ return (int)_dataA.size(); } return (int)_dataB.size(); }
ä»åã®ä¾ã§ã¯ã°ã©ãAãBã¨ãã2ã¤ã®ã°ã©ããç»å ´ãã¾ãããããã¯ã©ã¡ããåãDataSourceã¡ã½ããã使ããã¨ã«ãªãã®ã§ãplot.identifier
ã使ã£ã¦ã©ã¡ãã®ã°ã©ãã®ããã®æ
å ±ãè¿ãã¨ããªã®ããå¤å¥ãã¦å¤ãè¿ãã¦ãã¾ãã
åã°ã©ã
ãããªæãã®ã°ã©ããæ¸ãã¾ãã
ãããã¼ãã¡ã¤ã«
ãããã¼ã¯ãããªæããåºæ¬ã¯ä¼¼éã£ã¦ãã¾ãã
#import <UIKit/UIKit.h> #import <CorePlot/CorePlot-CocoaTouch.h> #import <vector> @interface PieViewController : UIViewController<CPTPieChartDataSource,CPTPieChartDelegate> { std::vector<float> _data; } @end
å®è£ ãã¡ã¤ã«
å®è£
ã¯ãããªæãã§ããCPTScatterPlot
ã®å¤ããã«CPTPieChart
ãç»å ´ãï¼ã©ã¡ããåã親ã¯ã©ã¹ãæã¡ã¾ãï¼ãããã«é¢é£ããè¨å®ãå¿
è¦ã¨ãªã£ã¦ãã¾ãã
ã¾ããCPTTheme
ã¨ããæ¦å¿µããã£ã¦ãã°ã©ãã®ãã¸ã¥ã¢ã«ãããã¤ãã®ãã¼ãã«åºã¥ãã¦åãæ¿ãããã¨ãã§ãã¾ãã
- (void)setupChartA { CGRect rect = CGRectMake(10, 40, 300, 300); CPTGraphHostingView* hostingView = [[CPTGraphHostingView alloc] initWithFrame:rect]; CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:rect]; graph.axisSet = nil; hostingView.hostedGraph = graph; [self.view addSubview:hostingView]; CPTTheme* theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; [graph applyTheme:theme]; CPTPieChart* plot = [[CPTPieChart alloc] init]; plot.identifier = @"PIE"; plot.dataSource = self; plot.delegate = self; [graph addPlot:plot]; plot.pieRadius = 100; _data.clear(); _data.push_back(50); _data.push_back(20); _data.push_back(15); _data.push_back(3); _data.push_back(12); }
ãã¼ã¿ã½ã¼ã¹
åã°ã©ãã®å ´åã¯2次å ã§ã¯ãªãã®ã§è¿ãå¤ã¯å ´ååãã®å¿ è¦ã¯ããã¾ããã
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx { return [NSNumber numberWithFloat:_data[idx]]; }
æ£ã°ã©ã
ãããªã°ã©ããæ¸ãã¾ãã使ã£ããã¼ããæªãã®ãããã¡ããã¡ãã·ã³ãã«ã ãªãã
é·ããªã£ãã®ã§ã½ã¼ã¹ã¯çç¥ãã¾ãã以ä¸ã«ãµã³ãã«ã¢ããªä¸å¼ãããã®ã§ã©ãã¼ã xoyip/CorePlotExample