core plot help to draw two plots together - ipad

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];

Related

CPTPlotSymbol larger touch area

How can I make the plot symbol touch area bigger. 10x10 is what I need to be filled with color, but it is really hard to touch on the screen with 'fat' fingers. So I need the area around it to activate the symbol, but not to be filled with color.
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor colorWithComponentRed:42./255. green:150./255. blue:232./255. alpha:1.];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:42./255. green:150./255. blue:232./255. alpha:1.]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
dataSourceLinePlot.plotSymbol = plotSymbol;
In your method that you make the plot and the graph there should be a line that goes like this.
_yourGraph.plotSymbolMarginForHitDetection = 20.0f;
I always use 20.0f because of 'fat' fingers too. I have a method that I called configurePlots, I get a reference to the graph and plot space. Then I call the plotSymbol... Hope this helps, ask questions if you have any.

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!

iOS Core Plot - Scatter plot symbol color appearance

I am using CorePlot for Scatter plot and would like to know how to obtain the circles of the scatter plot with such a color effect as it is here http://i.stack.imgur.com/7ZcqY.jpg
I would like to get the color as in CPTXYScatterPlot - Can I set the Z order of plot symbols?.
In your symbolForScatterPlot implementation, create a circlePlotSymbol with a gradient fill, like so:
CPTPlotSymbol *circlePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
// Obviously for a real bubble chart you'll want to get the color from somewhere so they're not all the same
CPTColor *endColor = [CPTColor redColor];
CPTColor *startColor = [endColor colorWithAlphaComponent:0.4f];
CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:startColor endingColor:endColor];
Then set the gradient type to Radial, and set your gradient as the fill on your plotSymbol. If you want it to look like it's lit from an angle, you can set the startAnchor to a point off center:
gradient.gradientType = CPTGradientTypeRadial;
graphGradient.startAnchor = CGPointMake(0.35, 0.75);
circlePlotSymbol.fill = [CPTFill fillWithGradient:gradient];
If you don't want a border around your circles, set the linestyle color to clear and/or set the width to 0.0f (probably don't need to do both):
CPTMutableLineStyle *lineStyle = [[CPTMutableLineStyle alloc] init];
lineStyle.lineColor = [CPTColor clearColor];
lineStyle.lineWidth = 0.0f;
circlePlotSymbol.lineStyle = lineStyle;
[lineStyle release];
Set the size of your bubble:
// For a real bubble chart, you'll need to calculate this from your data
circlePlotSymbol.size = CGSizeMake(50.0f, 50.0f);
and return your plot symbol:
return circlePlotSymbol;
That should do it. I don't have my code in front of me so I'm going from memory here (and please forgive any typos or bad syntax, as I don't have access to a compiler right now either), but hopefully this will be close enough to get you pointed in the right direction.

CPTScatterPlot not showing

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;

How to add offset between two scatter plot graphs using coreplot

I have added two bar graphs with a barOffset so that both bar graphs show side-by-side.
I also want to add two scatter plots using the same data with each scatter graph touching the tip of the each bar graphs respectively.
Here is how i added the two scatter graphs.
//Add line graph 1
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = #"Scatter-Plot-1";
CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor orangeColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
[graph addPlot:dataSourceLinePlot toPlotSpace:barPlotSpace];
//Add line graph 2
CPTScatterPlot *dataSourceLinePlot2 = [[[CPTScatterPlot alloc] init] autorelease];
dataSourceLinePlot2.identifier = #"Scatter-Plot-2";
CPTMutableLineStyle *lineStyle2 = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle2.miterLimit = 1.0f;
lineStyle2.lineWidth = 3.0f;
lineStyle2.lineColor = [CPTColor greenColor];
dataSourceLinePlot2.dataLineStyle = lineStyle2;
dataSourceLinePlot2.dataSource = self;
[graph addPlot:dataSourceLinePlot2 toPlotSpace:barPlotSpace];
But now both scatter graphs start from first bargraph tip itself when datavalue is same
I want each of them touching only bargraph tip.
How can i do that? Is there something barOffset kind of thing for scatter to move it?
I implemented the delegates correctly and data shows up correctly too
If barWidthsAreInViewCoordinates is NO (the default), you can just add or subtract the barOffset from the x or y coordinate in the datasource.
You can adjust the offset of the plotSpace as well
for example to start from 0.4 -
barPlotSpace.xRange = CPTPlotRange(location:0.5, length:NSNumber(value: end))
but in that case your entire plot is shifting.
Or better as suggested above:
in datasource 'number'
public func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any?
{
let idt = plot.identifier as! NSString;
switch CPTBarPlotField(rawValue: Int(field))! {
case .barLocation:
if idt.compare("Scatter-Plot-1") == .orderedSame {
// This will shift the Scatter-Plot-1 to the left
return NSNumber(value: Double(record) + 0.5)
} else {
return record as NSNumber
}
case .barTip:
//Enter your code
return nil
default:
return nil
}
}

Resources