CPTScatterPlot Custom Xaxis label overlaps - core-plot

I have created a CPTScatterPlot, with custom Xaxislabels defined.
var axisSet = (CPTXYAxisSet) graph.AxisSet;
// Label x with a fixed interval policy
var x = axisSet.XAxis;
x.LabelingPolicy = CPTAxisLabelingPolicy.None;
x.MinorTicksPerInterval = 4;
x.PreferredNumberOfMajorTicks = 8;
x.MajorGridLineStyle = major;
x.MinorGridLineStyle = minor;
x.Title = "X Axis";
x.TitleOffset = -30;
x.LabelOffset = 15.0f;
NSMutableArray customLabels = new NSMutableArray (dataToPlot.Count);
foreach (PointF pt in dataToPlot) {
string xlabel = pt.X.ToString ().PadLeft (6, '0');
CPTTextStyle tstyle = new CPTTextStyle ();
tstyle.Color = CPTColor.WhiteColor;
tstyle.FontSize = 15;
CPTAxisLabel newLabel = new CPTAxisLabel (xlabel, tstyle);
newLabel.TickLocation = NSDecimalNumber.FromUInt32(uint.Parse(pt.X.ToString())).NSDecimalValue;
newLabel.Offset = 3.0f;
newLabel.Rotation = (float)Math.PI/2f;
customLabels.Add (newLabel);
}
x.AxisLabels = new NSSet(customLabels);
This code works fine, but all the axis labels are overlapped. How to remove the overlapping and display only specific labels in the visible range?
Also how to reload the xaxis when user zooms in or zooms out.
Kindly help, I am struck with this.
Thanks
srrin

There is no automatic label overlap-removal built into Core Plot right now. You'll need to do it yourself based on the size and number of labels. Use a plot space delegate to monitor changes in the plot space and update the tick locations and labels as needed when the plot range changes.

Related

Core Plot: Align both y-axes to have the same zero-point

