MPAndroidChart Github地址: https://github.com/PhilJay/MPAndroidChart
eclipse使用时需要的lib 下载地址:https://download.csdn.net/detail/zhang_teng/9397224
最近项目中要用到折线图,,使用到了MPAndroidChart,,具体使用方法如下
布局中:
<strong><com.github.mikephil.charting.charts.LineChart android:id="@+id/LineChart" android:layout_width="match_parent" android:layout_height="match_parent" /></strong>
具体配置:
<span style="font-size:18px;"> <span style="background-color: rgb(204, 204, 204);"><span style="font-family:Microsoft YaHei;"> </span></span></span><span style="font-family:Microsoft YaHei;font-size:14px;"><strong style="background-color: rgb(204, 204, 204);">// 准备X轴数据 ArrayList<String> xVals = new ArrayList<String>(); xVals.add("0"); xVals.add("2013年"); xVals.add("2014年"); xVals.add("2015年"); xVals.add("2016年"); // 准备Y轴数据 ArrayList<Entry> yVals = new ArrayList<Entry>(); yVals.add(new Entry(55.8f, 1));// 应该从0开始,但是0那不需要值就不写啦 yVals.add(new Entry(30.8f, 2)); yVals.add(new Entry(25.5f, 3)); LineDataSet set1 = new LineDataSet(yVals, "测试数据1");// 一条数据线 // 设置数据线的样式 set1.setDrawCubic(true);// 改变折线样式,用曲线。 set1.setCubicIntensity(0.2f);// 设置曲线的平滑度 set1.setLineWidth(1.75f); // 线宽 set1.setCircleSize(3f);// 显示的圆形大小 set1.setColor(Color.YELLOW);// 显示颜色 set1.setCircleColor(Color.BLUE);// 圆形的颜色 set1.setHighLightColor(Color.GRAY); // 高亮的线的颜色 定位线 // 创建数据线的集合 ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); dataSets.add(set1); LineData data = new LineData(xVals, dataSets);// data.setValueFormatter(new MyFormatter());// 设置值的显示格式 linechart.setData(data); // 添加警戒线 YAxis yAxis = linechart.getAxisLeft(); LimitLine ll = new LimitLine(40, "警戒线"); ll.setLineWidth(0.5f); ll.setLineColor(Color.GRAY); ll.setTextColor(Color.GRAY); ll.setTextSize(12); ll.setEnabled(true); yAxis.addLimitLine(ll); // Y轴配置 //yAxis.setStartAtZero(true); // 设置Y轴坐标是否从0开始 yAxis.setInverted(false); // Y轴坐标反转,默认false,下小上大,,, yAxis.setSpaceTop(0); // Y轴坐标距顶有多少距离,, yAxis.setSpaceBottom(0); // Y轴坐标距底有多少距离,, yAxis.setShowOnlyMinMax(false); // 为true的话 Y轴坐标只显示最大值和最小值 yAxis.setLabelCount(10, false);//参数一Y轴坐标的个数,参数二是否不均匀分布,,默认 false即均匀分布 XAxis xAxis = linechart.getXAxis(); // X轴配置 // xAxis.setLabelsToSkip(1); //设置坐标相隔多少,参数是int类型 xAxis.setPosition(XAxisPosition.BOTTOM); // 让x轴在下面 // xAxis.resetLabelsToSkip(); //X轴自动计算坐标相隔多少 。。默认 linechart.getAxisRight().setEnabled(false); // 隐藏右边 的坐标轴 // 隐藏纵横网格线 linechart.getXAxis().setDrawGridLines(false); linechart.getAxisLeft().setDrawGridLines(false); linechart.getAxisRight().setDrawGridLines(false); linechart.setDescription("LineChart图表测试");// 右下角描述 linechart.setDragEnabled(false);// 拖拽禁止 linechart.setScaleEnabled(false);// 缩放禁止 // 隐藏表格的图案示例 Legend legend = linechart.getLegend(); legend.setEnabled(false); // Legend.setWordWrapEnabled(true); //坐标线描述 是否 不允许出边界 linechart.animateY(2000);// 执行Y轴动画 // linechart.invalidate(); // 重新更新显示 // 完毕</strong></span>
MYFormatter代码:
<span style="font-family:Microsoft YaHei;font-size:14px;">import java.text.DecimalFormat; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.formatter.ValueFormatter; import com.github.mikephil.charting.formatter.YAxisValueFormatter; import com.github.mikephil.charting.utils.ViewPortHandler; public class MyFormatter implements ValueFormatter, YAxisValueFormatter { DecimalFormat mFormat; public MyFormatter(){ mFormat = new DecimalFormat("###,###,##0.00"); } public MyFormatter(DecimalFormat format) { this.mFormat = format; } @Override public String getFormattedValue(float value, YAxis yAxis) { // TODO Auto-generated method stub return mFormat.format(value) + "万"; } @Override public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) { // TODO Auto-generated method stub return mFormat.format(value) + "万"; } }</span>
运行效果如下
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/addevelopment/894320.html