Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Candle chart - make the shadow same color as an candle color #122 #191

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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