PieShapeItem - 單一餅塊資料
[Serializable]
public class PieShapeItem
{
private double value; public double Value { get; set; }
private string text; public string Text { get; set; }
private string link; public string Link { get; set; }
private Color color = Color.Black; public Color Color { get; set; }
internal float StartAngle = 0, EndAngle = 0; // 由 RefreshState 設定
}
PieShape - 餅圖文件容器
- 繼承
CollectionBase,強型別集合僅接受 PieShapeItem。
- 靜態
StdColors 陣列提供預設配色,Add 方法自動分配顏色。
- 屬性:
Width、Height、Left、Top 定義外接矩形。
- 核心方法
RefreshState() 計算所有項目扇形角度 (見第 6 部分)。
RefreshState 核心邏輯
public void RefreshState()
{
double total = 0;
foreach (PieShapeItem item in this) total += item.Value;
float accum = 0;
for (int i = Count - 1; i >= 0; i--)
{
PieShapeItem item = this[i];
float angle = (float)(360.0 * item.Value / total);
item.StartAngle = (float)Math.Round(accum, 3);
item.EndAngle = (float)Math.Round(accum + angle, 3);
accum += angle;
item.StartAngle = (float)Math.Round(FixAngle(item.StartAngle), 3);
item.EndAngle = (float)Math.Round(FixAngle(item.EndAngle), 3);
}
}
設計優勢
- 資料與繪圖邏輯封裝於文件物件,主頁面僅需填充資料並呼叫
RefreshState。
- 強型別集合確保項目型別安全。
- 角度計算與修正集中管理,易於維護與擴展。