Charts: [Swift 3] LineChartData without xVals on constructor

Created on 7 Nov 2016  ·  12Comments  ·  Source: danielgindi/Charts

I know that "All dataset constructors have changed - they do not take an array of x-indices anymore" but how do I put strings on my x axis now if LineChartData doesn't have a "xVals" parameters anymore???
Before was so easy to do that...

let weights: [Double] = self.getWeigths()
let weightDates: [String] = self.getWeightDates()

var yValues : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0 ..< dateLastWeights.count {
        let entry = ChartDataEntry(x: Double(i), y: weights[i])
        yValues.append(entry)
    }
let set: LineChartDataSet = LineChartDataSet(values: yValues, label: "First Set")
var dataSets : [LineChartDataSet] = [LineChartDataSet]()
dataSets.append(set)

let data: LineChartData = LineChartData(xVals: weightDates, dataSets: dataSets)
self.lineChart.data = data

Most helpful comment

I was also struggling with this, but finally figured it out. You actually have to set it on the BarChartView itself using an axis value formatter:

let labels = ["Value 1", "Value 2", "Value 3"]
barChartView.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in
    return labels[Int(index)]
})

It is definitely not as clean as the previous way of doing it.


EDIT by danielgindi

Since 3.0.1, you can also do:

barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: labels)

Also, you probably want to add:

barChartView.xAxis.granularity = 1.0

All 12 comments

I was also struggling with this, but finally figured it out. You actually have to set it on the BarChartView itself using an axis value formatter:

let labels = ["Value 1", "Value 2", "Value 3"]
barChartView.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in
    return labels[Int(index)]
})

It is definitely not as clean as the previous way of doing it.


EDIT by danielgindi

Since 3.0.1, you can also do:

barChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: labels)

Also, you probably want to add:

barChartView.xAxis.granularity = 1.0

@esalter-va Yeah! That really works!! But somehow the graphic is displaying more indexes than it should... It showing bizzare decimal numbers on "index" parameter like (3.5; 0; 0.69; 1.39; 2.09; 2.79; 3.5) and then it casts to Int (3; 1; 1; 2; 2; 3) but my yValues has only 4 items... Do you have this kind of problem?

Nevermid! This problem was because the granularity! I tried granularity = 1 and works!

@reicamargo Thanks for actually posting your solution, because I was having the exact same issue. All good now though!

barChart.xAxis.granularity = 1

Hey guys. You can actually use the new IndexAxisValueFormatter, which tests for bounds etc, and simulated the old index behavior.

So chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["first index", "second index", "third index"]), combined with granularity = 1 will do just fine :-)

If you haven't set granularity - this will still behave correctly and not show labels for fractioned values. So this means that labels may be hidden when whole values are not rendered;

How come when I do the whole xAxis thing, I get fatal error: PieChart has no XAxis: file?

@danielgindi there is someone saying IndexAxisValueFormatter has issues while zooming https://github.com/danielgindi/Charts/issues/1909.

@danielgindi we should do that for every set? or it should be after we create the dataSet with all the sets?.

Thanks

I am sorry to post on closed issue. However, I have same problem, this solution is not working for me.
my issue

I apologize for posting on a closed issue as well, but I have been reading up on this for the last 7 hours and am lost. Let's say you want to have an x-axis label for every day of the week. If you use the solution posted by esalter-va, the labels seem to be dependent on the backing data. That is, if you only have data for Monday, the index referenced by the value formatter will only ever be equal to the Int representation for Monday, and no other day. Thus, you will only get one label (Monday). If you want a label for each day no matter if you have data for that day, how do you do it?

@bsachetta. You are not alone. I’ve been struggling with this problem for weeks. As far as I can tell, there is no support for a null data point. You must have a value for every sequential xAxis or you’ll have to figure out how to skip over them yourself. I was able to figure out how to do this and it kind of works but it falls apart when you don’t have enough data points to fill up the viewport.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Shunshine07 picture Shunshine07  ·  3Comments

cilasgimenez picture cilasgimenez  ·  4Comments

ahmedsafadii picture ahmedsafadii  ·  3Comments

guoyutaog picture guoyutaog  ·  3Comments

BrandonShega picture BrandonShega  ·  4Comments