Ranjit
Ranjit

Reputation: 4646

issues with iOS Chart library

I am using iOSCharts for swift by @danielgindi https://github.com/danielgindi/Charts

So here is my data set

let dateArray = [26,27,28,29,30,01,02]
        let values = [11.0,5.0,0.0,0.0,4.0,1.0,0.0]

        var dataEntries: [BarChartDataEntry] = []

        for i in 0..<dateArray.count {
            let dataEntry = BarChartDataEntry(x: Double(i), y: values[i])
            dataEntries.append(dataEntry)
        }

        let data = BarChartData()
        let ds1 = BarChartDataSet(values: dataEntries, label: "Hello")
        ds1.colors = [NSUIColor.red]
        data.addDataSet(ds1)




        self.barChartView.data = data

         self.barChartView.fitBars = true

        self.barChartView.gridBackgroundColor = NSUIColor.white

        self.barChartView.chartDescription?.text = ""

what I am expecting is this enter image description here

but what I get is this

enter image description here

So as you can see the problems are below

1) Graph starting from one grid above, you see space at the bottom 2) xaxis labels are different, I am not sure how to set them. 3) I want to show x axis and y axis labels as int and not double.

Upvotes: 0

Views: 1445

Answers (3)

Wingzero
Wingzero

Reputation: 9754

try leftAxis.axisMinValue = 0.0

Upvotes: 0

Ryan
Ryan

Reputation: 636

Check out the master version of the BarchartViewController in the ChartsDemo project, it implements both of your questions:

1) Left Axis alignment, see:

https://github.com/danielgindi/Charts/blob/master/ChartsDemo/Classes/Demos/BarChartViewController.m#L76

Specifically, you want to set the axisMinimum on the left Axis, for your example it'd be:

self.barChartView.leftAxis.axisMinimum = 0.0

2) X Axis values formatting.

This one is a little trickier. You could borrow the DayAxisValueFormatter from ChartsDemo, but that's Objective-C code, so you need to be familiar with adding Objective-C to your project.

You could also get exactly what you asked for by making your view controller a IAxisValueFormatter, and implementing like this:

class MyViewController: UIViewController, IAxisValueFormatter {

    var dateArray: [Int] = [26,27,28,29,30,01,02] //member variable

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return dateArray[i]
    }

    //edit:  forgot to say this earlier, you need to do this somewhere in your bar graph setup code
    self.barChwrtView.xAxis.formatter = self
}

It's up to you to keep dateArray and values in synch whenever you update your chart.

Upvotes: 1

Alexander
Alexander

Reputation: 63395

Your second problem, the X label issue, is caused on this line:

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

The x value is being set just to i (as a Double), when I suspect you meant to set it to Double(dateArray[i]), like this:

let dateArray = [26,27,28,29,30,01,02] let values = [11.0,5.0,0.0,0.0,4.0,1.0,0.0]

var dataEntries: [BarChartDataEntry] = []

for i in 0..<dateArray.count {
    let dataEntry = BarChartDataEntry(x: Double(dateArray[i]), y: values[i])
    dataEntries.append(dataEntry)
}

This code can be shorted with the use of zip and map:

let dataEntries = zip(dateArray, values).map(BarChartDataEntry.init(x:y:))

This code will pair up every date of dateArray with a value from value. Then, these pairs are transformed, by being passed into the BarChartDataEntry.init(x:y:) initializer. This technique is nice because it removes the need for dataEntries to be mutable.

Upvotes: 0

Related Questions