Has anyone worked out how to manually add labels into a Core-Plot (iPhone) graph?
-
21-09-2019 - |
Question
I have a simply bar graph created for my iPhone application however I want to manually add the x-axis labels. Has anyone worked out a way to do this?
I found the CPXYAxisSet.xAxis.axisLabels property however when I create an NSSet and assign it using:
axisSet.xAxis.axisLabelingPolicy = CPAxisLabelingPolicyNone;
NSSet *labels = [[NSSet alloc] initWithObjects:@"year 1", @"year 2" @"year 3", nil];
axisSet.xAxis.axisLabels = labels;
I get a:
*** Terminating app due to uncaught exception 'CALayerInvalid', reason: 'expecting model layer not copy: year 1'
error.
Anyone have a solution?
Many thanks.
Solution
I believe this has been addressed in the Core Plot mailing list. The axisLabels property takes in a set of CPAxisLabel objects (a descendant of CALayer), not NSStrings, which is why you're getting the above exception.
To create a CPAxisLabel for each of your custom labels, use code similar to the following:
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: customText];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.textStyle = x.axisLabelTextStyle;
newLabel.offset = x.axisLabelOffset + x.majorTickLength;
EDIT (1/18/2010): An example of this is now present in the Core Plot iPhone test application, under the bar chart tab.