Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift 3] LineChartData without xVals on constructor #1800

Closed
reicamargo opened this issue Nov 7, 2016 · 12 comments
Closed

[Swift 3] LineChartData without xVals on constructor #1800

reicamargo opened this issue Nov 7, 2016 · 12 comments

Comments

@reicamargo
Copy link

reicamargo commented Nov 7, 2016

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
@reicamargo reicamargo changed the title LineChartData without xVals on constructor LineChartData without xVals on constructor on Swift 3 Nov 7, 2016
@reicamargo reicamargo changed the title LineChartData without xVals on constructor on Swift 3 [Swift 3] LineChartData without xVals on constructor Nov 7, 2016
@evansalter
Copy link

evansalter commented Nov 8, 2016

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

@reicamargo
Copy link
Author

reicamargo commented Nov 8, 2016

@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?

@reicamargo
Copy link
Author

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

@evansalter
Copy link

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

@supratik94
Copy link

supratik94 commented Nov 21, 2016

barChart.xAxis.granularity = 1

@danielgindi
Copy link
Collaborator

danielgindi commented Nov 21, 2016

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;

@aaronwins88
Copy link

aaronwins88 commented Dec 16, 2016

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

@liuxuan30
Copy link
Member

@danielgindi there is someone saying IndexAxisValueFormatter has issues while zooming #1909.

@vicchirino
Copy link

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

Thanks

@Schabaani
Copy link

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

@bsachetta
Copy link

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?

@JCMcLovin
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants