Android图表库MPAndroidChart(九)——神神秘秘的散点图
Android图表库MPAndroidChart(九)——神神秘秘的散点图
今天所的散点图可能用的人不多,但是也算是图表界的一股清流,我们来看下实际的效果
添加的数据有点少,但是足以表示散点图了,我们先实现它
一.基本实现
实现还是老套路,看下布局
<com.github.mikephil.charting.charts.ScatterChartandroid: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_scatter);initView();}//初始化Viewprivate 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();}@Overridepublic void onValueSelected(Entry e, Highlight h) {}@Overridepublic void onNothingSelected() {}@Overridepublic 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();} elseToast.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(二)——线形图的方方面面,看完你会回来感谢我的
Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...
- Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路...
Android图表库MPAndroidChart(四)--条形图的绘制过程过程,隐隐约约我看到了套路 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...
- Android图表库MPAndroidChart(四)——条形图的绘制过程过程,隐隐约约我看到了套路
Android图表库MPAndroidChart(四)--条形图的绘制过程过程,隐隐约约我看到了套路 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...
- 【Android】开源图表库MPAndroidChart的学习
android开源图表库MPAndroidChart(中文翻译) MPAndroidChart简化版运行效果: 主要的Api方法: setDescription(String desc) : 设置表格 ...
- Android图表库hellocharts详解
感谢大佬:https://www.cnblogs.com/huolongluo/p/5988644.html 因为项目需要搞一个折线图,按照日期显示相应的成绩,所以有了本文. 以前用过一次XCL-c ...
- Android图表库--MPChart(Piechart)
1.添加依赖 在Project即工程下的build.gradle文件里添加 maven { url "https://jitpack.io" } 添加下来是这个样子的: allpr ...
- GitHub 上排名前 100 的 Android 开源库介绍
转自:http://www.codeceo.com/article/github-top-100-android-libs.html 本项目主要对目前 GitHub 上排名前 100 的 Androi ...
- 排名前100的Android开源库
本项目主要对目前GitHub上排名前100的Android开源库进行简单的介绍,至于排名完全是根据GitHub搜索Java语言选择「BestMatch」得到的结果,然后过滤了跟Android不相关的项 ...
- Android 第三方库前100
本文转自:https://github.com/Freelander/Android_Data/blob/master/Android-Librarys-Top-100.md 本项目主要对目前 Git ...
最新文章
- 分词器 keras.preprocessing.text.Tokenizer
- AutoCAD2012打开后一闪的解决方法
- 世界AI大会三马纵论:马云乐观、马斯克悲观,马化腾认为技术孤立主义有大危害...
- Mysql 百万级数据优化资料
- c# as 关键字作用
- session过期设置
- MySQL的一级索引和二级索引介绍,HBase中提到的二级索引【笔记自用】
- SAP Cloud for Customer和Hybris Commerce的session保护机制
- goland环境配置_Goland辅助工具goimports和gomodules
- 教你一招用python发送QQ邮件
- Vimtutor中文版
- 厉害,96秒100亿,阿里双十一到底做了什么杠过亿级流量??
- 30套后台管理界面分享
- 1070: 小汽车的位置 Python
- cola ui ajax,Cola-UI 文档中心
- Lesson 13-14 How often do you exercise?
- 回忆鸭掌门的麻辣兔丁
- mysql触发器实验小结_mysql 触发器小结
- bridge 的运用和数码照片的处理
- 饿了么ui elementui 浏览器日志报错的检查思路
热门文章
- 怎么用dw和wampserver建PHP,wampserver怎么使用
- nginx流媒体服务器(基于CentOS7)实现rtmp直播流,m3u8视频流
- Codeforces Round #782 (Div. 2)-D. Reverse Sort Sum(树状数组)
- 银行固定资产管理的解决方案
- core开发linux桌面应用,【.NET Core 跨平台 GUI 开发】第一篇:编写你的第一个 Gtk# 应用...
- 如何在寒冷的冬季,不摘手套玩手机
- linux中查看进程的启动时间
- buuctf wustctf2020_getshell_2 ret2shellcode
- 17Web服务器端控件
- 什么可以有助睡眠?增强睡眠质量的方法