当前位置: 移动技术网 > IT编程>开发语言>c# > WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限

WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限

2019年08月28日  | 移动技术网IT编程  | 我要评论

场景

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

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

在上面实现曲线相关属性的设置的基础上,要能修改曲线图的x轴以及y轴的上限和下限。

效果

实现

设置y轴的上限

拖拽按钮,双击进入其点击事件

 private void button8_click(object sender, eventargs e)
        {
            mypane.yaxis.scale.max = 1500;
            //更新图表
            zedgraphcontrol1.invalidate();

        }

 

设置y轴的下限

拖拽按钮,双击进入其点击事件

 private void button9_click(object sender, eventargs e)
        {
            mypane.yaxis.scale.min = -1500;
            //更新图表
            zedgraphcontrol1.invalidate();
        }

 

设置x轴的上限

拖拽按钮,双击进入其点击事件

 

private void button10_click(object sender, eventargs e)
        {
            mypane.xaxis.scale.max = 60;
            //更新图表
            zedgraphcontrol1.invalidate();
        }

 

设置x轴的下限

拖拽按钮,双击进入其点击事件

private void button11_click(object sender, eventargs e)
        {
            mypane.xaxis.scale.min = -5;
            //更新图表
            zedgraphcontrol1.invalidate();
        }

 

完整示例代码

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
using zedgraph;

namespace zedgraphtest
{
    public partial class form1 : form
    {

        graphpane mypane = new graphpane();
        public form1()
        {
            initializecomponent();
            //form1初始化后创建设置控件的方法并将当前zedgraph控件传递
            createpane(zedgraphcontrol1);
        }

        //需要引入命名空间--using zedgraph;
        public void createpane(zedgraphcontrol zgc) 
        {
            mypane = zgc.graphpane;

            
            //设置图表标题 和 x y 轴标题
            mypane.title.text = "霸道测试标题";

            mypane.xaxis.title.text = "x轴标题";

            mypane.yaxis.title.text = "y轴标题";

            //更改标题的字体

            fontspec myfont = new fontspec("arial",16,color.black,false,false,false);

            mypane.xaxis.title.fontspec = myfont;

            mypane.yaxis.title.fontspec = myfont;

            // 造一些数据,pointpairlist里有数据对x,y的数组

            random y = new random();

            pointpairlist list1 = new pointpairlist();

            for (int i = 0; i < 50; i++)
            {

                double x = i;

                double y1 = y.nextdouble() * 1000;

                list1.add(x, y1); //添加一组数据

            }

            // 用list1生产一条曲线,标注是“曲线1”
            //symboltype,枚举代表曲线的样式
            //square = 0,
            //diamond = 1,
            //triangle = 2,
            //circle = 3,
            //xcross = 4,
            //plus = 5,
            //star = 6,
            //triangledown = 7,
            //hdash = 8,
            //vdash = 9,
            //userdefined = 10,
            //default = 11,
            //none = 12,
            lineitem mycurve = mypane.addcurve("曲线1", list1, color.red, symboltype.none);

            //填充图表颜色
            mypane.fill = new fill(color.white, color.lightgray, 45.0f);

            //以上生成的图标x轴为数字,下面将转换为日期的文本

            string[] labels = new string[50];

            for (int i = 0; i < 50; i++)
            {

                labels[i] = system.datetime.now.adddays(i).toshortdatestring();

            }

            #region 坐标轴属性设置
            //x轴类型
            mypane.xaxis.type = axistype.text;   
            //显示小刻度 是false则看不到效果
            mypane.xaxis.minorgrid.isvisible = true;
            //线的颜色
            mypane.xaxis.color = color.black;
            //点线中点与点之间的间隔
            mypane.xaxis.minorgrid.dashoff = 1f;
            //点线中点的长度
            mypane.xaxis.minorgrid.dashon = 1f;
            //画笔宽度
            mypane.xaxis.minorgrid.penwidth = 1f;
            //
          
            #endregion

            #region 坐标轴上刻度线设置
            //x轴文本取值
            mypane.xaxis.scale.textlabels = labels; 
            //第一个刻度从哪里开始
            mypane.xaxis.scale.basetic = 1;
            //刻度值的字体属性
            mypane.xaxis.scale.fontspec = myfont;
           
         
            #endregion
            //画到zedgraphcontrol1控件中,此句必加

            zgc.axischange();//在数据变化时绘图

            //更新图表

            zedgraphcontrol1.invalidate();

            //重绘控件

            refresh();

            #region 属性设置
            //是否允许横向缩放
            this.zedgraphcontrol1.isenablehzoom = true;
            //是否允许纵向缩放
            this.zedgraphcontrol1.isenablevzoom = true;
            //是否允许缩放
            this.zedgraphcontrol1.isenablezoom = true;
            //是否显示右键菜单
            this.zedgraphcontrol1.isshowcontextmenu = true;
            //复制图像时是否显示提示信息
            this.zedgraphcontrol1.isshowcopymessage = true;
            //鼠标在图表上移动时是否显示鼠标所在点对应的坐标 默认为false
            this.zedgraphcontrol1.isshowcursorvalues = true;
            //是否显示横向滚动条
            this.zedgraphcontrol1.isshowhscrollbar = true;
            //是否显示纵向滚动条
            this.zedgraphcontrol1.isshowvscrollbar = true;
            //鼠标经过图表上的点时是否显示该点所对应的值 默认为false 
            this.zedgraphcontrol1.isshowpointvalues = true;
            //使用滚轮时以鼠标所在点为中心进行缩放还是以图形中心进行缩放
            //this.zedgraphcontrol1.iszoomonmousecenter = true;
            #endregion
            //修改右键为中文菜单
            this.zedgraphcontrol1.contextmenubuilder += mycontextmenubuilder;
        }
        private void zedgraphcontrol1_load(object sender, eventargs e)
        {

        }

