I’m using Core Plot 0.9 in my first iPad project. I need to create an horizontal bar plot, and to create UIButtons on its left aligned with the center of each bar, one button per bar, cf the attached screenshot.
My setup is pretty simple:
I have a custom UIView subclass that acts as a container.
Inside of it, I have 2 subviews: a CPTGraphHostingView and a UIView to contain my UIButtons.
I host a CPTXYGraph in my CPTGraphHostingView and I assign some paddings values.
In my custom class, I have a method that will create the buttons once the dataSource has been set:
for (int i = 0; i < nbRecords; i++) {
// grab the bar location and create a plot point for it
NSNumber* loc = [dataSource numberForPlot:plot field:CPTBarPlotFieldBarLocation recordIndex:i];
double plotPoint[2] = {0, [loc doubleValue]};
// Convert the plot point to plot area space coordinates (I guess ;))
CGPoint viewPoint = [graph.defaultPlotSpace plotAreaViewPointForDoublePrecisionPlotPoint:plotPoint];
// Convert the view point to the button container layer coordinate system
CGPoint p = [graph convertPoint:viewPoint fromLayer:graph.plotAreaFrame.plotArea];
// Create the button
CGRect rect = CGRectMake(0, p.y - 11, 100, 22);
UIButton *btn = [[UIButton alloc] initWithFrame:rect];
[btn setTitle:@"test" forState:UIControlStateNormal];
[buttonView addSubview:btn];
[self addSubview:btn];
[array addObject:btn];
[btn release];
}
The buttons are created but the alignment is not good… I noticed that if I set all the graph paddings to 0, then the buttons are aligned (but of course I need the padding).
Am I missing something or a step in the conversion of the points ? I am a bit confused on how to convert a plotAreaPoint coordinates to any other UIView …!
Ok I figured the problem myself. In fact, I was adding the buttons before the graph had a chance to layout itself. Calling the graph’s method
-layoutIfNeeded:just before the loop solved the alignment problem