Axis Position and Plot Color - ios

I have got two problems.
My axis are not meeting on proper point i.e. I want 0 of my y-axis to intersect first value of x-axis. I have tried many ways through orthogonalCoordinateDecimal property, but I am unable to manage it.
Secondly, my 2nd plot on right hand side is not plotting of red color as I have coded. Can you tell me what possibly may I be missing?
Figure:
Code snippets of I have written:
NSTimeInterval oneDay = 24 * 60 * 60;
// Setup plot spaces
//1- calorie
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
NSTimeInterval xLow = 0.0f;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat(oneDay * 7.0f)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(600)];
//2- exercise
plotSpace2 = [[[CPTXYPlotSpace alloc] init] autorelease];
plotSpace2.allowsUserInteraction = YES;
plotSpace2.xRange = plotSpace.xRange;
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(60.0)];
[graph addPlotSpace:plotSpace2];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.graphHost.hostedGraph.axisSet;
// Axes
CPTXYAxis *x = axisSet.xAxis;
NSTimeInterval oneDay = 24 * 60 * 60;
x.majorIntervalLength = CPTDecimalFromFloat(oneDay);
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.minorTicksPerInterval = 0;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromInt(200);
y.minorTicksPerInterval = 9;
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0);
y.title = #"Calories Burned";
CPTXYAxis *y2 = [[[CPTXYAxis alloc] init]retain];
y2.coordinate = CPTCoordinateY;
y2.majorIntervalLength = CPTDecimalFromString(#"5.0");
y2.minorTicksPerInterval = 3;
y2.orthogonalCoordinateDecimal = CPTDecimalFromString(#"-20.0");
y2.title = #"Exercise Time (min)";
//I added these lines too
//adding 2nd axis
NSMutableArray *newAxes = [graph.axisSet.axes mutableCopy];
[newAxes addObject:y2];
graph.axisSet.axes = newAxes;
[newAxes release];
CPTScatterPlot *caloriePlot = [[CPTScatterPlot alloc] init];
caloriePlot.dataSource = self;
caloriePlot.identifier = #"Calorie";
//create styles and symbols
CPTMutableLineStyle *calorieLineStyle = [caloriePlot.dataLineStyle mutableCopy];
calorieLineStyle.lineWidth = 2.5;
calorieLineStyle.lineColor = [CPTColor greenColor];
caloriePlot.dataLineStyle = calorieLineStyle;
[graph addPlot:caloriePlot toPlotSpace:plotSpace];
CPTScatterPlot *exercisePlot = [[CPTScatterPlot alloc] init];
exercisePlot.dataSource = self;
exercisePlot.identifier = #"Exercise";
CPTMutableLineStyle *exerciseLineStyle = [exercisePlot.dataLineStyle mutableCopy];
exerciseLineStyle.lineWidth = 2.5;
exerciseLineStyle.lineColor = [CPTColor redColor];
exerciseLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:5.0f], [NSNumber numberWithFloat:5.0f], nil];
exercisePlot.dataLineStyle =calorieLineStyle;
[graph addPlot:exercisePlot toPlotSpace:plotSpace2];
Any help will be appreciated.Thanks.

Hard to tell without seeing more of your code. How many plot spaces do you have? Which plot space is each axis assigned to? What are the plot ranges of each plot space?
Change this line:
exercisePlot.dataLineStyle = calorieLineStyle;
to
exercisePlot.dataLineStyle = exerciseLineStyle;

Related

Issue in drawing graph using CorePlot

