MPAndroidChart 折线图(Line Chart) 设置 Y轴值间隔 作者: Chuwen 时间: 2020-11-05 分类: Android 评论 # MPAndroidChart 折线图(Line Chart) 设置 Y轴值间隔 > 也就是每隔多少就分隔 ```java LieChart weizhangfenxiLineChart = (LineChart) findViewById(R.id.weizhangfenxi_line_chart); //设置 Y Left 值标签间隔 weizhangfenxiLineChart.getAxisLeft().setGranularity(50f); //设置 Y Right 值标签间隔 weizhangfenxiLineChart.getAxisRight().setGranularity(50f); ``` # 运行效果图 ![MPAndroidChart 折线图(Line Chart) 设置 Y轴值间隔][1] [1]: https://nowtime.cc/usr/uploads/2020/11/3884334187.jpg
MPAndroidChart 设置图标两边间距(偏移量)防止文字过长遮挡住 作者: Chuwen 时间: 2020-10-30 分类: Android 1 条评论 # 代码 ``` LineChart linechart2=(LineChart)findViewById(R.id.linechart2); linechart2.setExtraLeftOffset(100f);//左间距 linechart2.setExtraRightOffset(80f);//右间距 ``` ![未命名图片.png][1] [1]: https://cdn.nowtime.cc/2020/10/30/2262053361.png
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 柱状图,左(右)Y轴标签(值)向下偏移50% 作者: Chuwen 时间: 2020-10-29 分类: Android 评论 # 代码 > Y轴右侧也是同理的,你只需要将 `barChart.getAxisLeft()` 改成 `barChart.getAxisRight()` 就可以啦 ``` BarChart barChart = findViewById(R.id.barchart1); //...这里省略了部分代码 barChart.getAxisLeft().setYOffset(25f);//这样会向下偏移 50%。我也不知道为啥,自己试出来的 barChart.getAxisLeft().setAxisMinimum(0f);//最小刻度值 barChart.getAxisLeft().setAxisMaximum(5f);//最大刻度值 barChart.getAxisLeft().setLabelCount(6, true);//设置标签个数 ``` # 运行结果 ![MPAndroidChart 柱状图,左Y轴标签(值)向下偏移50%][1] [1]: https://cdn.nowtime.cc/2020/10/29/2068874471.jpg
MPAndroid 画柱状图,顶部显示值 并且设置文字颜色、大小 作者: Chuwen 时间: 2020-10-29 分类: Android 评论 ### 找了许久才发现,需要设置 AxisLeft 最小值 ``` HorizontalBarChart hBarChart = findViewById(R.id.two); YAxis lYAxis = hBarChart.getAxisLeft(); lYAxis.setAxisMinimum(0);//必须要设置这个才会显示 lYAxis.setAxisMaximum(100);//这个可以设置可以不设置 ``` # 结果 ![Snipaste_2020-10-29_15-12-05.jpg][1] # 设置文字颜色、大小 ``` BarDataSet barDataSet = new BarDataSet(values, ""); barDataSet.setValueTextColor(Color.RED); barDataSet.setValueTextSize(20f); ``` # 效果 ![Snipaste_2020-10-29_15-16-45.jpg][2] [1]: https://cdn.nowtime.cc/2020/10/29/1644885309.jpg [2]: https://cdn.nowtime.cc/2020/10/29/217619350.jpg