Android achartengine illegalArgumentException但我不知道为什么

时间:2014-02-16 22:36:34

标签: java android achartengine

我在android中使用了achartengine库。每次我尝试运行我的Android应用程序时,我都会收到此错误

  

java.lang.IllegalArgumentException:数据集和渲染器不应该为空,并且应该具有相同数量的系列

我对这个问题的理解是说我有一个以上的XYMultipleSeriesRenderer和一个XYMultipleSeriesDataset。但在我的代码中我只能找到1个。我很抱歉,如果这是一个愚蠢的问题,但我有点吓坏了因为我的应用程序已经过了它的截止日期,我无法弄清楚为什么它一直给我这个问题。

这是我的代码。对不起,这很麻烦,在很多地方可能没有意义,但如果有人愿意提供帮助,我会很乐意解释我在某些地方要做的事情。

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

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.RangeCategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;


public class BarGraph {

String title, realTitle;

double[] y;
int ySize;

double largest = 0.0, smallest = 0.0;

int[] colors;

RangeCategorySeries series;

XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

XYSeriesRenderer renderer = new XYSeriesRenderer();

public GraphicalView getView(Context context, int width) {
    // Bar 1

    List<double[]> linevalues = new ArrayList<double[]>();
    linevalues.add(new double[] { 0, 0, 0, 0, 0, 0 });

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

    dataset.addSeries(series.toXYSeries());

    for (int i = 0; i < 10; i++) {
        XYSeries yseries = new XYSeries("Demo series " + (i + 1));
        for (int k = 0; k < GlobalVar.cropList.size(); k++) {
            series.add(k, i);
        }
        dataset.addSeries(yseries);
    }
    // This is what the "Graph" itself will look like

    int flag = 0;
    // mRenderer.setXTitle("\n\n\n\n\n\n\n\n\n\nCrops");

    mRenderer.setXLabels(0);



    int marginT = 20, marginL = 100, marginB = 100, marginR = 0;
    if (width <= 960)
        flag = 0;
    else if (width > 960 && width < 1120)
        flag = 1;
    else
        flag = 2;
    switch (flag) {
    case 0:
        mRenderer.setLabelsTextSize(20);
        mRenderer.setAxisTitleTextSize(20);
        renderer.setChartValuesTextSize(20);
        mRenderer.setChartTitleTextSize(30);
        mRenderer.setBarSpacing(.001);
        mRenderer.setXAxisMax(5);
        marginT = 40;
        marginL = 100;
        marginB = 75;
        marginR = 0;
        break;
    case 1:
        mRenderer.setLabelsTextSize(25);
        mRenderer.setAxisTitleTextSize(20);
        renderer.setChartValuesTextSize(20);
        mRenderer.setBarSpacing(.5);
        mRenderer.setChartTitleTextSize(30);
        mRenderer.setXAxisMax(GlobalVar.cropList.size() + 1);
        marginT = 40;
        marginL = 100;
        marginB = 75;
        marginR = 0;
        break;
    case 2:
        mRenderer.setLabelsTextSize(35);
        mRenderer.setAxisTitleTextSize(30);
        renderer.setChartValuesTextSize(35);
        mRenderer.setChartTitleTextSize(30);
        mRenderer.setBarSpacing(.5);
        mRenderer.setXAxisMax(GlobalVar.cropList.size() + 1);
        marginT = 40;
        marginL = 100;
        marginB = 75;
        marginR = 0;
        break;
    }

    int[] margins = { marginT, marginL, marginB, marginR };

    // mRenderer.setLabelsTextSize(35);
    // mRenderer.setAxisTitleTextSize(30);

    mRenderer.setYTitle(title + " ($/acre)");
    mRenderer.setAxesColor(Color.BLACK);
    mRenderer.setLabelsColor(Color.BLACK);
    mRenderer.setXLabelsColor(Color.BLACK);
    mRenderer.setChartTitle(title);
    mRenderer.setMargins(margins);
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(context.getResources().getColor(
            R.color.graphBG));
    mRenderer.setPanEnabled(true, false);
    mRenderer.setClickEnabled(false);
    mRenderer.setZoomButtonsVisible(false);
    mRenderer.setZoomEnabled(true, false);
    mRenderer.setBarWidth(40);
    // mRenderer.setXLabels(0);
    mRenderer.setXAxisMin(0);
    mRenderer.setXLabelsAngle(30);
    mRenderer.setYAxisMax((((int) Math.ceil(largest / 100)) * 100) + 50);
    mRenderer.setMarginsColor(context.getResources().getColor(
            R.color.marginColor));
    // mRenderer.setChartTitleTextSize(20);
    mRenderer.setShowLabels(true);
    mRenderer.setShowLegend(false);
    mRenderer.setYLabelsAlign(Align.RIGHT);
    mRenderer.setYLabelsColor(0, Color.BLACK);
    if (smallest > 0)
        mRenderer.setYAxisMin(0);
    else
        mRenderer.setYAxisMin(smallest - 50);

    // Customize bar 1

    renderer.setDisplayChartValues(true);

    renderer.setChartValuesSpacing((float) 5.5);
    renderer.setColor(Color.parseColor("#800000"));
    renderer.setChartValuesTextAlign(Align.CENTER);

    mRenderer.addSeriesRenderer(renderer);

    return ChartFactory.getRangeBarChartView(context, dataset, mRenderer, Type.DEFAULT);

}

@SuppressWarnings("deprecation")
public void isReturnsOverTotal(Boolean returnsTot) {
    y = new double[GlobalVar.cropList.size()];
    // colors = new int[GlobalVar.cropList.size()];

    double largestOT = 0.0, smallestOT = 0;
    double largestOV = 0.0, smallestOV = 0;

    String placeholder = "";

    if (returnsTot) {
        series = new RangeCategorySeries("Returns Over Total Costs");
        // this.setChartSettings(mRenderer, "Returns Over Total Costs",
        // "Returns Over Variable Costs", "", .5, 12.5, smallest, largest,
        // Color.GRAY, Color.LTGRAY);
        for (int i = 0; i < GlobalVar.cropList.size(); i++) {
            placeholder = GlobalVar.cropList.get(i).getTableCropName()
                    .replaceAll(" ", "\n");
            y[i] = GlobalVar.cropList.get(i).getReturnOverTotal();
            if (y[i] > 0)
                series.add(0, y[i]);
            else
                series.add(y[i], 0);
            if (y[i] > largestOT)
                largestOT = y[i];
            else if (y[i] < smallestOT)
                smallestOT = y[i];

            mRenderer.addTextLabel(i + 1, placeholder);
        }
        title = "Returns Over Total Costs";
    } else {
        series = new RangeCategorySeries("Returns Over Variable Costs");
        for (int i = 0; i < GlobalVar.cropList.size(); i++) {
            placeholder = GlobalVar.cropList.get(i).getTableCropName()
                    .replaceAll(" ", "\n");
            y[i] = GlobalVar.cropList.get(i).getReturnOverVariable();
            if (y[i] > 0)
                series.add(0, y[i]);
            else
                series.add(y[i], 0);
            if (y[i] > largestOV)
                largestOV = y[i];
            else if (y[i] < smallestOT)
                smallestOT = y[i];

            mRenderer.addTextLabel(i + 1, placeholder);
        }

        title = "  Returns Over Variable Costs";
    }

    smallest += smallestOV;
    smallest += smallestOT;
    largest += largestOV;
    largest += largestOT;

}

}

1 个答案:

答案 0 :(得分:2)

您要向XYSeries添加11个XYMultipleSeriesDataset个对象,一个在for循环之前,另一个在循环内。每个XYSeries都应该有相应的XYSeriesRenderer,但XYMultipleSeriesRenderer只添加了一个此类对象。