c#使用System.Windows.Forms.DataVisualization.Charting.dll繪製圖表實例

首先下載System.Windows.Forms.DataVisualization.Charting.dll,然後引用到項目中

手動在代碼中創建chart類型並將其添加到某個控件中(control.controls.add(chart)),然後參數初始化添加樣式和數據就可以了。

以下是效果圖和代碼

 

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 System.Windows.Forms.DataVisualization.Charting;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            InitChart();

            for (int i = 0; i < 3; i++)
            {
                AddSeries(r.Next(1,100).ToString(), Color.Red);
            }
        }
        static int range = 0;
        Random r = new Random(6);

        private void AddSeries(string seriersName, Color serierscolor)
        {
            Series series = new Series(seriersName);
            //圖表類型  設置爲樣條圖曲線
            series.ChartType = SeriesChartType.Line;
            series.IsXValueIndexed = true;
            series.XValueType = ChartValueType.Time;
            series.MarkerStyle = MarkerStyle.Circle;
            series.MarkerColor = Color.Black;
            //設置點的大小
            series.MarkerSize = 5;
            //設置曲線的顏色
            series.Color = serierscolor;
            //設置曲線寬度
            series.BorderWidth = 2;
            series.CustomProperties = "PointWidth=2";
            series.IsValueShownAsLabel = true;
            chart.Series.Add(series);
        }


        private void CreateChart()
        {
            chart = new Chart();
            this.panel1.Controls.Add(chart);
            chart.Dock = DockStyle.Fill;
            chart.Visible = true;

            ChartArea chartArea = new ChartArea();
            //chartArea.Name = "FirstArea";

            chartArea.CursorX.IsUserEnabled = true;
            chartArea.CursorX.IsUserSelectionEnabled = true;
            chartArea.CursorX.SelectionColor = Color.SkyBlue;
            chartArea.CursorY.IsUserEnabled = true;
            chartArea.CursorY.AutoScroll = true;
            chartArea.CursorY.IsUserSelectionEnabled = true;
            chartArea.CursorY.SelectionColor = Color.SkyBlue;

            chartArea.CursorX.IntervalType = DateTimeIntervalType.Auto;
            chartArea.AxisX.ScaleView.Zoomable = false;
            chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;//啓用X軸滾動條按鈕

            chartArea.BackColor = Color.BlueViolet;                      //背景色
            chartArea.BackSecondaryColor = Color.White;                 //漸變背景色
            chartArea.BackGradientStyle = GradientStyle.TopBottom;      //漸變方式
            chartArea.BackHatchStyle = ChartHatchStyle.None;            //背景陰影
            chartArea.BorderDashStyle = ChartDashStyle.NotSet;          //邊框線樣式
            chartArea.BorderWidth = 1;                                  //邊框寬度
            chartArea.BorderColor = Color.Black;

            //chartArea.AxisX.
            chartArea.AxisX.MajorGrid.Enabled = true;
            chartArea.AxisY.MajorGrid.Enabled = true;

            // Axis
            chartArea.AxisY.Title = @"Value";
            chartArea.AxisY.LineWidth = 2;
            chartArea.AxisY.LineColor = Color.Black;
            chartArea.AxisY.Enabled = AxisEnabled.True;

            chartArea.AxisX.Title = @"Time";
            chartArea.AxisX.IsLabelAutoFit = true;
            chartArea.AxisX.LabelAutoFitMinFontSize = 5;
            chartArea.AxisX.LabelStyle.Angle = -15;

            chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;        //show the last label
            chartArea.AxisX.Interval = 10;
            chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
            chartArea.AxisX.IntervalType = DateTimeIntervalType.NotSet;
            chartArea.AxisX.TextOrientation = TextOrientation.Auto;
            chartArea.AxisX.LineWidth = 2;
            chartArea.AxisX.LineColor = Color.Black;
            chartArea.AxisX.Enabled = AxisEnabled.True;
            chartArea.AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Months;
            chartArea.AxisX.Crossing = 0;

            chartArea.Position.Height = 85;
            chartArea.Position.Width = 85;
            chartArea.Position.X = 0;
            chartArea.Position.Y = 13;

            chart.ChartAreas.Add(chartArea);
            chart.BackGradientStyle = GradientStyle.TopBottom;
            //圖表的邊框顏色、
            chart.BorderlineColor = Color.FromArgb(26, 59, 105);
            //圖表的邊框線條樣式
            chart.BorderlineDashStyle = ChartDashStyle.Solid;
            //圖表邊框線條的寬度
            chart.BorderlineWidth = 2;
            //圖表邊框的皮膚
            chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
        }
        bool flag = false;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "Zoom")
            {
                flag = true;
            }
            else if (comboBox1.SelectedItem.ToString() == "OverView" || comboBox1.SelectedItem.ToString() == "Follow")
            {
                comboBox1.Items.Remove("Zoom");
                flag = false;
            }
        }

        private void button_Stop_Click(object sender, EventArgs e)
        {
            switch (button_Stop.Text)
            {
                case "Stop":
                    {
                        button_Stop.Text = "Start";
                        t.Stop();
                        break;
                    }
                case "Start":
                    {
                        button_Stop.Text = "Stop";
                        t.Start();
                        break;
                    }
            }
        }

        private void InitChart()
        {
            CreateChart();
            chart.ChartAreas[0].AxisX.ScaleView.Scroll(ScrollType.Last);
            t.Interval = 100;
            t.Start();
        }

        int sum = 0;
        private void t_Tick(object sender, EventArgs e)
        {
            sum++;
            Random ra = new Random();
            DateTime nowTime = DateTime.Now;
            if (chart.Series.Count <= 0)
            {
                return;
            }
            for (int i = 0; i < chart.Series.Count; i++)
            {
                
                Series series = chart.Series[i];
                string value = ra.Next(1, 10).ToString();
                series.Points.AddXY(nowTime, value);
                if (comboBox1.SelectedItem.ToString() == "OverView")
                {
                    chart.ChartAreas[0].AxisX.ScaleView.Position = 1;
                    if (sum > 10)
                    {
                        double max = chart.ChartAreas[0].AxisX.Maximum;
                        max = (sum / 10 + 1) * 10;
                        chart.ChartAreas[0].AxisX.Interval = max / 10;
                    }
                    chart.ChartAreas[0].AxisX.ScaleView.Size = sum * 1.1;
                }
                if (comboBox1.SelectedItem.ToString() == "Follow")
                {
                    chart.ChartAreas[0].AxisX.Interval = 1D;
                    chart.ChartAreas[0].AxisX.ScaleView.Size = 10D;
                    if (sum <= chart.ChartAreas[0].AxisX.ScaleView.Size)
                        chart.ChartAreas[0].AxisX.ScaleView.Position = 1;
                    else
                        chart.ChartAreas[0].AxisX.ScaleView.Position = sum - chart.ChartAreas[0].AxisX.ScaleView.Size;
                }
                
            }
        }
    }
}
 

