Android MPAndroidChart快速实现柱形图表

一、添加依赖或者添加jar包

我添加依赖怎么都失败
所以我就下了个jar包,放进lib目录
这里就不
然后添加进library就OK

二、布局文件中添加图表控件

随便添加在哪

<com.github.mikephil.charting.charts.BarChart
            android:id="@+id/bar_chart2"
            android:layout_width="match_parent"
            android:layout_height="300dp" />

三、添加代码

将下面的各个方法函数添加进对应的Activcity的代码中

private void initBarChart() {
        barChart = findViewById(R.id.bar_chart2);
        barChart.getDescription().setEnabled(false); // 不显示描述
        barChart.setExtraOffsets(20, 20, 20, 20); // 设置饼图的偏移量,类似于内边距 ,设置视图窗口大小
        setAxis(); // 设置坐标轴
        setLegend(); // 设置图例
        setData();  // 设置数据
    }

    /**
     * 因为此处的 barData.setBarWidth(0.3f);,也就是说柱子的宽度是0.3f
     * 所以第二个柱子的值要比第一个柱子的值多0.3f,这样才会并列显示两根柱子
     */
    private void setData() {
        List<IBarDataSet> sets = new ArrayList<>();
        // 此处有两个DataSet,所以有两条柱子,BarEntry()中的x和y分别表示显示的位置和高度
        // x是横坐标,表示位置,y是纵坐标,表示高度
        List<BarEntry> barEntries1 = new ArrayList<>();
        barEntries1.add(new BarEntry(0, 1100f));
        barEntries1.add(new BarEntry(1, 1000f));
        barEntries1.add(new BarEntry(2, 900f));
        barEntries1.add(new BarEntry(3, 800f));
        barEntries1.add(new BarEntry(4, 700f));
        barEntries1.add(new BarEntry(5, 600f));
        barEntries1.add(new BarEntry(6, 500f));
        barEntries1.add(new BarEntry(7, 400f));
        barEntries1.add(new BarEntry(8, 300f));
        barEntries1.add(new BarEntry(9, 200f));
        BarDataSet barDataSet1 = new BarDataSet(barEntries1, "");
        barDataSet1.setValueTextColor(Color.RED); // 值的颜色
        barDataSet1.setValueTextSize(15f); // 值的大小
        barDataSet1.setColor(Color.parseColor("#1AE61A")); // 柱子的颜色
        barDataSet1.setLabel("蔬菜"); // 设置标签之后,图例的内容默认会以设置的标签显示
        // 设置柱子上数据显示的格式
        barDataSet1.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                // 此处的value默认保存一位小数
                return value + "斤";
            }
        });

        sets.add(barDataSet1);
//        //********第二种柱形*********
        List<BarEntry> barEntries2 = new ArrayList<>();
        barEntries2.add(new BarEntry(0.3f, 210f));
        barEntries2.add(new BarEntry(1.3f, 450f));
        barEntries2.add(new BarEntry(2.3f, 430f));
        barEntries2.add(new BarEntry(3.3f, 440f));
        barEntries2.add(new BarEntry(4.3f, 180f));
        barEntries2.add(new BarEntry(5.3f, 180f));
        barEntries2.add(new BarEntry(6.3f, 180f));
        barEntries2.add(new BarEntry(7.3f, 180f));
        barEntries2.add(new BarEntry(8.3f, 180f));
        barEntries2.add(new BarEntry(9.3f, 180f));
        BarDataSet barDataSet2 = new BarDataSet(barEntries2, "");
        // 不显示第二根柱子上的值
        barDataSet2.setDrawValues(true); // 不显示值
        barDataSet2.setColor(Color.parseColor("#F7F709"));//柱子颜色
        barDataSet2.setValueTextColor(Color.RED); // 值的颜色
        barDataSet2.setValueTextSize(15f); // 值的大小
        barDataSet2.setLabel("水果");
        sets.add(barDataSet2);


        BarData barData = new BarData(sets);
        barData.setBarWidth(0.3f); // 设置柱子的宽度
        barChart.setData(barData);
    }

    private void setLegend() {
        Legend legend = barChart.getLegend();
        legend.setFormSize(12f); // 图例的图形大小
        legend.setTextSize(15f); // 图例的文字大小
        legend.setDrawInside(true); // 设置图例在图中
        legend.setOrientation(Legend.LegendOrientation.VERTICAL); // 图例的方向为垂直
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); //显示位置,水平右对齐
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // 显示位置,垂直上对齐
        // 设置水平与垂直方向的偏移量
        legend.setYOffset(55f);
        legend.setXOffset(30f);
    }

    private void setAxis() {
        // 设置x轴
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);  // 设置x轴显示在下方,默认在上方
        xAxis.setDrawGridLines(false); // 将此设置为true,绘制该轴的网格线。
        xAxis.setLabelCount(10);  // 设置x轴上的标签个数
        xAxis.setTextSize(15f); // x轴上标签的大小
        final String labelName[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        // 设置x轴显示的值的格式
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if ((int) value < labelName.length) {
                    return labelName[(int) value];
                } else {
                    return "";
                }
            }
        });
        xAxis.setYOffset(15); // 设置标签对x轴的偏移量,垂直方向

        // 设置y轴,y轴有两条,分别为左和右
        YAxis yAxis_right = barChart.getAxisRight();
        yAxis_right.setAxisMaximum(1200f);  // 设置y轴的最大值
        yAxis_right.setAxisMinimum(0f);  // 设置y轴的最小值
        yAxis_right.setEnabled(false);  // 不显示右边的y轴

        YAxis yAxis_left = barChart.getAxisLeft();
        yAxis_left.setAxisMaximum(1200f);
        yAxis_left.setAxisMinimum(0f);
        yAxis_left.setTextSize(15f); // 设置y轴的标签大小
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值