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.
Related
As shown in the image, X-axis Labels are being truncated.
I am trying with this
[xRange expandRangeByFactor:CPTDecimalFromDouble(1.05)]
but still I am not able to succeed. Please let me know if any method to avoid this truncation.
This configures graph,
-(void)configureGraph {
CPTGraph *graph = [[CPTXYGraph alloc initWithFrame:self.hostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
self.hostView.hostedGraph = graph;
graph.paddingLeft = 0;
graph.paddingRight = 0;
graph.paddingTop = 0;
graph.paddingBottom = 0;
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineWidth = 1.0;
borderLineStyle.lineColor = [CPTColor colorWithCGColor:[UIColor whiteColor].CGColor];
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor blackColor];
titleStyle.fontName = [UIFont flukeFontBoldName];
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -15.0f);
[graph.plotAreaFrame setPaddingLeft:kPaddingLeft];
[graph.plotAreaFrame setPaddingBottom:kPaddingBottom];
[graph.plotAreaFrame setPaddingTop:kPaddingTop];
[graph.plotAreaFrame setPaddingRight:kPaddingRight];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
}
-(void)configureAxes
{
CPTGraph *graph = self.hostView.hostedGraph;
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.5;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.6] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.preferredNumberOfMajorTicks = 4;
x.minorTicksPerInterval = 1;
// pin axis to bottom
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
y.majorIntervalLength = CPTDecimalFromDouble(20.0f);
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.preferredNumberOfMajorTicks = kCorePlotYAxisPreferredTickLocationCount;
y.minorTicksPerInterval = 0;
// Pin axis to left
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
}
You need to do some combination of the following things. Which one(s) you choose depends on the appearance you want to achieve.
Increase the length of the xRange further to move the last data value in from the right edge.
Increase the right padding on the plot area frame to move the edge of the plot area further from the edge of the graph where the text is being clipped.
At xAxis, you can set label alignment to left.
default there is only two axis on left and bottom, I want to add another tow axis on right and bottom to show the same tick and label.
Thanks in advance.
You can do that with constraints when making your axis,
CPTXYAxis *x = [self _makeDefaultAxis];
x.coordinate = CPTCoordinateX;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
x.tickDirection = CPTSignPositive;
// ...
CPTXYAxis *y = [self _makeDefaultAxis];
y.coordinate = CPTCoordinateY;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y.tickDirection = CPTSignPositive;
// ...
CPTXYAxis *y2 = [self _makeDefaultAxis];
y2.coordinate = CPTCoordinateY;
y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0];
y2.tickDirection = CPTSignNegative;
// ...
CPTXYAxis *x2 = [self _makeDefaultAxis];
x2.coordinate = CPTCoordinateX;
x2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0.0];
x2.tickDirection = CPTSignNegative;
Which gives something like,
So also show the labels you need to adjust the padding etc.
I want to align the y axis labels of my scatter plot in Core Plot. Currently, the labels are like this:
16
12
8
4
How can I change they alignment to right :
16
12
_8
_4
I tried the following:
y.labelAlignment = CPTAlignmentRight;
It didn't work. How can I do this alignment?
Edit-
Here is my code:
CPTAxis *y = axisSet.yAxis;
y.title = #"Y Label";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -40.0f;
y.axisLineStyle = axisLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 16.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.minorTickLength = 2.0f;
y.tickDirection = CPTSignPositive;
y.labelAlignment = CPTAlignmentRight;
You can specify the label direction independently from the tick direction now. To have the tick marks extend to the right of the axis while lining up the labels on the left side, use the following settings:
y.tickDirection = CPTSignPositive;
y.tickLabelDirection = CPTSignNegative;
y.labelOffset = 5.0;
I have added an additional x axis to my plot space in order to show zero values:
CPTMutableLineStyle *boldLineStyle = [xAxis.axisLineStyle mutableCopy];
boldLineStyle.lineWidth = 1;
boldLineStyle.lineColor = [CPTColor darkGrayColor];
boldLineStyle.lineCap = kCGLineCapRound;
CPTXYAxis *xZeroAxis = [[CPTXYAxis alloc] init];
xZeroAxis.coordinate = CPTCoordinateX;
xZeroAxis.plotSpace = plotSpace;
xZeroAxis.axisLineStyle = boldLineStyle;
Interestingly, the labels on this axis are shown, but not the axis itself:
xZeroAxis.axisLabels = [NSSet setWithArray:naLabels];
xZeroAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
xyAxisSet.axes = #[xAxis, xLabelAxis, yAxis, xZeroAxis];
How would I need to change my code to show the x axis?
Thank you!
x.axisLineStyle is set as follows:
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 1.0;
axisLineStyle.lineCap = kCGLineCapRound;
axisLineStyle.lineColor = [CPTColor darkGrayColor];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = axisLineStyle;
This seems to be a bug in the current Core Plot release. The issue has been reported here: Core Plot Issue 514
I have a parent view with 6 UIViews to which I add my core-plot charts (This is working fine but only every second chart displays for some reason!)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if(!chartVC1){
chartVC1 = [[ChartVC alloc] initWithIdentifier:#"plot1"];
//[chartVC1 setKPlotIdentifier:#"plot1"];
[chartVC1.view setFrame:self.Panel1.frame];
}
[self.Panel1 addSubview:chartVC1.view];
if(!chartVC3){
chartVC3 = [[ChartVC alloc] initWithIdentifier:#"plot2"];
[chartVC3.view setFrame:self.Panel3.frame];
}
[self.Panel3 addSubview:chartVC3.view];
if(!chartVC2){
chartVC2 = [[ChartVC alloc] initWithIdentifier:#"plot3"];
[chartVC2.view setFrame:self.Panel2.frame];
}
[self.Panel2 addSubview:chartVC2.view];
if(!chartVC4){
chartVC4 = [[ChartVC alloc] initWithIdentifier:#"plot4"];
[chartVC4.view setFrame:self.Panel4.frame];
}
[self.Panel4 addSubview:chartVC4.view];
if(!chartVC5){
chartVC5 = [[ChartVC alloc] initWithIdentifier:#"plot5"];
[chartVC5.view setFrame:self.Panel5.frame];
}
[self.Panel5 addSubview:chartVC5.view];
if(!chartVC6){
chartVC6 = [[ChartVC alloc] initWithIdentifier:#"plot6"];
[chartVC6.view setFrame:self.Panel6.frame];
}
[self.Panel6 addSubview:chartVC6.view];
}
Here is some of my code for the class ChartVC
-(void)renderInLayer
{
CPTXYGraph *graph = [[[CPTXYGraph alloc] initWithFrame:self.view.bounds] autorelease];
[self addGraph:graph toHostingView:self.view];
[self applyTheme:nil toGraph:graph withDefault:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
//[self setTitleDefaultsForGraph:graph withBounds:self.view.bounds];
//[self setPaddingDefaultsForGraph:graph withBounds:self.view.bounds];
graph.plotAreaFrame.paddingTop = -20.0;
graph.plotAreaFrame.paddingRight = -20.0;
graph.plotAreaFrame.paddingBottom = -20.0;
graph.plotAreaFrame.paddingLeft = -20.0;
graph.plotAreaFrame.plotArea.frame = self.view.bounds;
graph.backgroundColor = [[UIColor whiteColor] CGColor];
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
// Axes
// X axis
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.minorTicksPerInterval = 9;
//x.title = #"X Axis";
//x.titleOffset = 35.0;
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.numberStyle = NSNumberFormatterNoStyle;
x.labelFormatter = labelFormatter;
[labelFormatter release];
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.minorTicksPerInterval = 3;
//y.labelOffset = 5.0;
//y.title = #"Y Axis";
//y.titleOffset = 30.0;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
// Rotate the labels by 45 degrees, just to show it can be done.
x.labelRotation = M_PI * 0.25;
// Create the plot
CPTScatterPlot *dataSourceLinePlot1 = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot1.identifier = [self kPlotIdentifier];
//NSLog(#"set identifier: %#", [self kPlotIdentifier]);
dataSourceLinePlot1.cachePrecision = CPTPlotCachePrecisionDouble;
CPTMutableLineStyle *lineStyle1 = [[dataSourceLinePlot1.dataLineStyle mutableCopy] autorelease];
lineStyle1.lineWidth = 1.0;
lineStyle1.lineColor = [CPTColor redColor];
dataSourceLinePlot1.dataLineStyle = lineStyle1;
dataSourceLinePlot1.dataSource = self;
[graph addPlot:dataSourceLinePlot1];
// Plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 1)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(100)];
}
I would think the issue was my own fault if only the 1st chart displayed but because every second chart displays it doesn't make any sense to me..
My only guess is it is some sort of timing issue but I debugged it and delayed the creation of each chart and it made no difference..
I even removed the 3rd, 5th charts to see if the others now rendered but the same 3 charts that did not display previously were still not visible which really threw me off.. (ie. only the 1st chart displayed out of the 4 enabled!)
Any help on this much appreciated...
I may just use the code from the Apple Developer Library that renders the accelerometer data as it is very lightweight to support 6 charts on a screen at one time without issue...
Core-Plot just doesn't seem up to the task I guess...
You didn't say which graphs aren't showing up, but since it's always the same ones, I suspect there's an issue with the view hierarchy. Try changing
`[chartVC1.view setFrame:self.Panel1.frame];`
to
`[chartVC1.view setFrame:self.Panel1.bounds];`
for each graph.