欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Winform中设置ZedGraph当前所有曲线的颜色

程序员文章站 2023-11-08 14:22:34
场景 Winforn中设置ZedGraph曲线图的属性、坐标轴属性、刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100112573 当ZedGraph的图形渲染完成后,添加设置所有曲线颜色功能。 注: 博客主页: h ......

场景

winforn中设置zedgraph曲线图的属性、坐标轴属性、刻度属性:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100112573

当zedgraph的图形渲染完成后,添加设置所有曲线颜色功能。

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

首先在zedgraph的右键菜单中添加设置曲线颜色选项,在面板初始化的方法中订阅右键事件。

zgc.contextmenubuilder -= zgc_contextmenubuilder;     //上下文菜单生成事件订阅
zgc.contextmenubuilder += zgc_contextmenubuilder;     //上下文菜单生成事件订阅

然后在订阅的方法中

private static void zgc_contextmenubuilder(zedgraphcontrol control, contextmenustrip menustrip, point mousept, zedgraphcontrol.contextmenuobjectstate objstate)
        {
            #region 扩展上下文菜单

            #region 图形选项菜单

            toolstripmenuitem mnuchartoption = new toolstripmenuitem();     //新建菜单项对象
            mnuchartoption.name = "set_curve_color";
            mnuchartoption.text = "设置曲线颜色";
            //点击弹出图形选项对话框
            mnuchartoption.click += delegate(object sender, eventargs e)
            {
                if (curvemodellist == null || curvemodellist.count == 0)
                {
                    xtramessagebox.show("当前没有曲线");
                }
                else
                {
                    frmsetcurvecolor setcurvecolor = new frmsetcurvecolor();
                    dialogresult result = setcurvecolor.showdialog();                               //显示dialog
                    if (result == dialogresult.ok)
                    {
                        triggersetcurvecolor();
                        xtramessagebox.show("设置曲线颜色成功");
                    }
                }
            };
            menustrip.items.add(mnuchartoption);        //把图形选项对话框添加到上下文菜单中

             #endregion

 

             #endregion

 

            #region 汉化上下文菜单中的菜单项

            foreach (toolstripmenuitem item in menustrip.items)
            {
                switch (item.name)
                {
                    case "copied_to_clip":
                        item.text = @"复制到剪贴板";
                        break;
                    case "copy":
                        item.text = @"复制";
                        break;
                    case "page_setup":
                        item.text = @"页面设置...";
                        break;
                    case "print":
                        item.text = @"打印...";
                        break;
                    case "save_as":
                        item.text = @"另存图表...";
                        break;
                    case "set_default":
                        item.text = @"恢复默认大小";
                        break;
                    case "show_val":
                        item.text = @"显示节点数值";
                        break;
                    case "title_def":
                        item.text = @"标题";
                        break;
                    case "undo_all":
                        item.text = @"还原所有缩放";
                        break;
                    case "unpan":
                        item.text = @"还原上一步缩放";
                        break;
                    case "unzoom":
                        item.text = @"还原缩放";
                        break;
                    case "set_curve_color":
                        item.text = @"设置曲线颜色";
                        break;
                }

             }

            #endregion

         }

 

 此时会汉化右键菜单并添加右键选项,并且其点击事件是打开设置曲线颜色面板。

 在设置曲线颜色面板的代码中,在其load方法中调用初始化面板的方法,

  此时要根据传递的曲线的model的list去生成当前已有曲线的设置的panel。

  那么当前已经有多少个曲线是如何传递的?

  在前面zedgraph生成曲线的地方所在的工具类中声明一个全局的list

 

public static list<curvemodel> curvemodellist= new list<curvemodel>();

其中curvemodel是自定义的存取曲线属性的model类

 public class curvemodel 
    {
        /// <summary>
        /// 曲线序号
        /// </summary>
        private int curveindex;

        public int curveindex
        {
            get { return curveindex; }
            set { curveindex = value; }

         }

        /// <summary>
        /// 源文件
        /// </summary>
        private string originfile;

        public string originfile
        {
            get { return originfile; }
            set { originfile = value; }

         }

        /// <summary>
        /// 源文件完整路径
        /// </summary>
        private string originfilefullpath;

        public string originfilefullpath
        {
            get { return originfilefullpath; }
            set { originfilefullpath = value; }

         }

        /// <summary>
        /// 属性名
        /// </summary>
        private string attributename;

        public string attributename
        {
            get { return attributename; }
            set { attributename = value; }

         }

        /// <summary>
        /// 颜色
        /// </summary>
        private string color;
        
        public string color
        {
            get { return color; }
            set { color = value; }
        }

     }

 

声明完list后,其中生成曲线的地方也在这个工具类中

 

