-
-
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 gradient line drawing to LineChartRenderer #3142
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,21 +161,27 @@ open class LineChartRenderer: LineRadarRenderer | |
} | ||
|
||
context.saveGState() | ||
|
||
defer { context.restoreGState() } | ||
|
||
if dataSet.isDrawFilledEnabled | ||
{ | ||
// Copy this path because we make changes to it | ||
let fillPath = cubicPath.mutableCopy() | ||
|
||
drawCubicFill(context: context, dataSet: dataSet, spline: fillPath!, matrix: valueToPixelMatrix, bounds: _xBounds) | ||
} | ||
|
||
context.beginPath() | ||
context.addPath(cubicPath) | ||
context.setStrokeColor(drawingColor.cgColor) | ||
context.strokePath() | ||
|
||
context.restoreGState() | ||
|
||
if dataSet.isDrawLineWithGradientEnabled | ||
{ | ||
drawGradientLine(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix) | ||
} | ||
else | ||
{ | ||
context.beginPath() | ||
context.addPath(cubicPath) | ||
context.setStrokeColor(drawingColor.cgColor) | ||
context.strokePath() | ||
} | ||
} | ||
|
||
@objc open func drawHorizontalBezier(context: CGContext, dataSet: LineChartDataSetProtocol) | ||
|
@@ -247,10 +253,10 @@ open class LineChartRenderer: LineRadarRenderer | |
|
||
open func drawCubicFill( | ||
context: CGContext, | ||
dataSet: LineChartDataSetProtocol, | ||
spline: CGMutablePath, | ||
matrix: CGAffineTransform, | ||
bounds: XBounds) | ||
dataSet: LineChartDataSetProtocol, | ||
spline: CGMutablePath, | ||
matrix: CGAffineTransform, | ||
bounds: XBounds) | ||
{ | ||
guard | ||
let dataProvider = dataProvider | ||
|
@@ -307,7 +313,8 @@ open class LineChartRenderer: LineRadarRenderer | |
} | ||
|
||
context.saveGState() | ||
|
||
defer { context.restoreGState() } | ||
|
||
context.setLineCap(dataSet.lineCapType) | ||
|
||
// more than 1 color | ||
|
@@ -416,8 +423,8 @@ open class LineChartRenderer: LineRadarRenderer | |
} | ||
|
||
context.addLine(to: CGPoint( | ||
x: CGFloat(e2.x), | ||
y: CGFloat(e2.y * phaseY) | ||
x: CGFloat(e2.x), | ||
y: CGFloat(e2.y * phaseY) | ||
).applying(valueToPixelMatrix)) | ||
} | ||
|
||
|
@@ -428,8 +435,17 @@ open class LineChartRenderer: LineRadarRenderer | |
} | ||
} | ||
} | ||
|
||
context.restoreGState() | ||
|
||
if (dataSet.isDrawLineWithGradientEnabled) | ||
{ | ||
let path = generateGradientLinePath(dataSet: dataSet, | ||
fillMin: dataSet.fillFormatter?.getFillLinePosition(dataSet: dataSet, dataProvider: dataProvider) ?? 0.0, | ||
from: _xBounds.min, | ||
to: _xBounds.max, | ||
matrix: trans.valueToPixelMatrix) | ||
|
||
drawGradientLine(context: context, dataSet: dataSet, spline: path, matrix: valueToPixelMatrix) | ||
} | ||
} | ||
|
||
open func drawLinearFill(context: CGContext, dataSet: LineChartDataSetProtocol, trans: Transformer, bounds: XBounds) | ||
|
@@ -680,7 +696,7 @@ open class LineChartRenderer: LineRadarRenderer | |
if drawCircleHole | ||
{ | ||
context.setFillColor(dataSet.circleHoleColor!.cgColor) | ||
|
||
// The hole rect | ||
rect.origin.x = pt.x - circleHoleRadius | ||
rect.origin.y = pt.y - circleHoleRadius | ||
|
@@ -719,7 +735,7 @@ open class LineChartRenderer: LineRadarRenderer | |
{ | ||
continue | ||
} | ||
|
||
context.setStrokeColor(set.highlightColor.cgColor) | ||
context.setLineWidth(set.highlightLineWidth) | ||
if set.highlightLineDashLengths != nil | ||
|
@@ -751,4 +767,113 @@ open class LineChartRenderer: LineRadarRenderer | |
|
||
context.restoreGState() | ||
} | ||
|
||
/// Generates the path that is used for gradient drawing. | ||
private func generateGradientLinePath(dataSet: LineChartDataSetProtocol, fillMin: CGFloat, from: Int, to: Int, matrix: CGAffineTransform) -> CGPath | ||
{ | ||
let phaseX = CGFloat(animator.phaseX) | ||
let phaseY = CGFloat(animator.phaseY) | ||
|
||
var e: ChartDataEntry! | ||
|
||
let generatedPath = CGMutablePath() | ||
e = dataSet.entryForIndex(from) | ||
if e != nil | ||
{ | ||
generatedPath.move(to: CGPoint(x: CGFloat(e.x), y: CGFloat(e.y) * phaseY), transform: matrix) | ||
} | ||
|
||
// create a new path | ||
let to = Int(ceil(CGFloat(to - from) * phaseX + CGFloat(from))) | ||
for i in (from + 1)..<to | ||
{ | ||
guard let e = dataSet.entryForIndex(i) else { continue } | ||
generatedPath.addLine(to: CGPoint(x: CGFloat(e.x), y: CGFloat(e.y) * phaseY), transform: matrix) | ||
} | ||
return generatedPath | ||
} | ||
|
||
func drawGradientLine(context: CGContext, dataSet: LineChartDataSetProtocol, spline: CGPath, matrix: CGAffineTransform) | ||
{ | ||
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 = gradientPath.boundingBox | ||
let gradientStart = CGPoint(x: 0, y: boundingBox.maxY) | ||
let gradientEnd = CGPoint(x: 0, y: boundingBox.minY) | ||
var gradientLocations = [CGFloat]() | ||
var gradientColors = [CGFloat]() | ||
var cRed: CGFloat = 0 | ||
var cGreen: CGFloat = 0 | ||
var cBlue: CGFloat = 0 | ||
var cAlpha: CGFloat = 0 | ||
|
||
//Set lower bound color | ||
gradientLocations.append(0) | ||
var cColor = dataSet.color(atIndex: 0) | ||
if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) | ||
{ | ||
gradientColors += [cRed, cGreen, cBlue, cAlpha] | ||
} | ||
|
||
//Set middle colors | ||
guard let gradientPositions = dataSet.gradientPositions else | ||
{ | ||
fatalError("Must set `gradientPositions if `dataSet.isDrawLineWithGradientEnabled` is true") | ||
} | ||
|
||
for position in gradientPositions | ||
{ | ||
let positionLocation = CGPoint(x: 0, y: position) | ||
.applying(matrix) | ||
let normPositionLocation = (positionLocation.y - gradientStart.y) / (gradientEnd.y - gradientStart.y) | ||
if (normPositionLocation < 0) { | ||
gradientLocations.append(0) | ||
} else if (normPositionLocation > 1) { | ||
gradientLocations.append(1) | ||
} else { | ||
gradientLocations.append(normPositionLocation) | ||
} | ||
} | ||
|
||
for _ in dataSet.colors | ||
{ | ||
cColor = dataSet.color(atIndex: 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. @jjatie just want to check that this line is correct. It iterates over 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 cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) | ||
{ | ||
gradientColors += [cRed, cGreen, cBlue, cAlpha] | ||
} | ||
} | ||
|
||
//Set upper bound color | ||
gradientLocations.append(1) | ||
cColor = dataSet.color(atIndex: dataSet.colors.count - 1) | ||
if cColor.getRed(&cRed, green: &cGreen, blue: &cBlue, alpha: &cAlpha) | ||
{ | ||
gradientColors += [cRed, cGreen, cBlue, cAlpha] | ||
} | ||
|
||
//Define gradient | ||
let baseSpace = CGColorSpaceCreateDeviceRGB() | ||
let gradient: CGGradient? | ||
if gradientPositions.count > 1 | ||
{ | ||
gradient = CGGradient(colorSpace: baseSpace, colorComponents: &gradientColors, locations: &gradientLocations, count: gradientColors.count / 4) | ||
} else | ||
{ | ||
gradient = CGGradient(colorSpace: baseSpace, colorComponents: gradientColors, locations: nil, count: gradientColors.count / 4) | ||
} | ||
|
||
guard gradient != nil else { return } | ||
|
||
//Draw gradient path | ||
context.beginPath() | ||
context.addPath(gradientPath) | ||
context.clip() | ||
context.drawLinearGradient(gradient!, start: gradientStart, end: gradientEnd, options: []) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
_xBounds.max
and_xBounds.min
can be zeros when there's only one data entry and thisfor
is crashing the app withFatal error: Can't form Range with upperBound < lowerBound
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 needs
if dataSet.entryCount > 1
somewhere.