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(七)-饼状图可以再简单一点 接上文,今天实现的是用的很多的,作用在统计上的饼状图,我们看下今天的效果 这个效果,我们实现,和之前一样的套路,我先来说下 ...
随机推荐
- 如何用.reg文件操作注册表
Windows Registry Editor Version 5.00 ;删除值 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpi ...
- Python 学习开篇
前言 最近看到一张图,有点意思: 蓝色是(成长+付出),红色是回报.有多久可以达到这个红心,要看我们自已的努力,付出了多少专注与汗水.我想说的是,水平轴并不是时间,如果不能坚持努力,可能永远都到不了那 ...
- Now trying to drop the old temporary tablespace, the session hangs.
1.描述 问题描述:删除临时表空间时,会话Hangs挂起 SQL> drop tablespace TEMP_B including contents and datafiles; 2.故障诊断 ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
- Virtual Box下虚拟机复制后ip地址重复
通过桥接模式上网的虚拟机在复制之后,出现三台机器的ip地址都是一样的,还都可以上网, 主要是因为在复制的时候,把网卡信息啥的都一起复制了, 为了设置为不同的ip,需要修改复制后的机器的mac地址. 首 ...
- [USACO08JAN]haybale猜测Haybale Guessing
题目描述 The cows, who always have an inferiority complex about their intelligence, have a new guessing ...
- 空间漫游(SAC大佬的测试)
题目描述由于球哥和巨佬嘉诚交了很多保护费,我们有钱进行一次 d 维空间漫游.d 维空间中有 d 个正交坐标轴,可以用这些坐标轴来描述你在空间中的位置和移动的方向.例如,d = 1 时,空间是一个数轴, ...
- [ZJOI2016]小星星
题目描述 小Y是一个心灵手巧的女孩子,她喜欢手工制作一些小饰品.她有n颗小星星,用m条彩色的细线串了起来,每条细线连着两颗小星星. 有一天她发现,她的饰品被破坏了,很多细线都被拆掉了.这个饰品只剩下了 ...
- 51 nod 1495 中国好区间
1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是& ...
- 2393Cirno的完美算数教室 容斥
2393: Cirno的完美算数教室 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 652 Solved: 389[Submit][Status][ ...