CPTScatterPlot not showing - ipad

I am using CPTScatterPlot for showing graphs in iPad. The code runs properly but the points are not plotted on the graph. Not even a single point is shown. Why is it so???
My code is:
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"Red Plot";
CPTMutableLineStyle *symbolLineStyle2 = [CPTLineStyle lineStyle];
symbolLineStyle2.lineColor = [CPTColor redColor];
symbolLineStyle2.lineWidth = 5.0;
//symbolLineStyle2.miterLimit = 1.0f;
dataSourceLinePlot.dataLineStyle = symbolLineStyle2;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot];
// Animate in the new plot, as an example
dataSourceLinePlot.opacity = 0.0f;
[graph addPlot:dataSourceLinePlot];
I checked both the delegate methods. Both return proper values but graph is not plotted what could be the reason. Please help...

Remove this line:
dataSourceLinePlot.opacity = 0.0f;

Related

How to set bounds on a plotspace in CorePlot?

I am trying to draw multiple scatter plots on the same graph using Core-Plot. They have the same x-axis (time), but I want them to be drawn independently of each other. For eg, for two plots, I want the screen to be divided into two parts, the upper part showing one plot and the lower part showing the other (I intend to ultimately plot a real-time multi-channel input). Something like this:
I tried modifying the tutorial given in the following link to use two plotspaces instead of one:
http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2
However, there is no function to set the bounds of a plotspace. The functions xRange and yRange change the range of the axes, but both the plotspaces still occupy the whole screen.
Following is a code snippet that shows two ways in which I tried to accomplish setting bounds:
-(void)configurePlots {
//Get graph and plot space
CPTGraph *graph = self.hostView.hostedGraph;
CPTXYPlotSpace *aaplPlotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
//**Setting xRange and yRange doesn't work**
aaplPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(0.0)
length:CPTDecimalFromCGFloat(graph.bounds.size.width)];
aaplPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat(0.0)
length:CPTDecimalFromCGFloat((graph.bounds.size.height)/3)];
CPTXYPlotSpace *googPlotSpace = [[CPTXYPlotSpace alloc] init];
googPlotSpace.xRange = aaplPlotSpace.xRange;
googPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromCGFloat((graph.bounds.size.height)/3)
length:CPTDecimalFromCGFloat((graph.bounds.size.height)/3)];
[graph addPlotSpace:googPlotSpace];
//Create Plots
//**Trying to set bounds using initWithFrame doesn't work either**
CGRect rect = CGRectMake(0.0, 0.0, graph.bounds.size.width, graph.bounds.size.height/2);
CPTScatterPlot *aaplPlot = [[CPTScatterPlot alloc] initWithFrame:rect];
aaplPlot.dataSource = self;
aaplPlot.identifier = CPDTickerSymbolAAPL;
CPTColor *aaplColor = [CPTColor redColor];
[graph addPlot:aaplPlot toPlotSpace:aaplPlotSpace];
rect = CGRectMake(0.0, graph.bounds.size.height/2, graph.bounds.size.width, graph.bounds.size.height/2);
CPTScatterPlot *googPlot = [[CPTScatterPlot alloc] initWithFrame:rect];
googPlot.dataSource = self;
googPlot.identifier = CPDTickerSymbolGOOG;
CPTColor *googColor = [CPTColor greenColor];
[graph addPlot:googPlot toPlotSpace:googPlotSpace];
//[graph addPlot:googPlot];
//Set up Plot space
[aaplPlotSpace scaleToFitPlots:[NSArray arrayWithObject:aaplPlot]];
CPTMutablePlotRange *xaRange = [aaplPlotSpace.xRange mutableCopy];
[xaRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
aaplPlotSpace.xRange = xaRange;
CPTMutablePlotRange *yaRange = [aaplPlotSpace.yRange mutableCopy];
[yaRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
aaplPlotSpace.yRange = yaRange;
//Create styles and symbols
CPTMutableLineStyle *aaplLineStyle = [aaplPlot.dataLineStyle mutableCopy];
aaplLineStyle.lineWidth = 2.5;
aaplLineStyle.lineColor = aaplColor;
aaplPlot.dataLineStyle = aaplLineStyle;
CPTMutableLineStyle *aaplSymbolLineStyle = [CPTMutableLineStyle lineStyle];
aaplSymbolLineStyle.lineColor = aaplColor;
CPTPlotSymbol *aaplSymbol = [CPTPlotSymbol ellipsePlotSymbol];
aaplSymbol.fill = [CPTFill fillWithColor:aaplColor];
aaplSymbol.lineStyle = aaplSymbolLineStyle;
aaplSymbol.size = CGSizeMake(6.0f, 6.0f);
aaplPlot.plotSymbol = aaplSymbol;
CPTMutableLineStyle *googLineStyle = [googPlot.dataLineStyle mutableCopy];
googLineStyle.lineWidth = 1.0;
googLineStyle.lineColor = googColor;
googPlot.dataLineStyle = googLineStyle;
CPTMutableLineStyle *googSymbolLineStyle = [CPTMutableLineStyle lineStyle];
googSymbolLineStyle.lineColor = googColor;
CPTPlotSymbol *googSymbol = [CPTPlotSymbol starPlotSymbol];
googSymbol.fill = [CPTFill fillWithColor:googColor];
googSymbol.lineStyle = googSymbolLineStyle;
googSymbol.size = CGSizeMake(6.0f, 6.0f);
googPlot.plotSymbol = googSymbol;
}
My question is, is my current approach correct in trying to set bounds on plotspaces or do I need to draw different graphs on the same screen and set the bounds on the graphs? And how do I set bounds on plotspaces or graphs (whichever is the right way!)?
Thanks!

Remove blank space around scatter graph in core plot

I'm using core plot for line graph.in the scatter graph i'm getting some blank space around the scatter graph.as you can see the screen shot(in debug mode) my graph plot area is covered by empty space.How can i remove it?
Initialising host view
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-10)];
self.hostView.backgroundColor = [UIColor clearColor];;
self.hostView.allowPinchScaling = NO;
[self addSubview:self.hostView];
Initializing graph
graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];
self.hostView.hostedGraph = graph;
EDIT
graph.plotAreaFrame.PaddingLeft = 55.0f;
graph.plotAreaFrame.PaddingRight = 15.0f;
graph.plotAreaFrame.PaddingTop = 10.0f;
graph.plotAreaFrame.paddingBottom = 28.0;
These are the paddings i have provided.
The graph starts with 20 pixels of padding on each edge but that's easy to change:
graph.PaddingLeft = 0.0;
graph.PaddingTop = 0.0;
graph.PaddingRight = 0.0;
graph.PaddingBottom = 0.0;

