draw a dotted line on CorePlot graph in iOS - ios

How to draw a dotted line on graph using core plot plot, that line is for maximum and minimum range of axis.
In AndroidPlot, ValueMarker is for drawing a maximum and minimum horizontal line on graph.
Please help me on this.
Thanks

This is what I used for such situation:
#property (nonatomic, retain) CPTScatterPlot *myLine;
#property (nonatomic, retain) CPTGraph *graph;
-(void) configureHorizontalLine
{
// 1 - Create plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graph.defaultPlotSpace;
// 2 - Create the plot
self.myLine = [[CPTScatterPlot alloc] init];
self.myLine.dataSource = self;
self.myLine.identifier = #"myLine";
CPTColor *myPlotColor = [CPTColor redColor];
[self.graph addPlot:self.myLine toPlotSpace:plotSpace];
// 3 - Create styles
CPTMutableLineStyle *myPlotLineStyle = [self.myLine.dataLineStyle mutableCopy];
myPlotLineStyle.dashPattern=[NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:3],[NSDecimalNumber numberWithInt:3],nil]; //dashed line
myPlotLineStyle.lineWidth = 1;
myPlotLineStyle.lineColor = myPlotColor;
self.myLine.dataLineStyle = myPlotLineStyle;
}
Also in CPTPlotDataSource methods you must do the following check to identify line plot:
if ([[plot identifier] isEqual:#"myLine"])

Related

Scrolling through data only in core plot iOS

Currently I'm using core plot cocoa pod to draw a scatter graph, currently I need scrolling to be enabled but at the same time I need the x & y axis the be fixed on the corner of the screen and only move through data any way to do this ?.
My current implementation for the following code makes me scrolls through data but x-axis or y-axis disappears when I scroll far through the graph
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CPTGraphHostingView* hostView = [[CPTGraphHostingView alloc] initWithFrame:self.view.frame];
[self.view addSubview: hostView];
// Create a CPTGraph object and add to hostView
CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
hostView.hostedGraph = graph;
// Get the (default) plotspace from the graph so we can set its x/y ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction=YES;
// Note that these CPTPlotRange are defined by START and LENGTH (not START and END) !!
[plotSpace setYRange:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:10]]];
[plotSpace setXRange:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:10]]];
plotSpace.delegate = self;
graph.paddingLeft = 10.0;
graph.paddingRight = 10.0;
graph.paddingTop = 10.0;
graph.paddingBottom = 10.0;
// Create the plot (we do not define actual x/y values yet, these will be supplied by the datasource...)
CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
// Let's keep it simple and let this class act as datasource (therefore we implemtn <CPTPlotDataSource>)
plot.dataSource = self;
// Finally, add the created plot to the default plot space of the CPTGraph object we created before
[graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
}
Thanks in advance :)
Use the axisConstraints:
x.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0.0];
y.axisConstraints = [CPTConstraints constraintWithRelativeOffset:0.0];
Use 0.0 to place the axis to the left (y-axis) or bottom (x-axis). Use 1.0 to place the axis to the right (y-axis) or top (x-axis). Any fraction between 0.0 and 1.0 is valid.

CorePlot multiple scatterplots huge memory consumption

