分組直方圖
代碼
private void showBarChart() {
setBarChartProperties();
setBarChartMarkView();
setXAxis();
setYAxis();
setBarChartLegend();
setBarChartData(xListValue.size(),yListValue);
}
private void setBarChartProperties() {
mBarChart.setDrawBarShadow(false);
mBarChart.setDrawValueAboveBar(false);
mBarChart.getDescription().setEnabled(false);
mBarChart.setPinchZoom(false);
mBarChart.setDrawGridBackground(false);
float ratio = (float) xListValue.size()/(float) 10;
mBarChart.zoom(ratio,1f,0,0);
mBarChart.animateY(1500);
mBarChart.setScaleEnabled(false);
mBarChart.setTouchEnabled(true);
mBarChart.setDragEnabled(true);
}
private void setBarChartMarkView() {
PandectMarkerView mv = new PandectMarkerView(getActivity(), R.layout.custom_marker_view);
mv.setChartView(mBarChart);
mBarChart.setMarker(mv);
}
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(10);
xAxis.setCenterAxisLabels(true);
xAxis.setValueFormatter(xValueFormatter);
xAxis.setLabelRotationAngle(-40f);
}
private void setYAxis() {
YAxis leftYAxis = mBarChart.getAxisLeft();
leftYAxis.setDrawGridLines(true);
leftYAxis.setAxisMinimum(0f);
leftYAxis.setEnabled(true);
YAxis rightYAxis = mBarChart.getAxisRight();
rightYAxis.setEnabled(false);
}
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);
float barWidth = (float) ((1 - 0.12) / yListValue.size() / 10 * 9);
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.setDrawValues(false);
mBarChart.setData(data);
mBarChart.getBarData().setBarWidth(barWidth);
mBarChart.getXAxis().setAxisMinimum(startYear);
mBarChart.getXAxis().setAxisMaximum(startYear + mBarChart.getBarData().getGroupWidth(groupSpace, barSpace) * count);
mBarChart.groupBars(startYear, groupSpace, barSpace);
mBarChart.invalidate();
}