Swift:ios-图表点击处理程序无法正常工作

Swift: ios-chart tap handler not working properly

我有以下图表:

当我点击周日、周一和周三的柱子时,它没有反应。当我点击星期二柱时,它会响应并正确打印“2”(表示它是第三个柱)。当我点击 table 中的其余柱时,它会打印“4”,这基本上意味着周四柱被点击了。

这是我的代码:

@IBOutlet weak var graphView: BarChartView!
func updateGraph(){
    var dataEntries: [BarChartDataEntry] = []
    let timeSheet: TimeSheet = TimeSheet.getTimeSheet()
    let weekData = timeSheet.getData()
    let keys: [String] = weekData.allKeys as! [String]
    for weekday in keys{
        let _weekday = weekday == "Sunday" ? 0 : weekday == "Monday" ? 1 : weekday == "Tuesday" ? 2: weekday == "Wednesday" ? 3 : weekday == "Thursday" ? 4 : weekday == "Friday" ? 5 : 6
        let dayData = weekData.value(forKeyPath: weekday) as! NSDictionary
        var totalWorkHour: Double = Double(dayData["totalWorkHour"] as! NSNumber)
        totalWorkHour = Double(Int(totalWorkHour)/60*100 + Int(totalWorkHour)%60)/100  //Chaing minutes to hour format
        let entry = BarChartDataEntry(x: Double(_weekday), y: totalWorkHour)
        dataEntries.append(entry)
    }
    let graphDataSet = BarChartDataSet(values: dataEntries, label: nil)
    graphDataSet.colors = ChartColorTemplates.colorful()  //Make bars colorful
    graphDataSet.axisDependency = .left
    //graphDataSet.barBorderWidth = graphDataSet.barBorderWidth/2
    let graphData = BarChartData(dataSet: graphDataSet)
    graphView.data = graphData
}

func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight){
    print("\(entry.x)")
}

我基本上是跟着ios-charts-api-tutorial

如果你有什么发现,请告诉我。

看来是追加顺序的问题。似乎如果我们不按顺序插入数据(即星期四、星期五、星期二、星期六......),点击操作就会出错。 我通过在周日、周一、周二、周三对我的字典进行排序解决了这个问题...