Vertical lines at X Axis iOS-charts - ios

Maybe this is a simple question but I wanted to know how I can draw vertical lines under X Axis and upper labels in X Axis in iOS-charts. (See the pic, like red lines)
UPDATE: the library I'm using is this https://github.com/danielgindi/ios-charts

I managed to draw the ticks below the x axis by overriding drawLabel. This function draws the labels below the x axis and therefore can draw something else there. For example our desired ticks!
class XAxisRendererWithTicks: XAxisRenderer {
override func drawLabel(context: CGContext, formattedLabel: String, x: CGFloat, y: CGFloat, attributes: [NSAttributedString.Key: Any], constrainedToSize: CGSize, anchor: CGPoint, angleRadians: CGFloat) {
super.drawLabel(context: context, formattedLabel: formattedLabel, x: x, y: y, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians)
context.beginPath()
context.move(to: CGPoint(x: x, y: y))
context.addLine(to: CGPoint(x: x, y: self.viewPortHandler.contentBottom))
context.strokePath()
}
}
To use the custom renderer:
let customXAxisRenderer = XAxisRendererWithTicks(viewPortHandler: self.chartView.viewPortHandler, xAxis: xAxis, transformer: self.chartView.getTransformer(forAxis: .left))
self.chartView.xAxisRenderer = customXAxisRenderer
Example image: Please donĀ“t mind the colors but I think you get what I mean. ;)
Hope this helps!

You could, but you have to override some methods, take a look at drawGridLine() and drawLabels() in x axis renderer for example.
drawGridLine gives you the full details about how to draw a grid line in ios-charts, understaning it will be very helpful to write your own customizations.

Set major tick locations and minor tick locations for the axis you want to get the lines like red color. Please try with the following sample code written for x-axis of the graph.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis; //Get the axis of the graph
// x and y axis line color
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor]; //Set color whatever you want
//Text Style for axis title
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor]; //Set color whatever you want
// Line color
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor]; //Set color whatever you want
tickLineStyle.lineWidth = 2.0f;
x.title = #"Title for the axis";
x.titleOffset = 50.0f;
x.axisLineStyle = axisLineStyle; //axis line style
x.titleTextStyle = axisTextStyle; //axis title text style
x.majorTickLineStyle = axisLineStyle; //Major axis tick style which you wanted
x.minorTickLineStyle = axisLineStyle; //Minor axis tick style
x.labelTextStyle = axisTextStyle; //axis label text style
x.labelingPolicy = CPTAxisLabelingPolicyNone; //axis labeling policy
x.tickDirection = CPTSignNegative; //This will set the red lines below the x-axis as displayed in screenshot you have attached.
Have a happy coding..!!

Related

How to set bounds on a plotspace in CorePlot?

I am trying to draw multiple scatter plots on the same graph using Core-Plot. They have the same x-axis (time), but I want them to be drawn independently of each other. For eg, for two plots, I want the screen to be divided into two parts, the upper part showing one plot and the lower part showing the other (I intend to ultimately plot a real-time multi-channel input). Something like this:
I tried modifying the tutorial given in the following link to use two plotspaces instead of one:
http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2
However, there is no function to set the bounds of a plotspace. The functions xRange and yRange change the range of the axes, but both the plotspaces still occupy the whole screen.
Following is a code snippet that shows two ways in which I tried to accomplish setting bounds:
-(void)configurePlots {
//Get graph and plot space
CPTGraph *graph = self.hostView.hostedGraph;
CPTXYPlotSpace *aaplPlotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
//**Setting xRange and yRange doesn't work**
aaplPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(0.0)
length:CPTDecimalFromCGFloat(graph.bounds.size.width)];
aaplPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(0.0)
length:CPTDecimalFromCGFloat((graph.bounds.size.height)/3)];
CPTXYPlotSpace *googPlotSpace = [[CPTXYPlotSpace alloc] init];
googPlotSpace.xRange = aaplPlotSpace.xRange;
googPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat((graph.bounds.size.height)/3)
length:CPTDecimalFromCGFloat((graph.bounds.size.height)/3)];
[graph addPlotSpace:googPlotSpace];
//Create Plots
//**Trying to set bounds using initWithFrame doesn't work either**
CGRect rect = CGRectMake(0.0, 0.0, graph.bounds.size.width, graph.bounds.size.height/2);
CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] initWithFrame:rect];
aaplPlot.dataSource = self;
aaplPlot.identifier = CPDTickerSymbolAAPL;
CPTColor *aaplColor = [CPTColor redColor];
[graph addPlot:aaplPlot toPlotSpace:aaplPlotSpace];
rect = CGRectMake(0.0, graph.bounds.size.height/2, graph.bounds.size.width, graph.bounds.size.height/2);
CPTScatterPlot *googPlot = [[CPTScatterPlot alloc] initWithFrame:rect];
googPlot.dataSource = self;
googPlot.identifier = CPDTickerSymbolGOOG;
CPTColor *googColor = [CPTColor greenColor];
[graph addPlot:googPlot toPlotSpace:googPlotSpace];
//[graph addPlot:googPlot];
//Set up Plot space
[aaplPlotSpace scaleToFitPlots:[NSArray arrayWithObject:aaplPlot]];
CPTMutablePlotRange *xaRange = [aaplPlotSpace.xRange mutableCopy];
[xaRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
aaplPlotSpace.xRange = xaRange;
CPTMutablePlotRange *yaRange = [aaplPlotSpace.yRange mutableCopy];
[yaRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
aaplPlotSpace.yRange = yaRange;
//Create styles and symbols
CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
aaplLineStyle.lineWidth = 2.5;
aaplLineStyle.lineColor = aaplColor;
aaplPlot.dataLineStyle = aaplLineStyle;
CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
aaplSymbolLineStyle.lineColor = aaplColor;
CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
aaplSymbol.lineStyle = aaplSymbolLineStyle;
aaplSymbol.size = CGSizeMake(6.0f, 6.0f);
aaplPlot.plotSymbol = aaplSymbol;
CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
googLineStyle.lineWidth = 1.0;
googLineStyle.lineColor = googColor;
googPlot.dataLineStyle = googLineStyle;
CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
googSymbolLineStyle.lineColor = googColor;
CPTPlotSymbol *googSymbol = [CPTPlotSymbol starPlotSymbol];
googSymbol.fill = [CPTFill fillWithColor:googColor];
googSymbol.lineStyle = googSymbolLineStyle;
googSymbol.size = CGSizeMake(6.0f, 6.0f);
googPlot.plotSymbol = googSymbol;
}
My question is, is my current approach correct in trying to set bounds on plotspaces or do I need to draw different graphs on the same screen and set the bounds on the graphs? And how do I set bounds on plotspaces or graphs (whichever is the right way!)?
Thanks!

Changing color for grid lines markers in Coreplot

I need to remove the grid line markers which are basically an extension of the major and minor grid lines on my graph (set to gray color). I need to make both these colors consistent. I am not able to find the necessary attribute to set. I would really appreciate if someone could help me out here. I cannot attach images to better depict my problem. Please feel free to ask any follow up questions and I'll clarify. Thanks in advance!
Below is my code so far:
CPTMutableLineStyle *majorGridLineStyle;
// Grid line styles
majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor blackColor] colorWithAlphaComponent:0.1];
// Axises (X & Y)
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
x = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromInt(0);
x.majorIntervalLength = CPTDecimalFromInt(1);
x.minorTicksPerInterval = 0;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorGridLineStyle = majorGridLineStyle;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.majorGridLineStyle = majorGridLineStyle;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
CPTColor defines the same basic colors as UIColor, including +lightGrayColor and +darkGrayColor.
If those don't meet your needs, you can use +colorWithGenericGray: to make any gray between black and white or +colorWithComponentRed:green:blue:alpha: to make any RGB color including those with transparency.

