Search code examples
iosswiftchartspie-chartios-charts

How to select pie chart slice by default in Charts library


Am using Pie chart in, Charts ios library. I want to know, how can i select first slice by default when it Pie chart loads.

I found this chartView.highlightValue(x: 45, dataSetIndex: 0). But this code is not working.

My pie chart has two slices with PieChartDataEntry. I want first one to be selected by default.

let entries = (0..<count).map { (i) -> PieChartDataEntry in
            if i == 0 {
                return PieChartDataEntry(value: 45,
                                         label: "")
            } else {
                return PieChartDataEntry(value: 55,
                                         label: "")
            }
        }

Solution

  • If you want the first slice to be selected, set the x to be 0. Note that x here is the index of your data and dataSetIndex is always 0 for PieCharts

    chartView.highlightValue(x: 0, dataSetIndex: 0)