1. 程式人生 > >MPAndroidchart 教程:圖例 Legend(七)

MPAndroidchart 教程:圖例 Legend(七)

一、概述

本章將重點介紹各個圖表型別特定的設定。

預設情況下,所有的圖表型別都支援 Legend 且在設定圖表資料後會自動生成 Legend
Legend 通常由一個標籤的 形式/形狀 來表示多個條目( entries ) 的每一個。

entries 數量自動生成的 legend 取決於DataSet 的標籤 不同顏色的數量(在所有 DataSet 的物件)。 Legend 的標籤取決於圖表中所使用的 DataSet 物件。 如果沒有為 DataSet 物件指定標籤,圖表將自動生成它們。 如果多個顏色用於一個 DataSet ,這些顏色分類 ,只通過一個標籤說明。

對於定製的 Legend

,可以通過圖表物件的 getLegend() 方法先獲取 Legen 在進行呼叫對應方法:

    Legend legend = chart.getLegend();

二、是否繪製 Legend

  • setEnabled(boolean enabled) : 設定Legend啟用或禁用。 如果禁用, Legend 將不會被繪製。

三、修改 Legend

  • setTextColor(int color) : 設定圖例標籤的顏色。
  • setTextSize(float size) : 設定在DP傳說標籤的文字大小。
    單位是”畫素” min = 6f, max = 24f, default 10f

  • setTypeface(Typeface tf) : 設定自定義Typeface圖例標籤。

四、Wrapping / clipping avoidance

  • setWordWrapEnabled(boolean enabled) : 如果啟用,Legend 的內容將不會超出圖表邊界之外,而是建立一個新的行。 請注意,這會降低效能和僅適用於”legend 位於圖表下面”的情況。
  • setMaxSizePercent(float maxSize) : 設定最大圖例的百分比(相對整個圖表大小)。 預設值:0.95f(95%)

五、自定義 Legend

  • setPosition(LegendPosition pos)

    : 通過 LegendPosition 設定 Legend 出現的位置。

    • RIGHT_OF_CHART
    • RIGHT_OF_CHART_CENTER
    • RIGHT_OF_CHART_INSIDE
    • BELOW_CHART_LEFT
    • BELOW_CHART_RIGHT
    • BELOW_CHART_CENTER
    • PIECHART_CENTERPieChart獨有)等等。


  • setFormSize(float size) : 設定 legend-forms 的大小,單位dp。

  • setForm(LegendForm shape) : 設定 LegendForm 。This is the shape that is drawn next to the legend-labels with the color of the DataSet the legend-entry represents. 正方形,圓形或線。
        legend.setFormSize(18f);
        legend.setForm(Legend.LegendForm.SQUARE);
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setForm(Legend.LegendForm.LINE);
  • setXEntrySpace(float space) : 設定在水平軸上 legend-entries 的間隙。
  • setYEntrySpace(float space) : 設定在垂直軸上 legend-entries 的間隙。
  • setFormToTextSpace(float space) : 設定 legend-formlegend-label 之間的空間。
  • setWordWrapEnabled(boolean enabled) : 設定 Legend 是否自動換行? 目前僅支援BelowChartLeftBelowChartRightBelowChartCenter。 / you may want to set maxSizePercent when word wrapping, to set the point where the text wraps.

六、設定自定義標籤和顏色

  • setCustom(int[] colors, String[] labels) : 設定 自定義Legend 的標籤和顏色。 顏色數應該匹配標籤數,並且相對應。A null label will start a group. A (-2) color will avoid drawing a form This will disable the feature that automatically calculates the legend labels and colors from the datasets. 呼叫 resetCustom() 重新啟用自動計算(and then notifyDataSetChanged() is needed to auto-calculate the legend again)。
  • resetCustom() : 呼叫此將禁用通過 setCustom(...)方法自定義的圖例標籤。在 notifyDataSetChanged() 被呼叫後,標籤將再次被自動計算。
  • setExtra(int[] colors, String[] labels) : Sets colors and labels that will be appended to the end of the auto calculated colors and labels arrays after calculating the legend. (if the legend has already been calculated, you will need to call notifyDataSetChanged() to let the changes take effect)

七、程式碼範例

    Legend l = chart.getLegend();
    l.setFormSize(10f); // set the size of the legend forms/shapes
    l.setForm(LegendForm.CIRCLE); // set what type of form/shape should be used
    l.setPosition(LegendPosition.BELOW_CHART_LEFT);
    l.setTypeface(...);
    l.setTextSize(12f);
    l.setTextColor(Color.BLACK);
    l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
    l.setYEntrySpace(5f); // set the space between the legend entries on the y-axis

    // set custom labels and colors
    l.setCustom(ColorTemplate.VORDIPLOM_COLORS, 
            new String[] { "Set1", "Set2", "Set3", "Set4", "Set5" });

    // and many more...