Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用鼠标在图表上缩放每个点_C# - Fatal编程技术网

C# 用鼠标在图表上缩放每个点

C# 用鼠标在图表上缩放每个点,c#,C#,我的c windows应用程序上有一个图表。 我想在鼠标放在图表上时缩放图表的每个点。 比如谷歌地图 我的意思是我不想放大图表的所有部分 我想缩放像谷歌地图一样的特定点 代码: 假设您使用的是命名空间System.Windows.Forms.DataVisualization.Charting中的.NET 4.0图表库之后的标准。当鼠标做这个或那个时,可以实现自定义交互缩放。MSDN是一个良好的开端和GIYF 在网络上也有很多信息 祝你好运 有什么问题吗?你用的是ms图表还是其他商业图表? pu

我的c windows应用程序上有一个图表。 我想在鼠标放在图表上时缩放图表的每个点。 比如谷歌地图

我的意思是我不想放大图表的所有部分 我想缩放像谷歌地图一样的特定点

代码:


假设您使用的是命名空间System.Windows.Forms.DataVisualization.Charting中的.NET 4.0图表库之后的标准。当鼠标做这个或那个时,可以实现自定义交互缩放。MSDN是一个良好的开端和GIYF

在网络上也有很多信息


祝你好运

有什么问题吗?你用的是ms图表还是其他商业图表?
public partial class Form1 : Form
    {

        int[] myArrayX = new int[5];
        double[] myArrayY = new double[5];
        int lastX = -1;
        double lastY = -0.6;
        double xmax;

        Graph.Chart chart;
        public Form1()
        {
            InitializeComponent();
            this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
        }

        void Form1_MouseWheel(object sender, MouseEventArgs e)
        {

            try
            {
                if (e.Delta > 0)
                {
                    double xMin = chart.ChartAreas["draw"].AxisX.ScaleView.ViewMinimum;
                    double xMax = chart.ChartAreas["draw"].AxisX.ScaleView.ViewMaximum;
                    double yMin = chart.ChartAreas["draw"].AxisY.ScaleView.ViewMinimum;
                    double yMax = chart.ChartAreas["draw"].AxisY.ScaleView.ViewMaximum;

                    double posXStart = chart.ChartAreas["draw"].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 2;
                    double posXFinish = chart.ChartAreas["draw"].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 2;
                    double posYStart = chart.ChartAreas["draw"].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 2;
                    double posYFinish = chart.ChartAreas["draw"].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 2;

                    chart.ChartAreas["draw"].AxisX.ScaleView.Zoom(posXStart, posXFinish);
                    chart.ChartAreas["draw"].AxisY.ScaleView.Zoom(posYStart, posYFinish);
                }
                else if (e.Delta < 0)
                {
                    ZoomOut();
                }

            }
            catch { }


        }

        private void ZoomOut()
        {
            chart.ChartAreas["draw"].AxisX.ScaleView.ZoomReset();
            chart.ChartAreas["draw"].AxisY.ScaleView.ZoomReset();
        }



        void CreateNewGraph()
        {
            // Create new Graph
            chart = new Graph.Chart();


            chart.Location = new System.Drawing.Point(13, 185);


            chart.Size = new System.Drawing.Size(900, 500);


            chart.ChartAreas.Add("draw");




            chart.ChartAreas["draw"].AxisX.Minimum = 0;
            chart.ChartAreas["draw"].AxisX.Maximum = 20;


            chart.ChartAreas["draw"].AxisX.Interval = 1;


            chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = Color.White;


            chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;


            chart.ChartAreas["draw"].AxisY.Minimum = -0.4;
            chart.ChartAreas["draw"].AxisY.Maximum = 1;


            chart.ChartAreas["draw"].AxisY.Interval = 0.2;


            chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.White;


            chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = Graph.ChartDashStyle.Dash;


            chart.ChartAreas["draw"].BackColor = Color.Black;



            var series = chart.Series.Add("Test");


            chart.Series["Test"].ChartType = Graph.SeriesChartType.Line;


            chart.Series["Test"].Color = Color.Yellow;


            chart.Series["Test"].BorderWidth = 3;


            chart.Legends.Add("MyLegend");
            chart.Legends["MyLegend"].BorderColor = Color.YellowGreen;

            // Set automatic zooming
            chart.ChartAreas["draw"].AxisX.ScaleView.Zoomable = true;
            chart.ChartAreas["draw"].AxisY.ScaleView.Zoomable = true;

            // Set automatic scrolling 
            chart.ChartAreas["draw"].CursorX.AutoScroll = true;
            chart.ChartAreas["draw"].CursorY.AutoScroll = true;

            // Allow user selection for Zoom
            chart.ChartAreas["draw"].CursorX.IsUserSelectionEnabled = true;
            chart.ChartAreas["draw"].CursorY.IsUserSelectionEnabled = true;

            chart.ChartAreas["draw"].AxisX.ScaleView.Zoomable = true;
            chart.ChartAreas["draw"].AxisY.ScaleView.Zoomable = true;

            //chart.MouseWheel += new MouseEventHandler(chart_MouseWheel);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            CreateNewGraph();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            fillarray();

            for (int i = 1; i <= 5; i += 1)
            {
                chart.Series["Test"].Points.AddXY(myArrayX[i - 1], myArrayY[i - 1]);
                xmax = myArrayX[i - 1];
            }

            if (xmax >= 20)
            {
                chart.ChartAreas["draw"].AxisX.ScrollBar.Enabled = true;
                chart.ChartAreas["draw"].AxisX.ScaleView.Zoomable = true;
                chart.ChartAreas["draw"].AxisX.ScaleView.Zoom(0, xmax);
            }

            Controls.Add(this.chart);

        }

        public void fillarray()
        {
            for (int i = 1; i <= 5; i += 1)
            {
                lastX = lastX + 1;
                myArrayX[i - 1] = lastX;

            }

            for (int i = 1; i < 5; i += 1)
            {
                lastY = lastY + 0.2;
                myArrayY[i - 1] = lastY;

            }


        }
    }