Android之MPAndroidChart庫——雙柱狀圖,X軸顯示漢字
阿新 • • 發佈:2019-01-27
public static void setTwoBarChart(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, String bartilte1, String bartitle2) { barChart.getDescription().setEnabled(false);//設定描述 barChart.setPinchZoom(true);//設定按比例放縮柱狀圖 barChart.setExtraBottomOffset(10); barChart.setExtraTopOffset(30); //x座標軸設定 XAxis xAxis = barChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setDrawLabels(true); xAxis.setGranularity(1f); xAxis.setLabelCount(xAxisValue.size()); xAxis.setCenterAxisLabels(true);//設定標籤居中 xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisValue)); //y軸設定 YAxis leftAxis = barChart.getAxisLeft(); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); leftAxis.setDrawGridLines(false); leftAxis.setDrawLabels(false); leftAxis.setDrawAxisLine(false); //設定座標軸最大最小值 Float yMin1 = Collections.min(yAxisValue1); Float yMin2 = Collections.min(yAxisValue2); Float yMax1 = Collections.max(yAxisValue1); Float yMax2 = Collections.max(yAxisValue2); Float yMin = Double.valueOf((yMin1 < yMin2 ? yMin1 : yMin2) * 0.1).floatValue(); Float yMax = Double.valueOf((yMax1 > yMax2 ? yMax1 : yMax2) * 1.1).floatValue(); leftAxis.setAxisMaximum(yMax); leftAxis.setAxisMinimum(yMin); barChart.getAxisRight().setEnabled(false); //圖例設定 Legend legend = barChart.getLegend(); legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); legend.setDrawInside(false); legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT); legend.setForm(Legend.LegendForm.SQUARE); legend.setTextSize(12f); //設定柱狀圖資料 setTwoBarChartData(barChart, xAxisValue, yAxisValue1, yAxisValue2, bartilte1, bartitle2); barChart.animateX(1500);//資料顯示動畫,從左往右依次顯示 barChart.invalidate(); } /** * 設定柱狀圖資料來源 */ private static void setTwoBarChartData(BarChart barChart, List<String> xAxisValue, List<Float> yAxisValue1, List<Float> yAxisValue2, String bartilte1, String bartitle2) { float groupSpace = 0.04f; float barSpace = 0.03f; float barWidth = 0.45f; // (0.45 + 0.03) * 2 + 0.04 = 1,即一個間隔為一組,包含兩個柱圖 -> interval per "group" ArrayList<BarEntry> entries1 = new ArrayList<>(); ArrayList<BarEntry> entries2 = new ArrayList<>(); for (int i = 0, n = yAxisValue1.size(); i < n; ++i) { entries1.add(new BarEntry(i, yAxisValue1.get(i))); entries2.add(new BarEntry(i, yAxisValue2.get(i))); } BarDataSet dataset1, dataset2; if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) { dataset1 = (BarDataSet) barChart.getData().getDataSetByIndex(0); dataset2 = (BarDataSet) barChart.getData().getDataSetByIndex(1); dataset1.setValues(entries1); dataset2.setValues(entries2); barChart.getData().notifyDataChanged(); barChart.notifyDataSetChanged(); } else { dataset1 = new BarDataSet(entries1, bartilte1); dataset2 = new BarDataSet(entries2, bartitle2); dataset1.setColor(Color.rgb(129, 216, 200)); dataset2.setColor(Color.rgb(181, 194, 202)); ArrayList<IBarDataSet> dataSets = new ArrayList<>(); dataSets.add(dataset1); dataSets.add(dataset2); BarData data = new BarData(dataSets); data.setValueTextSize(10f); data.setBarWidth(0.9f); data.setValueFormatter(new IValueFormatter() { @Override public String getFormattedValue(float value, Entry entry, int i, ViewPortHandler viewPortHandler) { return StringUtils.double2String(value, 2); } }); barChart.setData(data); } barChart.getBarData().setBarWidth(barWidth); barChart.getXAxis().setAxisMinimum(0); // barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters barChart.getXAxis().setAxisMaximum(barChart.getBarData().getGroupWidth(groupSpace, barSpace) * xAxisValue.size() + 0); barChart.groupBars(0, groupSpace, barSpace); }