I'm experiencing some problems with CorePlot. I try to plot multiple scatterPlots into one graph. Which works as expected but when I start scrolling or zooming the graph the whole app increases it's memory usage up to 900MB an crashes. I think I have to do some object releasing but I don't know how. Basically I plot each line with a different plot identifier and put the according data into the datasource.
Here is what I got: (In this example code I just reduced the axe ranges with static values for testing purposes.)
- (void)setupGraph {
// Create graph from theme
self.graph = [[CPTXYGraph alloc] initWithFrame:self.scatterPlotView.bounds];
self.graph.plotAreaFrame.masksToBorder = NO;
self.scatterPlotView.hostedGraph = self.graph;
CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainBlackTheme];
[self.graph applyTheme:theme];
self.graph.paddingLeft = 0.0;
self.graph.paddingTop = 0.0;
self.graph.paddingRight = 0.0;
self.graph.paddingBottom = 0.0;
// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate = self;
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(30)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(15)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(10)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble(15)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
CPTXYAxis *y = axisSet.yAxis;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
}
This method loads my data and draws the scatterplots
- (void)loadSignals {
//Data loading logic goes here nothing special just one array for each plot
for (Signal *sig in [signals allValues]) {
[self constructScatterPlot:sig.name];
}
}];
This is where the drawing happens:
- (void)constructScatterPlot:(NSString *)identifier {
CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy];
CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc] init];
boundLinePlot.identifier = identifier;
lineStyle = [boundLinePlot.dataLineStyle mutableCopy];
lineStyle.miterLimit = 1.0;
lineStyle.lineWidth = 1.0;
lineStyle.lineColor = [CPTColor redColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.dataSource = self;
boundLinePlot.cachePrecision = CPTPlotCachePrecisionDouble;
boundLinePlot.interpolation = CPTScatterPlotInterpolationStepped;
[self.graph addPlot:boundLinePlot];
}
...and this is where the datasource gets it's values:
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSString *plotIdent = (NSString *) plot.identifier;
if (![plotIdent isEqualToString:self.currentIdent]) {
self.countPlot++;
self.currentIdent = plotIdent;
}
Signal *newSig = [self.signals objectForKey:plotIdent];
Value * newValue = [newSig valueAtTime:index];
NSNumber *number = [NSNumber numberWithInteger:[newValue.value integerValue]];
if ( fieldEnum == CPTScatterPlotFieldY ) {
return number;
}
if ( fieldEnum == CPTScatterPlotFieldX ) {
return [NSNumber numberWithInteger:index];
}
return nil;
}
So first of all I'd like to know if this is the correct way of drawing multiple plots (up to 40 in my case) into one graph? If so, what could I do to optimize my plot performance?
The output should look like this:
I found this use
"try setting collapsesLayers to YES on the hosting view"
Application get stuck when drawing nearly 40 Scatter Plots using Core Plot in IPad
Each plot is a CALayer that covers the entire plot area of the graph, so with 40 of them you may be using up most of the available video memory. If they all use the same line style, you can plot all of the lines on the same plot and save some memory that way. Insert an extra data point between the line segments and return nil or #(NAN) from the datasource to break the line.

Draw Multiple Piecharts using coreplot

How to draw multiple pie charts using coreplot.Is it possible to draw these side by side?
Regards,
krishna
Yes, Krishna. You can add the multiple pie charts to the same graph like this:
CPTGraphHostingView *hostingView = (CPTGraphHostingView *) self;
CPTXYGraph *graph;
// set up the graph parameters
...
...
hostingView.hostedGraph = graph;
// Pie chart 1
CPTPieChart *pieChart1 = [[CPTPieChart alloc] init];
// Pie Chart 2
CPTPieChart *pieChart2 = [[CPTPieChart alloc] init];
// Add them to the graph
[graph addPlot:pieChart1];
[graph addPlot:pieChart2];

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
}
}

how to rotate label of a bar chart and add offset in core plot?

just wondering if there is a way to rotate the labels placed on bar charts and add offset to it ? Thank you.
Below my delegate implementation. Note the padding on label
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
CPTMutableTextStyle *whiteTextStyle = [[[CPTMutableTextStyle alloc] init] autorelease];
whiteTextStyle.color = [CPTColor whiteColor];
whiteTextStyle.fontSize = 14.0f;
CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:#"Test" style:whiteTextStyle];
label.paddingLeft = -100.0f; // <---
return [label autorelease];
}
Use the labelOffset and labelRotation properties on the plot. These are inherited from CPTPlot by all Core Plot plots.
You should not set the padding in -dataLabelForPlot:recordIndex:.

Resources