放大c#中的图表,如谷歌地图 [英] zooming on chart in c# like google map

查看:65
本文介绍了放大c#中的图表,如谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#windows应用程序上有一个图表。

i想要在鼠标上放大图表的每个点。

像google map



i意味着我不想放大图表的所有部分

i想要缩放只是像谷歌地图一样的点数



代码:

i have a chart on my c# windows application.
i want to zoom every point of chart when mouse on them.
like google map

i mean i dont want zoom all part of chart
i want zoom just specefic point like google map

code:

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;

           }


       }
   }

推荐答案

您的代码

Your code
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;

放置鼠标结束的位置(e.Location)进入图表中间。必须调整它以使位置不会改变。

我没有检查下面的代码是否真的满足了这个要求,它可能需要一些小的改动:

places the point where the mouse was over (e.Location) into the middle of the chart. It must be adjusted such that the position does not change.
I did not check if following code really fulfills that requirement, it may need some minor changes:

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


这篇关于放大c#中的图表,如谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