android開源圖表庫MPAndroidChart文檔翻譯(上)

來源:互聯網
上載者:User

標籤:

MPAndroidChart 是 Android 系統上一款開源的圖表庫。目前提供線圖和餅圖,支援選擇、縮放和拖放。

android開源圖表庫MPAndroidChar的githu地址:

https://github.com/PhilJay/MPAndroidChart

文檔地址:https://github.com/PhilJay/MPAndroidChart/wikiAPI地址:https://jitpack.io/com/github/PhilJay/MPAndroidChart/v2.2.5/javadoc/一、入門1、建立在Xml檔案中定義LineChart, BarChart, ScatterChart, CandleStickChart, PieChart, BubbleChart or RadarChart ,
 <com.github.mikephil.charting.charts.LineChart        android:id="@+id/chart"        android:layout_width="match_parent"        android:layout_height="match_parent" />
在Activity或者Fragment中引用
 // in this example, a LineChart is initialized from xml    LineChart chart = (LineChart) findViewById(R.id.chart);
或者直接建立
// programmatically create a LineChart    LineChart chart = new LineChart(Context);    // get a layout defined in xml    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);    rl.add(chart); // add the programmatically created chart
2、重新整理invalidate():這個方法能使圖表重繪.要使圖表更改生效,這個方法是必要的。
notifyDataSetChanged(): 讓圖表知道它的基礎資料發生更改,並執行所有必要的重新計算(offsets, legend, maxima, minima, ...)。 動態添加資料時,這是必須調用的。
3、列印日誌setLogEnabled(boolean enabled): 設定為true會啟用log輸出。 使用這種log會對效能造成影響, 沒有必要用的話關掉它。
4、圖表樣式一些樣式相關方法,可以直接使用有關更詳盡單獨圖表類型的樣式和設定,請看看具體的圖表設定的wiki頁面specific chart settings
setBackgroundColor(int color): 設定整個圖表視圖的背景
setDescription(String desc): 右下角對圖表的描述資訊
setDescriptionColor(int color): 描述資訊的顏色
setDescriptionPosition(float x, float y): 自訂描述資訊位置.
setDescriptionTypeface(Typeface t): 自訂描述資訊字型
setDescriptionTextSize(float size): 自訂描述資訊字型大小, 最小值6f, 最大值16f.
setNoDataTextDescription(String desc): 設定空表的描述資訊
setDrawGridBackground(boolean enabled): 是否繪製網格背景
setGridBackgroundColor(int color): 設定網格背景顏色
setDrawBorders(boolean enabled): 是否繪製邊線
setBorderColor(int color):邊線顏色
setBorderWidth(float width):邊線寬度,單位dp
setMaxVisibleValueCount(int count): 設定圖表繪製可見標籤數量最大值. 僅在setDrawValues() 啟用時生效

