5

I have a pie chart and I'm trying to remove the value labels from the chart as they are spilling on to each other, but no code seems to take it away.

This is the code I've been using to try to remove it:

chartIMG.drawEntryLabelsEnabled = false

but it does not seem to work.

My code for creating a chart:

func configure(dataPoints: [String], values: [Double]) {

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry1 = PieChartDataEntry(value: Double(i), label: dataPoints[i], data:  dataPoints[i] as AnyObject)

        dataEntries.append(dataEntry1)
    }
    print(dataEntries[0].data)
    let pieChartDataSet = PieChartDataSet(values: dataEntries, label: "Symptoms")
    let pieChartData = PieChartData(dataSet: pieChartDataSet)
    chartIMG.data = pieChartData
    chartIMG.drawEntryLabelsEnabled = false
    chartIMG.chartDescription?.text = ""
    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
}

Is this the only way to remove it or am I doing something wrong?

picciano
  • 22,341
  • 9
  • 69
  • 82
Chris Campbell
  • 313
  • 7
  • 21

3 Answers3

8

If you need to disable drawing values of Data Set Entries use this

pieChartDataSet.drawValuesEnabled = false

If you need to disable drawing values on some Axis use this:

chartIMG.rightAxis.drawLabelsEnabled = false
chartIMG.leftAxis.drawLabelsEnabled = false
chartIMG.xAxis.drawLabelsEnabled = false
chartIMG.rightAxis.drawLabelsEnabled = false
Dmitry
  • 2,963
  • 2
  • 21
  • 39
4

I know this is an old post but I searched forever to figure this problem out. I think with the last version released, they disabled access to drawLabelsEnabled which makes turning off the value labels on the pie chart nearly impossible.

The way I forced it to not draw labels was to change line 335 in Charts.xcodeproj > Source > Charts > Renderers > PieChartRenderer.swift:

 let drawValues = dataSet.isDrawValuesEnabled

to

 let drawValues = false
Jordan
  • 319
  • 2
  • 12
  • 1
    thank you very much, the guys doing the library are crazy enough to disable a configuration like that? on the Iphone these charts looks terrible because none can read the labels! – JBarros35 Nov 27 '19 at 15:44
0

Swift 5.0+ Charts v3.5+

pieChartView.data?.setDrawValues(false)
Mishka
  • 502
  • 5
  • 15