How can I fit x-axis labels according with its bar?

夙愿已清 提交于 2019-12-12 05:49:56

问题


I am using ios-chart library and I have a CombinedBarChart in which I made zoom on it by default

combinedBarChart.setScaleMinima(2, scaleY: 1)
combinedBarChart.setScaleEnabled(false)

I would like that each x-axis label will stay above each bar but I cannot make it work properly.

Here is my example:

As you can see, x-axis labels does not correspond with their bars respectively.

How can I make that each x-axis label will be above of its bar?

Thanks in advance!


回答1:


I was not able to reproduce the problem by adding this to my combinedChart:

 combinedBarChart.setScaleMinima(2, scaleY: 1)
 combinedBarChart.setScaleEnabled(false)

But until you upload your code, I can suggest the following:

  1. Make sure

    combinedBarChart.xAxis.granularity = 1
    

2. Shift the graph a bit to the left.

    var shift = -0.2
    var entries: [BarChartDataEntry] = []
    for index in 0..<datapoints.count {
       let value1 = Double(datapoints[index])
       let value2 = Double(datapoints[index])
       entries.append(BarChartDataEntry(x: Double(index)+shift, yValues: [value1, value2]))
    }         
  1. By lowering the number of bars shown on each screen, you create more space between bars and that might solve the problem:

     combinedBarChart.setVisibleXRange(minXRange: 4.0, maxXRange: 4.0)
    
  2. You might also want to change all the month names to a 3 digit name, like Jan, Feb, Mar, ... to see if that makes the problem go away. If it does, and you don´t want to use 3 digit names, you can add white spaces at the end of the month names to make them all equally long. Like "January ", "February ", "March ", "August ", "September", "October ", "December ".



来源:https://stackoverflow.com/questions/44843745/how-can-i-fit-x-axis-labels-according-with-its-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!