'Swift danielgindi/Charts Pie Chart Legend Labels
I am not getting all the labels to show when doing a legend for a Pie Chart.
here is the code;
func AllocationsChart( pieChart: PieChartView ) {
let entry1 = PieChartDataEntry(value: 65, label: "Stock")
let entry2 = PieChartDataEntry(value: 35, label: "Bond")
let dataSet = PieChartDataSet(entries: [entry1, entry2 ] , label: "Allocation" )
let data = PieChartData(dataSet: dataSet)
pieChart.data = data
let labels = ["Stock","Bonds"] // would like to set the label here
let colors = [ Color.stock, Color.bond ]
dataSet.colors = colors
dataSet.sliceSpace = 2
pieChart.drawHoleEnabled = true
pieChart.transparentCircleColor = .white
pieChart.holeRadiusPercent = 0.6667
let l = pieChart.legend
l.enabled = true
l.orientation = .vertical
l.form = .circle
}
and here is the results.
Is there anyway to set the labels like we can do with other charts views like this:
xAxis.valueFormatter = IndexAxisValueFormatter(values: labels)
First, why is the bond entry not showing?
Also the color for the legend label does not match entry.
Thank you very much.
Solution 1:[1]
it was a matter of adding this line of code to the end:
pieChart.notifyDataSetChanged()
still no idea how to set the labels other than the PieChartDataEntry label?
good luck.
Solution 2:[2]
Change the label statement to label: "nil" and see what it displays.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | diogenes |
| Solution 2 | Edward Hasted |

