|
| 1 | +import UIKit |
| 2 | + |
| 3 | +class DonutChartView: UIView { |
| 4 | + |
| 5 | + // MARK: Views |
| 6 | + |
| 7 | + private var segmentLayers = [CAShapeLayer]() |
| 8 | + |
| 9 | + private var titleStackView: UIStackView! |
| 10 | + private var titleLabel: UILabel! |
| 11 | + private var totalCountLabel: UILabel! |
| 12 | + private var chartContainer: UIView! |
| 13 | + private var legendStackView: UIStackView! |
| 14 | + |
| 15 | + // MARK: Configuration |
| 16 | + |
| 17 | + struct Segment { |
| 18 | + let title: String |
| 19 | + let value: Float |
| 20 | + let color: UIColor |
| 21 | + } |
| 22 | + |
| 23 | + var title: String? { |
| 24 | + didSet { |
| 25 | + titleLabel.text = title |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + var totalCount: Float = 0 { |
| 30 | + didSet { |
| 31 | + totalCountLabel.text = String(Int(totalCount)) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + var segments: [Segment] = [] |
| 36 | + |
| 37 | + // MARK: Initialization |
| 38 | + |
| 39 | + override init(frame: CGRect) { |
| 40 | + super.init(frame: frame) |
| 41 | + |
| 42 | + backgroundColor = .basicBackground |
| 43 | + |
| 44 | + configureChartContainer() |
| 45 | + configureTitleViews() |
| 46 | + configureLegend() |
| 47 | + configureConstraints() |
| 48 | + } |
| 49 | + |
| 50 | + required init?(coder: NSCoder) { |
| 51 | + fatalError("init(coder:) has not been implemented") |
| 52 | + } |
| 53 | + |
| 54 | + private func configureChartContainer() { |
| 55 | + chartContainer = UIView() |
| 56 | + chartContainer.translatesAutoresizingMaskIntoConstraints = false |
| 57 | + addSubview(chartContainer) |
| 58 | + } |
| 59 | + |
| 60 | + private func configureTitleViews() { |
| 61 | + titleLabel = UILabel() |
| 62 | + titleLabel.textAlignment = .center |
| 63 | + titleLabel.font = .preferredFont(forTextStyle: .subheadline) |
| 64 | + |
| 65 | + totalCountLabel = UILabel() |
| 66 | + totalCountLabel.textAlignment = .center |
| 67 | + totalCountLabel.font = .preferredFont(forTextStyle: .title1).bold() |
| 68 | + |
| 69 | + titleStackView = UIStackView(arrangedSubviews: [titleLabel, totalCountLabel]) |
| 70 | + |
| 71 | + titleStackView.translatesAutoresizingMaskIntoConstraints = false |
| 72 | + titleStackView.axis = .vertical |
| 73 | + titleStackView.spacing = Constants.titleStackViewSpacing |
| 74 | + |
| 75 | + addSubview(titleStackView) |
| 76 | + } |
| 77 | + |
| 78 | + private func configureLegend() { |
| 79 | + legendStackView = UIStackView() |
| 80 | + legendStackView.translatesAutoresizingMaskIntoConstraints = false |
| 81 | + legendStackView.spacing = Constants.legendStackViewSpacing |
| 82 | + legendStackView.distribution = .equalSpacing |
| 83 | + |
| 84 | + addSubview(legendStackView) |
| 85 | + } |
| 86 | + |
| 87 | + private func configureConstraints() { |
| 88 | + NSLayoutConstraint.activate([ |
| 89 | + chartContainer.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 90 | + chartContainer.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 91 | + chartContainer.topAnchor.constraint(equalTo: topAnchor), |
| 92 | + |
| 93 | + legendStackView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 94 | + legendStackView.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 95 | + legendStackView.topAnchor.constraint(equalTo: chartContainer.bottomAnchor, constant: Constants.chartToLegendSpacing), |
| 96 | + legendStackView.bottomAnchor.constraint(equalTo: bottomAnchor), |
| 97 | + |
| 98 | + titleStackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Constants.innerTextPadding), |
| 99 | + titleStackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -Constants.innerTextPadding), |
| 100 | + titleStackView.centerYAnchor.constraint(equalTo: chartContainer.centerYAnchor), |
| 101 | + titleStackView.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: Constants.innerTextPadding), |
| 102 | + titleStackView.bottomAnchor.constraint(lessThanOrEqualTo: chartContainer.bottomAnchor, constant: -Constants.innerTextPadding) |
| 103 | + ]) |
| 104 | + } |
| 105 | + |
| 106 | + /// Initializes the chart display with the provided data. |
| 107 | + /// |
| 108 | + /// - Parameters: |
| 109 | + /// - title: Displayed in the center of the chart |
| 110 | + /// - totalCount: Displayed in the center of the chart and used to calculate segment sizes |
| 111 | + /// - segments: Used for color, legend titles, and segment size |
| 112 | + func configure(title: String?, totalCount: Float, segments: [Segment]) { |
| 113 | + if segments.reduce(0.0, { $0 + $1.value }) > totalCount { |
| 114 | + // DDLogInfo |
| 115 | + print("DonutChartView: Segment values should total less than 100%.") |
| 116 | + } |
| 117 | + |
| 118 | + self.title = title |
| 119 | + self.totalCount = totalCount |
| 120 | + self.segments = segments |
| 121 | + |
| 122 | + segments.forEach({ legendStackView.addArrangedSubview(LegendView(segment: $0)) }) |
| 123 | + |
| 124 | + layoutChart() |
| 125 | + } |
| 126 | + |
| 127 | + private func layoutChart() { |
| 128 | + CATransaction.begin() |
| 129 | + CATransaction.setDisableActions(true) |
| 130 | + |
| 131 | + // Clear out any existing segments |
| 132 | + segmentLayers.forEach({ $0.removeFromSuperlayer() }) |
| 133 | + segmentLayers = [] |
| 134 | + |
| 135 | + guard totalCount > 0 else { |
| 136 | + // We must have a total count greater than 0, as we use it to calculate percentages |
| 137 | + print("DonutChartView: TotalCount must be greater than 0 for chart initialization.") |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + var currentTotal: Float = 0.0 |
| 142 | + |
| 143 | + for segment in segments { |
| 144 | + let segmentLayer = makeSegmentLayer() |
| 145 | + segmentLayer.strokeColor = segment.color.cgColor |
| 146 | + |
| 147 | + // Calculate the start and end of the new segment |
| 148 | + let segmentStartPercentage = CGFloat(currentTotal / totalCount) |
| 149 | + currentTotal += segment.value |
| 150 | + let segmentEndPercentage = CGFloat(currentTotal / totalCount) |
| 151 | + |
| 152 | + let path = UIBezierPath(arcCenter: chartCenterPoint, |
| 153 | + radius: chartRadius, |
| 154 | + startAngle: radiansFromPercent(segmentStartPercentage) + segmentOffset, |
| 155 | + endAngle: radiansFromPercent(segmentEndPercentage) - segmentOffset, |
| 156 | + clockwise: true) |
| 157 | + segmentLayer.path = path.cgPath |
| 158 | + |
| 159 | + segmentLayers.append(segmentLayer) |
| 160 | + } |
| 161 | + |
| 162 | + segmentLayers.forEach({ chartContainer.layer.addSublayer($0) }) |
| 163 | + |
| 164 | + CATransaction.commit() |
| 165 | + } |
| 166 | + |
| 167 | + override func layoutSubviews() { |
| 168 | + super.layoutSubviews() |
| 169 | + |
| 170 | + if !segmentLayers.isEmpty { |
| 171 | + layoutChart() |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // MARK: Helpers |
| 176 | + |
| 177 | + private func makeSegmentLayer() -> CAShapeLayer { |
| 178 | + let segmentLayer = CAShapeLayer() |
| 179 | + segmentLayer.frame = chartContainer.bounds |
| 180 | + segmentLayer.lineWidth = Constants.lineWidth |
| 181 | + segmentLayer.fillColor = UIColor.clear.cgColor |
| 182 | + segmentLayer.lineCap = .round |
| 183 | + |
| 184 | + return segmentLayer |
| 185 | + } |
| 186 | + |
| 187 | + private var chartCenterPoint: CGPoint { |
| 188 | + return CGPoint(x: chartContainer.bounds.midX, y: chartContainer.bounds.midY) |
| 189 | + } |
| 190 | + |
| 191 | + private var chartRadius: CGFloat { |
| 192 | + let smallestDimension = min(chartContainer.bounds.width, chartContainer.bounds.height) |
| 193 | + return (smallestDimension / 2.0) - (Constants.lineWidth / 2.0) |
| 194 | + } |
| 195 | + |
| 196 | + /// Offset used to adjust the endpoints of each chart segment so that the end caps |
| 197 | + /// don't overlap, as they draw from their center not from the line edge |
| 198 | + private var segmentOffset: CGFloat { |
| 199 | + return asin(Constants.lineWidth * 0.5 / chartRadius) |
| 200 | + } |
| 201 | + |
| 202 | + private func radiansFromPercent(_ percent: CGFloat) -> CGFloat { |
| 203 | + return (percent * 2.0 * CGFloat.pi) - (CGFloat.pi / 2.0) |
| 204 | + } |
| 205 | + |
| 206 | + // MARK: Constants |
| 207 | + |
| 208 | + enum Constants { |
| 209 | + static let lineWidth: CGFloat = 16.0 |
| 210 | + static let innerTextPadding: CGFloat = 24.0 |
| 211 | + static let titleStackViewSpacing: CGFloat = 8.0 |
| 212 | + static let legendStackViewSpacing: CGFloat = 8.0 |
| 213 | + static let chartToLegendSpacing: CGFloat = 32.0 |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// MARK: - Legend View |
| 218 | + |
| 219 | +private class LegendView: UIView { |
| 220 | + let segment: DonutChartView.Segment |
| 221 | + |
| 222 | + init(segment: DonutChartView.Segment) { |
| 223 | + self.segment = segment |
| 224 | + |
| 225 | + super.init(frame: .zero) |
| 226 | + |
| 227 | + configureSubviews() |
| 228 | + } |
| 229 | + |
| 230 | + required init?(coder: NSCoder) { |
| 231 | + fatalError("init(coder:) has not been implemented") |
| 232 | + } |
| 233 | + |
| 234 | + private func configureSubviews() { |
| 235 | + let indicator = UIView() |
| 236 | + indicator.backgroundColor = segment.color |
| 237 | + indicator.layer.cornerRadius = 6.0 |
| 238 | + |
| 239 | + let titleLabel = UILabel() |
| 240 | + titleLabel.font = .preferredFont(forTextStyle: .subheadline) |
| 241 | + titleLabel.text = segment.title |
| 242 | + |
| 243 | + let stackView = UIStackView(arrangedSubviews: [indicator, titleLabel]) |
| 244 | + stackView.translatesAutoresizingMaskIntoConstraints = false |
| 245 | + stackView.spacing = 8.0 |
| 246 | + addSubview(stackView) |
| 247 | + |
| 248 | + NSLayoutConstraint.activate([ |
| 249 | + indicator.widthAnchor.constraint(equalToConstant: 12.0), |
| 250 | + indicator.heightAnchor.constraint(equalToConstant: 12.0), |
| 251 | + |
| 252 | + stackView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 253 | + stackView.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 254 | + stackView.topAnchor.constraint(equalTo: topAnchor), |
| 255 | + stackView.bottomAnchor.constraint(equalTo: bottomAnchor) |
| 256 | + ]) |
| 257 | + } |
| 258 | +} |
0 commit comments