Reprint Please specify: http://blog.csdn.net/ly20116/article/details/50905789
Mpandroidchart is a very good open Source Chart library, Mpandroidchart can draw a variety of commonly used chart types: Line chart, column chart, pie chart, scatter chart and so on.
GitHub Address: Https://github.com/PhilJay/MPAndroidChart
The specific import method is no longer in detail, this article mainly solves the legend after adding numbers or text or percentage, which is the custom desired legend .
Mpandroidchart's provided pie chart legend is this: (Note: Picture is a reference)
And the effect we want to achieve is this:
is to add numbers or text after the legend
By drawing on the solution of the Great God on StackOverflow:
Https://stackoverflow.com/questions/29139061/mpandroidchart-legend-customization
Here's how to start our project:
First, get legend, make legend not show
Legend legend=mPieChart.getLegend();//设置比例图legend.setEnabled(false);//图例不显示
Second, define the array colors and labels and data datas
private int[] colors;//颜色集合private String[] labels;//标签文本private float[] datas={16912f,2488f,600f};//数据,可以是任何类型的数据,如String,int
Iii. acquisition of colors and labels in legend
colors=legend.getColors();labels=legend.getLabels();
Iv. define the Customizelegend () method to achieve the drawing of the legend
/** * Custom legends, generate layouts by code */PrivatevoidCustomizelegend () {Forint i=0;i<datas.length;i++) {Linearlayout.layoutparams lp=New LinearLayout. Layoutparams (layoutparams.match_parent,layoutparams.match_parent); lp.weight=1;Set gravity to 1 LinearLayout layout=New LinearLayout (this);The layout of a single legend Layout.setorientation (Linearlayout.horizontal);Horizontally arranged layout.setgravity (gravity.center_vertical);Vertical Center LAYOUT.SETLAYOUTPARAMS (LP);Add a color Linearlayout.layoutparams colorlp=New LinearLayout. Layoutparams (00n20); Colorlp.setmargins (0, 0, 20, 0); LinearLayout colorlayout=new linearlayout (this); Colorlayout.setlayoutparams (COLORLP); Colorlayout.setbackgroundcolor (Colors[i]); Layout.addview (colorlayout); //add label TextView labeltv=new TextView ( this); Labeltv.settext (Labels[i]+//Add data TextView datatv=new TextView ( this); Datatv.settext (Datas[i]+//legendlayout for the outer layout that is the entire legend layout, defined in the XML file}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
Legend:
The call to the Customizelegend () method can be called after the legend is not displayed, but it is also available elsewhere, but must be followed by the Piechart call to the SetData () method to obtain colors and labels.
Summary:
In short, you get legend color colors and label text labels, and then combine your own data to draw in a new layout .
You can add more types of data after the legend.
the location of the legend layout can be set in the XML file.
You can also implement legends for various layouts .
Android Mpandroidchart pie chart implementation legend plus number or text (custom legend)