From acce59b677dcf387b42561fe15b6515977b6eb42 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Sun, 28 Jan 2018 17:56:05 +0300 Subject: [PATCH 01/13] Removed bar chart renderer isSingleColor --- Source/Charts/Renderers/BarChartRenderer.swift | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 6603d8a4fd..5ec0bc1e4d 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -241,13 +241,6 @@ 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] @@ -255,12 +248,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer 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) - } - + // 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) if drawBorder From 42bbbed9b4fe1298ceb9ca84b3790df7ff6b6593 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Mon, 29 Jan 2018 11:58:04 +0300 Subject: [PATCH 02/13] Added vertical bar chart gradient --- .../Standard/BarChartDataSet.swift | 18 ++++++++++ .../Interfaces/BarChartDataSetProtocol.swift | 7 ++++ .../Charts/Renderers/BarChartRenderer.swift | 36 ++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index 6f88bf95cb..e45db938d5 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -149,6 +149,24 @@ 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 color pairs [[color1, color2], [color3, color4]] + open var barGradientColors: [[NSUIColor]]? + + /// - 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]? + { + guard let gradientColors = barGradientColors + else { return nil } + + var index = index + if index < 0 + { + index = 0 + } + return gradientColors[index % gradientColors.count] + } + // MARK: - NSCopying open override func copyWithZone(_ zone: NSZone?) -> AnyObject diff --git a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift index 3e64d24f49..5b5ce326bb 100644 --- a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift +++ b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift @@ -39,4 +39,11 @@ public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetP /// array of labels used to describe the different values of the stacked bars var stackLabels: [String] { get set } + + /// array of gradient color pairs [[color1, color2], [color3, color4]] + var barGradientColors: [[NSUIColor]]? { get set } + + /// - 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. + func barGradientColor(atIndex index: Int) -> [NSUIColor]? } diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 5ec0bc1e4d..5ac44a0b21 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -248,20 +248,48 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer guard viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width) else { continue } guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break } - // 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) + context.saveGState() + if let gradientColors = dataSet.barGradientColors, gradientColors.count > 0 + { + if let gradientColor = dataSet.barGradientColor(atIndex: j) + { + drawGradient(context: context, barRect: barRect, gradientColors: gradientColor) + } + } + else + { + // Set the color for the currently drawn value. If the index is out of bounds, reuse colors. + let fillColor = dataSet.color(atIndex: j).cgColor + context.setFillColor(fillColor) + context.fill(barRect) + } + context.restoreGState() if drawBorder { + context.saveGState() context.setStrokeColor(borderColor.cgColor) context.setLineWidth(borderWidth) context.stroke(barRect) + context.restoreGState() } } } + open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array) { + let cgColors = gradientColors.map{ $0.cgColor } as CFArray + let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: cgColors, locations: nil) + + let startPoint = CGPoint(x: barRect.midX, y: barRect.maxY) + let endPoint = CGPoint(x: barRect.midX, y: barRect.minY) + + let path = UIBezierPath.init(rect: barRect) + + context.addPath(path.cgPath) + context.clip() + context.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: []) + } + open func prepareBarHighlight( x: Double, y1: Double, From 22d0854c84e560a08d774df9a9880d09e564da47 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Mon, 29 Jan 2018 15:31:19 +0300 Subject: [PATCH 03/13] Added bar gradient orientations vertical/horizontal --- .../Standard/BarChartDataSet.swift | 9 +++++++++ .../Interfaces/BarChartDataSetProtocol.swift | 2 ++ .../Charts/Renderers/BarChartRenderer.swift | 20 +++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index e45db938d5..998bf9b3f4 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -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 @@ -152,6 +159,8 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData /// array of gradient color pairs [[color1, color2], [color3, color4]] open var barGradientColors: [[NSUIColor]]? + 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]? diff --git a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift index 5b5ce326bb..23e2529f13 100644 --- a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift +++ b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift @@ -43,6 +43,8 @@ public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetP /// array of gradient color pairs [[color1, color2], [color3, color4]] var barGradientColors: [[NSUIColor]]? { get set } + var barGradientOrientation: BarChartDataSet.BarGradientOrientation { get set } + /// - 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. func barGradientColor(atIndex index: Int) -> [NSUIColor]? diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 5ac44a0b21..5c749dffe4 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -253,7 +253,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer { if let gradientColor = dataSet.barGradientColor(atIndex: j) { - drawGradient(context: context, barRect: barRect, gradientColors: gradientColor) + drawGradient(context: context, barRect: barRect, gradientColors: gradientColor, orientation: dataSet.barGradientOrientation) } } else @@ -276,12 +276,24 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer } } - open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array) { + open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array, 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 - let startPoint = CGPoint(x: barRect.midX, y: barRect.maxY) - let endPoint = CGPoint(x: barRect.midX, y: barRect.minY) + 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 = UIBezierPath.init(rect: barRect) From e7ce37210385021123fbcb4e9f5f8b26fa5c160d Mon Sep 17 00:00:00 2001 From: alekzuz Date: Mon, 29 Jan 2018 16:44:06 +0300 Subject: [PATCH 04/13] added demo gradients --- ChartsDemo/Swift/DemoBaseViewController.swift | 4 ++++ .../Demos/AnotherBarChartViewController.swift | 18 ++++++++++++++++-- .../Swift/Demos/BarChartViewController.swift | 17 +++++++++++++++-- .../Demos/MultipleBarChartViewController.swift | 17 +++++++++++++++-- ...ositiveNegativeBarChartViewController.swift | 17 +++++++++++++++-- .../Demos/StackedBarChartViewController.swift | 17 +++++++++++++++-- Source/Charts/Utils/ChartColorTemplates.swift | 12 ++++++++++++ 7 files changed, 92 insertions(+), 10 deletions(-) diff --git a/ChartsDemo/Swift/DemoBaseViewController.swift b/ChartsDemo/Swift/DemoBaseViewController.swift index dcbd28026b..079aeebd17 100644 --- a/ChartsDemo/Swift/DemoBaseViewController.swift +++ b/ChartsDemo/Swift/DemoBaseViewController.swift @@ -44,6 +44,8 @@ enum Option { case toggleYLabels case toggleRotate case toggleHighlightCircle + // VericalBarChart + case toggleBarGradient var label: String { switch self { @@ -81,6 +83,8 @@ enum Option { case .toggleYLabels: return "Toggle Y-Labels" case .toggleRotate: return "Toggle Rotate" case .toggleHighlightCircle: return "Toggle highlight circle" + // VericalBarChart + case .toggleBarGradient: return "Toggle Bar Gradient" } } } diff --git a/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift b/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift index 878c5f011f..e70acf9fa9 100644 --- a/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/AnotherBarChartViewController.swift @@ -29,7 +29,8 @@ class AnotherBarChartViewController: DemoBaseViewController { .saveToGallery, .togglePinchZoom, .toggleData, - .toggleBarBorders] + .toggleBarBorders, + .toggleBarGradient] chartView.delegate = self @@ -85,7 +86,20 @@ class AnotherBarChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { - super.handleOption(option, forChartView: chartView) + switch option { + case .toggleBarGradient: + for set in chartView.data!.dataSets as! [BarChartDataSet] { + if set.barGradientColors == nil { + set.barGradientColors = ChartColorTemplates.gradients() + set.barGradientOrientation = .horizontal + } else { + set.barGradientColors = nil + } + } + chartView.notifyDataSetChanged() + default: + super.handleOption(option, forChartView: chartView) + } } // MARK: - Actions diff --git a/ChartsDemo/Swift/Demos/BarChartViewController.swift b/ChartsDemo/Swift/Demos/BarChartViewController.swift index a9a629120a..1be05f3cc4 100644 --- a/ChartsDemo/Swift/Demos/BarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/BarChartViewController.swift @@ -31,7 +31,8 @@ class BarChartViewController: DemoBaseViewController { .saveToGallery, .togglePinchZoom, .toggleData, - .toggleBarBorders] + .toggleBarBorders, + .toggleBarGradient] self.setup(barLineChartView: chartView) @@ -139,7 +140,19 @@ class BarChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { - super.handleOption(option, forChartView: chartView) + switch option { + case .toggleBarGradient: + for set in chartView.data!.dataSets as! [BarChartDataSet] { + if set.barGradientColors == nil { + set.barGradientColors = ChartColorTemplates.gradients() + } else { + set.barGradientColors = nil + } + } + chartView.notifyDataSetChanged() + default: + super.handleOption(option, forChartView: chartView) + } } // MARK: - Actions diff --git a/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift b/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift index 0390ac97bf..a3d2f0630a 100644 --- a/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/MultipleBarChartViewController.swift @@ -32,7 +32,8 @@ class MultipleBarChartViewController: DemoBaseViewController { .togglePinchZoom, .toggleAutoScaleMinMax, .toggleData, - .toggleBarBorders] + .toggleBarBorders, + .toggleBarGradient] chartView.delegate = self @@ -138,7 +139,19 @@ class MultipleBarChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { - super.handleOption(option, forChartView: chartView) + switch option { + case .toggleBarGradient: + for set in chartView.data!.dataSets as! [BarChartDataSet] { + if set.barGradientColors == nil { + set.barGradientColors = ChartColorTemplates.gradients() + } else { + set.barGradientColors = nil + } + } + chartView.notifyDataSetChanged() + default: + super.handleOption(option, forChartView: chartView) + } } // MARK: - Actions diff --git a/ChartsDemo/Swift/Demos/PositiveNegativeBarChartViewController.swift b/ChartsDemo/Swift/Demos/PositiveNegativeBarChartViewController.swift index 78876a3317..1cd25c3efe 100644 --- a/ChartsDemo/Swift/Demos/PositiveNegativeBarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/PositiveNegativeBarChartViewController.swift @@ -33,7 +33,8 @@ class PositiveNegativeBarChartViewController: DemoBaseViewController { .togglePinchZoom, .toggleAutoScaleMinMax, .toggleData, - .toggleBarBorders] + .toggleBarBorders, + .toggleBarGradient] self.setup(barLineChartView: chartView) @@ -109,7 +110,19 @@ class PositiveNegativeBarChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { - super.handleOption(option, forChartView: chartView) + switch option { + case .toggleBarGradient: + for set in chartView.data!.dataSets as! [BarChartDataSet] { + if set.barGradientColors == nil { + set.barGradientColors = ChartColorTemplates.gradients() + } else { + set.barGradientColors = nil + } + } + chartView.notifyDataSetChanged() + default: + super.handleOption(option, forChartView: chartView) + } } } diff --git a/ChartsDemo/Swift/Demos/StackedBarChartViewController.swift b/ChartsDemo/Swift/Demos/StackedBarChartViewController.swift index 3572958a17..92b442ac3f 100644 --- a/ChartsDemo/Swift/Demos/StackedBarChartViewController.swift +++ b/ChartsDemo/Swift/Demos/StackedBarChartViewController.swift @@ -41,7 +41,8 @@ class StackedBarChartViewController: DemoBaseViewController { .togglePinchZoom, .toggleAutoScaleMinMax, .toggleData, - .toggleBarBorders] + .toggleBarBorders, + .toggleBarGradient] chartView.delegate = self @@ -113,7 +114,19 @@ class StackedBarChartViewController: DemoBaseViewController { } override func optionTapped(_ option: Option) { - super.handleOption(option, forChartView: chartView) + switch option { + case .toggleBarGradient: + for set in chartView.data!.dataSets as! [BarChartDataSet] { + if set.barGradientColors == nil { + set.barGradientColors = ChartColorTemplates.gradients() + } else { + set.barGradientColors = nil + } + } + chartView.notifyDataSetChanged() + default: + super.handleOption(option, forChartView: chartView) + } } @IBAction func slidersValueChanged(_ sender: Any?) { diff --git a/Source/Charts/Utils/ChartColorTemplates.swift b/Source/Charts/Utils/ChartColorTemplates.swift index 461ef8ab3d..82e1575532 100644 --- a/Source/Charts/Utils/ChartColorTemplates.swift +++ b/Source/Charts/Utils/ChartColorTemplates.swift @@ -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), + 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: "( ") From 1d4cfa95cc419bfff413a7d4bcb07e8d2ab39404 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Mon, 29 Jan 2018 18:35:22 +0300 Subject: [PATCH 05/13] fix comments --- .../Charts/Data/Implementations/Standard/BarChartDataSet.swift | 2 +- Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index 998bf9b3f4..34e60c20bc 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -156,7 +156,7 @@ 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 color pairs [[color1, color2], [color3, color4]] + /// array of gradient colors [[color1, color2], [color3, color4]] open var barGradientColors: [[NSUIColor]]? open var barGradientOrientation: BarChartDataSet.BarGradientOrientation = .vertical diff --git a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift index 23e2529f13..fe8aae3343 100644 --- a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift +++ b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift @@ -40,7 +40,7 @@ public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetP /// array of labels used to describe the different values of the stacked bars var stackLabels: [String] { get set } - /// array of gradient color pairs [[color1, color2], [color3, color4]] + /// array of gradient colors [[color1, color2], [color3, color4]] var barGradientColors: [[NSUIColor]]? { get set } var barGradientOrientation: BarChartDataSet.BarGradientOrientation { get set } From 30a7addcb44ed0916f9d171c592ec008b05372e5 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Mon, 29 Jan 2018 18:37:17 +0300 Subject: [PATCH 06/13] added NSUIBezierPath for osX --- .../Charts/Renderers/BarChartRenderer.swift | 2 +- Source/Charts/Utils/Platform.swift | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 5c749dffe4..9b6835fe6b 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -295,7 +295,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer endPoint = CGPoint(x: barRect.maxX, y: barRect.midY) } - let path = UIBezierPath.init(rect: barRect) + let path = NSUIBezierPath.init(rect: barRect) context.addPath(path.cgPath) context.clip() diff --git a/Source/Charts/Utils/Platform.swift b/Source/Charts/Utils/Platform.swift index ae17c109a3..0ec4ad5145 100644 --- a/Source/Charts/Utils/Platform.swift +++ b/Source/Charts/Utils/Platform.swift @@ -17,6 +17,7 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple public typealias NSUIGestureRecognizerDelegate = UIGestureRecognizerDelegate public typealias NSUITapGestureRecognizer = UITapGestureRecognizer public typealias NSUIPanGestureRecognizer = UIPanGestureRecognizer + public typealias NSUIBezierPath = UIBezierPath #if !os(tvOS) public typealias NSUIPinchGestureRecognizer = UIPinchGestureRecognizer public typealias NSUIRotationGestureRecognizer = UIRotationGestureRecognizer @@ -229,6 +230,7 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple public typealias NSUIPinchGestureRecognizer = NSMagnificationGestureRecognizer public typealias NSUIRotationGestureRecognizer = NSRotationGestureRecognizer public typealias NSUIScreen = NSScreen + public typealias NSUIBezierPath = NSBezierPath /** On OS X there is no CADisplayLink. Use a 60 fps timer to render the animations. */ public class NSUIDisplayLink @@ -610,4 +612,33 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple return NSUIScreen.main } + extension NSBezierPath + { + public var cgPath: CGPath + { + let path = CGMutablePath() + var points = [CGPoint](repeating: .zero, count: 3) + + for i in 0 ..< self.elementCount + { + let type = self.element(at: i, associatedPoints: &points) + + switch type { + case .moveToBezierPathElement: + path.move(to: points[0]) + + case .lineToBezierPathElement: + path.addLine(to: points[0]) + + case .curveToBezierPathElement: + path.addCurve(to: points[2], control1: points[0], control2: points[1]) + + case .closePathBezierPathElement: + path.closeSubpath() + } + } + return path + } + } + #endif From dba3b7cc17a49273171c0f3337dcb3ad724db46e Mon Sep 17 00:00:00 2001 From: alekzuz Date: Sat, 17 Feb 2018 18:19:15 +0300 Subject: [PATCH 07/13] remove useless isSingleColor from HorizontalBarChartRenderer --- .../Renderers/HorizontalBarChartRenderer.swift | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index f95648206a..2a4cdc4a9c 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -234,13 +234,6 @@ open class HorizontalBarChartRenderer: BarChartRenderer let buffer = _buffers[index] - let isSingleColor = dataSet.colors.count == 1 - - if isSingleColor - { - context.setFillColor(dataSet.color(atIndex: 0).cgColor) - } - for j in stride(from: 0, to: buffer.rects.count, by: 1) { let barRect = buffer.rects[j] @@ -255,11 +248,8 @@ open class HorizontalBarChartRenderer: BarChartRenderer continue } - 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) - } + // 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) From 9af67e76d9a6ca0ba8a90def215b42ae6f194a53 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Sat, 17 Feb 2018 21:35:05 +0300 Subject: [PATCH 08/13] added horizontal chart bar gradient --- .../Charts/Renderers/BarChartRenderer.swift | 39 +++++++++++-------- .../HorizontalBarChartRenderer.swift | 5 +-- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 9b6835fe6b..affdb9a046 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -248,22 +248,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer guard viewPortHandler.isInBoundsLeft(barRect.origin.x + barRect.size.width) else { continue } guard viewPortHandler.isInBoundsRight(barRect.origin.x) else { break } - context.saveGState() - if let gradientColors = dataSet.barGradientColors, gradientColors.count > 0 - { - if let gradientColor = dataSet.barGradientColor(atIndex: j) - { - 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: j).cgColor - context.setFillColor(fillColor) - context.fill(barRect) - } - context.restoreGState() + drawBar(context: context, dataSet: dataSet, index: j, barRect: barRect) if drawBorder { @@ -276,6 +261,28 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer } } + open func drawBar(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int, barRect: CGRect) + { + context.saveGState() + + if let gradientColors = dataSet.barGradientColors, gradientColors.count > 0 + { + if let gradientColor = dataSet.barGradientColor(atIndex: index) + { + 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, orientation: BarChartDataSet.BarGradientOrientation) { let cgColors = gradientColors.map{ $0.cgColor } as CFArray diff --git a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift index 2a4cdc4a9c..22b8c9e400 100644 --- a/Source/Charts/Renderers/HorizontalBarChartRenderer.swift +++ b/Source/Charts/Renderers/HorizontalBarChartRenderer.swift @@ -248,10 +248,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer continue } - // 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 { From fa21ea982d7e91a28d05f233673cd1efdca8c7f4 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Tue, 27 Mar 2018 21:56:37 +0300 Subject: [PATCH 09/13] Used CGpath instead BezierPath --- .../Charts/Renderers/BarChartRenderer.swift | 4 +-- Source/Charts/Utils/Platform.swift | 33 +------------------ 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index affdb9a046..f77f48a30a 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -302,9 +302,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer endPoint = CGPoint(x: barRect.maxX, y: barRect.midY) } - let path = NSUIBezierPath.init(rect: barRect) + let path = CGPath(rect: barRect, transform: nil) - context.addPath(path.cgPath) + context.addPath(path) context.clip() context.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: []) } diff --git a/Source/Charts/Utils/Platform.swift b/Source/Charts/Utils/Platform.swift index 0ec4ad5145..74927daaa6 100644 --- a/Source/Charts/Utils/Platform.swift +++ b/Source/Charts/Utils/Platform.swift @@ -17,7 +17,6 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple public typealias NSUIGestureRecognizerDelegate = UIGestureRecognizerDelegate public typealias NSUITapGestureRecognizer = UITapGestureRecognizer public typealias NSUIPanGestureRecognizer = UIPanGestureRecognizer - public typealias NSUIBezierPath = UIBezierPath #if !os(tvOS) public typealias NSUIPinchGestureRecognizer = UIPinchGestureRecognizer public typealias NSUIRotationGestureRecognizer = UIRotationGestureRecognizer @@ -230,7 +229,6 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple public typealias NSUIPinchGestureRecognizer = NSMagnificationGestureRecognizer public typealias NSUIRotationGestureRecognizer = NSRotationGestureRecognizer public typealias NSUIScreen = NSScreen - public typealias NSUIBezierPath = NSBezierPath /** On OS X there is no CADisplayLink. Use a 60 fps timer to render the animations. */ public class NSUIDisplayLink @@ -611,34 +609,5 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple { return NSUIScreen.main } - - extension NSBezierPath - { - public var cgPath: CGPath - { - let path = CGMutablePath() - var points = [CGPoint](repeating: .zero, count: 3) - - for i in 0 ..< self.elementCount - { - let type = self.element(at: i, associatedPoints: &points) - - switch type { - case .moveToBezierPathElement: - path.move(to: points[0]) - - case .lineToBezierPathElement: - path.addLine(to: points[0]) - - case .curveToBezierPathElement: - path.addCurve(to: points[2], control1: points[0], control2: points[1]) - - case .closePathBezierPathElement: - path.closeSubpath() - } - } - return path - } - } - + #endif From ba0ae4ad475b610c356adc294c0b4f50ac8dfd53 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Tue, 27 Mar 2018 22:55:14 +0300 Subject: [PATCH 10/13] Made fixes according to the review --- .../Standard/BarChartDataSet.swift | 25 +++++++------------ .../Interfaces/BarChartDataSetProtocol.swift | 4 +-- .../Charts/Renderers/BarChartRenderer.swift | 13 +++------- 3 files changed, 15 insertions(+), 27 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift index 34e60c20bc..e285a6b605 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift @@ -12,15 +12,15 @@ import Foundation import CoreGraphics +@objc +public enum BarGradientOrientation: Int +{ + case vertical + case horizontal +} open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartDataSetProtocol { - @objc - public enum BarGradientOrientation: Int - { - case vertical - case horizontal - } private func initialize() { @@ -159,20 +159,13 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData /// array of gradient colors [[color1, color2], [color3, color4]] open var barGradientColors: [[NSUIColor]]? - open var barGradientOrientation: BarChartDataSet.BarGradientOrientation = .vertical + open var barGradientOrientation: 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]? + open func barGradientColor(at index: Int) -> [NSUIColor]? { - guard let gradientColors = barGradientColors - else { return nil } - - var index = index - if index < 0 - { - index = 0 - } + guard let gradientColors = barGradientColors else { return nil } return gradientColors[index % gradientColors.count] } diff --git a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift index fe8aae3343..79786dc3e8 100644 --- a/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift +++ b/Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift @@ -43,9 +43,9 @@ public protocol BarChartDataSetProtocol: BarLineScatterCandleBubbleChartDataSetP /// array of gradient colors [[color1, color2], [color3, color4]] var barGradientColors: [[NSUIColor]]? { get set } - var barGradientOrientation: BarChartDataSet.BarGradientOrientation { get set } + var barGradientOrientation: BarGradientOrientation { get set } /// - 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. - func barGradientColor(atIndex index: Int) -> [NSUIColor]? + func barGradientColor(at index: Int) -> [NSUIColor]? } diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index f77f48a30a..804a1c5da5 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -256,7 +256,6 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer context.setStrokeColor(borderColor.cgColor) context.setLineWidth(borderWidth) context.stroke(barRect) - context.restoreGState() } } } @@ -264,13 +263,11 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer open func drawBar(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int, barRect: CGRect) { context.saveGState() + defer { context.restoreGState() } - if let gradientColors = dataSet.barGradientColors, gradientColors.count > 0 + if let gradientColor = dataSet.barGradientColor(at: index) { - if let gradientColor = dataSet.barGradientColor(atIndex: index) - { - drawGradient(context: context, barRect: barRect, gradientColors: gradientColor, orientation: dataSet.barGradientOrientation) - } + drawGradient(context: context, barRect: barRect, gradientColors: gradientColor, orientation: dataSet.barGradientOrientation) } else { @@ -279,11 +276,9 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer context.setFillColor(fillColor) context.fill(barRect) } - - context.restoreGState() } - open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array, orientation: BarChartDataSet.BarGradientOrientation) + open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array, orientation: BarGradientOrientation) { let cgColors = gradientColors.map{ $0.cgColor } as CFArray let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: cgColors, locations: nil) From 13c88860c2504efe4887684d47793cb84d50cf07 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Thu, 29 Mar 2018 22:13:23 +0300 Subject: [PATCH 11/13] Fixed macOS info.plist issue --- ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj b/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj index 2d45668a87..922588b12b 100644 --- a/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj +++ b/ChartsDemo-macOS/ChartsDemo-macOS.xcodeproj/project.pbxproj @@ -371,7 +371,7 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = ""; - INFOPLIST_FILE = "ChartsDemo-OSX/Info.plist"; + INFOPLIST_FILE = "$(SRCROOT)/ChartsDemo-macOS/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.dcg.ChartsDemo-OSX"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -389,7 +389,7 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = ""; - INFOPLIST_FILE = "ChartsDemo-OSX/Info.plist"; + INFOPLIST_FILE = "$(SRCROOT)/ChartsDemo-macOS/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.dcg.ChartsDemo-OSX"; PRODUCT_NAME = "$(TARGET_NAME)"; From 4f9b35c503f90e92d316ecea87857988585d272d Mon Sep 17 00:00:00 2001 From: alekzuz Date: Thu, 29 Mar 2018 22:25:48 +0300 Subject: [PATCH 12/13] Used default palette for gradients --- Source/Charts/Utils/ChartColorTemplates.swift | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Source/Charts/Utils/ChartColorTemplates.swift b/Source/Charts/Utils/ChartColorTemplates.swift index 82e1575532..fdcb6c50ac 100644 --- a/Source/Charts/Utils/ChartColorTemplates.swift +++ b/Source/Charts/Utils/ChartColorTemplates.swift @@ -86,14 +86,10 @@ 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), - 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)] - ] + let palette = joyful() + return [[palette[0], palette[1]], + [palette[2], palette[3]], + [palette[4], palette[0]]] } @objc open class func colorFromString(_ colorString: String) -> NSUIColor From a65a3e7e0207d32657996562a9cec370b9e7b850 Mon Sep 17 00:00:00 2001 From: alekzuz Date: Sat, 9 Mar 2019 23:52:36 +0300 Subject: [PATCH 13/13] Update dependencies --- Cartfile.resolved | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index 1672a76f44..24432d0bc4 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1 @@ -github "facebook/ios-snapshot-test-case" "ed4e6a6e81bfb69a5223156e6c3d389a416cf6e3" +github "facebook/ios-snapshot-test-case" "3efc829601128f7d9306e8e58db04d32594b62ce"