如何从饼图中获取文本
How Can I get text from piechart
我使用了 MP Android 饼图库,这里我将文本显示到饼图中,我还想将此文本从图表中取出。
我想在饼图上单击时获取文本 (xvals)。假设当我点击饼图的一部分时,它应该得到它的文本值(例如,uber、paytm)
代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PieChart pieChart = (PieChart) findViewById(R.id.piechart);
pieChart.setUsePercentValues(true);
datatext = (TextView)findViewById(R.id.datatext);
datatitle = (TextView)findViewById(R.id.datatitle);
// IMPORTANT: In a PieChart, no values (Entry) should have the same
// xIndex (even if from different DataSets), since no values can be
// drawn above each other.
ArrayList<Entry> yvalues = new ArrayList<Entry>();
yvalues.add(new Entry(24f, 0));
yvalues.add(new Entry(15f, 1));
yvalues.add(new Entry(19f, 2));
yvalues.add(new Entry(22f, 3));
yvalues.add(new Entry(20f, 4));
//yvalues.add(new Entry(17f, 5));
PieDataSet dataSet = new PieDataSet(yvalues, "Balances");
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("Monthly Expenses");
xVals.add("Phonepe");
xVals.add("Uber");
xVals.add("Paytm");
xVals.add("Savings");
// xVals.add("Ola");
dataSet.setDrawValues(false);
PieData data = new PieData(xVals, dataSet);
// In Percentage term
data.setValueFormatter(new PercentFormatter());
// Default value
//data.setValueFormatter(new DefaultValueFormatter(0));
pieChart.setData(data);
pieChart.setDescription("Savings");
pieChart.setDrawHoleEnabled(true);
pieChart.setTransparentCircleRadius(25f);
pieChart.setHoleRadius(25f);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
data.setValueTextSize(13f);
data.setValueTextColor(Color.DKGRAY);
pieChart.setOnChartValueSelectedListener(this);
pieChart.animateXY(1400, 1400);
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if (e == null)
return;
Log.i("VAL SELECTED",
"Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
+ ", DataSet index: " + dataSetIndex);
float datatt = e.getVal();
Toast.makeText(this, ""+datatt, Toast.LENGTH_SHORT).show();
datatext.setText(""+datatt);
//datatitle.setText(""+title);
}
如何获取图表的文本值并在不同的文本视图上显示它
datatext.setText(pieChart.getXAxis().getValues().get(e.getXIndex()));
或
Sting data = ArrayList.get((int) e.getX());
我使用了 MP Android 饼图库,这里我将文本显示到饼图中,我还想将此文本从图表中取出。
我想在饼图上单击时获取文本 (xvals)。假设当我点击饼图的一部分时,它应该得到它的文本值(例如,uber、paytm)
代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PieChart pieChart = (PieChart) findViewById(R.id.piechart);
pieChart.setUsePercentValues(true);
datatext = (TextView)findViewById(R.id.datatext);
datatitle = (TextView)findViewById(R.id.datatitle);
// IMPORTANT: In a PieChart, no values (Entry) should have the same
// xIndex (even if from different DataSets), since no values can be
// drawn above each other.
ArrayList<Entry> yvalues = new ArrayList<Entry>();
yvalues.add(new Entry(24f, 0));
yvalues.add(new Entry(15f, 1));
yvalues.add(new Entry(19f, 2));
yvalues.add(new Entry(22f, 3));
yvalues.add(new Entry(20f, 4));
//yvalues.add(new Entry(17f, 5));
PieDataSet dataSet = new PieDataSet(yvalues, "Balances");
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("Monthly Expenses");
xVals.add("Phonepe");
xVals.add("Uber");
xVals.add("Paytm");
xVals.add("Savings");
// xVals.add("Ola");
dataSet.setDrawValues(false);
PieData data = new PieData(xVals, dataSet);
// In Percentage term
data.setValueFormatter(new PercentFormatter());
// Default value
//data.setValueFormatter(new DefaultValueFormatter(0));
pieChart.setData(data);
pieChart.setDescription("Savings");
pieChart.setDrawHoleEnabled(true);
pieChart.setTransparentCircleRadius(25f);
pieChart.setHoleRadius(25f);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
data.setValueTextSize(13f);
data.setValueTextColor(Color.DKGRAY);
pieChart.setOnChartValueSelectedListener(this);
pieChart.animateXY(1400, 1400);
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if (e == null)
return;
Log.i("VAL SELECTED",
"Value: " + e.getVal() + ", xIndex: " + e.getXIndex()
+ ", DataSet index: " + dataSetIndex);
float datatt = e.getVal();
Toast.makeText(this, ""+datatt, Toast.LENGTH_SHORT).show();
datatext.setText(""+datatt);
//datatitle.setText(""+title);
}
如何获取图表的文本值并在不同的文本视图上显示它
datatext.setText(pieChart.getXAxis().getValues().get(e.getXIndex()));
或
Sting data = ArrayList.get((int) e.getX());