2013/01/31

C#.Net使用zedgraph套件長條圖

這篇參考至[C#] 使用 ZedGraph Chart 來製作圖表(開源控件),Code大略改一些而已








using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace Chart
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GraphPane myPane = null;
            myPane = zedGraphControl1.GraphPane;
            myPane.CurveList.Clear();
            myPane.GraphObjList.Clear();

            //資料來源 處理
            int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] y = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

            //資料填充
            PointPairList list = new PointPairList();

            //清除清單
            list.Clear();
            for (int i = 0; i < x.Length; i++)
            {
                list.Add(x[i], y[i]);
            }

            //建立長條圖
            BarItem myCurve = myPane.AddBar("測試用標題", list, Color.Blue);

            //每個長條圖 都顯示個別數據
            BarItem.CreateBarLabels(myPane, false, "f0");

            //介面屬性處理
            myPane.Title.IsVisible = false;

            //X 軸 Title 文字Size
            myPane.XAxis.Title.FontSpec.Size = 10;
            string[] xLabel = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

            //X軸刻度資料來源
            myPane.XAxis.Scale.TextLabels = xLabel;

            //X軸刻度資料型態
            myPane.XAxis.Type = AxisType.Text;

            //X軸字型大小
            myPane.XAxis.Scale.FontSpec.Size = 16;

            //Y 軸的 Title
            myPane.YAxis.Title.Text = "Y軸名稱";

            //Y軸Label大小
            myPane.YAxis.Title.FontSpec.Size = 16;

            //Y軸字型大小
            myPane.YAxis.Scale.FontSpec.Size = 10;
            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MajorGrid.Color = Color.Black;

            //Chart 填色
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, -45F);

            //外部 填色
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), -45F);

            //圖例放置位置
            myPane.Legend.Position = ZedGraph.LegendPos.InsideTopRight;
            myPane.Legend.FontSpec.Size = 10;

            //畫面產生
            zedGraphControl1.AxisChange();

            //重要! 若資料有變更時 畫面重整
            zedGraphControl1.RestoreScale(myPane);
        }
    }
}

參考資料:
http://kuomingwang.blogspot.tw/2011/12/c-zedgraph-chart.html