Android图表-MPAndroidChart
2020-07-18 本文已影响0人
世外大帝
介绍
项目地址:
https://github.com/PhilJay/MPAndroidChart
导入
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
之前没做过图表类的view,试用了一下,这个项目做个非常好,集合了
线状图、条形图(包括垂直和水平)、散点图、饼状图,蛛网图、气泡图、烛台图(OHCL图表)以及他们的组合
我只需要显示1-2个点,所以选择了散点图, 动画可自行添加
代码
百分比布局已经融入了中 ConstraintLayout
中
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
<com.github.mikephil.charting.charts.ScatterChart
android:id="@+id/scatter_chart"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/a_bar_ll"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintRight_toLeftOf="@id/z_bar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.8" />
private ScatterChart mScatterChart;
private void setChart() {
mScatterChart = findViewById(R.id.scatter_chart);
mScatterChart.getDescription().setEnabled(false);//设置描述文本
// mScatterChart.setOnChartValueSelectedListener(this);//设置数值选择监听
//
mScatterChart.setDrawGridBackground(false);//后台绘制
// mScatterChart.setTouchEnabled(false); //设置支持触控手势
// mScatterChart.setMaxHighlightDistance(10f); // 最大高亮距离
//
//// mScatterChart.setDragEnabled(true);//设置拖动
//// mScatterChart.setScaleEnabled(true);//设置缩放
//
// //设置手势滑动事件
//// mScatterChart.setOnChartGestureListener(this);
//
// // 最大可见个数
mScatterChart.setMaxVisibleValueCount(1);
// //如果禁用,扩展可以在x轴和y轴分别完成
// mScatterChart.setPinchZoom(true);
// 设置图例说明 不设置则默认在图表底部水平显示
// Legend l = mScatterChart.getLegend();
// // 设置垂直和水平方向的位置,不设置则默认底部
// l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
// l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
// // 设置排列方向,不设置则默认水平方向
// l.setOrientation(Legend.LegendOrientation.VERTICAL);
// l.setDrawInside(false); // 是否画在内部
// l.setXOffset(5f); // 偏移量
// l.setEnabled(false); // 设置不可用
// x轴
XAxis xl = mScatterChart.getXAxis();
// 设置最大最小值
xl.setAxisMinimum(0);
xl.setAxisMaximum(ConstantUtils.maxValueX);
// 是否画线
// xl.setDrawGridLines(false);
// y轴
YAxis yl = mScatterChart.getAxisLeft();
yl.setAxisMinimum(0);
yl.setAxisMaximum(ConstantUtils.maxValueY);
// 设置右侧轴不可用
mScatterChart.getAxisRight().setEnabled(false);
setData(0, 0);
}
private Entry tempPoint = new Entry(0, 0);
private void setData(float x, float y) {
ArrayList<Entry> originals = new ArrayList<>();
ArrayList<Entry> targets = new ArrayList<>();
originals.add(tempPoint);
tempPoint = new Entry(x, y);
targets.add(tempPoint);
//创建一个数据集,并给它一个类型
ScatterDataSet set = new ScatterDataSet(originals, "坐标点");
// 设置形状
set.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
//设置颜色
set.setColor(ColorTemplate.COLORFUL_COLORS[1]);
ScatterDataSet set2 = new ScatterDataSet(targets, "目标点");
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set2.setColor(ColorTemplate.COLORFUL_COLORS[3]);
ArrayList<IScatterDataSet> dataSets = new ArrayList<IScatterDataSet>();
dataSets.add(set);
dataSets.add(set2);
//创建一个数据集的数据对象
ScatterData data = new ScatterData(dataSets);
mScatterChart.setData(data);
mScatterChart.invalidate();
}
一些其他的设置
// 动画 还有其他效果可自行查看
mScatterChart.animateX(3000);
mScatterChart.animateY(3000);
mScatterChart.animateXY(3000, 3000);
// 保存到相册
mScatterChart.saveToGallery("title");
// 保存到指定路径
mScatterChart.saveToPath("title",path)