How to make Legend for Core Plot bubble chart - core-plot

I have made a bubble chart using a coreplot scatterplot by adjusting the size of each symbol in the data source method
-(CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)plot recordIndex:(NSUInteger)idx {
Event *thisEvent;
CGSize size;
CPTPlotSymbol *symbol;
symbol = [CPTPlotSymbol ellipsePlotSymbol];
CPTMutableLineStyle *lineStyle = [symbol.lineStyle mutableCopy];
if (idx < self.events.count) {
thisEvent = [self.events objectAtIndex:idx];
size.width = [thisEvent scaledValue];
size.height = size.width;
}
}
How can I create a legend showing the values for each of several symbol sizes? I've been looking at CPTLegendEntry in the class reference, but haven't been able to work it out yet. If anyone out there can help (Eric?), i'd appreciate it.

You can use a legend delegate to customize drawing the swatches. Implement the - legend:shouldDrawSwatchAtIndex:forPlot:inRect:inContext: delegate method and draw your custom swatch into the given context. Return NO to tell the legend that it doesn't need to draw the default swatch, too.
If you need more control, you can create your own. Subclass CPTBorderedLayer, override -renderAsVectorInContext: to do the drawing, and attach the custom layer to the graph as an annotation.

Related

How to create UIImage from CPTPlotSymbol?

I am new to Core-Plot, and here is my problem.
I created my Graph using ScatterPlot class. And my plot symbol creation data source method is:
- (CPTPlotSymbol*)symbolForScatterPlot:(CPTScatterPlot *)plot recordIndex:(NSUInteger)index
{
CPTPlotSymbol *symbol = [[CPTPlotSymbol alloc]init];
if (index %2 == 0)
{
symbol.symbolType = CPTPlotSymbolTypeDiamond;
symbol.fill = [CPTFill fillWithColor:[CPTColor redColor]];
symbol.size = CGSizeMake(10, 10);
return symbol;
}
else
{
symbol.symbolType = CPTPlotSymbolTypeEllipse;
symbol.fill = [CPTFill fillWithColor:[CPTColor orangeColor]];
symbol.size = CGSizeMake(15, 15);
return symbol;
}
}
And when i select any plot symbol from my scatter plot, i want to convert that selected plot symbol to UIImage, because after converting into image i want to drag the image from the source place to any other place with that plotspace.
I implemented delegate method to get the selected plot symbol from my scatter plot, the code is
- (void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
{
CPTPlotSymbol *selectedSymbol = [plot plotSymbolForRecordIndex:index];
NSLog(#"%#", selectedSymbol);
}
from above code i am getting my selected plot symbol object. but from here i don't know how to covert this plot symbol object to UIImage. if any one guide me it will be great or there is any other better way to solve my problem means kindly help me in that way also. Thanks in advance
You can use the -renderAsVectorInContext:atPoint:scale: method to draw the symbol into a CGContext. Create a context, draw the symbol into it, and use UIGraphicsGetImageFromCurrentImageContext() to create a UIImage.

Core Plot: Add text label to padded area around plotAreaFrame

I am creating a bar graph and then adding empty space above as follows....
CPTGraph *graph = self.hostView.hostedGraph;
graph.plotAreaFrame.paddingTop = smallPlotTopOffset; //some number like 25.0
I have already added paddingTop to the graph itself, and then used titleDisplacement to move the graph.title into this space.
However, now I would like to add a label between the title and the graph, in the space created by the plotAreaFrame.paddingTop. Is this possible using CPTTextLayers? I've been trying to add a layer, but they seem constrained by the plotAreaFrame (i.e. within the padding). I would like to align my label centred below my graph.title hence trying to use the hostView, rather than just adding a UILabel to the superview.
For anyone else interested I did this as follows....
CPTLayerAnnotation *ann = [[CPTLayerAnnotation alloc] initWithAnchorLayer:self.hostView.hostedGraph.plotAreaFrame];
ann.rectAnchor = CPTRectAnchorTop; //to make it the top centre of the plotFrame
ann.displacement = CGPointMake(0, -20.0); //To move it down, below the title
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:#"Your Text" style:nil];
ann.contentLayer = textLayer;
[self.hostView.hostedGraph.plotAreaFrame addAnnotation:ann];
I think this is the best way, although it'd be better if I could anchor it to below the main title, rather than add a manual displacement.
Use a layer annotation and add it to the graph.

Add Multiple Annotations/Labels to CandlestickPlot in CorePlot

I am using Coreplot library to show a CandlestickPlot. The default implementation only has a single CPTPlotSpaceAnnotation attached to each plot in the graph(Highest value in OHLC/CandleStickPlot). I want to show two values for High as well as Low.
I tried adding below code but no luck.
CPTPlotSpaceAnnotation *labelAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:self.plotSpace anchorPlotPoint:[NSArray arrayWithObjects:newX, newY, nil]];
labelAnnotation.annotationHostLayer = label.annotationHostLayer;
labelAnnotation.contentLayer = label.contentLayer;
[self addAnnotation:labelAnnotation];
[label is the default annotation displayed]
An annotation cannot share a content layer with another annotation. You'll need to create a new layer for the annotation content. CPTTextLayer is a common choice, but it can be any CPTLayer.
What is self in this context? The default plot data label annotations are added to the plot; you should add your secondary plot labels to the plot, too.

Core-Plot: How to center custom image plot symbol

I'm using a custom image symbol, as described in Showing images on Scattering Graph as plot symbols in Core plot iOS. Also refer to Positioning label on CPTBarPlot (Core-Plot) regarding positioning of the data label in a CPTPlot.
However, I'm not seeing the behavior from CPTScatterPlot.labelOffset that I need. Positive values increase the distance between the image bottom and the point, while negative values increase the distance between the image top and the point. I need to center the image on the point. See screenshots:
positive labelOffset values:
negative labelOffset values:
My solution is hacky and requires me to modify the image frame in my CustomImageForScatterPlot's drawInContext method. Any ideas on how to make labelOffset work how I want?
That behavior is correct for data labels. You want to use a plot symbol.
-(CPTPlotSymbol *)symbolForScatterPlot:(CPTScatterPlot *)plot
recordIndex:(NSUInteger)index
{
CPTPlotSymbol *symbol = nil;
if ( /* use symbol for this index? */ ) {
symbol = [CPTPlotSymbol ellipsePlotSymbol];
symbol.fill = [CPTFill fillWithImage:/* symbol image */];
symbol.size = CGSizeMake(width, height);
}
return symbol;
}
If you want every point to have a symbol, set the plotSymbol property instead of implementing this datasource method.

Positioning label on CPTBarPlot (Core-Plot)

I am trying to change default position of labels in Bar chart with Core-Plot.
I am using this method:
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx;
And I return:
return textLayer = [[CPTTextLayer alloc] initWithText:#"2222" style:textStyle];
I get this result:
But I want to appear as follows:
Any idea? I tried to find answer on documentation, but I has been impossible.
Use a negative labelOffset for the bar plot. The default is +10 which puts the labels 10 pixels above the bars. This property is inherited from CPTPlot so it works for all plot types, although the default value and behavior varies somewhat.

Resources