How to show Line label with two digits after dote? - ios

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;

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 x-axis title?

i develop application with Xcode to iOS 8.
I want to create a temperature graph.
I added values to graph it shows me the graph but the x-axis and x-title are hidden.
How can i fix this issue?
this is my functions:
#import "CorePlot-CocoaTouch.h"
#property CPTScatterPlot *linePlot;
#property (nonatomic, strong) CPTGraphHostingView *hostView;
#property (nonatomic, strong) CPTGraph *graph;
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;
}
Add values to graph
-(void)addHRValueToGraph:(int)data
{
[tmpValues addObject:[NSDecimalNumber numberWithInt:data]];
if ([tmpValues count] > plotXMaxRange) {
plotXMaxRange = plotXMaxRange + plotXMaxRange;
plotXInterval = plotXInterval + plotXInterval;
[self updatePlotSpace];
}
[self.graph reloadData];
}
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
LogDebug(#"viewDidLoad");
tmpValues = [[NSMutableArray alloc]init];
tmpDBvalues= [[NSMutableArray alloc]init];
plotXMaxRange = 24;
plotXMinRange = 1;
plotYMaxRange = 5;
plotYMinRange = 35
plotYInterval = 1;
plotXInterval = 2;
}
The X Axis needs to know where to draw in relation to the Y axis.
Try adding in this line:
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(35);

Core plot line graph start at origin [duplicate]

I have the following bar graph. I want the y-axis to be present at x = 2005 (x range 2006-2012), however it seems to only want to show up when I change the x-axis range to 0-whatever:
Here is my code where I configure the plot:
//Create graph and set it as host view's graph
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[self.hostView setHostedGraph:self.graph];
//set graph padding and theme
self.graph.plotAreaFrame.paddingTop = 10.0f;
self.graph.plotAreaFrame.paddingRight = 10.0f;
self.graph.plotAreaFrame.paddingBottom = 60.0f;
self.graph.plotAreaFrame.paddingLeft = 60.0f;
[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
//set axes ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:
CPTDecimalFromFloat(2005) //min year minus 1
length:CPTDecimalFromFloat((8))]; //year difference plus 2
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:
CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat((700))]; // round up to next 50
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//set axes' title, labels and their text styles
CPTMutableTextStyle *axisStyle = [CPTMutableTextStyle textStyle];
axisStyle.fontName = #"ArialRoundedMTBold";
axisStyle.fontSize = 20;
axisStyle.color = [CPTColor whiteColor];
//label text style
CPTMutableTextStyle *labelStyle = [CPTMutableTextStyle textStyle];
labelStyle.fontName = #"ArialRoundedMTBold";
labelStyle.fontSize = 16;
labelStyle.color = [CPTColor whiteColor];
axisSet.xAxis.title = #"Year";
axisSet.yAxis.title = #"Distance (mi)";
axisSet.xAxis.titleTextStyle = axisStyle;
axisSet.yAxis.titleTextStyle = axisStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.yAxis.titleOffset = 30.0f;
axisSet.xAxis.labelTextStyle = labelStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.labelTextStyle = labelStyle;
axisSet.yAxis.labelOffset = 3.0f;
//set axes' line styles and interval ticks
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 3.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(100.0f);
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(50.0f);
axisSet.xAxis.majorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 5.0f;
// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:#"0.75"]
decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:#"0.0"]
decimalValue];
plot.barCornerRadius = 5.0;
// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor clearColor];
plot.lineStyle = borderLineStyle;
// Identifiers are handy if you want multiple plots in one graph
plot.identifier = #"yearlymiles";
[self.graph addPlot:plot];
There are two ways to control that:
Set the orthogonalCoordinateDecimal property. For example,
axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(2005.0);
Use constraints. For example,
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

How to change plot colour

i am using the core plot library for displaying scatter graph.i am displaying two plots in the same graph, i want to display 2nd plot is different colour. how to display to change the 2nd plot colour please help me. i am using below code
// Start with some simple sanity checks before we kick off
if ( (self.hostingView == nil) || (self.graphData == nil) ) {
NSLog(#"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or data.");
return;
}
if ( self.graph != nil ) {
NSLog(#"TUTSimpleScatterPlot: Graph object already exists.");
return;
}
// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[[CPTXYGraph alloc] initWithFrame:frame] autorelease];
// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 30.0f;
self.graph.plotAreaFrame.paddingBottom = 50.0f;
self.graph.plotAreaFrame.paddingLeft = 50.0f;
// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;
// If you want to use one of the default themes - apply that here.
//[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
// Create a line style that we will apply to the axis and data line.
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 2.0f;
// Create a text style that we will use for the axis labels.
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = #"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor whiteColor];
// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol snowPlotSymbol];
plotSymbol.lineStyle = lineStyle;
plotSymbol.size = CGSizeMake(8.0, 8.0);
// Setup some floats that represent the min/max values on our axis.
float xAxisMin = 0;
float xAxisMax = 120;
float yAxisMin = -200;
float yAxisMax = 400;
// We modify the graph's plot space to setup the axis' min / max values.
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];
// Modify the graph's axis with a label, line style, etc.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f);
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
// Define some custom labels for the data elements
axisSet.xAxis.labelRotation = M_PI / 2;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:10], [NSDecimalNumber numberWithInt:20],[NSDecimalNumber numberWithInt:30],[NSDecimalNumber numberWithInt:40],[NSDecimalNumber numberWithInt:50],[NSDecimalNumber numberWithInt:60],[NSDecimalNumber numberWithInt:70],[NSDecimalNumber numberWithInt:80],[NSDecimalNumber numberWithInt:90],[NSDecimalNumber numberWithInt:100],[NSDecimalNumber numberWithInt:110],[NSDecimalNumber numberWithInt:120],nil];
NSArray *xAxisLabels= [NSArray arrayWithObjects:#"Jan",#"Feb", #"Mar",#"Apr",#"May",#"Jun",#"Jul",#"Aug",#"Sep",#"Oct",#"Nov",#"Dec", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for ( NSNumber *tickLocation in customTickLocations ) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:labelLocation++] textStyle:axisSet.xAxis.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
NSLog(#"%f",newLabel.offset);
newLabel.rotation = M_PI / 4;
[customLabels addObject:newLabel];
[newLabel release];
}
axisSet.xAxis.axisLabels = [NSSet setWithArray:customLabels];
//axisSet.yAxis.title = #"Data Y";
//axisSet.yAxis.titleTextStyle = textStyle;
//axisSet.yAxis.titleOffset = 40.0f;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(100.0f);
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
// Add a plot to our graph and axis. We give it an identifier so that we
// could add multiple plots (data lines) to the same graph if necessary.
CPTScatterPlot *plot = [[[CPTScatterPlot alloc] init] autorelease];
plot.dataSource = self;
plot.identifier = #"mainplot";
plot.dataLineStyle = lineStyle;
plot.plotSymbol = plotSymbol;
[self.graph addPlot:plot];
CPTScatterPlot *plot2 = [[[CPTScatterPlot alloc] init] autorelease];
plot2.dataSource = self;
plot2.identifier = #"secondplot";
plot2.dataLineStyle = lineStyle;
plot2.plotSymbol = plotSymbol;
[self.graph addPlot:plot2];

Axis Position and Plot Color

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;

Resources