Android 水平矩形评论栏

Android horizontal rectangle review bar

我正在努力创建一个矩形水平评论栏,这是我想要实现的目标的照片。



为此,我使用了 MPAAndroidChart,但我最终得到了这个,



代码:

public class MainActivity extends AppCompatActivity {

    HorizontalBarChart skillRatingChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSkillGraph( );
    }

    public void setSkillGraph(){
        skillRatingChart = findViewById(R.id.skill_rating_chart);
        skillRatingChart.setDrawBarShadow(false);
        Description description = new Description();
        description.setText("");
        skillRatingChart.setDescription(description);
        skillRatingChart.getLegend().setEnabled(false);
        skillRatingChart.setPinchZoom(false);
        skillRatingChart.setDrawValueAboveBar(false);


        XAxis Xaxis = skillRatingChart.getXAxis();
        Xaxis.setDrawGridLines(false);
        Xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        Xaxis.setEnabled(true);
        skillRatingChart.getXAxis().setDrawGridLines(false);
        skillRatingChart.getAxisLeft().setDrawGridLines(false);
        Xaxis.setDrawAxisLine(false);

        final ArrayList<String> xLabels = new ArrayList<>();
        xLabels.add("Pathetic");
        xLabels.add("Poor");
        xLabels.add("Average");
        xLabels.add("Good");
        xLabels.add("Excellent");
        Xaxis.setValueFormatter(new XAxisValueFormatter(xLabels));
        YAxis yright = skillRatingChart.getAxisRight();
        yright.setDrawAxisLine(true);
        yright.setDrawGridLines(false);
        yright.isEnabled();

        //Set bar entries and add necessary formatting
        setGraphData();

        //Add animation to the graph
        skillRatingChart.animateY(2000);


    }
    public void setGraphData() {

        //Add a list of bar entries
        ArrayList<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(0, 3f));
        entries.add(new BarEntry(1, 8f));
        entries.add(new BarEntry(2, 6f));
        entries.add(new BarEntry(3, 30f));
        entries.add(new BarEntry(4, 50f));
        //Note : These entries can be replaced by real-time data, say, from an API

        BarDataSet dataSet = new BarDataSet(entries,"Horizontal Bar");
        BarData data = new BarData(dataSet);
        skillRatingChart.setData(data);
        skillRatingChart.animateXY(2000, 2000);
        skillRatingChart.invalidate();
        dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        skillRatingChart.setDrawBarShadow(true);
        dataSet.setBarShadowColor(Color.argb(40, 150, 150, 150));


    }
}

任何帮助我都会很高兴!。请帮助并告诉我如何将此图表更改为我想要实现的目标。

您可以使用这个库在一个很棒的 UI 已经内置的

中显示收视率

将此添加到您的项目级别 Gradle 文件

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

将此添加到您的模块 Gradle 文件

implementation 'com.github.Inconnu08:android-ratingreviews:1.2.0'

像这样添加评分和评论

RatingReviews ratingReviews = (RatingReviews) findViewById(R.id.rating_reviews);

    int colors[] = new int[]{
            Color.parseColor("#0e9d58"),
            Color.parseColor("#bfd047"),
            Color.parseColor("#ffc105"),
            Color.parseColor("#ef7e14"),
            Color.parseColor("#d36259")};

    int raters[] = new int[]{
            new Random().nextInt(100),
            new Random().nextInt(100),
            new Random().nextInt(100),
            new Random().nextInt(100),
            new Random().nextInt(100)
    };

    ratingReviews.createRatingBars(100, BarLabels.STYPE1, colors, raters);

要获得更多正确的文档,请使用此 link RatingReviews