        /// <summary>
        /// 打印预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_click(object sender, eventargs e)
        {
            this.zedgraphcontrol1.doprintpreview();
        }

        //复制到剪切板
        private void button2_click(object sender, eventargs e)
        {
            //ture代表复制成功提示
            this.zedgraphcontrol1.copy(true);
        }

        /// <summary>
        /// 获取图片并保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_click(object sender, eventargs e)
        {
            //获取图像
            image image = this.zedgraphcontrol1.getimage();
            //保存照片吗,指定保存路径
            image.save(@"c:\users\haohao\desktop\1.png");
            //弹窗提示
            messagebox.show("保存成功");

        }

        /// <summary>
        /// 显示另存为对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_click(object sender, eventargs e)
        {
            this.zedgraphcontrol1.saveas();
        }

        /// <summary>
        /// 另存为bmp文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_click(object sender, eventargs e)
        {
            this.zedgraphcontrol1.saveasbitmap();
        }
        /// <summary>
        /// 另存为emf文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_click(object sender, eventargs e)
        {
            this.zedgraphcontrol1.saveasemf();
        }
        /// <summary>
        /// 一键复原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_click(object sender, eventargs e)
        {
            //一键复原缩放
            this.zedgraphcontrol1.zoomoutall(mypane);
        }


        //右击菜单变中文
        private static void mycontextmenubuilder(zedgraphcontrol control, contextmenustrip menustrip,
                    point mousept, zedgraphcontrol.contextmenuobjectstate objstate)
        {
            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 "x_title_def":
                        item.text = @"x 轴";
                        break;
                    case "y_title_def":
                        item.text = @"y 轴";
                        break;

                }
            }
        }

        private void button8_click(object sender, eventargs e)
        {
            mypane.yaxis.scale.max = 1500;
            //更新图表
            zedgraphcontrol1.invalidate();

        }

        //设置y轴下限
        private void button9_click(object sender, eventargs e)
        {
            mypane.yaxis.scale.min = -1500;
            //更新图表
            zedgraphcontrol1.invalidate();
        }
        /// <summary>
        /// 设置x轴上限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_click(object sender, eventargs e)
        {
            mypane.xaxis.scale.max = 60;
            //更新图表
            zedgraphcontrol1.invalidate();
        }
        /// <summary>
        /// 设置x轴下限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_click(object sender, eventargs e)
        {
            mypane.xaxis.scale.min = -5;
            //更新图表
            zedgraphcontrol1.invalidate();
        }
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网