生活随笔
收集整理的這篇文章主要介紹了
C# chart画折线
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 一、版本一(DataGridView 和 Chart)
- 1.小目標(biāo)
- 2.過程實(shí)現(xiàn)
- 3.結(jié)果
- 二、版本二(Combobox 和 Chart)
- 1.小目標(biāo)
- 2.過程實(shí)現(xiàn)
- 3.淺貼一下效果
- 算是完結(jié)了吧 ,有問題一起交流鴨
一、版本一(DataGridView 和 Chart)
1.小目標(biāo)
控件綁定Sql Sever 數(shù)據(jù)庫
DataGridView列出數(shù)據(jù),Chart畫折線
2.過程實(shí)現(xiàn)
數(shù)據(jù)綁定
1.創(chuàng)建SqlHelper類
public static class SqlHelper{ public static DataTable GetData(string sql
){DataTable dataTable
= new DataTable();string connString
= "Data Source=.;Initial Catalog = Line;User ID = sa;Password=abcdef";SqlConnection conn
= new SqlConnection(connString
);SqlCommand cmd
= new SqlCommand(sql
, conn
);cmd
.Parameters
.Clear();conn
.Open();SqlDataAdapter da
= new SqlDataAdapter();da
.SelectCommand
= cmd
;conn
.Close();da
.Fill(dataTable
);return dataTable
;}}
2.窗體代碼
private void Form1_Load(object sender
, EventArgs e
){string Sql
= "select numX,numY from BrokenLine30";DataTable dt
= SqlHelper
.GetData(Sql
);dataGridView1
.DataSource
= dt
;for(int i
=0; i
<dt
.Rows
.Count
;i
++){chart1
.Series
[0].Points
.AddXY(i
+1,dt
.Rows
[i
][1]);}chart1
.ChartAreas
[0].AxisY
.Minimum
= 27.5;chart1
.ChartAreas
[0].AxisY
.Maximum
= 31.5;chart1
.Series
[0].BorderWidth
= 3;}
3.結(jié)果
- 讀取到了數(shù)據(jù)庫中的數(shù)據(jù),顯示在表格中
- 做一下改善,將線變粗,設(shè)置了Y軸上下限
- X軸還可以在屬性中做更一步設(shè)置,讓折線從1開始
- 空白部分還能發(fā)揮空間,添加一些標(biāo)簽和文本,做些數(shù)據(jù)統(tǒng)計(jì)
二、版本二(Combobox 和 Chart)
1.小目標(biāo)
Combobox選擇不同的值,Chart顯示相對應(yīng)的折線在Chart中顯示具體數(shù)據(jù)、Y軸上下限隨數(shù)值變化,添加三條輔助線,分別代表數(shù)據(jù)中的最大值,最小值和標(biāo)準(zhǔn)值
2.過程實(shí)現(xiàn)
- 界面設(shè)置
與前一部分設(shè)置相同,主要是只想呈現(xiàn)X軸和Y軸
combobox屬性中(Name)改為cbbMidLine
放一些標(biāo)簽和文本,分別改(Name)為textAvg,textMAX,textMIN,textCount
public static class SqlHelper{public static DataTable GetDataTable(string sql
){DataTable dataTable
= new DataTable();string connString
= "Data Source=.;Initial Catalog = Line;User ID = sa;Password=abcdef";SqlConnection conn
= new SqlConnection(connString
);SqlCommand cmd
= new SqlCommand(sql
, conn
);cmd
.Parameters
.Clear();conn
.Open();SqlDataAdapter da
= new SqlDataAdapter();da
.SelectCommand
= cmd
;conn
.Close();da
.Fill(dataTable
);return dataTable
;}public static double Count(string sql
){double result
= 0;string connString
= "Data Source=.;Initial Catalog = Line;User ID = sa;Password=abcdef";SqlConnection conn
= new SqlConnection(connString
);SqlCommand cmd
= new SqlCommand(sql
, conn
);cmd
.Parameters
.Clear();conn
.Open();result
= Convert
.ToDouble(cmd
.ExecuteScalar());return result
;}}
public static class Line{public static StripLine SetLine(string sql
,out double Line
){string connString
= "Data Source=DESKTOP-JQOODRR;Initial Catalog = Line;User ID = sa;Password=abcdef";SqlConnection conn
= new SqlConnection(connString
);SqlCommand cmd
= new SqlCommand(sql
, conn
);cmd
.Parameters
.Clear();conn
.Open();Line
= Convert
.ToDouble(cmd
.ExecuteScalar());conn
.Close();StripLine stripLine
= new StripLine();stripLine
.Interval
= 0;stripLine
.IntervalOffset
= Line
;stripLine
.StripWidth
= 0.02;stripLine
.BackColor
=Color
.DarkGreen
;stripLine
.Text
= String
.Format("{0:F}", Line
);return stripLine
;}public static StripLine DrawMid(double a
){StripLine stripMid
=new StripLine();stripMid
.Interval
= 0;stripMid
.StripWidth
= 0.03;stripMid
.IntervalOffset
= a
;stripMid
.BackColor
= Color
.Orange
;stripMid
.Text
= string.Format("標(biāo)準(zhǔn)值:{0:F}",a
);return stripMid
;}}
private void Form1_Load(object sender
, EventArgs e
){string sqlS
= "select numX,numY from BrokenLine30";DataTable dt
= SqlHelper
.GetDataTable(sqlS
);string sqlM
= "select * from MidLLine";DataTable dtM
= SqlHelper
.GetDataTable(sqlM
);cbbMidLine
.DataSource
= dtM
;cbbMidLine
.ValueMember
= "MidLine";cbbMidLine
.DisplayMember
= "MidLine";}private void cbbMidLine_SelectedIndexChanged(object sender
, EventArgs e
){double Max
,Min
,Mid
;StripLine stripMax
, stripMin
, stripMid
;if (cbbMidLine
.SelectedIndex
== 0){string sqlA
= "select AVG(numY) from BrokenLine10 ";string sqlMax
= "select MAX(numY) from BrokenLine10";string sqlMin
= "select MIN(numY) from BrokenLine10";string sqlCount
= "select count(1) from BrokenLine10";stripMax
= Line
.SetLine(sqlMax
,0.02,Color
.DarkGreen
,out Max
);textMAX
.Text
= Max
.ToString();stripMin
= Line
.SetLine(sqlMin
,0.02,Color
.DarkGreen
,out Min
);textMIN
.Text
= Min
.ToString();stripMid
=Line
.SetLine(sqlA
,0.03,Color
.Orange
,out Mid
);texAvg
.Text
= Mid
.ToString("0.00");double Count
=SqlHelper
.Count(sqlCount
);textCount
.Text
= Count
.ToString();stripMid
.Text
= string.Format("標(biāo)準(zhǔn)值:{0:F}", 10);stripMid
.IntervalOffset
= 10;string sqlS
= "select numX,numY from BrokenLine10";DataTable dt
= SqlHelper
.GetDataTable(sqlS
);chart1
.DataSource
= dt
;for (int i
= 0; i
< dt
.Columns
.Count
; i
++){chart1
.Series
[0].YValueMembers
= dt
.Columns
[i
].ColumnName
;}chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMax
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMin
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMid
);this.chart1
.ChartAreas
[0].AxisY
.Minimum
= Min
-0.05;this.chart1
.ChartAreas
[0].AxisY
.Maximum
= Max
+0.05;}if (cbbMidLine
.SelectedIndex
== 1){string sqlA
= "select AVG(numY) from BrokenLine20 ";string sqlMax
= "select MAX(numY) from BrokenLine20";string sqlMin
= "select MIN(numY) from BrokenLine20";string sqlCount
= "select count(1) from BrokenLine20";stripMax
= Line
.SetLine(sqlMax
, 0.02, Color
.DarkGreen
, out Max
);textMAX
.Text
= Max
.ToString();stripMin
= Line
.SetLine(sqlMin
, 0.02, Color
.DarkGreen
,out Min
);textMIN
.Text
= Min
.ToString();stripMid
=Line
.SetLine(sqlA
, 0.03, Color
.Orange
, out Mid
); texAvg
.Text
= Mid
.ToString("0.00");double Count
= SqlHelper
.Count(sqlCount
);textCount
.Text
= Count
.ToString();stripMid
.Text
= string.Format("標(biāo)準(zhǔn)值:{0:F}", 20);stripMid
.IntervalOffset
= 20;string sqlS
= "select numX,numY from BrokenLine20";DataTable dt
= SqlHelper
.GetDataTable(sqlS
);chart1
.DataSource
= dt
;for (int i
= 0; i
< dt
.Columns
.Count
; i
++){chart1
.Series
[0].YValueMembers
= dt
.Columns
[i
].ColumnName
;}chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMax
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMin
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMid
);this.chart1
.ChartAreas
[0].AxisY
.Minimum
= Min
-0.05;this.chart1
.ChartAreas
[0].AxisY
.Maximum
= Max
+0.05;}if (cbbMidLine
.SelectedIndex
== 2){string sqlA
= "select AVG(numY) from BrokenLine30 ";string sqlMax
= "select MAX(numY) from BrokenLine30";string sqlMin
= "select MIN(numY) from BrokenLine30";string sqlCount
= "select count(1) from BrokenLine30";stripMax
= Line
.SetLine(sqlMax
, 0.02, Color
.DarkGreen
, out Max
);textMAX
.Text
= Max
.ToString();stripMin
= Line
.SetLine(sqlMin
, 0.02, Color
.DarkGreen
, out Min
);textMIN
.Text
= Min
.ToString();stripMid
= Line
.SetLine(sqlA
, 0.03, Color
.Orange
, out Mid
);texAvg
.Text
= Mid
.ToString("0.00");double Count
= SqlHelper
.Count(sqlCount
);textCount
.Text
= Count
.ToString();stripMid
.Text
= string.Format("標(biāo)準(zhǔn)值:{0:F}", 30);stripMid
.IntervalOffset
= 30;string sqlS
= "select numX,numY from BrokenLine30";DataTable dt
= SqlHelper
.GetDataTable(sqlS
);chart1
.DataSource
= dt
;for (int i
= 0; i
< dt
.Columns
.Count
; i
++){chart1
.Series
[0].YValueMembers
= dt
.Columns
[i
].ColumnName
;}chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMax
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMin
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMid
);this.chart1
.ChartAreas
[0].AxisY
.Minimum
= Min
-0.05;this.chart1
.ChartAreas
[0].AxisY
.Maximum
= Max
+0.05;}if (cbbMidLine
.SelectedIndex
== 3){string sqlA
= "select AVG(numY) from BrokenLine40 ";string sqlMax
= "select MAX(numY) from BrokenLine40";string sqlMin
= "select MIN(numY) from BrokenLine40";string sqlCount
= "select count(1) from BrokenLine40";stripMax
= Line
.SetLine(sqlMax
, 0.02, Color
.DarkGreen
, out Max
);textMAX
.Text
= Max
.ToString();stripMin
= Line
.SetLine(sqlMin
, 0.02, Color
.DarkGreen
, out Min
);textMIN
.Text
= Min
.ToString();stripMid
= Line
.SetLine(sqlA
, 0.03, Color
.Orange
, out Mid
);texAvg
.Text
= Mid
.ToString("0.00");double Count
= SqlHelper
.Count(sqlCount
);textCount
.Text
= Count
.ToString();stripMid
.Text
= string.Format("標(biāo)準(zhǔn)值:{0:F}", 40);stripMid
.IntervalOffset
= 40;string sqlS
= "select numX,numY from BrokenLine40";DataTable dt
= SqlHelper
.GetDataTable(sqlS
);chart1
.DataSource
= dt
;for (int i
= 0; i
< dt
.Columns
.Count
; i
++){chart1
.Series
[0].YValueMembers
= dt
.Columns
[i
].ColumnName
;}chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMax
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMin
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMid
);this.chart1
.ChartAreas
[0].AxisY
.Minimum
= Min
-0.05;this.chart1
.ChartAreas
[0].AxisY
.Maximum
= Max
+0.05;}if (cbbMidLine
.SelectedIndex
== 4){string sqlA
= "select AVG(numY) from BrokenLine50 ";string sqlMax
= "select MAX(numY) from BrokenLine50";string sqlMin
= "select MIN(numY) from BrokenLine50";string sqlCount
= "select count(1) from BrokenLine50";stripMax
= Line
.SetLine(sqlMax
, 0.02, Color
.DarkGreen
, out Max
);textMAX
.Text
= Max
.ToString();stripMin
= Line
.SetLine(sqlMin
, 0.02, Color
.DarkGreen
, out Min
);textMIN
.Text
= Min
.ToString();stripMid
= Line
.SetLine(sqlA
, 0.03, Color
.Orange
, out Mid
);texAvg
.Text
= Mid
.ToString("0.00");double Count
= SqlHelper
.Count(sqlCount
);textCount
.Text
= Count
.ToString();stripMid
.Text
= string.Format("標(biāo)準(zhǔn)值:{0:F}", 50);stripMid
.IntervalOffset
= 50;string sqlS
= "select numX,numY from BrokenLine50";DataTable dt
= SqlHelper
.GetDataTable(sqlS
);chart1
.DataSource
= dt
;for (int i
= 0; i
< dt
.Columns
.Count
; i
++){chart1
.Series
[0].YValueMembers
= dt
.Columns
[i
].ColumnName
;}chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMax
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMin
);chart1
.ChartAreas
[0].AxisY
.StripLines
.Add(stripMid
);this.chart1
.ChartAreas
[0].AxisY
.Minimum
= Min
-0.05;this.chart1
.ChartAreas
[0].AxisY
.Maximum
= Max
+0.05;}}
- 窗體Load事件是雙擊觸發(fā)
- combobox(在此項(xiàng)目中改名為cbbMidLine)的值改變事件是在屬性里的事件里找到SelectedChanged再點(diǎn)擊
- 點(diǎn)擊完的效果
- 數(shù)據(jù)庫的設(shè)計(jì)
- 表的設(shè)計(jì)
BrokenLine10–BrokenLine50,設(shè)計(jì)都是一樣的此表放平均值
3.淺貼一下效果
算是完結(jié)了吧 ,有問題一起交流鴨
總結(jié)
以上是生活随笔為你收集整理的C# chart画折线的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。