MPAndroidBarChart柱状图,水平柱状图,圆饼图,看完不会,我跟你姓

BarChart基本的初始化操作,红色的标记为重点
private void initBarCahrt(BarChart barChart){
        // 数据描述
//        Description description = new Description();
//        description.setText("电梯故障统计柱状图");
//        description.setTextSize(10f);
        barChart.setDescription(null);
        // 如果没有数据的时候,会显示这个,类似ListView的EmptyView
        barChart.setNoDataText("没有图表数据,请添加");
        //禁止图表一切交互
        barChart.setTouchEnabled(false);
        // 是否显示表格颜色
        barChart.setDrawGridBackground(false);
        // 设置是否可以触摸
        barChart.setTouchEnabled(true);
        // 是否可以拖拽
        barChart.setDragEnabled(true);
        // 是否可以缩放
        barChart.setScaleEnabled(false);
        // 集双指缩放
        barChart.setPinchZoom(false);
        // 设置背景
        barChart.setBackgroundColor(Color.WHITE);
        // 如果打开,背景矩形将出现在已经画好的绘图区域的后边。
        barChart.setDrawGridBackground(false);
        // 集拉杆阴影
        barChart.setDrawBarShadow(false);
        // 图例
        barChart.getLegend().setEnabled(false);
        barChart.setFitBars(false);
        //设置图表周边间距
        barChart.setMinOffset(2f);
        //设置最小高度从0开始
        barChart.setMinimumHeight(0);
        // 隐藏右边的坐标轴
        barChart.getAxisRight().setEnabled(false);
        // 默认显示左边的左边轴
        barChart.getAxisLeft().setEnabled(true);
        // 设置数据
        barChart.animateX(2000);
        //设置图表周围边距
//        barChart.setMinOffset(20f);
        barChart.setExtraBottomOffset(20f);
    }

BarChart数据初始化,BarChart和HorizationalBarChart数据格式一样,
yValuse表示Y轴显示的数据,一般是数字,X轴设置数据必须实现  xAxis.setValueFormatter方法;

以下是BarChartde数据构造方法示例

 initBarCahrt(err_chart);
        int co = countBeenList.size();
        final ArrayList xValue = new ArrayList<>();
        int[] colors = new int[co];
        // y轴的数据集合
        ArrayList yValues = new ArrayList<>();
        for (int i=0;i
//设置X轴的数据,需要重新设置其格式,才能显示你自定义的项,Y轴同理
        XAxis xAxis = err_chart.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int n = (int) value;
                return xValue.get(n);
            }
        });
//        xAxis.setLabelRotationAngle(-20);//设置x轴字体显示角度
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴的位置TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE, BOTTOM_INSIDE
        xAxis.setDrawGridLines(false);//回执网格线
//        xAxis.setLabelCount(12);
        err_chart.animateY(2000); // 立即执行的动画
//        Legend mLegend = barChart.getLegend(); // 设置比例图标示
////         设置窗体样式
//        mLegend.setForm(Legend.LegendForm.CIRCLE);
////         字体
//        mLegend.setFormSize(9f);
//        mLegend.setTextSize(12f);
//        mLegend.setXEntrySpace(10);
//        mLegend.setTextColor(Color.BLACK);
//        mLegend.setWordWrapEnabled(true);
////         字体颜色
//        mLegend.setTextColor(Color.parseColor("#00000000"));

//        barChart.fitScreen();
        err_chart.setData(errBarData);
        err_chart.invalidate();

以下是HorizationalBarChart数据构造方法示例

initBarCahrt(area_chart);
        int cou = msgBeenList.size();
        final ArrayList xValue = new ArrayList<>();
        ArrayList yValue = new ArrayList<>();
        int[] areaColors = new int[cou];
        for (int i=0;iyAxis.setAxisMinimum(0);
        XAxis xAxis = area_chart.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int n = (int)value;
                return xValue.get(n);
            }
        });
        xAxis.setDrawGridLines(false);//回执网格线
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置起点位置
//        xAxis.setAxisMinimum(0);
        area_chart.getAxisLeft().setEnabled(false);
        area_chart.getAxisRight().setEnabled(true);
        area_chart.animateX(2000);
        area_chart.setData(areaBarData);
        area_chart.setDrawValueAboveBar(true);
        area_chart.invalidate();


你可能感兴趣的:(android布局,android工具类,android第三方库)