2016-07-07 123 views
0

我正在使用我的應用上的MPAndroid圖表從我的sqlite數據庫中繪製圖表,使用Y軸的「weight」值和X軸的「date」值。Android MPAndroidCharts LineChart跳過值

問題是我的數據庫只有一些行有這個權值。當所有的行都有重量佈局時,我的代碼就像一個魅力一樣,但是當有一行沒有重量值,下一行包含該重量值時,它會在圖表的Y軸上留下空間,altought日期值在XAxes上設置。因此,對於沒有重量值的每一行,Y軸上都會丟失一個值,因爲重量行之後的值向前移動了一個點。例如:enter image description here

我有設立圖表驗證碼:

public void setUpCharts(){ 
     chart = (LineChart) rootView.findViewById(R.id.weightChart); 

     chart.setNoDataTextDescription("Nothing"); 
     chart.setDrawBorders(false); 
     chart.setDescription(""); 
     chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); 
     chart.getAxisRight().setEnabled(false); 
     chart.getAxisLeft().setEnabled(false); 
     chart.getAxisLeft().setDrawGridLines(false); 
     chart.getAxisRight().setDrawGridLines(false); 
     chart.getXAxis().setDrawGridLines(false); 
     chart.getXAxis().setDrawAxisLine(false); 
     chart.getLegend().setEnabled(false); 

     chart.setScaleEnabled(false); 
     chart.setPinchZoom(false); 
     chart.setDoubleTapToZoomEnabled(false); 


     weights = new ArrayList<>(); //Pesos 
     dates = new ArrayList<String>(); //Días del mes 

     getChartData(); 

     LineDataSet dataSet = new LineDataSet(weights, "Weights"); 

     dataSet.setDrawCubic(false); 
     dataSet.setCircleRadius(4); 
     dataSet.setLineWidth(3); 
     dataSet.setDrawValues(true); 
     dataSet.setColor(getContext().getResources().getColor(R.color.colorWeightChart)); 
     dataSet.setCircleColor(getContext().getResources().getColor(R.color.colorWeightChart)); 
     dataSet.setCircleColorHole(getContext().getResources().getColor(R.color.colorWeightChart)); 
     LineData data = new LineData(dates, dataSet); 
     chart.setData(data); 

     //LimitLine 
     String weight_get = PreferenceManager.getDefaultSharedPreferences(getContext()).getString("Goal_weight", null); 
     if(!weight_get.equals(null) && !weight_get.equals("")){ 
      float weight_goal = Float.valueOf(weight_get); 
      YAxis leftAxis = chart.getAxisLeft(); 
      LimitLine ll = new LimitLine(weight_goal); 
      ll.setLineColor(getContext().getResources().getColor(R.color.weightTabFAB)); 
      ll.setLineWidth(1f); 
      ll.enableDashedLine(10f, 10f, 0f); 
      leftAxis.addLimitLine(ll); 

      float YMax= dataSet.getYMax(); 
      if(weight_goal > YMax){ 
       float difference = weight_goal - YMax; 
       chart.getAxisRight().setAxisMaxValue(weight_goal + difference); //So the limit line will be always visible 
       chart.getAxisLeft().setAxisMaxValue(weight_goal + difference); 
      } 
     } 

     chart.setVisibleXRangeMaximum(5f); 
    } 

而且我用getChartData設置所有圖表數據從我的SQLite數據庫中獲取的值。它應該只添加重量存在的行的值,並且實際上日誌顯示它正常工作,所以我不知道如何修復它。

protected void getChartData(){ 
    IDs = new ArrayList<Integer>(); 
    Cursor data = dataSource.getAllItems(); 
    if(data!=null) { 
     while(data.moveToNext()){ 
      int id = data.getInt(data.getColumnIndex(RecordsDataSource.ColumnRecords.ID_RECORDS)); 
      String weight= data.getString(data.getColumnIndex(RecordsDataSource.ColumnRecords.WEIGHTS_RECORDS)); 

      if(weight!=null && !weight.equals("")){ 
       long milisDate =data.getLong(data.getColumnIndex(RecordsDataSource.ColumnRecords.DATES_RECORDS)); 
       String date = Utils.milisToDate(milisDate, "dd/MM"); 

       IDs.add(id); 
       weights.add(new Entry(Float.valueOf(weight), id -1)); 
       dates.add(date); 
       Log.i("ID "+ String.valueOf(id),"Not null, "+ weight); 
      }else{ 
       Log.i("ID "+ String.valueOf(id), "Null"); 
      } 
     } 
     data.close(); 
    } 
} 

我一直試圖解決這個問題,但沒有任何工作。 有人可以幫我嗎?順便說一句,對不起我的英文不好。

回答

2

問題出在idgetChartData()方法中。 id正在跳過你的圖表Entry的位置。

取而代之的是

weights.add(new Entry(Float.valueOf(weight), id -1)); 

添加變量條目索引,並增加它在每次迭代

index++; 
weights.add(new Entry(Float.valueOf(weight), index)); 

它應該工作。

+0

謝謝!!這解決了我的問題。 這是一個愚蠢的恐怖,但我沒有注意到它 – Javierd98