How to set CandleStickStyle.stickColor to match candle color?


#1

Hi guys,

I’m having trouble with setting a candle stick’s stickColor to match the color of the candle. Browsing through the documentation the only thing I was able to find was the style’s stickColor, which will set the color to all sticks, obviously.

On a side note, setting the outlineWidth to @0 doesn’t have any effect. In order to remove the outline visually, you’d need to set the outlineColor to clearColor. Thought I’d mention it since it might be a bug.

Any help would be highly appreciated!

Cheers,

Max


#2

Hi Maximilian,

We don’t have as many style properties on our candlestick series’ style object for styling the stick color, as we do for styling the fill color, so there wouldn’t be a way to ensure that the stick color is always the same as the fill color if they vary as they rise of fall.

Fortunately, you can style a series on a per point basis by simply subclassing it and overriding styleForPoint:. As you’ll notice though, SFormCandleStickSeries has a slightly more complex method signiture, providing you with the previous and current point. This means you can work out whether the point is rising or falling and style it appropriately. Here’s a quick example, although I haven’t actually ran it myself to ensure it compiles, but it should give you the gist!

@interface MyCandlestickSeries : SChartCandlestickSeries

@end

@implementation MyCandlestickSeries

- (SChartCandlestickSeriesStyle*)styleForPoint:(idSChartData)point previousPoint:(idSChartData)prevPoint {

     SChartCandlestickSeriesStyle *newStyle = [super styleForPoint:point previousPoint:prevPoint];

     float currentOpen = [[point sChartYValueForKey: SChartCandlestickKeyOpen] floatValue];
     float currentClose = [[point sChartYValueForKey: SChartCandlestickKeyClose] floatValue];
     float priorClose = [[prevPoint sChartYValueForKey: SChartCandlestickKeyClose] floatValue];

     if (currentClose > priorClose && currentClose > currentOpen) {
         newStyle.outlineColor = [UIColor greenColor];
         newStyle.risingColor = [UIColor greenColor];
         newStyle.risingColorGradient = [UIColor greenColor];
     } else {
         newStyle.outlineColor = [UIColor redColor];
         newStyle.risingColor = [UIColor redColor];
         newStyle.risingColorGradient = [UIColor redColor];
     }
     return newStyle;
 }

@end

Thanks,
Jan


#3

Oh - I forgot to mention, I’ll raise that in our bug tracking system and we’ll take a look. Thanks!

Jan


#4

Thanks for your quick and thorough reply Jan, I’ll definitely try that tomorrow morning.

Personally, I feel that setting the setting the stick color according to the candle color should be a setting you can easily configure, if not be the default. Finance charts is our daily business and matching stick and candle colors is basically the market default. Especially with a very dense series (lots of data points in a narrow plot area) where the candles aren’t wider than the stick itself, a stick color that doesn’t match the candle color just doesn’t look right.

Still, nice to hear that this can be easily achieved by subclassing the CandleStickSeries.  :grin:

Max


#5

I’ll forward on your request. It’s always good to get feedback from people using our charts in a specific domain!

Thanks,
Jan


#6

Is subclassing SChartCandlestickSeries still required to match stick color to candle color ?


#7

Hi Mark,

Unfortunately subclassing is still required to achieve this.

I’ll add a note that you’re interested to the existing issue in our tasklist.

Thanks,

Sam