I wanna create a log based app for which am trying to make a graph that shows activities based on 24 hrs time. One hour has to be split into four(15 mins). I have tried using core plot for the same. I have attached the code i have done so far and the graph i have got till now.
const CGFloat majorTickLength = 20; // height of the major tick
const CGFloat minorTickLength = 8.0; // height of the minor tick
// const CGFloat titleOffset = self.titleSize;
#if TARGET_OS_IPHONE
CGRect bounds = hostingView.bounds;
#else
CGRect bounds = NSRectToCGRect(hostingView.bounds);
#endif
// Create graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
[self addGraph:graph toHostingView:hostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTSlateTheme]];
graph.fill = [CPTFill fillWithColor:[CPTColor blackColor]];
// Plot area
graph.plotAreaFrame.paddingTop = self.titleSize;
graph.plotAreaFrame.paddingBottom = self.titleSize;
graph.plotAreaFrame.paddingLeft = self.titleSize;
graph.plotAreaFrame.paddingRight = self.titleSize;
graph.plotAreaFrame.masksToBorder = NO;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:#0.0 length:#1440.0];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:#0.0 length:#20.0];
// Line styles
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0;
CPTMutableLineStyle *majorTickLineStyle = [axisLineStyle mutableCopy];
majorTickLineStyle.lineWidth = 1.0;
majorTickLineStyle.lineCap = kCGLineCapRound;
CPTMutableLineStyle *minorTickLineStyle = [axisLineStyle mutableCopy];
minorTickLineStyle.lineWidth = 1.0;
minorTickLineStyle.lineCap = kCGLineCapButt;
// Text styles
CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle];
axisTitleTextStyle.fontName = #"Helvetica-Bold";
CPTMutableNumberSet *majorTickLocations = [NSMutableSet set];
for (int i = 60; i <= 1440; i += 60)
{
[majorTickLocations addObject:#(i)];
}
CPTMutableNumberSet *minorTickLocations = [NSMutableSet set];
for ( NSUInteger loc = 0; loc <= 1440; loc += 15 )
{
[minorTickLocations addObject:#(loc)];
}
// Axis1
CPTXYAxis *axis1 = [[CPTXYAxis alloc] init];
axis1.plotSpace = graph.defaultPlotSpace;
axis1.labelingPolicy = CPTAxisLabelingPolicyNone;
axis1.orthogonalPosition = #1.0;
axis1.tickDirection = CPTSignPositive;
axis1.axisLineStyle = axisLineStyle;
axis1.majorTickLength = majorTickLength;
axis1.majorTickLineStyle = majorTickLineStyle;
axis1.minorTickLength = minorTickLength;
axis1.minorTickLineStyle = minorTickLineStyle;
axis1.majorTickLocations = majorTickLocations;
axis1.minorTickLocations = minorTickLocations;
// Axis2
CPTXYAxis *axis2 = [[CPTXYAxis alloc] init];
axis2.plotSpace = graph.defaultPlotSpace;
axis2.labelingPolicy = CPTAxisLabelingPolicyNone;
axis2.orthogonalPosition = #2.0;
axis2.tickDirection = CPTSignPositive;
axis2.axisLineStyle = axisLineStyle;
axis2.majorTickLength = majorTickLength;
axis2.majorTickLineStyle = majorTickLineStyle;
axis2.minorTickLength = minorTickLength;
axis2.minorTickLineStyle = minorTickLineStyle;
axis2.majorTickLocations = majorTickLocations;
axis2.minorTickLocations = minorTickLocations;
// Axis3
CPTXYAxis *axis3 = [[CPTXYAxis alloc] init];
axis3.plotSpace = graph.defaultPlotSpace;
axis3.labelingPolicy = CPTAxisLabelingPolicyNone;
axis3.orthogonalPosition = #3.0;
axis3.tickDirection = CPTSignPositive;
axis3.axisLineStyle = axisLineStyle;
axis3.majorTickLength = majorTickLength;
axis3.majorTickLineStyle = majorTickLineStyle;
axis3.minorTickLength = minorTickLength;
axis3.minorTickLineStyle = minorTickLineStyle;
axis3.majorTickLocations = majorTickLocations;
axis3.minorTickLocations = minorTickLocations;
// Axis4
CPTXYAxis *axis4 = [[CPTXYAxis alloc] init];
axis4.plotSpace = graph.defaultPlotSpace;
axis4.labelingPolicy = CPTAxisLabelingPolicyNone;
axis4.orthogonalPosition = #4.0;
axis4.tickDirection = CPTSignPositive;
axis4.axisLineStyle = axisLineStyle;
axis4.majorTickLength = majorTickLength;
axis4.majorTickLineStyle = majorTickLineStyle;
axis4.minorTickLength = minorTickLength;
axis4.minorTickLineStyle = minorTickLineStyle;
axis4.majorTickLocations = majorTickLocations;
axis4.minorTickLocations = minorTickLocations;
CPTMutableAxisLabelSet *axis4LabelSet = [NSMutableSet set];
for ( NSUInteger i = 1; i < 24; i++ )
{
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%lu", (unsigned long)i]
textStyle:axis4.labelTextStyle];
newLabel.tickLocation = #(i*60);
newLabel.offset = axis4.labelOffset + axis4.majorTickLength;
[axis4LabelSet addObject:newLabel];
}
axis4.axisLabels = axis4LabelSet;
// Add axes to the graph
graph.axisSet.axes = #[axis1, axis2, axis3, axis4];
In my above code i was able to use majorTickLocations to represent each HOUR and minorTickLocations to represent the 15 mins interval. But my desired output is the like the following image. Each 30 mins mark should be shown different too. How can i achieve this.
Add one more horizontal axis. On the four existing ones, use the minor tick marks for the 15 minute intervals and the major ticks for the 30 minute intervals. On the new axis, set the major grid lines (drawn at the majorTickLocations) on the hour intervals. You don't need any tick marks on the new axis.

How to show Line label with two digits after dote?

i use a core plot in my Xcode project and have a problem with plot values.
How can i show number on the CPTScatterPlot?
My plot shows me only one digit after dot (36.6).
For example show me 36.665 or 36.001?
I add a screen preview.
INIT
-(void)initLinePlotTmp
{
//Initialize and display Graph (x and y axis lines)
self.graph = [[CPTXYGraph alloc] initWithFrame:self.graphView.bounds];
self.hostView = [[CPTGraphHostingView alloc] initWithFrame:self.graphView.bounds];
self.hostView.hostedGraph = self.graph;
[self.graphView addSubview:hostView];
//apply styling to Graph
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
//set graph backgound area transparent
self.graph.backgroundColor = nil;
self.graph.fill = nil;
self.graph.plotAreaFrame.fill = nil;
self.graph.plotAreaFrame.plotArea.fill = nil;
//This removes top and right lines of graph
self.graph.plotAreaFrame.borderLineStyle = nil;
//This shows x and y axis labels from 0 to 1
self.graph.plotAreaFrame.masksToBorder = NO;
// set padding for graph from Left and Bottom
self.graph.paddingBottom = 10;
self.graph.paddingLeft = 50;
self.graph.paddingRight = 0;
self.graph.paddingTop = 10;
//Define x and y axis range
// x-axis from 0 to 100
// y-axis from 0 to 300
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(plotXMinRange)
length:CPTDecimalFromInt(plotXMaxRange)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(plotYMinRange)
length:CPTDecimalFromInt(plotYMaxRange)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
NSNumberFormatter *axisLabelFormatter = [[NSNumberFormatter alloc]init];
[axisLabelFormatter setGeneratesDecimalNumbers:NO];
[axisLabelFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
//Define x-axis properties
//x-axis intermediate interval 2
axisSet.xAxis.majorIntervalLength = CPTDecimalFromInt(plotXInterval);
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = 5;
axisSet.xAxis.majorTickLength = 7;
axisSet.xAxis.title = #"Time(Hours)";
axisSet.xAxis.titleOffset = 25;
axisSet.xAxis.labelFormatter = axisLabelFormatter;
//Define y-axis properties
//y-axis intermediate interval = 50;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromInt(plotYInterval);
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.minorTickLength = 5;
axisSet.yAxis.majorTickLength = 7;
axisSet.yAxis.title = #"Temperature";
axisSet.yAxis.titleOffset = 30;
axisSet.yAxis.labelFormatter = axisLabelFormatter;
//Define line plot and set line properties
self.linePlot = [[CPTScatterPlot alloc] init];
self.linePlot.dataSource = self;
[self.graph addPlot:self.linePlot toPlotSpace:plotSpace];
//set line plot style
CPTMutableLineStyle *lineStyle = [self.linePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 2;
lineStyle.lineColor = [CPTColor blackColor];
self.linePlot.dataLineStyle = lineStyle;
CPTMutableLineStyle *symbolineStyle = [CPTMutableLineStyle lineStyle];
symbolineStyle.lineColor = [CPTColor blackColor];
CPTPlotSymbol *symbol = [CPTPlotSymbol ellipsePlotSymbol];
symbol.fill = [CPTFill fillWithColor:[CPTColor blackColor]];
symbol.lineStyle = symbolineStyle;
symbol.size = CGSizeMake(3.0f, 3.0f);
self.linePlot.plotSymbol = symbol;
//set graph grid lines
CPTMutableLineStyle *gridLineStyle = [[CPTMutableLineStyle alloc] init];
gridLineStyle.lineColor = [CPTColor grayColor];
gridLineStyle.lineWidth = 0.5;
axisSet.xAxis.majorGridLineStyle = gridLineStyle;
axisSet.yAxis.majorGridLineStyle = gridLineStyle;
}
this is my function for plot init.
Create a NSNumberFormatter and configure it to format the labels however you want. Set the labelFormatter on the plot to the new formatter.
NSLog(#"Digit with Three decimal Values = %.3f",999.123456789);
This Prints 999.123 // three values after decimal point
Assign text to that label accordingly.
You are add to:
leftAxis.valueFormatter = leftAxis.valueFormatter;
//remove decimal
NSNumberFormatter *vf = [[NSNumberFormatter alloc] init];
vf.generatesDecimalNumbers = NO;
data.valueFormatter = vf;

Questions about scatter plot in Core Plot

I am making a scatter plot app in Core Plot 1.4. I am new to Objective-C and Core Plot, so sorry for asking some basic questions. Here is some code I wrote by imitating the sample program(CPTTestApp-iphone).
- (void)viewDidLoad
{
[super viewDidLoad];
...
// Add some initial data
NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:100];
int i;
for ( i = 0; i < 60; i++ ) {
id x = [NSNumber numberWithFloat:1 + i * 0.05];
id y = [NSNumber numberWithInt:1]; //[NSNumber numberWithFloat:1.2 * rand() / (float)RAND_MAX + 1.2];
[contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x, #"x", y, #"y", nil]];
}
dataForPlot = contentArray;
// Create graph from theme
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] init];
hostingView.collapsesLayers = NO; // Setting to YES reduces GPU memory usage, but can slow drawing/scrolling
hostingView.hostedGraph = graph;
[self setView:hostingView];
graph.paddingLeft = 0.0;
graph.paddingTop = 0.0;
graph.paddingRight = 0.0;
graph.paddingBottom = 30.0;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
[plotSpace setXRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(5)]];
[plotSpace setYRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(0) length:CPTDecimalFromInteger(7)]];
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(#"1");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
x.minorTicksPerInterval = 5;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(2.99) length:CPTDecimalFromFloat(0.02)],
nil];
x.labelExclusionRanges = exclusionRanges;
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromString(#"1");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(#"0");
exclusionRanges = [NSArray arrayWithObjects:
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(3.99) length:CPTDecimalFromFloat(0.02)],
nil];
y.labelExclusionRanges = exclusionRanges;
y.delegate = self;
// Create a blue plot area
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor blueColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.identifier = #"Blue Plot";
boundLinePlot.dataSource = self;
[graph addPlot:boundLinePlot toPlotSpace:plotSpace];
// Do a blue gradient
CPTColor *areaColor1 = [CPTColor colorWithComponentRed:0.3 green:0.3 blue:1.0 alpha:0.8];
CPTGradient *areaGradient1 = [CPTGradient gradientWithBeginningColor:areaColor1 endingColor:[CPTColor clearColor]];
areaGradient1.angle = -90.0f;
CPTFill *areaGradientFill = [CPTFill fillWithGradient:areaGradient1];
boundLinePlot.areaFill = areaGradientFill;
boundLinePlot.areaBaseValue = [[NSDecimalNumber zero] decimalValue];
// interpolation
boundLinePlot.interpolation = CPTScatterPlotInterpolationCurved;
#ifdef PERFORMANCE_TEST
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(changePlotRange) userInfo:nil repeats:YES];
#endif
}
Now I have some questions.
From dataForPlot, a blue line with gradient must be rendered the height y=1, but after running this it was not shown. (I haven't adjust the range of x values in dataForPlot but a partial line should be rendered.)
How can I move the origin (0,0) to the middle of the screen?
Why do 1.0-3.0 in x-axis and 1.0, 2.0, 4.0 in y-axis disappear?
Can anyone help me figure these out? Thank you.
What data is being plotted? Please show the datasource methods.
The visible data ranges in the plot area are controlled by the plot space:
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(-5)
length:CPTDecimalFromInteger(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInteger(-7)
length:CPTDecimalFromInteger(14)];
The labelExclusionRanges skips over any labels in the specified ranges. If you want to see all of the ticks and labels, don't set this property.

Multiple y axes with custom label in core plot

-(void)configureAxes {
CPTGraph* graph = self.hostView.hostedGraph;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
plot.dataSource = self;
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( 8 )]];
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( 10 )]];
self.arrayData=[[NSMutableArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"10",#"20",#"E", nil];
self.arrayDataY=[[NSMutableArray alloc] initWithObjects:#"CP",#"BD",#"Test",#"Notes",#"CM",#"Day",#"97.7",#"97.8",#"97.9", nil];
CPTMutableLineStyle *yMajorGridLineStyle = [CPTLineStyle lineStyle];
yMajorGridLineStyle.lineCap = kCGLineCapRound;
yMajorGridLineStyle.lineColor = [CPTColor whiteColor];
yMajorGridLineStyle.lineWidth = 1.0;
CPTMutableLineStyle *lineStyle=[CPTLineStyle lineStyle];
lineStyle.lineColor=[CPTColor redColor];
lineStyle.lineWidth=2.0f;
CPTMutableLineStyle *lineStyle1=[CPTLineStyle lineStyle];
lineStyle1.lineColor=[CPTColor greenColor];
lineStyle1.lineWidth=2.0f;
CPTMutableLineStyle *lineStyle2=[CPTLineStyle lineStyle];
lineStyle2.lineColor=[CPTColor blackColor];
lineStyle2.lineWidth=1.0f;
CPTXYAxis *x = axisSet.xAxis;
NSMutableArray *labelTick=[[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.majorIntervalLength= CPTDecimalFromInteger(1);
x.minorTicksPerInterval = 0;
x.majorTickLineStyle = lineStyle;
x.minorTickLineStyle = lineStyle1;
x.axisLineStyle = lineStyle2;
x.minorTickLength = 2.0f;
x.majorTickLength = 3.0f;
x.orthogonalCoordinateDecimal=CPTDecimalFromDouble(0.0);
x.majorTickLocations=[NSSet setWithArray:labelTick];
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
x.gridLinesRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( [arrayDataY count] )];
x.labelOffset=30.0f;
x.majorGridLineStyle = yMajorGridLineStyle;
CGFloat dateCount = [self.arrayData count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;
for (NSString *d in self.arrayData)
{
NSLog(#":%#",d);
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:d textStyle:axisSet.xAxis.labelTextStyle];
// [label setRotation:45.0];
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = axisSet.xAxis.majorTickLength;
//label.rotation = M_PI/4;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithFloat:location]];
}
}
x.axisLabels=xLabels;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
gridLineStyle.dashPattern=[NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:1],[NSDecimalNumber numberWithInt:2],nil];
CPTXYAxis *y = axisSet.yAxis;
NSMutableArray *ylabelTick=[[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8", nil];
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.majorIntervalLength = CPTDecimalFromInteger(0.5);
y.minorTicksPerInterval = 0;
y.majorTickLineStyle = lineStyle;
y.minorTickLineStyle = lineStyle1;
y.axisLineStyle = lineStyle2;
y.minorTickLength = 3.0f;
y.majorTickLength = 5.0f;
y.labelOffset=10.0f;
y.orthogonalCoordinateDecimal=CPTDecimalFromDouble(0.0);
y.majorTickLocations=[NSSet setWithArray:ylabelTick];
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
y.gridLinesRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat([arrayData count] )];
y.majorGridLineStyle=gridLineStyle;
CGFloat dateCounty = [self.arrayData count];
NSMutableSet *yLabels = [NSMutableSet setWithCapacity:dateCounty];
NSMutableSet *yLocations = [NSMutableSet setWithCapacity:dateCounty];
NSInteger j = 0;
for (NSString *d in self.arrayDataY)
{
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:d textStyle:axisSet.yAxis.labelTextStyle];
// [label setRotation:45.0];
CGFloat location = j++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = axisSet.yAxis.majorTickLength;
if (j<7) {
[label setAlignment:CPTAlignmentBottom];
}
//label.rotation = M_PI/4;
if (label) {
[yLabels addObject:label];
[yLocations addObject:[NSNumber numberWithFloat:location]];
}
}
y.axisLabels=yLabels;
CPTXYPlotSpace *plotSpace2 =[[CPTXYPlotSpace alloc] init];
plotSpace2.xRange = plotSpace.xRange;
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
[plotSpace2 setAllowsUserInteraction:YES];
[graph addPlotSpace:plotSpace2];
CPTXYAxis *y2 = [[CPTXYAxis alloc] init];
y2.plotSpace =plotSpace2;
y2.labelingPolicy = CPTAxisLabelingPolicyNone;
y2.orthogonalCoordinateDecimal = CPTDecimalFromFloat([arrayData count]);
y2.majorTickLineStyle=lineStyle;
y2.minorTickLineStyle=lineStyle1;
y2.axisLineStyle = lineStyle2;
y2.minorTicksPerInterval = 2;
y2.majorIntervalLength = CPTDecimalFromInteger(1);
y2.minorTickLength=5;
y2.coordinate =CPTCoordinateY;
y2.labelOffset = -30.0f;
y2.majorTickLocations=[NSSet setWithArray:ylabelTick];
y2.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat([arrayDataY count])];
y2.axisLabels=yLabels;
graph.axisSet.axes = [NSArray arrayWithObjects:x, y, y2, nil];
}
Please help me to add multiple y axis with custom label. also i want same x axis values in top of graph also (i.e. have two x axes, one at the top and one at the bottom).
In above image y2 axis label not showing. How to set y2 axis label with custom label
Your approach is correct. All you need now is to create more axes and add them to the axis set. In my example I created an indicator line that moves with the mouse pointer when the mouse is over the graph. You can easily adjust that for fixed x and y axes (see the .coordinate member to determine what type of axis it should be).
// The second y axis is used as the current location identifier.
mainIndicatorLine = [[CPTXYAxis alloc] init];
mainIndicatorLine.hidden = YES;
mainIndicatorLine.coordinate = CPTCoordinateY;
mainIndicatorLine.plotSpace = plotSpace;
mainIndicatorLine.axisConstraints = [CPTConstraints constraintWithLowerOffset: 0];
mainIndicatorLine.labelingPolicy = CPTAxisLabelingPolicyNone;
mainIndicatorLine.separateLayers = YES;
mainIndicatorLine.preferredNumberOfMajorTicks = 6;
mainIndicatorLine.minorTicksPerInterval = 0;
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineWidth = 1;
lineStyle.lineColor = [CPTColor colorWithGenericGray: 64 / 255.0];
lineStyle.lineCap = kCGLineCapRound;
lineStyle.dashPattern = #[#10.0f, #5.0f];
mainIndicatorLine.axisLineStyle = lineStyle;
mainIndicatorLine.majorTickLineStyle = nil;
// Add the mainIndicatorLine to the axis set.
// It is essential to first assign the axes to be used in the arrayWithObject call
// to local variables or all kind of weird things start showing up later (mostly with invalid coordinates).
CPTXYAxisSet *axisSet = (id)mainGraph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
CPTXYAxis *y = axisSet.yAxis;
axisSet.axes = #[x, y, mainIndicatorLine];

core plot - broken line graph

I am trying to produce a simple line graph using core plot and am having trouble getting a solid line. The issue I am having, is that it seems the line is defaulting to a dashed line.
I am new to iOS and Xcode development so forgive me if it's painfully simple. I have attached my code, can anyone see what am I missing?
thanks in advance!
The code is based on the example code included with the project:
-(void)constructScatterPlot
{
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
graphView.hostedGraph = graph;
graph.plotAreaFrame.borderLineStyle = nil;
graph.paddingLeft = 0;
graph.paddingTop = 0;
graph.paddingRight = 0;
graph.paddingBottom = 0;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(100.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(20.0)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelFormatter = nil;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.axisLineStyle = nil;
CPTXYAxis *y = axisSet.yAxis;
y.labelFormatter = nil;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.axisLineStyle = nil;
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
lineStyle.lineWidth = 1.f;
lineStyle.lineColor = [CPTColor whiteColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
}
Is it really a dashed line, or just anti-aliasing artifacts? Try increasing the line width and/or changing the line color to rule that out first.

Resources