flickering iOS core plot Graph - ios

I have a case where my app connects to the server to get data and plot on the graph. When the function to initialise the plot is called, the graph is redrawn at a wrong place and auto corrects itself back where it is supposed to be. This causes a flickering in the process.
I have a rised a timer for every 10 seconds to connect to the server.
Below is the plot initialization code.
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView andData:(NSMutableArray *)data
{
self = [super init];
if ( self != nil ) {
self.hostingView = hostingView;
self.graphData = data;
self.graph = nil;
}
return self;
}
// This does the actual work of creating the plot if we don't already have a graph object.
-(void)initialisePlot:(NSUInteger)indexOfPlot
{
// 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;
//CGRect frame = [self.hostingView bounds];
if(indexOfPlot == 0){
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];
// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 500.0f;
self.graph.plotAreaFrame.paddingRight = 20.0f;
self.graph.plotAreaFrame.paddingBottom = 50.0f;
self.graph.plotAreaFrame.paddingLeft= 20.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:kCPTPlainWhiteTheme]];
// Create a line style that we will apply to the axis and data line.
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor redColor];
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 blackColor];
// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.lineStyle = lineStyle;
plotSymbol.size = CGSizeMake(8.0, 8.0);
// Setup some floats that represent the min/max values on our axis.
float xAxisMin = -10;
float xAxisMax = 10;
float yAxisMin = 0;
float yAxisMax = 100;
// 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 - xAxisMin)];
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.title = #"Data X";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
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;
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(10.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];
plot.dataSource = self;
plot.identifier = #"mainplot";
plot.dataLineStyle = lineStyle;
plot.plotSymbol = plotSymbol;
//[self.graph reloadData];
[self.graph addPlot:plot];
}
if(indexOfPlot == 1){
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];
// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 20.0f;
self.graph.plotAreaFrame.paddingBottom = 600.0f;
self.graph.plotAreaFrame.paddingLeft= 20.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:kCPTPlainWhiteTheme]];
// Create a line style that we will apply to the axis and data line.
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor redColor];
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 blackColor];
// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol crossPlotSymbol];
plotSymbol.lineStyle = lineStyle;
plotSymbol.size = CGSizeMake(8.0, 8.0);
// Setup some floats that represent the min/max values on our axis.
float xAxisMin = -10;
float xAxisMax = 10;
float yAxisMin = 0;
float yAxisMax = 100;
// 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 - xAxisMin)];
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.title = #"Data X";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
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;
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(10.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];
plot.dataSource = self;
plot.identifier = #"mainplot";
plot.dataLineStyle = lineStyle;
plot.plotSymbol = plotSymbol;
//[self.graph reloadData];
[self.graph addPlot:plot];
}
}
Do i have to reconsider the way it is being initialised?

Initialize the frame. You probably have junk values in there when graph is initialized that then get corrected when you add the graph to the hosting view.
CGRect frame = [self.hostingView bounds];
or even just
CGRect frame = CGRectZero;

Related

Despite setting xRange, data is plotted in the wrong place

