MPAndroidChart3.0使用詳解(一)----基本屬性設定
阿新 • • 發佈:2019-02-12
說到Android中的圖表庫,除了老牌的谷歌推薦的Achartengine圖表庫,那不得不提一下MPAndroidChart這個類庫了,相比前者,後者功能十分豐富,github的star超過17K。其支援直方圖(柱狀圖)、折線圖、餅狀圖、氣泡圖、雷達圖、散點圖、蠟燭圖、組合圖。支援縮放、點選事件、動畫效果等。截至於本文,最新的版本是3.0.2,相比以前的2.x的版本,功能上有很大的豐富,支援的屬性更多了,API改動有點大。詳細的功能和特性介紹參見其github地址:https://github.com/PhilJay/MPAndroidChart
使用前,要先新增依賴:
首先在專案的build.gradle檔案下新增如下:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
然後在相應的module的build.gradle下新增依賴:
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
}
下面就一些常用的屬性來說明
軸的屬性(XAxis/YAxis)
leftAxis = mBarChart.getAxisLeft();//獲得左邊Y軸物件例項
rightAxis = mBarChart.getAxisRight();//獲得右邊Y軸物件例項
xAxis = mBarChart.getXAxis();//獲得x軸物件例項
獲得物件的例項後,我們就可以通過這些例項來設定一些常用的屬性
setEnabled(boolean enabled): //是否啟用軸,如果禁用,關於軸的設定所有屬性都將被忽略
setDrawLabels(boolean enabled):// 是否繪製標籤
setDrawAxisLine(boolean enabled):// 是否繪製軸線
setDrawGridLines(boolean enabled)://是否和網格軸線
setAxisLineWidth(float f)://設定軸線的寬度
setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);//設定軸的值顯示的位置
setAxisMinValue(float min): //設定軸的最小值。這樣設定將不會根據提供的資料自動計算。
setGranularity(float gran)://設定Y軸最小間隔
setShowOnlyMinMax(boolean enabled)://如果啟用,此軸直線式最大值和最小值架構忽略定義的標籤數。
setLabelCount(int count, boolean force): //設定軸的標籤數目,不是精確值,如果強制設定,可能導致軸線不均勻
setInverted(boolean enabled): //反轉該軸,如果為true,最大值在底部,頂部是最小值。
setSpaceTop(float percent): //設定軸上最高位置在表中最高位置的頂部間距,佔總軸的百分比。
setSpaceBottom(float percent): //設定軸上最低位置在表中最低位置的底部間距,佔總軸的百分比。
setAvoidFirstLastClipping(boolean enabled)://如果設定為true,將避免圖表或螢幕的邊緣的第一個和最後一個軸中的標籤條目被裁剪。
setSpaceBetweenLabels(int characters)://設定應該x軸標籤之間的被排除在外字元,預設空間:4。
enableGridDashedLine(float lineLength, float spaceLength, float phase): //使網格線在虛線畫模式,lineLength控制線長度 , spaceLength控制線之間空格長度, phase 控制起點
setDragScaleEnabled(boolean enabled): //設定是否可以拖拽,縮放
setHighlightEnabled(boolean enabled) ://設定點選value的時候,是否高亮顯示
x軸格式化值
setValueFormatter(XAxisValueFormatter formatter):在繪製之前,動態的設定自定義格式.
public class StringAxisValueFormatter implements IAxisValueFormatter {
private List<String> xValues;
public StringAxisValueFormatter(List<String> xValues) {
this.xValues = xValues;
}
@Override
public String getFormattedValue(float v, AxisBase axisBase) {
if (v < 0 || v > (xValues.size() - 1)){//使得兩側柱子完全顯示
return "";
}
return xValues.get((int)v);
}
}
或者
public class MyValueFormatter implements ValueFormatter {
private DecimalFormat mFormat;
public MyValueFormatter() {
mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
}
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
// write your logic here
return mFormat.format(value) + " $"; // e.g. append a dollar-sign
}
}
使用
// usage on whole data object
lineData.setValueFormatter(new MyValueFormatter());
// usage on individual dataset object
lineDataSet.setValueFormatter(new MyValueFormatter());
chart圖表的基本公有屬性
setBackgroundColor(int color): //設定整個圖表檢視的背景
setDescription(String desc): //右下角對圖表的描述資訊
setDescriptionColor(int color): //描述資訊的顏色
setDescriptionPosition(float x, float y): //自定義描述資訊位置.
setDescriptionTypeface(Typeface t): //自定義描述資訊字型
setDescriptionTextSize(float size): //自定義描述資訊字型大小, 最小值6f, 最大值16f.
setNoDataText(String desc): //設定空表的描述資訊
setDrawGridBackground(boolean enabled): //是否繪製網格背景
setGridBackgroundColor(int color): //設定網格背景顏色
setDrawBorders(boolean enabled): //是否繪製邊線
setBorderColor(int color)://邊線顏色
setBorderWidth(float width)://邊線寬度,單位dp
setMaxVisibleValueCount(int count): //設定圖表繪製可見標籤數量最大值. 僅在setDrawValues() 啟用時生效
setMinOffset(float dp);//設定填充屬性,類似與padding的效果
setScaleEnabled(false);//禁止縮放
setTouchEnabled(boolean b);//設定是否可以觸控,注意:設定false後,點選事件不再生效,也沒有高亮的顯示了
setValueTypeface(Typeface t)://設定字型
dataSet.setDrawValues(boolean)//是dataSet的屬性,設定是否在圖上顯示出當前點(柱狀圖)的值
圖例的說明
//折線圖例 標籤 設定
Legend legend = mBarChart.getLegend();
legend.setForm(Legend.LegendForm.SQUARE);//圖示 標籤的形狀。 正方形
legend.setTextSize(11f);
//顯示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setDrawInside(false);
動畫效果
animateX(int durationMillis) : x軸方向
animateY(int durationMillis) : y軸方向
animateXY(int xDuration, int yDuration) : xy軸方向
設定點選事件
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
Log.e("---->",e.getX()+" "+e.getY());
}
@Override
public void onNothingSelected() {
}
});
好的,基本屬性就介紹到這裡了,下一篇介紹其基本的使用。