使用核心图 iPhone 的分组条形图示例?
我想知道如何创建分组条形图?我尝试过,但我只得到了单个条形图。
下面是我的代码,任何人都告诉我出了什么错误。
谢谢
-(void)viewDidLoad {
[super viewDidLoad];
[self generateDataSamples];
double xAxisStart = 0;
double xAxisLength = [samples count];
double yAxisStart = 0;
double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue];
NSLog(@"xAxisLength===%f",xAxisLength);
NSLog(@"yAxisLength===%f",yAxisLength);
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hostingView];
CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame:self.view.bounds];
hostingView.hostedGraph = graph;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(xAxisStart)
length:CPDecimalFromDouble(xAxisLength+1)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(yAxisStart)
length:CPDecimalFromDouble(yAxisLength)];
graph.plotAreaFrame.borderLineStyle = nil;
graph.plotAreaFrame.cornerRadius = 0.0f;
// Paddings
graph.paddingLeft = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingBottom = 0.0f;
graph.plotAreaFrame.paddingLeft = 70.0;
graph.plotAreaFrame.paddingTop = 20.0;
graph.plotAreaFrame.paddingRight = 20.0;
graph.plotAreaFrame.paddingBottom = 80.0;
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPDecimalFromString(@"1");
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Y Axis";
y.titleOffset = 45.0f;
y.titleLocation = CPDecimalFromFloat(3.0f);
//first bar
CPBarPlot *plot = [[CPBarPlot alloc] initWithFrame:CGRectZero];
plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0)
length:CPDecimalFromDouble(xAxisLength)];
CPFill* cpFill = [[CPFill alloc] initWithColor:[CPColor greenColor]];
plot.barOffset = CPDecimalFromFloat(0.25f);
plot.fill = cpFill;
plot.dataSource = self;
plot.identifier=@"plot1";
[graph addPlot:plot toPlotSpace:plotSpace];
//second bar
plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(1.0)
length:CPDecimalFromDouble(xAxisLength)];
CPFill* cpFill1 = [[CPFill alloc] initWithColor:[CPColor lightGrayColor]];
plot.fill = cpFill1;
plot.dataSource = self;
plot.identifier=@"plot2";
[graph addPlot:plot toPlotSpace:plotSpace];
[graph addPlot:plot];
[plot release];
[graph release];
[hostingView release];
}
I want to know how to create grouped bar graph? I tried but I got only single bar graph.
Below, is my code any one tell me what is the mistake .
Thank you
-(void)viewDidLoad {
[super viewDidLoad];
[self generateDataSamples];
double xAxisStart = 0;
double xAxisLength = [samples count];
double yAxisStart = 0;
double yAxisLength = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue];
NSLog(@"xAxisLength===%f",xAxisLength);
NSLog(@"yAxisLength===%f",yAxisLength);
CPGraphHostingView *hostingView = [[CPGraphHostingView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hostingView];
CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame:self.view.bounds];
hostingView.hostedGraph = graph;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(xAxisStart)
length:CPDecimalFromDouble(xAxisLength+1)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(yAxisStart)
length:CPDecimalFromDouble(yAxisLength)];
graph.plotAreaFrame.borderLineStyle = nil;
graph.plotAreaFrame.cornerRadius = 0.0f;
// Paddings
graph.paddingLeft = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingTop = 0.0f;
graph.paddingBottom = 0.0f;
graph.plotAreaFrame.paddingLeft = 70.0;
graph.plotAreaFrame.paddingTop = 20.0;
graph.plotAreaFrame.paddingRight = 20.0;
graph.plotAreaFrame.paddingBottom = 80.0;
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPDecimalFromString(@"1");
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
y.title = @"Y Axis";
y.titleOffset = 45.0f;
y.titleLocation = CPDecimalFromFloat(3.0f);
//first bar
CPBarPlot *plot = [[CPBarPlot alloc] initWithFrame:CGRectZero];
plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(0.0)
length:CPDecimalFromDouble(xAxisLength)];
CPFill* cpFill = [[CPFill alloc] initWithColor:[CPColor greenColor]];
plot.barOffset = CPDecimalFromFloat(0.25f);
plot.fill = cpFill;
plot.dataSource = self;
plot.identifier=@"plot1";
[graph addPlot:plot toPlotSpace:plotSpace];
//second bar
plot.plotRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromDouble(1.0)
length:CPDecimalFromDouble(xAxisLength)];
CPFill* cpFill1 = [[CPFill alloc] initWithColor:[CPColor lightGrayColor]];
plot.fill = cpFill1;
plot.dataSource = self;
plot.identifier=@"plot2";
[graph addPlot:plot toPlotSpace:plotSpace];
[graph addPlot:plot];
[plot release];
[graph release];
[hostingView release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从未创建过第二个绘图对象——您的代码只是将同一个绘图对象添加到图表中两次。您还需要确保对第二个图使用不同的
barOffset
。如果您不使用垃圾收集,请注意您的内存管理。您问题中的代码存在多个内存泄漏。You never created a second plot object--your code just adds the same one to the graph twice. You also need to make sure to use a different
barOffset
for the second plot. If you're not using garbage collection, watch your memory management. The code in your question has several memory leaks.