android MPAndroidChart餅圖實現圖例後加數字或文本(定製圖例)

來源:互聯網
上載者:User

標籤:

轉載請註明:http://blog.csdn.net/ly20116/article/details/50905789

MPAndroidChart是一個非常優秀的開源圖表庫,MPAndroidChart可以繪製各種常用的圖表類型:折線圖、直條圖、餅圖、散佈圖等等。 
github地址:https://github.com/PhilJay/MPAndroidChart 
具體的匯入方式就不再詳細的說了,本文主要解決在圖例後面加上數字或文本或占的百分比等,也就是定製想要的圖例。 
MPAndroidChart的提供的餅圖圖例是這種: (註:圖片為引用) 
 

而我們想要實現的效果是這種: 

就是在圖例後面加上數字或文本

通過借鑒Stackoverflow上的大神的解決方案: 
https://stackoverflow.com/questions/29139061/mpandroidchart-legend-customization

下面來開始我們的項目: 
一、擷取Legend,使Legend不顯示

Legend legend=mPieChart.getLegend();//設定比例圖legend.setEnabled(false);//圖例不顯示
  • 1
  • 2

二、定義數組colors和labels及資料datas

private int[] colors;//顏色集合private String[] labels;//標籤文本private float[] datas={16912f,2488f,600f};//資料,可以是任何類型的資料,如String,int
  • 1
  • 2
  • 3

三、擷取Legend中的colors和labels

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

四、定義customizeLegend()方法,實現圖例的繪製

/**     * 定製圖例,通過代碼產生布局     */    private void customizeLegend(){        for(int i=0;i<datas.length;i++){            LinearLayout.LayoutParams lp=new LinearLayout.                    LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);            lp.weight=1;//設定比重為1            LinearLayout layout=new LinearLayout(this);//單個圖例的布局            layout.setOrientation(LinearLayout.HORIZONTAL);//水平排列            layout.setGravity(Gravity.CENTER_VERTICAL);//垂直置中            layout.setLayoutParams(lp);            //添加color            LinearLayout.LayoutParams colorLP=new LinearLayout.                    LayoutParams(20,20);            colorLP.setMargins(0, 0, 20, 0);            LinearLayout colorLayout=new LinearLayout(this);            colorLayout.setLayoutParams(colorLP);            colorLayout.setBackgroundColor(colors[i]);            layout.addView(colorLayout);            //添加label            TextView labelTV=new TextView(this);            labelTV.setText(labels[i]+" ");            layout.addView(labelTV);            //添加data            TextView dataTV=new TextView(this);            dataTV.setText(datas[i]+"");            layout.addView(dataTV);            legendLayout.addView(layout);//legendLayout為外層布局即整個圖例布局,是在xml檔案中定義        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

圖例: 

customizeLegend()方法的調用可在設定圖例不顯示的後面,也可以在其它地方調用,但是必須在PieChart調用setData()方法的後面,這樣才能擷取到colors和labels.

總結: 
簡而言之,就是擷取legend的顏色colors和標籤文本labels,然後結合自己的資料,在新的布局中繪製即可。 
你可以在圖例後面添加更多的類型的資料。 
圖例布局的位置可以在xml檔案中設定。 
也可以實現各種布局的圖例

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.