Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get which chart is selected on activity

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;
like image 557
Akshay Shinde Avatar asked Mar 16 '23 19:03

Akshay Shinde


1 Answers

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.

like image 88
Philipp Jahoda Avatar answered Mar 29 '23 10:03

Philipp Jahoda