Charts: BarChartView: xAxis values are not aligned with bars

Created on 22 Jan 2017  路  10Comments  路  Source: danielgindi/Charts

I migrated my diagrams to Charts 3.0.1. It worked quite smoothly, but I cannot manage to align the bar charts to my xAxis values. The following diagram should show a histogram of values between 0 and 10 (11 bars).

Variant 1

                barChart.xAxis.granularity = 1.0
                barChart.xAxis.granularityEnabled = true
                barChart.xAxis.setLabelCount(11, force: true)

All index values are shown, but not aligned with the bars.

bildschirmfoto 2017-01-22 um 19 06 29

Variant 2 (centerAxisLabelsEnabled)

                barChart.xAxis.granularity = 1.0
                barChart.xAxis.granularityEnabled = true
                barChart.xAxis.centerAxisLabelsEnabled = true
                barChart.xAxis.setLabelCount(11, force: true)

The index 10 is missing on the xAxis.

bildschirmfoto 2017-01-22 um 19 16 36

Variant 3 (fitBars)

                barChart.xAxis.granularity = 1.0
                barChart.xAxis.granularityEnabled = true
                barChart.xAxis.centerAxisLabelsEnabled = true
                barChart.xAxis.setLabelCount(11, force: true)
                barChart.fitBars = true

The xAxis starts with -1

bildschirmfoto 2017-01-22 um 19 24 59

**Variant 4 (axisMinimum and axisMaximum)

                barChart.xAxis.granularity = 1.0
                barChart.xAxis.granularityEnabled = true
                barChart.xAxis.centerAxisLabelsEnabled = true
                barChart.xAxis.setLabelCount(11, force: true)
                barChart.xAxis.axisMinimum = 0.0
                barChart.xAxis.axisMaximum = 10.0
                barChart.fitBars = true

The bars are not centered any more and the last xAxis value 10 is not shown.

bildschirmfoto 2017-01-22 um 19 30 14

I tried a couple of other combinations but was not able to align the bars with my numbers 0...10 on the xAxis. Someone has any idea?

Most helpful comment

For anyone else struggling with this, I found that I couldn't use this combination of granularity and setLabelCount() or labels would be misaligned and/or left & right bars cut off.

However, this seems to work for me:

self.periodGraphView.xAxis.labelCount = barValues.count
self.periodGraphView.xAxis.spaceMin = 0.5
self.periodGraphView.xAxis.spaceMax = 0.5
self.periodGraphView.xAxis.granularityEnabled = true
self.periodGraphView.xAxis.granularity = 1

All 10 comments

Another trial. Looks close to my expected result, but the first an last bars are shown only half (see also isst #2105).

               barChart.xAxis.axisMinimum = 0.0
                barChart.xAxis.axisMaximum = 10.0
                barChart.xAxis.granularity = 1.0
                barChart.xAxis.granularityEnabled = true
                barChart.xAxis.labelCount = 11
                barChart.fitBars = true

bildschirmfoto 2017-01-22 um 19 55 57

The closest diagram layout compared to Charts 2.x gives me the following code. But how can I move the gridlines between the bars as it is done in Charts 2.x? I tried xAxis.centerAxisLabelsEnabled = true and fitBars = true. Both attributes corrupted my xAxis/bar layout.

                barChart.xAxis.granularity = 1.0
                barChart.xAxis.granularityEnabled = true
                barChart.xAxis.labelCount = 11

bildschirmfoto 2017-01-22 um 20 06 19

@SvenMuc I'm also facing the same issue with Combined Chart (Bar and Line chart)... First and Last bar width is just half.. if you found the solution please help me out

For anyone else struggling with this, I found that I couldn't use this combination of granularity and setLabelCount() or labels would be misaligned and/or left & right bars cut off.

However, this seems to work for me:

self.periodGraphView.xAxis.labelCount = barValues.count
self.periodGraphView.xAxis.spaceMin = 0.5
self.periodGraphView.xAxis.spaceMax = 0.5
self.periodGraphView.xAxis.granularityEnabled = true
self.periodGraphView.xAxis.granularity = 1

Hey Thanks this helped me to get x axis labels centre to the bar but how about last bar getting cut?
How you fixed it?

Hey Thanks this helped me to get x axis labels centre to the bar but how about last bar getting cut?
How you fixed it?

barChart.xAxis.axisMaximum = Double(values.count)

barChart.xAxis.centerAxisLabelsEnabled = false
this fixed in my case.
Do not set centerAxisLabelsEnabled true

refer charts demo. this has been long.. so should be invalid now.

chartView.xAxis.axisMinimum = -0.5 //to avoid issue of last bar getting half cut

this worked for me

Can Any One Give me the Whole Code of the Graph Actually I Applied These But Didn't Work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sjdevlin picture sjdevlin  路  3Comments

ahmedsafadii picture ahmedsafadii  路  3Comments

Bharati555 picture Bharati555  路  4Comments

deepumukundan picture deepumukundan  路  3Comments

brytnvmg picture brytnvmg  路  4Comments