Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Problem with charts in UITableView cells #2888

Description

@hfabisiak

Hi! I am trying to show live data from accelerometer and gyroscope, but I get an error:

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

Also the preformance of tableview is very low. After a few seconds it lags a lot. I would like to scroll to the newest values from accelerometer and gyroscope.

`func startAccelerometer() {
let indexPathForAccCell = IndexPath(row: 0, section: 0)
let accCell = testView.tableView.cellForRow(at: indexPathForAccCell) as! ChartCell
let setX = LineChartDataSet(values: ChartDataEntry, label: "X")
let setY = LineChartDataSet(values: ChartDataEntry, label: "Y")
let setZ = LineChartDataSet(values: ChartDataEntry, label: "Z")

    setX.setColor(UIColor.blue)
    setY.setColor(UIColor.green)
    setZ.setColor(UIColor.red)

    setX.drawCirclesEnabled = false
    setY.drawCirclesEnabled = false
    setZ.drawCirclesEnabled = false
    
    setX.lineWidth = 1.0
    setY.lineWidth = 1.0
    setZ.lineWidth = 1.0
    
    accCell.chartView.data = LineChartData(dataSets: [setX,setY,setZ])
    accCell.chartView.setVisibleYRange(minYRange: -0.9, maxYRange: 0.9, axis: .left)
    accCell.chartView.setVisibleXRange(minXRange: Double(1), maxXRange: Double(1000))
    var i = 0.0
    
    if motion.isAccelerometerAvailable {
        let interval = 1.0 / 60.0 
        motion.accelerometerUpdateInterval = interval
        self.gatheringData = true
        
        var lowPassX = LowPassFilter(value: 0.0, filterFactor: 0.85)
        var lowPassY = LowPassFilter(value: 0.0, filterFactor: 0.85)
        var lowPassZ = LowPassFilter(value: 0.0, filterFactor: 0.85)

        motion.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in
            //getting accelerometer data
            if let data = self.motion.accelerometerData {
                let x = data.acceleration.x
                let y = data.acceleration.y
                let z = data.acceleration.z
                
                lowPassX.update(newValue: x)
                lowPassY.update(newValue: y)
                lowPassZ.update(newValue: z)
                
                accCell.chartView.data?.addEntry(ChartDataEntry(x: i, y: lowPassX.value), dataSetIndex: 0)
                accCell.chartView.data?.addEntry(ChartDataEntry(x: i, y: lowPassY.value), dataSetIndex: 1)
                accCell.chartView.data?.addEntry(ChartDataEntry(x: i, y: lowPassZ.value), dataSetIndex: 2)
                accCell.chartView.notifyDataSetChanged()
                accCell.chartView.moveViewToX(Double(CGFloat.greatestFiniteMagnitude))
                i += 1
            }

        })
    }
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions