1

I am using danielgindi/Charts for iOS/Swift. There is an extra Legend Entry with label "DataSet" displays in Pie Chart as seen in this image:

enter image description here

When I traced, I found there are two entries in the array of LegendEntry found in the PieChartView legend, i.e. PieChartView.legend.entries, where as I have only one object in my array.

Here is the code:

let dataSet = PieChartDataSet()
    dataSet.drawIconsEnabled = false
    dataSet.setColor(AppColors.selectedMenuItem)
    dataSet.sliceSpace = 3
    dataSet.iconsOffset = CGPoint(x: 0, y: 40)
    dataSet.selectionShift = 5

    var totalRevenuePer:Double = 0.0

    _ = arrRevenue.map({ (objRevenue) -> Void in
        if let percentage = Double(objRevenue.per ?? "0.0"), percentage != 0.0{
            dataSet.append(PieChartDataEntry(value: percentage, label: "\((objRevenue.rev_center_name ?? "") + " " + objRevenue.revenue.currencyString())"))
            totalRevenuePer += percentage
        }
    })

    var colors = AppColors.TenderColors
    if totalRevenuePer < 100{ colors.append(.clear) }
    dataSet.colors = colors

    let data = PieChartData(dataSet: dataSet)
    data.setValueFormatter(PercentageFormatter())
    data.setValueFont(NSUIFont.systemFont(ofSize: 11))
    data.setValueTextColor(.white)

    pieChart.data = data
    pieChart.highlightValue(nil)
    let legend = pieChart.legend
    legend.textColor = .white
    legend.entries.last?.label = ""

    pieChart.animate(yAxisDuration: 1.4, easingOption: .easeInOutQuad)

    // Refresh chart with new data
    pieChart.notifyDataSetChanged()

Appreciate any help, thank you.

Jigaroza287
  • 635
  • 5
  • 13
  • AppColors,arrRevenue, PercentageFormatter details please – Ben Rockey May 29 '19 at 10:20
  • I think the problem is about the line where you wrote `label: "\((objRevenue.rev_center_name ?? "") + " " + objRevenue.revenue.currencyString())")` If you get rid of ?? "", it will be ok. – atalayasa May 29 '19 at 10:43
  • @AtalayAsa, I don't think it should be the problem, it just handles optional value. In case the value is nil it prevents the crash. – Jigaroza287 May 29 '19 at 12:11
  • @BenRockey, AppColors is a simple Array of UIColor, arrRevenue is the array filled with values received from server (which has only one value, i.e. Positronics, as you can see in the image above) and PercentageFormatter is sub class of IValueFormatter, IAxisValueFormatter to print the pie chart value in percentage format. – Jigaroza287 May 29 '19 at 12:15

1 Answers1

9

It's a property of PieChartDataSet

The default value, if you don't set your own, is "DataSet"

let dataSet = PieChartDataSet()

// provide your own
dataSet.label = "My Label"

// or, no label
dataSet.label = ""
DonMag
  • 69,424
  • 5
  • 50
  • 86