0
MPAndroidChartでHorizontalBarChartウィジェットの高さを設定しようとしています。 HorizontalBarChartオブジェクトで呼び出すことができる唯一の高さ関連のメソッドは、setMinimumHeight()
です。これは役に立たないようです。私もHorizontalBarChartを400dp
に設定しようとしましたが、この値は無視され、グラフは実際に必要以上のスペースを占めているようです。このグラフの高さはどのように設定できますか?MPAndroidChartの高さを設定できませんHorizontalBarChart
実装:
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>
私はxmlファイルにマージンを設定することでこれを解決し
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);
}
}
}
これは私が探しているかなりのものを私に取得していません。私は、グラフの上に余分な空白を取り除きたいと思います。設定を上向きにすると、少し近づいてしまいます。 android:layout_marginTop = " - 155dp" – MalcolmMcFly
@MalcolmMcFlyは私の答えを編集しました。 – Kiya