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

Supporting borders on bars, Fixes issue #822 #844

Merged
merged 2 commits into from
Apr 10, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ public class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBarChartD

/// the color used for drawing the bar-shadows. The bar shadows is a surface behind the bar that indicates the maximum value
public var barShadowColor = NSUIColor(red: 215.0/255.0, green: 215.0/255.0, blue: 215.0/255.0, alpha: 1.0)


/// the width used for drawing borders around the bars. If borderWidth == 0, no border will be drawn.
public var barBorderWidth : CGFloat = 0.0

/// the color drawing borders around the bars.
public var barBorderColor = NSUIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)

/// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque)
public var highlightAlpha = CGFloat(120.0 / 255.0)

Expand Down
6 changes: 6 additions & 0 deletions Charts/Classes/Data/Interfaces/IBarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public protocol IBarChartDataSet: IBarLineScatterCandleBubbleChartDataSet
/// the color used for drawing the bar-shadows. The bar shadows is a surface behind the bar that indicates the maximum value
var barShadowColor: NSUIColor { get set }

/// the width used for drawing borders around the bars. If borderWidth == 0, no border will be drawn.
var barBorderWidth : CGFloat { get set }

/// the color drawing borders around the bars.
var barBorderColor: NSUIColor { get set }

/// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque)
var highlightAlpha: CGFloat { get set }

Expand Down
16 changes: 15 additions & 1 deletion Charts/Classes/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public class BarChartRenderer: ChartDataRendererBase
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
CGContextSetFillColorWithColor(context, dataSet.colorAt(j).CGColor)
CGContextFillRect(context, barRect)

self.drawDataSetBorder(context: context, dataSet: dataSet, rect: barRect)
}
else
{
Expand Down Expand Up @@ -237,13 +239,25 @@ public class BarChartRenderer: ChartDataRendererBase
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
CGContextSetFillColorWithColor(context, dataSet.colorAt(k).CGColor)
CGContextFillRect(context, barRect)

self.drawDataSetBorder(context: context, dataSet: dataSet, rect: barRect)
}
}
}

CGContextRestoreGState(context)
}


internal func drawDataSetBorder(context context: CGContext, dataSet: IBarChartDataSet, rect:CGRect)
{
if (dataSet.barBorderWidth > 0){
CGContextSetStrokeColorWithColor(context, dataSet.barBorderColor.CGColor)
CGContextSetLineWidth(context, dataSet.barBorderWidth)
CGContextStrokeRect(context, rect)
}

}

/// Prepares a bar for being highlighted.
public func prepareBarHighlight(x x: CGFloat, y1: Double, y2: Double, barspacehalf: CGFloat, trans: ChartTransformer, inout rect: CGRect)
{
Expand Down
4 changes: 4 additions & 0 deletions Charts/Classes/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public class HorizontalBarChartRenderer: BarChartRenderer
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
CGContextSetFillColorWithColor(context, dataSet.colorAt(j).CGColor)
CGContextFillRect(context, barRect)

self.drawDataSetBorder(context: context, dataSet: dataSet, rect: barRect)
}
else
{
Expand Down Expand Up @@ -215,6 +217,8 @@ public class HorizontalBarChartRenderer: BarChartRenderer
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
CGContextSetFillColorWithColor(context, dataSet.colorAt(k).CGColor)
CGContextFillRect(context, barRect)

self.drawDataSetBorder(context: context, dataSet: dataSet, rect: barRect)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ChartsDemo/Classes/DemoBaseViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@protected
NSArray *months;
NSArray *parties;
BOOL shouldRenderBarBorders;
}

@property (nonatomic, strong) IBOutlet UIButton *optionsButton;
Expand Down
6 changes: 6 additions & 0 deletions ChartsDemo/Classes/DemoBaseViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ - (void)handleOption:(NSString *)key forChartView:(ChartViewBase *)chartView
_shouldHideData = !_shouldHideData;
[self updateChartData];
}

if ([key isEqualToString:@"toggleBarBorders"])
{
shouldRenderBarBorders = !shouldRenderBarBorders;
[self updateChartData];
}
}

#pragma mark - Actions
Expand Down
2 changes: 2 additions & 0 deletions ChartsDemo/Classes/Demos/AnotherBarChartViewController.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": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

_chartView.delegate = self;
Expand Down Expand Up @@ -106,6 +107,7 @@ - (void)setDataCount:(int)count range:(double)range
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
set1.colors = ChartColorTemplates.vordiplom;
set1.drawValuesEnabled = NO;
set1.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
Expand Down
4 changes: 3 additions & 1 deletion ChartsDemo/Classes/Demos/BarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (void)viewDidLoad
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

[self setupBarLineChartView:_chartView];
Expand Down Expand Up @@ -128,7 +129,8 @@ - (void)setDataCount:(int)count range:(double)range

BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
set1.barSpace = 0.35;

set1.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];

