Skip to content

Commit 40fd4f0

Browse files
authored
Merge pull request #13 from tomosaaan/feat/range-hidden-option
feat: isHiddenChart option
2 parents 2628008 + f8bebf3 commit 40fd4f0

5 files changed

Lines changed: 49 additions & 29 deletions

File tree

Example/Example/ContentView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Element: GraphRangeElement {
77
}
88

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

1212
@State var selectedData = [Element]()
1313

@@ -17,7 +17,9 @@ struct ContentView: View {
1717
id: \.x,
1818
selectedData: $selectedData
1919
)
20-
.frame(height: 300, alignment: .bottom)
20+
// .hiddenChart(true)
21+
// .frame(height: 300, alignment: .bottom)
22+
// .hiddenChart(true)
2123
// .minCount(3)
2224
// .graph(width: .ratio(0.8), height: .fixed(50))
2325
// .inactiveColor(Color.gray.opacity(0.8))

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ This setting value is equivalent to the `MarkDimension` on SwiftCharts.
9898
}
9999
```
100100

101+
- Hidden the chart
102+
103+
You can set the flag that chat hidden.
104+
```swift
105+
.hiddenChart(true)
106+
```
107+
101108
- Delegate functions
102109

103110
Called when the slider range changes.

Sources/GraphRangeSlider/Extensions/EnvironmentValues+.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ extension EnvironmentValues {
88
@Entry var sliderBarHeight: CGFloat = 8
99
@Entry var margin: CGFloat = 0
1010
@Entry var minCount = 1
11+
@Entry var isHiddenChart = false
1112
}

Sources/GraphRangeSlider/Extensions/View+EnvironmentValues.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,11 @@ public extension View {
4343
public func minCount(_ value: Int) -> some View {
4444
environment(\.minCount, max(value, 1))
4545
}
46+
47+
/// Set the hidden flag for chart
48+
///
49+
/// - Parameter value: frag of isHidden, defaults to `false`
50+
public func hiddenChart(_ isHidden: Bool) -> some View {
51+
environment(\.isHiddenChart, isHidden)
52+
}
4653
}

Sources/GraphRangeSlider/GraphRangeSlider.swift

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ public struct GraphRangeSlider<Data, ID>: View where Data: RandomAccessCollectio
2323
@Environment(\.toggleRadius) private var toggleRadius: CGFloat
2424
@Environment(\.sliderBarHeight) private var sliderBarHeight: CGFloat
2525
@Environment(\.margin) private var margin: CGFloat
26+
@Environment(\.isHiddenChart) private var isHiddenChart: Bool
2627

2728
public var body: some View {
28-
Chart(data, id: id) { data in
29-
BarMark(
30-
x: .value(PlottableKeys.x, String(describing: data.x)),
31-
y: .value(PlottableKeys.y, data.y),
32-
width: builder.barDimension.call(data)?.width ?? .automatic,
33-
height: builder.barDimension.call(data)?.height ?? .automatic
34-
)
35-
.foregroundStyle(
36-
by: .value(
37-
PlottableKeys.status,
38-
selectedData.contains(data) ? Status.active: Status.inactive
39-
)
40-
)
41-
}
42-
.padding(.horizontal, toggleRadius * 2)
43-
.padding(.bottom, toggleRadius + sliderBarHeight / 2 + margin)
44-
.chartXAxis(.hidden)
45-
.chartYAxis(.hidden)
46-
.chartLegend(.hidden)
47-
.chartForegroundStyleScale([
48-
Status.active: activeColor,
49-
Status.inactive: inactiveColor
50-
])
51-
.background(
52-
Color.clear.viewSize { width = $0.width }
53-
)
54-
.overlay(alignment: .bottom) {
29+
VStack(spacing: margin) {
30+
if !isHiddenChart {
31+
Chart(data, id: id) { data in
32+
BarMark(
33+
x: .value(PlottableKeys.x, String(describing: data.x)),
34+
y: .value(PlottableKeys.y, data.y),
35+
width: builder.barDimension.call(data)?.width ?? .automatic,
36+
height: builder.barDimension.call(data)?.height ?? .automatic
37+
)
38+
.foregroundStyle(
39+
by: .value(
40+
PlottableKeys.status,
41+
selectedData.contains(data) ? Status.active: Status.inactive
42+
)
43+
)
44+
}
45+
.chartXAxis(.hidden)
46+
.chartYAxis(.hidden)
47+
.chartLegend(.hidden)
48+
.chartForegroundStyleScale([
49+
Status.active: activeColor,
50+
Status.inactive: inactiveColor
51+
])
52+
.padding(.horizontal, toggleRadius * 2)
53+
}
54+
5555
if !positions.isEmpty {
5656
Slider(
5757
positions: positions,
@@ -62,6 +62,9 @@ public struct GraphRangeSlider<Data, ID>: View where Data: RandomAccessCollectio
6262
.frame(height: toggleRadius * 2)
6363
}
6464
}
65+
.background(
66+
Color.clear.viewSize { width = $0.width }
67+
)
6568
.onChange(of: leftCurrentIndex) { _ in
6669
onChangedSelectedData()
6770
}

0 commit comments

Comments
 (0)