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

range, and therefore interval, is NaN in computeAxisValues #2845

Closed
pingd opened this issue Oct 3, 2017 · 3 comments
Closed

range, and therefore interval, is NaN in computeAxisValues #2845

pingd opened this issue Oct 3, 2017 · 3 comments

Comments

@pingd
Copy link

pingd commented Oct 3, 2017

For some reason min and max in computeAxis are NaN, which causes a problem with calculating range and interval in computeAxisValues (as referenced in my PR).

Here is a piece of code that breaks every time (after calling reloadDataTap).

class ViewController: UIViewController {

    @IBOutlet weak var chartView: CandleStickChartView!

    private let entries = [CandleChartDataEntry(x: 0, shadowH: 1.001, shadowL: 0.990, open: 0.999, close: 0.999), CandleChartDataEntry(x: 1, shadowH: 1.01, shadowL: 0.999, open: 0.997, close: 0.998), CandleChartDataEntry(x: 2, shadowH: 1.001, shadowL: 0.992, open: 1.002, close: 0.0989), CandleChartDataEntry(x: 3, shadowH: 1.0031, shadowL: 0.995, open: 1, close: 1.001), CandleChartDataEntry(x: 4, shadowH: 1.007, shadowL: 0.999, open: 0.997, close: 0.997)]
    private let entries2 = [CandleChartDataEntry(x: 5, shadowH: 1.001, shadowL: 0.990, open: 0.999, close: 0.999), CandleChartDataEntry(x: 6, shadowH: 1.001, shadowL: 0.990, open: 0.999, close: 0.999), CandleChartDataEntry(x: 7, shadowH: 1.001, shadowL: 0.990, open: 0.999, close: 0.999), CandleChartDataEntry(x: 8, shadowH: 1.001, shadowL: 0.990, open: 0.999, close: 0.999), CandleChartDataEntry(x: 94, shadowH: 1.001, shadowL: 0.990, open: 0.999, close: 0.999)]

    override func viewDidLoad() {
        super.viewDidLoad()
    
        setupChart()
    
        addEntries(entries: entries)
        addEntries(entries: entries2)
    }

    private func setupChart() {
        chartView.highlightPerDragEnabled = false
        chartView.doubleTapToZoomEnabled = false
        chartView.dragDecelerationEnabled = false
        chartView.pinchZoomEnabled = false
        chartView.scaleYEnabled = false
        chartView.scaleXEnabled = true
    
        chartView.chartDescription?.text = nil
        chartView.legend.enabled = false
    
        chartView.xAxis.spaceMin = 3.0
        chartView.xAxis.spaceMax = 5.0
        chartView.xAxis.setLabelCount(4, force: false)
    
        chartView.rightAxis.setLabelCount(7, force: false)
    
        chartView.leftAxis.enabled = false
    
        chartView.minOffset = 0
        chartView.setExtraOffsets(left: 0, top: 5, right: 0, bottom: 25)
    }

    private func createSet() -> CandleChartDataSet {
        let candleChartDataSet = CandleChartDataSet(values: nil, label: nil)
        candleChartDataSet.axisDependency = .right
        candleChartDataSet.drawIconsEnabled = false
        candleChartDataSet.drawValuesEnabled = false
    
        return candleChartDataSet
    }

    private func addEntries(entries: [CandleChartDataEntry]) {
        if chartView.data == nil {
            chartView.data = CandleChartData()
        }
    
        let data = chartView.candleData!
        
        var set = data.getDataSetByIndex(0)
        if set == nil {
            set = createSet()
            data.addDataSet(set)
        }
        
        for entry in entries {
            data.addEntry(entry, dataSetIndex: 0)
        }
        
        data.notifyDataChanged()
        
        chartView.notifyDataSetChanged()
        
        chartView.autoScaleMinMaxEnabled = true
        chartView.setVisibleXRange(minXRange: Double(5), maxXRange: Double(15))

        chartView.setNeedsDisplay()
    }

    @IBAction func reloadDataTap(_ sender: UIButton) {
        chartView.clear()
        addEntries(entries: entries)
    }
}
@liuxuan30
Copy link
Member

liuxuan30 commented Oct 13, 2017

You don't debug and walk through ChartsDemo very well, it's wrong calling chartView.data = CandleChartData() without any valid data, leading _xAxis._axisMinimum to be +inf and causes the matrix became a mess in the first place.

Never init an empty chart data because a lot of calculation involved in data setter. Another thing is data.notifyDataChanged() is not needed, chartView.notifyDataSetChanged() will do the job

I am going to close your PR as well, since we don't want to silent the mistakes people made or a true bug.

@pingd
Copy link
Author

pingd commented Oct 13, 2017

If initializing the chart with empty data is leading to problems, then perhaps a better design would be to either prevent doing so, or check if the data is empty before doing any calculations. I use the same logic in an Android app using MPAndroidChart library and have not run into that issue there.

@liuxuan30
Copy link
Member

I admit. It's a bit tricky here. the data setter has been modified several times to avoid different issues.

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

No branches or pull requests

2 participants