Expand Down
4 changes: 3 additions & 1 deletion ChartsDemo/Classes/Demos/CombinedChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ - (void)viewDidLoad
@{@"key": @"toggleBarValues", @"label": @"Toggle Bar Values"},
@{@"key": @"saveToGallery", @"label": @"Save to Camera Roll"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

_chartView.delegate = self;
Expand Down Expand Up @@ -172,7 +173,8 @@ - (BarChartData *)generateBarData
[set setColor:[UIColor colorWithRed:60/255.f green:220/255.f blue:78/255.f alpha:1.f]];
set.valueTextColor = [UIColor colorWithRed:60/255.f green:220/255.f blue:78/255.f alpha:1.f];
set.valueFont = [UIFont systemFontOfSize:10.f];

set.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

set.axisDependency = AxisDependencyLeft;

[d addDataSet:set];
Expand Down
4 changes: 3 additions & 1 deletion ChartsDemo/Classes/Demos/HorizontalBarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (void)viewDidLoad
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

[self setupBarLineChartView:_chartView];
Expand Down Expand Up @@ -125,7 +126,8 @@ - (void)setDataCount:(int)count range:(double)range

BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
set1.barSpace = 0.35;

set1.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];

Expand Down
6 changes: 5 additions & 1 deletion ChartsDemo/Classes/Demos/MultipleBarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ - (void)viewDidLoad
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

_chartView.delegate = self;
Expand Down Expand Up @@ -125,11 +126,14 @@ - (void)setDataCount:(int)count range:(double)range

BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals1 label:@"Company A"];
[set1 setColor:[UIColor colorWithRed:104/255.f green:241/255.f blue:175/255.f alpha:1.f]];
[set1 setBarBorderWidth:(shouldRenderBarBorders ? 1.0 : 0.0)];
BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithYVals:yVals2 label:@"Company B"];
[set2 setColor:[UIColor colorWithRed:164/255.f green:228/255.f blue:251/255.f alpha:1.f]];
//[set2 setBarBorderWidth:(shouldRenderBarBorders ? 1.0 : 0.0)];
BarChartDataSet *set3 = [[BarChartDataSet alloc] initWithYVals:yVals3 label:@"Company C"];
[set3 setColor:[UIColor colorWithRed:242/255.f green:247/255.f blue:158/255.f alpha:1.f]];

[set3 setBarBorderWidth:(shouldRenderBarBorders ? 1.0 : 0.0)];

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];
[dataSets addObject:set2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ - (void)viewDidLoad
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

NSNumberFormatter *customFormatter = [[NSNumberFormatter alloc] init];
Expand Down Expand Up @@ -124,7 +125,8 @@ - (void)setChartData
set.stackLabels = @[
@"Men", @"Women"
];

set.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

NSArray *xVals = @[ @"0-10", @"10-20", @"20-30", @"30-40", @"40-50", @"50-60", @"60-70", @"70-80", @"80-90", @"90-100", @"100+" ];

BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSet:set];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ - (void)viewDidLoad
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

[self setupBarLineChartView:_chartView];
Expand Down Expand Up @@ -152,7 +153,8 @@ - (void)setChartData
set.barSpace = 0.4f;
set.colors = colors;
set.valueColors = colors;

set.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

BarChartData *data = [[BarChartData alloc] initWithXVals:dates dataSet:set];
[data setValueFont:[UIFont systemFontOfSize:13.f]];

Expand Down
6 changes: 4 additions & 2 deletions ChartsDemo/Classes/Demos/StackedBarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ - (void)viewDidLoad
@{@"key": @"togglePinchZoom", @"label": @"Toggle PinchZoom"},
@{@"key": @"toggleAutoScaleMinMax", @"label": @"Toggle auto scale min/max"},
@{@"key": @"toggleData", @"label": @"Toggle Data"},
];
@{@"key": @"toggleBarBorders", @"label": @"Show Bar Borders"},
];

_chartView.delegate = self;

Expand Down Expand Up @@ -121,7 +122,8 @@ - (void)setDataCount:(int)count range:(double)range
BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"Statistics Vienna 2014"];
set1.colors = @[ChartColorTemplates.vordiplom[0], ChartColorTemplates.vordiplom[1], ChartColorTemplates.vordiplom[2]];
set1.stackLabels = @[@"Births", @"Divorces", @"Marriages"];

set1.barBorderWidth = shouldRenderBarBorders ? 1.0 : 0.0;

NSMutableArray *dataSets = [[NSMutableArray alloc] init];
[dataSets addObject:set1];

Expand Down
8 changes: 7 additions & 1 deletion ChartsRealm/Classes/Data/RealmBarDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ public class RealmBarDataSet: RealmBarLineScatterCandleBubbleDataSet, IBarChartD

/// the color used for drawing the bar-shadows. The bar shadows is a surface behind the bar that indicates the maximum value
public var barShadowColor = NSUIColor(red: 215.0/255.0, green: 215.0/255.0, blue: 215.0/255.0, alpha: 1.0)


/// the width used for drawing borders around the bars. If borderWidth == 0, no border will be drawn.
public var barBorderWidth : CGFloat = 0.0

/// the color drawing borders around the bars.
public var barBorderColor = NSUIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)

/// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque)
public var highlightAlpha = CGFloat(120.0 / 255.0)

Expand Down