Reputation: 51
In MPAndroid chart library, How to make clickable every individual item in x-axis or y- axis item in bar chart ?
For a Example : if in x axis of bar chart is as 1,2,3,4,5,6,7 and y axis of bar chart 100,200,300,400,500,600,700
If click on 1 or 100 it should open one activity, If click on 2 or 200 it should open another activity and so on ..
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() {
}
}
Upvotes: 0
Views: 874
Reputation: 21487
Please spend some time reading the wiki and javadoc for MPAndroidChart to understand the basic concepts. Remember, you can use Ctr-Q on PC or Cmd-J on Mac to get the tooltip for the the class you have the cursor on in Android Studio.
The solution in MPAndroidChart 3.0.2 looks something like this:
@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);
}
}
A smarter solution would store the class objects in an array or list rather than use conditionals but it's best to attempt the simple first. Please spend some more time learning the basic concepts in Java and Android otherwise you will find it very hard to make progress.
Upvotes: 0
Reputation: 51
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);
}
}
Upvotes: 1