在MPAndroid图表库中,如何在条形图中的x轴或y轴项目中单击每个项目?
例如:如果条形图的x轴为条形图的1,2,3,4,5,6,7和y轴,则为100,200,300,400,500,600,700
如果单击1或100,则应打开一个活动, 如果点击2或200,它应该打开另一个活动,依此类推..
MainActivity.java
public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BarChart barChart = (BarChart) findViewById(R.id.chart);
barChart.setHighlightPerTapEnabled(false);
barChart.setDescription("");
// set xaxis at bottom
barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
// set xaxis
barChart.getXAxis().setGridColor(Color.rgb(255,255,255));
// set x axis text color
barChart.getXAxis().setTextColor(Color.argb(42,44,44,44));
// HorizontalBarChart barChart= (HorizontalBarChart) findViewById(R.id.chart);
barChart.getAxisRight().setEnabled(false);
barChart.setScaleEnabled(false);
barChart.getLegend().setEnabled(false);
barChart.setTouchEnabled(true);
barChart.setDragEnabled(false);
barChart.setScaleEnabled(false);
barChart.setScaleXEnabled(false);
barChart.setScaleYEnabled(false);
barChart.setPinchZoom(false);
barChart.setHighlightPerDragEnabled(false);
barChart.setHighlightPerTapEnabled(false);
barChart.setDrawGridBackground(false);
barChart.setGridBackgroundColor(Color.rgb(255,255,255));
barChart.getXAxis().setGridColor(Color.rgb(255,255,255));
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(100, 0));
entries.add(new BarEntry(200, 1));
entries.add(new BarEntry(300, 2));
entries.add(new BarEntry(400, 3));
entries.add(new BarEntry(500, 4));
entries.add(new BarEntry(600, 5));
entries.add(new BarEntry(700, 6));
barChart.setOnChartValueSelectedListener(this);
BarDataSet dataset = new BarDataSet(entries, "# of Calls");
ArrayList<String> labels = new ArrayList<String>();
labels.add("1");
labels.add("2");
labels.add("3");
labels.add("4");
labels.add("5");
labels.add("6");
labels.add("7");
BarData data = new BarData(labels, dataset);
dataset.setColor(Color.rgb(33,200,215));
barChart.setData(data);
barChart.animateY(5000);
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
@Override
public void onNothingSelected() {
}
}
答案 0 :(得分:1)
The answer should be like this (for an older version of MPAndroidChart):
By e.getXIndex();
we can find x-Axis index then put condition on it. Every individual item click goes to different activity.
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
int x = e.getXIndex();
if (x == 0) {
Intent j = new Intent(MainActivity.this, Second.class);
startActivity(j);
}
else if (x==1) {
Intent i= new Intent(MainActivity.this, Third.class);
startActivity(i);
}
else if (x==2) {
Intent k= new Intent(MainActivity.this, Fourth.class);
startActivity(k);
}
else if (x==3) {
Intent k1 = new Intent(MainActivity.this, Fifth.class);
startActivity(k1);
}
else if (x==4) {
Intent k2 = new Intent(MainActivity.this, Sixth.class);
startActivity(k2);
}
else if (x==5) {
Intent k3 = new Intent(MainActivity.this, Seventh.class);
startActivity(k3);
}
else if (x==6) {
Intent k4 = new Intent(MainActivity.this, Eight.class);
startActivity(k4);
}
}
答案 1 :(得分:0)
请花一些时间阅读MPAndroidChart的wiki和javadoc以了解基本概念。请记住,您可以在PC上使用 Ctr-Q 或在Mac上使用 Cmd-J 来获取Android Studio中光标所在类的工具提示。
MPAndroidChart 3.0.2中的解决方案如下所示:
@Override
public void onValueSelected(Entry e, Highlight h) {
float x = e.getX(); //get the x value
int roundedX = (int) Math.round(x); //round it to an integer
Intent nextActivity;
if (roundedX == 1) {
nextActivity = new Intent(MainActivity.this, FirstActivity.class);
startActivity(nextActivity);
}
}
更智能的解决方案是将类对象存储在数组或列表中而不是使用条件,但最好先尝试简单。请花更多时间学习Java和Android的基本概念,否则你会发现很难取得进展。