ivnsch / SwiftCharts

Easy to use and highly customizable charts library for iOS
Apache License 2.0
2.53k stars 410 forks source link

Bars chart overlap after specific number of points #396

Closed Mohamed3amer closed 5 years ago

Mohamed3amer commented 5 years ago
private var chartSettings: ChartSettings {
        var chartSettings = ChartSettings()
        chartSettings.leading = 2
        chartSettings.top = 2
        chartSettings.trailing = 2
        chartSettings.bottom = 2
        chartSettings.axisStrokeWidth = 0.4
        chartSettings.spacingBetweenAxesX = 2
        chartSettings.spacingBetweenAxesY = 2
        chartSettings.labelsSpacing = 10
        chartSettings.labelsToAxisSpacingY = 0
        chartSettings.spacingBetweenAxesY = 0
        chartSettings.axisTitleLabelsToLabelsSpacing = 0
        chartSettings.zoomPan.panEnabled = true
        chartSettings.zoomPan.zoomEnabled = false
        chartSettings.zoomPan.maxZoomX = 3
        chartSettings.zoomPan.minZoomX = 3
        chartSettings.zoomPan.minZoomY = 1
        chartSettings.zoomPan.maxZoomY = 1
        return chartSettings
    }
  - When data source contains around (10 for ex) points, chart working without overlapping, but without fixed space between bars if you changed (increase/decrease) data source:

without overlapping

  - When data source contains around (35 for ex) points, bars overlapped:

overlapping

SwiftCharts version 0.6.5 Swift version 5

Mohamed3amer commented 5 years ago

Finally I found solution:

I had to set the minZoomX and maxZoomX (in chart settings) with fixed value which the number of pages for the chart (or number of scrollable area than the original width). why x ? because I want chart to scroll horizontally.

here is code:

        let barWidth: CGFloat = 30.0
        let spaceBetweenBars: CGFloat = 80.0

        let zoomXValue = CGFloat(barsDataSource.count) / (UIScreen.main.bounds.width / (barWidth + spaceBetweenBars))
        chartSettings.zoomPan.minZoomX = zoomXValue
        chartSettings.zoomPan.maxZoomX = zoomXValue