Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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;

           }


       }
   }
Posted
Comments
Richard MacCutchan 15-Apr-14 5:01am    
Please provide proper details of your problem. Saying "I want to ..." does not tell us what you are having difficulty with.
neda1367 15-Apr-14 5:08am    
i did zoom on my project. but i is works on all chart. when i do mouse wheel all my chart will be zoo but i want to zoo just one specefic point.

Bernhard Hiller 15-Apr-14 9:12am    
"i want zoom just specefic point" does not make sense at all.
I guess you do not like that the chart moves such that the point where the mouse was over becomes the center of the chart. Does that describe the problem better?
neda1367 15-Apr-14 23:40pm    
yes . what is the resolve?

1 solution

Your code
C#
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;
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:
C#
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;
 
Share this answer
 
Comments
AHTOXA 19-Dec-16 8:32am    
for zoom factors other than 2 there is more complex solution
(code is for x axis only, c++/cli)

System::Void ChartDiagramForm::OnDiagramMouseWheel(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
	const double ZOOM_FACTOR = 1.6;
	auto axis = chart->ChartAreas[0]->AxisX;

	double initialAxisMousePosition;
	try { initialAxisMousePosition = axis->PixelPositionToValue(e->X); }
	catch( ArgumentException ^ ) { initialAxisMousePosition = (axis->ScaleView->ViewMinimum + axis->ScaleView->ViewMaximum)/2.; }

	if( e->Delta > 0 ) // zoom in
	{
		double dToLeft = (initialAxisMousePosition - axis->ScaleView->ViewMinimum) / ZOOM_FACTOR;
		double dToRight = (axis->ScaleView->ViewMaximum - initialAxisMousePosition) / ZOOM_FACTOR;
		axis->ScaleView->Zoom(initialAxisMousePosition - dToLeft, initialAxisMousePosition + dToRight);
	}
	else
	{ // zoom out
		double dToLeft = (initialAxisMousePosition - axis->ScaleView->ViewMinimum) * ZOOM_FACTOR;
		double dToRight = (axis->ScaleView->ViewMaximum - initialAxisMousePosition) * ZOOM_FACTOR;
		if( dToLeft + dToRight >= axis->Maximum - axis->Minimum )
			axis->ScaleView->ZoomReset();
		else
			axis->ScaleView->Zoom(initialAxisMousePosition - dToLeft, initialAxisMousePosition + dToRight);
	}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900