本文实例为大家分享了c#根据excel数据绘制坐标图的具体代码,供大家参考,具体内容如下
效果如下图
界面
代码
using system;using system.collections;using system.collections.generic;using system.drawing;using system.runtime.interoprvices;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 nder, 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.udrange; 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 uful things here! // } //} } chart1.ries[0].points.databindxy(xlist, ylist); //cleanup gc.collect(); gc.waitforpendingfinalizers(); //rule of thumb for releasing com objects: // never u two dots, all com objects must be referenced and relead individually // ex: [somthing].[something].[something] is bad //relea com objects to fully kill excel process from running in the background marshal.releacomobject(xlrange); marshal.releacomobject(xlworksheet); //clo and relea xlworkbook.clo(); marshal.releacomobje传播方案ct(xlworkbook); //quit and relea xlapp.quit(); marshal.releacomobject(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 nder, 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("微软雅黑", 1热带雨林气候成因5f, fontstyle.regular);//设置title的字体 chart1.titles[0].alignment = contentalignment.topcenter;//设置title的对齐方式 //图表区chartareas chart1.chartareas[0].backcolor = color.white;//chartareas背景颜色 chart1.chartareas[0].border侮辱的近义词和反义词color = 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 = fal; 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 = fal; //启用网格刻度线,一排竖线 //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 = fal;//一排横线 //chart1.chartareas[0].axisy.majorgrid.linecolor = color.yellow; //#val为y轴的值,#valx为x轴数据 //chart1.ries[0].label = "hello";//数据点标签文本 //chart1.ries[0].label = "#val";//数据点标签为其对于的y值 //chart1.ries[0].labelbackcolor = color.blue; //数据点标签背景色 //chart1.ries[0].labelforecolor = color.white; //数据点标签颜色 //chart1.ries[0].color = color.red; //数据点颜色,数据点之间曲线的颜色 //chart1.ries[0].borderwidth = 3;//数据点边框宽度,曲线的宽度 //chart1.ries[0].tooltip = "#valx:#val";//鼠标移动到对应点显示数值 元素的工具提示 chart1.ries[0].charttype = riescharttype.spline; //图表类型(折线) 绘制该序列的图表类型 legend legend = new legend("波形显示");//初始化具有指定的图例名称 legend.title = "legendtitle"; //图例标题文本 chart1.ries[0].legendtext = legend.name; //图例中项的文本 chart1.legends.add(legend); chart1.legends[0].position.auto = fal; //图例矩形位置 - 元素自动定位标志 //绑定数据 //数据绑定到指定数据源的第一列的x值和y值的集合的数据点 chart1.ries[0].color = color.black; chart1.ries[0].points.databindxy(x, y); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-05 01:29:33,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/24772ea653790ff8af32add5d957e242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#根据excel数据绘制坐标图的方法.doc
本文 PDF 下载地址:C#根据excel数据绘制坐标图的方法.pdf
留言与评论(共有 0 条评论) |