curvemodellist.clear();

 

 int curveindex = 0;
                for (int k = 0; k < comptestdatalist.count; k++)
                {
                    //循环添加曲线
                    for (int i = 0; i < ylist.count; i++)
                    {
                        symboltype symboltype = getcurvesimple(ylist[i].curvetype);                                //根据配置文件中曲线符号类型返回曲线符号标识
                        list = setcurvetext(interval, xattribute.titlekey, ylist[i].titlekey, comptestdatalist[k]);
                        lineitem mycurve = mypane.addcurve(ylist[i].title, list, system.drawing.colortranslator.fromhtml(ylist[i].color), symboltype);
                        mycurve = setcurvetype(mycurve,symboltype, ylist[i].type, ylist[i].color);                         //根据配置文件设置曲线类型
                        mycurve.yaxisindex = i;                                                                 //很关键,对应使用那个坐标值
                        curvemodel curvemodel = new curvemodel();
                        string originfile = comptestdatalist[k].thisdatafile;
                        curvemodel.originfile = originfile.substring(originfile.lastindexof("\\") + 1);
                        curvemodel.curveindex = curveindex;
                        curvemodel.originfilefullpath = originfile;
                        curvemodel.attributename = ylist[i].title;
                        curvemodel.color = ylist[i].color;
                        curvemodellist.add(curvemodel);
                        curveindex++;
                    }
                }

 

这样在生成曲线时就将当前图形的所有曲线的属性存到一个工具类中全局的一个list中,然后在设置曲线颜色面板中获取这个list。

 

private void frmsetcurvecolor_load(object sender, eventargs e)
       {
             curvemodellist.clear();
             curvemodellist = chartcomparehelper.curvemodellist;
             initsetcurvecolorpane(curvemodellist);

        }

 

然后在使用这个list去初始化设置颜色的面板

 

private void initsetcurvecolorpane(list<curvemodel> curvemodellist)
        {
            int panelcontrolheight = 20;
            int panelcontrolwidth = 50;
            foreach(curvemodel curve in curvemodellist)
            {
                panelcontrol panelcontrol = new panelcontrol();
                #region 生成一个label标识x轴
                //生成一个label标识x轴
                labelcontrol label = new labelcontrol();
                label.text = "第" + (curve.curveindex + 1).tostring() + "条曲线(" + curve.originfile + "的" + curve.attributename + ")的颜色为:";
                label.location = new point(global.nogroup_horizontal_distance, global.group_vertical_distance);
                label.parent = panelcontrol;
                #endregion


                #region 生成一个颜色选择器

                //添加颜色选择器
                coloredit coloreditx = new coloredit();
                coloreditx.text = curve.color;
                coloreditx.width = global.chart_option_width;
                coloreditx.location = new point(global.nogroup_horizontal_distance * 2 + label.width, global.group_vertical_distance);
                coloreditx.parent = panelcontrol;
                coloreditx.name = "coloredit"+curve.curveindex;

                #endregion


                #region 设置x轴panel的位置以及属性

                
                //设置panel的宽度
                panelcontrol.width = label.width + coloreditx.width + global.nogroup_horizontal_distance * 3;
                //设置panel的高度
                panelcontrol.height = label.height + global.group_vertical_distance * 2;
                panelcontrolheight = panelcontrol.height;
                panelcontrolwidth = panelcontrol.width;
                //设置panel的位置
                panelcontrol.location = new point(global.nogroup_horizontal_distance, (panelcontrol.height * curve.curveindex) + (global.group_vertical_distance *(curve.curveindex + 1)));
                //设置name
                panelcontrol.name = "panelcontrol" + curve.curveindex;
                //将panel添加到当前窗体
                this.controls.add(panelcontrol);

                #endregion
            }

            this.width = panelcontrolwidth + global.nogroup_horizontal_distance * 4;

            this.height = (curvemodellist.count + 1) * panelcontrolheight + (curvemodellist.count + 2) * global.group_vertical_distance + +this.panelcontrol1.height;
        }

 

Winform中设置ZedGraph当前所有曲线的颜色

然后设置颜色后,点击保存后在确定按钮的点击事件中

 private void simplebutton2_click_1(object sender, eventargs e)
        {
            for (int i = 0; i < curvemodellist.count; i++)
            {
                coloredit coloredity = this.controls.find("coloredit" + i, true)[0] as coloredit;
                curvemodellist[i].color = coloredity.text;
            }


            this.dialogresult = system.windows.forms.dialogresult.ok;
        }

 

再将设置的颜色属性赋值给当前设置颜色面板作用域的list

list<curvemodel> curvemodellist = new list<curvemodel>();

此时窗体返回ok,再回到右键打开窗口的地方

 

if (result == dialogresult.ok)
                    {
                        triggersetcurvecolor();
                        xtramessagebox.show("设置曲线颜色成功");
                    }

此时会触发设置曲线颜色的方法

 public static void triggersetcurvecolor()
        {
            if (onsetcurvecolor != null)
            {
                onsetcurvecolor(null, system.eventargs.empty);
            }

         }

 

此事件声明

public static event eventhandler onsetcurvecolor;

 

 事件会在图形所在的页面进行订阅

 

chartcomparehelper.onsetcurvecolor + = chartcomparehelper_onsetcurvecolor;

 

订阅执行的方法

private void chartcomparehelper_onsetcurvecolor(object sender, eventargs e)
        {
            chartcomparehelper.setcurvecolor(this.zedgraphcontrol1);

          }

 

此方法传递当前zedgraph对象

 

 public static void setcurvecolor(zedgraphcontrol zedgraphcontrol)
        {
            for (int i = 0; i < curvemodellist.count;i++)
            {
                zedgraphcontrol.graphpane.curvelist[i].color = system.drawing.colortranslator.fromhtml(curvemodellist[i].color);
            }
            
            zedgraphcontrol.graphpane.axischange();
            zedgraphcontrol.refresh();

             zedgraphcontrol.invalidate();

      }

然后按顺序设置曲线颜色。