iOS: How to make a CPTTradingRangePlot .stickLength be dynamic? - core-plot

How to set the CPTTradingRangePlot stickLength dynamically in
core plot iOS?
CPTTradingRangePlot *tradePlot = [(CPTTradingRangePlot *)[CPTTradingRangePlot alloc] initWithFrame :....];
tradePlot.barWidth = 5.0;
tradePlot.stickLength = 10.0f; //can i vary this dynamically
Graph ScreenShot Link
The length of the symbol or (CPTTradingRangePlot) sticklength varies not uniform.

You can set the stickLength or any other Core Plot property at any time, as long as you call the setter from the main thread. The stickLength and many other properties are animatable using Core Animation or CPTAnimation.

Related

Adding shadow to floating UICollectionView headers

I have an UICollectionView with 2 sections, each one has its own floating header.
My question is if is there any way to add shadow below the header that will be displayed on the collection's cells?
Make a pointer to your collection view's header. (Info)
Then apply following CALayer? effect:
Objective-c
yourObject.clipsToBounds = NO;//necessary, dont change
yourObject.layer.masksToBounds = NO;//necessary, dont change
yourObject.layer.shadowColor = [[UIColor blackColor] CGColor];//color
yourObject.layer.shadowOpacity = 0.5f;//translucency (alpha)
yourObject.layer.shadowRadius = 1.5f;//size | spread
yourObject.layer.shadowOffset = CGSizeMake(0.0f, 2.5f);//direction (x,y)
Convert to Swift as needed.

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 to a Candlestick chart

I have a candle stick plot for a financial time series. Lets say there are 100 candles on the chart, based on certain criteria I short-list 20 points to annotate (i.e. show an image near the corresponding candle). All 20 points should carry the annotation simultaneously. A single annotation can be added, like so :
//the image to display
UIImage *flag = [UIImage imageNamed:#"flag.png"];
CPTImage *flagImage = [CPTImage imageWithCGImage:flag.CGImage
scale:flag.scale];
CPTBorderedLayer *borderedLayer = [[CPTBorderedLayer alloc] init];
CPTMutableLineStyle *blackLineStyle = [CPTMutableLineStyle lineStyle];
[blackLineStyle setLineColor:[CPTColor blackColor]];
[blackLineStyle setLineWidth:1.0f];
[borderedLayer setBorderLineStyle:blackLineStyle];
[borderedLayer setFill:[CPTFill fillWithImage:flagImage]];
//the 'i' in the next statement is from a 'for' loop iterating over NSArrays: xCoordinates & yCoordinates, containing the corresponding coordinates for short-listed data points
NSArray *anchorPoint = [NSArray arrayWithObjects:[xCoordinates objectAtIndex:i], [yCoordinates objectAtIndex:i], nil];
CPTPlotSpaceAnnotation *alertAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:[[[self hostView] hostedGraph] defaultPlotSpace]
anchorPlotPoint:anchorPoint];
[alertAnnotation setContentLayer:borderedLayer];
[alertAnnotation setDisplacement:CGPointMake(0.0f, 10.0f)];
[[[[self hostView] hostedGraph] plotAreaFrame] addAnnotation:alertAnnotation];
The problem is that only one CPTPlotSpaceAnnotation can be hosted at a time, so each time I loop through and perform addAnnotation: again the previous annotation is lost. Based on my research it figures that a CPTLayer (or subclass) has to be added for each annotation separately. Even after multiple attempts, I am unable to correctly add a layer and a corresponding image annotation to it on the chart. I unsuccessfully added multiple CPTLayers (using calls to addSublayer:) but they were not part of the Plot-space (i.e. they did not zoom or pan with the chart, which is the desired behaviour in my case)
Can someone help with how to achieve this behaviour? If needed, I will be happy to furnish more information about the problem/code.
There is no limit to the number of annotations you can add to a layer. Set the size of the annotation content layer to make sure it is visible. Create it with -initWithFrame: or set the bounds later.

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.

Resources