开发者

grouped bar graph example using core plot iphone?

开发者 https://www.devze.com 2023-03-05 20:07 出处:网络
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 .

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消