2

我是 AchartEngine 的新手。我正在使用带有 android 的 AchartEngine 创建条形图。我查看了 aChartEngine API 并创建了一个条形图,它工作正常。

  1. 当我想查看我的实际视图时,我必须通过单击右下角的缩放按钮来降低缩放率。我需要在不使用缩放按钮的情况下显示我在程序中准确声明的完整条形图。
  2. 我需要从一个视图导航到另一个视图。所以,我用 ontouchlistner 创建了一个图形视图,但它显示错误。

任何想法将不胜感激。我在这里错过了什么吗?

public class GraphicViewExample extends Activity {
  private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
            "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
    int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };
    public static final String TYPE = "type";
    private XYMultipleSeriesDataset mDataset = getDemoDataset();
    private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
    private GraphicalView mChartView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graphicviewexample);
        if (mChartView == null) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
            mChartView = ChartFactory.getBarChartView(this, mDataset,
                    mRenderer, Type.DEFAULT);
            mRenderer.setSelectableBuffer(100);
            layout.addView(mChartView, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        } else {
            mChartView.repaint();
        }

        mChartView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                @SuppressWarnings("unused")
                /*SeriesSelection seriesSelection = mChartView
                        .getCurrentSeriesAndPoint();*/
                SimpleSeriesRenderer r = new SimpleSeriesRenderer();
                mRenderer.removeAllRenderers();
                r.setColor(Color.RED);
                mChartView.repaint();
                return true;
            }
        });

        /*mChartView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                 * LinearLayout slayout = (LinearLayout)
                 * findViewById(R.id.chart); ChartView =
                 * ChartFactory.getBarChartView( getApplicationContext(),
                 * sDataset, smRenderer, Type.DEFAULT);
                 * slayout.addView(ChartView, new LayoutParams(
                 * LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                 * 
                 * ChartView.repaint();

                mChartView.repaint();
            }
        });*/
    }

    private XYMultipleSeriesDataset getDemoDataset() {

        XYSeries incomeSeries = new XYSeries("Income");
        for (int i = 0; i < x.length; i++) {
            incomeSeries.add(i, income[i]);
        }

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(incomeSeries);
        return dataset;

    }

    private XYMultipleSeriesRenderer getDemoRenderer() {
        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
        incomeRenderer.setColor(Color.rgb(130, 130, 230));
        incomeRenderer.setFillPoints(true);
        incomeRenderer.setLineWidth(2);
        incomeRenderer.setDisplayChartValues(true);

        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setXLabels(0);
        multiRenderer.setBarSpacing(0.3f);
        multiRenderer.setBarWidth(30);
        multiRenderer.setChartTitle("Income Chart");
        multiRenderer.setXTitle("Year 2013");
        multiRenderer.setYTitle("Amount in Dollars");
        //multiRenderer.setZoomLimits(1.0,0.7,1.0,3000.0);
        multiRenderer.setZoomButtonsVisible(true);
        //multiRenderer.setZoomEnabled(true, true);
        //multiRenderer.setPanEnabled(true, true);
        multiRenderer.setInScroll(true);
        multiRenderer.setXAxisMin(0);
        multiRenderer.setXAxisMax(7);
        multiRenderer.setYAxisMin(0);
        multiRenderer.setYAxisMax(4000);
        multiRenderer.setClickEnabled(true);
        multiRenderer.setShowGridX(true);
        for (int i = 0; i < x.length; i++) {
            multiRenderer.addXTextLabel(i, mMonth[i]);
        }

        multiRenderer.addSeriesRenderer(incomeRenderer);
        return multiRenderer;
    }

}

日志:java.lang.IndexOutOfBoundsException:无效索引 0,大小为 0

在 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

在 org.achartengine.renderer.DefaultRenderer.getSeriesRendererAt(DefaultRenderer.java:189)

在 org.achartengine.chart.XYChart.draw(XYChart.java:240)

在 org.achartengine.GraphicalView.onDraw(GraphicalView.java:168)

4

2 回答 2

2

此代码适用于我的折线图,

    v.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    SeriesSelection seriesSelection = ((GraphicalView) v)
                            .getCurrentSeriesAndPoint();

                    Log.d("sreedhu", String.valueOf(seriesSelection));
                    if (seriesSelection == null) {
                        Log.d("sreedhu", "Nothing Selected");

                    } else {
//your code}
}
}
于 2013-07-26T10:56:30.570 回答
0

使用此代码缩放您的条形图...!

renderer.setScale((float) 1);
于 2013-07-26T10:17:27.640 回答