|
| 1 | +import UIKit |
| 2 | +import simd |
| 3 | + |
| 4 | +class SparklineView: UIView { |
| 5 | + private let lineLayer = CAShapeLayer() |
| 6 | + private let maskLayer = CAShapeLayer() |
| 7 | + private let gradientLayer = CAGradientLayer() |
| 8 | + |
| 9 | + private static let defaultChartColor = UIColor.muriel(name: .blue, .shade50) |
| 10 | + var chartColor: UIColor! = SparklineView.defaultChartColor { |
| 11 | + didSet { |
| 12 | + if chartColor == nil { |
| 13 | + chartColor = SparklineView.defaultChartColor |
| 14 | + } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + var data: [CGFloat] = [] |
| 19 | + |
| 20 | + override init(frame: CGRect) { |
| 21 | + super.init(frame: frame) |
| 22 | + let initialData = [102, 109, 526, 253, 163, 227, 101].map({ CGFloat($0) }) |
| 23 | + data = interpolateData(initialData) |
| 24 | + |
| 25 | + initializeChart() |
| 26 | + layoutChart() |
| 27 | + } |
| 28 | + |
| 29 | + required init?(coder: NSCoder) { |
| 30 | + fatalError() |
| 31 | + } |
| 32 | + |
| 33 | + override func layoutSubviews() { |
| 34 | + super.layoutSubviews() |
| 35 | + |
| 36 | + layoutChart() |
| 37 | + } |
| 38 | + |
| 39 | + func initializeChart() {layer.isGeometryFlipped = true |
| 40 | + |
| 41 | + lineLayer.strokeColor = chartColor.cgColor |
| 42 | + lineLayer.lineWidth = Constants.lineWidth |
| 43 | + lineLayer.fillColor = UIColor.clear.cgColor |
| 44 | + |
| 45 | + maskLayer.strokeColor = UIColor.clear.cgColor |
| 46 | + maskLayer.fillColor = UIColor.black.cgColor |
| 47 | + |
| 48 | + gradientLayer.startPoint = Constants.gradientStart |
| 49 | + gradientLayer.endPoint = Constants.gradientEnd |
| 50 | + gradientLayer.colors = [chartColor.cgColor, UIColor(white: 1.0, alpha: 0.0).cgColor] |
| 51 | + gradientLayer.mask = maskLayer |
| 52 | + gradientLayer.opacity = Constants.gradientOpacity |
| 53 | + |
| 54 | + layer.addSublayer(gradientLayer) |
| 55 | + layer.addSublayer(lineLayer) |
| 56 | + } |
| 57 | + |
| 58 | + private func interpolateData(_ inputData: [CGFloat]) -> [CGFloat] { |
| 59 | + guard inputData.count > 0, |
| 60 | + let first = inputData.first else { |
| 61 | + return [] |
| 62 | + } |
| 63 | + |
| 64 | + var interpolatedData = [first] |
| 65 | + |
| 66 | + for (this, next) in zip(inputData, inputData.dropFirst()) { |
| 67 | + let interpolationIncrement = (next - this) / Constants.interpolationCount |
| 68 | + |
| 69 | + for index in stride(from: 1, through: Constants.interpolationCount, by: 1) { |
| 70 | + let normalized = simd_smoothstep(this, next, this + (index * interpolationIncrement)) |
| 71 | + let actual = simd_mix(this, next, normalized) |
| 72 | + |
| 73 | + interpolatedData.append(actual) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return interpolatedData |
| 78 | + } |
| 79 | + |
| 80 | + private func layoutChart() { |
| 81 | + CATransaction.begin() |
| 82 | + CATransaction.setDisableActions(true) |
| 83 | + |
| 84 | + lineLayer.frame = bounds |
| 85 | + maskLayer.frame = bounds |
| 86 | + gradientLayer.frame = bounds |
| 87 | + |
| 88 | + // Calculate points to fit along X axis, using existing interpolated Y values |
| 89 | + let segmentWidth = bounds.width / CGFloat(data.count-1) |
| 90 | + let points = data.enumerated().map({ CGPoint(x: CGFloat($0.offset) * segmentWidth, y: $0.element) }) |
| 91 | + |
| 92 | + // Scale Y values to fit within our bounds |
| 93 | + let maxYValue = points.map(\.y).max() ?? 1.0 |
| 94 | + let scaleFactor = bounds.height / maxYValue |
| 95 | + let scaleTransform = CGAffineTransform(scaleX: 1.0, y: scaleFactor) |
| 96 | + |
| 97 | + // Scale the points slightly so that the line remains within bounds, based on the line width. |
| 98 | + let xScaleFactor = (bounds.width - Constants.lineWidth) / bounds.width |
| 99 | + let yScaleFactor = (bounds.height - Constants.lineWidth) / bounds.height |
| 100 | + |
| 101 | + let halfLineWidth = Constants.lineWidth / 2.0 |
| 102 | + var lineTransform = CGAffineTransform(translationX: halfLineWidth, y: halfLineWidth) |
| 103 | + lineTransform = lineTransform.scaledBy(x: xScaleFactor, y: yScaleFactor) |
| 104 | + lineTransform = lineTransform.concatenating(scaleTransform) |
| 105 | + |
| 106 | + // Finally, create the paths – first the line... |
| 107 | + let lineLayerPath = CGMutablePath() |
| 108 | + lineLayerPath.addLines(between: points, transform: lineTransform) |
| 109 | + |
| 110 | + lineLayer.path = lineLayerPath |
| 111 | + |
| 112 | + // ... then the bottom gradient |
| 113 | + if let maskLayerPath = lineLayerPath.mutableCopy() { |
| 114 | + maskLayerPath.addLine(to: CGPoint(x: bounds.width, y: 0)) |
| 115 | + maskLayerPath.addLine(to: CGPoint(x: 0, y: 0)) |
| 116 | + maskLayer.path = maskLayerPath |
| 117 | + } |
| 118 | + |
| 119 | + CATransaction.commit() |
| 120 | + } |
| 121 | + |
| 122 | + private enum Constants { |
| 123 | + static let lineWidth: CGFloat = 2.0 |
| 124 | + static let gradientOpacity: Float = 0.1 |
| 125 | + |
| 126 | + // This number of extra data points will be interpolated in between each pair of original data points. |
| 127 | + // The higher the number, the smoother the chart line. |
| 128 | + static let interpolationCount: Double = 20 |
| 129 | + |
| 130 | + static let gradientStart = CGPoint(x: 0.0, y: 0.5) |
| 131 | + static let gradientEnd = CGPoint(x: 1.0, y: 0.5) |
| 132 | + } |
| 133 | +} |
0 commit comments