在android中钻取堆积条形图

时间:2013-08-21 05:19:32

标签: android

是否有任何图表库具有向下钻取功能? (可点击的事件)。我已经尝试使用onTouch在Achartengine中但是我无法获得SeriesSelection,它总是抛出null。 我使用的代码片段:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.barchart_show);

        barLayout = (LinearLayout) findViewById(R.id.barchart_main);

        stBarChart = new StackedBarChart();

        List<double[]> values = new ArrayList<double[]>();

        values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200, 22030, 21200, 19500, 15500,

            12600, 14000 });

        values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030, 11200, 9500, 10500,
                11600, 13500 });


       barChartView = stBarChart.execute(values, getApplicationContext());

       if(barChartView!=null) {
           barChartView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                 SeriesSelection seriesSelection = barChartView.getCurrentSeriesAndPoint();

                  double[] xy = barChartView.toRealPoint(0);

                  if (seriesSelection == null) {

                      Toast.makeText(BarChartShow.this, "No chart element was clicked", Toast.LENGTH_SHORT)
                        .show();

                  } else {

                      Toast.makeText(
                            BarChartShow.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();
                  }

                return true;
            }
        });
           barLayout.addView(barChartView, new LayoutParams(LayoutParams.FILL_PARENT,
                      LayoutParams.WRAP_CONTENT));
           }
        }

public GraphicalView execute(List values,Context context){

    String[] titles = new String[] { "2008", "2007" };



    int[] colors = new int[] { Color.BLUE, Color.CYAN };
    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);

    renderer.setOrientation(Orientation.VERTICAL);
    setChartSettings(renderer, "Monthly sales in the last 2 years", "Month", "Units sold", 0.5,
        12.5, 0, 24000, Color.GRAY, Color.LTGRAY);

    renderer.getSeriesRendererAt(0).setDisplayChartValues(true);

    renderer.getSeriesRendererAt(1).setDisplayChartValues(true);

    //renderer.getSeriesRendererAt(2).setDisplayChartValues(true);
    renderer.setXLabels(12);
    renderer.setYLabels(10);
    renderer.setXLabelsAlign(Align.LEFT);
    renderer.setYLabelsAlign(Align.LEFT);
    renderer.setPanEnabled(true, false);
    // renderer.setZoomEnabled(false);
    renderer.setZoomRate(1.1f);
    renderer.setBarSpacing(0.5f);
    renderer.setClickEnabled(true);

    barChartView = ChartFactory.getBarChartView(context, buildBarDataset(titles, values),

            renderer, Type.STACKED);

    return barChartView;
}

0 个答案:

没有答案