Reputation: 947
I am using latest release of mpandroidchart library. I have 2 bar charts on single activity. chart1 & chart2 are the id's in XML (I don't want to use barchart list view). chart1 cosnist Counts value & chart2 consist dollars value. I am getting the values already. But I want to know that is it a dollar value or counts value. so I can display the toast according to chart selected.
This is my Sample code.
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
View view;
TextView text;
switch (e.getXIndex()) {
case 0:
if (toast != null)
toast.cancel();
toast = Toast.makeText(getActivity(), "All Other Year Defectors: " +e.getVal(), Toast.LENGTH_SHORT);
view = toast.getView();
view.setBackgroundResource(R.color.all_odr_yr);
toast.setGravity(Gravity.TOP, 0, 950);
toast.show();
break;
case 1:
if (toast != null)
toast.cancel();
toast = Toast.makeText(getActivity(), "Last Year Defectors: " + e.getVal(), Toast.LENGTH_SHORT);
view = toast.getView();
view.setBackgroundResource(R.color.lst_yr_df);
toast.setGravity(Gravity.TOP, 0, 950);
toast.show();
break;
Upvotes: 4
Views: 2187
Reputation: 989
If you do not want to use inline listeners as Philip mentioned in his answer you can create a class implementing onChartValueSelectedListener
and identify each chart with an ID.
private class CustomOnValueSelectedListener implements OnChartValueSelectedListener {
private int CHART_ID;
public CustomOnValueSelectedListener() {}
public CustomOnValueSelectedListener(int chart_id) {
CHART_ID = chart_id;
}
@Override
public void onValueSelected(Entry e, Highlight h) {
switch (CHART_ID) {
case PIE_CHART_ID:
break;
case BAR_CHART_ID:
break;
case LINE_CHART_ID:
break;
default:
//common code
break;
}
}
@Override
public void onNothingSelected() {
}
}
You can now do this -
pieChart.setOnChartValueSelectedListener(new CustomOnValueSelectedListener(PIE_ID));
barChart.setOnChartValueSelectedListener(new CustomOnValueSelectedListener(BAR_ID));
lineChart.setOnChartValueSelectedListener(new CustomOnValueSelectedListener(LINE_ID));
someOtherChart.setOnChartValueSelectedListener(new CustomValueSelectedListener());
where PIE_ID
, LINE_ID
and BAR_ID
are some unique integers.
This way your code is concise in case you have more than 3-4 charts to deal with else inline listeners are better.
Upvotes: 1
Reputation: 51421
That seems to be quite difficult and hard to acheive with the library alone.
But what you could do is inline the listeners and use a separate listener for each chart, like this:
countChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// COUNT CHART VALUE SELECTED
}
@Override
public void onNothingSelected() { }
});
dollarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// DOLLAR CHART VALUE SELECTED
}
@Override
public void onNothingSelected() { }
});
In that way you can differentiate between the different charts.
Upvotes: 7