willdale / SwiftUICharts

A charts / plotting library for SwiftUI. Works on macOS, iOS, watchOS, and tvOS and has accessibility features built in.
MIT License
843 stars 105 forks source link

MultiLineChart on watchOS repeatedly re-draw the lines when new points are added #163

Open mxjcyw opened 2 years ago

mxjcyw commented 2 years ago

How can I set it to work like LineChart

willdale commented 2 years ago

Hey @mxjcyw,

I'm not super sure what you mean? Is the line re-rendering it's animation in a loop?

Please could you post the code you're having issues with?


Here is a Gist with a working implementation of multi line chart for WatchOS.

https://gist.github.com/willdale/7f9bead20117ace09dcda435ca7fe283

MultiLineChartWatchOSDemo

mxjcyw commented 2 years ago

when i add a new value,the chart redraws all the points

发自我的iPhone

在 2022年1月13日,04:09,Will Dale @.***> 写道:



Hey @mxjcywhttps://github.com/mxjcyw,

I'm not super sure what you mean? Is the line re-rendering it's animation in a loop?

Please could you post the code you're having issues with?


Here is a Gisthttps://gist.github.com/willdale/7f9bead20117ace09dcda435ca7fe283 with a working implementation of multi line chart for WatchOS.

https://gist.github.com/willdale/7f9bead20117ace09dcda435ca7fe283

[MultiLineChartWatchOSDemo]https://user-images.githubusercontent.com/63838770/149213955-fa3ac9b3-dd25-4c16-9bd8-7829be370ced.png

— Reply to this email directly, view it on GitHubhttps://github.com/willdale/SwiftUICharts/issues/163#issuecomment-1011409995, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AXIW5IR54RIZHUONQ7NYTCLUVXNX3ANCNFSM5LZDDLBA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.Message ID: @.***>

willdale commented 2 years ago

Without seeing any code it's hard for me to work out what might be going on.

One thing you could try would be to add .id(data.id) at the end of the chain of modifiers.

mxjcyw commented 2 years ago

Thank you for your reply. I hope add new point will be refreshed only once, and there is no animation effect. I don't know whether it can be realized

import SwiftUI import SwiftUICharts

var inx = 0

struct MultiLineChartWatchDemo:View { @State var lineDatas1:[LineChartDataPoint] = [LineChartDataPoint]() @State var lineDatas2:[LineChartDataPoint] = [LineChartDataPoint]()

var body: some View {
    VStack{
    MultiLineChart(chartData:MultiLineChartData(dataSets:MultiLineDataSet(dataSets: [
        LineDataSet(dataPoints:lineDatas1,
        style:LineStyle(lineColour:ColourStyle(colour: .red),lineType: .line)),

        LineDataSet(dataPoints:lineDatas2,
        style:LineStyle(lineColour:ColourStyle(colour: .blue),lineType: .line))
    ]))).id(MultiLineChartData(dataSets: MultiLineDataSet(dataSets: [
        LineDataSet(dataPoints:lineDatas1,
        style:LineStyle(lineColour:ColourStyle(colour: .red),lineType: .line)),

        LineDataSet(dataPoints:lineDatas2,
        style:LineStyle(lineColour:ColourStyle(colour: .blue),lineType: .line))
    ])).id)
    }.onAppear{
        Timer.scheduledTimer(withTimeInterval: 3, repeats: true){ tempTimer in
            inx = inx + 1
            let randomInt1 =Int.random(in: 80..<100)

                lineDatas1.append(LineChartDataPoint(value: Double(randomInt1), xAxisLabel: String(inx)))

            let randomInt2 =Int.random(in: 80..<100)

                lineDatas2.append(LineChartDataPoint(value: Double(randomInt2), xAxisLabel: String(inx)))
        }

    }}

}

structMultiLineChartWatchDemo_Previews:PreviewProvider { static var previews: someView { MultiLineChartWatchDemo() } }

发自我的iPhone

在 2022年1月13日,16:12,Will Dale @.***> 写道:



Without seeing any code it's hard for me to work out what might be going on.

One thing you could try would be to add .id(data.id) at the end of the chain of modifiers.

— Reply to this email directly, view it on GitHubhttps://github.com/willdale/SwiftUICharts/issues/163#issuecomment-1011896437, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AXIW5IQNIUZVK2NTF562NZLUV2CPXANCNFSM5LZDDLBA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.Message ID: @.***>