Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android achartengine - How to recognize which bar is touched in a bar chart?

Iam working on an android app and using AChartEngine for bar charting. Everything is working as it should except that I can't figure out which bar in the graph is touched (not clicked). It seems that .getCurrentSeriesAndPoint() isn't working within the OnTouchListener. It's everytime returning NULL. When I'm using OnClickListener everything is working fine, but I need to know which bar is touched (Action_Down and Action_Up) not clicked.

Is .getCurrentSeriesAndPoint() not working in a TouchListener in general? Is there a workaround or another way to realize which bar is touched?

mChartView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();


            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:{
                        Toast.makeText(MainActivity.this, "DOWN", Toast.LENGTH_SHORT) .show();                          
                        if(seriesSelection != null) touchedBar = seriesSelection.getPointIndex();
                        break;}

                case MotionEvent.ACTION_UP:{
                        Toast.makeText(MainActivity.this, "UP", Toast.LENGTH_LONG) .show();                         
                        touchedBar = 0;
                        break;}
                default: break;
            }

            mRenderer.removeAllRenderers(); 
            r.setColor(Color.RED);
            r.setSelectedBar(ClickedBar);
            mRenderer.addSeriesRenderer(r);                 
            mChartView.repaint();

            return true;
        }
    });

kind regards Christian

like image 863
ChristianR Avatar asked Oct 22 '12 12:10

ChristianR


1 Answers

It should work with the touch listener. However, you still have to do this:

mRenderer.setClickEnabled(true);

AChartEngine uses the onTouchEvent internally, so it is possible that your touch event is called after the ACE one, so this may be the cause for not getting the expected values.

like image 144
Dan D. Avatar answered Oct 26 '22 09:10

Dan D.