首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

ZedGraph 教學(八):PieSampleDemo 圓餅圖完整解析(一)

     編輯:關於C語言

圓餅圖範例概覽

PieSampleDemo 展示如何建立圓餅圖、設定扇區顏色/標籤/突出顯示、配置圖例位置、加入總計文字與陰影效果。

執行畫面

PieSampleDemo 執行結果:多扇區圓餅圖、浮動圖例、總計標籤與陰影

完整程式碼

using System;
using System.Drawing;
using System.Collections;
using ZedGraph;

namespace ZedGraph.Demo
{
    public class PieSampleDemo : DemoBase
    {
        public PieSampleDemo() : base("Code Project Pie Sample", "Pie Sample", DemoType.Pie, DemoType.Tutorial)
        {
            GraphPane myPane = base.GraphPane;

            // 1. 圖表標題樣式
            myPane.Title.Text = "2004 ZedGraph Sales by Region\n($M)";
            myPane.Title.FontSpec.IsItalic = true;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "Times";

            // 2. 窗格背景漸層
            myPane.PaneFill = new Fill(Color.White, Color.Goldenrod, 45.0f);

            // 3. 隱藏座標軸背景
            myPane.AxisFill.Type = FillType.None;

            // 4. 圖例浮動定位
            myPane.Legend.Position = LegendPos.Float;
            myPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
            myPane.Legend.FontSpec.Size = 10f;
            myPane.Legend.IsHStack = false; // 垂直排列

            // 5. 新增扇區
            PieItem segment1 = myPane.AddPieSlice(20, Color.Navy, Color.White, 45f, 0, "North");
            PieItem segment3 = myPane.AddPieSlice(30, Color.Purple, Color.White, 45f, 0, "East");
            PieItem segment4 = myPane.AddPieSlice(10.21, Color.LimeGreen, Color.White, 45f, 0, "West");
            PieItem segment2 = myPane.AddPieSlice(40, Color.SandyBrown, Color.White, 45f, 0.2, "South"); // 0.2 突出顯示
            PieItem segment6 = myPane.AddPieSlice(250, Color.Red, Color.White, 45f, 0, "Europe");
            PieItem segment7 = myPane.AddPieSlice(50, Color.Blue, Color.White, 45f, 0.2, "Pac Rim");
            PieItem segment8 = myPane.AddPieSlice(400, Color.Green, Color.White, 45f, 0, "South America");
            PieItem segment9 = myPane.AddPieSlice(50, Color.Yellow, Color.White, 45f, 0.2, "Africa");

            // 6. 調整特定扇區標籤顏色
            segment2.LabelDetail.FontSpec.FontColor = Color.Red;

            // 7. 計算總計並建立文字標註
            CurveList curves = myPane.CurveList;
            double total = 0;
            for (int x = 0; x < curves.Count; x++)
                total += ((PieItem)curves[x]).Value;

            TextItem text = new TextItem("Total 2004 Sales\n$" + total.ToString() + "M", 0.18F, 0.40F, CoordType.PaneFraction);
            text.Location.AlignH = AlignH.Center;
            text.Location.AlignV = AlignV.Bottom;
            text.FontSpec.Border.IsVisible = false;
            text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
            text.FontSpec.StringAlignment = StringAlignment.Center;
            myPane.GraphItemList.Add(text);

            // 8. 文字陰影 (複製並偏移)
            TextItem text2 = new TextItem(text);
            text2.FontSpec.Fill = new Fill(Color.Black);
            text2.Location.X += 0.008f;
            text2.Location.Y += 0.01f;
            myPane.GraphItemList.Add(text2);

            base.ZedGraphControl.AxisChange();
        }
    }
}

核心 API 解析

AddPieSlice

參數:數值、填充顏色、標籤顏色、傾斜角度、爆炸距離 (0~1)、圖例標籤。回傳 PieItem 可微調標籤細節。

圖例浮動定位

LegendPos.Float 搭配 Location(CoordType.PaneFraction) 以相對座標 (0~1) 精確放置圖例。

GraphItemList:疊加文字與陰影

利用 TextItem 繪製總計標籤,再複製一份設定黑色填充並微調座標產生陰影效果,提升可讀性。

DemoType.Pie

建構式傳入 DemoType.Pie 讓 ZedGraphControl 以圓餅圖模式呈現 (隱藏座標軸)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved