dataset and renderer should not be null and should have same number of series in AChartEngine

丶灬走出姿态 提交于 2019-12-20 06:34:41

问题


I'm using Multiple Y axis Graph for plotting points

I have 1)X-axis 2)Y1-axis 3)Y2-axis

a)Initially I used to draw 5 series of lines on Y1 axes(Height) and Same 5 series of lines(Weight) on Y2 axes both with respect to x-axis,,,,No problem exists here,,,these series of lines are standard lines and used for every calculation,,

b)Now If user enters both his height and weight I draw two extra Lines ,so there are 12 series of lines including standard lines,,

c)Supppose if the user enters alone Height but not weight the app force closes,,saying dataset and renderer should not be null and should have same number of series

code follows,,


public GraphicalView execution(Context context, String UserName,
            String HeightRepresentation, String WeightRepresentation,
            ArrayList<Double> mHeightValueCms,
            ArrayList<Double> mWeightValueKg,
            ArrayList<Double> mHeightInCmsMonthDate,
            ArrayList<Double> mWeightInKgMonthDate,
            ArrayList<Double> mHeightMonth97, ArrayList<Double> mHeightMonth75,
            ArrayList<Double> mHeightMonth50, ArrayList<Double> mHeightMonth25,
            ArrayList<Double> mHeightMonth3, ArrayList<Double> mWeightMonth97,
            ArrayList<Double> mWeightMonth75, ArrayList<Double> mWeightMonth50,
            ArrayList<Double> mWeightMonth25, ArrayList<Double> mWeightMonth3) {

XYMultipleSeriesRenderer renderer = null;
    String[] titles = new String[] { "97%", "75%", "50%", "25%", "3% " };
    List<double[]> x = new ArrayList<double[]>();
    List<double[]> x1 = new ArrayList<double[]>();
    for (int i = 0; i < titles.length; i++) {
        x.add(new double[] { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 });
    }

    List<double[]> values = new ArrayList<double[]>();
    values.add(new double[] { mHeightMonth97.get(0), mHeightMonth97.get(1),
            mHeightMonth97.get(2), mHeightMonth97.get(3),
            mHeightMonth97.get(4), mHeightMonth97.get(5),
            mHeightMonth97.get(6), mHeightMonth97.get(7),
            mHeightMonth97.get(8), mHeightMonth97.get(9),
            mHeightMonth97.get(10), mHeightMonth97.get(11),
            mHeightMonth97.get(12) });
    values.add(new double[] { mHeightMonth75.get(0), mHeightMonth75.get(1),
            mHeightMonth75.get(2), mHeightMonth75.get(3),
            mHeightMonth75.get(4), mHeightMonth75.get(5),
            mHeightMonth75.get(6), mHeightMonth75.get(7),
            mHeightMonth75.get(8), mHeightMonth75.get(9),
            mHeightMonth75.get(10), mHeightMonth75.get(11),
            mHeightMonth75.get(12) });
    values.add(new double[] { mHeightMonth50.get(0), mHeightMonth50.get(1),
            mHeightMonth50.get(2), mHeightMonth50.get(3),
            mHeightMonth50.get(4), mHeightMonth50.get(5),
            mHeightMonth50.get(6), mHeightMonth50.get(7),
            mHeightMonth50.get(8), mHeightMonth50.get(9),
            mHeightMonth50.get(10), mHeightMonth50.get(11),
            mHeightMonth50.get(12) });
    values.add(new double[] { mHeightMonth25.get(0), mHeightMonth25.get(1),
            mHeightMonth25.get(2), mHeightMonth25.get(3),
            mHeightMonth25.get(4), mHeightMonth25.get(5),
            mHeightMonth25.get(6), mHeightMonth25.get(7),
            mHeightMonth25.get(8), mHeightMonth25.get(9),
            mHeightMonth25.get(10), mHeightMonth25.get(11),
            mHeightMonth25.get(12) });
    values.add(new double[] { mHeightMonth3.get(0), mHeightMonth3.get(1),
            mHeightMonth3.get(2), mHeightMonth3.get(3),
            mHeightMonth3.get(4), mHeightMonth3.get(5),
            mHeightMonth3.get(6), mHeightMonth3.get(7),
            mHeightMonth3.get(8), mHeightMonth3.get(9),
            mHeightMonth3.get(10), mHeightMonth3.get(11),
            mHeightMonth3.get(12) });
    int[] colors = null;
    PointStyle[] styles = null;
    if (!(mWeightInKgMonthDate.isEmpty() && mHeightInCmsMonthDate.isEmpty())) {
        renderer = new XYMultipleSeriesRenderer(12);
        colors = new int[] {
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                Color.GREEN,
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                Color.RED };
        styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.TRIANGLE, PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.TRIANGLE };
    } else

    if (!mWeightInKgMonthDate.isEmpty() && mHeightInCmsMonthDate.isEmpty()) {
        renderer = new XYMultipleSeriesRenderer(11);
        colors = new int[] {
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                Color.RED };
        styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.TRIANGLE };
    } else

    if (mWeightInKgMonthDate.isEmpty() && !mHeightInCmsMonthDate.isEmpty()) {
        renderer = new XYMultipleSeriesRenderer(11);
        colors = new int[] {
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                Color.GREEN,
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed) };
        styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.TRIANGLE, PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT };
    } else

    if (mWeightInKgMonthDate.isEmpty() && mHeightInCmsMonthDate.isEmpty()) {
        renderer = new XYMultipleSeriesRenderer(10);
        colors = new int[] {
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightGreen),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed),
                context.getResources().getColor(R.color.LightRed) };
        styles = new PointStyle[] { PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT, PointStyle.POINT,
                PointStyle.POINT, PointStyle.POINT };
    }


    setRenderer(renderer, colors, styles);
    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) {
        XYSeriesRenderer r = ((XYSeriesRenderer) renderer
                .getSeriesRendererAt(i));
        r.setFillPoints(true);
        r.setLineWidth(3f);
        // /-----
        r.setShowLegendItem(false);
    }
    if(HeightRepresentation.equals("HeightInCms") && WeightRepresentation.equals("WeightInKg")){
        setChartSettings(renderer, "Growth Chart For " + UserName, "Month",
                "Height in (cms)", 0, 36, 45, 180, Color.LTGRAY, Color.LTGRAY);
        renderer.setYTitle("Weight in (kg)", 1);
    }else if(HeightRepresentation.equals("HeightInFt") && WeightRepresentation.equals("WeightInKg")){
        setChartSettings(renderer, "Growth Chart For " + UserName, "Month",
                "Height in (ft)", 0, 36, 0, 8, Color.LTGRAY, Color.LTGRAY);
        renderer.setYTitle("Weight in (kg)", 1);
    }else if(HeightRepresentation.equals("HeightInCms") && WeightRepresentation.equals("WeightInLbs")){
        setChartSettings(renderer, "Growth Chart For " + UserName, "Month",
                "Height in (cms)", 0, 36, 45, 180, Color.LTGRAY, Color.LTGRAY);
        renderer.setYTitle("Weight in (lbs)", 1);
    }else if(HeightRepresentation.equals("HeightInFt") && WeightRepresentation.equals("WeightInLbs")){
        setChartSettings(renderer, "Growth Chart For " + UserName, "Month",
                "Height in (ft)", 0, 36, 0, 8, Color.LTGRAY, Color.LTGRAY);
        renderer.setYTitle("Weight in (lbs)", 1);
    }


    renderer.setXLabels(12);
    renderer.setYLabels(10);
    renderer.setShowGrid(true);
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.BLACK);
    // renderer.setPointSize(3);
    // renderer.setGridColor(Color.WHITE);

    renderer.setXLabelsAlign(Align.RIGHT);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setZoomButtonsVisible(true);
    // renderer.setPanLimits(new double[] { -50, 50, 0, 250 });
    // renderer.setZoomLimits(new double[] { -50, 50, 0, 250 });
    renderer.setZoomRate(1.05f);
    renderer.setLabelsColor(Color.WHITE);
    renderer.setXLabelsColor(Color.WHITE);
    renderer.setYLabelsColor(0, colors[0]);
    renderer.setYLabelsColor(1, Color.RED);


    renderer.setYAxisAlign(Align.RIGHT, 1);
    renderer.setYLabelsAlign(Align.LEFT, 1);

    XYMultipleSeriesDataset dataset = null;
    if (!mHeightInCmsMonthDate.isEmpty() && !mHeightValueCms.isEmpty()) {
        XYSeries SeperateY1 = new XYSeries("");
        for (int i = 0; i < mHeightInCmsMonthDate.size(); i++) {
            SeperateY1.add(mHeightInCmsMonthDate.get(i),
                    mHeightValueCms.get(i));
        }
        dataset = buildDataset(titles, x, values);
        dataset.addSeries(SeperateY1);
    } else {
        dataset = buildDataset(titles, x, values);
    }

    XYSeries seriesMin1, seriesMin2, seriesMin3, seriesMin4, seriesMin5, seriesMin6, seriesMin7, seriesMin8, seriesMin9, seriesMin10, seriesMin11, seriesMin12;
    if (!mHeightInCmsMonthDate.isEmpty()) {
        seriesMin1 = dataset.getSeriesAt(0);
        seriesMin2 = dataset.getSeriesAt(1);
        seriesMin3 = dataset.getSeriesAt(2);
        seriesMin4 = dataset.getSeriesAt(3);
        seriesMin5 = dataset.getSeriesAt(4);
        seriesMin6 = dataset.getSeriesAt(5);

        seriesMin1.addAnnotation("3%", 37,
                mHeightMonth3.get(mHeightMonth3.size() - 1));
        seriesMin2.addAnnotation("25%", 37,
                mHeightMonth25.get(mHeightMonth25.size() - 1));
        seriesMin3.addAnnotation("50%", 37,
                mHeightMonth50.get(mHeightMonth50.size() - 1));
        seriesMin4.addAnnotation("75%", 37,
                mHeightMonth75.get(mHeightMonth75.size() - 1));
        seriesMin5.addAnnotation("97%", 37,
                mHeightMonth97.get(mHeightMonth97.size() - 1));
        for (int i = 0; i < mHeightInCmsMonthDate.size(); i++) {
            seriesMin6.addAnnotation("(" + mHeightInCmsMonthDate.get(i)
                    + " , " + mHeightValueCms.get(i) + ")",
                    mHeightInCmsMonthDate.get(i), mHeightValueCms.get(i));
        }

    } else {
        seriesMin1 = dataset.getSeriesAt(0);
        seriesMin2 = dataset.getSeriesAt(1);
        seriesMin3 = dataset.getSeriesAt(2);
        seriesMin4 = dataset.getSeriesAt(3);
        seriesMin5 = dataset.getSeriesAt(4);

        seriesMin1.addAnnotation("3%", 37,
                mHeightMonth3.get(mHeightMonth3.size() - 1));
        seriesMin2.addAnnotation("25%", 37,
                mHeightMonth25.get(mHeightMonth25.size() - 1));
        seriesMin3.addAnnotation("50%", 37,
                mHeightMonth50.get(mHeightMonth50.size() - 1));
        seriesMin4.addAnnotation("75%", 37,
                mHeightMonth75.get(mHeightMonth75.size() - 1));
        seriesMin5.addAnnotation("97%", 37,
                mHeightMonth97.get(mHeightMonth97.size() - 1));
    }

    values.clear();
    List<double[]> values1 = new ArrayList<double[]>();
    List<double[]> values2 = new ArrayList<double[]>();
    List<double[]> values3 = new ArrayList<double[]>();
    List<double[]> values4 = new ArrayList<double[]>();
    List<double[]> values5 = new ArrayList<double[]>();
    List<double[]> values6 = new ArrayList<double[]>();
    values1.add(new double[] { mWeightMonth97.get(0),
            mWeightMonth97.get(1), mWeightMonth97.get(2),
            mWeightMonth97.get(3), mWeightMonth97.get(4),
            mWeightMonth97.get(5), mWeightMonth97.get(6),
            mWeightMonth97.get(7), mWeightMonth97.get(8),
            mWeightMonth97.get(9), mWeightMonth97.get(10),
            mWeightMonth97.get(11), mWeightMonth97.get(12) });
    addXYSeries(dataset, new String[] { "97%" }, x, values1, 1);
    values2.add(new double[] { mWeightMonth75.get(0),
            mWeightMonth75.get(1), mWeightMonth75.get(2),
            mWeightMonth75.get(3), mWeightMonth75.get(4),
            mWeightMonth75.get(5), mWeightMonth75.get(6),
            mWeightMonth75.get(7), mWeightMonth75.get(8),
            mWeightMonth75.get(9), mWeightMonth75.get(10),
            mWeightMonth75.get(11), mWeightMonth75.get(12) });
    addXYSeries(dataset, new String[] { "75%" }, x, values2, 1);
    values3.add(new double[] { mWeightMonth50.get(0),
            mWeightMonth50.get(1), mWeightMonth50.get(2),
            mWeightMonth50.get(3), mWeightMonth50.get(4),
            mWeightMonth50.get(5), mWeightMonth50.get(6),
            mWeightMonth50.get(7), mWeightMonth50.get(8),
            mWeightMonth50.get(9), mWeightMonth50.get(10),
            mWeightMonth50.get(11), mWeightMonth50.get(12) });
    addXYSeries(dataset, new String[] { "50%" }, x, values3, 1);
    values4.add(new double[] { mWeightMonth25.get(0),
            mWeightMonth25.get(1), mWeightMonth25.get(2),
            mWeightMonth25.get(3), mWeightMonth25.get(4),
            mWeightMonth25.get(5), mWeightMonth25.get(6),
            mWeightMonth25.get(7), mWeightMonth25.get(8),
            mWeightMonth25.get(9), mWeightMonth25.get(10),
            mWeightMonth25.get(11), mWeightMonth25.get(12) });
    addXYSeries(dataset, new String[] { "25%" }, x, values4, 1);
    values5.add(new double[] { mWeightMonth3.get(0), mWeightMonth3.get(1),
            mWeightMonth3.get(2), mWeightMonth3.get(3),
            mWeightMonth3.get(4), mWeightMonth3.get(5),
            mWeightMonth3.get(6), mWeightMonth3.get(7),
            mWeightMonth3.get(8), mWeightMonth3.get(9),
            mWeightMonth3.get(10), mWeightMonth3.get(11),
            mWeightMonth3.get(12) });
    addXYSeries(dataset, new String[] { "3% (Weight)" }, x, values5, 1);
    double[] WeightInKg = null;
    if (!mWeightValueKg.isEmpty() && !mWeightInKgMonthDate.isEmpty()) {
        WeightInKg = new double[mWeightValueKg.size()];
        for (int i = 0; i < mWeightValueKg.size(); i++) {
            WeightInKg[i] = mWeightValueKg.get(i);
        }
        values6.add(WeightInKg);
        double[] dateValue = new double[mWeightInKgMonthDate.size()];
        for (int i = 0; i < mWeightInKgMonthDate.size(); i++) {
            dateValue[i] = mWeightInKgMonthDate.get(i);
        }
        x1.add(dateValue);

        addXYSeries(dataset, new String[] { "" }, x1, values6, 1);
    }
    if (!mHeightInCmsMonthDate.isEmpty()) {
        seriesMin7 = dataset.getSeriesAt(6);
        seriesMin8 = dataset.getSeriesAt(7);
        seriesMin9 = dataset.getSeriesAt(8);
        seriesMin10 = dataset.getSeriesAt(9);
        seriesMin11 = dataset.getSeriesAt(10);

        seriesMin7.addAnnotation("3%", 37,
                mWeightMonth3.get(mWeightMonth3.size() - 1));
        seriesMin8.addAnnotation("25%", 37,
                mWeightMonth25.get(mWeightMonth25.size() - 1));
        seriesMin9.addAnnotation("50%", 37,
                mWeightMonth50.get(mWeightMonth50.size() - 1));
        seriesMin10.addAnnotation("75%", 37,
                mWeightMonth75.get(mWeightMonth75.size() - 1));
        seriesMin11.addAnnotation("97%", 37,
                mWeightMonth97.get(mWeightMonth97.size() - 1));
        if (!mWeightInKgMonthDate.isEmpty()) {
            seriesMin12 = dataset.getSeriesAt(11);
            for (int i = 0; i < mWeightInKgMonthDate.size(); i++) {
                seriesMin12.addAnnotation("(" + mWeightInKgMonthDate.get(i)
                        + " , " + WeightInKg[i] + ")",
                        mWeightInKgMonthDate.get(i), WeightInKg[i]);
            }

        }

    } else {
        seriesMin6 = dataset.getSeriesAt(5);
        seriesMin7 = dataset.getSeriesAt(6);
        seriesMin8 = dataset.getSeriesAt(7);
        seriesMin9 = dataset.getSeriesAt(8);
        seriesMin10 = dataset.getSeriesAt(9);
        seriesMin6.addAnnotation("3%", 37,
                mWeightMonth3.get(mWeightMonth3.size() - 1));
        seriesMin7.addAnnotation("25%", 37,
                mWeightMonth25.get(mWeightMonth25.size() - 1));
        seriesMin8.addAnnotation("50%", 37,
                mWeightMonth50.get(mWeightMonth50.size() - 1));
        seriesMin9.addAnnotation("75%", 37,
                mWeightMonth75.get(mWeightMonth75.size() - 1));
        seriesMin10.addAnnotation("97%", 37,
                mWeightMonth97.get(mWeightMonth97.size() - 1));
        if (!mWeightInKgMonthDate.isEmpty()) {
            seriesMin11 = dataset.getSeriesAt(10);
            for (int i = 0; i < mWeightInKgMonthDate.size(); i++) {
                seriesMin11.addAnnotation("(" + mWeightInKgMonthDate.get(i)
                        + " , " + WeightInKg[i] + ")",
                        mWeightInKgMonthDate.get(i), WeightInKg[i]);
            }

        }

    }

    GraphicalView mView = ChartFactory.getCubeLineChartView(context,
            dataset, renderer, 0.3f);

return mView;
}

回答1:


sorry the problem was with the logic i have used above ,I have changed the line

if (!(mWeightInKgMonthDate.isEmpty() && mHeightInCmsMonthDate.isEmpty()))

to

if (!mWeightInKgMonthDate.isEmpty() && !mHeightInCmsMonthDate.isEmpty())

thats it..



来源:https://stackoverflow.com/questions/22142894/dataset-and-renderer-should-not-be-null-and-should-have-same-number-of-series-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!