willdale / SwiftUICharts

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

yAxis Double values? #105

Closed gesabo closed 3 years ago

gesabo commented 3 years ago

@willdale I can't seem to figure out what I am doing wrong here, attempting to display yAxis Labels in a double format e.g. 15.8 but they are all the same here? 🤔

image


import SwiftUI
import SwiftUICharts

struct DemoChart: View {

    @State var data : LineChartData = weekOfData()

    var body: some View {
        VStack {
            FilledLineChart(chartData: data)
                .filledTopLine(chartData: data,
                               lineColour: ColourStyle(colour: .red),
                               strokeStyle: StrokeStyle(lineWidth: 3))
                .touchOverlay(chartData: data, unit: .suffix(of: "Steps"))
                .pointMarkers(chartData: data)
                .yAxisPOI(chartData: data,
                          markerName: "Normal Range Max",
                          markerValue: 16.22,
                          labelPosition: .center(specifier: "%.0f"),
                          labelColour: Color.black,
                          labelBackground: Color(red: 1.0, green: 0.75, blue: 0.25),
                          lineColour: Color(red: 1.0, green: 0.75, blue: 0.25),
                          strokeStyle: StrokeStyle(lineWidth: 3, dash: [5,10]))
                .yAxisPOI(chartData: data,
                          markerName: "Normal Range Min",
                          markerValue: 15.78,
                          labelPosition: .center(specifier: "%.0f"),
                          labelColour: Color.white,
                          labelBackground: Color(red: 0.25, green: 0.75, blue: 1.0),
                          lineColour: Color(red: 0.25, green: 0.75, blue: 1.0),
                          strokeStyle: StrokeStyle(lineWidth: 3, dash: [5,10]))
                .averageLine(chartData: data,
                             strokeStyle: StrokeStyle(lineWidth: 3, dash: [5,10]))
                .xAxisGrid(chartData: data)
                .yAxisGrid(chartData: data)
                .xAxisLabels(chartData: data)
                 .yAxisLabels(chartData: data)  //if I comment out this line, yAxis labels are ints? 
                .yAxisLabels(chartData: data, specifier: "%.2f")
                .headerBox(chartData: data)
                .legends(chartData: data, columns: [GridItem(.flexible()), GridItem(.flexible())])
                .id(data.id)
                .frame(minWidth: 150, maxWidth: 900, minHeight: 150, idealHeight: 500, maxHeight: 600, alignment: .center)
                .padding(.horizontal)
        }
        .navigationTitle("Filled Line")
    }

    static func weekOfData() -> LineChartData {
        let data = LineDataSet(dataPoints: [
            LineChartDataPoint(value: 15.8, xAxisLabel: "M", description: "Monday"),
            LineChartDataPoint(value: 15.8, xAxisLabel: "T", description: "Tuesday"),
            LineChartDataPoint(value: 16.4,  xAxisLabel: "W", description: "Wednesday"),
            LineChartDataPoint(value: 15.9, xAxisLabel: "T", description: "Thursday"),
            LineChartDataPoint(value: 16.2, xAxisLabel: "F", description: "Friday"),
            LineChartDataPoint(value: 16.1, xAxisLabel: "S", description: "Saturday"),
            LineChartDataPoint(value: 15.6,  xAxisLabel: "S", description: "Sunday")
        ],
        legendTitle: "Test One",
        pointStyle: PointStyle(),
        style: LineStyle(lineColour: ColourStyle(colours: [Color.red.opacity(0.50),
                                                           Color.red.opacity(0.00)],
                                                 startPoint: .top,
                                                 endPoint: .bottom),
                         lineType: .line))

        return LineChartData(dataSets: data,
                             metadata: ChartMetadata(title: "Some Data", subtitle: "A Week"),
                             xAxisLabels: ["Monday", "Thursday", "Sunday"],
                             chartStyle: LineChartStyle(infoBoxPlacement: .header,
                                                        markerType: .full(attachment: .point),
                                                        xAxisLabelsFrom: .chartData(rotation: .degrees(0)), yAxisNumberOfLabels : 7,
                                                        yAxisLabelType: .numeric,
                                                        baseline: .minimumValue,
                                                        topLine: .maximumValue))
    }
}

struct DemoChart_Previews: PreviewProvider {
    static var previews: some View {
        DemoChart()
    }
}
willdale commented 3 years ago

The xAxisLabels are fixed as of v2.8.3.