Error With Daniel Gindi Charts Happening Randomly

So I just added charts to my app and I am having some problems. My app is randomly crashing and I cannot repro the issue no matter what I do. I have tried everything from trying this on a real device to logging in as the users who Crashlytics initially says were affected by the error to attempting to create new users and sign them in. Nothing I do causes the app to crash. I know this is a lot to look through but any advice would be very greatly appreciated. Thank you in advance for any help.

This is the code:

struct MultiLineChartView : UIViewRepresentable {
  var entries1 : [ChartDataEntry]
  var entries2 : [ChartDataEntry]
  var months: [String]
   
  func makeUIView(context: Context) -> LineChartView {
    let chart = LineChartView()
     
    return createChart(chart: chart)
  }
   
  func updateUIView(_ uiView: LineChartView, context: Context) {
    uiView.data = addData()
  }
   
  func createChart(chart: LineChartView) -> LineChartView{
    let numberFormatter = NumberFormatter()
    numberFormatter.generatesDecimalNumbers = false
    chart.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: numberFormatter)
    chart.chartDescription?.enabled = false
    chart.xAxis.drawGridLinesEnabled = false
    chart.xAxis.drawLabelsEnabled = true
    chart.xAxis.drawAxisLineEnabled = false
    chart.xAxis.labelPosition = .bottom
    chart.rightAxis.enabled = false
    chart.leftAxis.enabled = false
    chart.drawBordersEnabled = false
    chart.xAxis.forceLabelsEnabled = false
    chart.xAxis.granularityEnabled = true
    chart.xAxis.granularity = 1
    chart.xAxis.valueFormatter = CustomChartFormatter(days: months, entries1: entries1)
    chart.doubleTapToZoomEnabled = false
    chart.dragEnabled = true
    chart.noDataTextAlignment = .center
    chart.noDataText = "Not enough data. As soon as we have enough your history will display."
    chart.legend.enabled = true
    chart.extraBottomOffset = 25.0
     
    let legend = chart.legend
    legend.form = .circle
    legend.xOffset = 0.0

    // increase spacing between legends
    legend.xEntrySpace = 20.0
    legend.orientation = .horizontal

    legend.yOffset = 0.0
    legend.formSize = 10.0
     
    chart.data = addData()

    return chart
  }
   
  func addData() -> LineChartData {
    let data = LineChartData(dataSets: [
      generateLineChartDataSet(dataSetEntries: entries2, color: UIColor(Color( colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1))), fillColor: UIColor(Color( colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))), type: "future"),
      generateLineChartDataSet(dataSetEntries: entries1, color: UIColor(Color( colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1))), fillColor: UIColor(Color( colorLiteral(red: 0.3998093605, green: 0.738163054, blue: 0, alpha: 1))))

    ])

    return data
  }
     
  func generateLineChartDataSet(dataSetEntries: [ChartDataEntry], color: UIColor, fillColor: UIColor, type: String = "basic") -> LineChartDataSet{
    let dataSet = LineChartDataSet(entries: dataSetEntries, label: "")
    dataSet.colors = [color]
    dataSet.mode = .cubicBezier
    dataSet.circleRadius = 5
    dataSet.circleHoleColor = color
    dataSet.setCircleColor(UIColor.clear)
    dataSet.fill = Fill.fillWithColor(fillColor)
    dataSet.drawFilledEnabled = true
    dataSet.lineWidth = 1
    dataSet.valueTextColor = color
    dataSet.valueFont = UIFont(name: "Avenir", size: 13)!
    dataSet.valueFormatter = DigitValueFormatter()
     
    if type == "future" {
      dataSet.lineDashLengths = [3]
      dataSet.label = "Prediction"
    } else {
      dataSet.label = "Score History"
    }
       
    return dataSet
  }
   
}

class CustomChartFormatter: NSObject, IAxisValueFormatter {
  var days: [String]
  var entries1 : [ChartDataEntry]

  init(days: [String], entries1 : [ChartDataEntry]) {
    self.days = days
    self.entries1 = entries1
  }
   
  public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    let takeAway: Double = (entries1.count == 1 ? 0 : 1)

    return days[Int(value - takeAway)]
  }
}

class DigitValueFormatter : NSObject, IValueFormatter {
  func stringForValue(_ value: Double,
            entry: ChartDataEntry,
            dataSetIndex: Int,
            viewPortHandler: ViewPortHandler?) -> String {
    let valueWithoutDecimalPart = String(format: "%.0f", value)
    return "\(valueWithoutDecimalPart)"
  }
}

And I have attached the crash report.

Do you have a Apple crash report for this? If so, please post it here. See Posting a Crash Report for advice on how to do that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Error With Daniel Gindi Charts Happening Randomly
 
 
Q