2013-03-26 38 views
1

我正在嘗試使用AChartEngine庫在sqlitedatabase中顯示數據圖。我在數據庫中有兩條記錄。有一條記錄時顯示BarChart圖形。但是有兩條記錄的數據,它說:「數據集和渲染器不應該是空的,並且應該有相同數量的系列」。我不明白這一點。錯誤「數據集和渲染器應該不爲空,並且應該具有相同數量的系列」Android

我的代碼,

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

兩年的記錄,我要顯示兩個酒吧。 任何人都可以請指出錯誤。

任何幫助將不勝感激!

謝謝。

回答

6

您的圖表應該使用一個單獨的XYMultipleSeriesDataset和一個XYMultipleSeriesRenderer構建。

XYMultipleSeriesDataset應該包含儘可能多的XYSeries,因爲XYMultipleSeriesRenderer中有XYSeriesRenderer

在您的代碼中,您將爲每條記錄創建一個XYSeries,這可能是錯誤的。除非你有充分理由不這樣做,否則數據應該是同一系列的。但是,如果您添加多個XYSeries,則每個XYSeries需要一個XYSeriesRenderer

+0

謝謝。你的解釋有幫助! – Mahe 2013-03-27 04:52:23

+0

請看看這個丹http://stackoverflow.com/questions/22142894/dataset-and-renderer-should-not-be-null-in-achartengine – 2014-03-03 09:45:03

相關問題