iOS Chart: How to set bar width to a fixed size?

Viewed 8174

I would like bar width equal to 30 pixels.

barData.barWidth let me change it but this is proportional to chart width and number of bars to display, which display a big bar on iPad for only one element.

Do you have an idea?

Thanks.

3 Answers

Assuming chart width is fixed:

step 1: find the ratio for required width in scenario where there is only one element on x-axis (for me it was 0.05)

step 2: set the bar width to this ratio multiplied by element count on x-axis

data.barWidth = 0.05 * Double(xArray.count)

This will work because max width for a single bar is inversely proportional to number of elements on x-axis

For variable chart width you will need an additional factor for bar width correction: (original chart width/current chart width)

You can add a chartView to the scrollView, then set the chartView.widthand scrollView.contentsize

width = values.count * fixedWidth 
chartView.width = width

and

scrollView.contentSize = {width,scrollView.height}
Related