Reputation: 402
I am trying to set the height of a HorizontalBarChart widget in MPAndroidChart. The only height related method you can call on a HorizontalBarChart object is setMinimumHeight()
which didn't seem to help. I've also tried setting the HorizontalBarChart to 400dp
but it seems this value is ignored and chart still takes up more space than it actually needs. How can I set the height of this chart?
Implementation:
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="home">
<com.demo.companion.widgets.VerticalTextView
android:id="@+id/tv_device_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="40dp"
android:rotation="180"
android:text="Watch Name"
android:textColor="@color/green"
android:textSize="65sp" />
<ImageView
android:id="@+id/iv_watch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="40dp"
android:src="@drawable/watch" />
<RelativeLayout
android:id="@+id/rl_bluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_watch"
android:layout_toRightOf="@+id/tv_device_name">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_bluetooth" />
<TextView
android:id="@+id/tv_bluetooth_status"
style="@style/FTUEBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Bluetooth Connected" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/ic_check" />
</RelativeLayout>
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/vitals"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_below="@+id/rl_bluetooth"
android:layout_toRightOf="@+id/tv_device_name" />
</RelativeLayout>
HomeFragment.java
public class HomeFragment extends Fragment {
protected HorizontalBarChart mChart;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mChart = (HorizontalBarChart) rootView.findViewById(R.id.vitals);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(false);
mChart.getDescription().setEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawBarShadow(true);
mChart.setDrawGridBackground(false);
mChart.setTouchEnabled(false);
mChart.getAxisLeft().setDrawLabels(false);
mChart.getAxisLeft().setDrawAxisLine(false);
mChart.getAxisLeft().setDrawGridLines(false);
mChart.getAxisRight().setDrawLabels(false);
mChart.getAxisRight().setDrawAxisLine(false);
mChart.getAxisRight().setDrawGridLines(false);
mChart.getXAxis().setDrawLabels(false);
mChart.getXAxis().setDrawAxisLine(false);
mChart.getXAxis().setSpaceMax(5f);
mChart.getXAxis().setDrawGridLines(false);
mChart.getLegend().setEnabled(false);
setData(4, 100);
mChart.setFitBars(true);
mChart.animateY(1000);
return rootView;
}
private void setData(int count, float range) {
float barWidth = 0.5f;
float spaceForBar = 2f;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range);
yVals1.add(new BarEntry(i * spaceForBar, val,
null));
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet)mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "DataSet 1");
set1.setBarShadowColor(getResources().getColor(R.color.black));
set1.setColor(getResources().getColor(R.color.green));
set1.setDrawIcons(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(barWidth);
mChart.setData(data);
}
}
}
Upvotes: 0
Views: 725
Reputation: 639
I solved this by setting margins in the xml file:
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/vitals"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp" <!-- adjust -->
android:layout_marginTop="50dp" <!-- accordingly -->
android:layout_alignParentBottom="true"
android:layout_below="@+id/rl_bluetooth"
android:layout_toRightOf="@+id/tv_device_name" />
Edit ::
mChart.setExtraTopOffset(-155);
Upvotes: 0