实例介绍
【实例截图】
【核心代码】
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 = "霸道测试标题"; //设置X轴标题 myPane.XAxis.Title.Text = "X轴标题"; //更改标题的字体 //FontSpec myFont = new FontSpec("Arial",16,Color.Black,false,false,false); //myPane.XAxis.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; #region Y轴设置 //设置第一条Y轴标题 myPane.YAxis.Title.Text = "第一条Y轴标题"; //第一条Y轴的标题 //myPane.YAxis.Title.FontSpec = myFont; //第二条Y轴标题 myPane.Y2Axis.Title.Text = "第二条Y轴标题"; //第二条Y轴字体 //myPane.Y2Axis.Title.FontSpec = myFont; //让第二条Y轴显示 myPane.Y2Axis.IsVisible = true; // 创建第三条Y轴 YAxis yAxis3 = new YAxis("第三条Y轴标题"); //添加到Y轴的list myPane.YAxisList.Add(yAxis3); yAxis3.Scale.FontSpec.FontColor = Color.Green; yAxis3.Title.FontSpec.FontColor = Color.Green; yAxis3.Color = Color.Green; // turn off the opposite tics so the Y2 tics don't show up on the Y axis yAxis3.MajorTic.IsInside = false; yAxis3.MinorTic.IsInside = false; yAxis3.MajorTic.IsOpposite = false; yAxis3.MinorTic.IsOpposite = false; // Align the Y2 axis labels so they are flush to the axis yAxis3.Scale.Align = AlignP.Inside; // 创建第四条Y轴 YAxis yAxis4 = new YAxis("第四条Y轴标题"); //添加到Y轴的list myPane.YAxisList.Add(yAxis4); yAxis4.Scale.FontSpec.FontColor = Color.Blue; yAxis4.Title.FontSpec.FontColor = Color.Blue; yAxis4.Color = Color.Blue; // turn off the opposite tics so the Y2 tics don't show up on the Y axis yAxis4.MajorTic.IsInside = false; yAxis4.MinorTic.IsInside = false; yAxis4.MajorTic.IsOpposite = false; yAxis4.MinorTic.IsOpposite = false; // Align the Y2 axis labels so they are flush to the axis yAxis4.Scale.Align = AlignP.Inside; #endregion #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; //新建Border对象 false 参数表示是否可见、颜色、宽度 Border border = new Border(false, Color.Black, 10); //设置曲线标签的边框 this.zedGraphControl1.GraphPane.Legend.Border = border; } 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(); } } }
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论