MPAndroidChart 开启统计图之旅——水平柱状图(HorizontalBarChart)

先放一张demo图吧,供大家需求:

在这里插入图片描述
具体操作如下,几乎都是贴了代码,相关解释在注释里。

1. app.gradle中依赖库:

   dependencies {
            implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
      }

2. 布局文件中定义布局:

<com.github.mikephil.charting.charts.HorizontalBarChart
    android:id="@+id/chart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

3.java类中获取

public class HorizontalBarChartActivity extends Activity {
    private List<DictModel> dataList=new ArrayList<>();
    private HorizontalBarChart horizontalBarChart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal_chart);

        horizontalBarChart=findViewById(R.id.chart);

        dataList.add(new DictModel(90,50,"一月",100));
        dataList.add(new DictModel(60,30,"二月",100));
        dataList.add(new DictModel(30,20,"三月",100));
        dataList.add(new DictModel(90,50,"四月",100));
        dataList.add(new DictModel(60,30,"五月",100));
        dataList.add(new DictModel(30,20,"六月",100));

        initChart();
        setData(dataList);
    }


    private void initChart(){
        horizontalBarChart.setDrawBarShadow(false);//是否展示阴影
        horizontalBarChart.setDrawValueAboveBar(true);//将Y数据显示在点的上方
        horizontalBarChart.getDescription().setEnabled(false);//隐藏描述
        horizontalBarChart.setMaxVisibleValueCount(60);//最多显示60个条目
        horizontalBarChart.setPinchZoom(false);//如果设置为true,挤压缩放被打开。如果设置为false,x和y轴可以被单独挤压缩放。
        horizontalBarChart.setDrawGridBackground(false);//是否展示表格颜色
        horizontalBarChart.getAxisLeft().setEnabled(false);//隐藏上侧Y轴   默认是上下两侧都有Y轴

        //X轴
        XAxis xl = horizontalBarChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);//底部
        xl.setDrawAxisLine(true);//是否展示轴线
        xl.setDrawGridLines(false);//去掉网格线
        xl.setGranularity(10f);//每隔10f展示x轴内容
        xl.setDrawLabels(true);//设置为true打开绘制轴的标签
        xl.setLabelCount(dataList.size(), false);

        xl.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                if(((int)value/10)<dataList.size()){
                    return dataList.get(((int)value/10)).title;
                }else {
                    return "";
                }
            }
        });

        //获取左侧Y轴
        YAxis yl = horizontalBarChart.getAxisLeft();
        yl.setDrawAxisLine(true);//是否展示轴线
        yl.setDrawGridLines(true);
        yl.setAxisMinimum(0f);//从0开始

        //获取右侧Y轴
        YAxis yr = horizontalBarChart.getAxisRight();
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(false);
        yr.setAxisMinimum(0f);

        //设置X轴范围两侧柱形条是否显示一半
        horizontalBarChart.setFitBars(true);
        //设置Y轴动画
        horizontalBarChart.animateY(2500);

        //图例展示
        Legend l = horizontalBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setXEntrySpace(4f);
        l.setEnabled(false);
    }

    private void setData(List<DictModel> datas) {

        float barWidth = 9f;
        float spaceForBar = 10f;
        ArrayList<BarEntry> values = new ArrayList<>();

        for (int i = 0; i < datas.size(); i++) {
            values.add(new BarEntry( i*spaceForBar, new float[]{datas.get(i).realCnt,
                    datas.get(i).totalCnt-datas.get(i).realCnt}));
        }

        BarDataSet set = new BarDataSet(values, "");
        set.setStackLabels(new String[]{"Stack 1", "Stack 2"});
        set.setColors(Color.rgb(22, 145, 196), Color.rgb(208, 233, 243));
        set.setValueTextColor(Color.rgb(61, 165, 255));
        set.setValueTextSize(0f);
        set.setHighLightAlpha(0);
        set.setDrawIcons(false);

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set);

        BarData data = new BarData(dataSets);
        data.setValueTextSize(0f);
        data.setBarWidth(barWidth);
        horizontalBarChart.setData(data);
    }

    class DictModel{
        public float rate;
        public int realCnt;
        public String title;
        public int totalCnt;

        public DictModel(float rate, int realCnt, String title, int totalCnt) {
            this.rate = rate;
            this.realCnt = realCnt;
            this.title = title;
            this.totalCnt = totalCnt;
        }
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值