using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 數據源至界面 chart1.Series[0].Points.DataBindXY(listX, listY); chart1.Series[0].Points.DataBindY(listY); // 去除數據綁定 chart1.Series.Clear(); // X軸不從0開始 chart1.ChartAreas[0].AxisX.IsStartedFromZero = false; // X軸允許子柵格 chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = true; // 設置游標的可偏移量 chart1.ChartAreas[0].CursorX.Interval = 0.01D; chart1.ChartAreas[0].CursorX.IntervalOffset = 0.01D; // 游標可用、可選 chart1.ChartAreas[0].CursorX.IsUserEnabled = true; chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true; // 填充 chart1.Dock = DockStyle.Fill; // 線的Y軸依附於次Y軸 chart1.Series[0].YAxisType = AxisType.Secondary; // 設置線型 chart1.Series[0].ChartType = SeriesChartType.Line; // 設置線的顏色 chart1.Series[0].Color = Color.Black; // 設置線寬 chart1.Series[0].CustomProperties = "PointWidth=0.01"; // 游標位置改變時觸發的事件 chart1.CursorPositionChanged += (sender, args) => { }; // 游標的位置 var position = chart1.ChartAreas[0].CursorX.Position; // X軸的最小值 var mininum = chart1.ChartAreas[0].AxisX.Minimum; // Y軸可用性 chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.False; // 游標所在軸 chart1.ChartAreas[0].CursorY.AxisType = AxisType.Secondary; // 線可用性 chart1.Series[0].Enabled = true; // 軸坐標的形式 chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0.00"; // 清楚圖例 chart1.Legends.Clear(); // 軸不可縮放 chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false; // 選擇區域改變時的事件 chart1.SelectionRangeChanged += (sender, args) => { if (args.Axis.AxisName == AxisName.X) { chart1.ChartAreas[0].AxisX.Minimum = 0; chart1.ChartAreas[0].AxisX.Maximum = 100; } else if (args.Axis.AxisName == AxisName.Y) { chart1.ChartAreas[0].AxisY.Minimum = 0; chart1.ChartAreas[0].AxisY.Maximum = 100; } }; } private List<double> listX; private List<double> listY; } }
