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_plot_style modifier #96

Open
wants to merge 6 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
13 changes: 11 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/liveview-native/liveview-client-swiftui",
"state" : {
"branch" : "content-builder",
"revision" : "b80ea68b9d2ccf9e0d695b1f89e41278f003e740"
"branch" : "main",
"revision" : "307d322ece3e658875f695183963721849efdaa4"
}
},
{
Expand All @@ -27,6 +27,15 @@
"version" : "1.2.2"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "f1e9245226002bb134884345d4809b9543da3666",
"version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-06-17-a"
}
},
{
"identity" : "swiftphoenixclient",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
targets: ["LiveViewNativeCharts"]),
],
dependencies: [
.package(url: "https://github.com/liveview-native/liveview-client-swiftui", branch: "content-builder")
.package(url: "https://github.com/liveview-native/liveview-client-swiftui", branch: "main")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
3 changes: 3 additions & 0 deletions Sources/LiveViewNativeCharts/ChartsRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ public struct ChartsRegistry<Root: RootRegistry>: CustomRegistry {
}

public enum ModifierType: String {
case chartPlotStyle = "chart_plot_style"
case chartXAxis = "chart_x_axis"
case chartYAxis = "chart_y_axis"
}

public static func decodeModifier(_ type: ModifierType, from decoder: Decoder) throws -> some ViewModifier {
switch type {
case .chartPlotStyle:
try ChartPlotStyleModifier<Root>(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 @@ -9,3 +9,5 @@
### Axis Modifiers
- ``ChartXAxisModifier``
- ``ChartYAxisModifier``
### Style Modifiers
- ``ChartPlotStyleModifier``
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ``LiveViewNativeCharts/ChartPlotStyleModifier``

@Metadata {
@DocumentationExtension(mergeBehavior: append)
@DisplayName("chart_plot_style", style: symbol)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ChartPlotStyleModifier.swift
//
//
// Created by Carson Katri on 6/29/23.
//

import Charts
import SwiftUI
import LiveViewNative

/// Style the chart's plot with modifiers.
///
/// Pass any modifiers that should be applied to the plot to the ``modifiers`` argument.
///
/// ```html
/// <Chart modifiers={chart_plot_style(aspect_ratio(1, content_mode: :fit) |> frame(width: 200))}>
/// ...
/// </Chart>
/// ```
///
/// ## Arguments
/// * ``modifiers``
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
struct ChartPlotStyleModifier<R: RootRegistry>: ViewModifier, Decodable {
/// The modifier stack to apply to the plot.
#if swift(>=5.8)
@_documentation(visibility: public)
#endif
private let modifiers: _ModifierStack<R>

func body(content: Content) -> some View {
content
.chartPlotStyle { content in
content.modifier(modifiers)
}
}

enum CodingKeys: CodingKey {
case modifiers
}
}
12 changes: 12 additions & 0 deletions lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartPlotStyle do
use LiveViewNativePlatform.Modifier

alias LiveViewNativeSwiftUi.Types.ModifierStack

modifier_schema "chart_plot_style" do
field :modifiers, ModifierStack
end

def params(%LiveViewNativeSwiftUi.Modifiers{} = modifiers), do: [modifiers: modifiers]
def params(params), do: params
end