В этом макете я использую Горизонтальный график и ratingBar
виджет. Я хочу, чтобы каждый бар находился перед своим собственным рейтингом. Я изменяю barWidth графика, но не могу настроить макет. Это мой макет XML:
<LinearLayout
android:id="@+id/layoutRating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="@+id/txtLabelRate"
android:visibility="visible">
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/barChart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_weight="1"
android:layout_marginStart="10dp">
<RatingBar
android:id="@+id/ratingBarFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:rating="5"/>
<TextView
android:id="@+id/txtPercentFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{courseItem.percentageEachRating.get(0)+'%'}"
android:layout_toEndOf="@+id/ratingBarFive"
android:layout_marginStart="15dp"/>
<RatingBar
android:id="@+id/ratingBarFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:layout_below="@+id/ratingBarFive"
android:rating="4"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txtPercentFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{courseItem.percentageEachRating.get(1)+'%'}"
android:layout_below="@+id/ratingBarFive"
android:layout_toEndOf="@+id/ratingBarFour"
android:layout_marginStart="15dp"/>
<RatingBar
android:id="@+id/ratingBarThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:layout_below="@+id/ratingBarFour"
android:rating="3"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txtPercentThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{courseItem.percentageEachRating.get(2)+'%'}"
android:layout_below="@+id/ratingBarFour"
android:layout_toEndOf="@+id/ratingBarThree"
android:layout_marginStart="15dp"/>
<RatingBar
android:id="@+id/ratingBarTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:layout_below="@+id/ratingBarThree"
android:rating="2"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txtPercentTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{courseItem.percentageEachRating.get(3)+'%'}"
android:layout_below="@+id/ratingBarThree"
android:layout_toEndOf="@+id/ratingBarTwo"
android:layout_marginStart="15dp"/>
<RatingBar
android:id="@+id/ratingBarOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:layout_below="@+id/ratingBarTwo"
android:rating="1"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/txtPercentOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{courseItem.percentageEachRating.get(4)+'%'}"
android:layout_below="@+id/ratingBarTwo"
android:layout_toEndOf="@+id/ratingBarOne"
android:layout_marginStart="15dp"/>
</RelativeLayout>
</LinearLayout>
И это мой вывод:
И это вывод, который я хочу:
А это мой код для набора диаграмм:
private void setGraph(){
mBinding.barChart.getDescription().setEnabled(false);
mBinding.barChart.setDrawBarShadow(false);
mBinding.barChart.getLegend().setEnabled(false);
mBinding.barChart.setPinchZoom(false);
mBinding.barChart.setDrawValueAboveBar(false);
XAxis xAxis=mBinding.barChart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setEnabled(true);
xAxis.setDrawAxisLine(false);
YAxis yAxis=mBinding.barChart.getAxisLeft();
yAxis.setAxisMaximum(100f);
yAxis.setAxisMinimum(0f);
yAxis.setEnabled(false);
xAxis.setLabelCount(5);
YAxis yRight=mBinding.barChart.getAxisRight();
yRight.setDrawAxisLine(true);
yRight.setDrawGridLines(false);
yRight.setEnabled(false);
mBinding.barChart.animateY(2000);
mBinding.barChart.setDrawBarShadow(true);
mBinding.barChart.getAxisLeft().setDrawLabels(false);
mBinding.barChart.getAxisRight().setDrawLabels(false);
mBinding.barChart.getXAxis().setDrawLabels(false);
}
private void setDataChart(){
ArrayList<String> percentageEachRating=currentCourse.getPercentageEachRating();
ArrayList<BarEntry> entries=new ArrayList<>();
float barWidth=1f;
float spaceForBar=7f;
for (int i=0;i<percentageEachRating.size();i++){
float val=Float.valueOf(percentageEachRating.get(i));
entries.add(new BarEntry(i*spaceForBar,val));
}
BarDataSet set=new BarDataSet(entries,"Data set1");
set.setDrawValues(false);
set.setColors(ContextCompat.getColor(getContext(),R.color.colorRateOne),
ContextCompat.getColor(getContext(),R.color.colorRateTwo),
ContextCompat.getColor(getContext(),R.color.colorRateThree),
ContextCompat.getColor(getContext(),R.color.colorRateFour),
ContextCompat.getColor(getContext(),R.color.colorRateFive));
mBinding.barChart.setDrawBarShadow(true);
set.setBarShadowColor(Color.argb(40,150,150,150));
BarData data=new BarData(set);
data.setBarWidth(barWidth);
mBinding.barChart.setData(data);
mBinding.barChart.invalidate();
}