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

时间:2011-05-15 12:48:06

标签: iphone xcode graph core-plot

我想知道如何创建分组条形图?我尝试了,但我只有一个条形图。

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

谢谢

-(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];
}

1 个答案:

答案 0 :(得分:1)

您从未创建过第二个绘图对象 - 您的代码只是将相同的一个添加到图形中两次。您还需要确保为第二个图使用不同的barOffset。如果您不使用垃圾收集,请注意您的内存管理。你问题中的代码有几处内存泄漏。