最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swiftui - chartScrollableAxes moves axis - Stack Overflow

programmeradmin2浏览0评论

I am trying to implement a horizontal scrollable chart. However, the y axis moves along when scrolling. How can I make the y axis stay put?

Here is my code:

struct DataPoint: Identifiable {
    var id = UUID()
    let x: Double
    let y: Double
}

let minGlobalX: Double = 0
let maxGlobalX: Double = 100

let data: [DataPoint] = (Int(minGlobalX) ..< Int(maxGlobalX)).map { DataPoint(x: Double($0), y: Double(arc4random()) / Double(UInt32.max)) }

struct ContentView: View {
    var body: some View {
        Chart(data) {
            LineMark(
                x: .value("x", $0.x),
                y: .value("y", $0.y)
            )
            .lineStyle(StrokeStyle(lineWidth: 1))
        }
        .chartScrollableAxes(.horizontal)
        .chartXScale(domain: [minGlobalX, maxGlobalX])
        .chartXAxis {
            AxisMarks(position: .bottom, values: [0]) {
                AxisGridLine(stroke: .init(lineWidth: 1))
            }
        }
        .chartYScale(domain: [0, 1])
        .chartYAxis {
            AxisMarks(position: .leading, values: [0]) {
                AxisGridLine(stroke: .init(lineWidth: 1))
            }
        }
        .padding(20)
    }
}

I am trying to implement a horizontal scrollable chart. However, the y axis moves along when scrolling. How can I make the y axis stay put?

Here is my code:

struct DataPoint: Identifiable {
    var id = UUID()
    let x: Double
    let y: Double
}

let minGlobalX: Double = 0
let maxGlobalX: Double = 100

let data: [DataPoint] = (Int(minGlobalX) ..< Int(maxGlobalX)).map { DataPoint(x: Double($0), y: Double(arc4random()) / Double(UInt32.max)) }

struct ContentView: View {
    var body: some View {
        Chart(data) {
            LineMark(
                x: .value("x", $0.x),
                y: .value("y", $0.y)
            )
            .lineStyle(StrokeStyle(lineWidth: 1))
        }
        .chartScrollableAxes(.horizontal)
        .chartXScale(domain: [minGlobalX, maxGlobalX])
        .chartXAxis {
            AxisMarks(position: .bottom, values: [0]) {
                AxisGridLine(stroke: .init(lineWidth: 1))
            }
        }
        .chartYScale(domain: [0, 1])
        .chartYAxis {
            AxisMarks(position: .leading, values: [0]) {
                AxisGridLine(stroke: .init(lineWidth: 1))
            }
        }
        .padding(20)
    }
}
Share Improve this question edited Mar 17 at 20:54 koen asked Mar 17 at 19:12 koenkoen 5,7537 gold badges58 silver badges102 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The axis lines are just AxisGridLines positioned at the "0" position of the chart. When you scroll the chart, the "0" position moves, and so the line moves with it as well.

You can add the lines using a chartOverlay instead. Position some 1-wide Rectangles using ChartProxy.plotContainerFrame:

.chartOverlay { proxy in
    GeometryReader { geo in
        let plotContainer = geo[proxy.plotContainerFrame!]
        Rectangle()
            .frame(width: 1, height: plotContainer.height)
            .offset(x: plotContainer.minX, y: plotContainer.minY)
        Rectangle()
            .frame(width: plotContainer.width, height: 1)
            .offset(x: plotContainer.minX, y: plotContainer.maxY)
    }
}
发布评论

评论列表(0)

  1. 暂无评论