Black color squared border appear in core plot piechart

CPTXYGraph *graph = [[CPTXYGraph alloc]initWithFrame:self.hostingView.frame];
graph.borderColor = [CPTColor whiteColor].cgColor;
graph.paddingTop = 0.0f;
graph.paddingRight = 0.0f;
graph.paddingLeft = 0.0f;
graph.paddingBottom = 0.0f;
self.hostingView.hostedGraph = graph;
graph.axisSet = nil;
//Apply for theme to graph
self.graphTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph applyTheme:self.graphTheme];
[graph release];
CPTPieChart *pieChart = [[CPTPieChart alloc]init];
pieChart.identifier = #"OverView";
pieChart.dataSource = self;
pieChart.sliceDirection = CPTPieDirectionClockwise;
pieChart.pieRadius = ((self.hostingView.frame.size.height / 2) - 5);
pieChart.startAngle = M_PI;
CPTGraph *tempGraph = self.hostingView.hostedGraph;
[tempGraph addPlot:pieChart];
[pieChart release];
For this code i am getting black colored border ,i thought that is due to graph axisSet but i don't how to remove.
If what you're talking about is the border box that appears around the entire graph, I would say use this code after the statement [graph applyTheme:self.graphTheme];:
graph.plotAreaFrame.borderLineStyle = nil;
This removes the border completely from the graph area.

Core-Plot iOS - Adding multiple charts to a screen only every second chart displays

