aChartEngine, GraphicalView OnClickListener not working

南笙酒味 提交于 2019-12-01 23:02:12

问题


I'm new to android and am using aChartEngine to create a bar chart. I want to capture the x and y values when a user clicks on the chart. I have looked at the demos from aChartEngine and have my chart creating fine. However the onClickListner does not work when I click on the graphy. I have defined the listener in the OnResume method but it doesn't work. Any ideas would be greatly appreciated. Am I missing something here?

Here is my OnResume method which is taken from XYChartBuilder

@Override
protected void onResume() {
  super.onResume();
Toast.makeText(Clickable2.this, "In OnResume", Toast.LENGTH_SHORT).show();

  if (mChartView == null) {     

Toast.makeText(Clickable2.this, "mChartView is null", Toast.LENGTH_SHORT).show();      

    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    mChartView = ChartFactory.getBarChartView(this,mDatasetMethod(titles,x,values),renderer,Type.DEFAULT);
    renderer.setClickEnabled(true);
    renderer.setSelectableBuffer(100);

    //OnClickListener
    mChartView.setOnClickListener(new View.OnClickListener() {
     // @Override
      public void onClick(View v) {
 Toast.makeText(Clickable2.this, "ON CLICK", Toast.LENGTH_SHORT).show();              
        SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
        double[] xy = mChartView.toRealPoint(0);
        if (seriesSelection == null) {
          Toast.makeText(Clickable2.this, "No chart element was clicked", Toast.LENGTH_SHORT)
              .show();
        } else {
          Toast.makeText(
                  Clickable2.this,
              "Chart element in series index " + seriesSelection.getSeriesIndex()
                  + " data point index " + seriesSelection.getPointIndex() + " was clicked"
                  + " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
                  + " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
        }
      }
    });

    //LongClickListener
    mChartView.setOnLongClickListener(new View.OnLongClickListener() {
    //  @Override
      public boolean onLongClick(View v) {
        SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
  Toast.makeText(Clickable2.this, "ON LONG CLICK", Toast.LENGTH_SHORT).show();   
        if (seriesSelection == null) {
          Toast.makeText(Clickable2.this, "No chart element was long pressed",
              Toast.LENGTH_SHORT);
          return false; // no chart element was long pressed, so let something
          // else handle the event
        } else {
          Toast.makeText(Clickable2.this, "Chart element in series index "
              + seriesSelection.getSeriesIndex() + " data point index "
              + seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
          return true; // the element was long pressed - the event has been
          // handled
        }
      }
    });

    mChartView.addZoomListener(new ZoomListener() {
      public void zoomApplied(ZoomEvent e) {
        String type = "out";
        if (e.isZoomIn()) {
          type = "in";
        }
        System.out.println("Zoom " + type + " rate " + e.getZoomRate());
      }

      public void zoomReset() {
        System.out.println("Reset");
      }
    }, true, true);

    mChartView.addPanListener(new PanListener() {
      public void panApplied() {
        System.out.println("New X range=[" + renderer.getXAxisMin() + ", " + renderer.getXAxisMax()
            + "], Y range=[" + renderer.getYAxisMax() + ", " + renderer.getYAxisMax() + "]");
      }
    });

    layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    boolean enabled = mDataset.getSeriesCount() > 0;
  } else {
    mChartView.repaint();
 Toast.makeText(Clickable2.this, "mChartView is NOT null", Toast.LENGTH_SHORT).show();  
  }
} 

回答1:


Can you check your code for some definition of an object like XYMultipleSeriesRenderer? Maybe you forgot to set the setClickEnabled method to true?!

Or maybe you should try:

setOnTouchListener 

with the method:

public boolean onTouch(View v, MotionEvent event)

It works for me.




回答2:


Change the value of parameter of setSelectableBuffer method to a bigger number.



来源:https://stackoverflow.com/questions/9775909/achartengine-graphicalview-onclicklistener-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!