2017-06-11 6 views
0

チャートライブラリを使用しているときに、エントリの識別に問題があります。 私は、円グラフを作成するために、一連のカテゴリと金額の配列を持っています。私は、エントリをタップすると、私は、カテゴリの量と名前を表示したい:スウィフトチャート - 特定のエントリを一義的に特定します

var cats: [String]! = ["Transport","Shopping","Food","Accomodation","Fun","Other"] 
var expensesArray = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 

私はコアデータから値をフェッチ:

func requestExp() { 
     noExpView.isHidden = true 
     pieChartView.isHidden = false 

     total = 0 
     trans = 0.0 
     shop = 0.0 
     food = 0.0 
     acc = 0.0 
     fun = 0.0 
     other = 0.0 

     // Create Fetch Request 
     let fetchRequest: NSFetchRequest<Expense> = Expense.fetchRequest() 
     // Create Predicate 
     let predicate = NSPredicate(format: "%K >= %@ AND %K <= %@", argumentArray:["date", fromDate, "date", toDate]) 
     fetchRequest.predicate = predicate 
     // Execute Fetch Request 
     do { 
      let result = try context.fetch(fetchRequest) 

      for managedObject in result { 
       if let amount = managedObject.value(forKey: "amount"), let currency = managedObject.value(forKey: "currency") 
       { 
        switch managedObject.value(forKey: "Category") as! String { 
         case "Transportation": 
          trans+=amount as! Double 
         case "Shopping": 
          shop+=amount as! Double 
         case "Food": 
          food+=amount as! Double 
         case "Accomodation": 
          acc+=amount as! Double 
         case "Fun": 
          fun+=amount as! Double 
         case "Other": 
          other+=amount as! Double 
        default: 
          break 
        } 
        print("\(amount) \(currency)") 
        total+=amount as! Double 
       } 
      } 
      expensesArray = [trans, shop, food, acc, fun, other] 
      print("Total for "+dateFormatter.string(from: fromDate)+" to "+dateFormatter.string(from: toDate)+" = "+String(total)) 

      let final = formatter.string(from: total as NSNumber) 
      totalLabel.text = final 
      let dayAverage = total/calcDays() 
      average.text = formatter.string(from: dayAverage as NSNumber) 
      average.isHidden = false 

      if total==0 { 
       pieChartView.isHidden = true 
       noExpView.isHidden = false 
      } 

     } catch { 
      let fetchError = error as NSError 
      print(fetchError) 
     } 

    } 

func setChart(dataPoints: [Double]) { 


    var dataEntries: [ChartDataEntry] = [] 

    for i in 0..<dataPoints.count { 
     let dataEntry = PieChartDataEntry(value: Double(dataPoints[i]), label: cats[i]) 
     dataEntries.append(dataEntry) 
    } 

    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: nil) 
    pieChartDataSet.colors = colors 
    pieChartDataSet.drawValuesEnabled = false 
    let data = PieChartData(dataSet: pieChartDataSet) 
    pieChartView.data = data 
    pieChartView.noDataText = "No data available" 
    pieChartView.notifyDataSetChanged() 

} 

これはどのラベルを更新する私の方法であり、カテゴリ名が表示されます。同じ値を持つ2つのエントリがある場合は動作しません(indexofを使用しているため、配列内で最初に見つかったものが表示されます...)。問題を解決する方法がわかりません:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { 
     let style = NSMutableParagraphStyle() 
     style.alignment = NSTextAlignment.center 
     let centerText = NSMutableAttributedString() 
     let category = NSMutableAttributedString(string: cats[expensesArray.index(of: entry.y)!] , attributes: [NSParagraphStyleAttributeName: style, NSForegroundColorAttributeName:UIColor.black,NSFontAttributeName: UIFont(name: "GillSans",size:19)!]) 
     let amount = NSMutableAttributedString(string: "\n"+formatter.string(from: entry.y as NSNumber)!, attributes: [NSParagraphStyleAttributeName: style, NSForegroundColorAttributeName:UIColor.black,NSFontAttributeName: UIFont(name: "GillSans",size:20)!]) 
     centerText.append(category) 
     centerText.append(amount) 
     pieChartView.centerAttributedText = centerText 
    } 
+0

私はsetChartメソッドを含めるのを忘れていました。更新しました – gmoraleda

答えて

0

見つかりソリューションに直接アクセスするChartDataEntryオブジェクトは:

entry as! PieChartDataEntry).label 

ので、全体chartValueSelectedは次のようになります。

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { 
     let style = NSMutableParagraphStyle() 
     style.alignment = NSTextAlignment.center 
     let centerText = NSMutableAttributedString() 
     let category = NSMutableAttributedString(string: (entry as! PieChartDataEntry).label! 
, attributes: [NSParagraphStyleAttributeName: style, NSForegroundColorAttributeName:UIColor.black,NSFontAttributeName: UIFont(name: "GillSans",size:19)!]) 
     let amount = NSMutableAttributedString(string: "\n"+formatter.string(from: entry.y as NSNumber)!, attributes: [NSParagraphStyleAttributeName: style, NSForegroundColorAttributeName:UIColor.black,NSFontAttributeName: UIFont(name: "GillSans",size:20)!]) 
     centerText.append(category) 
     centerText.append(amount) 
     pieChartView.centerAttributedText = centerText 
    } 
関連する問題