MPAndroidChart直方圖、多組(可滑動、漸變可以看博主前面講直方圖的博客)

分組直方圖

這裏寫圖片描述


代碼

private void showBarChart() {
        setBarChartProperties();
        setBarChartMarkView();
        setXAxis();
        setYAxis();
        setBarChartLegend();
        setBarChartData(xListValue.size(),yListValue);
    }

    private void setBarChartProperties() {
        mBarChart.setDrawBarShadow(false);//設置陰影爲false
        mBarChart.setDrawValueAboveBar(false);//這裏設置爲true每一個直方圖的值就會顯示在直方圖的頂部
        mBarChart.getDescription().setEnabled(false);//設置描述不顯示
        mBarChart.setPinchZoom(false);
        mBarChart.setDrawGridBackground(false);//設置不顯示網格
        //mbarchart.setMaxVisibleValueCount(60);
        float ratio = (float) xListValue.size()/(float) 10;
        mBarChart.zoom(ratio,1f,0,0);//顯示的時候是按照多大的比率縮放顯示  1f表示不放大縮小
        //我默認手機屏幕上顯示10  剩下的滑動直方圖 然後顯示。。假如要顯示25個 那麼除以10 就是放大2.5f。。同理
        // 56個民族   那麼放大5.6f

        mBarChart.animateY(1500);//從Y軸彈出的動畫時間
        //mBarChart.getLegend().setEnabled(false);//設置不顯示比例圖
        mBarChart.setScaleEnabled(false);//設置是否可以縮放
        mBarChart.setTouchEnabled(true);//設置是否可以觸摸
        mBarChart.setDragEnabled(true);//設置是否可以拖拽
    }
//定義點擊直方圖頂部彈出的類似popuwindow的標記
private void setBarChartMarkView() {
        // create a custom MarkerView (extend MarkerView) and specify the layout
        // to use for it
        PandectMarkerView mv = new PandectMarkerView(getActivity(), R.layout.custom_marker_view);
        mv.setChartView(mBarChart); // For bounds control
        mBarChart.setMarker(mv); // Set the marker to the chart
    }
private void setXAxis() {
        //自定義設置橫座標
        IAxisValueFormatter xValueFormatter = new PandectModelFourXValueFormatter(xListValue);
        //設置不顯示網格線,保留水平線
        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        //xAxis.setLabelCount(xListValue.size());//設置橫座標顯示多少個
        xAxis.setLabelCount(10);//一個界面顯示10個Lable。那麼這裏要設置11個
        xAxis.setCenterAxisLabels(true);//字體下面的標籤 顯示在每個直方圖的中間
        xAxis.setValueFormatter(xValueFormatter);//將自定義的橫座標設置上去
        xAxis.setLabelRotationAngle(-40f);
    }
private void setYAxis() {
        //左邊Y軸
        YAxis leftYAxis = mBarChart.getAxisLeft();
        leftYAxis.setDrawGridLines(true);//設置從Y軸左側發出橫線
        leftYAxis.setAxisMinimum(0f);
        leftYAxis.setEnabled(true);//設置顯示左邊Y座標
        //右邊Y軸
        YAxis rightYAxis = mBarChart.getAxisRight();
        rightYAxis.setEnabled(false);//設置隱藏右邊y座標
    }
private void setBarChartLegend() {
        Legend l = mBarChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setYOffset(0f);
        l.setXOffset(10f);
        l.setFormSize(8f);
        l.setXEntrySpace(4f);
        l.setEnabled(true);//設置顯示比例圖
    }
    private int[] mColors = new int[] {
            Color.parseColor("#C23531"),
            Color.parseColor("#2F4554")
    };

    private void setBarChartData(int count, ArrayList<ArrayList<Float>> yListValue) {
        // 這裏是通用的設置
        float groupSpace = 0.12f; //柱狀圖組之間的間距
        float barSpace = (float) ((1 - 0.12) / yListValue.size() / 10); // x4 DataSet
        float barWidth = (float) ((1 - 0.12) / yListValue.size() / 10 * 9); // x4 DataSet
        //滿足的規則:(barWidth + barSpace)*4 + groupSpace = 1.00 -> interval per "group"
        int startYear = 0;
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        for(int i=0;i<yListValue.size();i++){
            ArrayList<BarEntry> yValues = new ArrayList<>();
            for(int j=0;j<count;j++){
                yValues.add(new BarEntry(j,yListValue.get(i).get(j)));
            }
            BarDataSet set = new BarDataSet(yValues,lableNames.get(i));
            set.setDrawIcons(false);//設置直方圖上面時候顯示圖標
            set.setColor(mColors[i%mColors.length]);
            dataSets.add(set);
        }
        BarData data = new BarData(dataSets);
        data.setValueFormatter(new LargeValueFormatter());
        data.setValueTextSize(10f);//設置直方圖上面文字的大小
        data.setBarWidth(0.9f);//設置直方圖的寬度
        data.setValueTextColor(Color.WHITE);
        data.setDrawValues(true);//設置直方圖上面的值顯示
        //data.setBarWidth(barWidth);//直方圖的寬度
        data.setDrawValues(false);//設置不顯示值
        mBarChart.setData(data);//設置值

        mBarChart.getBarData().setBarWidth(barWidth);//設置直方圖每個bar的寬度
        // restrict the x-axis range
        mBarChart.getXAxis().setAxisMinimum(startYear);//設置X軸的最小值從startYear開始

        //要實現分組下面的代碼必須實現,博主理解的不清楚,雖然簡單地懂 不好意思 O(∩_∩)O
        // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
        mBarChart.getXAxis().setAxisMaximum(startYear + mBarChart.getBarData().getGroupWidth(groupSpace, barSpace) * count);
        //起始點、直方圖組間距、直方圖之間的間距
        mBarChart.groupBars(startYear, groupSpace, barSpace);

        mBarChart.invalidate();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章