iOS charts will not draw multiple datasets when the sets use different ranges of X-coordinates
When creating a line chart from more than one data set, the line chart only shows one of the data sets and when zooming or panning the chart it crashes with Fatal error: Can't form Range with upperBound < lowerBound.
If I create the line chart from one data set it works as expected.
This problem only occurs when the two datasets have completely different ranges of X values.
The code below should draw a chart with x ranging from 0 to 19. But it only draws the second dataset. The chart crashes if you pan or zoom it.
If I edit the code, replacing for x in (10..<20) with for x in (0..<10), both datasets are correctly drawn and the chart does not crash.
To summarise: when adding two dataSets that have entries with different ranges of X coordinates the chart draws incorrectly and will crash.
Is there an iOS_charts API call needed to prevent this? How can I draw two datasets that do not have overlapping X-coordinates?
I've been able to produce the same crash when running code using this demo code if I modify it to create multiple datasets that have non-overlapping x-coordinates.
class ElevationChartViewController: UIViewController {
@IBOutlet var chartView: LineChartView!
override func viewDidLoad() {
super.viewDidLoad()
chartView.backgroundColor = .white
chartView.legend.enabled = false
chartView.maxVisibleCount = 20000
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let dataSets = createChartDataSets()
chartView.data = LineChartData(dataSets: dataSets)
}
}
func createChartDataSets() -> [LineChartDataSet] {
var dataSets = [LineChartDataSet]()
var entriesOne = [ChartDataEntry]()
var entriesTwo = [ChartDataEntry]()
var y = 0.0
for x in (0..<10) {
entriesOne.append( ChartDataEntry(x: Double(x), y: y))
y = y + 10
if y > 60 {y = 0.0}
}
dataSets.append(LineChartDataSet(entriesOne))
for x in (10..<20) {
entriesTwo.append( ChartDataEntry(x: Double(x), y: y))
y = y + 10
if y > 50 {y = 0.0}
}
dataSets.append(LineChartDataSet(entriesTwo))
return dataSets
}
Swift version: 5.4 Xcode 12.4 Observed running on a real iPhone 12 sw version 14.4 Charts v4.0.1
Oops. I actually meant to submit this question to StackOverflow instead of here. I'll leave it here for now. I'll put it on StackOverflow and close it here if I find an answer. I'm a long-time user of your Android library. Thanks for your efforts!
Same problem is observed with code with commit e91ba716190836b013ef8a9ca53e220ad5051e21. For various scenarios, unless all data sets have the same range of X-coordinates I see the same problem.
Please follow this stackoverflow link and look at the accepted answer. This answer solves the issue for me and another man and may be the correct solution for the iOS chart library or just a hack. Please comment here as to your evaluation of this workaround. Is it a good solution?
I was having the same problem and the fix in the stackoverflow fixed the issue for me as well. It would be great to get an official fix and release. Thanks for the library.
That wasn't an issue in 3.6.0, what changed?
Please follow this stackoverflow link and look at the accepted answer. This answer solves the issue for me and another man and may be the correct solution for the iOS chart library or just a hack. Please comment here as to your evaluation of this workaround. Is it a good solution?
Thanks a lot mate! You saved my day
I can confirm that this issue is happening in our project as well: Xcode 12.5 Charts 4.0.1
My app is also afflicted by this issue. The work-around on Stack Overflow partially resolves it, but not completely. Any chance of a proper fix?