Android图表库MPAndroidChart(九)——神神秘秘的散点图
Android图表库MPAndroidChart(九)——神神秘秘的散点图
今天所的散点图可能用的人不多,但是也算是图表界的一股清流,我们来看下实际的效果
添加的数据有点少,但是足以表示散点图了,我们先实现它
一.基本实现
实现还是老套路,看下布局
<com.github.mikephil.charting.charts.ScatterChart
android:id="@+id/mScatterChart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
散点图的View是ScatterChart,我们看下初始化
//散点图
mScatterChart = (ScatterChart) findViewById(R.id.mScatterChart);
mScatterChart.getDescription().setEnabled(false);
mScatterChart.setOnChartValueSelectedListener(this);
mScatterChart.setDrawGridBackground(false);
mScatterChart.setTouchEnabled(true);
mScatterChart.setMaxHighlightDistance(10f);
// 支持缩放和拖动
mScatterChart.setDragEnabled(true);
mScatterChart.setScaleEnabled(true);
mScatterChart.setMaxVisibleValueCount(10);
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);
YAxis yl = mScatterChart.getAxisLeft();
yl.setAxisMinimum(0f);
mScatterChart.getAxisRight().setEnabled(false);
XAxis xl = mScatterChart.getXAxis();
xl.setDrawGridLines(false);
setData();
可以明确一点的是,他的代码量是比较少的,说明简单啊,我们去理解就不是这么难了,我们模拟一些数据
//设置数据
private void setData() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
for (int i = 0; i < 10; i++) {
float val = (float) (Math.random() * 10 + 3);
yVals1.add(new Entry(i, val));
}
for (int i = 0; i < 10; i++) {
float val = (float) (Math.random() * 10 + 3);
yVals2.add(new Entry(i + 0.33f, val));
}
for (int i = 0; i < 10; i++) {
float val = (float) (Math.random() * 10 + 3);
yVals3.add(new Entry(i + 0.66f, val));
}
//创建一个数据集,并给它一个类型
ScatterDataSet set1 = new ScatterDataSet(yVals1, "优秀");
set1.setScatterShape(ScatterChart.ScatterShape.SQUARE);
//设置颜色
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
ScatterDataSet set2 = new ScatterDataSet(yVals2, "及格");
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set2.setScatterShapeHoleColor(ColorTemplate.COLORFUL_COLORS[3]);
set2.setScatterShapeHoleRadius(3f);
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
ScatterDataSet set3 = new ScatterDataSet(yVals3, "不及格");
set3.setShapeRenderer(new CustomScatterShapeRenderer());
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
set1.setScatterShapeSize(8f);
set2.setScatterShapeSize(8f);
set3.setScatterShapeSize(8f);
ArrayList<IScatterDataSet> dataSets = new ArrayList<IScatterDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
//创建一个数据集的数据对象
ScatterData data = new ScatterData(dataSets);
mScatterChart.setData(data);
mScatterChart.invalidate();
}
这样就实现完成了,也就是上面的那幅图片的效果了
二.x轴动画
三.y轴动画
四.xy轴动画
可以看到,他的扩展也是比较少的,我们看一下完整的代码
activity_scatter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.github.mikephil.charting.charts.ScatterChart
android:id="@+id/mScatterChart"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_show_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顶点显示值"/>
<Button
android:id="@+id/btn_anim_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X轴动画"/>
<Button
android:id="@+id/btn_anim_y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y轴动画"/>
<Button
android:id="@+id/btn_anim_xy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XY轴动画"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_save_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到相册"/>
<Button
android:id="@+id/btn_auto_mix_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动最大最小值"/>
<Button
android:id="@+id/btn_actionToggleHighlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="高亮显示"/>
</LinearLayout>
</LinearLayout>
ScatterChartActivity
public class ScatterChartActivity extends BaseActivity implements OnChartValueSelectedListener, View.OnClickListener {
private ScatterChart mScatterChart;
//显示顶点值
private Button btn_show_values;
//x轴动画
private Button btn_anim_x;
//y轴动画
private Button btn_anim_y;
//xy轴动画
private Button btn_anim_xy;
//保存到sd卡
private Button btn_save_pic;
//切换自动最大最小值
private Button btn_auto_mix_max;
//高亮显示
private Button btn_actionToggleHighlight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scatter);
initView();
}
//初始化View
private void initView() {
//基本控件
btn_show_values = (Button) findViewById(R.id.btn_show_values);
btn_show_values.setOnClickListener(this);
btn_anim_x = (Button) findViewById(R.id.btn_anim_x);
btn_anim_x.setOnClickListener(this);
btn_anim_y = (Button) findViewById(R.id.btn_anim_y);
btn_anim_y.setOnClickListener(this);
btn_anim_xy = (Button) findViewById(R.id.btn_anim_xy);
btn_anim_xy.setOnClickListener(this);
btn_save_pic = (Button) findViewById(R.id.btn_save_pic);
btn_save_pic.setOnClickListener(this);
btn_auto_mix_max = (Button) findViewById(R.id.btn_auto_mix_max);
btn_auto_mix_max.setOnClickListener(this);
btn_actionToggleHighlight = (Button) findViewById(R.id.btn_actionToggleHighlight);
btn_actionToggleHighlight.setOnClickListener(this);
//散点图
mScatterChart = (ScatterChart) findViewById(R.id.mScatterChart);
mScatterChart.getDescription().setEnabled(false);
mScatterChart.setOnChartValueSelectedListener(this);
mScatterChart.setDrawGridBackground(false);
mScatterChart.setTouchEnabled(true);
mScatterChart.setMaxHighlightDistance(10f);
// 支持缩放和拖动
mScatterChart.setDragEnabled(true);
mScatterChart.setScaleEnabled(true);
mScatterChart.setMaxVisibleValueCount(10);
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);
YAxis yl = mScatterChart.getAxisLeft();
yl.setAxisMinimum(0f);
mScatterChart.getAxisRight().setEnabled(false);
XAxis xl = mScatterChart.getXAxis();
xl.setDrawGridLines(false);
setData();
}
//设置数据
private void setData() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
ArrayList<Entry> yVals3 = new ArrayList<Entry>();
for (int i = 0; i < 10; i++) {
float val = (float) (Math.random() * 10 + 3);
yVals1.add(new Entry(i, val));
}
for (int i = 0; i < 10; i++) {
float val = (float) (Math.random() * 10 + 3);
yVals2.add(new Entry(i + 0.33f, val));
}
for (int i = 0; i < 10; i++) {
float val = (float) (Math.random() * 10 + 3);
yVals3.add(new Entry(i + 0.66f, val));
}
//创建一个数据集,并给它一个类型
ScatterDataSet set1 = new ScatterDataSet(yVals1, "优秀");
set1.setScatterShape(ScatterChart.ScatterShape.SQUARE);
//设置颜色
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
ScatterDataSet set2 = new ScatterDataSet(yVals2, "及格");
set2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
set2.setScatterShapeHoleColor(ColorTemplate.COLORFUL_COLORS[3]);
set2.setScatterShapeHoleRadius(3f);
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
ScatterDataSet set3 = new ScatterDataSet(yVals3, "不及格");
set3.setShapeRenderer(new CustomScatterShapeRenderer());
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
set1.setScatterShapeSize(8f);
set2.setScatterShapeSize(8f);
set3.setScatterShapeSize(8f);
ArrayList<IScatterDataSet> dataSets = new ArrayList<IScatterDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
//创建一个数据集的数据对象
ScatterData data = new ScatterData(dataSets);
mScatterChart.setData(data);
mScatterChart.invalidate();
}
@Override
public void onValueSelected(Entry e, Highlight h) {
}
@Override
public void onNothingSelected() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//显示顶点值
case R.id.btn_show_values:
for (IDataSet set : mScatterChart.getData().getDataSets())
set.setDrawValues(!set.isDrawValuesEnabled());
mScatterChart.invalidate();
break;
//x轴动画
case R.id.btn_anim_x:
mScatterChart.animateX(3000);
break;
//y轴动画
case R.id.btn_anim_y:
mScatterChart.animateY(3000);
break;
//xy轴动画
case R.id.btn_anim_xy:
mScatterChart.animateXY(3000, 3000);
break;
//保存到sd卡
case R.id.btn_save_pic:
if (mScatterChart.saveToGallery("title" + System.currentTimeMillis(), 50)) {
Toast.makeText(getApplicationContext(), "保存成功",
Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "保存失败",
Toast.LENGTH_SHORT).show();
break;
//切换自动最大最小值
case R.id.btn_auto_mix_max:
mScatterChart.setAutoScaleMinMaxEnabled(!mScatterChart.isAutoScaleMinMaxEnabled());
mScatterChart.notifyDataSetChanged();
break;
//高亮显示
case R.id.btn_actionToggleHighlight:
if (mScatterChart.getData() != null) {
mScatterChart.getData().setHighlightEnabled(
!mScatterChart.getData().isHighlightEnabled());
mScatterChart.invalidate();
}
break;
}
}
}
有兴趣的加群:555974449
Sample:http://download.csdn.net/detail/qq_26787115/9689868
Android图表库MPAndroidChart(九)——神神秘秘的散点图的更多相关文章
- Android图表库MPAndroidChart(三)——双重轴线形图的实现,这次就so easy了
Android图表库MPAndroidChart(三)--双重轴线形图的实现,这次就so easy了 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...
- Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的
Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...
- Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表
Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使 ...
- Android图表库MPAndroidChart(十三)——简约的底部柱状图
Android图表库MPAndroidChart(十三)--简约的底部柱状图 我们继续上一讲,今天还是说下柱状图,这个图的话应该是用的比较多的,所有拿出来溜溜,先看下效果 我们还是来看下基本实现 一. ...
- Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图
Android图表库MPAndroidChart(十二)--来点不一样的,正负堆叠条形图 接上篇,今天要说的,和上篇的类似,只是方向是有相反的两面,我们先看下效果 实际上这样就导致了我们的代码是比较类 ...
- Android图表库MPAndroidChart(十一)——多层级的堆叠条形图
Android图表库MPAndroidChart(十一)--多层级的堆叠条形图 事实上这个也是条形图的一种扩展,我们看下效果就知道了 是吧,他一般满足的需求就是同类数据比较了,不过目前我还真没看过哪个 ...
- Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图
Android图表库MPAndroidChart(十)--散点图的孪生兄弟气泡图 起泡图和散点图如出一辙,但是个人认为要比散点图好看一点,我们来看下实际的演示效果 这个和散点图的实现很相似,我们一起来 ...
- Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图
Android图表库MPAndroidChart(八)--饼状图的扩展:折线饼状图 我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果 因为之前对MPAndroidChart的熟悉,所 ...
- Android图表库MPAndroidChart(七)—饼状图可以再简单一点
Android图表库MPAndroidChart(七)-饼状图可以再简单一点 接上文,今天实现的是用的很多的,作用在统计上的饼状图,我们看下今天的效果 这个效果,我们实现,和之前一样的套路,我先来说下 ...
随机推荐
- VueJs 源码解析 (四) initRender.Js
vueJs 源码解析 (四) initRender.Js 在之前的文章中提到了 vuejs 源码中的 架构部分,以及 谈论到了 vue 源码三要素 vm.compiler.watcher 这三要素,那 ...
- java修改文件内容
文件的读和写,大家都不陌生,但是修改呢?按照普通的读写流去修改的话,只能全部读取出来,在内存中修改好后,全部写进去,这样对于文件内容过多的时,性能很低. 最近在遇到这个问题的时候,发现RandomAc ...
- SAC E#1 - 一道难题 Tree
题目背景 冴月麟和魏潇承是好朋友. 题目描述 冴月麟为了守护幻想乡,而制造了幻想乡的倒影,将真实的幻想乡封印了.任何人都无法进入真实的幻想乡了,但是她给前来救她的魏潇承留了一个线索. 她设置了一棵树( ...
- 洛谷P2319 [HNOI2006]超级英雄
一开始是用二分图匹配(网络流)+二分做的,后来发现直接用匈牙利更简单 #include<cstdio> #include<cstdlib> #include<algori ...
- NOIP2014-11-3模拟赛
字符串 题目描述 现在给一个字符串,你要做的就是当这个字符串中存在两个挨着的字符是相同的时就将这两个字符消除.需要注意的是,当把这两个字符消除后,可能又产生一对新的挨着的字符是相同的.比如,初始的字符 ...
- hdu 5439(找规律)
The sequence is generated by the following scheme. 1. First, write down 1, 2 on a paper. 2. The 2nd ...
- hdu5586 BestCoder Round #64 (div.2)
问题描述 给n个数{A}_{1},{A}_{2}....{A}_{n}A1,A2....An,你可以选择一个区间(也可以不选),区间里每个数x变成f(x),其中f(x)=(1890x ...
- Android Studio创建/打开项目时一直处于Building“project name”Gradle project info的解决办法
重新安装了Android studio 之后, 启动android studio,打开原来的项目,界面一直停留在: 一直停留在此界面的原因是:Android studio 在下载 Gradle ,但是 ...
- Springboot项目maven多模块拆分
多模块拆分的必要性 使用Java技术开发的工程项目,无论是数据处理系统还是Web网站,随着项目的不断发展,需求的不断细化与添加,工程项目中的代码越来越多,包结构也越来越复杂这时候工程的进展就会遇到各种 ...
- 使用redis做缓存
redis常本用来作为缓存服务器.缓存的好处是减少服务器的压力,数据查询速度快.解决数据响应慢的问题. 添加缓存:只用redis的Hash数据类型添加缓存. 例如:需要在查询的业务功能中,添加缓存 1 ...