user9483522
user9483522

Reputation: 304

Pie chart text is not showing correctly?

I have written the below code but pie chart is showing like this

let months = ["Jan", "Feb", "Apr", "Jun"]
        let unitsSold = [68, 10, 12, 10]
    setChart(dataPoints: months, values: unitsSold)
func setChart(dataPoints: [String], values: [Int]) {
        let screenBounds = UIScreen.main.bounds
        let width = screenBounds.width
        let height = screenBounds.height
        let frame = CGRect(x: 10, y: 10, width: width, height: height/1.5)
        let chart = PieChartView(frame: frame)
        var dataEntries: [ChartDataEntry] = []
        for i in 0..<dataPoints.count {
            let dataEntry = ChartDataEntry(x: Double(values[i]), y: Double(i))
            dataEntries.append(dataEntry)
        }
        let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Units Sold")
        var colors: [UIColor] = []
        for _ in 0..<dataPoints.count {
            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)

        }
        pieChartDataSet.colors = colors
        let data = PieChartData(dataSet: pieChartDataSet)
        piecharView.data = data
        piecharView.backgroundColor = UIColor.white
        piecharView.noDataText = "No Data Available"
        piecharView.isUserInteractionEnabled = true
         let d = Description()
        d.text = ""
        piecharView.chartDescription = d
        piecharView.centerText = "HEALTH SCORE"
        piecharView.holeRadiusPercent = 0.5
        piecharView.transparentCircleColor = UIColor.clear
        piecharView.usePercentValuesEnabled = true
        piecharView.drawEntryLabelsEnabled = true

        pieChartDataSet.colors = colors

    }

But I want like this pie Chart I have enabled the percentage but still not showing the pie chart what I want.

Now it is showing like this

Upvotes: 3

Views: 1565

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

After the line let data = PieChartData(dataSet: pieChartDataSet)

put this code

    let pFormatter = NumberFormatter()
    pFormatter.numberStyle = .percent
    pFormatter.maximumFractionDigits = 1
    pFormatter.multiplier = 1
    pFormatter.percentSymbol = " %"
    data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))

    data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 11)!)
    data.setValueTextColor(.white)

And after that

    piecharView.data = data

and

    piecharView.setNeedDisplay() 

EDIT

Step 1: Goto Storybaord and set proper constraint to pieChartView
Step 2: In storyboard select PieCharView and set it class PieChartView from right panel.
Step 3: remove old IBOutlet and attach new IBoutlet should looks like

@IBOutlet weak var pieChartView: PieChartView!

Add this method in you class

func setCharts(dataPoints:[String], values : [Double]) {
    var dataEntries = [PieChartDataEntry]()

    for i in 0..<dataPoints.count {
        let dataEntry =  PieChartDataEntry(value: values[i], label: dataPoints[i])
        dataEntries.append(dataEntry)
    }
    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Unit Sold")
    pieChartDataSet.sliceSpace = 3
    pieChartDataSet.selectionShift = 5

    var colors: [UIColor] = []

    for _ in 0..<dataPoints.count {
        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)
    }

    pieChartDataSet.colors = colors

    let pieChartData = PieChartData(dataSet: pieChartDataSet)

    let pFormatter = NumberFormatter()
    pFormatter.numberStyle = .percent
    pFormatter.maximumFractionDigits = 1
    pFormatter.multiplier = 1
    pFormatter.percentSymbol = " %"
    pieChartData.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))

    pieChartData.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 11)!)
    pieChartData.setValueTextColor(.white)


    self.pieChartView.data = pieChartData
    self.pieChartView.setNeedsDisplay()
    self.pieChartView.animate(xAxisDuration: 2.0,easingOption: .easeOutElastic)
}

And From viewDidLoad paste following

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]

    setCharts(dataPoints: months, values: unitsSold)

Output :

enter image description here

EDIT 2

    var attrString: NSMutableAttributedString?

    let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
    paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
    paragraphStyle.alignment = .center

    attrString = NSMutableAttributedString(string: "HEALTH SCORE HEALTH SCORE HEALTH SCORE HEALTH SCORE")
    attrString?.setAttributes([
        NSAttributedStringKey.foregroundColor: NSUIColor.black,
        NSAttributedStringKey.font: NSUIFont.systemFont(ofSize: 12.0),
        NSAttributedStringKey.paragraphStyle: paragraphStyle
        ], range: NSMakeRange(0, attrString!.length))

    pieChartView.centerAttributedText =  attrString

Hope it is helpful

Upvotes: 1

Related Questions