Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图


接上篇,今天要说的,和上篇的类似,只是方向是有相反的两面,我们先看下效果

实际上这样就导致了我们的代码是比较类似的,先来看下我们的基本实现

一.基本实现

布局还是那个布局,只不过是横向的了

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

而初始化这些也都是大同小异

      //正负堆叠条形图
        mHorizontalBarChart = (HorizontalBarChart) findViewById(R.id.mHorizontalBarChart);

        mHorizontalBarChart.setOnChartValueSelectedListener(this);
        mHorizontalBarChart.setDrawGridBackground(false);
        mHorizontalBarChart.getDescription().setEnabled(false);

        // 扩展现在只能分别在x轴和y轴
        mHorizontalBarChart.setPinchZoom(false);

        mHorizontalBarChart.setDrawBarShadow(false);
        mHorizontalBarChart.setDrawValueAboveBar(true);
        mHorizontalBarChart.setHighlightFullBarEnabled(false);

        mHorizontalBarChart.getAxisLeft().setEnabled(false);
        mHorizontalBarChart.getAxisRight().setAxisMaximum(25f);
        mHorizontalBarChart.getAxisRight().setAxisMinimum(-25f);
        mHorizontalBarChart.getAxisRight().setDrawGridLines(false);
        mHorizontalBarChart.getAxisRight().setDrawZeroLine(true);
        mHorizontalBarChart.getAxisRight().setLabelCount(7, false);
        mHorizontalBarChart.getAxisRight().setValueFormatter(new CustomFormatter());
        mHorizontalBarChart.getAxisRight().setTextSize(9f);

        XAxis xAxis = mHorizontalBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setTextSize(9f);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(110f);
        xAxis.setCenterAxisLabels(true);
        xAxis.setLabelCount(12);
        xAxis.setGranularity(10f);
        //日期格式化
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            private DecimalFormat format = new DecimalFormat("###");

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return format.format(value) + "-" + format.format(value + 10);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });

        Legend l = mHorizontalBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setFormToTextSpace(4f);
        l.setXEntrySpace(6f);

        // 重要:当使用负值在堆叠酒吧,总是确保-值数组中的第一个
        ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
        yValues.add(new BarEntry(5, new float[]{-10, 10}));
        yValues.add(new BarEntry(15, new float[]{-12, 13}));
        yValues.add(new BarEntry(25, new float[]{-15, 15}));
        yValues.add(new BarEntry(35, new float[]{-17, 17}));
        yValues.add(new BarEntry(45, new float[]{-19, 20}));
        yValues.add(new BarEntry(55, new float[]{-19, 19}));
        yValues.add(new BarEntry(65, new float[]{-16, 16}));
        yValues.add(new BarEntry(75, new float[]{-13, 14}));
        yValues.add(new BarEntry(85, new float[]{-10, 11}));
        yValues.add(new BarEntry(95, new float[]{-5, 6}));
        yValues.add(new BarEntry(105, new float[]{-1, 2}));

        BarDataSet set = new BarDataSet(yValues, "全国人口普查");
        set.setValueFormatter(new CustomFormatter());
        set.setValueTextSize(7f);
        set.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set.setColors(new int[]{Color.rgb(99, 67, 72), Color.rgb(14, 181, 136)});
        set.setStackLabels(new String[]{"男人", "女人"});

        String[] xLabels = new String[]{"0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+"};

        BarData data = new BarData(set);
        data.setBarWidth(8.5f);
        mHorizontalBarChart.setData(data);
        mHorizontalBarChart.invalidate();

这里我并没有写setData的方法,直接就模拟了一些数据,可以看出,这个图标是异常的简单,当我们运行之后就可以知道,这个实现的效果和上图的效果是一致的

二.显示顶点值

三.x轴动画

四.y轴动画

五.xy轴动画

六.显示边框

现在我把源码贴上

activity_statkedbar_negative.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.HorizontalBarChart
        android:id="@+id/mHorizontalBarChart"
        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="高亮显示"/>

        <Button
            android:id="@+id/btn_show_border"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示边框"/>

    </LinearLayout>

</LinearLayout>

实际上比较类似,可以看下代码

