user1060551 user1060551 - 9 months ago 40
Android Question

Android MPAndroidChart chart height

I am using MPAndroidChart library to plot charts but I can't get the height right. No matter what value I put it remains a very compressed and not valid chart height.

Would you be able to help out?

Chart now:
enter image description here

This is my .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>


This is my chart plotting:

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

Answer Source

I could make it work the way I wanted by removing the pressureLineChartLayout.setMinimumHeight property and setting the pressureLineChartLayout.setExtraBottomOffset to -50 instead of 50.

Those two things made the magic - don-t asked me why.