Winform中跨窗体设置Zedgraph的属性并刷新曲线图
程序员文章站
2022-04-15 08:58:48
场景 在使用ZedGraph时,经常有图形选项功能,设置曲线图相关属性后, 点击保存会设置另一个窗体的属性并刷新图。 效果 实现 在设置图形的选项的类中,声明委托和事件 在当前窗体的确定按钮的点击事件中 将相关曲线图的Y轴以及X轴的相关属性存到xml配置文件中,然后调用方法 去刷新曲线图,使其重新加 ......
场景
在使用zedgraph时,经常有图形选项功能,设置曲线图相关属性后,
点击保存会设置另一个窗体的属性并刷新图。
效果
实现
在设置图形的选项的类中,声明委托和事件
//委托的定义 public delegate void reloadpaneldelegete(); //事件声明 public static event reloadpaneldelegete reloadpanelevent;
在当前窗体的确定按钮的点击事件中
将相关曲线图的y轴以及x轴的相关属性存到xml配置文件中,然后调用方法
去刷新曲线图,使其重新加载。
private void confirmbbutton_click(object sender, eventargs e) { #region 获取x轴的属性并赋值给xaxismodel //对x轴的属性配置文件进行保存 //根据控件的 system.windows.forms.control.name 属性搜索控件并生成由所有匹配项组成的数组。 //如果搜索所有子控件,则为 true;否则为 false。 xaxismodel xaxismodel = new xaxismodel(); //获取标题 radiogroup radiogroupx = this.controls.find("radiogroupx", true)[0] as radiogroup; xaxismodel.title = radiogroupx.text; //获取下限 textedit mintexteditx = this.controls.find("mintexteditx", true)[0] as textedit; xaxismodel.min = double.parse(mintexteditx.text); //获取上限 textedit maxtexteditx = this.controls.find("maxtexteditx", true)[0] as textedit; xaxismodel.max = double.parse(maxtexteditx.text); //获取颜色 coloredit coloreditx = this.controls.find("coloreditx", true)[0] as coloredit; xaxismodel.color = coloreditx.text; #endregion #region 将x轴的属性存到配置文件中 boolean iswritex = chartoptionhelper.setxaxisattribute(xaxismodel); #endregion #region 获取y轴属性并赋值给list<yaxismodel> list<yaxismodel> yaxismodellist = new list<yaxismodel>(); for (int i = 1; i <= yaxiscount; i++) { yaxismodel yaxismodel = new yaxismodel(); //获取标题 radiogroup radiogroupy = this.controls.find("radiogroupy" + i, true)[0] as radiogroup; yaxismodel.title = radiogroupy.text; //获取下限 textedit mintextedity = this.controls.find("mintextedity" + i, true)[0] as textedit; yaxismodel.min = double.parse(mintextedity.text); //获取上限 textedit maxtextedity = this.controls.find("maxtextedity" + i, true)[0] as textedit; yaxismodel.max = double.parse(maxtextedity.text); //获取颜色 coloredit coloredity = this.controls.find("coloredity" + i, true)[0] as coloredit; yaxismodel.color = coloredity.text; yaxismodellist.add(yaxismodel); } #endregion #region 将y轴的属性存到配置文件中 boolean iswritey = chartoptionhelper.setyaxisattribute(yaxismodellist); #endregion #region 弹出对话框并设置点击确定之后的事件 if (iswritex == true && iswritey == true) { if (this.confirm("保存成功") == dialogresult.ok) { //将是否第一次打开软件的标识量改为0,,下次打开直接读取配置文件 global.isfirstopen = 0; if (reloadpanelevent != null) { reloadpanelevent(); } this.dispose(); } } else { devexpress.xtraeditors.xtramessagebox.show("保存失败", "提示", messageboxbuttons.okcancel, messageboxicon.question); } #endregion }
然后在曲线图显示的窗体中订阅事件
dialog.chartoption.reloadpanelevent += new dialog.chartoption.reloadpaneldelegete(reloadpanel);
其中左边的类是上面图形选项的类,右边的类是窗体显示的类
然后在窗体显示的类中新建图上面reloadpaneldelegete的参数同名的方法reloadpanel实现曲线图的刷新。
public void reloadpanel() { //声明pane对象 graphpane mypane = new graphpane(); //获取控件的pane mypane = this.zedgraphcontrol1.graphpane; #region 从配置文件中加载并设置x轴属性 //获取x轴属性 xaxismodel xattribute = chartoptionhelper.getxaxisattribute(global.radio_group_yaxis_filepath_x); mypane.xaxis.title.text = xattribute.title; mypane.xaxis.color = system.drawing.colortranslator.fromhtml(xattribute.color); mypane.xaxis.scale.min = xattribute.min; mypane.xaxis.scale.max = xattribute.max; #endregion //获取配置文件加载的y轴的list list<yaxismodel> yloadlist = chartoptionhelper.getyaxislist(global.radio_group_yaxis_filepath); //用来存储标题不为空的list list<yaxismodel> ylist = new list<yaxismodel>(); foreach (yaxismodel yaxismodel in yloadlist) { if (!yaxismodel.title.equals("")) { ylist.add(yaxismodel); } } //根据数量生成y轴 //说明选择全是空标题 if (ylist.count == 0) { //设置x轴标题 mypane.xaxis.title.text = "时间"; //x轴线的颜色 mypane.xaxis.color = color.black; //设置第一条y轴标题 mypane.yaxis.title.text = "y1"; this.zedgraphcontrol1.invalidate(); } else if (ylist.count == 1) { //设置第一条y轴标题 mypane.yaxis.title.text = ylist[0].title; mypane.yaxis.scale.min = ylist[0].min; mypane.yaxis.scale.max = ylist[0].max; mypane.yaxis.color = system.drawing.colortranslator.fromhtml(ylist[0].color); mypane.y2axis.title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[0].color); mypane.yaxis.scale.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[0].color); this.zedgraphcontrol1.invalidate(); } else if (ylist.count == 2) { //设置第一条y轴标题 mypane.yaxis.title.text = ylist[0].title; mypane.yaxis.scale.min = ylist[0].min; mypane.yaxis.scale.max = ylist[0].max; mypane.yaxis.color = system.drawing.colortranslator.fromhtml(ylist[0].color); mypane.yaxis.color = system.drawing.colortranslator.fromhtml(ylist[0].color); mypane.y2axis.title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[0].color); //第二条y轴标题 mypane.y2axis.title.text = ylist[1].title; //第二条y轴下限 mypane.y2axis.scale.min = ylist[1].min; //第二条y轴上限 mypane.y2axis.scale.max = ylist[1].max; //设置颜色 mypane.y2axis.color = system.drawing.colortranslator.fromhtml(ylist[1].color); //设置刻度线字体颜色 mypane.y2axis.scale.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[1].color); //设置标题字体颜色 mypane.y2axis.title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[1].color); //标题大小 mypane.y2axis.title.fontspec.size = global.font_size; mypane.y2axis.title.fontspec.family = global.font_family; mypane.y2axis.title.fontspec.isbold = false; //显示小刻度 是false则看不到效果 mypane.y2axis.minorgrid.isvisible = true; //设置y轴刻度线字体大小 mypane.y2axis.scale.fontspec.size = global.font_size; //让第二条y轴显示 mypane.y2axis.isvisible = true; this.zedgraphcontrol1.invalidate(); } //否则就是新增第三条及以上 else { //设置第一条y轴标题 mypane.yaxis.title.text = ylist[0].title; mypane.yaxis.scale.min = ylist[0].min; mypane.yaxis.scale.max = ylist[0].max; mypane.yaxis.scale.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[0].color); mypane.yaxis.color = system.drawing.colortranslator.fromhtml(ylist[0].color); //第二条y轴标题 mypane.y2axis.title.text = ylist[1].title; mypane.y2axis.title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[0].color); //第二条y轴下限 mypane.y2axis.scale.min = ylist[1].min; //第二条y轴上限 mypane.y2axis.scale.max = ylist[1].max; //设置颜色 mypane.y2axis.color = system.drawing.colortranslator.fromhtml(ylist[1].color); //设置刻度线字体颜色 mypane.y2axis.scale.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[1].color); //设置标题字体颜色 mypane.y2axis.title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[1].color); //标题大小 mypane.y2axis.title.fontspec.size = global.font_size; mypane.y2axis.title.fontspec.family = global.font_family; mypane.y2axis.title.fontspec.isbold = false; //显示小刻度 是false则看不到效果 mypane.y2axis.minorgrid.isvisible = true; //设置y轴刻度线字体大小 mypane.y2axis.scale.fontspec.size = global.font_size; //让第二条y轴显示 mypane.y2axis.isvisible = true; #region 第一次加载其他的y轴,要在y轴的list中add进y轴 //说明是第一次加载其他的y轴,要在y轴的list中add进y轴 if (mypane.yaxislist.count == 1) { //循环剩下的y轴并显示 for (int i = 2; i < ylist.count; i++) { yaxis yaxisnew = new yaxis(ylist[i].title); yaxisnew.scale.min = ylist[i].min; yaxisnew.scale.max = ylist[i].max; //设置y轴颜色 yaxisnew.color = system.drawing.colortranslator.fromhtml(ylist[i].color); //设置刻度线字体颜色 yaxisnew.scale.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[i].color); //设置标题字体颜色 yaxisnew.title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[i].color); yaxisnew.type = axistype.text; //显示小刻度 是false则看不到效果 yaxisnew.minorgrid.isvisible = true; //关闭相反的tics,使y2 tics不会显示在y轴上 yaxisnew.majortic.isinside = true; yaxisnew.minortic.isinside = true; yaxisnew.majortic.isopposite = true; yaxisnew.minortic.isopposite = true; yaxisnew.title.fontspec.family = global.font_family; yaxisnew.title.fontspec.size = global.font_size; yaxisnew.title.fontspec.isbold = false; //设置y轴刻度线字体大小 yaxisnew.scale.fontspec.size = global.font_size; //添加到y轴的list mypane.yaxislist.add(yaxisnew); } } #endregion else { //循环剩下的y轴并显示 for (int i = 1; i < mypane.yaxislist.count; i++) { mypane.yaxislist[i].scale.min = ylist[i+1].min; mypane.yaxislist[i].scale.max = ylist[i + 1].max; //设置y轴颜色 mypane.yaxislist[i].color = system.drawing.colortranslator.fromhtml(ylist[i+1].color); //设置刻度线字体颜色 mypane.yaxislist[i].scale.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[i + 1].color); //设置标题字体颜色 mypane.yaxislist[i].title.fontspec.fontcolor = system.drawing.colortranslator.fromhtml(ylist[i + 1].color); mypane.yaxislist[i].type = axistype.text; //显示小刻度 是false则看不到效果 mypane.yaxislist[i].minorgrid.isvisible = true; //关闭相反的tics,使y2 tics不会显示在y轴上 mypane.yaxislist[i].majortic.isinside = true; mypane.yaxislist[i].minortic.isinside = true; mypane.yaxislist[i].majortic.isopposite = true; mypane.yaxislist[i].minortic.isopposite = true; mypane.yaxislist[i].title.fontspec.family = global.font_family; mypane.yaxislist[i].title.fontspec.size = global.font_size; mypane.yaxislist[i].title.fontspec.isbold = false; //设置y轴刻度线字体大小 mypane.yaxislist[i].scale.fontspec.size = global.font_size; } } this.zedgraphcontrol1.invalidate(); } }
注:
1.mypane是zedgraph的面板,其默认是有一条y轴对象的,为mypane.yaxis
2.如果要显示第二条y轴,zedgraph是默认存在第二条y轴的,为mypane.y2axis,不过就是默认不显示,要使其显示,需要mypane.y2axis.isvisible = true;
3.如果是三条以上y轴的话,就要使用
mypane.yaxislist.add(yaxisnew);
加入其y轴的list。
4.这里的需求是首次打开默认加载一条y轴,所以在首次加载时判断
mypane.yaxislist.count == 1,其自己的list是否为1,为1则代表是首次加载,下次再设置并加载时,其list就不为1了。所以需要是设置,而不是再往list中新增了。
5.需要注意的是,如果显示4条y轴的话,其mypane.yaxislist.count为3,,一条是不算在内的。
6.刷新曲线图的代码
this.zedgraphcontrol1.invalidate();