Код: Выделить всё
import Charts
import SwiftUI
@MainActor
struct GraphScreen: View {
@Environment(ThemeManager.self) private var theme
@Environment(DataModel.self) private var dataModel: DataModel
@State private var scrollPosition = Date()
@State private var selectedPrice: CandlestickDataPoint?
@State var investmentData: [CandlestickDataPoint] = []
@State var minimum: Double = 0
@State var maximum: Double = 1
@State var minimumAdjusting: Double = 0.995
@State var maximumAdjusting: Double = 1.005
@State private var currentZoom = 0.0
@State private var totalZoom = 24.0
@State private var strideMinuteCount = 15
@State private var barWidth: CGFloat = 10
var body: some View {
content
.task {
do {
let response = try await dataModel.userStock()
investmentData = ChartManager.shared.process(response: response)
totalZoom = 12
} catch {
print(error.localizedDescription)
}
}
.onChange(of: totalZoom, { oldValue, newValue in
// Adjust the barWidth and strideMinuteCount based on the zoom level
strideMinuteCount = Int(5 * newValue) // Adjust the stride count based on some formula
barWidth = max(1, CGFloat(48 / newValue)) // Adjust the bar width
let visibleData = getVisibleValues()
setMinMax(forValues: visibleData)
})
}
func getVisibleValues() -> [CandlestickDataPoint] {
let minDate = scrollPosition
let maxDate = scrollPosition.addingTimeInterval((currentZoom + totalZoom) * 1800)
let values = investmentData.filter({ $0.date >= minDate && $0.date item.open ? Color.green : Color.red)
RuleMark(
x: .value("Date", item.date),
yStart: .value("Low", item.low),
yEnd: .value("High", item.high)
)
.foregroundStyle(item.close > item.open ? Color.green : Color.red)
}
.chartYScale(domain: [minimum, maximum])
.chartYAxis {
AxisMarks(position: .automatic, values: .automatic) {
AxisGridLine()
.foregroundStyle(theme.selected.bodyTextGrayColor)
AxisValueLabel()
.foregroundStyle(theme.selected.bodyTextGrayColor)
}
}
.chartXAxis {
AxisMarks(values: .stride(by: .minute, count: strideMinuteCount)) {
AxisGridLine()
.foregroundStyle(theme.selected.bodyTextGrayColor)
AxisValueLabel()
.foregroundStyle(theme.selected.bodyTextGrayColor)
}
}
//.disabled(true)
.chartScrollableAxes([.horizontal])
.chartScrollPosition(x: $scrollPosition)
.onChange(of: scrollPosition) {
print(scrollPosition)
let visibleData = getVisibleValues()
setMinMax(forValues: visibleData)
}
.chartXVisibleDomain(length: (currentZoom + totalZoom) * 1800)
.chartGesture { chartProxy in
magnifyGesture
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... tion-gestu
Мобильная версия