標籤:winform blog http color 使用 os 資料 io
添加mschart.dll動態連結程式庫添加引用System.Windows.Forms.DataVisualization
MSChart控制項作為方便的使用者資料展示控制項,可以方便的使用控制項提供的形狀和展示形式展示資料,早Web應用中用的比較多,這幾天一直在做一個基於Winform的CS結構的示範程式,用到了MSChart,由於一直也不太熟悉MSChart,又是動態自訂添加,所以一點一點的摸索著做起來,動態添加自訂的MSChart到WinForm程式中,上代碼:
1、建立一條曲線形式的Chart
System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
//定義一個chart
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
//定義一個繪圖區域
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
//定義一個資料列
chartArea1.Name = "ChartArea1";
//其實沒有必要,可以使用chart1。ChartAreas[0]就可以了
chart1.ChartAreas.Add(chartArea1);
//完成Chart和chartArea的關聯
//System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
//legend1.Name = "表徵圖";
//chart1.Legends.Add(legend1);
chart1.Name = "chart1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
//設定線性
Random rd = new Random();
double[] num = new double[20];
for (int i = 0; i < 20; i++)
{
int valuey = rd.Next(20, 100);
DataPoint point = new DataPoint((i + 1), valuey);
series1.Points.Add(point);
}
//產生點的座標
//chart1.Titles[0].Text = "";
series1.ChartArea = "ChartArea1";
chartArea1.AxisX.Title = "日期";
chartArea1.AxisY.Title = "值";
chartArea1.AxisX.Interval = 1;
chartArea1.AxisY.Interval = 5;
chartArea1.AxisY.Minimum = 20;
series1.Legend = "表徵圖";
series1.Name = "Series1";
chart1.Text = "測試";
chart1.Size = new System.Drawing.Size(700, 500);
//chart1.Location = new System.Drawing.Point(50,120);
series1.Color = Color.Blue;
chart1.Text = "ceshi";
//chart1.Titles[0].Text = "fff";
chart1.Series.Add(series1);
//這一句很重要,缺少的話繪圖區域將顯示空白
//chart1.SizeChanged += new System.EventHandler(DoSizeChanged);
//chart1.AllowDrop = true;
chart1.BackColor = Color.FromArgb(243, 223, 193);
//設定chart背景顏色
chartArea1.BackColor = Color.FromArgb(243, 223, 193);
//設定c繪圖區域背景顏色
series1.BorderWidth = 2;
series1.IsValueShownAsLabel = true;
//是否顯示Y的值
//this.groupBox9.Controls.Add(chart1);
this.panel21.Controls.Add(chart1);
chart1.Visible = true;
//this.label10.Visible = true;
//this.label10.Text = "【" + tn.Name + "】圖";
chart1.Titles.Add("【" + tn.Name + "】圖");
//為Chart1添加標題
chartArea1.AxisX.IsMarginVisible = true;
//是否顯示X軸兩端的空白
2、在上述chart的基礎上添加一條線
Control[] controls = this.groupBox9.Controls.Find("chart1",true);
//找到已經有的Chart1
System.Windows.Forms.DataVisualization.Charting.Chart ch = (System.Windows.Forms.DataVisualization.Charting.Chart)controls[0];
System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
//新定義一個資料項目
Random rd=new Random();
for (int i = 0; i < ch.Series[0].Points.Count; i++)
{
int valuey=rd.Next(20,100);
DataPoint point = new DataPoint((i + 1), valuey);
series2.Points.Add(point);
}
series2.Color = Color.FromArgb(rd.Next(100, 255), rd.Next(0, 150), rd.Next(0, 255));
series2.BorderWidth = 2;
series2.ChartType = ((System.Windows.Forms.DataVisualization.Charting.Chart)this.panel21.Controls[0]).Series[0].ChartType;
//定義線性和原有的線條形狀一致
series2.IsValueShownAsLabel = true;
ch.Series.Add(series2);
//添加資料列
3、減少一條曲線
Control[] controls = this.groupBox9.Controls.Find("chart1", true);
System.Windows.Forms.DataVisualization.Charting.Chart ch = (System.Windows.Forms.DataVisualization.Charting.Chart)controls[0];
if (ch.Series.Count>1)
{
//MessageBox.Show(this, "去掉一條線!", "資訊提示");
int i = ch.Series.Count - 1;
while (i>1)
{
if (ch.Series[i].Points.Count>0)
{
break;
}
i -=1;
}
ch.Series[i].Points.Clear();
this.toolStripStatusLabel2.Text = "減少對比曲線完成!";
}
else
{
MessageBox.Show(this, "繪圖區域至少要有一條線!", "資訊提示");
}
: