Custom label setup - font,color,size not being used? - core-plot

From the Plot Gallery sample - LabelingPolicyDemo.m, it uses a label policy of none, and sets each label up using the same style as the axis title:
CPTMutableLineStyle *majorTickLineStyle = [axisLineStyle mutableCopy];
majorTickLineStyle.lineWidth = 3.0;
majorTickLineStyle.lineCap = kCGLineCapRound;
CPTMutableLineStyle *minorTickLineStyle = [axisLineStyle mutableCopy];
minorTickLineStyle.lineWidth = 2.0;
minorTickLineStyle.lineCap = kCGLineCapRound;
// Text styles
CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle];
axisTitleTextStyle.fontName = #"Helvetica-Bold";
axisTitleTextStyle.color = [CPTColor redColor]; // differs from sample
// Tick locations
CPTNumberSet *majorTickLocations = [NSSet setWithObjects:#0, #30, #50, #85, #100, nil];
CPTMutableNumberSet *minorTickLocations = [NSMutableSet set];
for ( NSUInteger loc = 0; loc <= 100; loc += 10 ) {
[minorTickLocations addObject:#(loc)];
}
// Axes
// CPTAxisLabelingPolicyNone
CPTXYAxis *axisNone = [[CPTXYAxis alloc] init];
axisNone.plotSpace = graph.defaultPlotSpace;
axisNone.labelingPolicy = CPTAxisLabelingPolicyNone;
axisNone.orthogonalPosition = #1.0;
axisNone.tickDirection = CPTSignNone;
axisNone.axisLineStyle = axisLineStyle;
axisNone.majorTickLength = majorTickLength;
axisNone.majorTickLineStyle = majorTickLineStyle;
axisNone.minorTickLength = minorTickLength;
axisNone.minorTickLineStyle = minorTickLineStyle;
axisNone.title = #"CPTAxisLabelingPolicyNone";
axisNone.titleTextStyle = axisTitleTextStyle;
axisNone.titleOffset = titleOffset;
axisNone.majorTickLocations = majorTickLocations;
axisNone.minorTickLocations = minorTickLocations;
CPTMutableAxisLabelSet *newAxisLabels = [NSMutableSet set];
for ( NSUInteger i = 0; i <= 5; i++ ) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"Label %lu", (unsigned long)i]
textStyle:axisNone.labelTextStyle];
newLabel.tickLocation = #(i * 20);
newLabel.offset = axisNone.labelOffset + axisNone.majorTickLength / CPTFloat(2.0);
[newAxisLabels addObject:newLabel];
}
axisNone.axisLabels = newAxisLabels;
but the resulting axis label and title should match which they don't - neither size, color or font? The label text style appears constant.
See Axis Labeling Policies sample.

The code in the question sets the red text style to the titleTextStyle and uses the labelTextStyle when creating axis labels. These text styles are independent although you can use the same style for both if you want.

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;

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 multiple y axis alignment issues - possibly related to scaling

