MPAndrodChart 绘制分组柱状图实例 作者: Chuwen 时间: 2020-10-29 分类: Android # MPAndrodChart 绘制分组柱状图实例 自己摸索了许久写出来的,只给出死方法例子,有些地方有注释,不懂得话可以评论区留言 # 布局文件 ``` ``` # Java 代码 > 我直接分享方法了 > 你可以直接在 `XXXXXActivity` 的 `onCreate()` 方法调用本方法即可 ``` //我直接分享方法了 //你直接再 Activity 的 onCreate() 方法调用本方法即可 private void drawBarChart() { //这个不必多解释吧 BarChart barChart = findViewById(R.id.barchart1); barChart.getLegend().setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); barChart.getLegend().setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); final Description description = new Description(); description.setPosition(200, 60); description.setText("道路拥堵状况");//设置简介 description.setTextSize(10f); barChart.setDescription(description); List barEntryList = new ArrayList<>(); barEntryList.add(new BarEntry(1, 4)); barEntryList.add(new BarEntry(2, 5)); barEntryList.add(new BarEntry(3, 4)); barEntryList.add(new BarEntry(4, 3)); barEntryList.add(new BarEntry(5, 2)); barEntryList.add(new BarEntry(6, 1)); barEntryList.add(new BarEntry(7, 3)); List barEntryList2 = new ArrayList<>(); barEntryList2.add(new BarEntry(1, 5)); barEntryList2.add(new BarEntry(2, 4)); barEntryList2.add(new BarEntry(3, 3)); barEntryList2.add(new BarEntry(4, 2)); barEntryList2.add(new BarEntry(5, 1)); barEntryList2.add(new BarEntry(6, 2)); barEntryList2.add(new BarEntry(7, 3)); List barEntryList3 = new ArrayList<>(); barEntryList3.add(new BarEntry(1, 4)); barEntryList3.add(new BarEntry(2, 2)); barEntryList3.add(new BarEntry(3, 3)); barEntryList3.add(new BarEntry(4, 2)); barEntryList3.add(new BarEntry(5, 4)); barEntryList3.add(new BarEntry(6, 1)); barEntryList3.add(new BarEntry(7, 1)); List barEntryList4 = new ArrayList<>(); barEntryList4.add(new BarEntry(1, 2)); barEntryList4.add(new BarEntry(2, 1)); barEntryList4.add(new BarEntry(3, 5)); barEntryList4.add(new BarEntry(4, 2)); barEntryList4.add(new BarEntry(5, 3)); barEntryList4.add(new BarEntry(6, 4)); barEntryList4.add(new BarEntry(7, 4)); List barEntryList5 = new ArrayList<>(); barEntryList5.add(new BarEntry(1, 1)); barEntryList5.add(new BarEntry(2, 2)); barEntryList5.add(new BarEntry(3, 2)); barEntryList5.add(new BarEntry(4, 3)); barEntryList5.add(new BarEntry(5, 5)); barEntryList5.add(new BarEntry(6, 4)); barEntryList5.add(new BarEntry(7, 3)); List barEntryList6 = new ArrayList<>(); barEntryList6.add(new BarEntry(1, 2)); barEntryList6.add(new BarEntry(2, 1)); barEntryList6.add(new BarEntry(3, 1)); barEntryList6.add(new BarEntry(4, 2)); barEntryList6.add(new BarEntry(5, 4)); barEntryList6.add(new BarEntry(6, 3)); barEntryList6.add(new BarEntry(7, 3)); List barEntryList7 = new ArrayList<>(); barEntryList7.add(new BarEntry(1, 4)); barEntryList7.add(new BarEntry(2, 1)); barEntryList7.add(new BarEntry(3, 2)); barEntryList7.add(new BarEntry(4, 2)); barEntryList7.add(new BarEntry(5, 3)); barEntryList7.add(new BarEntry(6, 2)); barEntryList7.add(new BarEntry(7, 1)); BarDataSet barDataSet = new BarDataSet(barEntryList, "学院路"); BarDataSet barDataSet2 = new BarDataSet(barEntryList2, "联想路"); BarDataSet barDataSet3 = new BarDataSet(barEntryList3, "医院路"); BarDataSet barDataSet4 = new BarDataSet(barEntryList4, "幸福路"); BarDataSet barDataSet5 = new BarDataSet(barEntryList5, "环城快速路"); BarDataSet barDataSet6 = new BarDataSet(barEntryList6, "环城高速"); BarDataSet barDataSet7 = new BarDataSet(barEntryList7, "停车场"); barDataSet.setColor(Color.parseColor("#bf382d")); barDataSet2.setColor(Color.parseColor("#2f4455")); barDataSet3.setColor(Color.parseColor("#60a0a7")); barDataSet4.setColor(Color.parseColor("#d48164")); barDataSet5.setColor(Color.parseColor("#91c6af")); barDataSet6.setColor(Color.parseColor("#769e83")); barDataSet7.setColor(Color.parseColor("#c98624")); BarData barData = new BarData(barDataSet, barDataSet2, barDataSet3, barDataSet4, barDataSet5, barDataSet6, barDataSet7); barData.setDrawValues(false); barChart.getXAxis().setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { switch ((int) value) { case 0: return "周一"; case 10: return "周二"; case 20: return "周三"; case 30: return "周四"; case 40: return "周五"; case 50: return "周六"; case 60: default: return "周日"; } } }); barChart.getAxisLeft().setYOffset(25f);//这样会向下偏移 50% barChart.getAxisLeft().setAxisMinimum(0f); barChart.getAxisLeft().setAxisMaximum(5f); barChart.getAxisLeft().setLabelCount(6, true);//设置间距 barChart.getAxisLeft().setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { switch ((int) value) { case 1: return "畅通"; case 2: return "缓行"; case 3: return "一般拥堵"; case 4: return "中度拥堵"; case 5: return "严重拥堵"; default: return ""; } } }); barChart.getAxisRight().setAxisMinimum(0f); barChart.getAxisRight().setAxisMaximum(5f); barChart.getAxisRight().setLabelCount(6, true);//设置间距 barChart.getXAxis().setTextSize(12f); barChart.getXAxis().setYOffset(-0); barChart.getXAxis().setAxisMinimum(1); barChart.getXAxis().setAxisMaximum(70); barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); barChart.getXAxis().setCenterAxisLabels(true); barChart.getXAxis().setDrawAxisLine(false);//横线 barChart.getXAxis().setDrawGridLines(false);//垂线 barChart.setData(barData);//先设置数据 float groupSpace = 2f;//0.3 * DaraSet float barSpace = 0.28f; //0.04 * DataSet //0.1 * DataSet //然后再设置分组柱状图 barChart.groupBars(.7f, groupSpace, barSpace); barChart.invalidate(); } ``` # 运行截图 ![Snipaste_2020-10-29_20-30-36.jpg][1] [1]: https://cdn.nowtime.cc/2020/10/29/1302616410.jpg 标签: MPAndroidChart, Android 笔记