I'm trying to plot a histogram. The x-axis range should be about 881 to 1281 (I've verified that I'm using a location of 881 and range of 400 when constructing the CPTPlotRange for xRange). The axis draws with the correct range, but my bars end up near 2000 (data space), even though my data source method is sending location values between 881 and 1281.
Here's the code:
- (void)
viewDidLoad
{
[super viewDidLoad];
InputVariable temp(1081.0, 50.0);
self.hist = new Histogram(temp.mean(), temp.stdDev(), 5.0);
for (Histogram::SizeT i = 0; i < 1000; ++i)
{
double v = temp.randomValue();
self.hist->add(v);
}
const Histogram& hist = *self.hist;
CPTXYGraph* graph = [[CPTXYGraph alloc] initWithFrame: self.chart1.bounds];
graph.fill = [CPTFill fillWithColor: [CPTColor lightGrayColor]];
graph.cornerRadius = 4.0;
self.chart1.hostedGraph = graph;
self.graph = graph;
// Plot area
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor lightGrayColor]];
graph.plotAreaFrame.paddingTop = 4.0;
graph.plotAreaFrame.paddingBottom = 50.0;
graph.plotAreaFrame.paddingLeft = 50.0;
graph.plotAreaFrame.paddingRight = 4.0;
graph.plotAreaFrame.cornerRadius = 4.0;
graph.plotAreaFrame.masksToBorder = NO;
graph.plotAreaFrame.axisSet.borderLineStyle = [CPTLineStyle lineStyle];
graph.plotAreaFrame.plotArea.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(hist.lowValue()) length:CPTDecimalFromDouble(hist.range())];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(hist.maxBinCount() * 1.2)];
//plotSpace.yScaleType = CPTScaleTypeLog;
// Line styles
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0;
//axisLineStyle.lineCap = kCGLineCapRound;
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [CPTColor redColor];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [CPTColor blueColor];
// Text styles
CPTMutableTextStyle *axisTitleTextStyle = [CPTMutableTextStyle textStyle];
axisTitleTextStyle.fontName = #"Helvetica Neue Bold";
axisTitleTextStyle.fontSize = 14.0;
// Axes
// Label x axis with a fixed interval policy
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.separateLayers = NO;
x.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0.0);
x.majorIntervalLength = CPTDecimalFromDouble(0.5);
x.minorTicksPerInterval = 4;
x.tickDirection = CPTSignNone;
x.axisLineStyle = axisLineStyle;
x.majorTickLength = 12.0;
x.majorTickLineStyle = axisLineStyle;
//x.majorGridLineStyle = majorGridLineStyle;
x.minorTickLength = 8.0;
//x.minorGridLineStyle = minorGridLineStyle;
x.title = #"Temperature";
x.titleTextStyle = axisTitleTextStyle;
x.titleOffset = 25.0;
//x.alternatingBandFills = #[[[CPTColor redColor] colorWithAlphaComponent:0.1], [[CPTColor greenColor] colorWithAlphaComponent:0.1]];
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
// Label y with an automatic label policy.
//axisLineStyle.lineColor = [CPTColor greenColor];
CPTXYAxis *y = axisSet.yAxis;
y.separateLayers = YES;
y.orthogonalCoordinateDecimal = plotSpace.xRange.location;//CPTDecimalFromDouble(hist.lowValue());
y.minorTicksPerInterval = 9;
y.tickDirection = CPTSignNone;
y.axisLineStyle = axisLineStyle;
y.majorTickLength = 12.0;
y.majorTickLineStyle = axisLineStyle;
//y.majorGridLineStyle = majorGridLineStyle;
//y.minorTickLength = 8.0;
//y.minorGridLineStyle = minorGridLineStyle;
y.title = #"Number of Samples";
y.titleTextStyle = axisTitleTextStyle;
y.titleOffset = 40.0;
//y.alternatingBandFills = #[[[CPTColor blueColor] colorWithAlphaComponent:0.1], [NSNull null]];
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
//CPTFill *bandFill = [CPTFill fillWithColor:[[CPTColor darkGrayColor] colorWithAlphaComponent:0.5]];
//[y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(7.0) length:CPTDecimalFromDouble(1.5)] fill:bandFill]];
//[y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.5) length:CPTDecimalFromDouble(3.0)] fill:bandFill]];
// Set up the bar plot…
//CPTXYPlotSpace *barPlotSpace = [[CPTXYPlotSpace alloc] init];
CPTXYPlotSpace *barPlotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
//barPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(200.0)];
//barPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(15.00)];
//[self.graph addPlotSpace:barPlotSpace];
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars: false];
barPlot.dataSource = self;
barPlot.barsAreHorizontal = false;
barPlot.barWidth = CPTDecimalFromDouble(1.0);
barPlot.barBasesVary = false;
barPlot.baseValue = CPTDecimalFromDouble(0.0);
barPlot.barOffset = CPTDecimalFromDouble(hist.lowValue());
barPlot.identifier = #"Bar Plot";
//barPlot.plotRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0.0) length:CPTDecimalFromDouble(7.0)];
CPTMutableTextStyle *whiteTextStyle = [CPTMutableTextStyle textStyle];
whiteTextStyle.color = [CPTColor whiteColor];
//barPlot.labelTextStyle = whiteTextStyle;
[self.graph addPlot:barPlot toPlotSpace:barPlotSpace];
}
- (NSUInteger)
numberOfRecordsForPlot: (CPTPlot*) inPlot
{
return self.hist->numBins();
}
- (double)
doubleForPlot: (CPTPlot*) inPlot
field: (NSUInteger) inField
recordIndex: (NSUInteger) inIdx
{
const Histogram& h = *self.hist;
if (inField == CPTBarPlotFieldBarLocation)
{
double v = h.binMid(inIdx);
NSLog(#"v: %f", v);
return v;
}
else if (inField == CPTBarPlotFieldBarTip)
{
return h[inIdx];
}
return 0.0;
}
And the resulting graph:
Any idea what I'm doing wrong? Thanks!
For this application, the barOffset should be zero (0). The offset is used to displace the bars from the location given by the datasource. The most common use is to separate the bars at the same location when plotting multiple bar plots on the same graph that might have overlapping bars at common locations.

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;

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 add multiple core plot plots on one screen

Ok I'm stumped. I'm trying to use Core Plot 1.2 in my iOS app and I am trying to add two plots onto one screen. My code for my View Controller is below. There is a NIB file too but it is empty - the default view in just a UIView. I create two subviews of type CPTGraphHostingView, I add these as suvviews of self.view and then I create the graphs. In the code below I even commented out the method that creates one of the graphs to see if I could even get one graph to display, but all I get is a blank white screen.
I am using a simple X**2 function for testing purposes for each plot.
What am I doing wrong? Is there an easier way to get say 2 or 3 separate XY style plots onto a single screen?
#interface SensorPlotSecondViewController : UIViewController <CPTPlotDataSource>
{
CPTGraphHostingView *accelSubview;
CPTGraphHostingView *gyroSubview;
CPTGraphHostingView *magSubview;
CPTXYGraph *accelGraph;
CPTXYGraph *gyroGraph;
CPTXYGraph *magGraph;
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot;
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx;
#end
#implementation SensorPlotSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Second", #"Second");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
CGRect frm1=CGRectMake(0,0,320,150);
accelSubview=[[CPTGraphHostingView alloc] initWithFrame:frm1];
[self.view addSubview:accelSubview];
CGRect frm2=CGRectMake(0,150,320,150);
gyroSubview=[[CPTGraphHostingView alloc] initWithFrame:frm2];
[self.view addSubview:gyroSubview];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createAccelGraph];
//[self createGyroGraph];
}
-(void)createAccelGraph
{
accelGraph = [[CPTXYGraph alloc] initWithFrame: accelSubview.bounds];
//CPTTheme *theme=[CPTTheme themeNamed:kCPTPlainWhiteTheme];
//[accelGraph applyTheme:theme];
accelGraph.plotAreaFrame.borderLineStyle = nil;
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)accelSubview;
hostingView.hostedGraph=accelGraph;
accelGraph.paddingLeft = 0.0;
accelGraph.paddingTop = 0.0;
accelGraph.paddingRight = 0.0;
accelGraph.paddingBottom = 0.0;
//accelGraph.fill=nil;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)accelGraph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)accelGraph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromString(#"5");
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"0") length:CPTDecimalFromString(#"10")];
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(#"5");
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"0") length:CPTDecimalFromString(#"25")];
accelGraph.plotAreaFrame.paddingLeft = 40.0;
accelGraph.plotAreaFrame.paddingTop = 80.0;
accelGraph.plotAreaFrame.paddingRight = 40.0;
accelGraph.plotAreaFrame.paddingBottom = 300.0;
CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] initWithFrame:accelSubview.bounds] autorelease];
//CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease];
xSquaredPlot.identifier = #"X Squared Plot";
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle=dataLineStyle;
xSquaredPlot.dataSource = self;
[accelGraph addPlot:xSquaredPlot];
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
//xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol;
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;
/****
CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] init] autorelease];
xInversePlot.identifier = #"X Inverse Plot";
CPTMutableLineStyle *dataLineStyle2 = [CPTMutableLineStyle lineStyle];
dataLineStyle2.lineWidth = 1.0f;
dataLineStyle2.lineColor = [CPTColor blueColor];
xInversePlot.dataLineStyle=dataLineStyle2;
xInversePlot.dataSource = self;
[accelGraph addPlot:xInversePlot];
****/
}
-(void)createGyroGraph
{
gyroGraph = [[CPTXYGraph alloc] initWithFrame: gyroSubview.bounds];
CPTTheme *theme=[CPTTheme themeNamed:kCPTPlainWhiteTheme];
[gyroGraph applyTheme:theme];
gyroGraph.plotAreaFrame.borderLineStyle = nil;
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)gyroSubview;
hostingView.hostedGraph=gyroGraph;
gyroGraph.paddingLeft = 0.0;
gyroGraph.paddingTop = 0.0;
gyroGraph.paddingRight = 0.0;
gyroGraph.paddingBottom = 0.0;
gyroGraph.fill=nil;
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)gyroGraph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(30)];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor blackColor];
lineStyle.lineWidth = 2.0f;
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)gyroGraph.axisSet;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromString(#"5");
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"0") length:CPTDecimalFromString(#"10")];
axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(#"5");
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.visibleAxisRange=[CPTPlotRange plotRangeWithLocation:CPTDecimalFromString(#"0") length:CPTDecimalFromString(#"25")];
gyroGraph.plotAreaFrame.paddingLeft = 40.0;
gyroGraph.plotAreaFrame.paddingTop = 200.0;
gyroGraph.plotAreaFrame.paddingRight = 40.0;
gyroGraph.plotAreaFrame.paddingBottom = 150.0;
CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] initWithFrame:gyroSubview.bounds] autorelease];
//CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease];
xSquaredPlot.identifier = #"X Squared Plot";
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];
dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
xSquaredPlot.dataLineStyle=dataLineStyle;
xSquaredPlot.dataSource = self;
[gyroGraph addPlot:xSquaredPlot];
CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
//xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol;
xSquaredPlot.plotSymbol = greenCirclePlotSymbol;
/****
CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] init] autorelease];
xInversePlot.identifier = #"X Inverse Plot";
CPTMutableLineStyle *dataLineStyle2 = [CPTMutableLineStyle lineStyle];
dataLineStyle2.lineWidth = 1.0f;
dataLineStyle2.lineColor = [CPTColor blueColor];
xInversePlot.dataLineStyle=dataLineStyle2;
xInversePlot.dataSource = self;
[accelGraph addPlot:xInversePlot];
****/
}
Your padding on the plot area frame is too big. The graph is only 150 pixels high, yet you're trying to inset the plot area by 380 pixels, leaving no room to draw the plots.

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