zoukankan      html  css  js  c++  java
  • zedGraph画多个图表

    起初是想到用多个zedGraph做。然后实际做的时候 zedGraph的同步一直做的不好。尤其当采样速度很快的时候很难保持好。甚至会出现界面假死和卡死的现象。

    因此想用其他解决方法:

    查看资料后 发现了zedGraph的MasterPane: 它允许一个zedGraph控件 绘制多张Graphpane。并且只要zedGraphControl1.IsSynchronizeXAxes = true;就能实现两张Graphpane的轴同步。因此只需控制一个zedGraph就好。高效方便许多。

    实验代码如下:

    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 OfficeOpenXml;
    using OfficeOpenXml.Drawing;
    using OfficeOpenXml.Drawing.Chart;
    using OfficeOpenXml.Style;
    using System.IO;
    using ZedGraph;
    using System.Threading;
    
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            int count = 0;
            private void timer1_Tick(object sender, EventArgs e)
            {
     
               
                //zedGraphControl1.GraphPane.XAxis.Scale.MaxAuto = true;
    
                itime++;
                this.zedGraphControl1.AxisChange();
                this.zedGraphControl1.Refresh();
                this.zedGraphControl2.AxisChange();
                this.zedGraphControl2.Refresh();
                //if (list1 != null)
                //{
                //    if (itime >= 100)
                //    {
                //        zedGraphControl1.GraphPane.XAxis.Scale.Min = (double)itime - 100;
                //        zedGraphControl1.GraphPane.XAxis.Scale.Max = (double)itime;
                //        RefreshGraph(zedGraphControl1);
                //        //zedGraphControl2.GraphPane.XAxis.Scale.Min = (double)itime - 100;
                //        //zedGraphControl2.GraphPane.XAxis.Scale.Max = (double)itime;
                //        //RefreshGraph(zedGraphControl2);
                //    }
                //}
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                initZedGraph(zedGraphControl1);
                Task task = new Task(new Action(sampling));
                task.Start();
            }
            private PointPairList list1 = new PointPairList();  //AD1的点
            private PointPairList list2 = new PointPairList();  //AD2的点
            private PointPairList list3 = new PointPairList();
            private PointPairList list4 = new PointPairList();  //AD1的点
            private PointPairList list5 = new PointPairList();  //AD2的点
            private PointPairList list6 = new PointPairList();
    
            private PointPairList list7 = new PointPairList();  //AD1的点
            private PointPairList list8= new PointPairList();  //AD2的点
            private PointPairList list9 = new PointPairList();
    
            private void initZedGraph(ZedGraphControl zedgraph)
            {
                MasterPane master = zedgraph.MasterPane;
                master.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);
                master.PaneList.Clear();
                master.Title.IsVisible = true;
                master.Title.Text = "测头调试曲线图";
                master.Margin.All = 10;
                master.InnerPaneGap = 0;
                ColorSymbolRotator rotator = new ColorSymbolRotator();
                for (int j = 0; j < 2; j++)
                {
                    GraphPane myPaneT = new GraphPane(new Rectangle(40, 40, 250, 500),
                        "Case #" + (j + 1).ToString(),
                        "时间",
                        "电压");
                    //GraphPane myPaneT = new GraphPane(new Rectangle(40, 40, 500, 250),
                    //    "Case #" + (j + 1).ToString(),
                    //    "时间",
                    //    "电压");
                    myPaneT.Fill.IsVisible = false;
                    myPaneT.Chart.Fill = new Fill(Color.White, Color.SteelBlue, 45.0F);
                    myPaneT.BaseDimension = 2.0F;
                    myPaneT.XAxis.Title.IsVisible = false;
                    myPaneT.XAxis.Scale.IsVisible = false;
                    myPaneT.Legend.IsVisible = false;
                    myPaneT.Border.IsVisible = false;
                    myPaneT.Title.IsVisible = false;
    
                    myPaneT.XAxis.MajorTic.IsOpposite = false;
                    myPaneT.XAxis.MinorTic.IsOpposite = false;
                    myPaneT.XAxis.MajorTic.IsOutside = false;
                    myPaneT.XAxis.MinorTic.IsOutside = false;
                    myPaneT.XAxis.MajorGrid.IsVisible = true;
                    myPaneT.XAxis.MinorGrid.IsVisible = true;
                    myPaneT.XAxis.Scale.Max = 100;
                    myPaneT.XAxis.Scale.Min = 0;
    
                    myPaneT.YAxis.Title.Text = "距离(μm)";
                    myPaneT.YAxis.Title.FontSpec.Size = 5;
                    myPaneT.YAxis.Scale.Max = 3500;
                    myPaneT.YAxis.Scale.Min = -3500;
                    myPaneT.YAxis.Scale.MajorStep = 1000;
                    myPaneT.YAxis.MajorTic.IsOpposite = false;
                    myPaneT.YAxis.MinorTic.IsOpposite = false; //小刻度
                    myPaneT.YAxis.Scale.Align = AlignP.Inside;
    
                    myPaneT.Y2Axis.Title.Text = "电压值";
                    myPaneT.Y2Axis.Title.FontSpec.Size = 5;
                    myPaneT.Y2Axis.Scale.Max = 4;
                    myPaneT.Y2Axis.Scale.Min = -4;
                    myPaneT.Y2Axis.Scale.MajorStep = 1;
                    myPaneT.Y2Axis.Scale.FontSpec.FontColor = Color.Black;
                    myPaneT.Y2Axis.Title.FontSpec.FontColor = Color.Black;
                    myPaneT.Y2Axis.MajorTic.IsOpposite = false;
                    myPaneT.Y2Axis.MinorTic.IsOpposite = false;
                    myPaneT.Y2Axis.Scale.Align = AlignP.Inside;
                    myPaneT.Y2Axis.IsVisible = true;
                    myPaneT.Margin.All = 1;       // Graphpane之间的间隔边缘距离
    
                    myPaneT.YAxis.Scale.FontSpec.Size = 4;
                    myPaneT.Y2Axis.Scale.FontSpec.Size = 4;
                    if (j == 0)
                    {
                        myPaneT.Title.Text = "上测头";
                        LineItem Curve1 = myPaneT.AddCurve("电压1", list1, Color.Yellow, SymbolType.None);
                        LineItem Curve2 = myPaneT.AddCurve("电压2", list2, Color.Green, SymbolType.None);
                        LineItem Curve3 = myPaneT.AddCurve("电压3", list3, Color.Red, SymbolType.None);
                        Curve1.IsY2Axis = true;
                        Curve2.IsY2Axis = true;
                        
                    }
                    if (j == 1)
                    {
                        myPaneT.Title.Text = "下测头";
                        LineItem Curve1 = myPaneT.AddCurve("电压1", list4, Color.Yellow, SymbolType.None);
                        LineItem Curve2 = myPaneT.AddCurve("电压2", list5, Color.Green, SymbolType.None);
                        LineItem Curve3 = myPaneT.AddCurve("电压3", list6, Color.Red, SymbolType.None);
                        Curve1.IsY2Axis = true;
                        Curve2.IsY2Axis = true;
    
                    }
                   
                    master.Add(myPaneT);
                   // zedgraph.MasterPane[0].Chart.Rect = new RectangleF(100, 100, 200, 200);
                    zedgraph.AxisChange();
                    if (j > 0)
                    {
                        myPaneT.YAxis.Scale.IsSkipLastLabel = true;
                        myPaneT.YAxis.Scale.IsSkipLastLabel = true;
                        myPaneT.YAxis.MinSpace = 10;
                        myPaneT.Y2Axis.MinSpace = 10;
                        
                    }
                }
                using (Graphics g = zedgraph.CreateGraphics())
                {
                    ZedGraphControl z1 = zedgraph;
    
                    master.SetLayout(g, PaneLayout.SingleColumn);
                    z1.AxisChange();
                    z1.IsAutoScrollRange = true;
                    z1.IsShowHScrollBar = true;
                    z1.IsShowVScrollBar = true;
                    z1.IsSynchronizeXAxes = true;
                }
            }
            long itime = 0;
            private void sampling()
            {
               
                while (true)
                {
                    Random rd = new Random();  
    
                    //list1.Add((double)itime, rd.Next(1, 4));
                    //list2.Add((double)itime, rd.Next(1, 4));
                    //list4.Add((double)itime, rd.Next(1, 4));
                    //list5.Add((double)itime, rd.Next(1, 4));
                    //list3.Add((double)itime, rd.Next(1, 4000));
                    //list6.Add((double)itime, rd.Next(1, 4000));
    
    
                   
                    //if (itime%1000000==0)
                    //{
                        list1.Add((double)itime, rd.Next(1, 4));
                        list2.Add((double)itime, rd.Next(1, 4));
                        list4.Add((double)itime, rd.Next(1, 4));
                        list5.Add((double)itime, rd.Next(1, 4));
                        list3.Add((double)itime, rd.Next(1, 4000));
                        list6.Add((double)itime, rd.Next(1, 4000));
                    itime++;
                    if (itime > 10000)
                    {
                        itime = 0;
                        list1.Clear();
                        list2.Clear();
                        list4.Clear();
                        list5.Clear();
                        list3.Clear();
                        list6.Clear();
                    }
    
                    if (itime >= 100)
                    {
                        Thread.Sleep(1);
    
                        zedGraphControl1.MasterPane[0].XAxis.Scale.Min = (double)itime - 100;
                        zedGraphControl1.MasterPane[0].XAxis.Scale.Max = (double)itime;
                        zedGraphControl1.MasterPane[1].XAxis.Scale.Min = (double)itime - 100;
                        zedGraphControl1.MasterPane[1].XAxis.Scale.Max = (double)itime;
                        RefreshGraph(zedGraphControl1);
                    }
    
                }
            }
            private delegate void RefreshDelegate();
            private void RefreshGraph(ZedGraphControl zg)
            {
                if (zg == null)
                    return;
                //判断是否是非创建控件的线程调用控件
                if (zg.InvokeRequired)
                {
                    RefreshDelegate a = new RefreshDelegate(new Action(() => { zg.Invalidate();  }));
                    zg.BeginInvoke(a);
                }
                else
                {
                    zg.AxisChange();
                    zg.Invalidate();
                    zg.Refresh();
                }
            }
        }
    }

    invalidate是收到命令选取合适的时机再刷新

    refresh是强制重绘 refresh= invalidate+update

    问题: 这里采用的是通常的竖直排列的图表形式。改成水平的话 Graphpane的大小不好控制。改不好 ??

  • 相关阅读:
    如何使用go module导入本地包
    gin-vue-admin 03 项目打包上线
    golang map转xml
    vim简明文档
    goframe gf-cli的使用
    supervisor 的安装与使用
    element Tree 树形控件
    gin-vue-admin开发教程 02 了解项目目录结构和代码执行的流程
    gin-vue-admin开发教程 01安装与启用
    oraclesql遇见的问题(一)
  • 原文地址:https://www.cnblogs.com/zhayunjia/p/6305408.html
Copyright © 2011-2022 走看看