I have a parent view with 6 UIViews to which I add my core-plot charts (This is working fine but only every second chart displays for some reason!)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if(!chartVC1){
chartVC1 = [[ChartVC alloc] initWithIdentifier:#"plot1"];
//[chartVC1 setKPlotIdentifier:#"plot1"];
[chartVC1.view setFrame:self.Panel1.frame];
}
[self.Panel1 addSubview:chartVC1.view];
if(!chartVC3){
chartVC3 = [[ChartVC alloc] initWithIdentifier:#"plot2"];
[chartVC3.view setFrame:self.Panel3.frame];
}
[self.Panel3 addSubview:chartVC3.view];
if(!chartVC2){
chartVC2 = [[ChartVC alloc] initWithIdentifier:#"plot3"];
[chartVC2.view setFrame:self.Panel2.frame];
}
[self.Panel2 addSubview:chartVC2.view];
if(!chartVC4){
chartVC4 = [[ChartVC alloc] initWithIdentifier:#"plot4"];
[chartVC4.view setFrame:self.Panel4.frame];
}
[self.Panel4 addSubview:chartVC4.view];
if(!chartVC5){
chartVC5 = [[ChartVC alloc] initWithIdentifier:#"plot5"];
[chartVC5.view setFrame:self.Panel5.frame];
}
[self.Panel5 addSubview:chartVC5.view];
if(!chartVC6){
chartVC6 = [[ChartVC alloc] initWithIdentifier:#"plot6"];
[chartVC6.view setFrame:self.Panel6.frame];
}
[self.Panel6 addSubview:chartVC6.view];
}
Here is some of my code for the class ChartVC
-(void)renderInLayer
{
CPTXYGraph *graph = [[[CPTXYGraph alloc] initWithFrame:self.view.bounds] autorelease];
[self addGraph:graph toHostingView:self.view];
[self applyTheme:nil toGraph:graph withDefault:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
//[self setTitleDefaultsForGraph:graph withBounds:self.view.bounds];
//[self setPaddingDefaultsForGraph:graph withBounds:self.view.bounds];
graph.plotAreaFrame.paddingTop = -20.0;
graph.plotAreaFrame.paddingRight = -20.0;
graph.plotAreaFrame.paddingBottom = -20.0;
graph.plotAreaFrame.paddingLeft = -20.0;
graph.plotAreaFrame.plotArea.frame = self.view.bounds;
graph.backgroundColor = [[UIColor whiteColor] CGColor];
// Grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 0.75;
majorGridLineStyle.lineColor = [[CPTColor colorWithGenericGray:0.2] colorWithAlphaComponent:0.75];
CPTMutableLineStyle *minorGridLineStyle = [CPTMutableLineStyle lineStyle];
minorGridLineStyle.lineWidth = 0.25;
minorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.1];
// Axes
// X axis
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
x.majorGridLineStyle = majorGridLineStyle;
x.minorGridLineStyle = minorGridLineStyle;
x.minorTicksPerInterval = 9;
//x.title = #"X Axis";
//x.titleOffset = 35.0;
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.numberStyle = NSNumberFormatterNoStyle;
x.labelFormatter = labelFormatter;
[labelFormatter release];
// Y axis
CPTXYAxis *y = axisSet.yAxis;
y.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
y.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
y.majorGridLineStyle = majorGridLineStyle;
y.minorGridLineStyle = minorGridLineStyle;
y.minorTicksPerInterval = 3;
//y.labelOffset = 5.0;
//y.title = #"Y Axis";
//y.titleOffset = 30.0;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
// Rotate the labels by 45 degrees, just to show it can be done.
x.labelRotation = M_PI * 0.25;
// Create the plot
CPTScatterPlot *dataSourceLinePlot1 = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot1.identifier = [self kPlotIdentifier];
//NSLog(#"set identifier: %#", [self kPlotIdentifier]);
dataSourceLinePlot1.cachePrecision = CPTPlotCachePrecisionDouble;
CPTMutableLineStyle *lineStyle1 = [[dataSourceLinePlot1.dataLineStyle mutableCopy] autorelease];
lineStyle1.lineWidth = 1.0;
lineStyle1.lineColor = [CPTColor redColor];
dataSourceLinePlot1.dataLineStyle = lineStyle1;
dataSourceLinePlot1.dataSource = self;
[graph addPlot:dataSourceLinePlot1];
// Plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(kMaxDataPoints - 1)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromUnsignedInteger(0) length:CPTDecimalFromUnsignedInteger(100)];
}
I would think the issue was my own fault if only the 1st chart displayed but because every second chart displays it doesn't make any sense to me..
My only guess is it is some sort of timing issue but I debugged it and delayed the creation of each chart and it made no difference..
I even removed the 3rd, 5th charts to see if the others now rendered but the same 3 charts that did not display previously were still not visible which really threw me off.. (ie. only the 1st chart displayed out of the 4 enabled!)
Any help on this much appreciated...
I may just use the code from the Apple Developer Library that renders the accelerometer data as it is very lightweight to support 6 charts on a screen at one time without issue...
Core-Plot just doesn't seem up to the task I guess...
You didn't say which graphs aren't showing up, but since it's always the same ones, I suspect there's an issue with the view hierarchy. Try changing
`[chartVC1.view setFrame:self.Panel1.frame];`
to
`[chartVC1.view setFrame:self.Panel1.bounds];`
for each graph.

core plot help to draw two plots together

How to draw both bar and scatter plot together? I have created object of both in viewDidLoad and the datasource also read correctly. I add it to the same plotspace. But no use!
Have you looked at the example programs? Many of them show how to add multiple plots (of the same or different types) to a single graph.
What are you seeing? Does either plot show up, or neither of them?
Just add multiple plots to graph with graph addPlot
CPTScatterPlot * sPlot = [[CPTScatterPlot alloc] init];
dataLineStyle = [CPTMutableLineStyle lineStyle];
sPlot.identifier = #"SomeIdentifier"
dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];
sPlot.dataLineStyle = dataLineStyle;
sPlot.dataSource = self;
greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor redColor]];
greenCirclePlotSymbol.size = CGSizeMake(5.0, 5.0);
sPlot.plotSymbol = greenCirclePlotSymbol;
[graph addPlot:sPlot];

Resources