ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.42k stars 5.97k forks source link

Can't draw chart in widget SwiftUI #4644

Open ClayPwr opened 3 years ago

ClayPwr commented 3 years ago

What did you do?

ℹ Tried to add charts inside widget

What did you expect to happen?

ℹ Saw a new chart

What happened instead?

ℹ Just yellow rectangle

Charts Environment

Charts version/Branch/Commit Number: 3.6.0 Xcode version: 12.4 Swift version: 5 Platform(s) running Charts: iOS widgets macOS version running Xcode: 11.2.3 Screen Shot 2021-05-20 at 7 24 37 PM

JanC commented 3 years ago

hey, what exactly is your issue? I'm using Charts in a SwiftUI project just fine. Can you maybe post your code as source code instead of an image?

ClayPwr commented 3 years ago

Hi, yeah you can take this Try with use widget preview, when I start preview on phone it work good.

` import Charts import SwiftUI import WidgetKit

struct Bar : UIViewRepresentable {

var entries : [BarChartDataEntry]

func makeUIView(context: Context) -> BarChartView {

    let chart = BarChartView()

    chart.data = addData()

    return chart

}

func updateUIView(_ uiView: BarChartView, context: Context) {

    uiView.data = addData()

}

func addData() -> BarChartData{

    let data = BarChartData()

    let dataSet = BarChartDataSet(entries: entries)

    data.addDataSet(dataSet)

    return data

}

typealias UIViewType = BarChartView

}

struct Bar_Previews: PreviewProvider {

static var previews: some View {

    Bar(entries: [BarChartDataEntry(x: 1, y: 1)])
    .previewContext(WidgetPreviewContext(family: .systemMedium))
}

}`

JanC commented 3 years ago

Hi, yeah you can take this Try with use widget preview, when I start preview on phone it work good.

I'm confused now. Is you problem just with the XCode SwiftUI preview or when actually running the app/widget? Your ticket description is not quite clear

ClayPwr commented 3 years ago

@JanC sorry for that, problem in both situation when run app on simulator and on preview

iliaskarim commented 3 years ago

@ClayPwr

UIKit views wrapped in UIViewRepresentable will not work in WidgetKit.

Source: https://developer.apple.com/forums/thread/653471?answerId=619627022#619627022