MPAndroidChart 柱形图事例

本文详细介绍了如何使用MPAndroidChart库在Android应用中创建和定制柱形图。包括图表参数设置、数据加载和图形刷新,以及缩放操作的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 


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);

 

### 使用 MPAndroidChart 库在 Android 中绘制柱状图 要在 Android 应用中使用 MPAndroidChart 库绘制柱状图,需先确保项目已引入该库。通常通过 Gradle 文件配置依赖项来完成。 #### 添加 BarChart 到布局文件 为了展示柱状图,应在 XML 布局文件内定义 `BarChart` 组件: ```xml <com.github.mikephil.charting.charts.BarChart android:id="@+id/barChart" android:layout_width="match_parent" android:layout_height="300dp"/> ``` 此部分代码片段用于声明一个占据父容器宽度并具有固定高度的 `BarChart` 控件[^1]。 #### 初始化 BarChart 并设置数据集 接下来,在 Activity 或 Fragment 的 Java/Kotlin 代码里初始化这个控件,并为其准备相应的数据源。下面是一段简单的 Kotlin 示例代码用来填充一些随机数值到图表上: ```kotlin import com.github.mikephil.charting.data.* import java.util.* fun setupBarChart(barChart: BarChart){ val entries = mutableListOf<BarEntry>() // 创建条目列表 for (i in 0..4) { entries.add(BarEntry(i.toFloat(), Random().nextInt(10).toFloat())) } // 设置数据集合 val barDataSet = BarDataSet(entries, "Label") val data = BarData(barDataSet) // 配置样式和其他属性 with(barChart){ setFitBars(true) description.isEnabled = false xAxis.setDrawGridLines(false) axisLeft.setDrawGridLines(false) legend.isEnabled = true this.data = data invalidate() //刷新图表 } } ``` 上述函数接收一个 `BarChart` 参数作为输入参数,负责构建一组带有标签的数据点,并将这些信息绑定至给定的对象实例上[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值