Skip to content

Commit

Permalink
Merge pull request #13 from tomosaaan/feat/range-hidden-option
Browse files Browse the repository at this point in the history
feat: isHiddenChart option
  • Loading branch information
tomosaaan authored Nov 24, 2024
2 parents 2628008 + f8bebf3 commit 40fd4f0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
6 changes: 4 additions & 2 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Element: GraphRangeElement {
}

struct ContentView: View {
let data: [Element] = (1...50).map { .init(x: $0 * 10, y: $0 * 10) }
let data: [Element] = (1...5).map { .init(x: $0 * 10, y: $0 * 10) }

@State var selectedData = [Element]()

Expand All @@ -17,7 +17,9 @@ struct ContentView: View {
id: \.x,
selectedData: $selectedData
)
.frame(height: 300, alignment: .bottom)
// .hiddenChart(true)
// .frame(height: 300, alignment: .bottom)
// .hiddenChart(true)
// .minCount(3)
// .graph(width: .ratio(0.8), height: .fixed(50))
// .inactiveColor(Color.gray.opacity(0.8))
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ This setting value is equivalent to the `MarkDimension` on SwiftCharts.
}
```

- Hidden the chart

You can set the flag that chat hidden.
```swift
.hiddenChart(true)
```

- Delegate functions

Called when the slider range changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ extension EnvironmentValues {
@Entry var sliderBarHeight: CGFloat = 8
@Entry var margin: CGFloat = 0
@Entry var minCount = 1
@Entry var isHiddenChart = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public extension View {
public func minCount(_ value: Int) -> some View {
environment(\.minCount, max(value, 1))
}

/// Set the hidden flag for chart
///
/// - Parameter value: frag of isHidden, defaults to `false`
public func hiddenChart(_ isHidden: Bool) -> some View {
environment(\.isHiddenChart, isHidden)
}
}
57 changes: 30 additions & 27 deletions Sources/GraphRangeSlider/GraphRangeSlider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ public struct GraphRangeSlider<Data, ID>: View where Data: RandomAccessCollectio
@Environment(\.toggleRadius) private var toggleRadius: CGFloat
@Environment(\.sliderBarHeight) private var sliderBarHeight: CGFloat
@Environment(\.margin) private var margin: CGFloat
@Environment(\.isHiddenChart) private var isHiddenChart: Bool

public var body: some View {
Chart(data, id: id) { data in
BarMark(
x: .value(PlottableKeys.x, String(describing: data.x)),
y: .value(PlottableKeys.y, data.y),
width: builder.barDimension.call(data)?.width ?? .automatic,
height: builder.barDimension.call(data)?.height ?? .automatic
)
.foregroundStyle(
by: .value(
PlottableKeys.status,
selectedData.contains(data) ? Status.active: Status.inactive
)
)
}
.padding(.horizontal, toggleRadius * 2)
.padding(.bottom, toggleRadius + sliderBarHeight / 2 + margin)
.chartXAxis(.hidden)
.chartYAxis(.hidden)
.chartLegend(.hidden)
.chartForegroundStyleScale([
Status.active: activeColor,
Status.inactive: inactiveColor
])
.background(
Color.clear.viewSize { width = $0.width }
)
.overlay(alignment: .bottom) {
VStack(spacing: margin) {
if !isHiddenChart {
Chart(data, id: id) { data in
BarMark(
x: .value(PlottableKeys.x, String(describing: data.x)),
y: .value(PlottableKeys.y, data.y),
width: builder.barDimension.call(data)?.width ?? .automatic,
height: builder.barDimension.call(data)?.height ?? .automatic
)
.foregroundStyle(
by: .value(
PlottableKeys.status,
selectedData.contains(data) ? Status.active: Status.inactive
)
)
}
.chartXAxis(.hidden)
.chartYAxis(.hidden)
.chartLegend(.hidden)
.chartForegroundStyleScale([
Status.active: activeColor,
Status.inactive: inactiveColor
])
.padding(.horizontal, toggleRadius * 2)
}

if !positions.isEmpty {
Slider(
positions: positions,
Expand All @@ -62,6 +62,9 @@ public struct GraphRangeSlider<Data, ID>: View where Data: RandomAccessCollectio
.frame(height: toggleRadius * 2)
}
}
.background(
Color.clear.viewSize { width = $0.width }
)
.onChange(of: leftCurrentIndex) { _ in
onChangedSelectedData()
}
Expand Down

0 comments on commit 40fd4f0

Please sign in to comment.