//設計器

namespace test_chart
{
    partial class Form1
    {
        /// <summary>
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放託管資源,爲 true;否則爲 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.button_Stop = new System.Windows.Forms.Button();
            this.t = new System.Windows.Forms.Timer(this.components);
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "Follow",
            "OverView"});
            this.comboBox1.Location = new System.Drawing.Point(840, 429);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 20);
            this.comboBox1.TabIndex = 1;
            this.comboBox1.Text = "Follow";
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // button_Stop
            // 
            this.button_Stop.Location = new System.Drawing.Point(12, 429);
            this.button_Stop.Name = "button_Stop";
            this.button_Stop.Size = new System.Drawing.Size(75, 23);
            this.button_Stop.TabIndex = 2;
            this.button_Stop.Text = "Stop";
            this.button_Stop.UseVisualStyleBackColor = true;
            this.button_Stop.Click += new System.EventHandler(this.button_Stop_Click);
            // 
            // t
            // 
            this.t.Tick += new System.EventHandler(this.t_Tick);
            // 
            // panel1
            // 
            this.panel1.Location = new System.Drawing.Point(12, 13);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(906, 410);
            this.panel1.TabIndex = 3;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(973, 461);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button_Stop);
            this.Controls.Add(this.comboBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart chart;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.Button button_Stop;
        private System.Windows.Forms.Timer t;
        private System.Windows.Forms.Panel panel1;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章