使用核心图 iPhone 的分组条形图示例?

发布于 2024-11-07 08:05:54 字数 2704 浏览 0 评论 0原文

我想知道如何创建分组条形图?我尝试过,但我只得到了单个条形图。

下面是我的代码,任何人都告诉我出了什么错误。

谢谢

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2024-11-14 08:05:54

您从未创建过第二个绘图对象——您的代码只是将同一个绘图对象添加到图表中两次。您还需要确保对第二个图使用不同的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文