Positioning label on CPTBarPlot (Core-Plot) - ios

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.

Related

How to make Legend for Core Plot bubble chart

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.

Getting bar chart height in Core-Plot

I am trying to make stack bar graph with use Core-Plot.
In last station,labels are not centered. I thought, if i can find height of each bar, i can place labels to the center of bars. How can i find height of bars? Or is there any easy way place the labels center of bars?
Thanks for your answer and advice.Sorry for my bad english :)
You can use plot space annotations instead of data labels. They will move with the plots if you pan and zoom. The center of a bar is easy to calculate since the annotation is positioned in data coordinates. Many of the example apps add annotations to scatter plots in response to user action. The same annotation code will work with any plot type.
For example, the center of the first green bar is at (5, 3.5).
NSArray *anchorPoint = #[#5, #3.5];
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:#"Label Text"
style:[CPTTextStyle textStyle]];
textAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace
anchorPlotPoint:anchorPoint];
textAnnotation.contentLayer = textLayer;
[plot addAnnotation:textAnnotation];

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.

Core Plot: Setting different colours for each bar in bar graph

I am trying to plot bar graph in positive and negative direction,Just wanted to know if I can give one colour to bar plot in positve direction and another colour to bar plot in negative direction?Currently all my bars are in one colour.How do i change all bars in positive to red colour and all bars in negative to blue colour?
Implement the -barFillForBarPlot:recordIndex: datasource method. For each index, check the data value associated with the given index and return the desired fill.
you just should do something like this:
-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)idx {
return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:147./255. green:200./255. blue:1. alpha:0.5]];
}

Resources