Draw Multiple Piecharts using coreplot - ios

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

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.

How can I create a piechart that looks like a pie using CorePlot?

The graph on the left is the one from the CorePlot gallery demo, the one on the right is the one I created with the code below. I think I am missing something in order to make it look like an actual circular pie:
-(void)configureChart {
// 1 - Get reference to graph
pieChartGraph = [[CPTXYGraph alloc] initWithFrame:self.pieChartgraphHostView.bounds];
self.pieChartgraphHostView.hostedGraph = pieChartGraph;
// 2 - Create chart
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.delegate = self;
// pieChart.pieRadius = (self.pieChartgraphHostView.bounds.size.height * 0.7) / 2;
pieChart.pieRadius = (self.pieChartgraphHostView.bounds.size.height * 0.7) / 2;
pieChart.identifier = pieChartGraph.title;
pieChart.startAngle = CPTFloat(M_PI_4);
pieChart.sliceDirection = CPTPieDirectionClockwise;
pieChart.borderLineStyle = [CPTLineStyle lineStyle];
// 3 - Create gradient
CPTGradient *overlayGradient = [[CPTGradient alloc] init];
overlayGradient.gradientType = CPTGradientTypeRadial;
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.9];
overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.4] atPosition:1.0];
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient];
// 4 - Add chart to graph
[pieChartGraph addPlot:pieChart];
pieChart.dataSource = self;
self.dataForChart = [#[#20.0, #30.0, #60.0] mutableCopy];
//self.dataForChart = [#[#20.0, #30.0, #60.0]mutableCopy];
}
I tried to copy the code from the demo gallery (class called CorePlot from the CorePlot example project here).
What am I doing wrong? Why is my graph a square and not a circle?
Try edit the radius of the chart. That is what makes it a circle. To obtain a circle from a square view you usualy do something like:
squareView.radius = view.frame.size.width/2;
You can try this on you pieChart:
pieChart.pieRadius = pieChart.frame.size.width/2;
And by the way you are setting 2 times the pieChart dataSource

Enable zoom and pan in Coreplot for CPTPieChart

CPTPieChart *pieChart = [[CPTPieChart alloc] init];
pieChart.plotSpace.delegate = self;
pieChart.plotSpace.allowsUserInteraction=YES;
pieChart.labelOffset=-50;
pieChart.dataSource = self;
pieChart.delegate = self;
pieChart.pieRadius = (self.hostView.bounds.size.height * 0.4) / 2;
pieChart.pieInnerRadius=pieChart.pieRadius/2;
pieChart.identifier = graph.title;
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionClockwise;
enabled the userinteraction in plot space my delegate are getting called but the piechart is not zooming and panning.
Get reference to graph
CPTGraph *graph = self.hostView.hostedGraph;
self.hostView.allowPinchScaling=YES;
Pie charts don't support pinch-zoom (see issue #15) or panning. I haven't tried it, but you should be able to use the plot space delegate to adjust the center and/or radius of the plot.

draw a dotted line on CorePlot graph in 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"])

Weird border on Core Plot Pie Chart

Why is Core Plot drawing these two lines?
This is how I'm setting up the graph:
// setup the pie chart
graph = [[CPTXYGraph alloc] initWithFrame:[_pieView bounds]];
[_pieView setHostedGraph:graph];
CPTPieChart *pieChart = [[CPTPieChart alloc] init];
[pieChart setDataSource:self];
[pieChart setPieRadius:75.0];
[pieChart setIdentifier:#"PieChart1"];
[pieChart setStartAngle:M_PI_4];
[pieChart setSliceDirection:CPTPieDirectionClockwise];
[graph addPlot:pieChart];
(_pieView is the CPTGraphHostingView*)
How do I remove the two black lines? I tried doing what's described in How do remove the border around a core-plot graph, to no avail.
That's not a border—it's the default axes that come with a CPTXYGraph. It's one line of code to remove them:
graph.axisSet = nil;

Resources