I have a second y-axis (y2) in my Core Plot project which I have set up as described in the code below.
How can I get both y-axes to have the same zero-point? Currently, the zero-point of each axis is at different heights (in the screen shot below, the y-axis' zero-point equals the y2-value of 8.8%). How can I get Core Plot to plot the zero-point at the same height for both axes?
My code:
CPTXYAxis *y = axisSet.yAxis;
y.coordinate = CPTCoordinateY;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(xAxisStart);
y.axisLineStyle = axisLineStyle;
y.majorIntervalLength = CPTDecimalFromInt(1);
y.majorTickLength = 5;
y.minorTickLength = 0;
y.majorGridLineStyle = axisLineStyle;
CPTXYPlotSpace *relativePlotSpace = [[CPTXYPlotSpace alloc] init];
[graph addPlotSpace:relativePlotSpace];
CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.axisLineStyle = axisLineStyle;
y2.coordinate = CPTCoordinateY;
y2.orthogonalCoordinateDecimal = CPTDecimalFromDouble(xAxisLength);
y2.plotSpace = relativePlotSpace;
y2.separateLayers = NO;
y2.majorTickLineStyle = axisLineStyle;
y2.minorTicksPerInterval = 1;
y2.preferredNumberOfMajorTicks = 8;
y2.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
Edit
Based on Eric's answer below I have added the following code snippet to calculate the range for the y2-axis which should align its zero-point to the y-axis's zero-point:
double yAxisAbsoluteLength =
ABS(plotSpace.yRange.lengthDouble);
double y2AxisAbsoluteLength =
(ABS(relativePlotSpace.yRange.lengthDouble) +
ABS(relativePlotSpace.yRange.locationDouble));
double yAxisZeroPointToLengthRatio =
plotSpace.yRange.locationDouble /
yAxisAbsoluteLength;
double y2AxisLocation = yAxisZeroPointToLengthRatio
*y2AxisAbsoluteLength;
relativePlotSpace.yRange = [CPTPlotRange
plotRangeWithLocation:CPTDecimalFromDouble(y2AxisLocation)
length:CPTDecimalFromDouble(y2AxisAbsoluteLength)];
This seems to work fine if y-values are > 0. However, for y-values < 0, the y2 location is dragged also below the zero-point, which leads to the problem that the calculated y2AxisAbsoluteLength value is no longer sufficient to compensate for the dragging of the y2 location below the zero-point (y2 points with a high values are cut off or not plotted in the chart). My solution would be simply to add the absolute value of the y2 location to the y2AxisAbsoluteLength value, but this would distort the alignment of the two zero points (y and y2).
How would I need to change my calculation to account correctly for positive and negative y-values?
In this situation, I can't use expandRangeByFactor because the multiplication factor is unknown at the time of coding.
Both axes stretch the full height of the plot area, so you can calculate some ratios to determine a range for the second axis.
Example:
Plot range 1: location -100, length 400
This means the zero-point is at a point 25% (100/400) of the length from the range location. If for the second plot range to have a length of 20, you would give it a location of -5, putting 25% of the length below the zero-point.

Core-Plot - preferredNumberOfMajorTicks do not limit the number of Ticks

I want to draw only 10 Majors Ticks on my Y axis. For that I'm using the methode preferredNumberOfMajorTicks = 10.
but as seen on the screenshot, it doesn't really work ...
Here is my configuration for the y axis :
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
//y.majorIntervalLength = CPTDecimalFromDouble(2);
y.preferredNumberOfMajorTicks = 10;
y.minorTicksPerInterval = 5;
y.tickDirection = CPTSignNone;
y.majorTickLineStyle = majorLineStyle;
y.minorTickLineStyle = minorLineStyle;
y.axisLineStyle = majorLineStyle;
y.majorTickLength = 7.0;
y.minorTickLength = 5.0;
y.labelTextStyle = whiteTextStyle;
y.minorTickLabelTextStyle = minorTickBlackTextStyle;
y.titleTextStyle = whiteTextStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:40.0];
Something should over right the methode preferredNumberOfMajorTicks = 10 but I cannot see what ...
The preferredNumberOfMajorTicks property does not apply to the fixed interval labeling policy. See the Core Plot docs for details. You have two options here:
Keep the fixed interval labeling policy and change the majorIntervalLength to a value that will give the desired number of ticks.
Change the labeling policy to CPTAxisLabelingPolicyAutomatic or CPTAxisLabelingPolicyEqualDivisions.

issue with custom label

I think i found a bug similar to issue 104
I have a bar graph. My axis is fixed with constraint and i use padding left on my plot area and custom label for my X axis.
It is import to show the last bars so my xRange starts on the end with the code below
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat([[posicoes lastObject] floatValue]) length:CPTDecimalFromFloat(tipoEscalaGrafico*60*13)];
Everything works perfectly except from the label.
On the print bellow you can see the problem
http://3.bp.blogspot.com/-BLWrnrZ1MZY/T8aMqEc3PzI/AAAAAAAAAG8/sXhIgvuuj_0/s1600/Captura+de+Tela+2012-05-30+às+18.05.01.png
The label doesn't respect the padding.
If i scroll the graph then it respect the padding as the screen bellow
http://1.bp.blogspot.com/-uvFem8xERAo/T8aMmaLRl2I/AAAAAAAAAG0/DZx_UwCcmbA/s1600/Captura+de+Tela+2012-05-30+às+18.05.01+(2).png
I have a class that inherits from CPTXYGraph and i did the method below to add custom labl=els
-(void)setarLabelsEixoX:(NSArray *)labels naPosicao:(NSArray *)posicaoLabels;{
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[labels count]];
for ( NSNumber *tickLocation in posicaoLabels ) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[labels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset;
newLabel.alignment = CPTAlignmentCenter;
[customLabels addObject:newLabel];
}
axisSet.xAxis.majorTickLocations = (NSSet*)posicaoLabels;
x.axisLabels = [NSSet setWithArray:customLabels];
}
I did a little testing with the latest Core Plot code. I don't see an issue. Did you verify that the tick location of the errant label was actually outside the plot range? What version of Core Plot are you using?

