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

Suggestion: Apply `specifier` from `yAxisLabels` to `floatingInfoBox` #193

Closed owenzhao closed 2 years ago

owenzhao commented 2 years ago
import SwiftUI
import SwiftUICharts

struct ContentView: View {
    @State var data:BarChartData?

    var body: some View {
        ScrollView {
            VStack {
                if let data = data {
                    BarChart(chartData: data)
                        .touchOverlay(chartData: data)
                        .yAxisGrid(chartData: data)
                        .yAxisLabels(chartData: data, specifier: "%0.f KGs")
                        .floatingInfoBox(chartData: data)
                        .headerBox(chartData: data)

                    Button {
                        data.dataSets.dataPoints.append(getRandomPoint())
                    } label: {
                        Text("Append Item")
                    }

                } else {
                    ProgressView().onAppear {
                        let points = (1...10).map { _ in getRandomPoint()}
                        let barStyle = BarStyle(barWidth: 0.5, colourFrom: .dataPoints)
                        data = BarChartData(dataSets: BarDataSet(dataPoints: points),
                                            barStyle: barStyle)
                    }
                }
            }
        }
        .padding()
        .frame(minWidth: 800)
    }

    private func getRandomPoint() -> BarChartDataPoint {
        let i = Int.random(in: 50...100)
        return BarChartDataPoint(value: Double(i), colour: ColourStyle(colour: .green))
    }
}

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

When it runs, it shows

Screen Shot 2022-04-28 at 16 54 29

It would be better if it shows 94 KGS instead of 94 only.

willdale commented 2 years ago

Hey @owenzhao, in the touch modifier there is an option for a number formatter.

.touchOverlay(chartData: data,
                          formatter: numberFormatter)