Tag: CorePlot
platform :ios pod 'CorePlot'
$ pod install
#import "CorePlot-CocoaTouch.h"
@interface CPTTestAppBarChartController : UIViewController<CPTPlotDataSource>
@interface DemoFirstViewController () @property(nonatomic, strong) CPTXYGraph *barChart; @end
- (void)viewDidLoad { [super viewDidLoad]; //viewを置き換える必要がある CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] init]; hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling self.view = hostingView; // Create barChart from theme self.barChart = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; [self.barChart applyTheme:theme]; hostingView.hostedGraph = self.barChart; // Border self.barChart.plotAreaFrame.borderLineStyle = nil; self.barChart.plotAreaFrame.cornerRadius = 0.0f; // Paddings self.barChart.paddingLeft = 0.0f; self.barChart.paddingRight = 0.0f; self.barChart.paddingTop = 0.0f; self.barChart.paddingBottom = 0.0f; self.barChart.plotAreaFrame.paddingLeft = 70.0; self.barChart.plotAreaFrame.paddingTop = 20.0; self.barChart.plotAreaFrame.paddingRight = 20.0; self.barChart.plotAreaFrame.paddingBottom = 80.0; // Graph title self.barChart.title = @"Graph Title\nLine 2"; CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle]; textStyle.color = [CPTColor grayColor]; textStyle.fontSize = 16.0f; textStyle.textAlignment = CPTTextAlignmentCenter; self.barChart.titleTextStyle = textStyle; self.barChart.titleDisplacement = CGPointMake(0.0f, -20.0f); self.barChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop; // Add plot space for horizontal bar charts CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.barChart.defaultPlotSpace; plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(300.0f)]; plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(16.0f)]; CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.barChart.axisSet; CPTXYAxis *x = axisSet.xAxis; x.axisLineStyle = nil; x.majorTickLineStyle = nil; x.minorTickLineStyle = nil; x.majorIntervalLength = CPTDecimalFromString(@"5"); x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); x.title = @"X Axis"; x.titleLocation = CPTDecimalFromFloat(7.5f); x.titleOffset = 55.0f; // Define some custom labels for the data elements x.labelRotation = M_PI / 4; x.labelingPolicy = CPTAxisLabelingPolicyNone; NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil]; NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil]; NSUInteger labelLocation = 0; NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; for ( NSNumber *tickLocation in customTickLocations ) { CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; newLabel.tickLocation = [tickLocation decimalValue]; newLabel.offset = x.labelOffset + x.majorTickLength; newLabel.rotation = M_PI / 4; [customLabels addObject:newLabel]; } x.axisLabels = [NSSet setWithArray:customLabels]; CPTXYAxis *y = axisSet.yAxis; y.axisLineStyle = nil; y.majorTickLineStyle = nil; y.minorTickLineStyle = nil; y.majorIntervalLength = CPTDecimalFromString(@"50"); y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); y.title = @"Y Axis"; y.titleOffset = 45.0f; y.titleLocation = CPTDecimalFromFloat(150.0f); // First bar plot CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO]; barPlot.baseValue = CPTDecimalFromString(@"0"); barPlot.dataSource = self; barPlot.barOffset = CPTDecimalFromFloat(-0.25f); barPlot.identifier = @"Bar Plot 1"; [self.barChart addPlot:barPlot toPlotSpace:plotSpace]; // Second bar plot barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO]; barPlot.dataSource = self; barPlot.baseValue = CPTDecimalFromString(@"0"); barPlot.barOffset = CPTDecimalFromFloat(0.25f); barPlot.barCornerRadius = 2.0f; barPlot.identifier = @"Bar Plot 2"; [self.barChart addPlot:barPlot toPlotSpace:plotSpace]; }
#pragma mark - #pragma mark Plot Data Source Methods -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { return 16; } -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { NSDecimalNumber *num = nil; if ( [plot isKindOfClass:[CPTBarPlot class]] ) { switch ( fieldEnum ) { case CPTBarPlotFieldBarLocation: num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index]; break; case CPTBarPlotFieldBarTip: num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index + 1) * (index + 1)]; if ( [plot.identifier isEqual:@"Bar Plot 2"] ) { num = [num decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:@"10"]]; } break; } } return num; }
// Border self.barChart.plotAreaFrame.borderLineStyle = nil; self.barChart.plotAreaFrame.cornerRadius = 0.0f; // Paddings self.barChart.paddingLeft = 0.0f; self.barChart.paddingRight = 0.0f; self.barChart.paddingTop = 0.0f; self.barChart.paddingBottom = 0.0f; self.barChart.plotAreaFrame.paddingLeft = 70.0; self.barChart.plotAreaFrame.paddingTop = 20.0; self.barChart.plotAreaFrame.paddingRight = 20.0; self.barChart.plotAreaFrame.paddingBottom = 80.0; // Graph title self.barChart.title = @"Graph Title\nLine 2"; CPTMutableTextStyle *textStyle = [CPTTextStyle textStyle]; textStyle.color = [CPTColor grayColor]; textStyle.fontSize = 16.0f; textStyle.textAlignment = CPTTextAlignmentCenter; self.barChart.titleTextStyle = textStyle; self.barChart.titleDisplacement = CGPointMake(0.0f, -20.0f); self.barChart.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.barChart.defaultPlotSpace; //Y軸の範囲(0〜300) plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(300.0f)]; //X軸の範囲(0〜16) plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(16.0f)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.barChart.axisSet; CPTXYAxis *x = axisSet.xAxis; x.axisLineStyle = nil; x.majorTickLineStyle = nil; x.minorTickLineStyle = nil; x.majorIntervalLength = CPTDecimalFromString(@"5"); x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); x.title = @"X Axis"; x.titleLocation = CPTDecimalFromFloat(7.5f); x.titleOffset = 55.0f; // Define some custom labels for the data elements x.labelRotation = M_PI / 4; x.labelingPolicy = CPTAxisLabelingPolicyNone; NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1], [NSDecimalNumber numberWithInt:5], [NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:15], nil]; NSArray *xAxisLabels = [NSArray arrayWithObjects:@"Label A", @"Label B", @"Label C", @"Label D", @"Label E", nil]; NSUInteger labelLocation = 0; NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]]; for ( NSNumber *tickLocation in customTickLocations ) { CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle]; newLabel.tickLocation = [tickLocation decimalValue]; newLabel.offset = x.labelOffset + x.majorTickLength; newLabel.rotation = M_PI / 4; [customLabels addObject:newLabel]; } x.axisLabels = [NSSet setWithArray:customLabels];
CPTXYAxis *y = axisSet.yAxis; y.axisLineStyle = nil; y.majorTickLineStyle = nil; y.minorTickLineStyle = nil; y.majorIntervalLength = CPTDecimalFromString(@"50"); y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0"); y.title = @"Y Axis"; y.titleOffset = 45.0f; y.titleLocation = CPTDecimalFromFloat(150.0f);
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { return 16; }
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { NSDecimalNumber *num = nil; // if ( [plot isKindOfClass:[CPTBarPlot class]] ) { switch ( fieldEnum ) { case CPTBarPlotFieldBarLocation: //X方向の位置を返す num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index]; break; case CPTBarPlotFieldBarTip: //棒の高さを返す(サンプルでは機械的に計算してかえしているだけ) num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:(index + 1) * (index + 1)]; if ( [plot.identifier isEqual:@"Bar Plot 2"] ) { num = [num decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:@"10"]]; } break; } } return num; }