1.大神的官网

https://github.com/PhilJay/MPAndroidChart

2.使用方法

(1)在Project下的build.gradle文件中添加maven链接
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}
(2)在app下的build.gradle中添加依赖,//在官网查看最新版本
dependencies{
 	implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
 }
(3)xml文件
<com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"/>
(4)Java文件
lineChart = findViewById(R.id.chart);
//设置lineChart
lineChart.fitScreen();// 设置自适应屏幕

//设置折线图右下角不添加描述,当然,需要添加的话,就自己添加
Description description = new Description();
description.setText("");
lineChart.setDescription(description);

//设置不显示折线图左下角的标签,同上
Legend legend = lineChart.getLegend();
legend .setEnabled(false);

//设置X轴
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴在折线图的底部
xAxis.setTextSize(11f);//设置X轴的坐标字体的大小
xAxis.setTextColor(Color.BLACK);//设置X轴的坐标字体的颜色
xAxis.setDrawAxisLine(true);//设置显示X轴

xAxis.setAxisLineColor(Color.rgb(238,238,238));
/*注:  设置X轴的颜色,这里我使用在values/colors中自己定义的颜色不行
*即这样设置无效
*<color name="colorGrey">#EEEEEE</color> 
*xAxis.setAxisLineColor(R.color.colorGrey); 
* /
xAxis.setAxisLineWidth(1.1f);//设置X轴的宽度

//设置X轴显示的坐标的样式
String[] xName= new String[]{"06月","07月","08月","09月","10月","11月"};
IAxisValueFormatter xFormatter= new IAxisValueFormatter()
{
     @Override
     public String getFormattedValue(float value, AxisBase axis)
     {
         return xName[(int) value];
     }
 };
xAxis.setValueFormatter(xFormatter);
 xAxis.setGranularity(1f);//如果想让坐标轴按照你设置的xName数组显示,就需要这一句,否则,可能会出现不是6个坐标

//设置折线图上的显示的数据的格式
//https://github.com/PhilJay/MPAndroidChart/wiki/The-ValueFormatter-interface
lineData.setValueFormatter(new MyIValueFormatter(){});//这里可以参照官网,以自己想定义的格式重写

//给折线图添加数据
private List<Entry> vals = new ArrayList<Entry>();//Entry是库里的
vals.add(new Entry(0f,150.20f));//x轴,y轴
vals.add(new Entry(1f,1355f));
vals.add(new Entry(2f,203f));
//...

LineDataSet set = new LineDataSet(vals, "");//前面我不想显示标签,所以这里没有设置为空
LineData lineData = new LineData(set);//这里可以添加多个数据集
lineChart.setData(lineData);
lineChart.invalidate();

//设置折线图的样式
set.setLineWidth(1.5f);//设置折线图的宽度
set.setHighLightColor(Color.rgb(255,255,255));//设置点击折线图上点的时候的颜色,我不想有这个,所以设置成白色了
set.setColor(Color.rgb(255,80,62));//设置折线图的颜色
set.setCircleRadius(3f);//设置折线图的点的圆的半径
set.setCircleColor(Color.rgb(255,80,62));//设置折线图的点的颜色
set.setCircleHoleColor(Color.rgb(255,80,62));//设置折线图的圆的中心的颜色
set.setValueTextSize(10f);//设置折线图上点的字体的大小
set.setValueTextColor(Color.rgb(255,80,62));//设置折线图上点的字体的颜色

(5)可能出现的错误

在我准备运行的时候出现了问题:
More than one file was found with OS independent path 'META-INF/proguard/androidx-annotations.pro’
是因为这个文件在Jar包中出现了多次,此时的解决方法是
在app下的build.gradle文件中加上这句话

android {
    ...
    defaultConfig {
        ...
        packagingOptions {
            exclude 'META-INF/proguard/androidx-annotations.pro'
        }
    }
    ...
}

3.总结

这个库真的是太好用了,官网写的很详细,有什么不懂的就去官网看看。现在我还只是学会了用LineChart,等有时间了把大神的源码下下来看学习学习,真的好厉害,膜拜~,希望自己有一天也能从小白变成大神,哈哈哈。。。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