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
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.
I am trying display some data on a chart using CorePlot. The y axis title and the labels get displayed without an issue, however the title of the x axis and the tick labels does not get displayed even with the automatic labeling policy. Please help me.
CPTMutableLineStyle *whiteLineStyle = [CPTMutableLineStyle lineStyle];
whiteLineStyle.lineColor = [CPTColor whiteColor];
whiteLineStyle.lineWidth = 1.0;
CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];
newGraph.paddingRight = 50;
newGraph.paddingLeft = 50;
newGraph.paddingTop = 10;
newGraph.paddingBottom = 40;
newGraph.plotAreaFrame.paddingBottom = 40;
newGraph.plotAreaFrame.masksToBorder = NO;
newGraph.plotAreaFrame.borderLineStyle = nil;
self.graphHost.hostedGraph = newGraph;
CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme];
[newGraph applyTheme:theme];
CPTXYAxisSet *xyAxisSet= (CPTXYAxisSet *)newGraph.axisSet;
CPTXYAxis *xAxis = xyAxisSet.xAxis;
CPTXYAxis *yAxis = xyAxisSet.yAxis;
xAxis.title = #"Date / Time";
yAxis.title = #"Trading Range";
[xAxis setLabelingPolicy:CPTAxisLabelingPolicyNone];
xAxis.majorTickLocations = [self majorTickLocations];
[xAxis setAxisLabels:[NSSet setWithArray:[self getXAxisTitleArray]]];
xAxis.majorTickLineStyle = whiteLineStyle;
[yAxis setLabelingPolicy:CPTAxisLabelingPolicyAutomatic];
CPTTradingRangePlot *ohlcPlot = [[CPTTradingRangePlot alloc] initWithFrame:newGraph.bounds];
ohlcPlot.labelOffset = 10.0;
ohlcPlot.stickLength = 10.0;
ohlcPlot.dataSource = self;
ohlcPlot.plotStyle = CPTTradingRangePlotStyleCandleStick;
ohlcPlot.lineStyle = whiteLineStyle;
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
whiteTextStyle.fontSize = 10.0;
ohlcPlot.labelTextStyle = whiteTextStyle;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) newGraph.defaultPlotSpace;
[newGraph addPlot:ohlcPlot toPlotSpace:plotSpace];
The following code solved my problem:
xAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
I have a data array with values:
[1,2,4,5,7..]
and after set auto labeling policy, it will auto draw data at each point.
but there will be gap between 2 and 4, 5 and 7.
my question is how to remove this gap between each data and have a normal behavior when pinch or scale the graph, also the position of data "4" with show 4 at x axis, data "2" will show 2 at x axis.
Another problem I met is that, when I drag with in the graph, the axis segment sometimes will disappear or total axis will disappear.
(I cannot upload image, there is a copy of that in
https://github.com/core-plot/core-plot/issues/113
Thanks in advance.
here is my graph setup code:
_graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme * theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[_graph applyTheme:theme];
[_graph setCornerRadius:0.0f];
//
_graphHost = [[CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, hostView.width, hostView.height)];
[_graphHost setBackgroundColor:[UIColor whiteColor]];
_dynamicPrice = [[UILabel alloc] initWithFrame:CGRectMake(10, _graphHost.height/2, 35, 10) font:[UIFont systemFontOfSize:8.0] color:[UIColor blueColor] text:#"test"];
[_dynamicPrice setBackgroundColor:[UIColor grayColor]];
[_dynamicPrice setTextAlignment:NSTextAlignmentCenter];
_dynamicPrice.layer.transform = CATransform3DMakeRotation(M_PI, 1, 0, 0);
_graphHost.hostedGraph = _graph;
[_graphHost addSubview:_dynamicPrice];
[hostView addSubview:_graphHost];
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor whiteColor];
borderLineStyle.lineWidth = 0.1f;
_graph.plotAreaFrame.borderLineStyle = nil;
_graph.plotAreaFrame.paddingTop += 10.0;
_graph.plotAreaFrame.paddingRight += 10.0;
_graph.plotAreaFrame.paddingBottom += 10.0;
_graph.plotAreaFrame.paddingLeft += 25.0;
_graph.plotAreaFrame.masksToBorder = NO;
_graph.plotAreaFrame.plotArea.delegate = self;
//
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.3f;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
//
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.1f;
minorGridLineStyle.lineColor = [[CPTColor lightGrayColor] colorWithAlphaComponent:0.1];
UIFont *labelFont = [UIFont systemFontOfSize:8.0f];
CPTTextStyle *labelStyle = [CPTTextStyle textStyleWithAttributes:#{NSFontAttributeName:labelFont, NSForegroundColorAttributeName:[UIColor blueColor]}];
// NSBackgroundColorAttributeName:[UIColor grayColor]
// Axes
CPTXYAxisSet *xyAxisSet = (id)_graph.axisSet;
CPTXYAxis *x = xyAxisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.majorIntervalLength = CPTDecimalFromDouble(60*3);
x.minorTicksPerInterval = 0;
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0.0];
x.labelTextStyle = labelStyle;
//x.minorTickLabelTextStyle = labelStyle;
[self setXLabelDateFormater:_currentKLineType];
CPTXYAxis *y = xyAxisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(20);
y.majorIntervalLength = CPTDecimalFromDouble(20);
y.minorTicksPerInterval = 0;
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y.labelOffset = 0.0;
y.labelTextStyle = labelStyle;
y.labelFormatter = yFormater;
// OHLC plot
CPTMutableLineStyle *redLineStyle = [CPTMutableLineStyle lineStyle];
redLineStyle.lineColor = [CPTColor redColor];
redLineStyle.lineWidth = 1.0f;
CPTMutableLineStyle *greenLineStyle = [CPTMutableLineStyle lineStyle];
greenLineStyle.lineColor = [CPTColor greenColor];
greenLineStyle.lineWidth = 1.0f;
CPTTradingRangePlot *kLineChart= [[CPTTradingRangePlot alloc] initWithFrame:_graph.bounds];
kLineChart.identifier = PLOT_KLINE;
kLineChart.lineStyle = redLineStyle;
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
whiteTextStyle.fontSize = 12.0;
kLineChart.labelOffset = 0.0;
kLineChart.barCornerRadius = 0.0;
kLineChart.barWidth = 15.0;
kLineChart.increaseFill = [CPTFill fillWithColor:[CPTColor redColor]];
kLineChart.decreaseFill = [CPTFill fillWithColor:[CPTColor greenColor]];
kLineChart.increaseLineStyle = redLineStyle;
kLineChart.decreaseLineStyle = greenLineStyle;
kLineChart.dataSource = self;
kLineChart.delegate = self;
kLineChart.plotStyle = CPTTradingRangePlotStyleCandleStick;
[_graph addPlot:kLineChart];
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)_graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-0.5) length:CPTDecimalFromDouble(oneDay * _kLineDataItems.count)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(4)];
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
[plotSpace scaleToFitPlots:[_graph allPlots]];
when I pinch or scale the graph, I will set the plot range dynamic according to the postion shown in current view port.
What I need is a sooth change when scale or pinch, and fixed major grid position.
How can I implement that effect?
The bars for a trading range plot are drawn at the x-value provided by the datasource. If you want a bar at x=3, make sure the datasource provides one. Remember that the x-value is independent of the data index.
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.
I want to add multiple x-axis, My code is as follows:
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)barChart.axisSet;
/// Here I customize default x and y axis. And they are correctly visible
// Now create additional x axis
CPTXYAxis *bottomX = [[CPTXYAxis alloc]init];
bottomX.orthogonalCoordinateDecimal = CPTDecimalFromString(#"4");
CPTMutableLineStyle * lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor greenColor];
lineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
bottomX.axisLineStyle = lineStyle;
NSMutableArray * axes=[NSMutableArray arrayWithArray:axisSet.axes];
[axes addObject:bottomX];
axisSet.axes=axes;
The default x,y axes are perfect, but the additional x axis (bottomX) is not showing any where.
You need to set the plot space on the new axis:
bottomX.plotSpace = barChart.defaultPlotSpace;