Skip to content

Commit

Permalink
Candle chart - make the shadow same color as an increasing/decreasing…
Browse files Browse the repository at this point in the history
… candle color ChartsOrg#122
  • Loading branch information
Dor Alon committed Jul 8, 2015
1 parent 855c5ff commit ff585ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Charts/Classes/Data/CandleChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class CandleChartDataSet: BarLineScatterCandleChartDataSet
/// the color of the shadow line
public var shadowColor: UIColor?

/// use candle color for the shadow
public var makeShadowSameColorAsCandle = false

/// color for open <= close
public var decreasingColor: UIColor?

Expand Down
24 changes: 21 additions & 3 deletions Charts/Classes/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,20 @@ public class CandleStickChartRenderer: ChartDataRendererBase

// draw the shadow

CGContextSetStrokeColorWithColor(context, (dataSet.shadowColor ?? dataSet.colorAt(j)).CGColor)
var shadowColor = dataSet.shadowColor ?? dataSet.colorAt(j)
if (dataSet.makeShadowSameColorAsCandle)
{
if (e.open > e.close)
{
shadowColor = dataSet.decreasingColor ?? dataSet.colorAt(j)
}
else if (e.open < e.close)
{
shadowColor = dataSet.increasingColor ?? dataSet.colorAt(j)
}
}

CGContextSetStrokeColorWithColor(context, shadowColor.CGColor)

CGContextStrokeLineSegments(context, _shadowPoints, 2)

Expand All @@ -112,9 +125,14 @@ public class CandleStickChartRenderer: ChartDataRendererBase
trans.rectValueToPixel(&_bodyRect)

// draw body differently for increasing and decreasing entry
if (e.open >= e.close)

if (e.open == e.close)
{
CGContextSetStrokeColorWithColor(context, shadowColor.CGColor)
CGContextStrokeRect(context, _bodyRect)
}
else if (e.open > e.close)
{

var color = dataSet.decreasingColor ?? dataSet.colorAt(j)

if (dataSet.isDecreasingFilled)
Expand Down
11 changes: 11 additions & 0 deletions ChartsDemo/Classes/Demos/CandleStickChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ - (void)viewDidLoad
@{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleMakeShadowSameColorAsCandle", @"label": @"Toggle shadow same color"},
];

_chartView.delegate = self;
Expand Down Expand Up @@ -208,6 +209,16 @@ - (void)optionTapped:(NSString *)key
_chartView.autoScaleMinMaxEnabled = !_chartView.isAutoScaleMinMaxEnabled;
[_chartView notifyDataSetChanged];
}

if ([key isEqualToString:@"toggleMakeShadowSameColorAsCandle"])
{
for (CandleChartDataSet *set in _chartView.data.dataSets)
{
set.makeShadowSameColorAsCandle = !set.makeShadowSameColorAsCandle;
}

[_chartView notifyDataSetChanged];
}
}

#pragma mark - Actions
Expand Down

0 comments on commit ff585ba

Please sign in to comment.