简体   繁体   English

如何使 CareKitUI 条形图与 SwiftUI 兼容

[英]How to make CareKitUI Bar Charts compatible with SwiftUI

Currently, there is limited support for CareKit using SwiftUI.目前,有限的支持CareKit使用SwiftUI。

I understand, generally, the idea of making an object conforming to UIViewRepresentable , but am struggling to get going with how this would work in practice.一般来说,我理解使对象符合UIViewRepresentable ,但我正在努力了解这在实践中的工作方式。

The following is example code from the readme :以下是自述文件中的示例代码:

let chartView = OCKCartesianChartView(type: .bar)

chartView.headerView.titleLabel.text = "Doxylamine"

chartView.graphView.dataSeries = [
    OCKDataSeries(values: [0, 1, 1, 2, 3, 3, 2], title: "Doxylamine")
]

So the init(type) , headerView.titleLabel and graphView.dataSeries would need to be set as @Binding variables in a struct conforming to UIViewRepresentable , but I'm struggling to figure out how I have to use the following two functions:因此init(type)headerView.titleLabelgraphView.dataSeries需要在符合UIViewRepresentable的结构中设置为@Binding变量,但我正在努力弄清楚我必须如何使用以下两个函数:

func makeUIView() {}

func updateUIView() {}

Any help would be much appreciated.任何帮助将非常感激。

Actually only data requires binding, because type is part of initialization and title is hardly possible to be changed, so here is possible variant实际上只有数据需要绑定,因为类型是初始化的一部分,标题几乎不可能改变,所以这里有可能的变体

struct CartesianChartView: UIViewRepresentable {
    var title: String
    var type: OCKCartesianGraphView.PlotType = .bar
    @Binding var data: [OCKDataSeries]

    func makeUIView(context: Context) -> OCKCartesianChartView {
        let chartView = OCKCartesianChartView(type: type)

        chartView.headerView.titleLabel.text = title
        chartView.graphView.dataSeries = data

        return chartView
    }

    func updateUIView(_ uiView: OCKCartesianChartView, context: Context) {
        // will be called when bound data changed, so update internal 
        // graph here when external dataset changed
        uiView.graphView.dataSeries = data
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM