7

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

ChristianR
  • 75
  • 4

2 Answers2

2

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.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • Thanks, but I already set mRenderer.setClickEnabled(true) earlier in my code. And its still not working with the touch listener. I don't get a value from .getCurrentSeriesAndPoint(). – ChristianR Oct 23 '12 at 09:55
  • set OnClickListener instead of OnTouchListener. – Kolchuga Apr 07 '14 at 15:28
0

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

mRenderer.setOnClickable(false)
Kristján
  • 18,165
  • 5
  • 50
  • 62
Justlove
  • 1
  • 2