2017-08-04 71 views
0

向swift中添加數據如果沒有可用數據,我使用圖表庫創建幾個線圖,我希望圖表清除。圖表清除細小,並顯示其一貫的「nodatatext」但是當我嘗試設置圖表再次失敗代碼上設置數據線:fastestChartView.data = chartData3如何清除圖表,然後在swift中使用chartView.clear()

func setChart3(dataEntryX forX:[String],dataEntryY forY: [Double]) { 
    fastestChartView.noDataText = "You need to provide data for the chart." 
    var dataEntries:[ChartDataEntry] = [] 
    for i in 0..<forX.count{ 

     let dataEntry = ChartDataEntry(x: Double(i), y: Double(forY[i]) , data: chugIndexArr3 as AnyObject?) 
     dataEntries.append(dataEntry) 
    } 

    let chartDataSet = LineChartDataSet(values: dataEntries, label: "Fastest Chugs") 
    chartDataSet.setColor(UIColor.green, alpha: 0.6) 
    chartDataSet.circleColors = self.colors3 
    chartDataSet.circleHoleColor = UIColor(red: 255/255, green: 223/255, blue: 24/255, alpha: 1.0) 

    chartData3 = LineChartData(dataSet: chartDataSet) 
    chartData3.setDrawValues(true) 

    fastestChartView.data = chartData3 
    fastestChartView.legend.enabled = false 
    fastestChartView.xAxis.labelPosition = .bottom 
    fastestChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: chugIndexArr3) 
    fastestChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0) 
    fastestChartView.drawBordersEnabled = true 
    fastestChartView.data?.setValueFormatter(valueFormatter) 

    if chugIndexArr3.count > 25 { 
     fastestChartView.setVisibleXRange(minXRange: 10, maxXRange: 25) 
     fastestChartView.moveViewToAnimated(xValue: Double(self.chugIndexArr3.count - 25), yValue: 0, axis: .left, duration: 2) 
    } else if chugIndexArr3.count == 0 { 
     fastestChart.clear() 
    } 
    else { 
     fastestChartView.resetZoom() 
    } 

} 

我知道我的程序工作,因爲我使用相同的函數來初始顯示圖形,就像我嘗試重新加載數據時一樣。但由於某種原因,當我嘗試使用fasterChartView.clear(),然後嘗試使用與viewdidload()中相同的方法設置數據時,它會崩潰。這裏是在viewdidload()和導致崩潰的按鈕動作函數中的代碼:

self.ref.child("users").child(uid!).child("chugStats").child("4avgs").observeSingleEvent(of: .value, with: { (snapshot) in 

     if let dict = snapshot.value as? NSDictionary { 

      for index in dict.allKeys { 


       let currentKey = String(describing: index) 

       if currentKey != "avg" && currentKey != "fastest" { 

        let currentValue = dict[currentKey] as? String ?? "" 

        let messageArr = currentValue.components(separatedBy: ":") 

        self.chugIndexArr2.append(currentKey) 
        self.chugTimeArr2.append(Double(messageArr[0])!) 
        self.intervals2.append(Double(messageArr[1])!) 

        if let myDouble = NumberFormatter().number(from: messageArr[1])?.doubleValue { 
         let date = NSDate(timeIntervalSince1970: myDouble) 
         let dateFormatter = DateFormatter() 
         dateFormatter.timeStyle = .short 
         let dateStr = dateFormatter.string(from: date as Date) 
         self.chugInterval2.append(dateStr) 
        } 

        //Color array setup 
        if messageArr.count == 2 { 
         self.colors2.append(UIColor(red: 100/255, green: 200/255, blue: 110/255, alpha: 0.9)) 
        } else if messageArr.count == 3 { 
         if messageArr[2] == "fun" { 
          self.colors2.append(UIColor(red: 100/255, green: 200/255, blue: 110/255, alpha: 0.9)) 
         } else if messageArr[2] == "ranked" { 
          self.colors2.append(UIColor.red) 
         } else if messageArr[2] == "challenge" { 
          self.colors2.append(UIColor.blue) 
         } 
        } 

       } 
      } 
      let sorted1 = zip(self.chugTimeArr2, self.intervals2).sorted { $0.1 < $1.1 } 
      self.chugTimeArr2 = sorted1.map { $0.0 } 

      let sorted2 = zip(self.chugInterval2, self.intervals2).sorted { $0.1 < $1.1 } 
      self.chugInterval2 = sorted2.map { $0.0 } 

      let sorted3 = zip(self.chugIndexArr2, self.intervals2).sorted { $0.1 < $1.1 } 
      self.chugIndexArr2 = sorted3.map { $0.0 } 

      let sorted4 = zip(self.colors2, self.intervals2).sorted { $0.1 < $1.1 } 
      self.colors2 = sorted4.map { $0.0 } 

     } 
     self.setChart2(dataEntryX: self.chugIndexArr2, dataEntryY: self.chugTimeArr2) 

    }) 

請幫我這個。我找不到解決辦法,也找不到在互聯網上遇到同樣問題的人。

+0

錯誤說的是什麼? – 3stud1ant3

+0

Double值無法轉換爲Int,因爲它無論是無限還是NaN – lawndogg

回答

0

我遇到了同樣的問題。清除圖表似乎有些問題,並且可能不是所有變量都返回到默認值。

我會假設你正在使用圖表v3.0.3(目前使用3.0.2我自己)。我使用的修復應該足夠好。

轉到AxisRendererBase.swift,如果使用Pods解鎖文件,並將第106行(if labelCount == 0 || range <= 0 || range.isInfinite)更改爲:if labelCount == 0 || range <= 0 || range.isInfinite || range.isNaN

編輯: 創建一個PR

相關問題