In MPAndroidChart, how to add the click event for the each Bar in Barchart?

陌路散爱 提交于 2019-12-10 17:15:46

问题


Am using MPAndroidChart library to implemented the Barchart, How to implement the click listener for each bar in Barchart.I want to show the clicked barvalue in Toast.How to implement this one ?

mChart = (BarChart) findViewById(R.id.chart1);
mChart.setDrawYValues(true);
mChart.setDrawValueAboveBar(true);
mChart.setDescription("");
mChart.setMaxVisibleValueCount(10);
mChart.set3DEnabled(false);

mChart.setPinchZoom(false);
mChart.setUnit(" mg/dL");
mChart.setDrawGridBackground(false);
mChart.setDrawHorizontalGrid(true);
mChart.setDrawVerticalGrid(false);
mChart.setValueTextSize(10f);
mChart.setDrawBorder(false);


XLabels xl = mChart.getXLabels();
xl.setPosition(XLabelPosition.BOTTOM);
xl.setSpaceBetweenLabels(10);
xl.setCenterXLabelText(true);

YLabels yl = mChart.getYLabels();
yl.setLabelCount(8);
yl.setPosition(YLabelPosition.LEFT);

setData();

Legend l = mChart.getLegend();
l.setPosition(LegendPosition.BELOW_CHART_RIGHT);
l.setFormSize(8f);
l.setXEntrySpace(4f);

mChart.animateY(3000);


private void setData() {

    ArrayList<String> xVals = new ArrayList<String>();
    xVals.add("F");
    xVals.add("PP");
    xVals.add("U");
    xVals.add("C");
    xVals.add("TC");
    xVals.add("Tri");
    xVals.add("HDL");
    xVals.add("LDL");
    xVals.add("VLDL");

    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
    yVals1.add(new BarEntry(95, 0));
    yVals1.add(new BarEntry(99, 1));
    yVals1.add(new BarEntry(19, 2));
    yVals1.add(new BarEntry((float) 1.21, 3));
    yVals1.add(new BarEntry(250, 4));
    yVals1.add(new BarEntry(323, 5));
    yVals1.add(new BarEntry(47, 6));
    yVals1.add(new BarEntry(135, 7));
    yVals1.add(new BarEntry(68, 8));


    BarDataSet set1 = new BarDataSet(yVals1, "");
    //set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
    set1.setColors(new int[]{R.color.green,R.color.green,R.color.green,R.color.green,R.color.margin,R.color.red,R.color.green,R.color.green,R.color.green},getApplicationContext());
    set1.setBarSpacePercent(50f);

    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    dataSets.add(set1);

    BarData data = new BarData(xVals, dataSets);

    mChart.setData(data);
}

回答1:


Try implementing OnChartValueSelectedListener present in the mpandroidchart library




回答2:


You need to implement OnChartValueSelectedListener. Click here for the javadoc for that class.

There is an example from the MPAndroidChart demo project here (using MPAndroidChart 3.0.2)

@Override
public void onValueSelected(Entry e, Highlight h) {
    Log.i("VAL SELECTED",
            "Value: " + e.getY() + ", xIndex: " + e.getX()
                    + ", DataSet index: " + h.getDataSetIndex());
}

@Override
public void onNothingSelected() {
}


来源:https://stackoverflow.com/questions/28314966/in-mpandroidchart-how-to-add-the-click-event-for-the-each-bar-in-barchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!