您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#怎么根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C#怎么根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖文章都會有所收獲,下面我們一起來看看吧。
效果如下圖
界面
代碼
using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApp2 { public partial class Form1 : Form { //x和y軸數(shù)據(jù) double[] x = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] y = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; List<Double> xList = new List<Double>(); List<Double> yList = new List<Double>(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string fname = ""; OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "Excel File Dialog"; fdlg.InitialDirectory = @"c:\"; fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; fdlg.FilterIndex = 2; fdlg.RestoreDirectory = true; if (fdlg.ShowDialog() == DialogResult.OK) { fname = fdlg.FileName; } Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname); Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange; int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; for (int i = 1; i <= rowCount; i++) { double px = System.Convert.ToDouble(xlRange.Cells[i, 1].Value2.ToString()); double py = System.Convert.ToDouble(xlRange.Cells[i, 2].Value2.ToString()); Console.Out.WriteLine("第" + i + "行 :" + px + "," + py); xList.Add(px); yList.Add(py); //for (int j = 1; j <= colCount; j++) //{ //write the value to the Grid //if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) //{ //xList.Add(xlRange.Cells[i, j]); // Console.WriteLine(xlRange.Cells[i, j].Value2.ToString()); //add useful things here! // } //} } chart1.Series[0].Points.DataBindXY(xList, yList); //cleanup GC.Collect(); GC.WaitForPendingFinalizers(); //rule of thumb for releasing com objects: // never use two dots, all COM objects must be referenced and released individually // ex: [somthing].[something].[something] is bad //release com objects to fully kill excel process from running in the background Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(xlWorksheet); //close and release xlWorkbook.Close(); Marshal.ReleaseComObject(xlWorkbook); //quit and release xlApp.Quit(); Marshal.ReleaseComObject(xlApp); } //Graphics g = this.CreateGraphics(); //Pen pen = new Pen(Brushes.Red, 1); //g.DrawLine(pen, new Point(30, 50), new Point(250, 250)); private void Form1_Load(object sender, EventArgs e) { //控件chart背景色 //chart1.BackColor = Color.Transparent;//Color.Transparent系統(tǒng)定義的顏色 //chart1.BackColor = Color.White; //圖表標(biāo)題, chart1.Titles.Add("測試數(shù)據(jù)"); //添加title到titleCollection集合的末尾 chart1.Titles[0].ForeColor = Color.DarkBlue;//設(shè)置title的文本顏色 chart1.Titles[0].Font = new Font("微軟雅黑", 15f, FontStyle.Regular);//設(shè)置title的字體 chart1.Titles[0].Alignment = ContentAlignment.TopCenter;//設(shè)置title的對齊方式 //圖表區(qū)chartAreas chart1.ChartAreas[0].BackColor = Color.White;//chartAreas背景顏色 chart1.ChartAreas[0].BorderColor = Color.Red;//chartAreas邊框顏色 chart1.ChartAreas[0].BackGradientStyle = GradientStyle.None;//chartAreas背景漸變,不使用 //AxisX表示圖表的主x軸; chart1.ChartAreas[0].AxisX.LineColor = Color.Red; //線條顏色 chart1.ChartAreas[0].AxisX.Interval = 0.5;//設(shè)置x軸的間隔 chart1.ChartAreas[0].AxisX.Minimum = 0; chart1.ChartAreas[0].AxisX.Maximum = 25;//Y軸坐標(biāo)固定,不會隨綁定的數(shù)據(jù)而變 chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//設(shè)置X軸標(biāo)簽間距,如果不設(shè)置默認為x軸的間隔 chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false; chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("微軟雅黑", 13f, FontStyle.Regular); //標(biāo)簽字體 //設(shè)置x軸標(biāo)題的字體樣式和顏色 chart1.ChartAreas[0].AxisX.Title = "圓周位置,mm"; chart1.ChartAreas[0].AxisX.TitleFont = new Font("微軟雅黑", 15f, FontStyle.Regular);// 標(biāo)題字體 chart1.ChartAreas[0].AxisX.TitleForeColor = Color.Blue; //軸標(biāo)題顏色 chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;//軸標(biāo)題文本方向 chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;//軸標(biāo)題對齊方式 //X軸網(wǎng)格線 chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; //啟用網(wǎng)格刻度線,一排豎線 //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#2c4c6d"); //線條顏色 //chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Yellow; //y軸 chart1.ChartAreas[0].AxisY.LineColor = Color.Red; //線條顏色 chart1.ChartAreas[0].AxisY.Interval = 0.05;//設(shè)置Y軸的間隔 chart1.ChartAreas[0].AxisY.Minimum = 5;//Y軸坐標(biāo)固定,不會隨綁定的數(shù)據(jù)而變 chart1.ChartAreas[0].AxisY.Maximum = 6.35;//Y軸坐標(biāo)固定,不會隨綁定的數(shù)據(jù)而變 chart1.ChartAreas[0].AxisY.LabelStyle.Interval = 0.05;//設(shè)置X軸標(biāo)簽間距,如果不設(shè)置默認為x軸的間隔 //Y坐標(biāo)軸標(biāo)題 chart1.ChartAreas[0].AxisY.Title = "圓周半徑,mm"; //軸標(biāo)題 chart1.ChartAreas[0].AxisY.TitleFont = new Font("微軟雅黑", 15f, FontStyle.Regular); //標(biāo)題字體 chart1.ChartAreas[0].AxisY.TitleForeColor = Color.Blue; //軸標(biāo)題顏色 chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270; //標(biāo)題文本方向 chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far; //y軸標(biāo)簽樣式 chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Black; //標(biāo)簽顏色 chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("微軟雅黑", 13f, FontStyle.Regular); //標(biāo)簽字體 //Y軸網(wǎng)格線條 chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;//一排橫線 //chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Yellow; //#VAL為y軸的值,#VALX為x軸數(shù)據(jù) //chart1.Series[0].Label = "hello";//數(shù)據(jù)點標(biāo)簽文本 //chart1.Series[0].Label = "#VAL";//數(shù)據(jù)點標(biāo)簽為其對于的y值 //chart1.Series[0].LabelBackColor = Color.Blue; //數(shù)據(jù)點標(biāo)簽背景色 //chart1.Series[0].LabelForeColor = Color.White; //數(shù)據(jù)點標(biāo)簽顏色 //chart1.Series[0].Color = Color.Red; //數(shù)據(jù)點顏色,數(shù)據(jù)點之間曲線的顏色 //chart1.Series[0].BorderWidth = 3;//數(shù)據(jù)點邊框?qū)挾龋€的寬度 //chart1.Series[0].ToolTip = "#VALX:#VAL";//鼠標(biāo)移動到對應(yīng)點顯示數(shù)值 元素的工具提示 chart1.Series[0].ChartType = SeriesChartType.Spline; //圖表類型(折線) 繪制該序列的圖表類型 Legend legend = new Legend("波形顯示");//初始化具有指定的圖例名稱 legend.Title = "legendTitle"; //圖例標(biāo)題文本 chart1.Series[0].LegendText = legend.Name; //圖例中項的文本 chart1.Legends.Add(legend); chart1.Legends[0].Position.Auto = false; //圖例矩形位置 - 元素自動定位標(biāo)志 //綁定數(shù)據(jù) //數(shù)據(jù)綁定到指定數(shù)據(jù)源的第一列的x值和y值的集合的數(shù)據(jù)點 chart1.Series[0].Color = Color.Black; chart1.Series[0].Points.DataBindXY(x, y); } } }
關(guān)于“C#怎么根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“C#怎么根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。