如何使用ios-charts在饼图视图中获取选定切片的索引?

时间:2022-12-03 17:15:14

I can't figure out how to make a UILabel visible if a slice is selected in a Pie Chart View. I need to know how to identify if a slice was selected and then what slice was selected so I can put the right text in a UILabel and then unhide the label.

如果在饼状图视图中选择了一个切片,我就不知道如何使UILabel可见。我需要知道如何识别一个切片是否被选中,然后选择哪个切片,这样我就可以在UILabel中放入正确的文本,然后去掉标签。

pseudocode:

伪代码:

if sliceSelected == true {

    var index = sliceSelected.index

    label.text = categoryArray[index]

    label.hidden = false

}

3 个解决方案

#1


1  

Here's how you do it. Add ChartViewDelegate to your class and then insert the method below into the body of your code. Any thing that you want to happen if someone touches a value on your chart should go in the body of this function.

这是你怎么做的。将ChartViewDelegate添加到类中,然后将下面的方法插入到代码中。任何你想要发生的事情,如果有人触摸你的图表上的值,应该在这个函数的主体。

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {

}

Shout out to this very useful tutorial for the ios-charts API: http://www.appcoda.com/ios-charts-api-tutorial/

向这个非常有用的ios-charts API教程大声喊出来:http://www.appcoda.com/ios-charts-api-tutorial/

#2


0  

Have you tried storing the selected slice in a variable like:

您是否尝试过将选定的切片存储在如下变量中:

var slice: NSMutableDictionary = [:]

var slice: NSMutableDictionary = [:]

And then set the value for forKeyPath: index?

然后设置forKeyPath的值:index?

#3


0  

This is what worked for me. Attach an enum value as the data field of each entry, then analyze that value within the chartValueSelected delegate method.

这对我起了作用。将enum值作为每个条目的数据字段,然后在chartvaluesselected delegate方法中分析该值。

For instance, if you have two slices, create the enum

例如,如果您有两个切片,则创建enum。

internal enum SliceType {
    case pepperoni
    case cheese
}

Then when constructing the PieChartDataSet, construct your PieChartDataEntry objects with their SliceType as associated data:

然后在构造PieChartDataSet时,使用它们的SliceType作为关联数据构建您的PieChartDataEntry对象:

let entry = PieChartDataEntry(value: pepperoniValue,
                              data: SliceType.pepperoni as AnyObject?)

Now make sure to set your view controller as the pie chart's delegate. Then in your chartValueSelected delegate method, do something like the following:

现在请确保将视图控制器设置为饼图的委托。然后在chartvalues民选委托方法中,执行以下操作:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    // Extract the SliceType from the selected value
    guard let sliceType = entry.data as? SliceType else {
        return
    }

    switch sliceType {
    case .pepperoni:
        // Configure the view for pepperoni being selected
    case .cheese:
        // Configure the view for cheese being selected
    }
}

#1


1  

Here's how you do it. Add ChartViewDelegate to your class and then insert the method below into the body of your code. Any thing that you want to happen if someone touches a value on your chart should go in the body of this function.

这是你怎么做的。将ChartViewDelegate添加到类中,然后将下面的方法插入到代码中。任何你想要发生的事情,如果有人触摸你的图表上的值,应该在这个函数的主体。

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {

}

Shout out to this very useful tutorial for the ios-charts API: http://www.appcoda.com/ios-charts-api-tutorial/

向这个非常有用的ios-charts API教程大声喊出来:http://www.appcoda.com/ios-charts-api-tutorial/

#2


0  

Have you tried storing the selected slice in a variable like:

您是否尝试过将选定的切片存储在如下变量中:

var slice: NSMutableDictionary = [:]

var slice: NSMutableDictionary = [:]

And then set the value for forKeyPath: index?

然后设置forKeyPath的值:index?

#3


0  

This is what worked for me. Attach an enum value as the data field of each entry, then analyze that value within the chartValueSelected delegate method.

这对我起了作用。将enum值作为每个条目的数据字段,然后在chartvaluesselected delegate方法中分析该值。

For instance, if you have two slices, create the enum

例如,如果您有两个切片,则创建enum。

internal enum SliceType {
    case pepperoni
    case cheese
}

Then when constructing the PieChartDataSet, construct your PieChartDataEntry objects with their SliceType as associated data:

然后在构造PieChartDataSet时,使用它们的SliceType作为关联数据构建您的PieChartDataEntry对象:

let entry = PieChartDataEntry(value: pepperoniValue,
                              data: SliceType.pepperoni as AnyObject?)

Now make sure to set your view controller as the pie chart's delegate. Then in your chartValueSelected delegate method, do something like the following:

现在请确保将视图控制器设置为饼图的委托。然后在chartvalues民选委托方法中,执行以下操作:

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    // Extract the SliceType from the selected value
    guard let sliceType = entry.data as? SliceType else {
        return
    }

    switch sliceType {
    case .pepperoni:
        // Configure the view for pepperoni being selected
    case .cheese:
        // Configure the view for cheese being selected
    }
}