AppPear / ChartView

ChartView made in SwiftUI
MIT License
5.26k stars 645 forks source link

Bar Chart shows columns as full when no data is present #259

Open tmaasen opened 1 year ago

tmaasen commented 1 year ago

When there's no data in the bar chart, all the columns show as if they are full. However, as soon as I add a real positive number to any bar, the entire chart shows correctly with 1 bar having data and the rest are empty.

Screenshot 2023-02-24 at 5 45 57 PM

Description

Here's the code. There's a chance something might be wrong with the config of the chart? I also tried test data (0's) to confirm it wasn't a problem with the variables, but it shows the same issue. I'm on v2.0.0-beta.2

struct Analytics_Graph1: View {
    var viewModel: AnalyticsViewModel    
    @State private var g1HappyMoods: Double = 0
    @State private var g1NeutralMoods: Double = 0
    @State private var g1SickMoods: Double = 0
    @State private var g1OverateMoods: Double = 0
    @State private var g1TotalDataPoints: Int = 0

    let multiStyle = ChartStyle(backgroundColor: Color.green.opacity(0.2),
                                foregroundColor:
                                    [ColorGradient(.purple, .blue),
                                     ColorGradient(.orange, .red),
                                     ColorGradient(.green, .yellow),
                                     ColorGradient(.red, .purple),
                                     ColorGradient(.yellow, .orange),
                                    ])

    var body: some View {
            ZStack(alignment: .center) {
                CardView {
                    BarChart()
                }
                .data(
                    [((Mood.happy.text+Mood.happy.emoji), g1HappyMoods),
                     ((Mood.neutral.text+Mood.neutral.emoji), g1NeutralMoods),
                     ((Mood.sick.text+Mood.sick.emoji), g1SickMoods),
                     ((Mood.overate.text+Mood.overate.emoji), g1OverateMoods)
                    ])
                .chartStyle(multiStyle)
                .onAppear() {
                    getData()
                }
            }
            .padding()
    }
    func getData() {
        viewModel.getData(completion: { moods in
            g1TotalDataPoints = Int(moods[0])
            g1HappyMoods = moods[1]
            g1NeutralMoods = moods[2]
            g1SickMoods = moods[3]
            g1OverateMoods = moods[4]
        })
    }
}

Your Environment