3

I want to turn the 1.00 and 2.00 into 1 and 2. When I try to change entry.y = Double(value) to entry.y = Int(value) it says it has to be a Double. How do I turn the values into whole numbers?

var entries = [PieChartDataEntry]()
        for (index, value) in dataarray.enumerated() {
            let entry = PieChartDataEntry()
            entry.y = Double(value)
            entry.label = self.labels[index]
            entries.append(entry)
        }

This is the chart I am using:

Chart

DevB2F
  • 4,674
  • 4
  • 36
  • 60
Hunter
  • 313
  • 5
  • 20
  • I have no experience with PieChart, but this looks similar: https://stackoverflow.com/questions/40453604/how-to-add-to-data-in-ios-chart, i.e. you have to set a "value formatter". – Martin R Jun 27 '17 at 18:10
  • @MartinR Thanks for the link but my data entry is structured differently than the one in that post. – Hunter Jun 27 '17 at 18:22
  • It was just meant as a hint that you look for "value formatters". – Martin R Jun 27 '17 at 18:27

3 Answers3

6
let  pieChartView = PieChartView(frame:CGRect(x:60,y:50,width:200,height:300))

let track = ["Passed", "Failed", "Pending"]
let money = [10, 6, 10]

var entries = [PieChartDataEntry]()
   for (index, value) in money.enumerated() {
      let entry = PieChartDataEntry()

      entry.y = Double(value)     
      entry.label = track[index]
      entries.append( entry)
   }

let set = PieChartDataSet( values: entries, label: "")
let data = PieChartData(dataSet: set)

pieChartView.data = data

let formatter = NumberFormatter()
formatter.minimumFractionDigits = 0
data.setValueFormatter(DefaultValueFormatter(formatter:formatter))
maniponken
  • 265
  • 3
  • 15
Shilpashree MC
  • 611
  • 1
  • 6
  • 16
0

Instead of:

entry.y = Double(value)

Do:

entry.y = Int(value)

Note that 2.1, 2.2 etc will return 2

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • I get the error of 'cannot assign value of type 'Int' to type 'Double''. It then suggests me to change it to `entry.y = Double(Int(value))` – Hunter Jun 27 '17 at 18:07
  • That´s because your `entry.y` is of type `Double`. What you can do is to remove the numbers after the decimal. I´ll update the answer. – Rashwan L Jun 27 '17 at 18:11
  • How would I remove the numbers after the decimal? – Hunter Jun 27 '17 at 18:23
  • Investigated this and even if you remove the numbers after the decimals you would still need to add Double value to `entry.y` my suggestion is to check the `DefaultValueFormatter` as MartinR gave example for to solve you issue. – Rashwan L Jun 27 '17 at 18:31
0

Try this:

let number = NSNumber(value: value)
entry.y = number.integerValue
Alfredo Luco G
  • 876
  • 7
  • 18
  • Thanks for your answer, but it does not work. It makes me change it to `entry.y = Double(number.intValue)` – Hunter Jun 27 '17 at 20:55