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

Points in line diagramm do not match values #172

Open ns1045 opened 2 years ago

ns1045 commented 2 years ago

In a line diagram the marked points do not match the corresponding values. The y-Axis is shifted by ca 6

PointsOfValuesDoesNotMatches
fredeiden commented 2 years ago

I was not able to replicate your result. Maybe attach the rest of your code? I made a simplified version of your implementation and got a correct result. You might be able to spot something that is causing your issue. ` struct Chart: View {

let data = lineData()

var body: some View {

    LineChart(chartData: data)
        .pointMarkers(chartData: data)
        .xAxisGrid(chartData: data)
        .yAxisGrid(chartData: data)
        .yAxisLabels(chartData: data)
        .id(data.id)
        .padding()
}

static func lineData() -> LineChartData {

    let data = LineDataSet(dataPoints: [LineChartDataPoint(value: 458.0),
                                        LineChartDataPoint(value: 464.3),
                                        LineChartDataPoint(value: 480.3),
                                        LineChartDataPoint(value: 490.8),
                                        LineChartDataPoint(value: 500.2),
                                        LineChartDataPoint(value: 490.7),
                                        LineChartDataPoint(value: 460.0)],
                           pointStyle: PointStyle(pointSize: 2))

    let chartStyle = LineChartStyle(xAxisGridStyle: GridStyle(numberOfLines: 7),
                                    yAxisGridStyle: GridStyle(numberOfLines: 8),
                                    yAxisNumberOfLabels: 8,
                                    baseline: .minimumWithMaximum(of: 450),
                                    topLine: .maximum(of: 520))

    return LineChartData(dataSets: data,
                         chartStyle: chartStyle)

}

}

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

Screen Shot 2022-02-19 at 9 30 43 AM
ns1045 commented 2 years ago

`struct Chart: View {

let data = lineData() let height: Double;

init(_ height: CGFloat) { self.height = height; }

var body: some View { FilledLineChart(chartData: data) .pointMarkers(chartData: data) .infoBox(chartData: data) .yAxisLabels(chartData: data) .xAxisLabels(chartData: data) .id(data.id) .padding() .frame(height: height) }

static func lineData() -> LineChartData {

let data = LineDataSet(dataPoints: [LineChartDataPoint(value: 0), //458.0),
                                    LineChartDataPoint(value: 0), //464.3),
                                    LineChartDataPoint(value: 0), //480.3),
                                    LineChartDataPoint(value: 0), //490.8),
                                    LineChartDataPoint(value: 0), //500.2),
                                    LineChartDataPoint(value: 0), //512.8),
                                    LineChartDataPoint(value: 0), //490.7),
                                    LineChartDataPoint(value: 0)], //460.0)],
                       legendTitle: "",
                       pointStyle: PointStyle(pointSize: 1, borderColour: UI_Constants.background, fillColour: .white, pointType: .filled),
                       style: LineStyle(lineColour: ColourStyle(colours: [.white, UI_Constants.lightBackground], startPoint: .top, endPoint: .bottom), lineType: .line))

let chartStyle = LineChartStyle(infoBoxPlacement: .infoBox(isStatic: true),
                                infoBoxBorderColour: UI_Constants.background,
                                infoBoxBorderStyle: StrokeStyle(lineWidth: 1),

                                markerType: .vertical(attachment: .line(dot: .style(DotStyle()))),

                                xAxisGridStyle: GridStyle(numberOfLines: 20,
                                                          lineColour: UI_Constants.background.opacity(0.2),
                                                          lineWidth: 1,
                                                          dash: [2],
                                                          dashPhase: 1),
                                xAxisLabelPosition: .bottom,
                                xAxisLabelColour: UI_Constants.background,
                                xAxisLabelsFrom: .chartData(rotation: .degrees(90)),

                                yAxisGridStyle: GridStyle(numberOfLines: 5,
                                                          lineColour: UI_Constants.background.opacity(0.2),
                                                          lineWidth: 1,
                                                          dash: [16],
                                                          dashPhase: 1),
                                yAxisLabelPosition: .leading,
                                yAxisLabelColour: UI_Constants.background,
                                yAxisNumberOfLabels: 5,

                                baseline: .minimumWithMaximum(of: -20), //450),
                                topLine: .maximum(of: 20), //520))

                                globalAnimation: .easeOut(duration: 1));

return LineChartData(dataSets: data,
                     chartStyle: chartStyle)

}

}

struct Chart_Previews: PreviewProvider { static var previews: some View { Chart(100) } }`

I can reproduce that with following example code. The effect does not occurs when I remove the .infoBox(chartData: data)

rifqifadh commented 1 year ago

I have the same issue when adding .infoBox, is there a fix for this issue?