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_x_scale and chart_y_scale modifiers #88

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions Sources/LiveViewNativeCharts/ChartsRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ public struct ChartsRegistry<Root: RootRegistry>: CustomRegistry {
}

public enum ModifierType: String {
case chartSymbolScale = "chart_symbol_scale"
case chartBackground = "chart_background"
case chartOverlay = "chart_overlay"
case chartXAxis = "chart_x_axis"
case chartYAxis = "chart_y_axis"
case chartXScale = "chart_x_scale"
case chartYScale = "chart_y_scale"
}

public static func decodeModifier(_ type: ModifierType, from decoder: Decoder) throws -> some ViewModifier {
switch type {
case .chartSymbolScale:
try ChartSymbolScaleModifier(from: decoder)
case .chartBackground:
try ChartBackgroundModifier<Root>(from: decoder)
case .chartOverlay:
Expand All @@ -41,6 +46,10 @@ public struct ChartsRegistry<Root: RootRegistry>: CustomRegistry {
try ChartXAxisModifier<Root>(from: decoder)
case .chartYAxis:
try ChartYAxisModifier<Root>(from: decoder)
case .chartXScale:
try ChartXScaleModifier(from: decoder)
case .chartYScale:
try ChartYScaleModifier(from: decoder)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
### Axis Modifiers
- ``ChartXAxisModifier``
- ``ChartYAxisModifier``
### Axis Scale Modifiers
- ``ChartXScaleModifier``
- ``ChartYScaleModifier``
### Symbol Scale Modifiers
- ``ChartSymbolScaleModifier``
### Background Modifiers
- ``ChartBackgroundModifier``
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ``LiveViewNativeCharts/ChartSymbolScaleModifier``

@Metadata {
@DocumentationExtension(mergeBehavior: append)
@DisplayName("chart_symbol_scale", style: symbol)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ``LiveViewNativeCharts/ChartXScaleModifier``

@Metadata {
@DocumentationExtension(mergeBehavior: append)
@DisplayName("chart_x_scale", style: symbol)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ``LiveViewNativeCharts/ChartYScaleModifier``

@Metadata {
@DocumentationExtension(mergeBehavior: append)
@DisplayName("chart_y_scale", style: symbol)
}
125 changes: 125 additions & 0 deletions Sources/LiveViewNativeCharts/Modifiers/ChartSymbolScaleModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// ChartSymbolScaleModifier.swift
//
//
// Created by Carson Katri on 6/27/23.
//

import Charts
import SwiftUI
import LiveViewNative

/// Configures the scale used for the symbol.
///
/// Provide a ``domain`` and/or ``range`` to configure the scale.
///
/// The ``domain`` provides the list or range of values to display along the axis.
///
/// See ``AnyScaleDomain`` for more details.
///
/// - Note: Only categorical values, such as an array of values, can be used as a symbol domain.
/// Ranges cannot be used.
///
/// ```html
/// <Chart modifiers={chart_symbol_scale(domain: ["A", "B", "C"])}>
/// ...
/// </Chart>
/// ```
///
/// The ``range`` sets the symbols that get used for each value in the domain.
///
/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values.
///
/// ```html
/// <Chart modifiers={chart_symbol_scale(range: [:circle, :asterisk, :plus])}>
/// ...
/// </Chart>
/// ```
///
/// The ``mapping`` can be used to customize the symbols for each category.
///
/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values.
///
/// - Note: If no ``domain`` is provided, the keys of the map are used as the domain.
/// In this case, the keys will be sorted alphabetically.
///
/// ```html
/// <Chart modifiers={chart_symbol_scale(mapping: %{
/// "A" => :circle,
/// "B" => :asterisk,
/// "C" => :plus
/// })}>
/// ...
/// </Chart>
/// ```
///
/// ## Arguments
/// * ``domain``
/// * ``range``
/// * ``mapping``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct ChartSymbolScaleModifier: ViewModifier, Decodable {
/// The range or values to use for the symbols.
///
/// See ``AnyScaleDomain`` for more details.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let domain: AnyScaleDomain?

/// The range of symbols to use.
///
/// See ``LiveViewNativeCharts/Charts/BasicChartSymbolShape`` for a list of possible values.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let range: [BasicChartSymbolShape]?

/// A mapping from plottable values to symbol shapes.
///
/// 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
}

func body(content: Content) -> some View {
if let mapping {
if case let .some(.values(domain)) = domain {
content.chartSymbolScale(domain: domain) { key in
mapping[key]!
}
} else {
content.chartSymbolScale(domain: mapping.keys.sorted()) { key in
mapping[key]!
}
}
} else {
switch (domain, range) {
case let (domain?, range?):
content.chartSymbolScale(domain: domain, range: range)
case let (domain?, nil):
content.chartSymbolScale(domain: domain)
case let (nil, range?):
content.chartSymbolScale(range: range)
case (nil, nil):
content
}
}
}
}
91 changes: 91 additions & 0 deletions Sources/LiveViewNativeCharts/Modifiers/ChartXScaleModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// ChartXScaleModifier.swift
//
//
// Created by Carson Katri on 6/20/23.
//

import Charts
import SwiftUI
import LiveViewNative

/// Configures the scale used for the x axis.
///
/// Provide a ``domain``, ``range``, and/or ``scaleType`` to configure the scale.
///
/// The ``domain`` provides the list or range of values to display along the axis.
///
/// For example, the domain `{1, 5}` will display the values from 1 through 5 on the axis.
/// The default domain is `:automatic`.
///
/// See ``AnyScaleDomain`` for more details.
///
/// ```html
/// <Chart modifiers={chart_x_scale(@native, domain: {1, 5})}>
/// ...
/// </Chart>
/// ```
///
/// The ``range`` can be used to add padding to the chart.
///
/// See ``LiveViewNativeCharts/Charts/PlotDimensionScaleRange`` for a list of possible values.
///
/// ```html
/// <Chart modifiers={chart_x_scale(@native, range: {:plot_dimension, [start_padding: 50]})>
/// ...
/// </Chart>
/// ```
///
/// The ``scaleType`` describes the type of scale to use, such as `linear`, `log`, `category`, etc.
///
/// ```html
/// <Chart modifiers={chart_x_scale(@native, domain: ["A", "B", "C"], type: :category)}>
/// ...
/// </Chart>
/// ```
///
/// ## Arguments
/// * ``domain``
/// * ``range``
/// * ``scaleType``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct ChartXScaleModifier: ViewModifier, Decodable {
/// The range or values to use along the axis.
///
/// See ``AnyScaleDomain`` for more details.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let domain: AnyScaleDomain?

/// Apply padding to the start/end of the axis.
///
/// See ``LiveViewNativeCharts/Charts/PlotDimensionScaleRange`` for a list of possible values.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let range: PlotDimensionScaleRange?

/// `scale_type`, the type of scale to use.
///
/// See ``LiveViewNativeCharts/Charts/ScaleType`` for a list of possible values.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let scaleType: ScaleType?

func body(content: Content) -> some View {
switch (domain, range) {
case let (domain?, range?):
content.chartXScale(domain: domain, range: range, type: scaleType)
case let (domain?, nil):
content.chartXScale(domain: domain, type: scaleType)
case let (nil, range?):
content.chartXScale(range: range, type: scaleType)
case (nil, nil):
content.chartXScale(type: scaleType)
}
}
}
91 changes: 91 additions & 0 deletions Sources/LiveViewNativeCharts/Modifiers/ChartYScaleModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// ChartYScaleModifier.swift
//
//
// Created by Carson Katri on 6/20/23.
//

import Charts
import SwiftUI
import LiveViewNative

/// Configures the scale used for the y axis.
///
/// Provide a ``domain``, ``range``, and/or ``scaleType`` to configure the scale.
///
/// The ``domain`` provides the list or range of values to display along the axis.
///
/// For example, the domain `{1, 5}` will display the values from 1 through 5 on the axis.
/// The default domain is `:automatic`.
///
/// See ``AnyScaleDomain`` for more details.
///
/// ```html
/// <Chart modifiers={chart_y_scale(@native, domain: {1, 5})}>
/// ...
/// </Chart>
/// ```
///
/// The ``range`` can be used to add padding to the chart.
///
/// See ``LiveViewNativeCharts/Charts/PlotDimensionScaleRange`` for a list of possible values.
///
/// ```html
/// <Chart modifiers={chart_y_scale(@native, range: {:plot_dimension, [start_padding: 50]})>
/// ...
/// </Chart>
/// ```
///
/// The ``scaleType`` describes the type of scale to use, such as `linear`, `log`, `category`, etc.
///
/// ```html
/// <Chart modifiers={chart_y_scale(@native, domain: ["A", "B", "C"], type: :category)}>
/// ...
/// </Chart>
/// ```
///
/// ## Arguments
/// * ``domain``
/// * ``range``
/// * ``scaleType``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct ChartYScaleModifier: ViewModifier, Decodable {
/// The range or values to use along the axis.
///
/// See ``AnyScaleDomain`` for more details.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let domain: AnyScaleDomain?

/// Apply padding to the start/end of the axis.
///
/// See ``LiveViewNativeCharts/Charts/PlotDimensionScaleRange`` for a list of possible values.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let range: PlotDimensionScaleRange?

/// `scale_type`, the type of scale to use.
///
/// See ``LiveViewNativeCharts/Charts/ScaleType`` for a list of possible values.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let scaleType: ScaleType?

func body(content: Content) -> some View {
switch (domain, range) {
case let (domain?, range?):
content.chartYScale(domain: domain, range: range, type: scaleType)
case let (domain?, nil):
content.chartYScale(domain: domain, type: scaleType)
case let (nil, range?):
content.chartYScale(range: range, type: scaleType)
case (nil, nil):
content.chartYScale(type: scaleType)
}
}
}
Loading