Hunter
Hunter

Reputation: 421

swift/charts: chart value int instead of double

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

Upvotes: 4

Views: 2564

Answers (3)

Shilpashree MC
Shilpashree MC

Reputation: 641

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))

Upvotes: 7

Alfredo Luco G
Alfredo Luco G

Reputation: 974

Try this:

let number = NSNumber(value: value)
entry.y = number.integerValue

Upvotes: 0

Rashwan L
Rashwan L

Reputation: 38833

Instead of:

entry.y = Double(value)

Do:

entry.y = Int(value)

Note that 2.1, 2.2 etc will return 2

Upvotes: 0

Related Questions