首先声明下本人是第一次写文章,排版可能会有点乱,有不清楚的地方还请提出来,谢谢。
Android studio集成MpAndroidChart
dependencies {
api"com.github.PhilJay:MPAndroidChart:3.0.3"
}
chart的基本属性
//背景颜色
lineChart.setBackgroundColor(Color.WHITE);
lineChart.setGridBackgroundColor(Color.WHITE);
// 隐藏右边Y轴
lineChart.getAxisRight().setEnabled(false);
//设置是否可以缩放
lineChart.setScaleEnabled(false);
//设置是否可以通过双击屏幕放大图表。默认是true
lineChart.setDoubleTapToZoomEnabled(false);
//设置描述
lineChart.getDescription().setEnabled(false);
//设置按比例放缩柱状图
lineChart.setPinchZoom(true);
//是否展示网格线
lineChart.setDrawGridBackground(false);
//是否有触摸事件
lineChart.setTouchEnabled(true);
//禁止y轴缩放
lineChart.setScaleYEnabled(false);
//禁止Y轴可以拖拽
lineChart.setDragYEnabled(false);
//以防止值由敲击姿态被突出显示。
lineChart.setHorizontalFadingEdgeEnabled(true);
lineChart.setHighlightPerDragEnabled(false);
//折线图例 标签 设置
Legend legend =lineChart.getLegend();
legend.setTextSize(12);
// 图例间隔
legend.setXEntrySpace(30);
// 图例和文字间隔
legend.setFormToTextSpace(10);
// 线高
legend.setFormLineWidth(4);
// 线宽
legend.setFormSize(30);
//标签中文字的颜色
legend.setTextColor(Color.parseColor("#333333"));
legend.setForm(Legend.LegendForm.LINE);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(false);
//x坐标轴设置
XAxis xAxis =lineChart.getXAxis();
// x轴对齐位置
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisLineColor(Color.parseColor("#DCDCDC"));
// 隐藏网格线
xAxis.setDrawGridLines(false);
// 隐藏X轴横线
xAxis.setDrawAxisLine(false);
xAxis.setGranularity(1f);
xAxis.setTextColor(Color.parseColor("#999999"));
xAxis.setTextSize(11);
//y轴设置
YAxis leftAxis =lineChart.getAxisLeft();
//此代码段处理y轴文本居做显示
ViewPortHandler viewPortHandler =lineChart.getViewPortHandler();
Transformer transformer =lineChart.getTransformer(leftAxis.getAxisDependency());
lineChart.setRendererLeftYAxis(new YAxisRendererUtil(viewPortHandler, leftAxis, transformer));
leftAxis.setXOffset(10);
leftAxis.setTextColor(Color.parseColor("#999999"));
leftAxis.setTextSize(12);
leftAxis.setLabelCount(4, true);
leftAxis.setDrawGridLines(false);
// Y轴在图表内部
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
//设置y轴的线是否显示
leftAxis.setDrawAxisLine(false);
leftAxis.setDrawLimitLinesBehindData(false);
leftAxis.setGranularity(1f);
leftAxis.setAxisMinimum(0f);
leftAxis.setYOffset(-5);
这些相信小伙伴们都是很熟悉的了,后续我会更新以下几个功能点:
1.添加数据到chart
2.添加marker
3.自定义marker
4.讲解chart的onTouch事件
5.自定义OnTouch事件
6.等等个性化的效果
后面我会抽时间写个包含以上所有功能点的demo放到gitHub上,欢迎大家star。
今天先写到这里。
网友评论