我想获取x条目的值,当单击某个扇区时,该条目是一个字符串。怎么样?我可以使用getVal()得到y值,但x怎么样? x轴条目的值来自sqlite。代码如下所示
public class chart extends Activity {
private FrameLayout chartContainer;
private PieChart mChart;
Context CTX = this;
DatabaseOperations DOP;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chart);
chartContainer=(FrameLayout) findViewById(R.id.chartContainer);
mChart=new PieChart(this);
//add pie chart to the layout
chartContainer.addView(mChart);
//enable hole and configure
mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { if (e == null)
return;
Toast.makeText(chart.this, xVals.get(e.getXIndex()) + " used " + e.getVal() + "kwh", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected() {
}
});
//add data
addeachData();
Legend l=mChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
}
protected void addeachData() {
DatabaseOperations DOP = new DatabaseOperations(CTX);
Cursor CR = DOP.getInformation(DOP);
int count=CR.getCount();
float[]values=new float[count];
String[]name=new String[count];
for (int m=0; m<count; m++){
CR.moveToNext();
name[m]=CR.getString(0);
values[m]=CR.getFloat(1);
}
ArrayList<Entry> yVals = new ArrayList<Entry>();
for (int i = 0; i <name.length; i++)
yVals.add(new Entry(values[i], i));
final ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i <name.length; i++)
xVals.add(name[i]);
ArrayList<Integer> colors=new ArrayList<Integer>();
for(int c: ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
PieData data=new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
mChart.setData(data);
mChart.highlightValues(null);
mChart.invalidate();
}
}
答案 0 :(得分:0)
首先,您声明了一个像“xVals”这样的ArrayList来保存x轴值。将其更改为final,以便可以从内部类访问它。
final ArrayList<String> xVals = new ArrayList<String>();
然后使用条目对象中的getXIndex()方法。例如:
xVals.get(entry.getXIndex());