14

I know that it is possible to hide y values when their values are equal to 0 on MPAndroidChart using a custom class for your value formatter (MPAndroidChart: Hide 0 value labels in a stacked bar chart).

Despite this, I am not able to create the same class on Swift 3.0 or get any other way to do this. I tried to "translate" the custom class from Java to Swift 3.0 without success (I can copy the code of what I have tried if you want but it is full of errors).

Is it possible to hide y values when they are equals to 0 on ios-chart library?

P.S: I am using Swift 3.0.

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

3 Answers3

31

I made it happen on a PieChart in one of my apps just like that :

    ...
    let dataSet = PieChartDataSet(yVals: yVals, label: nil)

    // This is where the magic happen
    // You set a NSNumberFormatter with an empty zero Symbol
    let noZeroFormatter = NumberFormatter()
    noZeroFormatter.zeroSymbol = ""
    dataSet.valueFormatter = ChartDefaultValueFormatter(formatter: noZeroFormatter)

    let chartData = PieChartData(xVals: xVals, dataSet: dataSet)
    ...
Dean
  • 1,512
  • 13
  • 28
  • Thank you for answer! I forgot to add that I was using `Swift 3.0` and I had to change a bit your code (change from `NSNumberFormatter` to `NumberFormatter`) but it gives to me an error on `dataSet.valueFormatter = integerFormatter` that says: `Cannot assign value of type 'NumberFormatter' to type 'IValueFormatter?'` – Francisco Romero Oct 19 '16 at 10:11
  • Okay, it's an issue with Swift 3 conversion. Use `dataSet.valueFormatter = ChartDefaultValueFormatter(formatter: integerFormatter)` instead. I update the answer right away – Dean Oct 19 '16 at 10:28
  • Wow it works like a charm! Thank you! And how can I make that the rest of elements will have two decimals? I need that at the same time 0 values are hidden the rest of values will be float, but the rest of values are shown automatically as integers. I know that I can do this on Android as follows: `DecimalFormat("###,###,###,##0.0")` but it does not exist on Swift. I also could see that exists `noZeroFormatter.generatesDecimalNumbers` that I guessed that it is to allow decimal numbers on the chart. I have set it to `true` but it does not seems to work. – Francisco Romero Oct 19 '16 at 11:00
  • Try to set `minimumFractionDigits` and `maximumFractionDigits` at 2 on your `NumberFormatter` instead. – Dean Oct 19 '16 at 11:45
  • It shows now the values with two decimals but rounding the value so all the values are like: 1456.00, 7894.00 when they are really as: 1455.52, 7893.87. How can I show the real value? – Francisco Romero Oct 19 '16 at 12:14
  • Are you sure you do not cast your data in Integer at some point ? Maybe it is the iOS Chart library that do so, I don't really know. Have you set other properties on your NumberFormatter object ? – Dean Oct 19 '16 at 12:35
  • @Gero Sure. I will award the bounty to him last day. He deserves to get more rep until the bounty ends. By the moment: accept and upvote :) – Francisco Romero Oct 20 '16 at 10:23
  • 1
    @Error404 Ah, nice to see you have an eye on that. :) May the errors keep avoiding you, hehe... – Gero Oct 21 '16 at 06:39
  • 2
    In the new version (3.0), **ChartDefaultValueFormatter** has been rename to **DefaultValueFormatter**. Hope it helps. – mmk Aug 13 '19 at 02:15
2

if you want to add % in in your graph as well as hide/remove 0.0 values from graph :

used below lines of code for # Swift 3:-

   func updateChartData()  {

        let chart = PieChartView(frame: mViewOutlet.frame)
//         let chart = PieChartView(frame: CGRect(x: 122, y: 235 , width: self.mViewOutlet.frame.size.width, height: self.mViewOutlet.frame.size.height))


        // 2. generate chart data entries
        let track = ["Present","Leave", "EG/LC", "Halfday", "Absent", "Weeklyoff", "Holidays"]
        //        let money = [65, 13, 10, 2]
        let money = mDaysArray

        var entries = [PieChartDataEntry]()
        for (index, value) in money.enumerated() {
            print("index: \(index) \n value: \(value)")
            let entry = PieChartDataEntry()

            if value != 0 {
                 entry.y = Double(value)
            }else{

            }
            entries.append(entry)
//            entry.label = track[index]  // if we want to remove name label
        }

        // 3. chart setup
        let set = PieChartDataSet( values: entries, label: "Pie Chart")
        // this is custom extension method. Download the code for more details.

        //4. set chart color
        let presentColor = UIColor(red: 80.0/255.0, green: 180.0/255.0, blue: 50.0/255.0, alpha: 1.0)
        //        let lateColor = UIColor(red: 241.0/255.0, green: 194.0/255.0, blue: 114.0/255.0, alpha: 1.0)
        let leaveColor = UIColor(red: 203.0/255.0, green: 68.0/255.0, blue: 242.0/255.0, alpha: 1.0)
        let egColor = UIColor(red: 95.0/255.0, green: 180.0/255.0, blue: 239.0/255.0, alpha: 1.0)
        let halfdayColor = UIColor(red: 82.0/255.0, green: 64.0/255.0, blue: 152.0/255.0, alpha: 1.0)
        let absentColor = UIColor(red: 242.0/255.0, green: 58.0/255.0, blue: 02.0/255.0, alpha: 1.0)
        let weekOffColor = UIColor(red: 186.0/255.0, green: 221.0/255.0, blue: 79.0/255.0, alpha: 1.0)
        let holidayColor = UIColor(red: 35.0/255.0, green: 215.0/255.0, blue: 179.0/255.0, alpha: 1.0)

        let colors: [UIColor] = [presentColor,leaveColor,egColor,halfdayColor,absentColor,weekOffColor,holidayColor]

        set.colors = colors
        let data = PieChartData(dataSet: set)

        let formatter = NumberFormatter()
        formatter.numberStyle = .percent
        formatter.maximumFractionDigits = 2
        formatter.multiplier = 1.0
        formatter.percentSymbol = "%"
        formatter.zeroSymbol = ""
        data.setValueFormatter(DefaultValueFormatter(formatter: formatter))

        chart.data = data
        chart.noDataText = "No data available"
         chart.usePercentValuesEnabled = true
        // user interaction
        chart.isUserInteractionEnabled = false

        let d = Description()
//        d.text = "iOSCharts.io"
        chart.chartDescription = d
//        chart.tintColor = UIColor.black
//        chart.centerText = "Pie Chart"

        chart.holeRadiusPercent = 0.2
        chart.chartDescription?.enabled = false
        chart.legend.enabled = false

        chart.data?.notifyDataChanged()
        chart.notifyDataSetChanged()
        chart.setNeedsDisplay()
        chart.animate(xAxisDuration: 1.3, yAxisDuration: 1.3)

        chart.transparentCircleColor = UIColor.clear
//        self.view.addSubview(chart)
        self.mPieChartMainView.addSubview(chart)
    }
Kiran Jadhav
  • 3,209
  • 26
  • 29
0
  let data = PieChartData(dataSet: set)

    let pFormatter = NumberFormatter()
    pFormatter.numberStyle = .none
    pFormatter.zeroSymbol = "";
    pFormatter.maximumFractionDigits = 1
    pFormatter.multiplier = 1
    pFormatter.percentSymbol = ""
    data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
TapulaRasa
  • 325
  • 3
  • 13