-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Added bar gradient #3225
Added bar gradient #3225
Changes from 8 commits
acce59b
42bbbed
22d0854
e7ce372
1d4cfa9
30a7add
dba3b7c
9af67e7
3277e20
fa21ea9
ba0ae4a
13c8886
4f9b35c
4d9d292
35256ba
a65a3e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,13 @@ import CoreGraphics | |
|
||
open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartDataSetProtocol | ||
{ | ||
@objc | ||
public enum BarGradientOrientation: Int | ||
{ | ||
case vertical | ||
case horizontal | ||
} | ||
|
||
private func initialize() | ||
{ | ||
self.highlightColor = NSUIColor.black | ||
|
@@ -149,6 +156,26 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData | |
/// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque) | ||
open var highlightAlpha = CGFloat(120.0 / 255.0) | ||
|
||
/// array of gradient colors [[color1, color2], [color3, color4]] | ||
open var barGradientColors: [[NSUIColor]]? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a nested array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested array is array of colors for each bar. |
||
|
||
open var barGradientOrientation: BarChartDataSet.BarGradientOrientation = .vertical | ||
|
||
/// - returns: The gradient colors at the given index of the DataSet's gradient color array. | ||
/// This prevents out-of-bounds by performing a modulus on the gradient color index, so colours will repeat themselves. | ||
open func barGradientColor(atIndex index: Int) -> [NSUIColor]? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Swift naming convention would be |
||
{ | ||
guard let gradientColors = barGradientColors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one line |
||
else { return nil } | ||
|
||
var index = index | ||
if index < 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary, it is already common place in Swift and Obj-C that collections require a positive index. |
||
{ | ||
index = 0 | ||
} | ||
return gradientColors[index % gradientColors.count] | ||
} | ||
|
||
// MARK: - NSCopying | ||
|
||
open override func copyWithZone(_ zone: NSZone?) -> AnyObject | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,37 +241,74 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer | |
} | ||
} | ||
|
||
let isSingleColor = dataSet.colors.count == 1 | ||
|
||
if isSingleColor | ||
{ | ||
context.setFillColor(dataSet.color(atIndex: 0).cgColor) | ||
} | ||
|
||
for j in buffer.indices | ||
{ | ||
let barRect = buffer[j] | ||
|
||
guard viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width) else { continue } | ||
guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break } | ||
|
||
if !isSingleColor | ||
{ | ||
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors. | ||
context.setFillColor(dataSet.color(atIndex: j).cgColor) | ||
} | ||
|
||
context.fill(barRect) | ||
drawBar(context: context, dataSet: dataSet, index: j, barRect: barRect) | ||
|
||
if drawBorder | ||
{ | ||
context.saveGState() | ||
context.setStrokeColor(borderColor.cgColor) | ||
context.setLineWidth(borderWidth) | ||
context.stroke(barRect) | ||
context.restoreGState() | ||
} | ||
} | ||
} | ||
|
||
open func drawBar(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int, barRect: CGRect) | ||
{ | ||
context.saveGState() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if let gradientColors = dataSet.barGradientColors, gradientColors.count > 0 | ||
{ | ||
if let gradientColor = dataSet.barGradientColor(atIndex: index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the if statement? We know
|
||
{ | ||
drawGradient(context: context, barRect: barRect, gradientColors: gradientColor, orientation: dataSet.barGradientOrientation) | ||
} | ||
} | ||
else | ||
{ | ||
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors. | ||
let fillColor = dataSet.color(atIndex: index).cgColor | ||
context.setFillColor(fillColor) | ||
context.fill(barRect) | ||
} | ||
|
||
context.restoreGState() | ||
} | ||
|
||
open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array<NSUIColor>, orientation: BarChartDataSet.BarGradientOrientation) | ||
{ | ||
let cgColors = gradientColors.map{ $0.cgColor } as CFArray | ||
let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: cgColors, locations: nil) | ||
|
||
let startPoint: CGPoint | ||
let endPoint: CGPoint | ||
|
||
switch orientation | ||
{ | ||
case .vertical: | ||
startPoint = CGPoint(x: barRect.midX, y: barRect.maxY) | ||
endPoint = CGPoint(x: barRect.midX, y: barRect.minY) | ||
|
||
case .horizontal: | ||
startPoint = CGPoint(x: barRect.minX, y: barRect.midY) | ||
endPoint = CGPoint(x: barRect.maxX, y: barRect.midY) | ||
} | ||
|
||
let path = NSUIBezierPath.init(rect: barRect) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't actually need to define
|
||
|
||
context.addPath(path.cgPath) | ||
context.clip() | ||
context.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: []) | ||
} | ||
|
||
open func prepareBarHighlight( | ||
x: Double, | ||
y1: Double, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,18 @@ open class ChartColorTemplates: NSObject | |
] | ||
} | ||
|
||
@objc open class func gradients () -> [[NSUIColor]] | ||
{ | ||
return [ | ||
[NSUIColor(red: 146/255.0, green: 132/255.0, blue: 240/255.0, alpha: 1.0), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where did these colours come from? If they are from other templates, you should directly call them instead of redefining them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's custom colours especially for the gradients. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but where did the values for the colours come from? We already have a list of themes in |
||
NSUIColor(red: 225/255.0, green: 129/255.0, blue: 222/255.0, alpha: 1.0)], | ||
[NSUIColor(red: 253/255.0, green: 155/255.0, blue: 168/255.0, alpha: 1.0), | ||
NSUIColor(red: 236/255.0, green: 215/255.0, blue: 144/255.0, alpha: 1.0)], | ||
[NSUIColor(red: 98/255.0, green: 199/255.0, blue: 203/255.0, alpha: 1.0), | ||
NSUIColor(red: 147/255.0, green: 210/255.0, blue: 173/255.0, alpha: 1.0)] | ||
] | ||
} | ||
|
||
@objc open class func colorFromString(_ colorString: String) -> NSUIColor | ||
{ | ||
let leftParenCharset: CharacterSet = CharacterSet(charactersIn: "( ") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make sense to nest this in
BarChartDataSet