Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图

简介: Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图 起泡图和散点图如出一辙,但是个人认为要比散点图好看一点,我们来看下实际的演示效果 这个和散点图的实现很相似,我们一起来看下一.

Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图


起泡图和散点图如出一辙,但是个人认为要比散点图好看一点,我们来看下实际的演示效果

这里写图片描述

这个和散点图的实现很相似,我们一起来看下

一.基本实现

先看下我的xml

<com.github.mikephil.charting.charts.BubbleChart
        android:id="@+id/mBubbleChart"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

用的是BubbleChart这个View,再来初始化下

        //起泡图
        mBubbleChart = (BubbleChart) findViewById(R.id.mBubbleChart);

        mBubbleChart.getDescription().setEnabled(false);
        mBubbleChart.setOnChartValueSelectedListener(this);
        mBubbleChart.setDrawGridBackground(false);
        mBubbleChart.setTouchEnabled(true);
        mBubbleChart.setDragEnabled(true);
        mBubbleChart.setScaleEnabled(true);
        mBubbleChart.setMaxVisibleValueCount(200);
        mBubbleChart.setPinchZoom(true);

        Legend l = mBubbleChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);

        YAxis yl = mBubbleChart.getAxisLeft();
        yl.setSpaceTop(30f);
        yl.setSpaceBottom(30f);
        yl.setDrawZeroLine(false);

        mBubbleChart.getAxisRight().setEnabled(false);

        XAxis xl = mBubbleChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);

        setData();

很简单,设置一些假数据

    //设置数据
    private void setData() {

        ArrayList<BubbleEntry> yVals1 = new ArrayList<BubbleEntry>();
        ArrayList<BubbleEntry> yVals2 = new ArrayList<BubbleEntry>();
        ArrayList<BubbleEntry> yVals3 = new ArrayList<BubbleEntry>();

        for (int i = 0; i < 10; i++) {
            float val = (float) (Math.random() * 30);
            float size = (float) (Math.random() * 40);

            yVals1.add(new BubbleEntry(i, val, size));
        }

        for (int i = 0; i < 20; i++) {
            float val = (float) (Math.random() * 40);
            float size = (float) (Math.random() * 50);

            yVals2.add(new BubbleEntry(i, val, size));
        }

        for (int i = 0; i < 30; i++) {
            float val = (float) (Math.random() * 50);
            float size = (float) (Math.random() * 60);

            yVals3.add(new BubbleEntry(i, val, size));
        }

        BubbleDataSet set1 = new BubbleDataSet(yVals1, "优秀");
        //可以谁知alpha
        set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
        set1.setDrawValues(true);
        BubbleDataSet set2 = new BubbleDataSet(yVals2, "及格");
        set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
        set2.setDrawValues(true);
        BubbleDataSet set3 = new BubbleDataSet(yVals3, "不及格");
        set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
        set3.setDrawValues(true);

        ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
        dataSets.add(set1);
        dataSets.add(set2);
        dataSets.add(set3);

        BubbleData data = new BubbleData(dataSets);
        data.setDrawValues(false);
        data.setValueTextSize(8f);
        data.setValueTextColor(Color.WHITE);
        data.setHighlightCircleWidth(1.5f);

        mBubbleChart.setData(data);
        mBubbleChart.invalidate();

        //默认动画
        mBubbleChart.animateXY(3000, 3000);
    }

这样就大功告成了,和之前的散点图一样了

二.显示顶点

这里写图片描述

三.X轴动画

这里写图片描述

四.Y轴动画

这里写图片描述

五.XY轴动画

这里写图片描述

嘻嘻,很简单对不对,那我们现在来看下全部的代码

activity_bubble.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.BubbleChart
        android:id="@+id/mBubbleChart"
        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>

BubbleChartActivity

public class BubbleChartActivity extends BaseActivity implements OnChartValueSelectedListener, View.OnClickListener {

    private BubbleChart mBubbleChart;

