Getting bar chart height in Core-Plot - ios

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

Related

Can OHLC plots have up/down colour fills like Candle Stick increase/ decrease fills?

I am using Core-Plot to draw some financial time-series plots. The user can choose between candlestick or OHLC plots to draw. To distinguish between 'up'/'down' movements different fill colours can be set for a candlestick plot, like so :
//have included only some part of the relevant initialisation code for brevity
CPTTradingRangePlot *candlestickPlot = [[CPTTradingRangePlot alloc] init];
[candlestickPlot setPlotStyle:CPTTradingRangePlotStyleCandleStick];
[candlestickPlot setIncreaseFill:[CPTFill fillWithColor:[CPTColor greenColor]]];
[candlestickPlot setDecreaseFill:[CPTFill fillWithColor:[CPTColor redColor]]];
CPTTradingRangePlot *ohlcPlot = [[CPTTradingRangePlot alloc] init];
[ohlcPlot setPlotStyle:CPTTradingRangePlotStyleOHLC];
[ohlcPlot setIncreaseFill:[CPTFill fillWithColor:[CPTColor greenColor]]];
[ohlcPlot setDecreaseFill:[CPTFill fillWithColor:[CPTColor redColor]]];
Can the colour of 'up'/'down' movements be changed for the OHLC plot? Currently, as shown in the code above, setting the increase/decrease fill does not have any affect on it.
If needed, I will be happy to share more code or information about the problem.
This isn't supported in Core Plot right now. Please open an enhancement request on the issue tracker.

clip the image to the border of the bar chart bars in Core Plot CPTBarPlot

In my code images used instead of data labels using CPTPlotSpaceAnnotaions to display in center in the each bars of the bar plot. The images placed at the center and working fine, but if the image is enlarged it is stretched outside of the bar. I need it to cut off upto the bar width.
Image is drawn using,
CPTBorderedLayer *borderedLayerObject =[[CPTBorderedLayer alloc] initWithFrame:CGRectMake(0, 0, 52, 52)];
borderedLayerObject.fill = [CPTFill fillWithImage:cptimage];
NSArray *anchorPoint = [NSArray arrayWithObjects:xPosition, yPosition, nil];
CPTPlotSpaceAnnotation *imageAnnotation = [[CPTPlotSpaceAnnotation alloc]initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
imageAnnotation.contentLayer = borderedLayerObject;
[graph.plotAreaFrame addAnnotation:imageAnnotation];
return imageAnnotation;
and this borderedLayerObject is set as the contentLayer of CPTPlotSpaceAnnotation, and CPTPlotSpaceAnnotation object is returned...
the image i draw take the frame size of borderedLayerObject and its placed over the bar in the barplot, so i'm unable to understand how to crop the image i've drawn...
CPTPlotSpaceAnnotation is used becoz i need to display the image at the center of the bar plot, using anchorpoint i've given the x and y position that where the images must be displayed in the bar of barplot. Please help me to get the solution...
It's easy to determine the width of the bars. If barWidthsAreInViewCoordinates is YES, the width is given by the barWidth property. Otherwise, you have to use the plot space to convert data coordinates to pixels. Pick two points that are barWidth units apart along the axis that is perpendicular to the bars (e.g., the x-axis for vertical bars), convert them both to view coordinates with the plot space, and find the width from the converted points.

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.

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