Search code examples
ioscolorspositioncore-plot

Axis Position and Plot Color


I have got two problems.

  1. 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.

  2. 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:

enter image description here

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.


Solution

    1. 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?

    2. Change this line:

      exercisePlot.dataLineStyle = calorieLineStyle;
      

      to

      exercisePlot.dataLineStyle = exerciseLineStyle;