在条形图列上触发条形图而不是仅在条形图上触发MPAndroidChart中条形图上的Click事件

时间:2018-07-26 10:29:50

标签: java android kotlin mpandroidchart

我正在使用MPAndroidChart库为必须显示的某些数据制作条形图。就我而言,当用户单击条形图中的条形时,有关该特定条形的详细信息应显示在recyclerview中,如下所示:

应用截图

enter image description here

现在,我的点击事件运行良好,它们在触发时仅显示相关数据。

问题在于,单击事件会在栏的整个列而不是仅在栏上触发。基本上,如果我单击栏上方的空白区域,则仍会触发该栏的相关click事件。但是,我只希望在单击工具栏时触发它。

下面给出了我的相关代码(在Kotlin中,但是对Java的任何帮助也将不胜感激):

    mBarChart=findViewById(R.id.bargraph)
    mBarChart.setNoDataText("No data has been logged to the cloud yet")

    rootRef7.addValueEventListener(object : ValueEventListener, com.github.mikephil.charting.listener.OnChartValueSelectedListener {
        override fun onNothingSelected() {
        }

        override fun onValueSelected(e: Entry?, dataSetIndex: Int, h: Highlight?) {
            barSelected= e!!.xIndex
            if(barSelected!=-1)
            {
                mBGListShow.clear()
                mBGListShow.add(mBGList[barSelected])
                RV2.setLayoutManager(LinearLayoutManager(applicationContext))
                RV2.setAdapter(adapter1(mBGListShow))
                //Toast.makeText(applicationContext,"Data loaded, contains " + mBGList.size.toString() + " items", Toast.LENGTH_SHORT).show()
            }
        }

        override fun onDataChange(dataSnapshot: DataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            var i:Int=0
            for (data: DataSnapshot in dataSnapshot.getChildren()) {
                var emailIDDB = data.child("emailID").getValue().toString().trim()

                if (emailIDDB.equals(currentUserEmailID)) {
                    dateDB=data.child("calendarTime").getValue().toString().trim() + " (" + data.child("beforeEvent").getValue().toString().trim() + ")"
                    BGDB=data.child("currentBGLevel").getValue().toString().trim()
                    BGDBNumber=BGDB.toFloat()

                    dateList.add(dateDB)
                    BGList.add(BarEntry(BGDBNumber,i))
                    i++

                    eventDB=data.child("beforeEvent").getValue().toString().trim()
                    currentBGDB=data.child("currentBGLevel").getValue().toString().trim()
                    targetBGDB=data.child("targetBGLevel").getValue().toString().trim()
                    amountOfCHODB=data.child("totalCHO").getValue().toString().trim()
                    disposedCHODB=data.child("amountDisposedByInsulin").getValue().toString().trim()
                    correctionFactorDB=data.child("correctionFactor").getValue().toString().trim()
                    insulinRecommendationDB=data.child("insulinRecommendation").getValue().toString().trim()
                    typeOfInsulinDB=data.child("typeOfBG").getValue().toString().trim()

                    var mBGRecyclerView: BGRecyclerView= BGRecyclerView(eventDB,currentBGDB,targetBGDB,amountOfCHODB,disposedCHODB,correctionFactorDB,insulinRecommendationDB,typeOfInsulinDB)
                    mBGList.add(mBGRecyclerView)
                }
            }

            mBarChart.xAxis.setLabelsToSkip(0)
            mBarChart.setOnChartValueSelectedListener(this)
            var mBarBGDataSet: BarDataSet= BarDataSet(BGList,"BG Level")
            var mBarData:BarData=BarData(dateList,mBarBGDataSet)
            mBarChart.setData(mBarData)
            mBarChart.setTouchEnabled(true)
            mBarChart.setDragEnabled(true)
            mBarChart.setScaleEnabled(true)
            mBarChart.setDescription("")
            mBarChart.setMaxVisibleValueCount(1000)
            mBarChart.invalidate()
        }

        override fun onCancelled(p0: DatabaseError?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }
    })

2 个答案:

答案 0 :(得分:0)

我迟到了。 这仍然是MPAndroidChart中的错误。它不会区分列的填充区域和未填充区域。如果我们点击未填充区域(顶部,底部,左侧,右侧),则会触发OnValueSelected事件。我已经尝试了一切,但无法解决。

最后,我找到了解决方法。我使用OnChartSingleTap的{​​{1}}事件来捕获当前的y(getY)位置。然后在OnChartGestureListener中比较OnValueselected。您也可以对x轴执行相同的操作。 这为我解决了问题。

答案 1 :(得分:0)

这是解决问题的方法:onValueSelected即使在未填充(空)栏区域(已填充栏区域的顶部)上也会被调用:

/**
 * Selected Y Index on user tap
 */
private var selectedYIndex: Float? = null

 override fun onChartSingleTapped(motionEvent: MotionEvent) {
     // Touched chart entry
    val entry: Entry? =
        chartView.getEntryByTouchPoint(motionEvent.x, motionEvent.y)
    selectedYIndex = if (entry != null) {
        //Entry is non-null which means user clicked on bar area (filled or non-filled)
        motionEvent.y
    } else {
        //Entry is null which means user clicked outside of bar area
        null
    }
}

然后在onValueSelected中:

override fun onValueSelected(e: Entry?, h: Highlight?) {
    //We check for Selected Y Index is non-null (user tapped on non-filled or filled area of bar)
    //Also, we check for selected highlight is non-null and whether user tapped filled area of bar
    if (selectedYIndex != null && h != null && h.yPx <= selectedYIndex!!) {
              //TODO: User tapped on filled bar area
    } 
}