2

I am using the Charts framework found here.

My graphs look like this:

enter image description here

My left graph has a bunch of values under my first bar and my second graph is missing a value under my second bar. My function to set up the graph is as follows:

func setChart(chartview: BarChartView, xarray: [String], yarray: [Double]){

        var dataEntries: [BarChartDataEntry] = []


        for i in 0..<xarray.count {

            let dataEntry = BarChartDataEntry(x: Double(i), y: Double(yarray[i]))

            dataEntries.append(dataEntry)
        }

        let chartDataSet = BarChartDataSet(values: dataEntries, label: "")

        let chartData = BarChartData(dataSet: chartDataSet)

        chartview.xAxis.valueFormatter = IndexAxisValueFormatter(values:xarray)

        chartview.xAxis.labelPosition = .bottom

        chartview.chartDescription?.enabled = false

        chartview.rightAxis.enabled = false

        chartview.legend.enabled = false

        chartview.data = chartData

    }

I use the same functions to create other graphs and they show up fine but for some reason sometimes they show like this.

Update:

Below are both arrays during runtime for each graph:

Graph 1:

["iOS 10", "iOS 9"]
[67.0, 19.0]

Graph 2:

["2.1.0.6", "2.1.0.5", "2.0.32", "2.0.30", "2.0.23", "1.3.2.8"]
[67.0, 7.0, 4.0, 5.0, 2.0, 1.0]
5
  • Can you please post some sample arrays data for the second graph? Commented Aug 28, 2017 at 5:55
  • I have provided the print out of both xarray and yarray at runtime for both graphs above. Commented Aug 28, 2017 at 14:57
  • There is no problem with your code. May be sometimes you are getting wrong data? Like ["2.1.0.6", "", "2.0.32", "2.0.30", "2.0.23", "1.3.2.8"] Commented Aug 28, 2017 at 20:18
  • chartview.xAxis.valueFormatter = IndexAxisValueFormatter(values:xarray) is what is setting the values so I printed out xarray and the array is perfectly fine. It only shows "iOS 10, iOS 9" but it prints out iOS 10 three times. Commented Aug 28, 2017 at 20:26
  • Added the answer. Please check. Commented Aug 28, 2017 at 20:37

1 Answer 1

5

Please try this :

barChartView.xAxis.granularityEnabled = true
Sign up to request clarification or add additional context in comments.

3 Comments

I think that fixed it. What does that do?
/// When true, axis labels are controlled by the granularity property. /// When false, axis values could possibly be repeated. /// This could happen if two adjacent axis values are rounded to same value. /// If using granularity this could be avoided by having fewer axis values visible. Default value for granularityEnabled is false
Would you happen to know how to solve the issue here: github.com/danielgindi/Charts/issues/2751

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.