chartView.renderer = GradientMaskLineRenderer(
dataProvider: chartView,
animator: chartView.chartAnimator,
viewPortHandler: chartView.viewPortHandler
)
Last active
October 4, 2019 08:22
-
-
Save devxoul/395ba414c3aef1f0100889ff209c11d4 to your computer and use it in GitHub Desktop.
A custom gradient mask line renderer for https://github.com/danielgindi/Charts
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
class GradientMaskLineRenderer: LineChartRenderer { | |
override init(dataProvider: LineChartDataProvider, animator: Animator, viewPortHandler: ViewPortHandler) { | |
super.init(dataProvider: dataProvider, animator: animator, viewPortHandler: viewPortHandler) | |
} | |
override func drawFilledPath(context: CGContext, path: CGPath, fill: Fill, fillAlpha: CGFloat) { | |
let rect = viewPortHandler.contentRect | |
let colorSpace = CGColorSpaceCreateDeviceGray() | |
let bitmapContext = CGContext( | |
data: nil, | |
width: Int(rect.size.width), | |
height: Int(rect.size.height), | |
bitsPerComponent: 8, | |
bytesPerRow: Int(rect.size.width), | |
space: colorSpace, | |
bitmapInfo: CGImageAlphaInfo.none.rawValue | |
)! | |
let colors = [UIColor.white.cgColor, UIColor.black.cgColor] as CFArray | |
let gradient = CGGradient(colorsSpace: colorSpace, colors: colors, locations: nil)! | |
bitmapContext.drawLinearGradient( | |
gradient, | |
start: CGPoint(x: 0, y: 0), | |
end: CGPoint(x: 0, y: rect.size.height), | |
options: [.drawsAfterEndLocation, .drawsBeforeStartLocation] | |
) | |
let mask = bitmapContext.makeImage()! | |
context.clip(to: rect, mask: mask) | |
super.drawFilledPath(context: context, path: path, fill: fill, fillAlpha: fillAlpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment