Skip to content

Commit

Permalink
Fix gradient location calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
larryonoff committed Apr 26, 2018
1 parent 99441eb commit 98d4148
Showing 1 changed file with 31 additions and 40 deletions.
71 changes: 31 additions & 40 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -817,42 +817,35 @@ open class LineChartRenderer: LineRadarRenderer
return
}

context.saveGState()
defer { context.restoreGState() }

let gradientPath = spline.copy(strokingWithWidth: dataSet.lineWidth, lineCap: .butt, lineJoin: .miter, miterLimit: 10)
context.addPath(gradientPath)
context.drawPath(using: .fill)
let boundingBox = spline.boundingBox
guard !boundingBox.isNull && !boundingBox.isInfinite && !boundingBox.isNull else {
return
}

let boundingBox = gradientPath.boundingBox
let gradientStart = CGPoint(x: 0, y: boundingBox.maxY)
let gradientEnd = CGPoint(x: 0, y: boundingBox.minY)
var gradientColorComponents = [CGFloat]()
var gradientLocations = [CGFloat]()
let gradientStart = CGPoint(x: 0, y: boundingBox.minY)
let gradientEnd = CGPoint(x: 0, y: boundingBox.maxY)
var gradientColorComponents: [CGFloat] = []
var gradientLocations: [CGFloat] = []

gradientLocations.append(0)
for position in gradientPositions
for position in gradientPositions.reversed()
{
let positionLocation = CGPoint(x: 0, y: position)
let location = CGPoint(x: boundingBox.minX, y: position)
.applying(matrix)
let normPositionLocation = (positionLocation.y - gradientStart.y) / (gradientEnd.y - gradientStart.y)
if normPositionLocation < 0 {
let normalizedLocation =
(location.y - boundingBox.minY) / (boundingBox.maxY - boundingBox.minY)
switch normalizedLocation {
case ..<0:
gradientLocations.append(0)
} else if normPositionLocation > 1 {
case 0..<1:
gradientLocations.append(normalizedLocation)
case 1...:
gradientLocations.append(1)
} else {
gradientLocations.append(normPositionLocation)
default:
assertionFailure()
}
}
gradientLocations.append(1)

// Lower bound color
// + Middle colors
// + Upper bound color
let colors =
[dataSet.color(atIndex: 0)] + dataSet.colors + [dataSet.color(atIndex: dataSet.colors.count - 1)]

for color in colors
for color in dataSet.colors.reversed()
{
guard let (r, g, b, a) = color.nsuiRGBA else {
continue
Expand All @@ -861,22 +854,20 @@ open class LineChartRenderer: LineRadarRenderer
}

let baseColorSpace = CGColorSpaceCreateDeviceRGB()
let baseColorSpaceComponentsCount = baseColorSpace.numberOfComponents + 1 // // Add 1 for the alpha channel

let gradient: CGGradient?
if gradientPositions.count > 1
{
gradient = CGGradient(colorSpace: baseColorSpace, colorComponents: &gradientColorComponents, locations: &gradientLocations, count: gradientColorComponents.count / baseColorSpaceComponentsCount)
} else {
gradient = CGGradient(colorSpace: baseColorSpace, colorComponents: gradientColorComponents, locations: nil, count: gradientColorComponents.count / baseColorSpaceComponentsCount)
guard let gradient = CGGradient(
colorSpace: baseColorSpace,
colorComponents: &gradientColorComponents,
locations: &gradientLocations,
count: gradientLocations.count) else {
return
}

guard gradient != nil else { return }
context.saveGState()
defer { context.restoreGState() }

// Draw gradient path
context.beginPath()
context.addPath(gradientPath)
context.addPath(spline)
context.replacePathWithStrokedPath()
context.clip()
context.drawLinearGradient(gradient!, start: gradientStart, end: gradientEnd, options: [])
context.drawLinearGradient(gradient, start: gradientStart, end: gradientEnd, options: [])
}
}

0 comments on commit 98d4148

Please sign in to comment.