MPAndroidChart Pie处理OnChartValueSelectedListener和OnSwipeTouchListener

时间:2019-05-31 03:19:50

标签: android kotlin mpandroidchart

尝试使用下面的代码捕获两种类型的事件。基于此代码的结果是OnSwipeTouchListener正在吞噬或模糊OnChartValueSelectedListener事件。

我正在跟踪OnSwipeTouchListener中的以下事件:OnSwipeLeft,OnSwipeRight和OnDoubleTap。

如果可能的话,我宁愿使用这些高级基元,而不是尝试使用OnChartGestureListener在较低的层次上构建它,如下所示:

https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart

    peakChart.let {
        // settings
        it.legend.isEnabled = false
        it.setDrawEntryLabels(true)
        //it.setDrawSliceText(true)
        it.setEntryLabelTextSize(12.toFloat())
        it.setTouchEnabled(true)

        // touch
        it.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
            override fun onValueSelected(e: Entry, h: Highlight) {
                val index = chartPageControl.selection
                toast("duration: ${durationIndex[h.x.toInt()]}, type=${thresholdType[index]}")
            }
            override fun onNothingSelected() {
            }
        })

        // swiping
        it.setOnTouchListener(object : OnSwipeTouchListener(applicationContext) {
            override fun onSwipeLeft() {
                val chartCount = thresholdType.size
                if (chartPageControl.selection == chartCount-1 ) {
                    chartPageControl.selection = 0
                } else {
                    chartPageControl.selection += 1
                }
                loadPeaks()
                super.onSwipeLeft()
            }

            override fun onSwipeRight() {
                val chartCount = thresholdType.size
                if (chartPageControl.selection == 0 ) {
                    chartPageControl.selection = chartCount-1
                } else {
                    chartPageControl.selection -= 1
                }
                loadPeaks()
                super.onSwipeRight()
            }

            override fun onDoubleTap() {
                onSwipeLeft()
            }
        })
    }

0 个答案:

没有答案