1. 程式人生 > 實用技巧 >C# chart初識

C# chart初識

如果需要檢視更多文章,請微信搜尋公眾號csharp程式設計大全,需要進C#交流群群請加微信z438679770,備註進群, 我邀請你進群! ! !

首先,我用最簡潔的語句做一幅圖:

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


using System.Windows.Forms.DataVisualization.Charting;



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

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int j = 0; j < 100; j++)
                chart1.Series[0].Points.AddXY(j, j);
        }

        private void button1_Click(object sender, EventArgs e)
        {


        }
    }
}

  執行結果:

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


using System.Windows.Forms.DataVisualization.Charting;



namespace WindowsFormsApp13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Queue<double> dataQueue = new Queue<double>(100);
        private void Form1_Load(object sender, EventArgs e)
        {
            /* https://www.cnblogs.com/topmount/p/8430689.html */
            // 定義圖表區域
            this.chart1.ChartAreas.Clear();
            ChartArea chartArea1 = new ChartArea("C1");
            this.chart1.ChartAreas.Add(chartArea1);
            //定義儲存和顯示點的容器
            this.chart1.Series.Clear();
            Series series1 = new Series("S1");
            series1.ChartArea = "C1";
            this.chart1.Series.Add(series1);
            chart1.Series[0].Points.Clear();
            chart1.Series[0].ChartType = SeriesChartType.Spline;//設定圖表型別
            chart1.ChartAreas[0].AxisX.Interval = 5;   //設定X軸座標的間隔為5
            chart1.ChartAreas[0].AxisX.IntervalOffset = 0;  //設定X軸座標偏移為0
            chart1.ChartAreas[0].AxisX.Minimum = 0;//設定X軸最小值
            chart1.ChartAreas[0].AxisX.Maximum = 100;//設定X軸最大值
            chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true; //設定是否交錯顯示,比如資料多的時間分成兩行來顯示 
            chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;//X軸標籤的角度
            chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0%";//Y軸標籤以百分數格式顯示
            chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//不顯示網格線

            chart1.ChartAreas[0].AxisY.Minimum = 0.0;
            chart1.ChartAreas[0].AxisY.Maximum = 1.0;
            chart1.ChartAreas[0].AxisY.Interval = 0.1;
            chart1.Legends[0].Enabled = false;//不顯示圖例
            chart1.ChartAreas[0].BackColor = Color.White;//設定背景為白色
            chart1.ChartAreas[0].Area3DStyle.Enable3D = true;//設定3D效果
            chart1.ChartAreas[0].Area3DStyle.PointDepth = 50;
            chart1.ChartAreas[0].Area3DStyle.PointGapDepth = 50;//設定一下深度,看起來舒服點……
            chart1.ChartAreas[0].Area3DStyle.WallWidth = 0;//設定牆的寬度為0;
            //chart1.Series[0].Label = "#VAL{P}";//設定標籤文字 (在設計期通過屬性視窗編輯更直觀) 標籤變成百分數
            //chart1.Series[0].IsValueShownAsLabel = true;//顯示標籤
            chart1.Series[0].CustomProperties = "DrawingStyle=Cylinder, PointWidth=1";//設定為圓柱形 (在設計期通過屬性視窗編輯更直觀)
            chart1.Series[0].Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.Pastel;//設定調

            this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Red;
            this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Red;
            //設定標題
            this.chart1.Titles.Clear();
            this.chart1.Titles.Add("S01");
            this.chart1.Titles[0].Text = "XXX顯示";
            this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
            this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            //設定圖表顯示樣式
            this.chart1.Series[0].Color = Color.Red;
           

            for (int j = 0; j < 100; j++)
                chart1.Series[0].Points.AddXY(j, j/100.0);
        }

        private void button1_Click(object sender, EventArgs e)
        {


        }
    }
}