一. 配置
(1)model下build.gradle implementation 'com.github.PhilJay:MPAndroidChart:v3.0.1'
(2)project下build.gradle allprojects { repositories { jcenter() maven { url 'https://jitpack.io' } } }
二. 使用
(1)main_layout布局 <?xml version="1.0" encoding="utf-8"?>
(2)MainActivity.java /** * 曲线图 */ public class MainActivity extends AppCompatActivity { LineChart lineChart; XAxis xAxis; YAxis leftYAxis; YAxis rightYaxis; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ lineChart = findViewById(R.id.lineChart); //显示边界 lineChart.setDrawBorders(false); //显示网格线 lineChart.setDrawGridBackground(false); /***XY轴的设置***/ xAxis = lineChart.getXAxis(); leftYAxis = lineChart.getAxisLeft(); rightYaxis = lineChart.getAxisRight(); //X轴设置显示位置在底部 xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setAxisMinimum(0f); xAxis.setGranularity(1f); //保证Y轴从0开始,不然会上移一点 leftYAxis.setAxisMinimum(0f); rightYaxis.setAxisMinimum(0f); xAxis.setDrawGridLines(false); rightYaxis.setDrawGridLines(false); leftYAxis.setDrawGridLines(true); leftYAxis.enableGridDashedLine(10f, 10f, 0f); rightYaxis.setEnabled(false); /***折线图例 标签 设置***/ Legend legend = lineChart.getLegend(); //设置显示类型,LINE(线) CIRCLE(圆) SQUARE(方) EMPTY(无) 等等 多种方式,查看LegendForm 即可 legend.setForm(Legend.LegendForm.LINE); legend.setTextSize(25f); //描述显示位置 右上方 legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); //是否绘制在图表里面 legend.setDrawInside(false); //设置数据 List entries = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { entries.add(new Entry(i, random.nextInt(10) * 8)); Log.i("1111111", i + " : " + random.nextInt(10) * 8); } Description description = new Description(); // description.setText("需要展示的内容"); description.setEnabled(false); lineChart.setDescription(description); //一个LineDataSet就是一条线 LineDataSet lineDataSet = new LineDataSet(entries, "PM2.5"); lineChart.setData(initLine(lineDataSet, getResources().getColor(R.color.colorYell))); setMarkerView(); } /** * 初始化曲线,多条多次调用 * * @param lineDataSet * @param color * @return */ private LineData initLine(LineDataSet lineDataSet, int color) { //设置颜色 // lineDataSet.setColor(color); // lineDataSet.setCircleColor(color); //设置曲线值的圆点 lineDataSet.setDrawCircleHole(true); lineDataSet.setValueTextSize(10f); //内部填充 lineDataSet.setDrawFilled(true); lineDataSet.setFormLineWidth(1f); lineDataSet.setFormSize(15.f); //设置为曲线图 lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER); LineData data1 = new LineData(); data1.addDataSet(lineDataSet); return data1; } /** * 点击后弹出方框显示数据 */ public void setMarkerView() { LineChartMarkView mv = new LineChartMarkView(this, xAxis.getValueFormatter()); mv.setChartView(lineChart); lineChart.setMarker(mv); lineChart.invalidate(); } }
(3)用到的工具类LineChartMarkView.java public class LineChartMarkView extends MarkerView { private TextView tvDate; private TextView tvValue; private IAxisValueFormatter xAxisValueFormatter; DecimalFormat df = new DecimalFormat(".00"); public LineChartMarkView(Context context, IAxisValueFormatter xAxisValueFormatter) { super(context, R.layout.layout_markview); this.xAxisValueFormatter = xAxisValueFormatter; tvDate = findViewById(R.id.tv_date); tvValue = findViewById(R.id.tv_value); } @SuppressLint("SetTextI18n") @Override public void refreshContent(Entry e, Highlight highlight) { //展示自定义X轴值 后的X轴内容 tvDate.setText(xAxisValueFormatter.getFormattedValue(e.getX(), null)); tvValue.setText("温度:" + df.format(e.getY())); super.refreshContent(e, highlight); } @Override public MPPointF getOffset() { return new MPPointF(-(getWidth() / 2), -getHeight()); } }
如果有报错就是选错包了,换一个试一下
至此。