4

Tried to implement scroll on Barchart using scale x to 2 for zoom in. But the issue is the x-axis values are not center-aligned with Bar chart. Labels count is based on day and month values.

barChartView.xAxis.axisMinimum = 0.0
barChartView.xAxis.axisMaximum = Double(labels.count - 1)

barChartView.setVisibleXRangeMaximum(Double(labels.count)/2)
or 
barChartView.zoom(scaleX: 2, scaleY: 0, x: 0, y: 0)

for scroll implemented like,

barChartView.xAxis.setLabelCount(Int(Double(labels.count)/2), force: true)

Please suggest to us the correct approach to avoid miss align of x-axis values with the bar chart.

enter image description here

enter image description here

2 Answers 2

2

From your code, I assume your expected behaviour is that the chart

  • shows at most half of all available bars
  • allows the user to zoom indefinitly (minimum = 0)
  • allows the user to scroll (left/right)

If this is the case, you would apply the following code:

barChartView.xAxis.axisMinimum = 0.0
barChartView.xAxis.axisMaximum = Double(labels.count - 1)

barChartView.setVisibleXRangeMaximum(Double(labels.count)/2)
barChartView.xAxis.setLabelCount(Int(Double(labels.count)/2), force: false)

.zoomshould be avoided since it sets multiple unwanted values (scaleY, x, y) and hence adds unwanted features when scrolling.

It is important to set the force flag to false. This is the parameter that caused your miss alignment as described in the documentation:

https://weeklycoding.com/mpandroidchart-documentation/axis-general/

setLabelCount(int count, boolean force): Sets the number of labels for the y-axis. Be aware that this number is not fixed (if force == false) and can only be approximated. If force is enabled (true), then the exact specified label-count is drawn – this can lead to uneven numbers on the axis.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. It works fine with set force to false. :)
0

To make the chart horizontal scroll and adjust the bars and y-axis labels properly:

let visibleXRange = 6 // Number of values to show in y-Axis
barChartView.xAxis.axisMaximum = Double(xAxisValues.count)
barChartView.setVisibleXRangeMaximum(Double(visibleXRange))
barChartView.xAxis.setLabelCount(visibleXRange, force: false)
barChartView.xAxis.granularity = 1
barChartView.xAxis.labelCount = visibleXRange

Make sure the dragEnabled = true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.