CorePlot horizontal gradient on axis

I'm trying to create a horizontal gradient on my y axis in CorePlot:
CPTGradient *axisGradient = [CPTGradient gradientWithBeginningColor:[CPTColor redColor] endingColor:[CPTColor yellowColor]];
axisGradient.angle = 180.f;
CPTMutableLineStyle *axisStyle = [y.axisLineStyle mutableCopy];
axisStyle.lineWidth = 6.f;
axisStyle.lineGradient = axisGradient;
y.axisLineStyle = axisStyle;
No matter what I set the gradient angle to the gradient is vertical. Anyone have any ideas?
for horizontal gradient you can use
CPTColor *areaColorStart = [CPTColor colorWithComponentRed:0.74 green:0.78 blue:0.82 alpha:1];
CPTColor *areaColorEnd = [CPTColor colorWithComponentRed:0.9 green:0.91 blue:0.92 alpha:.2];
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColorStart endingColor:areaColorEnd];
areaGradient1.angle = 0.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
plot.areaFill = areaGradientFill;
plot.areaBaseValue = CPTDecimalFromFloat(yRange);
hope it will help.
Core Plot draws gradient line fills along the line path. If you want a tall, skinny rectangle filled with a gradient that fades from one color at the top to another at the bottom, use a plot space annotation. Create a CPTBorderedLayer for the annotation content and give it a gradient fill ([CPTFill fillWithGradient:axisGradient]).

iOS Core Plot - Scatter plot symbol color appearance

I am using CorePlot for Scatter plot and would like to know how to obtain the circles of the scatter plot with such a color effect as it is here http://i.stack.imgur.com/7ZcqY.jpg
I would like to get the color as in CPTXYScatterPlot - Can I set the Z order of plot symbols?.
In your symbolForScatterPlot implementation, create a circlePlotSymbol with a gradient fill, like so:
CPTPlotSymbol *circlePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
// Obviously for a real bubble chart you'll want to get the color from somewhere so they're not all the same
CPTColor *endColor = [CPTColor redColor];
CPTColor *startColor = [endColor colorWithAlphaComponent:0.4f];
CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:startColor endingColor:endColor];
Then set the gradient type to Radial, and set your gradient as the fill on your plotSymbol. If you want it to look like it's lit from an angle, you can set the startAnchor to a point off center:
gradient.gradientType = CPTGradientTypeRadial;
graphGradient.startAnchor = CGPointMake(0.35, 0.75);
circlePlotSymbol.fill = [CPTFill fillWithGradient:gradient];
If you don't want a border around your circles, set the linestyle color to clear and/or set the width to 0.0f (probably don't need to do both):
CPTMutableLineStyle *lineStyle = [[CPTMutableLineStyle alloc] init];
lineStyle.lineColor = [CPTColor clearColor];
lineStyle.lineWidth = 0.0f;
circlePlotSymbol.lineStyle = lineStyle;
[lineStyle release];
Set the size of your bubble:
// For a real bubble chart, you'll need to calculate this from your data
circlePlotSymbol.size = CGSizeMake(50.0f, 50.0f);
and return your plot symbol:
return circlePlotSymbol;
That should do it. I don't have my code in front of me so I'm going from memory here (and please forgive any typos or bad syntax, as I don't have access to a compiler right now either), but hopefully this will be close enough to get you pointed in the right direction.

Add additional x axes in core-plot

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;

Resources