Skip to content

Commit

Permalink
Merge pull request #1 from felcana/bar-gradient
Browse files Browse the repository at this point in the history
Added Bar Gradient
  • Loading branch information
Nermin-Barucija authored Apr 4, 2019
2 parents 5689a1c + a65a3e7 commit fb64ecf
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "facebook/ios-snapshot-test-case" "ed4e6a6e81bfb69a5223156e6c3d389a416cf6e3"
github "facebook/ios-snapshot-test-case" "3efc829601128f7d9306e8e58db04d32594b62ce"
4 changes: 4 additions & 0 deletions ChartsDemo-iOS/Swift/DemoBaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ enum Option {
case toggleYLabels
case toggleRotate
case toggleHighlightCircle
// VericalBarChart
case toggleBarGradient

var label: String {
switch self {
Expand Down Expand Up @@ -89,6 +91,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"
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions ChartsDemo-iOS/Swift/Demos/AnotherBarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class AnotherBarChartViewController: DemoBaseViewController {
.saveToGallery,
.togglePinchZoom,
.toggleData,
.toggleBarBorders]
.toggleBarBorders,
.toggleBarGradient]

chartView.delegate = self

Expand Down Expand Up @@ -87,7 +88,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
Expand Down
17 changes: 15 additions & 2 deletions ChartsDemo-iOS/Swift/Demos/BarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class BarChartViewController: DemoBaseViewController {
.saveToGallery,
.togglePinchZoom,
.toggleData,
.toggleBarBorders]
.toggleBarBorders,
.toggleBarGradient]

self.setup(barLineChartView: chartView)

Expand Down Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions ChartsDemo-iOS/Swift/Demos/MultipleBarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class MultipleBarChartViewController: DemoBaseViewController {
.togglePinchZoom,
.toggleAutoScaleMinMax,
.toggleData,
.toggleBarBorders]
.toggleBarBorders,
.toggleBarGradient]

chartView.delegate = self

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class PositiveNegativeBarChartViewController: DemoBaseViewController {
.togglePinchZoom,
.toggleAutoScaleMinMax,
.toggleData,
.toggleBarBorders]
.toggleBarBorders,
.toggleBarGradient]

self.setup(barLineChartView: chartView)

Expand Down Expand Up @@ -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)
}
}
}

Expand Down
17 changes: 15 additions & 2 deletions ChartsDemo-iOS/Swift/Demos/StackedBarChartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class StackedBarChartViewController: DemoBaseViewController {
.togglePinchZoom,
.toggleAutoScaleMinMax,
.toggleData,
.toggleBarBorders]
.toggleBarBorders,
.toggleBarGradient]


chartView.delegate = self
Expand Down Expand Up @@ -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?) {
Expand Down
20 changes: 20 additions & 0 deletions Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
import Foundation
import CoreGraphics

@objc
public enum BarGradientOrientation: Int
{
case vertical
case horizontal
}

open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartDataSetProtocol
{

private func initialize()
{
self.highlightColor = NSUIColor.black
Expand Down Expand Up @@ -117,6 +124,19 @@ 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]]?

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(at index: Int) -> [NSUIColor]?
{
guard let gradientColors = barGradientColors else { return nil }
return gradientColors[index % gradientColors.count]
}

// MARK: - NSCopying

open override func copyWithZone(_ zone: NSZone?) -> AnyObject
Expand Down
9 changes: 9 additions & 0 deletions Source/Charts/Data/Interfaces/BarChartDataSetProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ 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 colors [[color1, color2], [color3, color4]]
var barGradientColors: [[NSUIColor]]? { 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(at index: Int) -> [NSUIColor]?
}
53 changes: 46 additions & 7 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,11 @@ 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)
}

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)
Expand All @@ -350,6 +345,50 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
}
}

open func drawBar(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int, barRect: CGRect)
{
context.saveGState()
defer { context.restoreGState() }

if let gradientColor = dataSet.barGradientColor(at: 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)
}
}

open func drawGradient(context: CGContext, barRect: CGRect, gradientColors: Array<NSUIColor>, orientation: 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 = CGPath(rect: barRect, transform: nil)

context.addPath(path)
context.clip()
context.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: [])
}

open func prepareBarHighlight(
x: Double,
y1: Double,
Expand Down
2 changes: 2 additions & 0 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ open class HorizontalBarChartRenderer: BarChartRenderer
continue
}

drawBar(context: context, dataSet: dataSet, index: j, barRect: barRect)

if !isSingleColor
{
// Set the color for the currently drawn value. If the index is out of bounds, reuse colors.
Expand Down
8 changes: 8 additions & 0 deletions Source/Charts/Utils/ChartColorTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ open class ChartColorTemplates: NSObject
]
}

@objc open class func gradients () -> [[NSUIColor]]
{
let palette = joyful()
return [[palette[0], palette[1]],
[palette[2], palette[3]],
[palette[4], palette[0]]]
}

@objc open class func colorFromString(_ colorString: String) -> NSUIColor
{
let leftParenCharset: CharacterSet = CharacterSet(charactersIn: "( ")
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Utils/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -661,5 +661,5 @@ public typealias NSUIGestureRecognizerState = UIGestureRecognizer.State
{
return NSUIScreen.main
}

#endif

0 comments on commit fb64ecf

Please sign in to comment.