1

I am using iOS-Chart to display this image i know that value is coming from label but i need to hide zero labels from chart but not from below description.I have tried to pass empty on label but that wont make it dynamic. What i want is --> if it is zero, label should not be generated on pie chart

func pieChartUpdate () {
    //future home of pie chart code
    
    HalfPieChartView.delegate = self
    
    HalfPieChartView.holeColor = .white
    HalfPieChartView.transparentCircleColor = NSUIColor.white.withAlphaComponent(0.43)
    HalfPieChartView.holeRadiusPercent = 0.58
    HalfPieChartView.rotationEnabled = false
    HalfPieChartView.highlightPerTapEnabled = true
    
    HalfPieChartView.maxAngle = 180 // Half chart
    HalfPieChartView.rotationAngle = 180 // Rotate to make the half on the upper side
    HalfPieChartView.centerTextOffset = CGPoint(x: 0, y: -20)
    
    
    let entry1 = PieChartDataEntry(value: Double(pieProdPer), label: "Productive")
    let entry2 = PieChartDataEntry(value: Double(pieUnProdPer), label: "Unproductive")
    let entry3 = PieChartDataEntry(value: Double(pieNeutralPer), label: "Neutral")
    let dataSet = PieChartDataSet(entries: [entry1, entry2, entry3], label: "")

    dataSet.sliceSpace = 3
    dataSet.selectionShift = 5
    let prodcolor = NSUIColor(red: 72/255.0, green: 195/255.0, blue: 252/255.0, alpha: 1.0)
    let unprodcolor = NSUIColor(red: 255/255.0, green: 125/255.0, blue: 139/255.0, alpha: 1.0)
    let neutralcolor = NSUIColor(red: 228/255.0, green: 228/255.0, blue: 228/255.0, alpha: 1.0)
    
    dataSet.colors = [prodcolor,unprodcolor,neutralcolor]
    
    let data = PieChartData(dataSet: dataSet)
    let pFormatter = NumberFormatter()
    pFormatter.numberStyle = .percent
    pFormatter.maximumFractionDigits = 1
    pFormatter.multiplier = 1
    pFormatter.percentSymbol = " %"
    //using zeroSymbol i can remove number from chart
    pFormatter.zeroSymbol = ""
    data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))

    data.setValueFont(NSFont(name: "Open Sans", size: 11)!)
    data.setValueTextColor(.white)

    HalfPieChartView.data = data
    HalfPieChartView.chartDescription?.text = ""

    //All other additions to this function will go here

    //This must stay at end of function
    HalfPieChartView.notifyDataSetChanged()
   
}

Half Pie Chart

pradeexsu
  • 1,029
  • 1
  • 10
  • 27

1 Answers1

0

You can try with @JoakimDanielson's comment like not adding the entry to dataSet if the value is 0. You can check an example with an array of static data

Code:

import UIKit
import Charts

class PieViewController: UIViewController {
    
    @IBOutlet weak var pieChartView: PieChartView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
        let unitSold = [6.0, 8.0, 26.0, 0.0, 8.0, 10.0]
        setChart(dataPoints: months, values: unitSold.map{ Double($0) })
    }

    func setChart(dataPoints: [String], values: [Double]) {
        
      //Set ChartDataEntry
      var dataEntries: [ChartDataEntry] = []
      for i in 0..<dataPoints.count {
        
        //if value is 0 not adding to array of dataEntries
        if values[i] != 0 {
            
            let dataEntry = PieChartDataEntry(value: values[i], label: dataPoints[i], data: dataPoints[i] as AnyObject)
            dataEntries.append(dataEntry)
        }
        
      }
        
      //Set ChartDataSet
      let pieChartDataSet = PieChartDataSet(entries: dataEntries, label: nil)
      pieChartDataSet.colors = colorsOfCharts(numbersOfColor: dataPoints.count)
        
      //Set ChartData
      let pieChartData = PieChartData(dataSet: pieChartDataSet)
      let format = NumberFormatter()
      format.numberStyle = .none
      let formatter = DefaultValueFormatter(formatter: format)
      pieChartData.setValueFormatter(formatter)
        
      //Assign it to the chart’s data
      pieChartView.data = pieChartData

    }

    //colors for entries of the dataSet
    func colorsOfCharts(numbersOfColor: Int) -> [UIColor] {
        var colors: [UIColor] = []
        for _ in 0..<numbersOfColor {
            let red = Double(arc4random_uniform(256))
            let green = Double(arc4random_uniform(256))
            let blue = Double(arc4random_uniform(256))
            let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
            colors.append(color)
        }
        return colors
    }

}

Output:

enter image description here

Kishan Bhatiya
  • 2,175
  • 8
  • 14
  • productive, unproductive and neutral colors are set, that should not change if count is less.I used your method even though i removed labels but color also changes with it – Jis Thottan Nov 17 '20 at 13:15