|
| 1 | +// |
| 2 | +// AnnotationModifier.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Carson.Katri on 6/20/23. |
| 6 | +// |
| 7 | + |
| 8 | +import Charts |
| 9 | +import SwiftUI |
| 10 | +import LiveViewNative |
| 11 | + |
| 12 | +/// Annotate a chart element with custom Views. |
| 13 | +/// |
| 14 | +/// Place an annotation on a mark to annotate it. |
| 15 | +/// Provide a key name to the ``content`` argument, and create a nested element with the `template` attribute set to the same key. |
| 16 | +/// |
| 17 | +/// ```html |
| 18 | +/// <BarMark |
| 19 | +/// ... |
| 20 | +/// modifiers={@native |> annotation(content: :icon)} |
| 21 | +/// > |
| 22 | +/// <Image template={:icon} system-name="flag.fill" /> |
| 23 | +/// </BarMark> |
| 24 | +/// ``` |
| 25 | +/// |
| 26 | +/// ## Arguments |
| 27 | +/// * ``position`` |
| 28 | +/// * ``alignment`` |
| 29 | +/// * ``spacing`` |
| 30 | +/// * ``content`` |
| 31 | +#if swift(>=5.8) |
| 32 | +@_documentation(visibility: public) |
| 33 | +#endif |
| 34 | +struct AnnotationModifier: ContentModifier { |
| 35 | + typealias Builder = ChartContentBuilder |
| 36 | + |
| 37 | + /// The position of the annotation with respect to the annotated item. Defaults to `automatic`. |
| 38 | + /// |
| 39 | + /// See ``LiveViewNativeCharts/Charts/AnnotationPosition`` for a list of possible values. |
| 40 | + #if swift(>=5.8) |
| 41 | + @_documentation(visibility: public) |
| 42 | + #endif |
| 43 | + private let position: AnnotationPosition |
| 44 | + |
| 45 | + /// The alignment of the annotation with respect to the annotated item. Defaults to `center`. |
| 46 | + /// |
| 47 | + /// See ``LiveViewNative/SwiftUI/Alignment`` for a list of possible values. |
| 48 | + #if swift(>=5.8) |
| 49 | + @_documentation(visibility: public) |
| 50 | + #endif |
| 51 | + private let alignment: Alignment |
| 52 | + |
| 53 | + /// The space between the annotation and the annotated item. |
| 54 | + #if swift(>=5.8) |
| 55 | + @_documentation(visibility: public) |
| 56 | + #endif |
| 57 | + private let spacing: CGFloat? |
| 58 | + |
| 59 | + /// `overflow_resolution`, the method used to resolve annotations that do not fit within the chart. Defaults to `automatic`. |
| 60 | + /// |
| 61 | + /// See ``OverflowResolution`` for more details. |
| 62 | + #if swift(>=5.8) |
| 63 | + @_documentation(visibility: public) |
| 64 | + #endif |
| 65 | + private let overflowResolution: OverflowResolution? |
| 66 | + |
| 67 | + /// The key name of the content to place in the annotation. |
| 68 | + #if swift(>=5.8) |
| 69 | + @_documentation(visibility: public) |
| 70 | + #endif |
| 71 | + private let content: String |
| 72 | + |
| 73 | + func apply<R: RootRegistry>( |
| 74 | + to content: Builder.Content, |
| 75 | + on element: ElementNode, |
| 76 | + in context: Builder.Context<R> |
| 77 | + ) -> Builder.Content { |
| 78 | + if #available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *), |
| 79 | + let overflowResolution = overflowResolution?.value |
| 80 | + { |
| 81 | + #if swift(>=5.9) |
| 82 | + content |
| 83 | + .annotation( |
| 84 | + position: position, |
| 85 | + alignment: alignment, |
| 86 | + spacing: spacing, |
| 87 | + overflowResolution: overflowResolution |
| 88 | + ) { |
| 89 | + Builder.buildChildViews(of: element, forTemplate: self.content, in: context) |
| 90 | + } |
| 91 | + #endif |
| 92 | + } else { |
| 93 | + content |
| 94 | + .annotation( |
| 95 | + position: position, |
| 96 | + alignment: alignment, |
| 97 | + spacing: spacing |
| 98 | + ) { |
| 99 | + Builder.buildChildViews(of: element, forTemplate: self.content, in: context) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// Resolves an annotation that overflows the chart on the `x`/`y` axes. |
| 106 | +/// |
| 107 | +/// Use a tuple where the first element is the `x` strategy is the first element, and the `y` strategy is the second element. |
| 108 | +/// Both elements should contain a ``LiveViewNativeCharts/Charts/AnnotationOverflowResolution/Strategy`` type. |
| 109 | +/// |
| 110 | +/// ```elixir |
| 111 | +/// {:automatic, :fit} |
| 112 | +/// ``` |
| 113 | +#if swift(>=5.8) |
| 114 | +@_documentation(visibility: public) |
| 115 | +#endif |
| 116 | +struct OverflowResolution: Decodable { |
| 117 | + let _value: Any |
| 118 | + |
| 119 | + #if swift(>=5.9) |
| 120 | + @available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) |
| 121 | + var value: AnnotationOverflowResolution { |
| 122 | + _value as! AnnotationOverflowResolution |
| 123 | + } |
| 124 | + #else |
| 125 | + var value: Any { fatalError() } |
| 126 | + #endif |
| 127 | + |
| 128 | + init(from decoder: Decoder) throws { |
| 129 | + var container = try decoder.unkeyedContainer() |
| 130 | + if #available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) { |
| 131 | + let x = try container.decode(AnnotationOverflowResolution.Strategy.self) |
| 132 | + let y = try container.decode(AnnotationOverflowResolution.Strategy.self) |
| 133 | + self._value = AnnotationOverflowResolution(x: x, y: y) |
| 134 | + } else { |
| 135 | + throw DecodingError.dataCorrupted(.init(codingPath: container.codingPath, debugDescription: "AnnotationOverflowResolution is only available on iOS 17, macOS 14, tvOS 17, and watchOS 10 or higher.")) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#if swift(>=5.9) |
| 141 | +/// The strategy used to resolve an overflowing annotation. |
| 142 | +/// |
| 143 | +/// Possible values: |
| 144 | +/// * `automatic` |
| 145 | +/// * `disabled` |
| 146 | +/// * `fit`, `{:fit, boundary}` - See ``LiveViewNativeCharts/Charts/AnnotationOverflowResolution/Boundary`` for a list of possible values. |
| 147 | +/// * `pad_scale` |
| 148 | +@_documentation(visibility: public) |
| 149 | +@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) |
| 150 | +extension AnnotationOverflowResolution.Strategy: Decodable { |
| 151 | + public init(from decoder: Decoder) throws { |
| 152 | + if var container = try? decoder.unkeyedContainer() { |
| 153 | + switch try container.decode(String.self) { |
| 154 | + case "fit": |
| 155 | + self = .fit(to: try container.decode(AnnotationOverflowResolution.Boundary.self)) |
| 156 | + case let `default`: |
| 157 | + throw DecodingError.dataCorrupted(.init(codingPath: container.codingPath, debugDescription: "Unknown strategy '\(`default`)'")) |
| 158 | + } |
| 159 | + } else { |
| 160 | + let container = try decoder.singleValueContainer() |
| 161 | + switch try container.decode(String.self) { |
| 162 | + case "automatic": |
| 163 | + self = .automatic |
| 164 | + case "disabled": |
| 165 | + self = .disabled |
| 166 | + case "fit": |
| 167 | + self = .fit |
| 168 | + case "pad_scale": |
| 169 | + self = .padScale |
| 170 | + case let `default`: |
| 171 | + throw DecodingError.dataCorrupted(.init(codingPath: container.codingPath, debugDescription: "Unknown strategy '\(`default`)'")) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/// The boundary to fit to. |
| 178 | +/// |
| 179 | +/// Possible values: |
| 180 | +/// * `automatic` |
| 181 | +/// * `chart` |
| 182 | +/// * `plot` |
| 183 | +@_documentation(visibility: public) |
| 184 | +@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) |
| 185 | +extension AnnotationOverflowResolution.Boundary: Decodable { |
| 186 | + public init(from decoder: Decoder) throws { |
| 187 | + let container = try decoder.singleValueContainer() |
| 188 | + switch try container.decode(String.self) { |
| 189 | + case "automatic": |
| 190 | + self = .automatic |
| 191 | + case "chart": |
| 192 | + self = .chart |
| 193 | + case "plot": |
| 194 | + self = .plot |
| 195 | + case let `default`: |
| 196 | + throw DecodingError.dataCorrupted(.init(codingPath: container.codingPath, debugDescription: "Unknown boundary '\(`default`)'")) |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | +#endif |
0 commit comments