    //显示顶点值
    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_bubble);

        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);


        //起泡图
        mBubbleChart = (BubbleChart) findViewById(R.id.mBubbleChart);

        mBubbleChart.getDescription().setEnabled(false);
        mBubbleChart.setOnChartValueSelectedListener(this);
        mBubbleChart.setDrawGridBackground(false);
        mBubbleChart.setTouchEnabled(true);
        mBubbleChart.setDragEnabled(true);
        mBubbleChart.setScaleEnabled(true);
        mBubbleChart.setMaxVisibleValueCount(200);
        mBubbleChart.setPinchZoom(true);

        Legend l = mBubbleChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);

        YAxis yl = mBubbleChart.getAxisLeft();
        yl.setSpaceTop(30f);
        yl.setSpaceBottom(30f);
        yl.setDrawZeroLine(false);

        mBubbleChart.getAxisRight().setEnabled(false);

        XAxis xl = mBubbleChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);

        setData();
    }

    //设置数据
    private void setData() {

        ArrayList<BubbleEntry> yVals1 = new ArrayList<BubbleEntry>();
        ArrayList<BubbleEntry> yVals2 = new ArrayList<BubbleEntry>();
        ArrayList<BubbleEntry> yVals3 = new ArrayList<BubbleEntry>();

        for (int i = 0; i < 10; i++) {
            float val = (float) (Math.random() * 30);
            float size = (float) (Math.random() * 40);

            yVals1.add(new BubbleEntry(i, val, size));
        }

        for (int i = 0; i < 20; i++) {
            float val = (float) (Math.random() * 40);
            float size = (float) (Math.random() * 50);

            yVals2.add(new BubbleEntry(i, val, size));
        }

        for (int i = 0; i < 30; i++) {
            float val = (float) (Math.random() * 50);
            float size = (float) (Math.random() * 60);

            yVals3.add(new BubbleEntry(i, val, size));
        }

        BubbleDataSet set1 = new BubbleDataSet(yVals1, "优秀");
        //可以谁知alpha
        set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
        set1.setDrawValues(true);
        BubbleDataSet set2 = new BubbleDataSet(yVals2, "及格");
        set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
        set2.setDrawValues(true);
        BubbleDataSet set3 = new BubbleDataSet(yVals3, "不及格");
        set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);
        set3.setDrawValues(true);

        ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
        dataSets.add(set1);
        dataSets.add(set2);
        dataSets.add(set3);

        BubbleData data = new BubbleData(dataSets);
        data.setDrawValues(false);
        data.setValueTextSize(8f);
        data.setValueTextColor(Color.WHITE);
        data.setHighlightCircleWidth(1.5f);

        mBubbleChart.setData(data);
        mBubbleChart.invalidate();

        //默认动画
        mBubbleChart.animateXY(3000, 3000);
    }

    @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 : mBubbleChart.getData().getDataSets())
                    set.setDrawValues(!set.isDrawValuesEnabled());

                mBubbleChart.invalidate();
                break;
            //x轴动画
            case R.id.btn_anim_x:
                mBubbleChart.animateX(3000);
                break;
            //y轴动画
            case R.id.btn_anim_y:
                mBubbleChart.animateY(3000);
                break;
            //xy轴动画
            case R.id.btn_anim_xy:
                mBubbleChart.animateXY(3000, 3000);
                break;
            //保存到sd卡
            case R.id.btn_save_pic:
                if (mBubbleChart.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:
                mBubbleChart.setAutoScaleMinMaxEnabled(!mBubbleChart.isAutoScaleMinMaxEnabled());
                mBubbleChart.notifyDataSetChanged();
                break;
            //高亮显示
            case R.id.btn_actionToggleHighlight:
                if (mBubbleChart.getData() != null) {
                    mBubbleChart.getData().setHighlightEnabled(
                            !mBubbleChart.getData().isHighlightEnabled());
                    mBubbleChart.invalidate();
                }
                break;
        }
    }
}

就是这么简单

有兴趣的加群:555974449

Sample:http://download.csdn.net/detail/qq_26787115/9689868

目录
相关文章
|
8月前
|
Android开发
Android自定义View 水波气泡2
Android自定义View 水波气泡
|
8月前
|
Android开发
Android自定义View 水波气泡1
Android自定义View 水波气泡
Android自定义View 水波气泡1
|
Android开发
android Jetpack Navigation组件—— 之嵌套导航图
android Jetpack Navigation组件—— 之嵌套导航图
693 0
android Jetpack Navigation组件—— 之嵌套导航图
|
XML 算法 安全
❤️Android Apk 的打包过程 ❤️ 只需两幅图
官方介绍 在分析安装过程之前,需要先了解一下 Android 项目是如何经过编译->打包生成最终的 .apk 格式的安装包。谷歌有一张官方图片来描述 apk 的打包流程,如下图所示。
335 0
❤️Android Apk 的打包过程 ❤️ 只需两幅图
|
移动开发 测试技术 Android开发
一分钟教你Android、iOS如何实现自动化截长图功能,超实用!
在移动端自动化测试过程中经常会遇到需要截长图的场景,比如大促活动的H5页面、动态信息流页面等,但是目前在网上检索只能搜到关于截长图的软件推荐,没有讲关于如何通过自动化脚本的方式实现的文章,今天就来给大家分享一个简单的实现方案。
615 0
一分钟教你Android、iOS如何实现自动化截长图功能,超实用!
|
前端开发 Android开发
Android实现无序树形结构图,类似思维导图和级联分层图(无序,随机位置)
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣!!!
1149 0
|
算法 Android开发 数据安全/隐私保护
【Android 逆向】IDA 工具使用 ( 函数窗口 Function window | 创建引用图 Xrefs graph to | 创建调用图 Xrefs graph from )
【Android 逆向】IDA 工具使用 ( 函数窗口 Function window | 创建引用图 Xrefs graph to | 创建调用图 Xrefs graph from )
434 0
【Android 逆向】IDA 工具使用 ( 函数窗口 Function window | 创建引用图 Xrefs graph to | 创建调用图 Xrefs graph from )
|
XML Android开发 数据格式
Android 图表开发开源库MPAndroidChart
Android 图表开发开源库MPAndroidChart
Android 图表开发开源库MPAndroidChart
|
Android开发 编解码 开发工具
Android 录制gif图
录制gif图思路: 把App操作过程录制成视频 根据视频转换成Gif 方法一:使用adb命令 在Android sdk下面有一些很有用的工具,adb位于platform-tools文件夹,开发者用它在设备上安装启动应用。
982 0
|
前端开发 Android开发
Android 自定义漏斗图FunnelView(二)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
1152 0