Android Mpandroidchart pie chart implementation legend plus number or text (custom legend)

Source: Internet
Author: User

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);//图例不显示
    • 1
    • 2

Second, define the array colors and labels and data datas

private int[] colors;//颜色集合private String[] labels;//标签文本private float[] datas={16912f,2488f,600f};//数据,可以是任何类型的数据,如String,int
    • 1
    • 2
    • 3

Iii. acquisition of colors and labels in legend

colors=legend.getColors();labels=legend.getLabels();
    • 1
    • 2

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.