一运挫、導(dǎo)入MPAndroidChart 3.0包
在項目中的build.gradle中導(dǎo)入compile'com.github.PhilJay:MPAndroidChart:v3.0.1'。
二蚓哩、項目中的使用(以柱狀圖為例)
新建布局文件activity_barchart。
<pre>
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/check_bc"
android:layout_width="match_parent"
android:layout_height="200dp"
android:paddingTop="10dp"
/>
</pre>
三蚪拦、項目中使用
-
獲取BarChart的id杖剪,并設(shè)置一些相關(guān)的屬性
<pre>private void initCheckParChart(){ check_bc = (BarChart) getView().findViewById(R.id.check_bc); check_bc.getDescription().setEnabled(false); Description description = new Description(); description.setText("驗收統(tǒng)計");//設(shè)置右下角的描述信息 description.setTextSize(14f);//設(shè)置描述信息字體大小 check_bc.setDescription(description); check_bc.setNoDataText("暫無數(shù)據(jù)"); //設(shè)置空表的描述信息 check_bc.setDrawGridBackground(false);//是否繪制網(wǎng)格背景 check_bc.setDrawValueAboveBar(true);//將Y數(shù)據(jù)顯示在點的上方 check_bc.setPinchZoom(true);//擠壓縮放 check_bc.setDrawBarShadow(false); check_bc.setDrawGridBackground(false); check_bc.setScaleYEnabled(false); check_bc.setDoubleTapToZoomEnabled(false);//雙擊縮放 check_bc.animateY(2500); check_bc.getLegend().setEnabled(false); initCheckData(); }
</pre>
2.解析統(tǒng)計數(shù)據(jù)
數(shù)據(jù)使用的是自己寫的json串冻押,后期可以修改成從服務(wù)器獲取的json驰贷。
<pre>
private String CHECKJSON = "{\n" +
" \"rows\":[\n" +
" {\n" +
" \"NAME\":\"立項\",\n" +
" \"NUM\":\"10\"\n" +
" },\n" +
" {\n" +
" \"NAME\":\"招標(biāo)\",\n" +
" \"NUM\":\"26\"\n" +
" },\n" +
" {\n" +
" \"NAME\":\"簽合同\",\n" +
" \"NUM\":\"30\"\n" +
" },\n" +
" {\n" +
" \"NAME\":\"驗收\",\n" +
" \"NUM\":\"24\"\n" +
" },\n" +
" {\n" +
" \"NAME\":\"結(jié)項\",\n" +
" \"NUM\":\"15\"\n" +
" }\n" +
" ]\n" +
"}";
</pre>
<pre>
public void initCheckData(){
try {
JSONObject object = new JSONObject(CHECKJSON);
JSONArray array = object.getJSONArray("rows");
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
ArrayList<String> xVals = new ArrayList<String>();
for(int j = 0;j<array.length();j++){
JSONObject jsonObject = array.getJSONObject(j);
BarEntry barEntry = new BarEntry(j,Float.parseFloat(jsonObject.optString("NUM")));
xVals.add(jsonObject.optString("NAME"));
entries.add(barEntry);
}
setData(entries,xVals);
} catch (JSONException e) {
e.printStackTrace();
}
}
</pre>
3.給柱狀圖添加數(shù)據(jù)
最需要注意的是3.0以后給X軸賦值為漢字的時候不能使用BarData bardata = new BarData( xVals,dataSets)方法了洛巢,我使用以下方法給X軸添加漢字括袒。在使用的同時需要注意一定要添加此方法xAxis.setLabelCount(xVals.size())來設(shè)置X軸的個數(shù),如果不使用會出現(xiàn)多個相同的名稱稿茉。
<pre>
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf(xVals.get((int) value));
}
});
</pre>
<pre>
private void setData(ArrayList<BarEntry> entries,final ArrayList<String> xVals) {
XAxis xAxis = check_bc.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
//設(shè)置x軸的數(shù)據(jù)
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf(xVals.get((int) value));
}
});
//顯示個數(shù)
xAxis.setLabelCount(xVals.size());
BarDataSet dataSet = new BarDataSet(entries, "驗收統(tǒng)計");
//數(shù)據(jù)和顏色
ArrayList<Integer> colors = new ArrayList<Integer>();
// 柱狀圖顏色
colors.add(Color.rgb(44,120,221));
colors.add(Color.rgb(239,167,52));
colors.add(Color.rgb(71,183,61));
colors.add(Color.rgb(87,162,253));
colors.add(Color.rgb(255,125,39));
dataSet.setColors(colors);
// dataSet.setValueTextColors(colors);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(dataSet);
BarData bardata = new BarData(dataSets);
bardata.setDrawValues(true);
bardata.setValueTextColor(Color.BLACK);
bardata.setValueTextSize(13);
check_bc.setData(bardata);
check_bc.animateXY(800,800);//圖表數(shù)據(jù)顯示動畫
check_bc.setVisibleXRangeMaximum(15);//設(shè)置屏幕顯示條數(shù)
check_bc.invalidate();
//刷新
check_bc.invalidate();
}
</pre>
作者水平有限锹锰,如有錯誤,請多多指教漓库。