StackedBarActivityNegative


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

    private HorizontalBarChart mHorizontalBarChart;

    //显示顶点值
    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;
    //显示边框
    private Button btn_show_border;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statkedbar_negative);

        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);
        btn_show_border = (Button) findViewById(R.id.btn_show_border);
        btn_show_border.setOnClickListener(this);

        //正负堆叠条形图
        mHorizontalBarChart = (HorizontalBarChart) findViewById(R.id.mHorizontalBarChart);

        mHorizontalBarChart.setOnChartValueSelectedListener(this);
        mHorizontalBarChart.setDrawGridBackground(false);
        mHorizontalBarChart.getDescription().setEnabled(false);

        // 扩展现在只能分别在x轴和y轴
        mHorizontalBarChart.setPinchZoom(false);

        mHorizontalBarChart.setDrawBarShadow(false);
        mHorizontalBarChart.setDrawValueAboveBar(true);
        mHorizontalBarChart.setHighlightFullBarEnabled(false);

        mHorizontalBarChart.getAxisLeft().setEnabled(false);
        mHorizontalBarChart.getAxisRight().setAxisMaximum(25f);
        mHorizontalBarChart.getAxisRight().setAxisMinimum(-25f);
        mHorizontalBarChart.getAxisRight().setDrawGridLines(false);
        mHorizontalBarChart.getAxisRight().setDrawZeroLine(true);
        mHorizontalBarChart.getAxisRight().setLabelCount(7, false);
        mHorizontalBarChart.getAxisRight().setValueFormatter(new CustomFormatter());
        mHorizontalBarChart.getAxisRight().setTextSize(9f);

        XAxis xAxis = mHorizontalBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(false);
        xAxis.setTextSize(9f);
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(110f);
        xAxis.setCenterAxisLabels(true);
        xAxis.setLabelCount(12);
        xAxis.setGranularity(10f);
        //日期格式化
        xAxis.setValueFormatter(new IAxisValueFormatter() {

            private DecimalFormat format = new DecimalFormat("###");

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return format.format(value) + "-" + format.format(value + 10);
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });

        Legend l = mHorizontalBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setFormToTextSpace(4f);
        l.setXEntrySpace(6f);

        // 重要:当使用负值在堆叠酒吧,总是确保-值数组中的第一个
        ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
        yValues.add(new BarEntry(5, new float[]{-10, 10}));
        yValues.add(new BarEntry(15, new float[]{-12, 13}));
        yValues.add(new BarEntry(25, new float[]{-15, 15}));
        yValues.add(new BarEntry(35, new float[]{-17, 17}));
        yValues.add(new BarEntry(45, new float[]{-19, 20}));
        yValues.add(new BarEntry(55, new float[]{-19, 19}));
        yValues.add(new BarEntry(65, new float[]{-16, 16}));
        yValues.add(new BarEntry(75, new float[]{-13, 14}));
        yValues.add(new BarEntry(85, new float[]{-10, 11}));
        yValues.add(new BarEntry(95, new float[]{-5, 6}));
        yValues.add(new BarEntry(105, new float[]{-1, 2}));

        BarDataSet set = new BarDataSet(yValues, "全国人口普查");
        set.setValueFormatter(new CustomFormatter());
        set.setValueTextSize(7f);
        set.setAxisDependency(YAxis.AxisDependency.RIGHT);
        set.setColors(new int[]{Color.rgb(99, 67, 72), Color.rgb(14, 181, 136)});
        set.setStackLabels(new String[]{"男人", "女人"});

        String[] xLabels = new String[]{"0-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "100+"};

        BarData data = new BarData(set);
        data.setBarWidth(8.5f);
        mHorizontalBarChart.setData(data);
        mHorizontalBarChart.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 : mHorizontalBarChart.getData().getDataSets())
                    set.setDrawValues(!set.isDrawValuesEnabled());

                mHorizontalBarChart.invalidate();
                break;
            //x轴动画
            case R.id.btn_anim_x:
                mHorizontalBarChart.animateX(3000);
                break;
            //y轴动画
            case R.id.btn_anim_y:
                mHorizontalBarChart.animateY(3000);
                break;
            //xy轴动画
            case R.id.btn_anim_xy:
                mHorizontalBarChart.animateXY(3000, 3000);
                break;
            //保存到sd卡
            case R.id.btn_save_pic:
                if (mHorizontalBarChart.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:
                mHorizontalBarChart.setAutoScaleMinMaxEnabled(!mHorizontalBarChart.isAutoScaleMinMaxEnabled());
                mHorizontalBarChart.notifyDataSetChanged();
                break;
            //高亮显示
            case R.id.btn_actionToggleHighlight:
                if (mHorizontalBarChart.getData() != null) {
                    mHorizontalBarChart.getData().setHighlightEnabled(
                            !mHorizontalBarChart.getData().isHighlightEnabled());
                    mHorizontalBarChart.invalidate();
                }
                break;
            //显示边框
            case R.id.btn_show_border:
                for (IBarDataSet set : mHorizontalBarChart.getData().getDataSets())
                    ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
                mHorizontalBarChart.invalidate();
                break;
        }
    }
}

