在Android中,错误“数据集和渲染器应该不为空,并且应该具有相同的系列数”

时间:2013-03-26 08:48:19

标签: android charts achartengine

我正在尝试使用AChartEngine库在sqlitedatabase中显示包含数据的图形。我在数据库中有两条记录。存在一条记录时显示BarChart图形。但是有两个数据记录,它说,“数据集和渲染器应该不为空,并且应该具有相同数量的系列”。我不明白这一点。

我的代码是,

    Button ReportGraph = (Button) findViewById(R.id.ReportGraph);
    ReportGraph.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            GraphicalView GraphChartView = ChartFactory.getBarChartView(
                    getApplicationContext(), getWaterElectricityReading(),
                    getDemoRenderer(), Type.DEFAULT);
            // Intent intent=ChartFactory.getLineChartIntent(this,
            // getDemoDataset(), getDemoRenderer());
            // startActivity(intent);
            if (GraphChartView != null) {
                GraphChartView.repaint();
            }
            relChartView.addView(GraphChartView);
            mainLayout.setVisibility(View.GONE);
            relChartView.setVisibility(View.VISIBLE);

        }
    });


private XYMultipleSeriesDataset getWaterElectricityReading() {
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd", Locale.US);
    SimpleDateFormat df2 = new SimpleDateFormat("MMM dd yyyy");

    Date StartDate;
    Date EndDate;
    String Pastdateformat = "";
    String CurrentDateFormat = "";

    SQLiteDatabase DB = Context
            .openOrCreateDatabase("WaterElectricityReadingDataBase.db",
                    MODE_WORLD_READABLE, null);
    Cursor c = DB
            .rawQuery(
                    "select ReadingValue,strftime('%m/%d/%Y',StartDateTime) StartDateTime,strftime('%m/%d/%Y',EndDateTime) EndDateTime from WaterElectricity order by StartDateTime desc ",
                    null);
    int rowCount = c.getCount();

    if (c != null) {
        if (rowCount > 0) {
            if (c.moveToFirst()) {
                do {
                    String ReadingValue = c.getString(c
                            .getColumnIndex("ReadingValue"));
                    String PastDate = c.getString(c
                            .getColumnIndex("StartDateTime"));
                    String CurrentDate = c.getString(c
                            .getColumnIndex("EndDateTime"));
                    try {
                        StartDate = df2.parse(PastDate);
                        Pastdateformat = sdf1.format(StartDate);

                        EndDate = df2.parse(CurrentDate);
                        CurrentDateFormat = sdf1.format(EndDate);
                    } catch (Exception e) {

                    }
                    XYSeries series = new XYSeries(
                            "WaterElectricityReading");
                    double ReadingVal = Double.parseDouble(ReadingValue);
                    series.add(rowCount, ReadingVal);
                    XYMultipleSeriesRenderer render = new XYMultipleSeriesRenderer();


                    dataset.addSeries(series);
                    rowCount++;
                } while (c.moveToNext());
            }
        }
    }
    return dataset;
}

private XYMultipleSeriesRenderer getDemoRenderer() {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    renderer.setAxisTitleTextSize(12);
    renderer.setChartTitleTextSize(12);
    renderer.setLabelsTextSize(15);
    renderer.setLegendTextSize(15);
    renderer.setPointSize(5f);
    renderer.setMargins(new int[] { 20, 30, 15, 0 });
    XYSeriesRenderer r = new XYSeriesRenderer();
    r.setColor(Color.GREEN);
    r.setPointStyle(PointStyle.DIAMOND);
    r.setFillBelowLine(false);
    r.setFillPoints(true);
    renderer.addSeriesRenderer(r);
    setChartSettings(renderer);
    return renderer;
}

private void setChartSettings(XYMultipleSeriesRenderer renderer) {
    renderer.setChartTitle("Chart demo");
    renderer.setXTitle("x values");
    renderer.setYTitle("y values");
    renderer.setApplyBackgroundColor(false);
    renderer.setRange(new double[] { 0, 6, -70, 40 });
    renderer.setFitLegend(false);
    renderer.setAxesColor(Color.BLACK);
    renderer.setShowGrid(true);
    renderer.setXAxisMin(0);
    renderer.setXAxisMax(10.5);
    renderer.setYAxisMin(0);
    renderer.setZoomEnabled(false);
    renderer.setYAxisMax(1000);
}

对于两条记录,我想要显示两个条形。  任何人都可以指出错误。

任何帮助将不胜感激!!

感谢。

1 个答案:

答案 0 :(得分:5)

您的图表应使用一个XYMultipleSeriesDataset和一个XYMultipleSeriesRenderer构建。

XYMultipleSeriesDataset应包含与XYSeriesXYSeriesRenderer一样多的XYMultipleSeriesRenderer

在您的代码中,您为每条记录创建XYSeries,这可能是错误的。除非您有充分的理由不这样做,否则数据应该在同一系列中。但是,如果您添加多个XYSeries,那么每个XYSeriesRenderer都需要一个XYSeries