My Issues is as follows:
I'm trying to graph two data sets (One is in Feet and one is in degrees) in such a way that they can be compared (they do share a common x-axis value).
As you can see my data seems to be interlaced correctly but I'm having trouble getting the yAxises to behave as I'd like. I would like the X-Axis Max/Min to actually line up with both Y-Axises. My code is as follows:
- (void)initPlot {
[self loadCoreData];
[self configureHost];
[self configureGraph];
[self configurePlots];
[self configureAxes];
}
- (void)configureHost {
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.view.bounds];
self.hostView.allowPinchScaling = YES;
[self.view addSubview:self.hostView];
}
- (void)configureGraph {
// 1 - Create the graph
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
self.hostView.hostedGraph = graph;
// 2 - Set graph title
NSString *title = [NSString stringWithFormat:#"Altitude Plot [%# to %#]", self.flightRecording.originAirport, self.flightRecording.destinationAirport];
graph.title = title;
// 3 - Create and set text style
CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = #"Helvetica-Bold";
titleStyle.fontSize = 16.0f;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -20.0f);
// 4 - Set padding for plot area
float padding = 10.0f;
[graph.plotAreaFrame setPaddingLeft:padding];
[graph.plotAreaFrame setPaddingBottom:50.0f];
[graph.plotAreaFrame setPaddingRight:padding];
[graph.plotAreaFrame setPaddingTop:padding];
// 5 - Enable user interactions for plot space
altSpace= (CPTXYPlotSpace *) graph.defaultPlotSpace;
altSpace.allowsUserInteraction = NO;
rollSpace = [[CPTXYPlotSpace alloc] init];
rollSpace.allowsUserInteraction = NO;
[self.hostView.hostedGraph addPlotSpace:rollSpace];
//
// pitchSpace = [[CPTXYPlotSpace alloc] init];
// pitchSpace.allowsUserInteraction = NO;
// [self.hostView.hostedGraph addPlotSpace:pitchSpace];
//
//
// slipSpace = [[CPTXYPlotSpace alloc] init];
// slipSpace.allowsUserInteraction = NO;
// [self.hostView.hostedGraph addPlotSpace:slipSpace];
//
//
// gSpace = [[CPTXYPlotSpace alloc] init];
// gSpace.allowsUserInteraction = NO;
// [self.hostView.hostedGraph addPlotSpace:gSpace];
}
- (void)configurePlots {
// 1 - Get graph and plot space
CPTGraph *graph = self.hostView.hostedGraph;
// 2 - Create the three plots
// Custom AHRS Plot
CPTScatterPlot *ahrsAltPlot = [[CPTScatterPlot alloc] init];
ahrsAltPlot.dataSource = self;
ahrsAltPlot.identifier = #"AHRSALT";
CPTColor *ahrsAltColor = [CPTColor yellowColor];
[graph addPlot:ahrsAltPlot toPlotSpace:altSpace];
CPTScatterPlot *rollPlot = [[CPTScatterPlot alloc] init];
rollPlot.dataSource = self;
rollPlot.identifier = #"ROLL";
CPTColor *rollColor = [CPTColor greenColor];
[graph addPlot:rollPlot toPlotSpace:rollSpace];
// 3 - Set up plot space
CPTMutablePlotRange *xRange = [altSpace.xRange mutableCopy];
xRange=[CPTMutablePlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f ) length:CPTDecimalFromFloat(maxEpoch + 300.0f )];
// [xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
altSpace.xRange = xRange;
CPTMutablePlotRange *yRange = [altSpace.yRange mutableCopy];
yRange = [CPTMutablePlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(maxAlt)];
[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
altSpace.yRange = yRange;
rollSpace.xRange = xRange;
CPTMutablePlotRange *yRangeRoll =[CPTMutablePlotRange plotRangeWithLocation:CPTDecimalFromFloat(0 ) length:CPTDecimalFromFloat(40 )];
[yRangeRoll expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
rollSpace.yRange = yRangeRoll;
[altSpace scaleToFitPlots:[NSArray arrayWithObjects:ahrsAltPlot, nil] ];
[rollSpace scaleToFitPlots:[NSArray arrayWithObjects:rollPlot, nil] ];
[rollSpace setYRange:yRangeRoll];
// 4 - Create styles and symbols
// AHRS Style
CPTMutableLineStyle *ahrsLineStyle = [ahrsAltPlot.dataLineStyle mutableCopy];
ahrsLineStyle.lineWidth = 2.5;
ahrsLineStyle.lineColor = [CPTColor blueColor];
ahrsAltPlot.dataLineStyle = ahrsLineStyle;
}
- (void)configureAxes {
// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = #"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = #"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
CPTAxis *x = axisSet.xAxis;
x.title = #"Flight Time";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 15.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignNegative;
NSMutableSet *xLabels = [NSMutableSet new];
NSMutableSet *xLocations = [NSMutableSet new];
NSInteger i = 0;
for (i; i < maxEpoch; i += 600) {
int hour = i / (68 * 60);
int min = i / 60;
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%02d:%02d", hour, min] textStyle:x.labelTextStyle];
CGFloat location = i++;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = x.majorTickLength;
if (label) {
[xLabels addObject:label];
[xLocations addObject:[NSNumber numberWithInteger:i]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xLocations;
// 4 - Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.title = #"Altitude in Feet";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = 20.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 21.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 20.0f;
y.minorTickLength = 10.0f;
y.tickDirection = CPTSignNegative;
// Axis #2
CPTXYAxis *yRoll = [[CPTXYAxis alloc] init];
yRoll.title = #"Degrees";
// Styling
yRoll.titleTextStyle = axisTitleStyle;
yRoll.labelTextStyle = axisTextStyle;
yRoll.axisLineStyle = axisLineStyle;
yRoll.majorTickLineStyle = axisLineStyle;
yRoll.plotSpace = rollSpace;
yRoll.delegate = self;
yRoll.labelingPolicy = CPTAxisLabelingPolicyNone;
yRoll.coordinate = CPTCoordinateY;
yRoll.tickDirection = CPTSignNone;
yRoll.separateLayers = NO;
yRoll.tickDirection = CPTSignNegative;
yRoll.labelOffset = 21.0f;
yRoll.axisConstraints = [CPTConstraints constraintWithLowerOffset:40.0];
yRoll.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
NSMutableSet *rollLabels = [NSMutableSet new];
NSMutableSet *rollLocations = [NSMutableSet new];
for (int i =0; i <= 40; i+= 5) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%02d˚", (i - 20)] textStyle:yRoll.labelTextStyle];
CGFloat location = i;
label.tickLocation = CPTDecimalFromCGFloat(location);
label.offset = -16.0f;
if (label) {
[rollLabels addObject:label];
[rollLocations addObject:[NSNumber numberWithInteger:i]];
}
}
yRoll.axisLabels = rollLabels;
yRoll.majorTickLocations = rollLocations;
NSInteger majorIncrement = 500;
NSInteger minorIncrement = 100;
CGFloat yMax = maxAlt; // should determine dynamically based on max price
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
NSUInteger mod = j % majorIncrement;
if (mod == 0) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:#"%i ft", j] textStyle:y.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j);
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
} else {
[yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
// Position the y2 axis
// y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:40.0];
axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithUpperOffset:40.0];
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;
self.hostView.hostedGraph.axisSet.axes = [NSArray arrayWithObjects:x,y,yRoll,nil];
// Position floating YAxis
// y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:150.0];
}
EDIT::
I tried adding the following lines and they did nothing either
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
I seem to be making progress actually: It appears that if I remove
yAxis2.axisConstraints = [CPTConstraints constraintWithLowerOffset:40.0];
Then my axis actually seems to pin to the x-axis (although its off the screen) now but its a start.
Problem Solved:
I had some conflicting directives going on.
In order to achieve what I needed I had to use the two calls:
yAxis2.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(maxEpoch); // maximum X value
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.0f);
Then in order to make sure everything could be seen correctly I had to play with the padding values:
float padding = 50.0f;
[graph.plotAreaFrame setPaddingLeft:padding];
[graph.plotAreaFrame setPaddingBottom:50.0f];
[graph.plotAreaFrame setPaddingRight:padding];
[graph.plotAreaFrame setPaddingTop:padding];
And I had to remove all the constraints that were messing with things:
// yAxis2.axisConstraints = [CPTConstraints constraintWithLowerOffset:40.0];
// y.axisConstraints = [CPTConstraints constraintWithUpperOffset:150.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];

Resources