C#根据excel数据绘制坐标图的方法
程序员文章站
2022-06-09 19:03:11
本文实例为大家分享了c#根据excel数据绘制坐标图的具体代码,供大家参考,具体内容如下效果如下图界面代码using system;using system.collections;using sys...
本文实例为大家分享了c#根据excel数据绘制坐标图的具体代码,供大家参考,具体内容如下
效果如下图
界面
代码
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轴数据 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系统定义的颜色 //chart1.backcolor = color.white; //图表标题, chart1.titles.add("测试数据"); //添加title到titlecollection集合的末尾 chart1.titles[0].forecolor = color.darkblue;//设置title的文本颜色 chart1.titles[0].font = new font("微软雅黑", 15f, fontstyle.regular);//设置title的字体 chart1.titles[0].alignment = contentalignment.topcenter;//设置title的对齐方式 //图表区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;//设置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 = new font("微软雅黑", 13f, fontstyle.regular); //标签字体 //设置x轴标题的字体样式和颜色 chart1.chartareas[0].axisx.title = "圆周位置,mm"; chart1.chartareas[0].axisx.titlefont = new font("微软雅黑", 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 = new font("微软雅黑", 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 = new font("微软雅黑", 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; //图表类型(折线) 绘制该序列的图表类型 legend legend = new legend("波形显示");//初始化具有指定的图例名称 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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: CSRF跨站请求伪造漏洞分析与防御
下一篇: 51单片机实验(一)定时/计数器及其中断