当前位置:   article > 正文

MPAndroidChart 之 CombinedChart组合图简单使用

combinedchart

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.rlzz.mpandroidchart.Test2Activity">
    <com.github.mikephil.charting.charts.CombinedChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="500dp"/>
</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Activity代码

private CombinedChart combinedChart;
    protected String[] suppliers = new String[]{
            "鸿兴", "利科", "大阪", "沃佳尔", "大唐", "聚科", "其他", "联博", "聚源升", "尹信达"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        combinedChart = (CombinedChart) findViewById(chart);
        combinedChart.setDrawBorders(true); // 显示边界
        combinedChart.getDescription().setEnabled(false);  // 不显示备注信息
        combinedChart.setPinchZoom(true); // 比例缩放
        combinedChart.animateY(1500);

        XAxis xAxis = combinedChart.getXAxis();
        xAxis.setDrawGridLines(false);
        /*解决左右两端柱形图只显示一半的情况 只有使用CombinedChart时会出现,如果单独使用BarChart不会有这个问题*/
        xAxis.setAxisMinimum(-0.5f);
        xAxis.setAxisMaximum(suppliers.length - 0.5f);

        xAxis.setLabelCount(suppliers.length); // 设置X轴标签数量
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴标签位置,BOTTOM在底部显示,TOP在顶部显示
        xAxis.setValueFormatter(new IAxisValueFormatter() { // 转换要显示的标签文本,value值默认是int从0开始
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return suppliers[(int) value];
            }
        });


        YAxis axisLeft = combinedChart.getAxisLeft(); // 获取左边Y轴操作类
        axisLeft.setAxisMinimum(0); // 设置最小值
        axisLeft.setGranularity(10); // 设置Label间隔
        axisLeft.setLabelCount(10);// 设置标签数量
        axisLeft.setValueFormatter(new IAxisValueFormatter() { // 在左边Y轴标签文本后加上%号
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return value + "%";
            }
        });

        YAxis axisRight = combinedChart.getAxisRight(); // 获取右边Y轴操作类
        axisRight.setDrawGridLines(false); // 不绘制背景线,上面左边Y轴并没有设置此属性,因为不设置默认是显示的
        axisRight.setGranularity(10); // 设置Label间隔
        axisRight.setAxisMinimum(0); // 设置最小值
        axisRight.setLabelCount(20); // 设置标签个数
        axisRight.setValueFormatter(new IAxisValueFormatter() { // 在右边Y轴标签文本后加上%号
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return value + "%";
            }
        });

        /**
         * 初始化柱形图的数据
         * 此处用suppliers的数量做循环,因为总共所需要的数据源数量应该和标签个数一致
         * 其中BarEntry是柱形图数据源的实体类,包装xy坐标数据
         */
        /******************BarData start********************/
        List<BarEntry> barEntries = new ArrayList<>();
        for (int i = 0; i < suppliers.length; i++) {
            barEntries.add(new BarEntry(i, getRandom(100, 50)));
        }
        BarDataSet barDataSet = new BarDataSet(barEntries, "LAR");  // 新建一组柱形图,"LAR"为本组柱形图的Label
        barDataSet.setColor(Color.parseColor("#0288d1")); // 设置柱形图颜色
        barDataSet.setValueTextColor(Color.parseColor("#0288d1")); //  设置柱形图顶部文本颜色
        BarData barData = new BarData();
        barData.addDataSet(barDataSet);// 添加一组柱形图,如果有多组柱形图数据,则可以多次addDataSet来设置
        /******************BarData end********************/

        /**
         * 初始化折线图数据
         * 说明同上
         */
        /******************LineData start********************/
        List<Entry> lineEntries = new ArrayList<>();
        for (int i = 0; i < suppliers.length; i++) {
            lineEntries.add(new Entry(i, getRandom(100, 0)));
        }
        LineDataSet lineDataSet = new LineDataSet(lineEntries, "不良率");
        lineDataSet.setColor(Color.parseColor("#b71c1c"));
        lineDataSet.setCircleColor(Color.parseColor("#b71c1c"));
        lineDataSet.setValueTextColor(Color.parseColor("#f44336"));
        lineDataSet.setLineWidth(3f);
        lineDataSet.setHighlightEnabled(false);
        LineData lineData = new LineData();
        lineData.addDataSet(lineDataSet);
        /******************LineData end********************/

        CombinedData combinedData = new CombinedData(); // 创建组合图的数据源
        combinedData.setData(barData);  // 添加柱形图数据源
        combinedData.setData(lineData); // 添加折线图数据源
        combinedChart.setData(combinedData); // 为组合图设置数据源
    }

    protected float getRandom(float range, float startsfrom) {
        return (float) (Math.random() * range) + startsfrom;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号