lludo / SwiftSunburstDiagram

SwiftUI library to easily render diagrams given a tree of objects. Similar to ring chart, sunburst chart, multilevel pie chart.
MIT License
525 stars 47 forks source link

When used with SwiftUI, computed slice weights don't appear on display #7

Closed jeffbstewart closed 4 years ago

jeffbstewart commented 4 years ago

tl/dr: Sunburst displays all slices with equal weight when called from SwiftUI (instead of from UIKit).

I'm attempting to integrate SwiftSunburstDiagram in a project using SwiftUI. Most properties work properly, but all slices are displayed equally weighted. I've set a breakpoint, and the computed weights are computed (and computed properly). However, those weights being computed on the async DispatchQueue do not appear to cause the UI display to change.

Using your example from the main documentation, SunburstDiagram version 1.1.0, XCode version 11.5, iphone X running iOS 13.4 (but the same problem appears in the simulators) the following code reproduces the problem.

The only difference from your example demo is that because this is SwiftUI, there's no UIHostingController involved.

To recreate: File -> New Project -> SwiftUI, Single View.

Replace the contents of ContentView.swift with the following:

import SunburstDiagram
import SwiftUI

struct ContentView: View {

    let configuration = SunburstConfiguration(nodes: [
        Node(name: "Walking", value: 10.0, backgroundColor: .systemBlue),
        Node(name: "Restaurant", value: 30.0, backgroundColor: .systemRed, children: [
            Node(name: "Dessert", image: UIImage(named: "croissant"), value: 6.0),
            Node(name: "Dinner", image: UIImage(named: "poultry"), value: 10.0),
        ]),
        Node(name: "Transport", value: 10.0, backgroundColor: .systemPurple),
        Node(name: "Home", value: 50.0, backgroundColor: .systemTeal),
    ])

    var body: some View {
        SunburstView(configuration: configuration)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I expect a diagram much like the made readme.md demonstrates, instead I get a view with all slices equally weighted

What I expected: diagram-with-text

What I got: IMG_3701

Note that the transport slice is the same size as the home slice. It should be smaller.

jeffbstewart commented 4 years ago

RTFM. Default calculation mode behaves this way.

Set the calculationMode to .parentDependent(totalValue: 100.0) gets what I was looking for.