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

Fixes a crash caused by NaNs in CombinedChartView with empty data set #577

Closed
wants to merge 1 commit into from
Closed

Conversation

natethegreat44
Copy link

Fixes a crash caused by NaNs in CombinedChartView when an empty dataset is passed in.

Also fixes a simlar NaN-related crash when such a chart is touched.

A minimal view controller to reproduce is:

import UIKit
import Charts

class InformationViewController: UIViewController {
    @IBOutlet weak var chartView: CombinedChartView!

    override func viewDidLoad() {
        let xAxisData = [NSDate]()
        let bar1Data = BarChartDataSet(yVals: [BarChartDataEntry]())
        let bar2Data = BarChartDataSet(yVals: [BarChartDataEntry]())

        chartView.xAxis.labelPosition = .Bottom
        chartView.descriptionText = ""
        chartView.animate(xAxisDuration: 0.5, yAxisDuration: 1.0)

        let combinedData = CombinedChartData(xVals: xAxisData)
        combinedData.barData = BarChartData(xVals: xAxisData, dataSets: [bar1Data, bar2Data])

        chartView.data = combinedData
    }
}

…et is passed in.

Also fixes a simlar NaN-related crash when such a chart is touched.

A minimal view controller to reproduce is:

import UIKit
import Charts

class InformationViewController: UIViewController {
    @IBOutlet weak var chartView: CombinedChartView!

    override func viewDidLoad() {
        let xAxisData = [NSDate]()
        let bar1Data = BarChartDataSet(yVals: [BarChartDataEntry]())
        let bar2Data = BarChartDataSet(yVals: [BarChartDataEntry]())

        chartView.xAxis.labelPosition = .Bottom
        chartView.descriptionText = ""
        chartView.animate(xAxisDuration: 0.5, yAxisDuration: 1.0)

        let combinedData = CombinedChartData(xVals: xAxisData)
        combinedData.barData = BarChartData(xVals: xAxisData, dataSets: [bar1Data, bar2Data])

        chartView.data = combinedData
    }
}
@natethegreat44 natethegreat44 changed the title Fixes a crash caused by NaNs in CombinedChartView when an empty datas… Fixes a crash caused by NaNs in CombinedChartView with empty data set Nov 27, 2015
@liuxuan30
Copy link
Member

hold on, this seems a regression.
Missing data entries should be taken care of before we start rendering. There is a check when setting data:

        set
        {
            if newValue == nil
            {
                print("Charts: data argument is nil on setData()", terminator: "\n")
                return
            }

            _dataNotSet = false
            _offsetsCalculated = false
            _data = newValue

            // calculate how many digits are needed
            calculateFormatter(min: _data.getYMin(), max: _data.getYMax())

            notifyDataSetChanged()
        }

in drawRect, if we detect dataNotSet is true, we won't continue the drawing at all.

However, I checked the commits, @danielgindi delete one condition:

if (newValue == nil || newValue?.yValCount == 0)

for 062a61b Reduce to nil check only (Fixes #417)

@danielgindi, seems we cannot delete newValue?.yValCount == 0? Or we have other code to defend it?

BTW, if it's nan, returning 0 is not a good choice here I think.

@danielgindi
Copy link
Collaborator

Maybe keep the older condition - but only print if the newValue is nil? :-)
I think this would satisfy both issues...

liuxuan30 added a commit to liuxuan30/Charts that referenced this pull request Nov 30, 2015
…artsOrg#577

We need to check if yValCount is 0, to prevent drawing

This reverts commit 062a61b.
@liuxuan30
Copy link
Member

I will close this PR and reverse 062a61b in PR #582
@natethegreat44 thanks for pointing out this issue!

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

Successfully merging this pull request may close these issues.

3 participants