二、圖表的互動這個庫允許你自訂手勢與圖表視圖的互動的回調方法。
1、啟用/禁用互動setTouchEnabled(boolean enabled): 啟用圖表觸摸事件
setDragEnabled(boolean enabled): 啟用圖表拖拽事件
setScaleEnabled(boolean enabled): 啟用圖表縮放事件
setScaleXEnabled(boolean enabled): 啟用X軸上的縮放
setScaleYEnabled(boolean enabled):啟用Y軸上的縮放
setPinchZoom(boolean enabled): XY同時縮放
setDoubleTapToZoomEnabled(boolean enabled): 啟用雙擊縮放
setHighlightPerDragEnabled(boolean enabled): 拖拽超過表徵圖繪製畫布時高亮顯示
setHighlightPerTapEnabled(boolean enabled): 雙擊高亮顯示2、圖表的減速器setDragDecelerationEnabled(boolean enabled):抬起之後繼續滾動
setDragDecelerationFrictionCoef(float coef): 減速插值,取值範圍[0,1)。0表示立停止。值越大速度下降越緩慢3、高亮方式highlightValues(Highlight[] highs): 高亮點的集合,如果為空白,全部不高亮
highlightValue(int xIndex, int dataSetIndex): x軸上的資料集合高亮。如果為-1,全部不高兩
getHighlighted():擷取高亮點的集合
高亮顯示使用OnChartValueSelectedListener不會產生一個回調。可以通過ChartData或DataSet對象啟用和禁用高亮顯示。4、自訂高亮符號所有的使用者輸入在內部被預設ChartHighlighter類處理。它可以用下面的方法自訂實現替換預設highligher:
setHighlighter(ChartHighlighter highlighter): 通過繼承ChartHighlighter類實現自訂高亮符號。通過setHighlighter設定點擊等操作高亮顯示的符號
5、選擇回調該庫提供了互動後的一些後回調。其中之一是OnChartValueSelectedListener監聽器,通過觸摸高亮值時回調:
public interface OnChartValueSelectedListener {    /**    * Called when a value has been selected inside the chart.    *    * @param e The selected Entry.    * @param dataSetIndex The index in the datasets array of the data object    * the Entrys DataSet is in.    * @param h the corresponding highlight object that contains information    * about the highlighted position    */    public void onValueSelected(Entry e, int dataSetIndex, Highlight h);    /**    * Called when nothing has been selected or an "un-select" has been made.    */    public void onNothingSelected();}
讓你的需要接收回調的類實現介面,將它作為監聽器設定給chart
chart.setOnChartValueSelectedListener(this);
6、手勢回調OnChartGestureListener 這個回調可以定製手勢操作相關回調
public interface OnChartGestureListener {    /**     * Callbacks when a touch-gesture has started on the chart (ACTION_DOWN)     *     * @param me     * @param lastPerformedGesture     */    void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);    /**     * Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL)     *     * @param me     * @param lastPerformedGesture     */    void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);    /**     * Callbacks when the chart is longpressed.     *      * @param me     */    public void onChartLongPressed(MotionEvent me);    /**     * Callbacks when the chart is double-tapped.     *      * @param me     */    public void onChartDoubleTapped(MotionEvent me);    /**     * Callbacks when the chart is single-tapped.     *      * @param me     */    public void onChartSingleTapped(MotionEvent me);    /**     * Callbacks then a fling gesture is made on the chart.     *      * @param me1     * @param me2     * @param velocityX     * @param velocityY     */    public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY);   /**     * Callbacks when the chart is scaled / zoomed via pinch zoom gesture.     *      * @param me     * @param scaleX scalefactor on the x-axis     * @param scaleY scalefactor on the y-axis     */    public void onChartScale(MotionEvent me, float scaleX, float scaleY);   /**    * Callbacks when the chart is moved / translated via drag gesture.    *    * @param me    * @param dX translation distance on the x-axis    * @param dY translation distance on the y-axis    */    public void onChartTranslate(MotionEvent me, float dX, float dY);}
三、軸AxisBase 主要是AxisBase 這個類,他是XAxis 和YAxis的基類。
下面提到的方法可以應用到這兩個軸。
軸類允許自訂樣式和(可以包含)由以下組件/組件:
標籤(在垂直(y軸)或水平(x軸)對齊),其中包含軸描述值
繪製了一個所謂的“axis-line”,在標籤旁邊直接繪製,與標籤平行
LimitLines,允許存在特殊的資訊,如邊界或限制。
1、控制應該繪製哪些部分(軸)setEnabled(boolean enabled): 是否啟用軸,如果禁用,關於軸的設定所有屬性都將被忽略
setDrawLabels(boolean enabled): 是否繪製標籤
setDrawAxisLine(boolean enabled): 是否繪製軸線
setDrawGridLines(boolean enabled):是否和網格軸線
2、修改軸的樣式setTextColor(int color): 設定軸標籤顏色
setTextSize(float size): 設定軸標籤字型大小,單位dp
setTypeface(Typeface tf): 設定軸標籤字型
setGridColor(int color): 設定網格顏色
setGridLineWidth(float width):設定網格寬度.
setAxisLineColor(int color): 設定軸線顏色
setAxisLineWidth(float width): 設定軸線寬度
enableGridDashedLine(float lineLength, float spaceLength, float phase): 使網格線在虛線畫模式,
lineLength控制線長度 , spaceLength控制線之間空格長度, phase 控制起點
3、LimitLine 類兩軸支援,所謂LimitLines允許顯示特殊資訊,如邊界或限制。LimitLine在水平方向時添加到YAxis,而在垂直方向時添加到XAxis。這是如何從軸添加和刪除LimitLines
addLimitLine(LimitLine l): 在軸上添加新的 LimitLine 
removeLimitLine(LimitLine l): 從軸上移除 LimitLine 
還有更多添加/移除可用的方法。
setDrawLimitLinesBehindData(boolean enabled):允許控制LimitLines之間的z軸上的實際的資料順序。如果設定為true,LimitLines在真實資料後邊繪製,,否則在上面。預設false
正如它的名字LimitLine,可以用來為使用者提供額外限制的資訊。
例如,你的圖表可能顯示不同喲使用者的血壓測量結果。為了通知使用者超過140毫米汞柱的收縮壓被認為是一種健康的風險,你可以添加一個LimitLine 140提供這些資訊。
YAxis leftAxis = chart.getAxisLeft();LimitLine ll = new LimitLine(140f, "Critical Blood Pressure");ll.setLineColor(Color.RED);ll.setLineWidth(4f);ll.setTextColor(Color.BLACK);ll.setTextSize(12f);// .. and more styling optionsleftAxis.addLimitLine(ll);
四、X軸XAxisXAxis是AxisBase的子類。
XAxis類(在2.0.0版本包含調用),是所有的資料和資訊的容器與水平軸有關。。XAxis顯示什麼是交給ChartData對象作為一個ArrayList<String> 或者String[]。
XAxis類允許自訂樣式和以下部分:
水平對齊標籤繪製,其中包含軸描述值,為圖表X軸提供的資料對象設定。
在標籤旁邊與標籤平行繪製了一個“axis-line”。
每個在垂直方向座標軸標籤的網格線。
擷取執行個體方法
XAxis xAxis = chart.getXAxis();
1、自訂軸的值setLabelsToSkip(int count): 設定應該在軸線繪製下一個標籤前要跳過標籤的數量。這將禁用自動計算軸標籤之間的空間的功能,設定跳過由該方法提供的固定數量的標籤數。調用resetLabelsToSkip(...)重新啟用自動計算
resetLabelsToSkip():禁用標記被跳過的自訂的數目,自動計算軸標籤之間的空間
setAvoidFirstLastClipping(boolean enabled):如果設定為true,將避免圖表或螢幕的邊緣的第一個和最後一個軸中的標籤條目被裁剪。
setSpaceBetweenLabels(int characters):設定應該x軸標籤之間的被排除在外字元,預設空間:4。
setPosition(XAxisPosition pos): x軸應該出現的位置。頂部底部都出現,頂部,底部,BOTH_SIDED,TOP_INSIDE或BOTTOM_INSIDE之間進行選擇。2、格式化值setValueFormatter(XAxisValueFormatter formatter):在繪製之前,動態設定自訂格式,詳細資料3、程式碼範例
XAxis xAxis = chart.getXAxis();xAxis.setPosition(XAxisPosition.BOTTOM);xAxis.setTextSize(10f);xAxis.setTextColor(Color.RED);xAxis.setDrawAxisLine(true);xAxis.setDrawGridLines(false);// set a custom value formatterxAxis.setXValueFormatter(new MyCustomFormatter()); // and more...
五、Y軸YAxisYAxis 是AxisBase的子類。
YAxis 類是與垂直軸相關的所有資料和資訊容器,與左邊右邊垂直的軸相關。RadarChart 只有一個Y軸,預設情況下,表徵圖的兩個軸都啟用繪製。
1、擷取Y軸執行個體的方法
YAxis leftAxis = chart.getAxisLeft();YAxis rightAxis = chart.getAxisRight();YAxis leftAxis = chart.getAxis(AxisDependency.LEFT);YAxis yAxis = radarChart.getYAxis(); // this method radarchart only
2、自訂軸的值setAxisMaxValue(float max): 設定軸的最大值。如果設定,此值不會自動根據提供的資料計算。
resetAxisMaxValue():撤銷之前設定的最大值,將自動計算最大值。
setAxisMinValue(float min): 設定軸的最小值。這樣設定將不會根據提供的資料自動計算。
resetAxisMinValue(): 撤銷之前設定的軸最小值,將自動計算最小值
setStartAtZero(boolean enabled): 已經過時- 使用setAxisMinValue(...) 或者setAxisMaxValue(...) 
setInverted(boolean enabled): 反轉該軸,如果為true,最大值在底部,頂部是最小值。
setSpaceTop(float percent): 設定軸上最高位置在表中最高位置的頂部間距,佔總軸的百分比。
setSpaceBottom(float percent): 設定軸上最低位置在表中最低位置的底部間距,佔總軸的百分比。
setShowOnlyMinMax(boolean enabled):如果啟用,此軸直線式最大值和最小值架構忽略定義的標籤數。
setLabelCount(int count, boolean force): 設定軸的標籤數目,不是精確值,如果強制設定,可能導致軸線不均勻
setPosition(YAxisLabelPosition pos): 設定軸線繪製位置. Either INSIDE_CHART or OUTSIDE_CHART.
setGranularity(float gran):設定Y軸最小間隔
setGranularityEnabled(boolean enabled): 是否啟用Y軸最小間隔
必須在設定資料之前設定屬性才會生效。3、自訂標籤格式setValueFormatter(YAxisValueFormatterf): 在軸上設定一個自訂ValueFormatter。可以格式化原始標籤文本,返回一個定製的文本。更多
4、zeroline除了網格線,在水平方向Y軸的每個值,有所謂的zeroline,這是在0位置軸線上值繪製的,是類似於網格線,但可以單獨配置。

// data has AxisDependency.LEFTYAxis left = mChart.getAxisLeft();left.setDrawLabels(false); // no axis labelsleft.setDrawAxisLine(false); // no axis lineleft.setDrawGridLines(false); // no grid linesleft.setDrawZeroLine(true); // draw a zero linemChart.getAxisRight().setEnabled(false); // no right axis



6、更多執行個體代碼
YAxis yAxis = mChart.getAxisLeft();yAxis.setTypeface(...); // set a different fontyAxis.setTextSize(12f); // set the textsizeyAxis.setAxisMaxValue(100f); // the axis maximum is 100yAxis.setTextColor(Color.BLACK);yAxis.setValueFormatter(new MyValueFormatter());yAxis.setLabelCount(6, true); // force 6 labels//... and more




歡迎掃描二維碼,關注公眾帳號


android開源圖表庫MPAndroidChart文檔翻譯(上)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.