gpt4 book ai didi

android - 在MPAndroidChart中,如何为Barchart中的每个Bar添加点击事件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:32 25 4
gpt4 key购买 nike

我正在使用 MPAndroidChart 库来实现 Barchart , 如何在 Barchart 中为每个柱实现点击监听器.我想显示点击的bar Toast 中的值。如何实现这个?

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);
}

最佳答案

尝试实现 mpandroidchart 库中的 OnChartValueSelectedListener

关于android - 在MPAndroidChart中,如何为Barchart中的每个Bar添加点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com