Jamie
Jamie

Reputation: 5122

Core Plot x-axis Labels Overlapping/CPTLabellingPolicyAutomatic Won't Show Dates

I have a Core Plot graph in an iPhone app and want to limit the number of labels on the x-axis. Currently I am using the CPTLabelingPolicyNone and showing NSDates (displaying like: Jan 2) on the x-axis without a problem, other than the fact that once there are more than 10 or so entries, they start overlapping each other. I really want there to be more like 7 or 8 entries max on the x-axis.

I have read that I can use CPTAxisLabelingPolicyAutomatic and set the preferredNumberOfTicks to what I want. The problem is when I do that, I don't get dates on my x-axis anymore, I get decimal numbers like 0.0, 0.1, etc. I'm not an expert at this, so I'm not really sure why I am not getting dates anymore. Could anyone shed some insight on this?

Thanks.

Upvotes: 0

Views: 845

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

Use a CPTTimeFormatter:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease];
timeFormatter.referenceDate = refDate;
axis.labelFormatter = timeFormatter;

Several of the Core Plot example apps demonstrate this feature including the Plot Gallery and DatePlot.

Upvotes: 1

Related Questions