好了,这个图表就是这么简单,到这里就结束了

有兴趣的加群:555974449

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

Android图表库MPAndroidChart(十二)——来点不一样的,正负堆叠条形图的更多相关文章

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

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

  2. Android图表库MPAndroidChart(十四)——在ListView种使用相同的图表

    Android图表库MPAndroidChart(十四)--在ListView种使用相同的图表 各位好久不见,最近挺忙的,所有博客更新的比较少,这里今天说个比较简单的图表,那就是在ListView中使 ...

  3. Android图表库MPAndroidChart(二)——线形图的方方面面,看完你会回来感谢我的

    Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MP ...

  4. Android图表库MPAndroidChart(三)——双重轴线形图的实现,这次就so easy了

    Android图表库MPAndroidChart(三)--双重轴线形图的实现,这次就so easy了 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库 ...

  5. Android图表库MPAndroidChart(十三)——简约的底部柱状图

    Android图表库MPAndroidChart(十三)--简约的底部柱状图 我们继续上一讲,今天还是说下柱状图,这个图的话应该是用的比较多的,所有拿出来溜溜,先看下效果 我们还是来看下基本实现 一. ...

  6. Android图表库MPAndroidChart(十一)——多层级的堆叠条形图

    Android图表库MPAndroidChart(十一)--多层级的堆叠条形图 事实上这个也是条形图的一种扩展,我们看下效果就知道了 是吧,他一般满足的需求就是同类数据比较了,不过目前我还真没看过哪个 ...

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

    Android图表库MPAndroidChart(九)--神神秘秘的散点图 今天所的散点图可能用的人不多,但是也算是图表界的一股清流,我们来看下实际的效果 添加的数据有点少,但是足以表示散点图了,我们 ...

  8. Android图表库MPAndroidChart(八)——饼状图的扩展:折线饼状图

    Android图表库MPAndroidChart(八)--饼状图的扩展:折线饼状图 我们接着上文,饼状图的扩展,增加折现的说明,来看下我们要实现的效果 因为之前对MPAndroidChart的熟悉,所 ...

  9. Android图表库MPAndroidChart(七)—饼状图可以再简单一点

    Android图表库MPAndroidChart(七)-饼状图可以再简单一点 接上文,今天实现的是用的很多的,作用在统计上的饼状图,我们看下今天的效果 这个效果,我们实现,和之前一样的套路,我先来说下 ...

随机推荐

  1. js变量的生命周期

    1.在JavaScript中,对于for循环中定义的i变量,其生命周期在循环结束后仍然是有效的. for (var i=0; i < 10; i++){ doSomething(i); } al ...

  2. java面试2(java技术栈和Hollis面试内容分享)

    1.什么是java虚拟机? java虚拟机(JVM)是一个可执行java字节码的虚拟机进程,java源文件被编译成能被java虚拟机可执行的字节码文件. 2.什么是平台无关性,java是如何做到平台无 ...

  3. jenkins构建个人github上的项目

    最近刚进一家新公司,公司采用的是自动化集成测试工具jenkins进行,构建,部署项目 因为以前,没接触过这类工具,所以打算在自己本机安装一个jenkins进行学习 具体安装步骤,很简单,不做讲解 1. ...

  4. Javasript设计模式之链式调用

    写过jquery的可能都知道,jquery里面可以很方便的使用以下代码: // 不使用链式调用 const element = $(ele); element.addClass('red'); ele ...

  5. [HNOI2008]越狱

    题目描述 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 输入输出格式 输入 ...

  6. ●BZOJ 2154 Crash的数字表格

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2154 题解: 莫比乌斯反演. 题意还是很清楚的,就不赘述了. 显然有 $ANS=\sum_{ ...

  7. hdu 5646DZY Loves Partition(构造)

    DZY Loves Partition  Accepts: 154  Submissions: 843  Time Limit: 4000/2000 MS (Java/Others)  Memory ...

  8. Java多线程基础总结

    一.线程和进程关系 二.创建方式1.继承Thread类,重写run方法2.实现Runable接口,重写run方法3.使用匿名内部类 三.API接口start()currentThread() 获取当前 ...

  9. C++函数式编程实现牛顿法

    In numerical analysis, Newton's method (also known as the Newton–Raphson method), named after Isaac ...

  10. C语言第二次作业-----顺序结构

    一:改错题 (1)输出指定信息: 将给定源代码输入编译器: 执行编译命令,发现编译器报错,错误信息如下: 经检查,发现源程序将"stdio.h"误拼为"stido.h&q ...