Swift:ios-chart tap处理程序无法正常工作

时间:2017-02-27 23:51:21

标签: ios swift ios-charts

我有以下图表:

graphView

当我点击星期日,星期一和星期三的酒吧时,它没有回应。当我点击Tue栏时,它会响应并正确打印“2”(意思是第三栏)。当我点击表格中的其余栏时,它会打印“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

如果你弄明白了,请告诉我。

1 个答案:

答案 0 :(得分:0)

显然,这是附加订单的问题。看起来如果我们不按顺序插入数据(即周四,周五,周二,周六......),点击操作就会出错。 我通过在Sun,Mon,Tue,Wed ...中排序我的字典来解决这个问题。