C#根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的方法
更新時間:2022年02月17日 09:40:48 作者:神奇小白
這篇文章主要為大家詳細(xì)介紹了C#根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#根據(jù)excel數(shù)據(jù)繪制坐標(biāo)圖的具體代碼,供大家參考,具體內(nèi)容如下
效果如下圖
界面
代碼
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è)置默認(rèn)為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è)置默認(rèn)為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); ? ? ? ? } ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用SqlServer作為日志數(shù)據(jù)庫的設(shè)計與實現(xiàn)
這篇文章主要給大家介紹了關(guān)于C#使用SqlServer作為日志數(shù)據(jù)庫的設(shè)計與實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C# 使用匿名函數(shù)解決EventHandler參數(shù)傳遞的難題
C#動態(tài)生成PictureBox并綁定右鍵菜單,實現(xiàn)刪除圖片2009-05-05C# Redis學(xué)習(xí)系列(二)Redis基本設(shè)置
這篇文章主要為大家分享了C# Redis學(xué)習(xí)系列教程第二篇, Redis基本設(shè)置,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05