美文网首页面试题
MpAndroidChart 使用

MpAndroidChart 使用

作者: 白兔糖丶 | 来源:发表于2019-07-23 10:50 被阅读0次

这段时间一直在搞图表,属性很多,现在记录一下,方便下次使用。

图表整体设置:

添加图表依赖,可以查找图表库最新版本

    //图表
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

chart.setBackgroundColor(color);//设置图表的背景颜色
 chart.setNoDataText("");//图表在没有数据时的提示文字
chart.setScaleEnabled(false);//禁止缩放图表
 chart.setDragEnabled(false);//禁止平移图表
 chart.setExtraOffsets(35, 10, 15, 15);//和四周相隔一段距离,如果图表周围有遮挡可以设置

  Legend legend = chartDistance.getLegend();//获取图例对象
  legend .setEnabled(false);//是否显示图例
//设置图例位置
  legend .setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
  legend .setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
  legend .setOrientation(Legend.LegendOrientation.HORIZONTAL);
  legend .setTextSize(11f);//设置字体大小

//x轴设置
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //x轴的位置
 xAxis.setDrawGridLines(false);//设置竖直网格
 xAxis.setGranularity(1f); // only intervals of 1 day
  xAxis.setLabelCount(7);//强制设置x轴个数
xAxis.setTextColor(Color.parseColor("#999999"));//设置字体颜色
xAxis.setDrawLabels(false);//设置是否显示值
xAxis.setDrawGridLines(false);//是否显示网格线
xAxis.setSpaceTop(15f);//距离顶部距离
//自定义x轴的值
 xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisValues));//设置x坐标
xAxis.setValueFormatter(new ValueFormatter() {//取整或者自定义格式
            @Override
            public String getFormattedValue(float value) {
                int leftValue = (int) value;
                return leftValue + "";
            }
        });

  //柱状图
BarDataSet  set1 = new BarDataSet(values, "");
set1.setColor(Color.parseColor("#278DFF"));//设置柱子颜色
 set1.setDrawValues(false);//柱子上不显示值
BarData data = new BarData(dataSets);
data.setBarWidth(0.5f);//设置柱子的宽度

//设置倾斜
XAxis xl = mChart.getXAxis();
xl.setLabelRotationAngle(-20);//设置x轴字体显示角度
xl.setPosition(XAxisPosition.BOTTOM);//设置X轴的位置TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
xl.setSpaceBetweenLabels(int spaceCharacters)//设置Lable之间的距离(字符),小于距离将不显示,需要放大图标才能看到

部分参考来自:https://www.jianshu.com/p/9fc2b3942163

相关文章

网友评论

    本文标题:MpAndroidChart 使用

    本文链接:https://www.haomeiwen.com/subject/qoanlctx.html