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

fatal error: Double value cannot be converted to Int because it is either infinite or NaN #1550

Closed
digidhamu opened this issue Sep 24, 2016 · 11 comments · Fixed by #1558
Closed
Assignees
Labels

Comments

@digidhamu
Copy link

When I use LineChart APIs, I am getting fatal error at ChartUtils.swift @ line 53.

fatal error: Double value cannot be converted to Int because it is either infinite or NaN

    internal class func decimals(_ number: Double) -> Int
    {
        if number.isNaN || number.isInfinite || number == 0.0
        {
            return 0
        }

        let i = roundToNextSignificant(number: Double(number))
        **return Int(ceil(-log10(i))) + 2**
    }
@digidhamu
Copy link
Author

I am now checking array elements count before passing to Charts API as workaround.

@pmairoldi
Copy link
Collaborator

What double value are you passing? I'll write some test cases to try to fix this.

@digidhamu
Copy link
Author

@petester42 I was passing array of Double but array is empty.

@pmairoldi
Copy link
Collaborator

so let x:[Double] = [] or an array of empty values?

@digidhamu
Copy link
Author

let x: [Double] = [] is passed without appending.

@pmairoldi
Copy link
Collaborator

Then my assumption would be that the function would never be called. Can you provide the snippit you use to create this chart so I can reproduce it in my environment?

@digidhamu
Copy link
Author

digidhamu commented Sep 25, 2016

Previous: this produces fatal error when array is empty
setChart(lineChartSpeed, dataPoints: counterDrive, values: speedDrive)

Current one with condition to avoid this fatal error

if counterDrive.count > 0 {
     setChart(lineChartSpeed, dataPoints: counterDrive, values: speedDrive)
}

Function to set the graph datasets

fileprivate func setChart(_ lineChartView: LineChartView, dataPoints: [Double], values: [Double]) {

    var dataEntries: [ChartDataEntry] = []

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

    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Speed")
    lineChartDataSet.setColor(UIColor.blue)
//    lineChartDataSet.drawCubicEnabled = true
    lineChartDataSet.mode = .cubicBezier
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.lineWidth = 1.0
    lineChartDataSet.circleRadius = 5.0
    lineChartDataSet.highlightColor = UIColor.red
    lineChartDataSet.drawHorizontalHighlightIndicatorEnabled = true

    var dataSets = [IChartDataSet]()
    dataSets.append(lineChartDataSet)

    let lineChartData = LineChartData(dataSets: dataSets)

    lineChartView.data = lineChartData
  }

@pmairoldi
Copy link
Collaborator

Yup confirmed bug. Should be a simple fix.

@pmairoldi pmairoldi added the bug label Sep 25, 2016
@pmairoldi pmairoldi self-assigned this Sep 25, 2016
@digidhamu
Copy link
Author

@petester42 Thanks for this update and will wait for the fix.

@pmairoldi
Copy link
Collaborator

fixed by #1558

@josman185
Copy link

josman185 commented Jul 13, 2017

I have found the solution to this. Just be careful with the implementation of this pod, specifically in this method:

func setChart(dataPoints: [String], values: [Double]) {
...
...
let chartDataSet = LineChartDataSet(yVals: dataEntries, label: "")
data = LineChartData(xVals: dataPoints, dataSet: chartDataSet)
}

Here the new implementation:
Make sure you are passing the "chartDataSet" instead of the "dataPoints".

func setChart(_ dataPoints: [String], values: [Double]) {
...
...
let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
data = LineChartData(dataSet: chartDataSet)
}

I was making a big mistake passing the "dataPoints" instead of the "chartDataSet" values. Obviously Xcode doesn´t show any error until you run your code.
Hope this helps.

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

Successfully merging a pull request may close this issue.

3 participants