使用aChartengine的折线图

时间:2012-02-13 10:18:28

标签: android

我想在我的应用程序中使用折线图,我想在设置为线性布局1的背景图像的图像下方显示它,在第二个线性布局中我想添加折线图,我正在使用achart引擎库。第66行的空指针异常(layout.addView(mChartView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));)请帮忙

    package com.android.linegraph;

    import java.util.ArrayList;
    import java.util.List;

    import org.achartengine.ChartFactory;
    import org.achartengine.GraphicalView;
    import org.achartengine.chart.PointStyle;
    import org.achartengine.model.XYMultipleSeriesDataset;
    import org.achartengine.model.XYSeries;
    import org.achartengine.renderer.XYMultipleSeriesRenderer;
    import org.achartengine.renderer.XYSeriesRenderer;

    import android.app.Activity;
    import android.graphics.Color;
    import android.graphics.Paint.Align;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;

    public class LineGraph extends Activity {
        private GraphicalView mChartView;

        XYMultipleSeriesRenderer renderer;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout2);

            String[] titles = new String[] { "spens1" };
            List<double[]> x = new ArrayList<double[]>();
            x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
            // }
            List<double[]> values = new ArrayList<double[]>();
            values
                    .add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13,
                            10 });
            int[] colors = new int[] { Color.BLUE };
            PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };

            renderer = buildRenderer(colors, styles);
            int length = renderer.getSeriesRendererCount();
            for (int i = 0; i < length; i++) {
                ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
                        .setFillPoints(true);
            }
            setChartSettings(renderer, "SCORE CARD VIEW", "Attetempts",
                    "Marks Scored", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY);
            renderer.setXLabels(12);
            renderer.setYLabels(10);
            renderer.setShowGrid(true);
            renderer.setXLabelsAlign(Align.RIGHT);
            renderer.setYLabelsAlign(Align.RIGHT);
            renderer.setZoomButtonsVisible(true);
            renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
            renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });

            if (mChartView == null) {
                Log.d("Oncreate ", "if (mChartView == null)");
                mChartView = ChartFactory.getLineChartView(this, mDataset(titles,
                        x, values), renderer);
                layout.addView(mChartView, new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

            } else {
    //          mChartView.repaint();
                Log.d("Oncreate ", "if (mChartView != null)");
            }
        }

        private void setChartSettings(XYMultipleSeriesRenderer renderer3,
                String title, String xTitle, String yTitle, double xMin,
                double xMax, double yMin, double yMax, int axesColor,
                int labelsColor) {
            // TODO Auto-generated method stub

            renderer3.setChartTitle(title);
            renderer3.setXTitle(xTitle);
            renderer3.setYTitle(yTitle);
            renderer3.setXAxisMin(xMin);
            renderer3.setXAxisMax(xMax);
            renderer3.setYAxisMin(yMin);
            renderer3.setYAxisMax(yMax);
            renderer3.setAxesColor(axesColor);
            renderer3.setLabelsColor(labelsColor);

        }

        private XYMultipleSeriesRenderer buildRenderer(int[] colors,
                PointStyle[] styles) {
            // TODO Auto-generated method stub
            XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
            setRenderer(renderer, colors, styles);
            return renderer;
        }

        private void setRenderer(XYMultipleSeriesRenderer renderer2, int[] colors,
                PointStyle[] styles) {
            // TODO Auto-generated method stub
            renderer2.setAxisTitleTextSize(16);
            renderer2.setChartTitleTextSize(20);
            renderer2.setLabelsTextSize(15);
            renderer2.setLegendTextSize(15);
            renderer2.setPointSize(5f);
            renderer2.setMargins(new int[] { 20, 30, 15, 20 });
            int length = colors.length;
            for (int i = 0; i < length; i++) {
                XYSeriesRenderer r = new XYSeriesRenderer();
                r.setColor(colors[i]);
                r.setPointStyle(styles[i]);
                renderer2.addSeriesRenderer(r);
            }
        }

        private XYMultipleSeriesDataset mDataset(String[] titles,
                List<double[]> xValues, List<double[]> yValues) {
            // TODO Auto-generated method stub
            XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
            addXYSeries(dataset1, titles, xValues, yValues, 0);
            return dataset1;
        }

        private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
                List<double[]> xValues, List<double[]> yValues, int scale) {
            // TODO Auto-generated method stub

            int length = titles.length;
            for (int i = 0; i < length; i++) {
                XYSeries series = new XYSeries(titles[i], scale);
                double[] xV = xValues.get(i);
                double[] yV = yValues.get(i);
                int seriesLength = xV.length;
                for (int k = 0; k < seriesLength; k++) {
                    series.add(xV[k], yV[k]);
                }
                dataset.addSeries(series);
            }

        }

    }

<RelativeLayout android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:id="@+id/linearLayout1"
        android:background="@drawable/cricket" android:layout_alignParentLeft="true"
        android:layout_height="wrap_content" android:layout_width="wrap_content"></LinearLayout>

    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_below="@id/linearLayout1"
        android:layout_alignParentLeft="true" android:layout_height="wrap_content"
        android:layout_width="wrap_content"></LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

我现在无法在实践中测试这个,但我认为你错过了对

的调用
 setContentView(R.layout.NAME_OF_YOUR_XML_FILE);

这可能是例如在这一行之后:

 super.onCreate(savedInstanceState);

如果没有这个,layout变量为空,从而导致异常(我猜)。