BarChart柱狀圖(多組)

多組柱狀圖.png
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp" />
首先在activity中對(duì)柱狀圖的屬性進(jìn)行設(shè)置
其中mChart是對(duì)柱狀圖的樣式及效果進(jìn)行設(shè)置,Legend是圖表標(biāo)題圖例位置樣式的設(shè)置
/***圖表設(shè)置***/
//背景顏色
chart.setBackgroundColor(Color.WHITE);
//不顯示圖表網(wǎng)格
chart.setDrawGridBackground(false);
//背景陰影
chart.setDrawBarShadow(false);
chart.setHighlightFullBarEnabled(false);
//顯示邊框
chart.setDrawBorders(true);
Matrix m = new Matrix();
m.postScale(10f, 1f);//兩個(gè)參數(shù)分別是x,y軸的縮放比例。例如:將x軸的數(shù)據(jù)放大為之前的1.5倍
chart.getViewPortHandler().refresh(m, chart, false);//將圖表動(dòng)畫(huà)顯示之前進(jìn)行縮放
chart.animateX(1000);
/***XY軸的設(shè)置***/
//X軸設(shè)置顯示位置在底部
xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);//設(shè)置標(biāo)簽居中
leftAxis = chart.getAxisLeft();
rightAxis = chart.getAxisRight();
//保證Y軸從0開(kāi)始,不然會(huì)上移一點(diǎn)
leftAxis.setAxisMinimum(0f);
rightAxis.setAxisMinimum(0f);
/***折線圖例 標(biāo)簽 設(shè)置***/
legend = chart.getLegend();
legend.setForm(Legend.LegendForm.LINE);
legend.setTextSize(11f);
//顯示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
//是否繪制在圖表里面
legend.setDrawInside(false);
在設(shè)置好柱狀圖的樣式及效果之后,對(duì)其中的數(shù)據(jù)進(jìn)行設(shè)置,list為數(shù)據(jù)值
屬性為T(mén)ime,OneNum,TwoNum,ThreeNum
多組主要設(shè)置為
- float groupSpace = 0.04f;
- float barSpace = 0.02f;
- float barWidth = 0.3f;
三個(gè)數(shù)值相加小于1
float groupSpace = 0.04f;
float barSpace = 0.02f;
float barWidth = 0.3f;
List<BarEntry> barEntryList = new ArrayList();
for (int i = 0; i < list.size(); i++) {
if (list.getOneNum()==null||list.getOneNum()==0)
barEntryList.add(new BarEntry((float) i, 0f));
else
barEntryList.add(new BarEntry((float) i, list.getOneNum()));
}
BarDataSet dataset1 = new BarDataSet(barEntryList, "數(shù)據(jù)一");
dataset1.setColor(Color.rgb(255, 0, 0));
List<BarEntry> barEntryList1 = new ArrayList();
for (int i = 0; i < list.size(); i++) {
if (list.getTwoNum()==null||list.getTwoNum()==0)
barEntryList1.add(new BarEntry((float) i, 0f));
else
barEntryList1.add(new BarEntry((float) i, list.getTwoNum()));
}
BarDataSet dataset2 = new BarDataSet(barEntryList1, "數(shù)據(jù)二");
dataset2.setColor(Color.rgb(0, 255, 0));
List<BarEntry> barEntryList2 = new ArrayList();
for (int i = 0; i < list.size(); i++) {
if (list.getThreeNum()==null||list.getThreeNum()==0)
barEntryList2.add(new BarEntry((float) i, 0f));
else
barEntryList2.add(new BarEntry((float) i,list.getThreeNum()));
}
BarDataSet dataset3 = new BarDataSet(barEntryList2, "數(shù)據(jù)三");
dataset3.setColor(Color.rgb(0, 0, 255));
final LevelOneEntity levelOneEntity1 = levelOneEntity;
List<IBarDataSet> dataSetList = new ArrayList<>();
dataSetList.add(dataset1);
dataSetList.add(dataset2);
dataSetList.add(dataset3);
BarData data = new BarData(dataSetList);
data.setValueTextSize(10f);
data.setBarWidth(0.9f);
設(shè)置x軸標(biāo)簽樣式
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f);
xAxis.setLabelCount(list.size());
xAxis.setCenterAxisLabels(true);//設(shè)置標(biāo)簽居中
設(shè)置圖表柱狀圖樣式寬度
chart.setData(data);
chart.getBarData().setBarWidth(barWidth);
chart.getXAxis().setAxisMinimum(0);
chart.getXAxis().setAxisMaximum(chart.getBarData().getGroupWidth(groupSpace, barSpace) *list.size() + 0);
chart.groupBars(0, groupSpace, barSpace);
chart.invalidate(); //將圖表重繪以顯示設(shè)置的屬性和數(shù)據(jù)
chart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return double2String(value, list);
}
});
X軸數(shù)字轉(zhuǎn)中文
public String double2String(float f, List list) {
String s = "";
if (f >= 0 && f < list.size())
s = list.getName();
return s;
}