ChristianR
ChristianR

Reputation: 75

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

Upvotes: 7

Views: 2119

Answers (2)

Justlove
Justlove

Reputation: 1

When you want to touch and long click, you need to set the:

mRenderer.setOnClickable(false)

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

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.

Upvotes: 2

Related Questions