Skip to content

Commit

Permalink
added horizontal chart bar gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
alekzuz committed Feb 17, 2018
1 parent 85c0c8d commit abfaadb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
39 changes: 23 additions & 16 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,22 +296,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
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
{
Expand All @@ -326,6 +311,28 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
context.restoreGState()
}

open func drawBar(context: CGContext, dataSet: IBarChartDataSet, 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<NSUIColor>, orientation: BarChartDataSet.BarGradientOrientation)
{
let cgColors = gradientColors.map{ $0.cgColor } as CFArray
Expand Down
5 changes: 1 addition & 4 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit abfaadb

Please sign in to comment.