Core plot axis labels not visible outside plot area

I'm having some trouble with the axis labels. It seems my custom labels aren't visible outside of the plot area. Looking at the Design Overview for core plot, the axis labels should be outside of the plot area. Any ideas on how I can make them visible outside the plot area?
The code/image below shows how the labels only show within the plot area. If I change the label offset to a positive number, the labels do not show at all. I'm using Core Plot 0.9 with iOS 5.
Thanks in advance!
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
graph = [[theme newGraph] retain];;
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = graph;
graph.paddingLeft = 40.0;
graph.paddingTop = 40.0;
graph.paddingRight = 40.0;
graph.paddingBottom = 40.0;
graph.masksToBorder = NO;
// ... setup axis
axisSet.yAxis.majorIntervalLength = CPTDecimalFromInt(0);
axisSet.yAxis.minorTicksPerInterval = 0;
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
// if this number is negative, the labels show,
// if it is positive, that is outside the plot area, the labels are hidden.
axisSet.yAxis.labelOffset = -20;
// ... setup labels
NSMutableArray *labels = [NSMutableArray array];
for (int i = 0; i < reportData.rowCount; ++i) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"Test Row %i", i] textStyle:labelStyle];
label.tickLocation = CPTDecimalFromInt(i);
label.offset = axisSet.yAxis.labelOffset;
label.alignment = CPTAlignmentLeft;
[labels addObject:label];
[label release];
}
axisSet.yAxis.axisLabels = [NSSet setWithArray:labels];
Set some padding on the plot area frame in addition to or instead of the graph itself.
graph.plotAreaFrame.paddingTop = 20.0;
graph.plotAreaFrame.paddingBottom = 50.0;
graph.plotAreaFrame.paddingLeft = 50.0;
graph.plotAreaFrame.paddingRight = 50.0;
This code is from the axis demo in the Plot Gallery example app.
I was having exactly the same problem and tried every solution I could find online for this with no success.
Finally I got it to work by turning off the border masking, like this:
graph.plotAreaFrame.masksToBorder = NO;

Core Plot, custom labels and tick marks

I am using Core Plot alpha release 0.2 and generating a plot with custom labels. The labels are appearing in the correct location but I have no tick marks. I have tried every combination of these settings:
x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:#"5"] decimalValue];
x.minorTicksPerInterval = 0;
x.majorTickLineStyle = lineStyle;
x.minorTickLineStyle = lineStyle;
x.axisLineStyle = lineStyle;
x.minorTickLength = 5.0f;
x.majorTickLength = 7.0f;
x.tickDirection=CPSignNegative;
I've also added padding to the bottom of the graph using:
graph.plotAreaFrame.paddingBottom=10.0;
which has the effect of moving the X axis 'up' but no tick marks are visible. This seems like it should be pretty simple, generate a tick mark at the location of the custom label. But I don't see any way to do it.
Ideas?
**** Here's the code I'm using to generate the labels and tick marks, with no luck *
for (int i = 0; i < [xAxisLabels count]; i++) {
NSDictionary * labelDict = [xAxisLabels objectAtIndex:i];
NSString *label=[labelDict valueForKey:#"element"];
NSNumber *offset=[labelDict valueForKey:#"x"];
float location=[offset floatValue]*multiplier;
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: label textStyle:labelStyle];
newLabel.tickLocation = CPDecimalFromFloat(location);
newLabel.offset = x.labelOffset + x.majorTickLength;
[customTicks addObject:[NSDecimalNumber decimalNumberWithDecimal:CPDecimalFromFloat(location)]];
[customLabels addObject:newLabel];
[newLabel release];
}
x.axisLabels = [NSSet setWithArray:customLabels];
[x setMinorTickLocations:[NSSet setWithArray:customTicks]];
[x setMajorTickLocations:[NSSet setWithArray:customTicks]];
[customTicks release];
With CPAxisLabelingPolicyNone you need to provide the tick locations (minorTickLocations and majorTickLocations), too.

Resources