C#怎么根据excel数据绘制坐标图
这篇文章主要介绍了C#怎么根据excel数据绘制坐标图的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#怎么根据excel数据绘制坐标图文章都会有所收获,下面我们一起来看看吧。
效果如下图
界面
代码
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms;usingSystem.Windows.Forms.DataVisualization.Charting;namespaceWindowsFormsApp2{publicpartialclassForm1:Form{//x和y轴数据double[]x=newdouble[]{0,0,0,0,0,0,0,0,0,0};double[]y=newdouble[]{0,0,0,0,0,0,0,0,0,0};List<Double>xList=newList<Double>();List<Double>yList=newList<Double>();publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){stringfname="";OpenFileDialogfdlg=newOpenFileDialog();fdlg.Title="ExcelFileDialog";fdlg.InitialDirectory=@"c:\";fdlg.Filter="Allfiles(*.*)|*.*|Allfiles(*.*)|*.*";fdlg.FilterIndex=2;fdlg.RestoreDirectory=true;if(fdlg.ShowDialog()==DialogResult.OK){fname=fdlg.FileName;}Microsoft.Office.Interop.Excel.ApplicationxlApp=newMicrosoft.Office.Interop.Excel.Application();Microsoft.Office.Interop.Excel.WorkbookxlWorkbook=xlApp.Workbooks.Open(fname);Microsoft.Office.Interop.Excel._WorksheetxlWorksheet=xlWorkbook.Sheets[1];Microsoft.Office.Interop.Excel.RangexlRange=xlWorksheet.UsedRange;introwCount=xlRange.Rows.Count;intcolCount=xlRange.Columns.Count;for(inti=1;i<=rowCount;i++){doublepx=System.Convert.ToDouble(xlRange.Cells[i,1].Value2.ToString());doublepy=System.Convert.ToDouble(xlRange.Cells[i,2].Value2.ToString());Console.Out.WriteLine("第"+i+"行:"+px+","+py);xList.Add(px);yList.Add(py);//for(intj=1;j<=colCount;j++)//{//writethevaluetotheGrid//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());//addusefulthingshere!//}//}}chart1.Series[0].Points.DataBindXY(xList,yList);//cleanupGC.Collect();GC.WaitForPendingFinalizers();//ruleofthumbforreleasingcomobjects://neverusetwodots,allCOMobjectsmustbereferencedandreleasedindividually//ex:[somthing].[something].[something]isbad//releasecomobjectstofullykillexcelprocessfromrunninginthebackgroundMarshal.ReleaseComObject(xlRange);Marshal.ReleaseComObject(xlWorksheet);//closeandreleasexlWorkbook.Close();Marshal.ReleaseComObject(xlWorkbook);//quitandreleasexlApp.Quit();Marshal.ReleaseComObject(xlApp);}//Graphicsg=this.CreateGraphics();//Penpen=newPen(Brushes.Red,1);//g.DrawLine(pen,newPoint(30,50),newPoint(250,250));privatevoidForm1_Load(objectsender,EventArgse){//控件chart背景色//chart1.BackColor=Color.Transparent;//Color.Transparent系统定义的颜色//chart1.BackColor=Color.White;//图表标题,chart1.Titles.Add("测试数据");//添加title到titleCollection集合的末尾chart1.Titles[0].ForeColor=Color.DarkBlue;//设置title的文本颜色chart1.Titles[0].Font=newFont("微软雅黑",15f,FontStyle.Regular);//设置title的字体chart1.Titles[0].Alignment=ContentAlignment.TopCenter;//设置title的对齐方式//图表区chartAreaschart1.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;//设置x轴的间隔chart1.ChartAreas[0].AxisX.Minimum=0;chart1.ChartAreas[0].AxisX.Maximum=25;//Y轴坐标固定,不会随绑定的数据而变chart1.ChartAreas[0].AxisX.LabelStyle.Interval=1;//设置X轴标签间距,如果不设置默认为x轴的间隔chart1.ChartAreas[0].AxisX.IsLabelAutoFit=false;chart1.ChartAreas[0].AxisX.LabelStyle.Font=newFont("微软雅黑",13f,FontStyle.Regular);//标签字体//设置x轴标题的字体样式和颜色chart1.ChartAreas[0].AxisX.Title="圆周位置,mm";chart1.ChartAreas[0].AxisX.TitleFont=newFont("微软雅黑",15f,FontStyle.Regular);//标题字体chart1.ChartAreas[0].AxisX.TitleForeColor=Color.Blue;//轴标题颜色chart1.ChartAreas[0].AxisX.TextOrientation=TextOrientation.Horizontal;//轴标题文本方向chart1.ChartAreas[0].AxisX.TitleAlignment=StringAlignment.Far;//轴标题对齐方式//X轴网格线chart1.ChartAreas[0].AxisX.MajorGrid.Enabled=false;//启用网格刻度线,一排竖线//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;//设置Y轴的间隔chart1.ChartAreas[0].AxisY.Minimum=5;//Y轴坐标固定,不会随绑定的数据而变chart1.ChartAreas[0].AxisY.Maximum=6.35;//Y轴坐标固定,不会随绑定的数据而变chart1.ChartAreas[0].AxisY.LabelStyle.Interval=0.05;//设置X轴标签间距,如果不设置默认为x轴的间隔//Y坐标轴标题chart1.ChartAreas[0].AxisY.Title="圆周半径,mm";//轴标题chart1.ChartAreas[0].AxisY.TitleFont=newFont("微软雅黑",15f,FontStyle.Regular);//标题字体chart1.ChartAreas[0].AxisY.TitleForeColor=Color.Blue;//轴标题颜色chart1.ChartAreas[0].AxisY.TextOrientation=TextOrientation.Rotated270;//标题文本方向chart1.ChartAreas[0].AxisY.TitleAlignment=StringAlignment.Far;//y轴标签样式chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor=Color.Black;//标签颜色chart1.ChartAreas[0].AxisY.LabelStyle.Font=newFont("微软雅黑",13f,FontStyle.Regular);//标签字体//Y轴网格线条chart1.ChartAreas[0].AxisY.MajorGrid.Enabled=false;//一排横线//chart1.ChartAreas[0].AxisY.MajorGrid.LineColor=Color.Yellow;//#VAL为y轴的值,#VALX为x轴数据//chart1.Series[0].Label="hello";//数据点标签文本//chart1.Series[0].Label="#VAL";//数据点标签为其对于的y值//chart1.Series[0].LabelBackColor=Color.Blue;//数据点标签背景色//chart1.Series[0].LabelForeColor=Color.White;//数据点标签颜色//chart1.Series[0].Color=Color.Red;//数据点颜色,数据点之间曲线的颜色//chart1.Series[0].BorderWidth=3;//数据点边框宽度,曲线的宽度//chart1.Series[0].ToolTip="#VALX:#VAL";//鼠标移动到对应点显示数值元素的工具提示chart1.Series[0].ChartType=SeriesChartType.Spline;//图表类型(折线)绘制该序列的图表类型Legendlegend=newLegend("波形显示");//初始化具有指定的图例名称legend.Title="legendTitle";//图例标题文本chart1.Series[0].LegendText=legend.Name;//图例中项的文本chart1.Legends.Add(legend);chart1.Legends[0].Position.Auto=false;//图例矩形位置-元素自动定位标志//绑定数据//数据绑定到指定数据源的第一列的x值和y值的集合的数据点chart1.Series[0].Color=Color.Black;chart1.Series[0].Points.DataBindXY(x,y);}}}
关于“C#怎么根据excel数据绘制坐标图”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C#怎么根据excel数据绘制坐标图”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。