MPAndroidChart 柱形图事例

项目中需要柱行图 ,效果如下图:

 

build中MPchart 版本如下:

implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

 

 

 

 

1. 初始化柱形图 基本 参数

 chart.setDrawBarShadow(false);
        chart.setDrawValueAboveBar(true);
        chart.setBackgroundColor(getResources().getColor(R.color.white));
        chart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        chart.setMaxVisibleValueCount(10);

        // scaling can now only be done on x- and y-axis separately
        chart.setPinchZoom(false);

        chart.setDrawGridBackground(false);

        // X 轴显示的类别内容
        mXAxis = chart.getXAxis();
        mXAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        mXAxis.setTextSize(12); // 设置字体大小
        mXAxis.setDrawGridLines(false);
        mXAxis.setGranularity(1f); // only intervals of 1 day 隔几个显示一下 X轴名称
        mXAxis.setLabelCount(7);
        mXAxis.setYOffset(2);  // 默认是5

        // 左 Y 轴设置
        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setLabelCount(8, false);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);// 不显示右边Y轴
//        rightAxis.setDrawGridLines(false);
//        rightAxis.setTypeface(tfLight);
//        rightAxis.setLabelCount(8, false);
//        rightAxis.setValueFormatter(custom);
//        rightAxis.setSpaceTop(15f);
//        rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        // 标签显示 
        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(true);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setFormSize(9f);
        l.setTextSize(15f);

        l.setXEntrySpace(4f);

 

2. 初始化数据 并进行图形的刷新显示 

 

 private void loadBarData(List<PayCountResultBean.BodyBean.ExpensesBean> rcyList) {
        double allCount = 0;
        ArrayList xStrs = new ArrayList();
        ArrayList<BarEntry> values = new ArrayList<>();
        for (int i = 0; i < rcyList.size(); i++) {
            PayCountResultBean.BodyBean.ExpensesBean rcyListBean = rcyList.get(i);
            allCount += rcyListBean.getAmount();
            xStrs.add(rcyListBean.getPayTypeName());
            values.add(new BarEntry(i, (float) rcyListBean.getAmount()));
        }

        mXAxis.setValueFormatter(new IndexAxisValueFormatter(xStrs));

        BarDataSet set1 = new BarDataSet(values, "支付统计");

        set1.setDrawIcons(false);

        set1.setColors(ColorTemplate.MATERIAL_COLORS);

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

        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
//            data.setValueTypeface(tfLight);
        data.setBarWidth(0.45f); //修改柱形每项的宽度

        chart.setData(data);

        chart.postInvalidate();

    }

 

3.柱形图 缩放代码 

float scaleNum = 1;
//
//        if(rcyList.size() > 0){
//            scaleNum = ((float)rcyList.size()+0.5f) / 5;
//        }
//
//        LogUtil.e(scaleNum + " scaleNum --- ");
//
//
//        Matrix mMatrix = new Matrix();
//        mMatrix.postScale(scaleNum, 1f);
//        chart.getViewPortHandler().refresh(mMatrix, chart, true);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值