在触摸时将点添加到散点图但它仅适用于第一个触摸事件(achartengine)

时间:2014-03-12 09:05:35

标签: android achartengine ontouchevent

这是我的OnTouchListener()

private GraphicalView mChart;

mChart.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP)
                {
                      xy = mChart.toRealPoint(0);
                          flagtouch =1;
                          seriestouch.add(xy[0],xy[1]);
                          addseries();
                          view.removeView(mChart);
                          mChart=null;
                      drawgraph(ch); 
                }
                    return false;
            }
     });

addseries功能如下: -

private void addseries()
{
            XYSeries series = new XYSeries("Series 1");
                for(int i=0;i<size;i++)
                {
                    series.add(value1[i],value2[i]);
                }
                dataset = new XYMultipleSeriesDataset();
                dataset.addSeries(series);
                if(flagtouch==1)
                {
                dataset.addSeries(seriestouch);
                }
    }

和drawgraph函数如下: -

    private void drawgraph()
     {       

           mRenderer = new XYMultipleSeriesRenderer();
           XYSeriesRenderer renderer = new XYSeriesRenderer();
            renderer.setColor(Color.RED);
            renderer.setPointStyle(PointStyle.CIRCLE);
            renderer.setFillPoints(true);
            mRenderer.addSeriesRenderer(renderer);
             if(flagtouch==1)
              {
                    XYSeriesRenderer renderer1 = new XYSeriesRenderer();
                    renderer1.setColor(Color.YELLOW);
                    renderer1.setPointStyle(PointStyle.CIRCLE);
                    renderer1.setFillPoints(true);
                    mRenderer.addSeriesRenderer(renderer1);
              }
            mRenderer.setXTitle(colnam.get(0));
            mRenderer.setYTitle(colnam.get(1));
            mRenderer.setShowLegend(false);
            mRenderer.setMargins(new int[]{10,45,60,0});
            title.setText("Scatter Plot");
            mChart = ChartFactory.getScatterChartView(this, dataset, mRenderer);
            view.addView(mChart);
    }

新点显示在图表上,但仅针对第一个触摸事件,对于后续触摸事件,touchlistener()根本没有被调用我使用Log检查了这一点,并且如果我删除了语句&#34; mChart = ChartFactory.getScatterChartView(this,dataset,mRenderer);&#34;每当flagtouch = 1时,就会在每次触摸时调用touchlistener(),但由于图中没有任何变化,因此无用。

1 个答案:

答案 0 :(得分:0)

试试这个

mChart.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    double[] xy = mChart.toRealPoint(0);
                series.add(xy[0], xy[1]);
                dataset.clear();
                    dataset.addSeries(series);
                    mChart.repaint();
                }
                return false;
            }
        });