在x轴和y轴上的coreplot iOS中的动态数据 [英] dynamic data in coreplot iOS on x-axis and y-axis
本文介绍了在x轴和y轴上的coreplot iOS中的动态数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在coreplot iOS中获得x轴/ y轴上的动态数据:目前使用演示代码这里:每一列下面我需要年度x轴,它本身是动态和y轴收入金额。这是否动态?
//
// CPDStockPriceStore.m
// CorePlotDemo
//
// NB:从Yahoo! Finance获取的价格数据:
// http://finance.yahoo.com/q/hp?s=AAPL
// http:/ /finance.yahoo.com/q/hp?s=GOOG
// http://finance.yahoo.com/q/hp?s=MSFT
//
//创建通过Steve Baranski在5/4/12。
//版权所有(c)2012 komorka technology,llc。版权所有。
//
#importCPDStockPriceStore.h
@interface CPDStockPriceStore()
@end
@implementation CPDStockPriceStore
#pragma mark - 类方法
+(CPDStockPriceStore *)sharedInstance
{
static CPDStockPriceStore * sharedInstance ;
static dispatch_once_t once;
dispatch_once(& once,^ {
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark - API方法
- (NSArray *)tickerSymbols
{
static NSArray * symbols = nil;
if(!symbols)
{
symbols = [NSArray arrayWithObjects:
@AAPL,
@GOOG,
@MSFT ,
nil];
}
返回符号;
}
- (NSArray *)dailyPortfolioPrices
{
static NSArray * prices = nil;
if(!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:582.13],
[NSDecimalNumber numberWithFloat:604.43],
[NSDecimalNumber numberWithFloat:32.01],
nil];
}
返回价格;
}
- (NSArray *)datesInWeek
{
static NSArray * dates = nil;
if(!dates)
{
dates = [NSArray arrayWithObjects:
@4/23,
@4/24,
@4/25,
@4/26,
@4/27,
nil];
}
返回日期;
}
- (NSArray *)weeklyPrices:(NSString *)tickerSymbol
{
if([CPDTickerSymbolAAPL isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self weeklyAaplPrices];
}
else if([CPDTickerSymbolGOOG isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self weeklyGoogPrices];
}
else if([CPDTickerSymbolMSFT isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self weeklyMsftPrices];
}
return [NSArray array];
}
- (NSArray *)datesInMonth
{
static NSArray * dates = nil;
if(!dates)
{
dates = [NSArray arrayWithObjects:
@2,
@3,
@4 ,
@5,
@9,
@10,
@11,
@12,
@13,
@16,
@17,
@18,
@19,
@20
@23,
@24,
@25,
@26,
@27,
@ 30,
nil];
}
返回日期;
}
- (NSArray *)monthlyPrices:(NSString *)tickerSymbol
{
if([CPDTickerSymbolAAPL isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self monthlyAaplPrices];
}
else if([CPDTickerSymbolGOOG isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self monthlyGoogPrices];
}
else if([CPDTickerSymbolMSFT isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self monthlyMsftPrices];
}
return [NSArray array];
}
#pragma mark - 私人行为
- (NSArray *)weeklyAaplPrices
{
static NSArray * prices = nil;
if(!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:571.70],
[NSDecimalNumber numberWithFloat:560.28],
[NSDecimalNumber numberWithFloat:610.00],
[NSDecimalNumber numberWithFloat:607.70],
[NSDecimalNumber numberWithFloat:603.00],
nil];
}
返回价格;
}
- (NSArray *)weeklyGoogPrices
{
static NSArray * prices = nil;
if(!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:597.60],
[NSDecimalNumber numberWithFloat:601.27],
[NSDecimalNumber numberWithFloat:609.72],
[NSDecimalNumber numberWithFloat:615.47],
[NSDecimalNumber numberWithFloat:614.98],
nil];
}
返回价格;
}
- (NSArray *)weeklyMsftPrices
{
static NSArray * prices = nil;
if(!prices)
{
price = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:32.12],
[NSDecimalNumber numberWithFloat:31.92],
[NSDecimalNumber numberWithFloat:32.20],
[NSDecimalNumber numberWithFloat:32.11],
[NSDecimalNumber numberWithFloat:31.98],
nil];
}
返回价格;
}
- (NSArray *)monthlyAaplPrices
{
static NSArray * prices = nil;
if(!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:618.63],
[NSDecimalNumber numberWithFloat:629.32],
[NSDecimalNumber numberWithFloat:624.31],
[NSDecimalNumber numberWithFloat:633.68],
[NSDecimalNumber numberWithFloat:636.23],
[NSDecimalNumber numberWithFloat:628.44],
[NSDecimalNumber numberWithFloat:626.20] ,
[NSDecimalNumber numberWithFloat:622.77],
[NSDecimalNumber numberWithFloat:605.23],
[NSDecimalNumber numberWithFloat:580.13],
[NSDecimalNumber numberWithFloat:609.70] NSDecimalNumber numberWithFloat:571.70],
[NSDecimalNumber numberWithFloat:572.98],
[NSDecimalNumber numberWithFloat:571.70],
[NSDecimalNumber numberWithFloat:
[NSDecimalNumber numberWithFloat:610.00],
[NSDecimalNumber numberWithFloat:607.70],
[NSDecimalNumber numberWithFloat:603.00],
[NSDecimalNumber numberWithFloat:583.98],
nil] ;
}
返回价格;
}
- (NSArray *)monthlyGoogPrices
{
static NSArray * prices = nil;
if(!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:646.92],
[NSDecimalNumber numberWithFloat:642.62],
[NSDecimalNumber numberWithFloat:635.15],
[NSDecimalNumber numberWithFloat:632.32],
[NSDecimalNumber numberWithFloat:630.84],
[NSDecimalNumber numberWithFloat:626.86],
[NSDecimalNumber numberWithFloat:635.96] ,
[NSDecimalNumber numberWithFloat:659.01],
[NSDecimalNumber numberWithFloat:624.60],
[NSDecimalNumber numberWithFloat:606.07],
[NSDecimalNumber numberWithFloat:609.57] NSDecimalNumber numberWithFloat:607.45],
[NSDecimalNumber numberWithFloat:599.30],
[NSDecimalNumber numberWithFloat:596.06],
[NSDecimalNumber numberWithFloat:597.60]
[NSDecimalNumber numberWithFloat:604.75],
[NSDecimalNumber numberWithFloat:615.47],
[NSDecimalNumber numberWithFloat:614.98],
[NSDecimalNumber numberWithFloat:604.85],
nil] ;
}
返回价格;
}
- (NSArray *)monthlyMsftPrices
{
static NSArray * prices = nil;
if(!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:32.29],
[NSDecimalNumber numberWithFloat:31.94],
NSDecimalNumber numberWithFloat:31.52],
[NSDecimalNumber numberWithFloat:31.10],
[NSDecimalNumber numberWithFloat:30.47],
[NSDecimalNumber numberWithFloat:30.35] ,
[NSDecimalNumber numberWithFloat:30.98],
[NSDecimalNumber numberWithFloat:30.81],
[NSDecimalNumber numberWithFloat:31.08],
[NSDecimalNumber numberWithFloat:31.44] NSDecimalNumber numberWithFloat:31.14],
[NSDecimalNumber numberWithFloat:31.01],
[NSDecimalNumber numberWithFloat:32.42],
[NSDecimalNumber numberWithFloat:32.12],
[NSDecimalNumber numberWithFloat:31.92]
[NSDecimalNumber numberWithFloat:32.20],
[NSDecimalNumber numberWithFloat:32.11],
[NSDecimalNumber numberWithFloat:31.98],
[NSDecimalNumber numberWithFloat:32.02],
nil] ;
}
返回价格;
}
@end
#importCPDBarGraphViewController.h
@interface CPDBarGraphViewController()
@property(nonatomic,strong)IBOutlet CPTGraphHostingView * hostView;
@property(nonatomic,strong)CPTBarPlot * aaplPlot;
@property(nonatomic,strong)CPTBarPlot * googPlot;
@property(nonatomic,strong)CPTBarPlot * msftPlot;
@property(nonatomic,strong)CPTPlotSpaceAnnotation * priceAnnotation;
- (IBAction)aaplSwitched:(id)sender;
- (IBAction)googSwitched:(id)sender;
- (IBAction)msftSwitched:(id)sender;
- (void)initPlot;
- (void)configureGraph;
- (void)configurePlots;
- (void)configureAxes;
- (void)hideAnnotation:(CPTGraph *)graph;
@end
@implementation CPDBarGraphViewController
CGFloat const CPDBarWidth = 0.25f;
CGFloat const CPDBarInitialX = 0.25f;
@synthesize hostView = hostView_;
@synthesize aaplPlot = aaplPlot_;
@synthesize googPlot = googPlot_;
@synthesize msftPlot = msftPlot_;
@synthesize priceAnnotation = priceAnnotation_;
#pragma mark - 旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
#pragma mark - UIViewController生命周期方法
- (void)viewDidLoad {
[super viewDidLoad];
[self initPlot];
}
#pragma mark - 图表行为
- (void)initPlot {
self.hostView.allowPinchScaling = NO;
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
- (void)configureGraph {
// 1 - 创建图形
CPTGraph * graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds ];
graph.plotAreaFrame.masksToBorder = NO;
self.hostView.hostedGraph = graph;
// 2 - 配置图
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainBlackTheme]];
graph.paddingBottom = 30.0f;
graph.paddingLeft = 30.0f
graph.paddingTop = -1.0f;
graph.paddingRight = -5.0f;
// 3 - 设置样式
CPTMutableTextStyle * titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = @Helvetica-Bold;
titleStyle.fontSize = 16.0f;
// 4 - 设置标题
NSString * title = @投资组合价格:2012年4月23日 - 27日;
graph.title = title;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f,-16.0f);
// 5 - 设置绘图空间
CGFloat xMin = 0.0f;
CGFloat xMax = [[[CPDStockPriceStore sharedInstance] datesInWeek] count];
CGFloat yMin = 0.0f;
CGFloat yMax = 800.0f; //应该根据最大价格动态确定
CPTXYPlotSpace * plotSpace =(CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin)length:CPTDecimalFromFloat(xMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin)length:CPTDecimalFromFloat(yMax)];
}
- (void)configurePlots {
// 1 - 设置三个绘图
self.aaplPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:没有];
self.aaplPlot.identifier = CPDTickerSymbolAAPL;
self.googPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];
self.googPlot.identifier = CPDTickerSymbolGOOG;
self.msftPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
self.msftPlot.identifier = CPDTickerSymbolMSFT;
// 2 - 设置行样式
CPTMutableLineStyle * barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor = [CPTColor lightGrayColor];
barLineStyle.lineWidth = 0.5;
// 3 - 添加图到图
CPTGraph * graph = self.hostView.hostedGraph;
CGFloat barX = CPDBarInitialX;
NSArray * plots = [NSArray arrayWithObjects:self.aaplPlot,self.googPlot,self.msftPlot,nil];
for(CPTBarPlot * plot in plots){
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = CPTDecimalFromDouble(CPDBarWidth);
plot.barOffset = CPTDecimalFromDouble(barX);
plot.lineStyle = barLineStyle;
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
barX + = CPDBarWidth;
}
}
- (void)configureAxes {
// 1 - 配置样式
CPTMutableTextStyle * axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @Helvetica-Bold;
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle * axisLineStyle = [CPTMutableLineStyle lineStyle];
轴LineLtyStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];
// 2 - 获取图形的轴集
CPTXYAxisSet * axisSet =(CPTXYAxisSet *)self.hostView.hostedGraph.axisSet;
// 3 - 配置x轴
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.xAxis.title = @星期几(星期一至星期五);
axisSet.xAxis.titleTextStyle = axisTitleStyle;
axisSet.xAxis.titleOffset = 10.0f;
axisSet.xAxis.axisLineStyle = axisLineStyle;
// 4 - 配置y轴
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.yAxis.title = @Price;
axisSet.yAxis.titleTextStyle = axisTitleStyle;
axisSet.yAxis.titleOffset = 5.0f;
axisSet.yAxis.axisLineStyle = axisLineStyle;
}
- (void)hideAnnotation:(CPTGraph *)graph {
if((graph.plotAreaFrame.plotArea)&&(self.priceAnnotation)){
[graph.plotAreaFrame.plotArea removeAnnotation:self.priceAnnotation];
self.priceAnnotation = nil;
}
}
#pragma mark - CPTPlotDataSource方法
- (NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [[[CPDStockPriceStore sharedInstance ] datesInWeek] count];
}
- (NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
if((fieldEnum == CPTBarPlotFieldBarTip) &&&&&&&&&&&&&" [index]< / [index [< / pt] CPDTickerSymbolAAPL] objectAtIndex:index];
}}
return [NSDecimalNumber numberWithUnsignedInteger:index];
}
#pragma mark - CPTBarPlotDelegate方法
- (void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index {
NSLog (@barWasSelectedAtRecordIndex%d,index);
}
@end
解决方案>
这里有其他教学这可能会帮助你。
检查3.图形的布局,第24行。他动态地创建Y轴,但这一切都取决于你如何制作数据源。
希望它清除:)
how to have dynamic data in x-axis/y-axis in coreplot iOS :Currently using the demo code here:Below every column I need year on x-axis which itself is dynamic and y axis revenue amount .How do I do that dynamically?
//
// CPDStockPriceStore.m
// CorePlotDemo
//
// NB: Price data obtained from Yahoo! Finance:
// http://finance.yahoo.com/q/hp?s=AAPL
// http://finance.yahoo.com/q/hp?s=GOOG
// http://finance.yahoo.com/q/hp?s=MSFT
//
// Created by Steve Baranski on 5/4/12.
// Copyright (c) 2012 komorka technology, llc. All rights reserved.
//
#import "CPDStockPriceStore.h"
@interface CPDStockPriceStore ()
@end
@implementation CPDStockPriceStore
#pragma mark - Class methods
+ (CPDStockPriceStore *)sharedInstance
{
static CPDStockPriceStore *sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark - API methods
- (NSArray *)tickerSymbols
{
static NSArray *symbols = nil;
if (!symbols)
{
symbols = [NSArray arrayWithObjects:
@"AAPL",
@"GOOG",
@"MSFT",
nil];
}
return symbols;
}
- (NSArray *)dailyPortfolioPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:582.13],
[NSDecimalNumber numberWithFloat:604.43],
[NSDecimalNumber numberWithFloat:32.01],
nil];
}
return prices;
}
- (NSArray *)datesInWeek
{
static NSArray *dates = nil;
if (!dates)
{
dates = [NSArray arrayWithObjects:
@"4/23",
@"4/24",
@"4/25",
@"4/26",
@"4/27",
nil];
}
return dates;
}
- (NSArray *)weeklyPrices:(NSString *)tickerSymbol
{
if ([CPDTickerSymbolAAPL isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self weeklyAaplPrices];
}
else if ([CPDTickerSymbolGOOG isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self weeklyGoogPrices];
}
else if ([CPDTickerSymbolMSFT isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self weeklyMsftPrices];
}
return [NSArray array];
}
- (NSArray *)datesInMonth
{
static NSArray *dates = nil;
if (!dates)
{
dates = [NSArray arrayWithObjects:
@"2",
@"3",
@"4",
@"5",
@"9",
@"10",
@"11",
@"12",
@"13",
@"16",
@"17",
@"18",
@"19",
@"20",
@"23",
@"24",
@"25",
@"26",
@"27",
@"30",
nil];
}
return dates;
}
- (NSArray *)monthlyPrices:(NSString *)tickerSymbol
{
if ([CPDTickerSymbolAAPL isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self monthlyAaplPrices];
}
else if ([CPDTickerSymbolGOOG isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self monthlyGoogPrices];
}
else if ([CPDTickerSymbolMSFT isEqualToString:[tickerSymbol uppercaseString]] == YES)
{
return [self monthlyMsftPrices];
}
return [NSArray array];
}
#pragma mark - Private behavior
- (NSArray *)weeklyAaplPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:571.70],
[NSDecimalNumber numberWithFloat:560.28],
[NSDecimalNumber numberWithFloat:610.00],
[NSDecimalNumber numberWithFloat:607.70],
[NSDecimalNumber numberWithFloat:603.00],
nil];
}
return prices;
}
- (NSArray *)weeklyGoogPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:597.60],
[NSDecimalNumber numberWithFloat:601.27],
[NSDecimalNumber numberWithFloat:609.72],
[NSDecimalNumber numberWithFloat:615.47],
[NSDecimalNumber numberWithFloat:614.98],
nil];
}
return prices;
}
- (NSArray *)weeklyMsftPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:32.12],
[NSDecimalNumber numberWithFloat:31.92],
[NSDecimalNumber numberWithFloat:32.20],
[NSDecimalNumber numberWithFloat:32.11],
[NSDecimalNumber numberWithFloat:31.98],
nil];
}
return prices;
}
- (NSArray *)monthlyAaplPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:618.63],
[NSDecimalNumber numberWithFloat:629.32],
[NSDecimalNumber numberWithFloat:624.31],
[NSDecimalNumber numberWithFloat:633.68],
[NSDecimalNumber numberWithFloat:636.23],
[NSDecimalNumber numberWithFloat:628.44],
[NSDecimalNumber numberWithFloat:626.20],
[NSDecimalNumber numberWithFloat:622.77],
[NSDecimalNumber numberWithFloat:605.23],
[NSDecimalNumber numberWithFloat:580.13],
[NSDecimalNumber numberWithFloat:609.70],
[NSDecimalNumber numberWithFloat:608.34],
[NSDecimalNumber numberWithFloat:587.44],
[NSDecimalNumber numberWithFloat:572.98],
[NSDecimalNumber numberWithFloat:571.70],
[NSDecimalNumber numberWithFloat:560.28],
[NSDecimalNumber numberWithFloat:610.00],
[NSDecimalNumber numberWithFloat:607.70],
[NSDecimalNumber numberWithFloat:603.00],
[NSDecimalNumber numberWithFloat:583.98],
nil];
}
return prices;
}
- (NSArray *)monthlyGoogPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:646.92],
[NSDecimalNumber numberWithFloat:642.62],
[NSDecimalNumber numberWithFloat:635.15],
[NSDecimalNumber numberWithFloat:632.32],
[NSDecimalNumber numberWithFloat:630.84],
[NSDecimalNumber numberWithFloat:626.86],
[NSDecimalNumber numberWithFloat:635.96],
[NSDecimalNumber numberWithFloat:651.01],
[NSDecimalNumber numberWithFloat:624.60],
[NSDecimalNumber numberWithFloat:606.07],
[NSDecimalNumber numberWithFloat:609.57],
[NSDecimalNumber numberWithFloat:607.45],
[NSDecimalNumber numberWithFloat:599.30],
[NSDecimalNumber numberWithFloat:596.06],
[NSDecimalNumber numberWithFloat:597.60],
[NSDecimalNumber numberWithFloat:601.27],
[NSDecimalNumber numberWithFloat:609.72],
[NSDecimalNumber numberWithFloat:615.47],
[NSDecimalNumber numberWithFloat:614.98],
[NSDecimalNumber numberWithFloat:604.85],
nil];
}
return prices;
}
- (NSArray *)monthlyMsftPrices
{
static NSArray *prices = nil;
if (!prices)
{
prices = [NSArray arrayWithObjects:
[NSDecimalNumber numberWithFloat:32.29],
[NSDecimalNumber numberWithFloat:31.94],
[NSDecimalNumber numberWithFloat:31.21],
[NSDecimalNumber numberWithFloat:31.52],
[NSDecimalNumber numberWithFloat:31.10],
[NSDecimalNumber numberWithFloat:30.47],
[NSDecimalNumber numberWithFloat:30.35],
[NSDecimalNumber numberWithFloat:30.98],
[NSDecimalNumber numberWithFloat:30.81],
[NSDecimalNumber numberWithFloat:31.08],
[NSDecimalNumber numberWithFloat:31.44],
[NSDecimalNumber numberWithFloat:31.14],
[NSDecimalNumber numberWithFloat:31.01],
[NSDecimalNumber numberWithFloat:32.42],
[NSDecimalNumber numberWithFloat:32.12],
[NSDecimalNumber numberWithFloat:31.92],
[NSDecimalNumber numberWithFloat:32.20],
[NSDecimalNumber numberWithFloat:32.11],
[NSDecimalNumber numberWithFloat:31.98],
[NSDecimalNumber numberWithFloat:32.02],
nil];
}
return prices;
}
@end
#import "CPDBarGraphViewController.h"
@interface CPDBarGraphViewController ()
@property (nonatomic, strong) IBOutlet CPTGraphHostingView *hostView;
@property (nonatomic, strong) CPTBarPlot *aaplPlot;
@property (nonatomic, strong) CPTBarPlot *googPlot;
@property (nonatomic, strong) CPTBarPlot *msftPlot;
@property (nonatomic, strong) CPTPlotSpaceAnnotation *priceAnnotation;
-(IBAction)aaplSwitched:(id)sender;
-(IBAction)googSwitched:(id)sender;
-(IBAction)msftSwitched:(id)sender;
-(void)initPlot;
-(void)configureGraph;
-(void)configurePlots;
-(void)configureAxes;
-(void)hideAnnotation:(CPTGraph *)graph;
@end
@implementation CPDBarGraphViewController
CGFloat const CPDBarWidth = 0.25f;
CGFloat const CPDBarInitialX = 0.25f;
@synthesize hostView = hostView_;
@synthesize aaplPlot = aaplPlot_;
@synthesize googPlot = googPlot_;
@synthesize msftPlot = msftPlot_;
@synthesize priceAnnotation = priceAnnotation_;
#pragma mark - Rotation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
#pragma mark - UIViewController lifecycle methods
-(void)viewDidLoad {
[super viewDidLoad];
[self initPlot];
}
#pragma mark - Chart behavior
-(void)initPlot {
self.hostView.allowPinchScaling = NO;
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
-(void)configureGraph {
// 1 - Create the graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
graph.plotAreaFrame.masksToBorder = NO;
self.hostView.hostedGraph = graph;
// 2 - Configure the graph
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainBlackTheme]];
graph.paddingBottom = 30.0f;
graph.paddingLeft = 30.0f;
graph.paddingTop = -1.0f;
graph.paddingRight = -5.0f;
// 3 - Set up styles
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = @"Helvetica-Bold";
titleStyle.fontSize = 16.0f;
// 4 - Set up title
NSString *title = @"Portfolio Prices: April 23 - 27, 2012";
graph.title = title;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -16.0f);
// 5 - Set up plot space
CGFloat xMin = 0.0f;
CGFloat xMax = [[[CPDStockPriceStore sharedInstance] datesInWeek] count];
CGFloat yMin = 0.0f;
CGFloat yMax = 800.0f; // should determine dynamically based on max price
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];
}
-(void)configurePlots {
// 1 - Set up the three plots
self.aaplPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor redColor] horizontalBars:NO];
self.aaplPlot.identifier = CPDTickerSymbolAAPL;
self.googPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor greenColor] horizontalBars:NO];
self.googPlot.identifier = CPDTickerSymbolGOOG;
self.msftPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
self.msftPlot.identifier = CPDTickerSymbolMSFT;
// 2 - Set up line style
CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor = [CPTColor lightGrayColor];
barLineStyle.lineWidth = 0.5;
// 3 - Add plots to graph
CPTGraph *graph = self.hostView.hostedGraph;
CGFloat barX = CPDBarInitialX;
NSArray *plots = [NSArray arrayWithObjects:self.aaplPlot, self.googPlot, self.msftPlot, nil];
for (CPTBarPlot *plot in plots) {
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = CPTDecimalFromDouble(CPDBarWidth);
plot.barOffset = CPTDecimalFromDouble(barX);
plot.lineStyle = barLineStyle;
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
barX += CPDBarWidth;
}
}
-(void)configureAxes {
// 1 - Configure styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];
// 2 - Get the graph's axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure the x-axis
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.xAxis.title = @"Days of Week (Mon - Fri)";
axisSet.xAxis.titleTextStyle = axisTitleStyle;
axisSet.xAxis.titleOffset = 10.0f;
axisSet.xAxis.axisLineStyle = axisLineStyle;
// 4 - Configure the y-axis
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.yAxis.title = @"Price";
axisSet.yAxis.titleTextStyle = axisTitleStyle;
axisSet.yAxis.titleOffset = 5.0f;
axisSet.yAxis.axisLineStyle = axisLineStyle;
}
-(void)hideAnnotation:(CPTGraph *)graph {
if ((graph.plotAreaFrame.plotArea) && (self.priceAnnotation)) {
[graph.plotAreaFrame.plotArea removeAnnotation:self.priceAnnotation];
self.priceAnnotation = nil;
}
}
#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
return [[[CPDStockPriceStore sharedInstance] datesInWeek] count];
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
if ((fieldEnum == CPTBarPlotFieldBarTip) && (index < [[[CPDStockPriceStore sharedInstance] datesInWeek] count])) {
if ([plot.identifier isEqual:CPDTickerSymbolAAPL]) {
return [[[CPDStockPriceStore sharedInstance] weeklyPrices:CPDTickerSymbolAAPL] objectAtIndex:index];
} }
return [NSDecimalNumber numberWithUnsignedInteger:index];
}
#pragma mark - CPTBarPlotDelegate methods
-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index {
NSLog(@"barWasSelectedAtRecordIndex %d", index);
}
@end
解决方案
There is another tutorial here that might help you.
Check 3. Layout of the graph, line 24. There he makes the Y axis dynamically, but it all depends how you make your datasource.
Hope it clears up :)
这篇关于在x轴和y轴上的coreplot iOS中的动态数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文