Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chart_forground_style_scale modifier #97

Open
wants to merge 7 commits into
base: chart-scale-modifiers
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/LiveViewNativeCharts/ChartsRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct ChartsRegistry<Root: RootRegistry>: CustomRegistry {
}

public enum ModifierType: String {
case chartForegroundStyleScale = "chart_foreground_style_scale"
case chartXAxis = "chart_x_axis"
case chartYAxis = "chart_y_axis"
case chartXScale = "chart_x_scale"
Expand All @@ -33,6 +34,8 @@ public struct ChartsRegistry<Root: RootRegistry>: CustomRegistry {

public static func decodeModifier(_ type: ModifierType, from decoder: Decoder) throws -> some ViewModifier {
switch type {
case .chartForegroundStyleScale:
try ChartForegroundStyleScaleModifier(from: decoder)
case .chartXAxis:
try ChartXAxisModifier<Root>(from: decoder)
case .chartYAxis:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
}

## Topics
### Foreground Style Scale Modifiers
- ``ChartForegroundStyleScaleModifier``
### Axis Modifiers
- ``ChartXAxisModifier``
- ``ChartYAxisModifier``
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ``LiveViewNativeCharts/ChartForegroundStyleScaleModifier``

@Metadata {
@DocumentationExtension(mergeBehavior: append)
@DisplayName("chart_foreground_style_scale", style: symbol)
murtza marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// 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/LiveViewNative/SwiftUI/AnyShapeStyle`` for a list of possible values.
///
/// ```html
/// <Chart modifiers={chart_foreground_style_scale(mapping: %{
/// "A" => {:color, :blue},
/// "B" => {:opacity, 0.5}
/// })}>
/// ...
/// </Chart>
/// ```
///
/// 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
/// <Chart modifiers={chart_foreground_style_scale(domain: ["A", "B", "C"])}>
/// ...
/// </Chart>
/// ```
///
/// The ``range`` of foreground styles that correspond to the scale domain.
///
/// See ``LiveViewNativeCharts/LiveViewNative/SwiftUI/AnyShapeStyle`` for a list of possible values.
///
/// ```html
/// <Chart modifiers={chart_foreground_style_scale(range: [:circle, :asterisk, :plus])}>
/// ...
/// </Chart>
/// ```
///
/// 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
/// <Chart modifiers={chart_foreground_style_scale(type: :category)}>
/// ...
/// </Chart>
/// ```
///
/// ## 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/LiveViewNative/SwiftUI/AnyShapeStyle``.
///
/// ```elixir
/// %{
/// "A" => :circle,
/// "B" => :asterisk,
/// "C" => :square
murtza marked this conversation as resolved.
Show resolved Hide resolved
/// }
/// ```
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let mapping: [String:AnyShapeStyle]?

struct Mapping: Decodable {
let value: String
let symbol: AnyShapeStyle
}
murtza marked this conversation as resolved.
Show resolved Hide resolved

/// 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]?
murtza marked this conversation as resolved.
Show resolved Hide resolved

/// 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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartForegroundStyleScale do
use LiveViewNativePlatform.Modifier

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, ShapeStyle}
field :type, ScaleType
end
end