1

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

我的代码是,

    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);
}

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

任何帮助将不胜感激!!

谢谢。

4

1 回答 1

5

您的图表应该使用一个 singleXYMultipleSeriesDataset和一个 single来构建XYMultipleSeriesRenderer

XYMultipleSeriesDataset应该包含与 中一样多XYSeries的内容。XYSeriesRendererXYMultipleSeriesRenderer

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

于 2013-03-26T11:43:08.103 回答