I'm working on the axisLabels and need to make the labels side by side with no space in between. However it seems the labels are autosized to the text. Is there a way to set the width of the labels?
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.weightHistoryChart.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:#1.0, #3.0, #5.0, nil];
NSMutableSet *customLabels = [NSMutableSet setWithCapacity:3];
for ( NSNumber *tickLocation in customTickLocations ) {
CPTTextLayer *layer = [[CPTTextLayer alloc] initWithText:#"label"];
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithContentLayer:layer];
newLabel.tickLocation = tickLocation;
newLabel.offset = 0;
[newLabel.contentLayer setBackgroundColor:[UIColor lightGrayColor].CGColor];
[customLabels addObject:newLabel];
}
x.axisLabels = customLabels;
With space:
Remove space:
You can set the left and right padding on the CPTTextLayer to leave extra space on the sides around the text. However, if all you want is a solid rectangle behind the labels, it might look better to add a layer annotation behind the labels.
Related
I've got a core-plot line graph which is displaying as follows (background view coloured red for clarity):
I have 0 padding at the bottom, and I have an x-axis configured, with labels. The graph is squeezed right down against the bottom of the view though. If I increase the bottom padding I get a grey bar at the bottom with the same result.
What do I need to change in order to move the graph up from the bottom of the view and display the axis?
The code is quite lengthy, but here's some excerpts:
self->graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
hostView.hostedGraph = graph;
self->graph.paddingLeft = 0.0f;
self->graph.paddingTop = 10.0f;
self->graph.paddingRight = 0.0f;
self->graph.paddingBottom = 0.0f;
...
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:p1, p2, p3, nil]];
CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(500.0f)];
plotSpace.yRange = yRange;
...
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self->graph.axisSet;
CPTAxis *x = axisSet.xAxis;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignNegative;
...
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
Try adding bottom padding to graph.plotAreaFrame.
Am trying to plot a simple statistics using Core Plot in an iPhone App. I initially used the labeling policy as CPTAxisLabelingPolicyAutomatic for both the axes, and the grid lines appeared fine as here :
You can see that the graph has both vertical and horizontal grid lines. Then, I changed the labeling policy to
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:xAxisLabels.count];
CPTMutableTextStyle *textStyle = [[CPTMutableTextStyle alloc] init];
textStyle.color = [CPTColor lightGrayColor];
for (NSNumber *tickLocation in customTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc]
initWithText:[NSString stringWithFormat:#"%#",(NSDate *)[xAxisLabels objectAtIndex:labelLocation++]] textStyle:textStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.rotation = 25.0;
newLabel.offset = 10.0;
[customLabels addObject:newLabel];
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels];
But, since when I added custom labeling for X-Axis, the X-Axis grid lines (i.e. the vertical ones) are not appearing. Only horizontal grid lines are seen, as you can see in the below screenshot from the App :
The CPTAxisLabelingPolicyNone labeling policy does not create any labels or tick marks. You need to create sets of locations and set the majorTickLocations and/or minorTickLocations.
Here's some sample code from the Plot Gallery example app:
NSSet *majorTickLocations = [NSSet setWithObjects:[NSDecimalNumber zero],
[NSDecimalNumber numberWithUnsignedInteger:30],
[NSDecimalNumber numberWithUnsignedInteger:50],
[NSDecimalNumber numberWithUnsignedInteger:85],
[NSDecimalNumber numberWithUnsignedInteger:100],
nil];
xAxis.majorTickLocations = majorTickLocations;
The tick locations are often the same as the label locations, but they don't have to be.
Thank you, #Erik Skroch ! What worked for me was a simple change in the labeling policy from
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
to
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyFixedInterval;
This was instigated by your statement :
"The CPTAxisLabelingPolicyNone labeling policy does not create any labels or tick marks."
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?
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;
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.