diff --git a/Example/Example/ContentView.swift b/Example/Example/ContentView.swift index 078da8d..eb7667f 100644 --- a/Example/Example/ContentView.swift +++ b/Example/Example/ContentView.swift @@ -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]() @@ -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)) diff --git a/README.md b/README.md index 1ad96dc..45ec1cc 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Sources/GraphRangeSlider/Extensions/EnvironmentValues+.swift b/Sources/GraphRangeSlider/Extensions/EnvironmentValues+.swift index 13436cb..7951a0e 100644 --- a/Sources/GraphRangeSlider/Extensions/EnvironmentValues+.swift +++ b/Sources/GraphRangeSlider/Extensions/EnvironmentValues+.swift @@ -8,4 +8,5 @@ extension EnvironmentValues { @Entry var sliderBarHeight: CGFloat = 8 @Entry var margin: CGFloat = 0 @Entry var minCount = 1 + @Entry var isHiddenChart = false } diff --git a/Sources/GraphRangeSlider/Extensions/View+EnvironmentValues.swift b/Sources/GraphRangeSlider/Extensions/View+EnvironmentValues.swift index 2ae62d5..625e96d 100644 --- a/Sources/GraphRangeSlider/Extensions/View+EnvironmentValues.swift +++ b/Sources/GraphRangeSlider/Extensions/View+EnvironmentValues.swift @@ -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) + } } diff --git a/Sources/GraphRangeSlider/GraphRangeSlider.swift b/Sources/GraphRangeSlider/GraphRangeSlider.swift index dd14919..c410fba 100644 --- a/Sources/GraphRangeSlider/GraphRangeSlider.swift +++ b/Sources/GraphRangeSlider/GraphRangeSlider.swift @@ -23,35 +23,35 @@ public struct GraphRangeSlider: 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, @@ -62,6 +62,9 @@ public struct GraphRangeSlider: View where Data: RandomAccessCollectio .frame(height: toggleRadius * 2) } } + .background( + Color.clear.viewSize { width = $0.width } + ) .onChange(of: leftCurrentIndex) { _ in onChangedSelectedData() }