Android中使用Canvas繪製南丁格爾玫瑰圖(Nightingale rose diagram)_Android

來源:互聯網
上載者:User

南丁格爾玫瑰圖 在常規圖表中實在很驚豔,但我初看沒看懂,一查原來南丁格爾這麼偉大,確實值得尊敬。

再仔細研究了下這種圖的構成,發現原來就是把直條圖的柱形換成了扇形圖的半徑來表示,當然,變種有好多,我這隻是說我理解的這種。

知道了其構成方式後就好實現了,依傳入參數個數決定其扇形角度,依百分比決定其扇形的半徑長度,然後就一切都水到渠成了。

漂亮的美圖獻上:

附上實現代碼:

package com.xcl.chart;/** * Canvas練習  *  自已畫南丁格爾玫瑰圖(Nightingale rose diagram) *    * author:xiongchuanliang * date:2014-4-12 */import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.graphics.RectF;import android.util.DisplayMetrics;import android.view.View;public class PanelRoseChart extends View{private int ScrWidth,ScrHeight; //示範用的百分比例,實際使用中,即為外部傳入的比例參數 private final float arrPer[] = new float[]{40f,50f,60f,35f,70f,80f,90f}; //示範用標籤private final String arrPerLabel[] = new String[]{"PostgreSQL","Sybase","DB2","國產及其它","MySQL","Ms Sql","Oracle"}; //RGB顏色數組private final int arrColorRgb[][] = { {77, 83, 97},        {148, 159, 181},        {253, 180, 90},       {52, 194, 188},       {39, 51, 72},       {255, 135, 195},       {215, 124, 124}} ;public PanelRoseChart(Context context) {super(context);// TODO Auto-generated constructor stub//螢幕資訊DisplayMetrics dm = getResources().getDisplayMetrics();ScrHeight = dm.heightPixels;ScrWidth = dm.widthPixels;}public void onDraw(Canvas canvas){//畫布背景canvas.drawColor(Color.BLACK);              float cirX = ScrWidth / 2;     float cirY = ScrHeight / 3 ;     float radius = ScrHeight / 5 ;//150;                      float arcLeft = cirX - radius;     float arcTop = cirY - radius ;     float arcRight = cirX + radius ;     float arcBottom = cirY + radius ;     RectF arcRF0 = new RectF(arcLeft ,arcTop,arcRight,arcBottom);          //畫筆初始化Paint PaintArc = new Paint(); Paint PaintLabel = new Paint(); PaintLabel.setColor(Color.WHITE);PaintLabel.setTextSize(16);     PaintLabel.setAntiAlias(true);PaintArc.setAntiAlias(true);    //位置計算類     XChartCalc xcalc = new XChartCalc();    float Percentage = 0.0f; float CurrPer = 0.0f; float NewRaidus = 0.0f;int i= 0;  //將百分比轉換為扇形半徑長度Percentage = 360 / arrPer.length;Percentage = (float)(Math.round(Percentage *100))/100;     for(i=0; i<arrPer.length; i++)      {       //將百分比轉換為新扇區的半徑       NewRaidus = radius * (arrPer[i]/ 100);       NewRaidus = (float)(Math.round(NewRaidus *100))/100;             float NewarcLeft = cirX - NewRaidus;       float NewarcTop = cirY - NewRaidus ;       float NewarcRight = cirX + NewRaidus ;       float NewarcBottom = cirY + NewRaidus ;       RectF NewarcRF = new RectF(NewarcLeft ,NewarcTop,NewarcRight,NewarcBottom);                    //分配顏色            PaintArc.setARGB(255,arrColorRgb[i][0], arrColorRgb[i][1], arrColorRgb[i][2]);              //在餅圖中顯示所佔比例       canvas.drawArc(NewarcRF, CurrPer, Percentage, true, PaintArc);               //計算百分比標籤      xcalc.CalcArcEndPointXY(cirX, cirY, radius - radius/2/2, CurrPer + Percentage/2); //標識  canvas.drawText(arrPerLabel[i],xcalc.getPosX(), xcalc.getPosY() ,PaintLabel);        //下次的起始角度       CurrPer += Percentage;     }     //外環    PaintLabel.setStyle(Style.STROKE);    PaintLabel.setColor(Color.GREEN);    canvas.drawCircle(cirX,cirY,radius,PaintLabel);     canvas.drawText("author:xiongchuanliang", 10, ScrHeight - 200, PaintLabel);    }}

代碼實現起來很容易,但這種圖的設計創意確實非常好。 歎服。

一定要附上南丁格爾維基百科的連結:      http://zh.wikipedia.org/wiki/%E5%BC%97%E7%BE%85%E5%80%AB%E6%96%AF%C2%B7%E5%8D%97%E4%B8%81%E6%A0%BC%E7%88%BE

感興趣的可以看看。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.