Android圖表庫之MPAndroidChart使用技巧(基於v3.1.0版本)


MPAndroidChart:一個強大的Android圖表視圖/圖形視圖庫,支持線-柱-餅-雷達-氣泡-散點-燭台圖表以及縮放,拖動和動畫。

 

可實現但不限於如下圖表效果

♠折線圖(LineChart)

♠柱狀圖(BarChart)

♠餅狀圖(PieChart)

♠散點圖(ScatterChart)

♠燭台圖(CandleStickChart)

♠氣泡圖(BubbleChart)

 ♠雷達圖(RadarChart)

 

 ♠聯合圖(Combined-Chart)

 

 開始使用

1、添加庫

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

2、創建View

//靜態布局
<com.github.mikephil.charting.charts.LineChart
     android:id="@+id/chart"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

/*************************或*************************/

//動態布局
LineChart chart = new LineChart(context);

3、設置表屬性/交互(不設置則采用默認值)

Description description = new Description();//描述信息
description.setEnabled(false);//是否可用
chart.setDescription(description);//不然會顯示默認的 Description。
chart.setTouchEnabled(true); // 設置是否可以觸摸
chart.setDragEnabled(true);// 是否可以拖拽
chart.setScaleEnabled(false);// 是否可以縮放
chart.setPinchZoom(false);//y軸的值是否跟隨圖表變換縮放;如果禁止,y軸的值會跟隨圖表變換縮放
chart.setDoubleTapToZoomEnabled(false);//是否允許雙擊進行縮放
chart.setScaleXEnabled(false);//是否允許以X軸縮放
chart.setDrawGridBackground(false);// 是否顯示表格顏色
chart.setGridBackgroundColor(Color.TRANSPARENT);// 表格的的顏色
chart.animateY(1000, Easing.Linear);//設置動畫
chart.setExtraBottomOffset(5f);//防止底部數據顯示不完整,設置底部偏移量
//x軸配置
XAxis xAxis = lineChart.getXAxis();
xAxis.setEnabled(true);//是否可用
xAxis.setDrawLabels(true);//是否顯示數值
xAxis.setDrawAxisLine(true);//是否顯示坐標線
xAxis.setAxisLineColor(Color.BLACK);//設置坐標軸線的顏色
xAxis.setAxisLineWidth(0.8f);//設置坐標軸線的寬度
xAxis.setDrawGridLines(false);//是否顯示豎直風格線
xAxis.setTextColor(Color.BLACK);//X軸文字顏色
xAxis.setTextSize(12f);//X軸文字大小
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//X軸文字顯示位置
xAxis.setSpaceMin(1f);//左空白區大小
xAxis.setSpaceMax(1f);//右空白區大小
//左y軸配置
YAxis lyAxis = lineChart.getAxisLeft();
lyAxis.setEnabled(true);//是否可用
lyAxis.setDrawLabels(true);//是否顯示數值
lyAxis.setDrawAxisLine(false);//是否顯示坐標線
lyAxis.setDrawGridLines(true);//是否顯示水平網格線
lyAxis.setDrawZeroLine(true);////是否繪制零線
lyAxis.setZeroLineColor(Color.BLACK);
lyAxis.setZeroLineWidth(0.8f);
lyAxis.enableGridDashedLine(10f, 10f, 0f);//網格虛線
lyAxis.setGridColor(Color.BLACK);//網格線顏色
lyAxis.setGridLineWidth(0.8f);//網格線寬度
lyAxis.setAxisLineColor(Color.BLACK);//坐標線顏色
lyAxis.setTextColor(Color.BLACK);//左側文字顏色
lyAxis.setTextSize(12f);//左側文字大小
//右y軸配置
YAxis ryAxis = lineChart.getAxisRight();
ryAxis.setEnabled(false);//是否可用
//標簽配置
Legend legend = lineChart.getLegend();
legend.setEnabled(false);//是否可用

4、添加數據

//x軸和y軸的數據
YourData[] dataObjects = ...;
List<Entry> entries = new ArrayList<Entry>();
for (YourData data : dataObjects) {
    entries.add(new Entry(data.getValueX(), data.getValueY())); 
}

LineDataSet dataSet = new LineDataSet(entries, "Label"); // 添加數據
 // 自定義各種數據樣式(不設置則使用默認值)
dataSet.setColor(...);
dataSet.setValueTextColor(...);
...

5、填充數據並顯示

LineData lineData = new LineData(dataSet);
chart.setData(lineData);
chart.invalidate(); // 刷新

 

更多自定義樣式及圖表使用方法可參考以下文檔:

英文文檔:https://weeklycoding.com/mpandroidchart-documentation/

中文文檔:https://www.jianshu.com/p/fc73b490edd5

 

庫地址及其運行apk:

github庫地址:https://github.com/PhilJay/MPAndroidChart

apk下載地址(要有梯子):https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample

沒有梯子?已經幫你下載好了:https://pan.baidu.com/s/1rzNigQMDfJOLB6v4tDVtuw  提取碼:22f3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM