Android图表库MPAndroidChart(九)——神神秘秘的散点图

简介: 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开发
Android统计图表MPAndroidChart:动态添加数据更新【6】
 Android统计图表MPAndroidChart:动态添加数据更新【6】 Android MPAndroidChart的LineDataSet代表一条统计图表中统计折线,一张统计图表可以同时存在若干条统计折线,其在内存中存储的模型类型数组,从0开始下标。
3024 0
|
XML Android开发 数据格式
Android 图表开发开源库MPAndroidChart
Android 图表开发开源库MPAndroidChart
Android 图表开发开源库MPAndroidChart
|
Android开发
Android统计图表MPAndroidChart:为多条统计折线动态更新数据,以高温低温曲线为例【7】
 Android统计图表MPAndroidChart:为多条统计折线动态更新数据,以高温低温曲线为例【7】 本文在附录文章6的基础上,为Android统计图表MPAndroidChart的同一个LineChart中同时增加两条统计折线,动态的为这两条折线同时增加数据并更新结果。
2469 0
Android MPAndroidChart:动态添加统计数据线【8】
Android MPAndroidChart:动态添加统计数据线【8】 本文在附录相关文章6的基础上,动态的依次增加若干条统计折线(相当于批量增加数据点)。
1393 0
|
Android开发
Android MPAndroidChart之PieChart和数据结构以及模型【5】
Android MPAndroidChart之PieChart和数据结构以及模型【5】 以MPAndroidChart的饼状图PieChart为例。
1089 0
|
BI Android开发
基于Android MPAndroidChart实现腾讯QQ群数据统计报表核心功能
 基于Android MPAndroidChart实现腾讯QQ群数据统计报表核心功能  腾讯QQ移动客户端(新版)的QQ群有一项功能比较有趣,是关于QQ群的。
1185 0
|
Java Android开发 数据格式
Android统计图表MPAndroidChart
 Android统计图表MPAndroidChart MPAndroidChart是在Android平台上开源的第三方统计图表库,可以绘制样式复杂、丰富的各种统计图表,如一般常见的折线图、饼状图、柱状图、散点图、金融股票中使用的的“蜡烛”图、“泡泡”统计图、雷达状统计饼状图等等。
1252 0
|
10天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
12天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
3天前
|
Java Android开发
Android开发--Intent-filter属性详解
Android开发--Intent-filter属性详解