7

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.

thierryb
  • 3,660
  • 4
  • 42
  • 58

3 Answers3

16

You Can not fix BarSize directly in ios-charts but you can change the default ratio width of the bar.

By default barWidth ratio is 0.85 so based on this it will cover 85% area of the chart if you have only 1 Bar on the chart and if you have 2 it will calculate the ratio based on Bar count so you can set approximate bar width with this property.

Default value:

/// **default**: 0.85
    open var barWidth = Double(0.85)

You can set:

let chartData = BarChartData(dataSet: chartDataSet)

chartData.barWidth = Double(0.01)

chartData.barWidth = Double(0.10)

chartData.barWidth = Double(0.25)

chartData.barWidth = Double(0.30)

chartData.barWidth = Double(0.50)

like that you will achieve your fix width between 0.01 to 1.00.

Hope this will helps!

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
1

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)

Dharay
  • 195
  • 1
  • 7
0

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}
Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47
路贵斌
  • 1
  • 1