13.Legend(MPAndroidChart中文翻譯)

目錄

第8節.Setting Colors(MPAndroidChart中文翻譯)
第9節.Formatting Data Values (ValueFormatter)(MPAndroidChart中文翻譯)
第10節-Formatting Axis Values (AxisValueFormatter)(MPAndroidChart中文翻譯)
第11節.General Settings & Styling(MPAndroidChart中文翻譯)
第12節.Specific Settings & Styling(MPAndroidChart中文翻譯)
第13節.Legend(MPAndroidChart中文翻譯)
第14節.Dynamic & Realtime Data(MPAndroidChart中文翻譯)
第15節. Modifying the Viewport(MPAndroidChart中文翻譯)
第16節.Animations(MPAndroidChart中文翻譯)
第17節. MarkerView (Popup View)(MPAndroidChart中文翻譯)
第18節. The ChartData class(MPAndroidChart中文翻譯)
第19節. ChartData subclasses(MPAndroidChart中文翻譯)
第20節. The DataSet class (general DataSet styling)(MPAndroidChart中文翻譯)
第21節. DataSet subclasses (specific DataSet styling)(MPAndroidChart中文翻譯)
第22節. The ViewPortHandler(MPAndroidChart中文翻譯)
第23節. Customizing the Fill-Line-Position (FillFormatter)(MPAndroidChart中文翻譯)
第24節. Proguard(MPAndroidChart中文翻譯)
第25節. Realm.io mobile database(MPAndroidChart中文翻譯)
第26節. Creating your own (custom) DataSets(MPAndroidChart中文翻譯)
第27節. Miscellaneous (more useful stuff)(MPAndroidChart中文翻譯)

默認情況下,所有類型的圖表都支持圖例,并在設置數據后自動為圖表生成并繪制圖例.圖例通常由多個條目組成,每個條目由一個標簽和一個表格/形狀組成.

自動生成的圖例包含的條目數量取決于不同的顏色(通過所有DataSet對象)和DataSet的標簽數.圖例中的標簽取決于圖表中DataSet對象設置的標簽.如果沒有為DataSet對象指定標簽,圖表將自動生成他們.如果一個DataSet對象使用了很多顏色,這些顏色是一個組并且僅用一個標簽類描述.

為了自定義圖例,你可以用getLegend()方法獲取圖例對象:

Legend legend = chart.getLegend();

Control if the legend should be drawn(控制圖例是否顯示)

  • setEnabled(boolean enabled):設置圖例是否顯示,如果禁用,圖例將不會繪制.

Styling / modifying the legend(圖例的樣式和模式)

  • setTextColor(int color):設置圖例標簽的顏色.
  • setTextSize(float size):設置圖例標簽的字體大小,單位dp.
  • setTypeface(Typeface tf): 給圖例標簽設置自定義字體.

Wrapping / clipping avoidance(包裹內容/避免裁剪)

  • setWordWrapEnabled(boolean enabled):如果啟用,圖例內容將不會超出圖表的邊框,而是創建新的一行.注意,這會降低性能,并且僅用于圖表下方的圖例.
    setMaxSizePercent(float maxSize): 用百分比形式設置相對于整個圖表的最大尺寸.默認值為0.95f(95%)

Customizing the legend(自定義圖例)

  • setPosition(LegendPosition pos):設置圖例的位置,它表示圖例出現的位置. 在他們之中選擇:
RIGHT_OF_CHART, 
RIGHT_OF_CHART_CENTER, 
RIGHT_OF_CHART_INSIDE, 
BELOW_CHART_LEFT, 
BELOW_CHART_RIGHT, 
BELOW_CHART_CENTER or 
PIECHART_CENTER (PieChart only), 
... 等.
  • setForm(LegendForm shape): 設置圖例表單形狀,這個形狀使用DataSet中設置的圖例顏色,被繪制在圖例標簽旁邊.從他們之中選擇:
SQUARE, 
CIRCLE or 
LINE.
  • setFormSize(float size): 設置圖例表單大小,單位dp.
  • setXEntrySpace(float space):設置x軸方向圖例條目之間的距離.
  • setYEntrySpace(float space):設置y軸方向圖例條目之間的距離.
  • setFormToTextSpace(float space):設置圖例標簽和相應的表單之間的空隙.
  • setWordWrapEnabled(boolean enabled):是否設置包裹圖例內容?/目前僅支持BelowChartLeft, BelowChartRight, BelowChartCenter./為了設置文本換行的位置,設置當文本換行時的最大尺寸百分比.

Setting custom labels & colors(設置自定義便簽和顏色)

  • setCustom(int[] colors, String[] labels):設置自定義圖例的標簽和顏色數組.顏色的數量應該和便簽的數量一致.每個表單都會繪制相同索引處的顏色.空標簽將會開啟一個組.A(-2)顏色將避免繪制表單,這將會禁用從dataSets中自動計算圖例標簽和顏色功能.調用resetCustom()方法重新開啟自動計算功能(需要調用notifyDataSetChanged()方法來自動計算圖例)
  • resetCustom(): 調用他將禁止自定義圖例標簽(有setCustom()方法設置的).
  • setExtra(int[] colors, String[] labels): 設置額外的顏色和標簽,將會在圖例自動計算完畢后添加到顏色和標簽集合末尾.(如果圖例已經計算完畢,你需要調用notifyDataSetChanged()方法來使更改生效)

Example(舉個栗子)

   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...
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容