马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

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

[复制链接]
查看3034 | 回复2 | 2019-11-8 09:42:20 | 显示全部楼层 |阅读模式

场景

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

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

当ZedGraph的图形渲染完成后,添加设置全部曲线颜色功能。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的步调猿
获取编程相干电子书、教程推送与免费下载。

实现

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

  1. zgc.ContextMenuBuilder -= zgc_ContextMenuBuilder; //上下文菜单生成事件订阅
  2. zgc.ContextMenuBuilder += zgc_ContextMenuBuilder; //上下文菜单生成事件订阅
复制代码

然后在订阅的方法中

  1. private static void zgc_ContextMenuBuilder(ZedGraphControl control, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
  2. {
  3. #region 扩展上下文菜单
  4. #region 图形选项菜单
  5. ToolStripMenuItem mnuChartOption = new ToolStripMenuItem(); //新建菜单项对象
  6. mnuChartOption.Name = "set_curve_color";
  7. mnuChartOption.Text = "设置曲线颜色";
  8. //点击弹出图形选项对话框
  9. mnuChartOption.Click += delegate(object sender, EventArgs e)
  10. {
  11. if (curveModelList == null || curveModelList.Count == 0)
  12. {
  13. XtraMessageBox.Show("当前没有曲线");
  14. }
  15. else
  16. {
  17. FrmSetCurveColor setCurveColor = new FrmSetCurveColor();
  18. DialogResult result = setCurveColor.ShowDialog(); //表现Dialog
  19. if (result == DialogResult.OK)
  20. {
  21. TriggerSetCurveColor();
  22. XtraMessageBox.Show("设置曲线颜色乐成");
  23. }
  24. }
  25. };
  26. menuStrip.Items.Add(mnuChartOption); //把图形选项对话框添加到上下文菜单中
  27. #endregion
  28. #endregion
  29. #region 汉化上下文菜单中的菜单项
  30. foreach (ToolStripMenuItem item in menuStrip.Items)
  31. {
  32. switch (item.Name)
  33. {
  34. case "copied_to_clip":
  35. item.Text = @"复制到剪贴板";
  36. break;
  37. case "copy":
  38. item.Text = @"复制";
  39. break;
  40. case "page_setup":
  41. item.Text = @"页面设置...";
  42. break;
  43. case "print":
  44. item.Text = @"打印...";
  45. break;
  46. case "save_as":
  47. item.Text = @"另存图表...";
  48. break;
  49. case "set_default":
  50. item.Text = @"规复默认巨细";
  51. break;
  52. case "show_val":
  53. item.Text = @"表现节点数值";
  54. break;
  55. case "title_def":
  56. item.Text = @"标题";
  57. break;
  58. case "undo_all":
  59. item.Text = @"还原全部缩放";
  60. break;
  61. case "unpan":
  62. item.Text = @"还原上一步缩放";
  63. break;
  64. case "unzoom":
  65. item.Text = @"还原缩放";
  66. break;
  67. case "set_curve_color":
  68. item.Text = @"设置曲线颜色";
  69. break;
  70. }
  71. }
  72. #endregion
  73. }
复制代码

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

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

此时要根据通报的曲线的Model的list去生成当前已有曲线的设置的panel。

那么当前已经有多少个曲线是怎样通报的?

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

  1. public static List<CurveModel> curveModelList= new List<CurveModel>();
复制代码

此中CurveModel是自界说的存取曲线属性的Model类

  1. public class CurveModel
  2. {
  3. /// <summary>
  4. /// 曲线序号
  5. /// </summary>
  6. private int curveIndex;
  7. public int CurveIndex
  8. {
  9. get { return curveIndex; }
  10. set { curveIndex = value; }
  11. }
  12. /// <summary>
  13. /// 源文件
  14. /// </summary>
  15. private string originFile;
  16. public string OriginFile
  17. {
  18. get { return originFile; }
  19. set { originFile = value; }
  20. }
  21. /// <summary>
  22. /// 源文件完备路径
  23. /// </summary>
  24. private string originFileFullPath;
  25. public string OriginFileFullPath
  26. {
  27. get { return originFileFullPath; }
  28. set { originFileFullPath = value; }
  29. }
  30. /// <summary>
  31. /// 属性名
  32. /// </summary>
  33. private string attributeName;
  34. public string AttributeName
  35. {
  36. get { return attributeName; }
  37. set { attributeName = value; }
  38. }
  39. /// <summary>
  40. /// 颜色
  41. /// </summary>
  42. private string color;
  43. public string Color
  44. {
  45. get { return color; }
  46. set { color = value; }
  47. }
  48. }
复制代码

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

  1. curveModelList.Clear();
  2. int curveIndex = 0;
  3. for (int k = 0; k < compTestDataList.Count; k++)
  4. {
  5. //循环添加曲线
  6. for (int i = 0; i < yList.Count; i++)
  7. {
  8. SymbolType symbolType = GetCurveSimple(yList[i].CurveType); //根据设置文件中曲线符号范例返回曲线符号标识
  9. list = SetCurveText(interval, xAttribute.TitleKey, yList[i].TitleKey, compTestDataList[k]);
  10. LineItem myCurve = myPane.AddCurve(yList[i].Title, list, System.Drawing.ColorTranslator.FromHtml(yList[i].Color), symbolType);
  11. myCurve = SetCurveType(myCurve,symbolType, yList[i].Type, yList[i].Color); //根据设置文件设置曲线范例
  12. myCurve.YAxisIndex = i; //很关键,对应使用谁人坐标值
  13. CurveModel curveModel = new CurveModel();
  14. string originFile = compTestDataList[k].ThisDataFile;
  15. curveModel.OriginFile = originFile.Substring(originFile.LastIndexOf("\") + 1);
  16. curveModel.CurveIndex = curveIndex;
  17. curveModel.OriginFileFullPath = originFile;
  18. curveModel.AttributeName = yList[i].Title;
  19. curveModel.Color = yList[i].Color;
  20. curveModelList.Add(curveModel);
  21. curveIndex++;
  22. }
  23. }
复制代码

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

  1. private void FrmSetCurveColor_Load(object sender, EventArgs e)
  2. {
  3. curveModelList.Clear();
  4. curveModelList = ChartCompareHelper.curveModelList;
  5. initSetCurveColorPane(curveModelList);
  6. }
复制代码

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

  1. private void initSetCurveColorPane(List<CurveModel> curveModelList)
  2. {
  3. int panelControlHeight = 20;
  4. int panelControlWidth = 50;
  5. foreach(CurveModel curve in curveModelList)
  6. {
  7. PanelControl panelControl = new PanelControl();
  8. #region 生成一个Label标识X轴
  9. //生成一个Label标识X轴
  10. LabelControl label = new LabelControl();
  11. label.Text = "第" + (curve.CurveIndex + 1).ToString() + "条曲线(" + curve.OriginFile + "的" + curve.AttributeName + ")的颜色为:";
  12. label.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE, Global.GROUP_VERTICAL_DISTANCE);
  13. label.Parent = panelControl;
  14. #endregion
  15. #region 生成一个颜色选择器
  16. //添加颜色选择器
  17. ColorEdit colorEditX = new ColorEdit();
  18. colorEditX.Text = curve.Color;
  19. colorEditX.Width = Global.CHART_OPTION_WIDTH;
  20. colorEditX.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE * 2 + label.Width, Global.GROUP_VERTICAL_DISTANCE);
  21. colorEditX.Parent = panelControl;
  22. colorEditX.Name = "colorEdit"+curve.CurveIndex;
  23. #endregion
  24. #region 设置X轴Panel的位置以及属性
  25. //设置panel的宽度
  26. panelControl.Width = label.Width + colorEditX.Width + Global.NOGROUP_HORIZONTAL_DISTANCE * 3;
  27. //设置panel的高度
  28. panelControl.Height = label.Height + Global.GROUP_VERTICAL_DISTANCE * 2;
  29. panelControlHeight = panelControl.Height;
  30. panelControlWidth = panelControl.Width;
  31. //设置panel的位置
  32. panelControl.Location = new Point(Global.NOGROUP_HORIZONTAL_DISTANCE, (panelControl.Height * curve.CurveIndex) + (Global.GROUP_VERTICAL_DISTANCE *(curve.CurveIndex + 1)));
  33. //设置Name
  34. panelControl.Name = "panelControl" + curve.CurveIndex;
  35. //将panel添加到当前窗体
  36. this.Controls.Add(panelControl);
  37. #endregion
  38. }
  39. this.Width = panelControlWidth + Global.NOGROUP_HORIZONTAL_DISTANCE * 4;
  40. this.Height = (curveModelList.Count + 1) * panelControlHeight + (curveModelList.Count + 2) * Global.GROUP_VERTICAL_DISTANCE + +this.panelControl1.Height;
  41. }
复制代码

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

  1. private void simpleButton2_Click_1(object sender, EventArgs e)
  2. {
  3. for (int i = 0; i < curveModelList.Count; i++)
  4. {
  5. ColorEdit colorEditY = this.Controls.Find("colorEdit" + i, true)[0] as ColorEdit;
  6. curveModelList[i].Color = colorEditY.Text;
  7. }
  8. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  9. }
复制代码

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

  1. List<CurveModel> curveModelList = new List<CurveModel>();
复制代码

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

  1. if (result == DialogResult.OK)
  2. {
  3. TriggerSetCurveColor();
  4. XtraMessageBox.Show("设置曲线颜色乐成");
  5. }
复制代码

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

  1. public static void TriggerSetCurveColor()
  2. {
  3. if (OnSetCurveColor != null)
  4. {
  5. OnSetCurveColor(null, System.EventArgs.Empty);
  6. }
  7. }
复制代码

此事件声明

  1. public static event EventHandler OnSetCurveColor;
复制代码

事件会在图形地点的页面举行订阅

  1. ChartCompareHelper.OnSetCurveColor + = ChartCompareHelper_OnSetCurveColor;
复制代码

订阅实行的方法

  1. private void ChartCompareHelper_OnSetCurveColor(object sender, EventArgs e)
  2. {
  3. ChartCompareHelper.SetCurveColor(this.zedGraphControl1);
  4. }
复制代码

此方法通报当前ZedGraph对象

  1. public static void SetCurveColor(ZedGraphControl zedGraphControl)
  2. {
  3. for (int i = 0; i < curveModelList.Count;i++)
  4. {
  5. zedGraphControl.GraphPane.CurveList[i].Color = System.Drawing.ColorTranslator.FromHtml(curveModelList[i].Color);
  6. }
  7. zedGraphControl.GraphPane.AxisChange();
  8. zedGraphControl.Refresh();
  9. zedGraphControl.Invalidate();
  10. }
复制代码

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

西瓜啦啦 | 2020-8-26 17:03:47 | 显示全部楼层
关注公众号福彩3Dhttps://1680380.com/view/fc3D/index.html
霸道的步调猿幸运飞艇https://1680380.com/view/xingyft/pk10kai.html获取编程相干电子书、教程推送与免费下载

*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则