gpt4 book ai didi

java - 如何从饼图中获取文本

转载 作者:行者123 更新时间:2023-12-02 00:39:35 25 4
gpt4 key购买 nike

我使用了 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);
}

如何获取图表的文本值并将其显示在不同的 TextView 上

最佳答案

datatext.setText(pieChart.getXAxis().getValues().get(e.getXIndex()));

Sting data = ArrayList.get((int) e.getX());

关于java - 如何从饼图中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57953640/

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