編輯:關於 Android 程式設計
第一次在圖表作品集裡看到南丁格爾玫瑰圖時,我的反應是「這也太漂亮了」,但老實說完全沒看懂在表達什麼。查了資料才知道,這張圖的背後是弗羅倫斯・南丁格爾——現代護理的奠基者,她用這種圖形把戰地醫院的月度死因統計攤在決策者面前,讓冰冷的數字變成一眼就懂的視覺衝擊。難怪它又被叫作「極座標面積圖」或「玫瑰圖」。
仔細拆解它的構成之後,原理其實相當單純:把柱形圖的柱條彎成扇形,用扇形的半徑長度來編碼數值大小,再依角度把各扇區均分排列。變種當然很多,這裡說的只是我理解的這一種。
一旦想通這層對應關係,用 Canvas 實作就水到渠成:傳入的資料筆數決定每個扇形的角度(360 除以筆數),百分比決定扇形的半徑,剩下的就是畫弧線與填色的例行工作。

整個自訂 View 的骨架如下:
package com.xcl.chart;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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);
DisplayMetrics dm = getResources().getDisplayMetrics();
ScrHeight = dm.heightPixels;
ScrWidth = dm.widthPixels;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
float cirX = ScrWidth / 2f;
float cirY = ScrHeight / 3f;
float radius = ScrHeight / 5f;
// 計算外接正方形,供 drawArc 使用
RectF arcRF = new RectF(
cirX - radius, cirY - radius,
cirX + radius, cirY + radius);
Paint paintArc = new Paint();
paintArc.setAntiAlias(true);
paintArc.setStyle(Paint.Style.FILL);
Paint paintLabel = new Paint();
paintLabel.setColor(Color.WHITE);
paintLabel.setTextSize(16);
paintLabel.setAntiAlias(true);
// 每個扇區佔據的角度
float percentage = 360f / arrPer.length;
for (int i = 0; i < arrPer.length; i++) {
float startAngle = percentage * i;
// 半徑依數值縮放:數值越大,扇形伸得越遠
float scale = arrPer[i] / 100f;
RectF current = new RectF(
cirX - radius * scale, cirY - radius * scale,
cirX + radius * scale, cirY + radius * scale);
paintArc.setColor(Color.rgb(
arrColorRgb[i][0], arrColorRgb[i][1], arrColorRgb[i][2]));
canvas.drawArc(current, startAngle, percentage, true, paintArc);
// 沿著扇形中線擺放標籤
float midRad = (float) Math.toRadians(startAngle + percentage / 2);
float labelX = cirX + (float) Math.cos(midRad) * (radius * scale + 20);
float labelY = cirY + (float) Math.sin(midRad) * (radius * scale + 20);
canvas.drawText(arrPerLabel[i], labelX, labelY, paintLabel);
}
}
}
360 / arrPer.length,若資料筆數除不盡會有微小誤差,正式專案建議先四捨五入到小數兩位再使用。setTextAlign(CENTER) 或用 measureText 做微調,避免文字偏移。程式碼寫起來其實不難,但這張圖的設計創意實在令人 setState歎服——兩百年前的人就能想到用極座標來重新組織統計資訊。
有興趣的可以看看維基百科上的介紹:
https://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
這個系列的其他篇章:
MAIL: xcl_168@aliyum.com
BLOG: https://blog.csdn.net/xcl168
Android 簡易實戰教程——第六話《開發一鍵鎖屏應用(完成篇)》
上一篇留下兩個尾巴:一是仍然無法卸載;二是必須手動點開 App 再點按鈕才能鎖屏,操作太繞。 這篇把兩個問題都解決掉。 一、解決無法卸載的問題 在清單文件中,再配置一個「
在 PagerSlidingTabStrip 的標題旁加上小圖示
PagerSlidingTabStrip 是 ViewPager 的經典配套元件,提供可滑動的頁籤列。原生版本只支援文字標題,但實務上常需要在標題旁邊加個小圖示來強化辨識
Android 聯絡人按字母索引快速查找實作
效果目標聯絡人清單若有大量資料,通常需要字母索引來加速查找。這個範例會在清單右側顯示 A 到 Z 的索引,當使用者滑動或點擊索引時,ListView 會快速跳到對應位置,
Android AsyncTask 原理剖析與 Java 多線程探索
一、Java 線程 Thread 在 Java 中最常見的起線程方式,就是 new Thread 然後覆寫 run 方法。新線程執行的程式碼就是 run 函式。 new