ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.6k stars 6k forks source link

Bar chart content not centerd horizontally #4082

Open mickaelhero opened 5 years ago

mickaelhero commented 5 years ago

I have a problem I can not center the content chart horizontally

Capture d’écran 2019-07-30 à 12 21 43
private func setup(xValue: [String]) {
    noDataText = "No data"

    xAxis.drawGridLinesEnabled = false
    xAxis.labelPosition = .bottom
    xAxis.valueFormatter = IndexAxisValueFormatter(values: xValue)
    xAxis.granularity = 1
    xAxis.granularityEnabled = true
    xAxis.labelCount = xValue.count

    xAxis.axisMinimum = 0.0
    xAxis.axisMaximum = Double(xValue.count)

    leftAxis.spaceTop = 0.35
    leftAxis.axisMinimum = 0
    leftAxis.drawGridLinesEnabled = false

    rightAxis.enabled = false
}

And to create the chart:

private func setChart(xValue: xAxisValue, values: [SBChartEntry]) {
        let dataSets: [BarChartDataSet] = values.map {
            let set = BarChartDataSet(entries: $0.value.enumerated().map { index, element in
                BarChartDataEntry(x: Double(index), y: element)
            }, label: $0.name)

            set.colors = [$0.color]
            return set
        }

        let chartData = BarChartData(dataSets: dataSets)

        let groupSpace: Double = (values.count > 1) ? 0.7 / (Double(values.count) * 2.2) : 0.6
        let barSpace = (values.count > 1) ? 0.05 : 0
        let barWidth: Double = (values.count > 1) ? 0.75 / Double(values.count) : 0.4

        chartData.barWidth = barWidth

        if values.count > 1 {
            xAxis.centerAxisLabelsEnabled = true
            chartData.groupBars(fromX: Double(0), groupSpace: groupSpace, barSpace: barSpace)
        } else {
           // For one bar
        }

        notifyDataSetChanged()

        data = chartData

        //chart animation
        animate(xAxisDuration: 1, yAxisDuration: 1, easingOption: .linear)
    }

I can not configure when the chart contains only one array (so only one bar)

wongzigii commented 4 years ago

image

Same problem here. How can I fix this? The first label text is not aligning the center.