setOnTouchListener acharenginebarchart

时间:2013-03-18 21:38:19

标签: android charts achartengine

我希望能够点击图表来访问另一个布局,但我不能。我输入的代码是好的,直到我输入以下内容:

mChartView.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {

                pointx = (int)event.getX();
                pointy = (int)event.getY();


                Toast.makeText(MainActivity.this, "x="+Integer.toString(pointx)+"y="+Integer.toString(pointy), Toast.LENGTH_SHORT).show();
                return false;
            }});

一旦这给了我一个重大错误,不会让我运行程序。请一位专家帮助我绘图。提前问候 我也留下所有代码

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.ChartFactory;

import org.achartengine.GraphicalView;

import org.achartengine.chart.PointStyle;

//import com.artfulbits.aiCharts.ChartView;


import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.Toast;
import android.content.Intent;




public class MainActivity extends Activity {
    private GraphicalView mChartView;
    private int pointx;
    private int pointy;

    //me declaro un string con los meses del año
    private String[] mYear = new String[] {
                "2005", "2006" , "2007", "2008", "2009", "2010",
                "2011", "2012" , "2013"
            };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        XYSeries incomeSeries = new XYSeries("AÑOS");

        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();




        //mChartView xv =(mChartView)findViewById(R.id.LinearLayout01);


        int[] x = { 0,1,2,3 };
        int[] income = { 10,12,14,10};
        //XYSeries encapsula los valores de los gráficos XY como la línea, tiempo, superficie, dispersión 


        //añado dato a la categoria 1
        for(int i=0;i<x.length;i++)
        {
            incomeSeries.add(i,income[i]);//0 de 2000

            //float ancho = incomeRenderer.getLineWidth();

        }
        dataset.addSeries(incomeSeries);

        // creacion de un conjunto xa cada serie

        // Adding Income Series to the dataset

        // Creating XYSeriesRenderer to customize incomeSeries

        incomeRenderer.setColor(Color.WHITE);//rgb(130, 360, 230)130, 130, 230
        incomeRenderer.setFillPoints(true);//ESTABLECER relleno
        incomeRenderer.setLineWidth(1);//ancho = 2
        incomeRenderer.setDisplayChartValues(true);//establece valores de visualización de las gráficas

        // definir titulo y demas

        multiRenderer.setXLabels(0);//0
        multiRenderer.setChartTitle("AÑOS");
        multiRenderer.setXTitle("años ventas");
        multiRenderer.setYTitle("Cantidad en euros");
        multiRenderer.setZoomButtonsVisible(true); 
        multiRenderer.addSeriesRenderer(incomeRenderer);
        multiRenderer.setClickEnabled(true);

        multiRenderer.isClickEnabled();


        for(int i=0; i< x.length;i++){
            multiRenderer.addXTextLabel(i, mYear[i]);  


        }       
        //setontouch

        mChartView.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {

                pointx = (int)event.getX();
                pointy = (int)event.getY();


                Toast.makeText(MainActivity.this, "x="+Integer.toString(pointx)+"y="+Integer.toString(pointy), Toast.LENGTH_SHORT).show();
                return false;
            }});



        //prueba1
        // Creating an intent to plot bar chart using dataset and multipleRenderer      
        Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);

        // Start Activity
        startActivity(intent);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



}

1 个答案:

答案 0 :(得分:0)

我建议你看看this示例,了解如何处理图表监听器。

另请查看this文章,了解有关AChartEngine的其他内容。