From 6b897332ccaa4ba55d0259d6726a53f54bb16f1a Mon Sep 17 00:00:00 2001 From: Syed Date: Mon, 3 Jul 2023 19:48:55 +0500 Subject: [PATCH 1/5] Add 'chart_forground_style_scale' modifier --- .../LiveViewNativeCharts/ChartsRegistry.swift | 3 + .../Extensions/Chart.md | 2 + .../ChartForegroundStyleScaleModifier.md | 5 + .../ChartForegroundStyleScale.swift | 136 ++++++++++++++++++ .../chart_foreground_style_scale.ex | 12 ++ 5 files changed, 158 insertions(+) create mode 100644 Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md create mode 100644 Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift create mode 100644 lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex diff --git a/Sources/LiveViewNativeCharts/ChartsRegistry.swift b/Sources/LiveViewNativeCharts/ChartsRegistry.swift index 5585bde..2dfb80a 100644 --- a/Sources/LiveViewNativeCharts/ChartsRegistry.swift +++ b/Sources/LiveViewNativeCharts/ChartsRegistry.swift @@ -27,6 +27,7 @@ public struct ChartsRegistry: CustomRegistry { public enum ModifierType: String { case chartXAxis = "chart_x_axis" case chartYAxis = "chart_y_axis" + case chartForegroundStyleScale = "chart_foreground_style_scale" } public static func decodeModifier(_ type: ModifierType, from decoder: Decoder) throws -> some ViewModifier { @@ -35,6 +36,8 @@ public struct ChartsRegistry: CustomRegistry { try ChartXAxisModifier(from: decoder) case .chartYAxis: try ChartYAxisModifier(from: decoder) + case .chartForegroundStyleScale: + try ChartForegroundStyleScaleModifier(from: decoder) } } } diff --git a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md index 973c340..c395386 100644 --- a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md +++ b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md @@ -9,3 +9,5 @@ ### Axis Modifiers - ``ChartXAxisModifier`` - ``ChartYAxisModifier`` +### Foreground Style Scale Modifiers +- ``ChartForegroundStyleScaleModifier`` diff --git a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md new file mode 100644 index 0000000..7c8d207 --- /dev/null +++ b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md @@ -0,0 +1,5 @@ +# ``LiveViewNativeCharts/ChartForegroundStyleScaleModifier`` + +@Metadata { + @DocumentationExtension(mergeBehavior: append) + @DisplayName("chart_foreground_style_scale", style: symbol) diff --git a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift new file mode 100644 index 0000000..c26a691 --- /dev/null +++ b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift @@ -0,0 +1,136 @@ +// +// ChartForegroundStyleScale.swift +// +// +// Created by murtza on 28/06/2023. +// + +import Charts +import SwiftUI +import LiveViewNative + +/// The ``mapping`` Maps data categories to foreground styles. +/// +/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values. +/// +/// ```html +/// :circle, +/// "B" => :asterisk, +/// "C" => :plus +/// })}> +/// ... +/// +/// ``` +/// +/// The possible data values plotted as foreground style in the chart. You can define the ``domain`` with an array for categorical values (e.g., ["A", "B", "C"]) +/// +/// See ``LiveViewNativeCharts/Charts/AnyScaleDomain`` for more details. +/// +/// ```html +/// +/// ... +/// +/// ``` +/// +/// The ``range`` of foreground styles that correspond to the scale domain. +/// +/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values. +/// +/// ```html +/// +/// ... +/// +/// ``` +/// +/// The ``type`` provides the ways you can scale the domain or range of a plot. +/// +/// See ``LiveViewNativeCharts/Charts/ScaleType`` for a list of possible values. +/// +/// ```html +/// +/// ... +/// +/// ``` +/// +/// ## Arguments +/// * ``mapping`` +/// * ``domain`` +/// * ``range`` +/// * ``type`` +#if swift(>=5.8) +@_documentation(visibility: public) +#endif +struct ChartForegroundStyleScaleModifier: ViewModifier, Decodable { + /// The ``mapping`` Maps data categories to foreground styles. + /// + /// Create a mapping between a string and a ``LiveViewNativeCharts/Charts/BasicChartSymbolShape``. + /// + /// ```elixir + /// %{ + /// "A" => :circle, + /// "B" => :asterisk, + /// "C" => :square + /// } + /// ``` + #if swift(>=5.8) + @_documentation(visibility: public) + #endif + private let mapping: [String:BasicChartSymbolShape]? + + struct Mapping: Decodable { + let value: String + let symbol: BasicChartSymbolShape + } + + /// The possible data values plotted as foreground style in the chart. + /// + /// See ``LiveViewNativeCharts/Charts/AnyScaleDomain`` for more details. + #if swift(>=5.8) + @_documentation(visibility: public) + #endif + private let domain: AnyScaleDomain? + + /// The ``range`` of foreground styles that correspond to the scale domain. + /// + /// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values. + #if swift(>=5.8) + @_documentation(visibility: public) + #endif + private let range: [BasicChartSymbolShape]? + + /// The scale type. + /// + /// See ``LiveViewNativeCharts/Charts/ScaleType`` for a list of possible values. + #if swift(>=5.8) + @_documentation(visibility: public) + #endif + private let type: ScaleType? + + func body(content: Content) -> some View { + if let mapping { + if case let .some(.values(domain)) = domain { + content.chartForegroundStyleScale(domain: domain) { key in + mapping[key]! + } + } else { + content.chartForegroundStyleScale(domain: mapping.keys.sorted()) { key in + mapping[key]! + } + } + } else { + switch (domain, range, type) { + case let (domain?, range?, type?): + content.chartForegroundStyleScale(domain: domain, range: range, type: type) + case let (domain?, nil, type?): + content.chartForegroundStyleScale(domain: domain, type: type) + case let (nil, range?, type?): + content.chartForegroundStyleScale(range: range, type: type) + case (nil, nil, type?): + content.chartForegroundStyleScale(type: type) + case (nil, nil, nil): + content + } + } + } +} diff --git a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex new file mode 100644 index 0000000..edc36d6 --- /dev/null +++ b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex @@ -0,0 +1,12 @@ +defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartForegroundStyleScale do + use LiveViewNativePlatform.Modifier + + alias LiveViewNativeSwiftUiCharts.Types.{ScaleDomain, BasicChartSymbolShape, ScaleType} + + modifier_schema "chart_symbol_scale" do + field :mapping, {:map, BasicChartSymbolShape} + field :domain, ScaleDomain + field :range, {:array, BasicChartSymbolShape} + field :type, ScaleType + end +end From 314012f4cd0e4588a046a646393df738601a1180 Mon Sep 17 00:00:00 2001 From: Syed Date: Tue, 4 Jul 2023 23:04:01 +0500 Subject: [PATCH 2/5] Use AnyShapeStyle --- .../ChartForegroundStyleScale.swift | 22 +++++-------------- .../chart_foreground_style_scale.ex | 4 ++-- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift index c26a691..91fe4ce 100644 --- a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift +++ b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift @@ -11,14 +11,10 @@ import LiveViewNative /// The ``mapping`` Maps data categories to foreground styles. /// -/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values. +/// See ``LiveViewNativeCharts/LiveViewNative/SwiftUI/AnyShapeStyle`` for a list of possible values. /// /// ```html -/// :circle, -/// "B" => :asterisk, -/// "C" => :plus -/// })}> +/// /// ... /// /// ``` @@ -64,23 +60,15 @@ import LiveViewNative struct ChartForegroundStyleScaleModifier: ViewModifier, Decodable { /// The ``mapping`` Maps data categories to foreground styles. /// - /// Create a mapping between a string and a ``LiveViewNativeCharts/Charts/BasicChartSymbolShape``. - /// - /// ```elixir - /// %{ - /// "A" => :circle, - /// "B" => :asterisk, - /// "C" => :square - /// } - /// ``` + /// Create a mapping between a string and a ``LiveViewNativeCharts/LiveViewNative/SwiftUI/AnyShapeStyle``. #if swift(>=5.8) @_documentation(visibility: public) #endif - private let mapping: [String:BasicChartSymbolShape]? + private let mapping: [String:AnyShapeStyle]? struct Mapping: Decodable { let value: String - let symbol: BasicChartSymbolShape + let symbol: AnyShapeStyle } /// The possible data values plotted as foreground style in the chart. diff --git a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex index edc36d6..3d28a67 100644 --- a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex +++ b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex @@ -1,10 +1,10 @@ defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartForegroundStyleScale do use LiveViewNativePlatform.Modifier - alias LiveViewNativeSwiftUiCharts.Types.{ScaleDomain, BasicChartSymbolShape, ScaleType} + alias LiveViewNativeSwiftUiCharts.Types.{ScaleDomain, ShapeStyle, BasicChartSymbolShape, ScaleType} modifier_schema "chart_symbol_scale" do - field :mapping, {:map, BasicChartSymbolShape} + field :mapping, {:map, ShapeStyle} field :domain, ScaleDomain field :range, {:array, BasicChartSymbolShape} field :type, ScaleType From aa2862b67b630c4a4d145248be13ad5d582381de Mon Sep 17 00:00:00 2001 From: Syed Date: Tue, 4 Jul 2023 23:33:19 +0500 Subject: [PATCH 3/5] Change alias of ShapeStyle --- .../modifiers/view_fundamentals/chart_foreground_style_scale.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex index 3d28a67..b8511e8 100644 --- a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex +++ b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex @@ -2,6 +2,7 @@ defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartForegroundStyleScale do use LiveViewNativePlatform.Modifier alias LiveViewNativeSwiftUiCharts.Types.{ScaleDomain, ShapeStyle, BasicChartSymbolShape, ScaleType} + alias LiveViewNativeSwiftUi.Types.ShapeStyle modifier_schema "chart_symbol_scale" do field :mapping, {:map, ShapeStyle} From eee06bfd6a5a727797272d1599b5d8353d178c5c Mon Sep 17 00:00:00 2001 From: Syed Date: Tue, 4 Jul 2023 23:53:59 +0500 Subject: [PATCH 4/5] Address feedback --- .../ChartForegroundStyleScale.swift | 15 +++++++++++++-- .../chart_foreground_style_scale.ex | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift index 91fe4ce..ab52d0b 100644 --- a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift +++ b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift @@ -14,7 +14,10 @@ import LiveViewNative /// See ``LiveViewNativeCharts/LiveViewNative/SwiftUI/AnyShapeStyle`` for a list of possible values. /// /// ```html -/// +/// {:color, :blue}, +/// "B" => {:opacity, 0.5} +/// })}> /// ... /// /// ``` @@ -31,7 +34,7 @@ import LiveViewNative /// /// The ``range`` of foreground styles that correspond to the scale domain. /// -/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values. +/// See ``LiveViewNativeCharts/LiveViewNative/SwiftUI/AnyShapeStyle`` for a list of possible values. /// /// ```html /// @@ -61,6 +64,14 @@ struct ChartForegroundStyleScaleModifier: ViewModifier, Decodable { /// The ``mapping`` Maps data categories to foreground styles. /// /// Create a mapping between a string and a ``LiveViewNativeCharts/LiveViewNative/SwiftUI/AnyShapeStyle``. + /// + /// ```elixir + /// %{ + /// "A" => :circle, + /// "B" => :asterisk, + /// "C" => :square + /// } + /// ``` #if swift(>=5.8) @_documentation(visibility: public) #endif diff --git a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex index b8511e8..dc581a0 100644 --- a/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex +++ b/lib/live_view_native_swift_ui_charts/modifiers/view_fundamentals/chart_foreground_style_scale.ex @@ -1,13 +1,13 @@ defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartForegroundStyleScale do use LiveViewNativePlatform.Modifier - alias LiveViewNativeSwiftUiCharts.Types.{ScaleDomain, ShapeStyle, BasicChartSymbolShape, ScaleType} + alias LiveViewNativeSwiftUiCharts.Types.{ScaleDomain, ShapeStyle, ScaleType} alias LiveViewNativeSwiftUi.Types.ShapeStyle modifier_schema "chart_symbol_scale" do field :mapping, {:map, ShapeStyle} field :domain, ScaleDomain - field :range, {:array, BasicChartSymbolShape} + field :range, {:array, ShapeStyle} field :type, ScaleType end end From 12327a04ff8b94c53b47d1d1879df6c77b8dcf08 Mon Sep 17 00:00:00 2001 From: Syed Date: Mon, 7 Aug 2023 20:11:48 +0500 Subject: [PATCH 5/5] Address feedback --- .../Extensions/ChartForegroundStyleScaleModifier.md | 1 + .../ChartForegroundStyleScale.swift | 12 +++--------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md index 7c8d207..719f8a8 100644 --- a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md +++ b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartForegroundStyleScaleModifier.md @@ -3,3 +3,4 @@ @Metadata { @DocumentationExtension(mergeBehavior: append) @DisplayName("chart_foreground_style_scale", style: symbol) +} \ No newline at end of file diff --git a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift index ab52d0b..d5b5542 100644 --- a/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift +++ b/Sources/LiveViewNativeCharts/Modifiers/View Fundamentals Modifiers/ChartForegroundStyleScale.swift @@ -67,9 +67,8 @@ struct ChartForegroundStyleScaleModifier: ViewModifier, Decodable { /// /// ```elixir /// %{ - /// "A" => :circle, - /// "B" => :asterisk, - /// "C" => :square + /// "A" => {:color, :blue}, + /// "B" => {:opacity, 0.5} /// } /// ``` #if swift(>=5.8) @@ -77,11 +76,6 @@ struct ChartForegroundStyleScaleModifier: ViewModifier, Decodable { #endif private let mapping: [String:AnyShapeStyle]? - struct Mapping: Decodable { - let value: String - let symbol: AnyShapeStyle - } - /// The possible data values plotted as foreground style in the chart. /// /// See ``LiveViewNativeCharts/Charts/AnyScaleDomain`` for more details. @@ -96,7 +90,7 @@ struct ChartForegroundStyleScaleModifier: ViewModifier, Decodable { #if swift(>=5.8) @_documentation(visibility: public) #endif - private let range: [BasicChartSymbolShape]? + private let range: [AnyShapeStyle]? /// The scale type. ///