|
| 1 | +import Foundation |
| 2 | +import Charts |
| 3 | + |
| 4 | +/// This class is a custom view which is displayed over a chart element (e.g. a Bar) when it is highlighted. It |
| 5 | +/// basically is a duplicate of the `BalloonMarker` class found in the Charts demo project. |
| 6 | +/// |
| 7 | +/// See: https://github.com/danielgindi/Charts/blob/master/ChartsDemo-iOS/Swift/Components/BalloonMarker.swift |
| 8 | +/// |
| 9 | +class ChartMarker: MarkerImage { |
| 10 | + @objc open var color: UIColor |
| 11 | + @objc open var arrowSize = CGSize(width: 15, height: 11) |
| 12 | + @objc open var font: UIFont |
| 13 | + @objc open var textColor: UIColor |
| 14 | + @objc open var insets: UIEdgeInsets |
| 15 | + @objc open var minimumSize = CGSize() |
| 16 | + |
| 17 | + private var label: String? |
| 18 | + private var _labelSize: CGSize = CGSize() |
| 19 | + private var _paragraphStyle: NSMutableParagraphStyle? |
| 20 | + private var _drawAttributes = [NSAttributedStringKey: AnyObject]() |
| 21 | + |
| 22 | + @objc public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) { |
| 23 | + self.color = color |
| 24 | + self.font = font |
| 25 | + self.textColor = textColor |
| 26 | + self.insets = insets |
| 27 | + |
| 28 | + _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle |
| 29 | + _paragraphStyle?.alignment = .center |
| 30 | + super.init() |
| 31 | + } |
| 32 | + |
| 33 | + open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint { |
| 34 | + var offset = self.offset |
| 35 | + var size = self.size |
| 36 | + |
| 37 | + if size.width == 0.0 && image != nil { |
| 38 | + size.width = image!.size.width |
| 39 | + } |
| 40 | + |
| 41 | + if size.height == 0.0 && image != nil { |
| 42 | + size.height = image!.size.height |
| 43 | + } |
| 44 | + |
| 45 | + let width = size.width |
| 46 | + let height = size.height |
| 47 | + let padding: CGFloat = 8.0 |
| 48 | + |
| 49 | + var origin = point |
| 50 | + origin.x -= width / 2 |
| 51 | + origin.y -= height |
| 52 | + |
| 53 | + if origin.x + offset.x < 0.0 { |
| 54 | + offset.x = -origin.x + padding |
| 55 | + } else if let chart = chartView, origin.x + width + offset.x > chart.bounds.size.width { |
| 56 | + offset.x = chart.bounds.size.width - origin.x - width - padding |
| 57 | + } |
| 58 | + |
| 59 | + if origin.y + offset.y < 0 { |
| 60 | + offset.y = height + padding |
| 61 | + } else if let chart = chartView, origin.y + height + offset.y > chart.bounds.size.height { |
| 62 | + offset.y = chart.bounds.size.height - origin.y - height - padding |
| 63 | + } |
| 64 | + |
| 65 | + return offset |
| 66 | + } |
| 67 | + |
| 68 | + open override func draw(context: CGContext, point: CGPoint) { |
| 69 | + guard let label = label else { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + let offset = self.offsetForDrawing(atPoint: point) |
| 74 | + let size = self.size |
| 75 | + |
| 76 | + var rect = CGRect( |
| 77 | + origin: CGPoint( |
| 78 | + x: point.x + offset.x, |
| 79 | + y: point.y + offset.y), |
| 80 | + size: size) |
| 81 | + rect.origin.x -= size.width / 2.0 |
| 82 | + rect.origin.y -= size.height |
| 83 | + |
| 84 | + context.saveGState() |
| 85 | + context.setFillColor(color.cgColor) |
| 86 | + |
| 87 | + if offset.y > 0 { |
| 88 | + context.beginPath() |
| 89 | + context.move(to: CGPoint( |
| 90 | + x: rect.origin.x, |
| 91 | + y: rect.origin.y + arrowSize.height)) |
| 92 | + context.addLine(to: CGPoint( |
| 93 | + x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0, |
| 94 | + y: rect.origin.y + arrowSize.height)) |
| 95 | + //arrow vertex |
| 96 | + context.addLine(to: CGPoint( |
| 97 | + x: point.x, |
| 98 | + y: point.y)) |
| 99 | + context.addLine(to: CGPoint( |
| 100 | + x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0, |
| 101 | + y: rect.origin.y + arrowSize.height)) |
| 102 | + context.addLine(to: CGPoint( |
| 103 | + x: rect.origin.x + rect.size.width, |
| 104 | + y: rect.origin.y + arrowSize.height)) |
| 105 | + context.addLine(to: CGPoint( |
| 106 | + x: rect.origin.x + rect.size.width, |
| 107 | + y: rect.origin.y + rect.size.height)) |
| 108 | + context.addLine(to: CGPoint( |
| 109 | + x: rect.origin.x, |
| 110 | + y: rect.origin.y + rect.size.height)) |
| 111 | + context.addLine(to: CGPoint( |
| 112 | + x: rect.origin.x, |
| 113 | + y: rect.origin.y + arrowSize.height)) |
| 114 | + context.fillPath() |
| 115 | + } else { |
| 116 | + context.beginPath() |
| 117 | + context.move(to: CGPoint( |
| 118 | + x: rect.origin.x, |
| 119 | + y: rect.origin.y)) |
| 120 | + context.addLine(to: CGPoint( |
| 121 | + x: rect.origin.x + rect.size.width, |
| 122 | + y: rect.origin.y)) |
| 123 | + context.addLine(to: CGPoint( |
| 124 | + x: rect.origin.x + rect.size.width, |
| 125 | + y: rect.origin.y + rect.size.height - arrowSize.height)) |
| 126 | + context.addLine(to: CGPoint( |
| 127 | + x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0, |
| 128 | + y: rect.origin.y + rect.size.height - arrowSize.height)) |
| 129 | + //arrow vertex |
| 130 | + context.addLine(to: CGPoint( |
| 131 | + x: point.x, |
| 132 | + y: point.y)) |
| 133 | + context.addLine(to: CGPoint( |
| 134 | + x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0, |
| 135 | + y: rect.origin.y + rect.size.height - arrowSize.height)) |
| 136 | + context.addLine(to: CGPoint( |
| 137 | + x: rect.origin.x, |
| 138 | + y: rect.origin.y + rect.size.height - arrowSize.height)) |
| 139 | + context.addLine(to: CGPoint( |
| 140 | + x: rect.origin.x, |
| 141 | + y: rect.origin.y)) |
| 142 | + context.fillPath() |
| 143 | + } |
| 144 | + |
| 145 | + if offset.y > 0 { |
| 146 | + rect.origin.y += self.insets.top + arrowSize.height |
| 147 | + } else { |
| 148 | + rect.origin.y += self.insets.top |
| 149 | + } |
| 150 | + rect.size.height -= self.insets.top + self.insets.bottom |
| 151 | + UIGraphicsPushContext(context) |
| 152 | + label.draw(in: rect, withAttributes: _drawAttributes) |
| 153 | + UIGraphicsPopContext() |
| 154 | + context.restoreGState() |
| 155 | + } |
| 156 | + |
| 157 | + open override func refreshContent(entry: ChartDataEntry, highlight: Highlight) { |
| 158 | + let hintString = entry.accessibilityValue ?? String(entry.y) |
| 159 | + setLabel(hintString) |
| 160 | + } |
| 161 | + |
| 162 | + @objc open func setLabel(_ newLabel: String) { |
| 163 | + label = newLabel |
| 164 | + |
| 165 | + _drawAttributes.removeAll() |
| 166 | + _drawAttributes[.font] = self.font |
| 167 | + _drawAttributes[.paragraphStyle] = _paragraphStyle |
| 168 | + _drawAttributes[.foregroundColor] = self.textColor |
| 169 | + _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero |
| 170 | + |
| 171 | + var size = CGSize() |
| 172 | + size.width = _labelSize.width + self.insets.left + self.insets.right |
| 173 | + size.height = _labelSize.height + self.insets.top + self.insets.bottom |
| 174 | + size.width = max(minimumSize.width, size.width) |
| 175 | + size.height = max(minimumSize.height, size.height) |
| 176 | + self.size = size |
| 177 | + } |
| 178 | +} |
0 commit comments