我正在使用MPAndroidChart库绘制图表,但我无法获得正确的高度。无论我把它放在什么价值,它仍然是一个非常压缩和无效的图表高度。
你能帮忙吗?
这是我的.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 :(得分:0)
我可以通过移除pressureLineChartLayout.setMinimumHeight
属性并将pressureLineChartLayout.setExtraBottomOffset
设置为-50
而不是50
来使其按照我想要的方式工作。
这两件事成了魔术 - 不要问我为什么。