导航

ZedGraph Scroll Event

Posted on 2011-04-06 22:04  beeone  阅读(783)  评论(0编辑  收藏  举报

In One of my Project,I have two Zedgraph in the one Window Form.I am loading data in the Graph,for both zedGraph ,Making AutoScroll is True.
if i move Left or Right the Horizontal scrollBar of the First Graph,then the Graph of the First is moving on the move of Fist Horizontal ScrollBar move.Now i want to develop same effect on the Second Graph on the Move of First Graph Scrollbar move.Code of my Project is Below:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.IO;

namespace MyTestGraph
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            CreateGraph_Test(zedGraphControl_Left, zedGraphControl_Right);
            SetSize();         
           
        }

        public void CreateChart(ZedGraphControl zgc)
        {
            GraphPane myPane = zgc.GraphPane;

            // Set the titles and axis labels
            myPane.Title.Text = "My Test Graph \n Wind Management";
            myPane.XAxis.Title.Text = "Time (hour)";
            myPane.YAxis.Title.Text = "Wind Speed (m/s)";
            myPane.Y2Axis.Title.Text = "Temparature (ºC)";

            PointPairList vList = new PointPairList();
            vList = ListWindSpeed();
            PointPairList aList = new PointPairList();
            aList = ListTemparature();

            LineItem myCurve = myPane.AddCurve("Velocity", vList, Color.Green, SymbolType.Star);
            myCurve.Symbol.Fill = new Fill(Color.White);

            myCurve = myPane.AddCurve("Acceleration", aList, Color.Blue, SymbolType.Circle);
            myCurve.Symbol.Fill = new Fill(Color.White);

            myCurve.IsY2Axis = true;
            myCurve.Symbol.Fill = new Fill(Color.White);
            myCurve.YAxisIndex = 1;
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Associate this curve with the Y2 axis
            myCurve.IsY2Axis = true;
            // Associate this curve with the second Y2 axis
            myCurve.YAxisIndex = 1;

            // Show the x axis grid
            myPane.XAxis.MajorGrid.IsVisible = true;

            // Setting Color
            myPane.YAxis.Scale.FontSpec.FontColor = Color.Green;
            myPane.YAxis.Title.FontSpec.FontColor = Color.Green;
            myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
            myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;

            // turn off the opposite tics so the Y tics don't show up on the Y2 axis
            myPane.YAxis.MajorTic.IsOpposite = false;
            myPane.YAxis.MinorTic.IsOpposite = false;
            // Don't display the Y zero line
            myPane.YAxis.MajorGrid.IsZeroLine = false;
            // Align the Y axis labels so they are flush to the axis
            myPane.YAxis.Scale.Align = AlignP.Inside;
            myPane.YAxis.Scale.Max = 100;

            // Enable the Y2 axis display
            myPane.Y2Axis.IsVisible = true;
            // Make the Y2 axis scale blue

            // turn off the opposite tics so the Y2 tics don't show up on the Y axis
            myPane.Y2Axis.MajorTic.IsOpposite = false;
            myPane.Y2Axis.MinorTic.IsOpposite = false;
            // Display the Y2 axis grid lines
            myPane.Y2Axis.MajorGrid.IsVisible = true;
            // Align the Y2 axis labels so they are flush to the axis
            myPane.Y2Axis.Scale.Align = AlignP.Inside;
            //myPane.Y2Axis.Scale.Min = 1.5;
            myPane.Y2Axis.Scale.Max = 100;

            myPane.Chart.Fill = new Fill(Color.White, Color.WhiteSmoke, 45.0f);

            zgc.AxisChange();
        }

        public void CreateChart1(ZedGraphControl zgc1)
        {
            GraphPane myPane = zgc1.GraphPane;

            // Set the titles and axis labels
            myPane.Title.Text = "My Test Graph \n Wind Management";
            myPane.XAxis.Title.Text = "Time (hour)";
            myPane.YAxis.Title.Text = "Wind Speed (m/s)";
            myPane.Y2Axis.Title.Text = "Temparature (ºC)";

            PointPairList vList = new PointPairList();
            vList = ListWindSpeed();
            PointPairList aList = new PointPairList();
            aList = ListTemparature();

            LineItem myCurve = myPane.AddCurve("Velocity", vList, Color.Green, SymbolType.Star);
            myCurve.Symbol.Fill = new Fill(Color.White);

            myCurve = myPane.AddCurve("Acceleration", aList, Color.Blue, SymbolType.Circle);
            myCurve.Symbol.Fill = new Fill(Color.White);

            myCurve.IsY2Axis = true;
            myCurve.Symbol.Fill = new Fill(Color.White);
            myCurve.YAxisIndex = 1;
            myCurve.Symbol.Fill = new Fill(Color.White);

            // Associate this curve with the Y2 axis
            myCurve.IsY2Axis = true;
            // Associate this curve with the second Y2 axis
            myCurve.YAxisIndex = 1;

            // Show the x axis grid
            myPane.XAxis.MajorGrid.IsVisible = true;

            // Setting Color
            myPane.YAxis.Scale.FontSpec.FontColor = Color.Green;
            myPane.YAxis.Title.FontSpec.FontColor = Color.Green;
            myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
            myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;

            // turn off the opposite tics so the Y tics don't show up on the Y2 axis
            myPane.YAxis.MajorTic.IsOpposite = false;
            myPane.YAxis.MinorTic.IsOpposite = false;
            // Don't display the Y zero line
            myPane.YAxis.MajorGrid.IsZeroLine = false;
            // Align the Y axis labels so they are flush to the axis
            myPane.YAxis.Scale.Align = AlignP.Inside;
            myPane.YAxis.Scale.Max = 100;

            // Enable the Y2 axis display
            myPane.Y2Axis.IsVisible = true;
            // Make the Y2 axis scale blue

            // turn off the opposite tics so the Y2 tics don't show up on the Y axis
            myPane.Y2Axis.MajorTic.IsOpposite = false;
            myPane.Y2Axis.MinorTic.IsOpposite = false;
            // Display the Y2 axis grid lines
            myPane.Y2Axis.MajorGrid.IsVisible = true;
            // Align the Y2 axis labels so they are flush to the axis
            myPane.Y2Axis.Scale.Align = AlignP.Inside;
            //myPane.Y2Axis.Scale.Min = 1.5;
            myPane.Y2Axis.Scale.Max = 100;

            myPane.Chart.Fill = new Fill(Color.White, Color.WhiteSmoke, 45.0f);

            zgc1.AxisChange();
        }

        private void SetSize()
        {
            zedGraphControl_Left.Location = new Point(10, 10);

            zedGraphControl_Left.Size = new Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20);


            zedGraphControl_Right.Location = new Point(10, 10);

            zedGraphControl_Right.Size = new Size(ClientRectangle.Width - 20, ClientRectangle.Height - 20);
        }


        private PointPairList ListWindSpeed()
        {
            PointPairList list = new PointPairList();
            string line;
            // Read the file and display it line by line.
            using (StreamReader file = new StreamReader("D:\\WindSpeed.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    string[] xy = line.Split('\t');
                    if (xy.Length == 2)
                    {
                        if (xy[0].ToString() != "X" && xy[1].ToString() != "Y")
                        {
                            double x = Convert.ToDouble(xy[0]);
                            double y = Convert.ToDouble(xy[1]);

                            list.Add(x, y);
                        }
                    }
                }

                file.Close();
            }
            return list;
        }

        private PointPairList ListTemparature()
        {
            PointPairList list = new PointPairList();
            string line;

            // Read the file and display it line by line.
            using (StreamReader file = new StreamReader("D:\\Temparature.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    string[] xy = line.Split('\t');
                    if (xy.Length == 2)
                    {
                        if (xy[0].ToString() != "X" && xy[1].ToString() != "Y")
                        {
                            double x = Convert.ToDouble(xy[0]);
                            double y = Convert.ToDouble(xy[1]);

                            list.Add(x, y);
                        }
                    }
                }
                file.Close();
            }
            return list;
        }

        private PointPairList ListForGraph2()
        {
            PointPairList list = new PointPairList();
            string line;

            // Read the file and display it line by line.
            using (StreamReader file = new StreamReader("D:\\Data2.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    string[] xy = line.Split('\t');
                    if (xy.Length == 2)
                    {
                        if (xy[0].ToString() != "X" && xy[1].ToString() != "Y")
                        {
                            double x = Convert.ToDouble(xy[0]);
                            double y = Convert.ToDouble(xy[1]);

                            list.Add(x, y);
                        }
                    }
                }

                file.Close();
            }
            return list;
        }

        private PointPairList ListForGraph3()
        {
            PointPairList list = new PointPairList();
            string line;

            // Read the file and display it line by line.
            using (StreamReader file = new StreamReader("D:\\Data3.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    string[] xy = line.Split('\t');
                    if (xy.Length == 2)
                    {
                        if (xy[0].ToString() != "X" && xy[1].ToString() != "Y")
                        {
                            double x = Convert.ToDouble(xy[0]);
                            double y = Convert.ToDouble(xy[1]);

                            list.Add(x, y);
                        }
                    }
                }

                file.Close();
            }
            return list;
        }

        private void CreateGraph_Test(ZedGraphControl zgc, ZedGraphControl zgc1)
        {
            // show the horizontal scroll bar
            zgc.IsShowHScrollBar = true;

            // automatically set the scrollable range to cover the data range from the curves
            zgc.IsAutoScrollRange = true;
            // Add 10% to scale range
            zgc.ScrollGrace = 0.1;

            // Horizontal pan and zoom allowed
            zgc.IsEnableHPan = false;
            zgc.IsEnableHZoom = false;

            // Vertical pan and zoom not allowed
            zgc.IsEnableVPan = false;
            zgc.IsEnableVZoom = false;

            // Set the initial viewed range
            zgc.GraphPane.XAxis.Scale.Min = 50.0;
            zgc.GraphPane.XAxis.Scale.Max = 250.0;

            //show the cursor Value
            zgc.IsShowCursorValues = true;
           
            ScrollSample_Setup(zgc);

            zgc1.IsShowHScrollBar = true;

            // automatically set the scrollable range to cover the data range from the curves
            zgc1.IsAutoScrollRange = true;
            // Add 10% to scale range
            zgc1.ScrollGrace = 0.1;

            // Horizontal pan and zoom allowed
            zgc1.IsEnableHPan = false;
            zgc1.IsEnableHZoom = false;

            // Vertical pan and zoom not allowed
            zgc1.IsEnableVPan = false;
            zgc1.IsEnableVZoom = false;

            //show the cursor Value
            zgc1.IsShowCursorValues = true;
            // Set the initial viewed range
            zgc1.GraphPane.XAxis.Scale.Min = 50.0;
            zgc1.GraphPane.XAxis.Scale.Max = 250.0;

            ScrollSample_Setup(zgc1);
        }

        private void ScrollSample_Setup(ZedGraphControl zgc)
        {
            // get a reference to the GraphPane
            GraphPane myPane = zgc.GraphPane;
            // Set the titles
            myPane.Title.Text = "Sample ScrollBar Graph";
            myPane.XAxis.Title.Text = "Index";
            myPane.XAxis.Title.Text = "Phased Sine Data";
            // Generate some sample sine data in PointPairList's
            PointPairList list = new PointPairList();
            PointPairList list2 = new PointPairList();
            PointPairList list3 = new PointPairList();
            for (int i = 0; i < 100; i++)
            {
                double x = (double)i * 5.0 + 25.0;
                double y = Math.Sin((double)i * Math.PI / 25.0) * 16.0;
                double y2 = Math.Sin((double)i * Math.PI / 25.0 + 30.0) * 12.0;
                double y3 = Math.Sin((double)i * Math.PI / 25.0 + 60.0) * 8.0;
                list.Add(x, y);
                list2.Add(x, y2);
                list3.Add(x, y3);
            }
            // create three curves from the above data sets
            LineItem myCurve = myPane.AddCurve("Alpha", list, Color.Red, SymbolType.Diamond);
            myCurve = myPane.AddCurve("Beta", list2, Color.Blue, SymbolType.Plus);
            myCurve = myPane.AddCurve("Sigma", list3, Color.Green, SymbolType.XCross);
            // scale the axes base on the data
            zgc.AxisChange();
        }

        private void zedGraphControl_Left_Scroll(object sender, ScrollEventArgs e)
        {

        }

        private void zedGraphControl_Left_ScrollDoneEvent(ZedGraphControl sender, ScrollBar scrollBar, ZoomState oldState, ZoomState newState)
        {
            //ScrollBar sc = (ScrollBar)zedGraphControl_Right.Controls[0];
            //sc.Value = scrollBar.Value;
            //zedGraphControl_Right_ScrollDoneEvent(sender, scrollBar, oldState, newState);
        }

        private void zedGraphControl_Left_ScrollEvent(object sender, ScrollEventArgs e)
        {
            ScrollBar sc = (ScrollBar)zedGraphControl_Right.Controls[0];
            sc.Value = e.NewValue;
        }

        private void zedGraphControl_Left_ScrollProgressEvent(ZedGraphControl sender, ScrollBar scrollBar, ZoomState oldState, ZoomState newState)
        {
          
        }


        private void zedGraphControl_Right_ScrollDoneEvent(ZedGraphControl sender, ScrollBar scrollBar, ZoomState oldState, ZoomState newState)
        {

        }

        private void zedGraphControl_Right_ScrollEvent(object sender, ScrollEventArgs e)
        {
            ScrollBar sc = (ScrollBar)zedGraphControl_Left.Controls[0];
            sc.Value = e.NewValue;
        }

    }
}