2017-03-09 63 views
0

我正在使用MPAndroidChart庫來繪製圖表,但我無法獲得正確的高度。無論我把它放在什麼位置,它仍然是一個非常壓縮和無效的圖表高度。Android MPAndroidChart圖表高度

你能幫忙嗎?現在

圖: enter image description here

這是我的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="it.bitrack.fabio.bitrack.AssetView" 
    android:id="@+id/assetView_relative_layout" 
    android:layout_marginTop="60dp"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true"> 

      <LinearLayout 
       android:id="@+id/pressureLinearLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:text="Pressure" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textStyle="bold|italic" 
        android:id="@+id/textView6" 
        android:padding="10dp" /> 

       <com.github.mikephil.charting.charts.LineChart 
        android:id="@+id/pressureLineChart" 
        android:layout_width="match_parent" 
        android:layout_height="250dp" 
        android:padding="10dp" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:id="@+id/pressureSelectedTextView" 
        android:layout_height="wrap_content" 
        android:text="Nothing selected" 
        android:padding="10dp" /> 

      </LinearLayout> 

     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 

這是我的圖表繪製:

final ArrayList<Entry> lineEntries = new ArrayList<>(); 


      for (Asset a: assetList) { 

       lineEntries.add(new Entry((epoch.getEpoch(a.datetime) - EPOCH_OFFSET), a.pressure)); 

       Log.i("BiTrack(adding)", epoch.getEpoch(a.datetime) + " " + a.datetime); 

      } 

      //line chart data 
      LineDataSet lineDataSet = new LineDataSet(lineEntries, "Pressure"); 
      lineDataSet.setValueTextSize(CHART_VALUE_TEXT_SIZE); 

      ArrayList<ILineDataSet> lineDataSets = new ArrayList<>(); 
      lineDataSets.add(lineDataSet); 

      LineData lineData = new LineData(lineDataSets); 
      pressureLineChartLayout.setData(lineData); 

      Description d = new Description(); 
      d.setText(spinner.getSelectedItem().toString()); 

      pressureLineChartLayout.setDescription(d); 
      pressureLineChartLayout.setMinimumHeight(250); 
      pressureLineChartLayout.setExtraBottomOffset(50f); 

      XAxis xAxis = pressureLineChartLayout.getXAxis(); 
      xAxis.setLabelRotationAngle(45f); 
      xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); 

      xAxis.setValueFormatter(new IAxisValueFormatter() { 

       @Override 
       public String getFormattedValue(float value, AxisBase axis) { 

        return epoch.getDatetimeNoYear(((long) value) + EPOCH_OFFSET); 

       } 
      }); 

      pressureLineChartLayout.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { 
       @Override 
       public void onValueSelected(Entry entry, Highlight highlight) { 
        //display msg when value selected 
        if (entry == null) 
         return; 
//    Toast.makeText(context, epoch.getDatetime((int) entry.getX()), Toast.LENGTH_SHORT).show(); 
        pressureSelectedTextView.setText("Date: " + epoch.getDatetime(((long) entry.getX()) + EPOCH_OFFSET) + ", Pressure: " + entry.getY() + " mbar"); 
       } 

       @Override 
       public void onNothingSelected() { 
        pressureSelectedTextView.setText("Nothing selected"); 
       } 
      }); 

      pressureLineChartLayout.notifyDataSetChanged(); 
      pressureLineChartLayout.invalidate(); 

      pressureLinearLayout.setVisibility(getView().VISIBLE); 
     } 

回答

0

我可以使它工作,我想通過刪除的方式pressureLineChartLayout.setMinimumHeight屬性,並將pressureLineChartLayout.setExtraBottomOffset設置爲-50而不是50.

這兩件事情使魔術 - 不要問我爲什麼。