zoom in zoom out from a point mschart









up vote
2
down vote

favorite
1












enter image description here
How to zoom from a point
for example zoom from point 300 to 500 axisX and 50 to 100 AxisY step by step



Current location



private void chart1_MouseClick(object sender, MouseEventArgs e)



 lastPoint = e.Location;



Zooming X And Y



 private void btnZoomXY_Click(object sender, EventArgs e)




step = (int)(chart1.ChartAreas["ChartArea1"].AxisX.Maximum - lastPoint.X ) / 20;
if (zoomx > chart1.ChartAreas["ChartArea1"].AxisX.Maximum)

zoomx -= step;


else
zoomx += step;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(lastPoint.X+ zoomx, this.chart1.ChartAreas["ChartArea1"].AxisX.Maximum - zoomx);


if (Mode == SpectometrMode.Absorbance)

step1 = 0.2f;

else
step1 = (int)(chart1.ChartAreas["ChartArea1"].AxisY.Maximum - lastPoint.Y) / 20;
if (zoomY > chart1.ChartAreas["ChartArea1"].AxisY.Maximum)

zoomY -= step1;


else
zoomY += step1;
this.chart1.ChartAreas["ChartArea1"].AxisY.ScaleView.Zoom(lastPoint.Y+ zoomY, this.chart1.ChartAreas["ChartArea1"].AxisY.Maximum - zoomY);




chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";



@taW










share|improve this question



















  • 1




    Hehe, this kind of Ping doesn't work. You need to a) know the current zoom center and your steps. Then the code should work. Always keep the center in the midle between zoom start and -end. If the center is at x=400 and the current zoom is at xstart= 100 you have a xdelta=300. For step=100 you can zoom to (origin-i*step, origin+i*step)
    – TaW
    Nov 11 at 8:29











  • @TaW my steps is different I have 3 mode in code . current zoom center showing by mouse click. please help me write this code .Thanks
    – kamran amingalvani
    Nov 11 at 8:44






  • 1




    I'll have a look later..
    – TaW
    Nov 11 at 13:00










  • I found the code a little confusing. My answer adresses the prime issue of wrong coordinates and shows a simple zooming scheme. Adapt as needed..!
    – TaW
    Nov 12 at 10:03















up vote
2
down vote

favorite
1












enter image description here
How to zoom from a point
for example zoom from point 300 to 500 axisX and 50 to 100 AxisY step by step



Current location



private void chart1_MouseClick(object sender, MouseEventArgs e)



 lastPoint = e.Location;



Zooming X And Y



 private void btnZoomXY_Click(object sender, EventArgs e)




step = (int)(chart1.ChartAreas["ChartArea1"].AxisX.Maximum - lastPoint.X ) / 20;
if (zoomx > chart1.ChartAreas["ChartArea1"].AxisX.Maximum)

zoomx -= step;


else
zoomx += step;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(lastPoint.X+ zoomx, this.chart1.ChartAreas["ChartArea1"].AxisX.Maximum - zoomx);


if (Mode == SpectometrMode.Absorbance)

step1 = 0.2f;

else
step1 = (int)(chart1.ChartAreas["ChartArea1"].AxisY.Maximum - lastPoint.Y) / 20;
if (zoomY > chart1.ChartAreas["ChartArea1"].AxisY.Maximum)

zoomY -= step1;


else
zoomY += step1;
this.chart1.ChartAreas["ChartArea1"].AxisY.ScaleView.Zoom(lastPoint.Y+ zoomY, this.chart1.ChartAreas["ChartArea1"].AxisY.Maximum - zoomY);




chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";



@taW










share|improve this question



















  • 1




    Hehe, this kind of Ping doesn't work. You need to a) know the current zoom center and your steps. Then the code should work. Always keep the center in the midle between zoom start and -end. If the center is at x=400 and the current zoom is at xstart= 100 you have a xdelta=300. For step=100 you can zoom to (origin-i*step, origin+i*step)
    – TaW
    Nov 11 at 8:29











  • @TaW my steps is different I have 3 mode in code . current zoom center showing by mouse click. please help me write this code .Thanks
    – kamran amingalvani
    Nov 11 at 8:44






  • 1




    I'll have a look later..
    – TaW
    Nov 11 at 13:00










  • I found the code a little confusing. My answer adresses the prime issue of wrong coordinates and shows a simple zooming scheme. Adapt as needed..!
    – TaW
    Nov 12 at 10:03













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





enter image description here
How to zoom from a point
for example zoom from point 300 to 500 axisX and 50 to 100 AxisY step by step



Current location



private void chart1_MouseClick(object sender, MouseEventArgs e)



 lastPoint = e.Location;



Zooming X And Y



 private void btnZoomXY_Click(object sender, EventArgs e)




step = (int)(chart1.ChartAreas["ChartArea1"].AxisX.Maximum - lastPoint.X ) / 20;
if (zoomx > chart1.ChartAreas["ChartArea1"].AxisX.Maximum)

zoomx -= step;


else
zoomx += step;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(lastPoint.X+ zoomx, this.chart1.ChartAreas["ChartArea1"].AxisX.Maximum - zoomx);


if (Mode == SpectometrMode.Absorbance)

step1 = 0.2f;

else
step1 = (int)(chart1.ChartAreas["ChartArea1"].AxisY.Maximum - lastPoint.Y) / 20;
if (zoomY > chart1.ChartAreas["ChartArea1"].AxisY.Maximum)

zoomY -= step1;


else
zoomY += step1;
this.chart1.ChartAreas["ChartArea1"].AxisY.ScaleView.Zoom(lastPoint.Y+ zoomY, this.chart1.ChartAreas["ChartArea1"].AxisY.Maximum - zoomY);




chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";



@taW










share|improve this question















enter image description here
How to zoom from a point
for example zoom from point 300 to 500 axisX and 50 to 100 AxisY step by step



Current location



private void chart1_MouseClick(object sender, MouseEventArgs e)



 lastPoint = e.Location;



Zooming X And Y



 private void btnZoomXY_Click(object sender, EventArgs e)




step = (int)(chart1.ChartAreas["ChartArea1"].AxisX.Maximum - lastPoint.X ) / 20;
if (zoomx > chart1.ChartAreas["ChartArea1"].AxisX.Maximum)

zoomx -= step;


else
zoomx += step;
this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(lastPoint.X+ zoomx, this.chart1.ChartAreas["ChartArea1"].AxisX.Maximum - zoomx);


if (Mode == SpectometrMode.Absorbance)

step1 = 0.2f;

else
step1 = (int)(chart1.ChartAreas["ChartArea1"].AxisY.Maximum - lastPoint.Y) / 20;
if (zoomY > chart1.ChartAreas["ChartArea1"].AxisY.Maximum)

zoomY -= step1;


else
zoomY += step1;
this.chart1.ChartAreas["ChartArea1"].AxisY.ScaleView.Zoom(lastPoint.Y+ zoomY, this.chart1.ChartAreas["ChartArea1"].AxisY.Maximum - zoomY);




chart1.ChartAreas[0].AxisX.LabelStyle.Format = "0";



@taW







c# mschart zooming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 9:41

























asked Nov 11 at 8:10









kamran amingalvani

2918




2918







  • 1




    Hehe, this kind of Ping doesn't work. You need to a) know the current zoom center and your steps. Then the code should work. Always keep the center in the midle between zoom start and -end. If the center is at x=400 and the current zoom is at xstart= 100 you have a xdelta=300. For step=100 you can zoom to (origin-i*step, origin+i*step)
    – TaW
    Nov 11 at 8:29











  • @TaW my steps is different I have 3 mode in code . current zoom center showing by mouse click. please help me write this code .Thanks
    – kamran amingalvani
    Nov 11 at 8:44






  • 1




    I'll have a look later..
    – TaW
    Nov 11 at 13:00










  • I found the code a little confusing. My answer adresses the prime issue of wrong coordinates and shows a simple zooming scheme. Adapt as needed..!
    – TaW
    Nov 12 at 10:03













  • 1




    Hehe, this kind of Ping doesn't work. You need to a) know the current zoom center and your steps. Then the code should work. Always keep the center in the midle between zoom start and -end. If the center is at x=400 and the current zoom is at xstart= 100 you have a xdelta=300. For step=100 you can zoom to (origin-i*step, origin+i*step)
    – TaW
    Nov 11 at 8:29











  • @TaW my steps is different I have 3 mode in code . current zoom center showing by mouse click. please help me write this code .Thanks
    – kamran amingalvani
    Nov 11 at 8:44






  • 1




    I'll have a look later..
    – TaW
    Nov 11 at 13:00










  • I found the code a little confusing. My answer adresses the prime issue of wrong coordinates and shows a simple zooming scheme. Adapt as needed..!
    – TaW
    Nov 12 at 10:03








1




1




Hehe, this kind of Ping doesn't work. You need to a) know the current zoom center and your steps. Then the code should work. Always keep the center in the midle between zoom start and -end. If the center is at x=400 and the current zoom is at xstart= 100 you have a xdelta=300. For step=100 you can zoom to (origin-i*step, origin+i*step)
– TaW
Nov 11 at 8:29





Hehe, this kind of Ping doesn't work. You need to a) know the current zoom center and your steps. Then the code should work. Always keep the center in the midle between zoom start and -end. If the center is at x=400 and the current zoom is at xstart= 100 you have a xdelta=300. For step=100 you can zoom to (origin-i*step, origin+i*step)
– TaW
Nov 11 at 8:29













@TaW my steps is different I have 3 mode in code . current zoom center showing by mouse click. please help me write this code .Thanks
– kamran amingalvani
Nov 11 at 8:44




@TaW my steps is different I have 3 mode in code . current zoom center showing by mouse click. please help me write this code .Thanks
– kamran amingalvani
Nov 11 at 8:44




1




1




I'll have a look later..
– TaW
Nov 11 at 13:00




I'll have a look later..
– TaW
Nov 11 at 13:00












I found the code a little confusing. My answer adresses the prime issue of wrong coordinates and shows a simple zooming scheme. Adapt as needed..!
– TaW
Nov 12 at 10:03





I found the code a little confusing. My answer adresses the prime issue of wrong coordinates and shows a simple zooming scheme. Adapt as needed..!
– TaW
Nov 12 at 10:03













1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Here is a solution that will zoom in 2x with each click.



It uses a variable span as the range of values that will be visible in the zoomed view.



It also moves the clicked position to the next closest DataPoint. You may remove this to allow zooming in on positions between DataPoints.



Let's see it in action:



enter image description here



Note that the first issue with your code is the coordinates you use.



These are three (!) coordinate systems in MSChart:




  • Pixels as in MouseClick


  • Values as in Axis values


  • Positions as in ElementPositions i.e. percentages related to the next higher container

Your main issue is that you mix pixels with values which will result in chaos.



You need to convert the pixels to values as in the code shown.



Here are the variables I use:



double span = 0; // axis range in values
Point lastPoint = Point.Empty; // the clicked position
PointF clickedValues = PointF.Empty; // the values of the clicked positiom
DataPoint closePoint = null; // the next closest DataPoint


Now the click code with the conversion:



private void chart1_MouseClick(object sender, MouseEventArgs e)

lastPoint = e.Location;
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;

if (closePoint != null) closePoint.MarkerColor = chart1.Series[0].MarkerColor;

clickedValues = new PointF((float)ax.PixelPositionToValue(lastPoint.X),
(float)ay.PixelPositionToValue(lastPoint.Y));

closePoint = chart1.Series[0].Points.Where(x => x.XValue >= clickedValues .X).First();
closePoint.MarkerColor = Color.Red; // optionally mark the point

// optionally move clicked position to actual datapoint
nextDPoint = new PointF((float)closePoint.XValue, (float)closePoint.YValues[0]);

span = ax.Maximum - ax.Minimum; // the full range of values



Finally the code for the zoom-in button:



private void button1_Click(object sender, EventArgs e)

span /= 2; // zoom in 2x each time
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
ax.ScaleView.Zoom(nextDPoint.X - span, nextDPoint.X + span);



A few notes:



  • The conversion functions are only available after the chart has finished layout.

  • I only zoom the x-axis and I only zoom-in. Adding y-axis zoom and zooming-out should be simple..

  • changing the speed or step size is also not rocket science. Note that the steps should be a factor to allow a good user experience. If you add (instead of multiply) the zooming will not look linear but will either get faster or slower with each step..

  • Of course the markers and the visualization of the clicked points is optional and may not work well with your chart.

  • I use a PointF to store the clicked values. Usually floats will do; this may not be true if the values are DateTime, though. In such a case do use two doubles!

  • Note how I put the Axis into variables. I usually do the same with Series and ChartAreas. Much easier to write, read and I think even (a little) faster to run.. .





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246913%2fzoom-in-zoom-out-from-a-point-mschart%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Here is a solution that will zoom in 2x with each click.



    It uses a variable span as the range of values that will be visible in the zoomed view.



    It also moves the clicked position to the next closest DataPoint. You may remove this to allow zooming in on positions between DataPoints.



    Let's see it in action:



    enter image description here



    Note that the first issue with your code is the coordinates you use.



    These are three (!) coordinate systems in MSChart:




    • Pixels as in MouseClick


    • Values as in Axis values


    • Positions as in ElementPositions i.e. percentages related to the next higher container

    Your main issue is that you mix pixels with values which will result in chaos.



    You need to convert the pixels to values as in the code shown.



    Here are the variables I use:



    double span = 0; // axis range in values
    Point lastPoint = Point.Empty; // the clicked position
    PointF clickedValues = PointF.Empty; // the values of the clicked positiom
    DataPoint closePoint = null; // the next closest DataPoint


    Now the click code with the conversion:



    private void chart1_MouseClick(object sender, MouseEventArgs e)

    lastPoint = e.Location;
    Axis ax = chart1.ChartAreas[0].AxisX;
    Axis ay = chart1.ChartAreas[0].AxisY;

    if (closePoint != null) closePoint.MarkerColor = chart1.Series[0].MarkerColor;

    clickedValues = new PointF((float)ax.PixelPositionToValue(lastPoint.X),
    (float)ay.PixelPositionToValue(lastPoint.Y));

    closePoint = chart1.Series[0].Points.Where(x => x.XValue >= clickedValues .X).First();
    closePoint.MarkerColor = Color.Red; // optionally mark the point

    // optionally move clicked position to actual datapoint
    nextDPoint = new PointF((float)closePoint.XValue, (float)closePoint.YValues[0]);

    span = ax.Maximum - ax.Minimum; // the full range of values



    Finally the code for the zoom-in button:



    private void button1_Click(object sender, EventArgs e)

    span /= 2; // zoom in 2x each time
    Axis ax = chart1.ChartAreas[0].AxisX;
    Axis ay = chart1.ChartAreas[0].AxisY;
    ax.ScaleView.Zoom(nextDPoint.X - span, nextDPoint.X + span);



    A few notes:



    • The conversion functions are only available after the chart has finished layout.

    • I only zoom the x-axis and I only zoom-in. Adding y-axis zoom and zooming-out should be simple..

    • changing the speed or step size is also not rocket science. Note that the steps should be a factor to allow a good user experience. If you add (instead of multiply) the zooming will not look linear but will either get faster or slower with each step..

    • Of course the markers and the visualization of the clicked points is optional and may not work well with your chart.

    • I use a PointF to store the clicked values. Usually floats will do; this may not be true if the values are DateTime, though. In such a case do use two doubles!

    • Note how I put the Axis into variables. I usually do the same with Series and ChartAreas. Much easier to write, read and I think even (a little) faster to run.. .





    share|improve this answer


























      up vote
      3
      down vote



      accepted










      Here is a solution that will zoom in 2x with each click.



      It uses a variable span as the range of values that will be visible in the zoomed view.



      It also moves the clicked position to the next closest DataPoint. You may remove this to allow zooming in on positions between DataPoints.



      Let's see it in action:



      enter image description here



      Note that the first issue with your code is the coordinates you use.



      These are three (!) coordinate systems in MSChart:




      • Pixels as in MouseClick


      • Values as in Axis values


      • Positions as in ElementPositions i.e. percentages related to the next higher container

      Your main issue is that you mix pixels with values which will result in chaos.



      You need to convert the pixels to values as in the code shown.



      Here are the variables I use:



      double span = 0; // axis range in values
      Point lastPoint = Point.Empty; // the clicked position
      PointF clickedValues = PointF.Empty; // the values of the clicked positiom
      DataPoint closePoint = null; // the next closest DataPoint


      Now the click code with the conversion:



      private void chart1_MouseClick(object sender, MouseEventArgs e)

      lastPoint = e.Location;
      Axis ax = chart1.ChartAreas[0].AxisX;
      Axis ay = chart1.ChartAreas[0].AxisY;

      if (closePoint != null) closePoint.MarkerColor = chart1.Series[0].MarkerColor;

      clickedValues = new PointF((float)ax.PixelPositionToValue(lastPoint.X),
      (float)ay.PixelPositionToValue(lastPoint.Y));

      closePoint = chart1.Series[0].Points.Where(x => x.XValue >= clickedValues .X).First();
      closePoint.MarkerColor = Color.Red; // optionally mark the point

      // optionally move clicked position to actual datapoint
      nextDPoint = new PointF((float)closePoint.XValue, (float)closePoint.YValues[0]);

      span = ax.Maximum - ax.Minimum; // the full range of values



      Finally the code for the zoom-in button:



      private void button1_Click(object sender, EventArgs e)

      span /= 2; // zoom in 2x each time
      Axis ax = chart1.ChartAreas[0].AxisX;
      Axis ay = chart1.ChartAreas[0].AxisY;
      ax.ScaleView.Zoom(nextDPoint.X - span, nextDPoint.X + span);



      A few notes:



      • The conversion functions are only available after the chart has finished layout.

      • I only zoom the x-axis and I only zoom-in. Adding y-axis zoom and zooming-out should be simple..

      • changing the speed or step size is also not rocket science. Note that the steps should be a factor to allow a good user experience. If you add (instead of multiply) the zooming will not look linear but will either get faster or slower with each step..

      • Of course the markers and the visualization of the clicked points is optional and may not work well with your chart.

      • I use a PointF to store the clicked values. Usually floats will do; this may not be true if the values are DateTime, though. In such a case do use two doubles!

      • Note how I put the Axis into variables. I usually do the same with Series and ChartAreas. Much easier to write, read and I think even (a little) faster to run.. .





      share|improve this answer
























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        Here is a solution that will zoom in 2x with each click.



        It uses a variable span as the range of values that will be visible in the zoomed view.



        It also moves the clicked position to the next closest DataPoint. You may remove this to allow zooming in on positions between DataPoints.



        Let's see it in action:



        enter image description here



        Note that the first issue with your code is the coordinates you use.



        These are three (!) coordinate systems in MSChart:




        • Pixels as in MouseClick


        • Values as in Axis values


        • Positions as in ElementPositions i.e. percentages related to the next higher container

        Your main issue is that you mix pixels with values which will result in chaos.



        You need to convert the pixels to values as in the code shown.



        Here are the variables I use:



        double span = 0; // axis range in values
        Point lastPoint = Point.Empty; // the clicked position
        PointF clickedValues = PointF.Empty; // the values of the clicked positiom
        DataPoint closePoint = null; // the next closest DataPoint


        Now the click code with the conversion:



        private void chart1_MouseClick(object sender, MouseEventArgs e)

        lastPoint = e.Location;
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;

        if (closePoint != null) closePoint.MarkerColor = chart1.Series[0].MarkerColor;

        clickedValues = new PointF((float)ax.PixelPositionToValue(lastPoint.X),
        (float)ay.PixelPositionToValue(lastPoint.Y));

        closePoint = chart1.Series[0].Points.Where(x => x.XValue >= clickedValues .X).First();
        closePoint.MarkerColor = Color.Red; // optionally mark the point

        // optionally move clicked position to actual datapoint
        nextDPoint = new PointF((float)closePoint.XValue, (float)closePoint.YValues[0]);

        span = ax.Maximum - ax.Minimum; // the full range of values



        Finally the code for the zoom-in button:



        private void button1_Click(object sender, EventArgs e)

        span /= 2; // zoom in 2x each time
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;
        ax.ScaleView.Zoom(nextDPoint.X - span, nextDPoint.X + span);



        A few notes:



        • The conversion functions are only available after the chart has finished layout.

        • I only zoom the x-axis and I only zoom-in. Adding y-axis zoom and zooming-out should be simple..

        • changing the speed or step size is also not rocket science. Note that the steps should be a factor to allow a good user experience. If you add (instead of multiply) the zooming will not look linear but will either get faster or slower with each step..

        • Of course the markers and the visualization of the clicked points is optional and may not work well with your chart.

        • I use a PointF to store the clicked values. Usually floats will do; this may not be true if the values are DateTime, though. In such a case do use two doubles!

        • Note how I put the Axis into variables. I usually do the same with Series and ChartAreas. Much easier to write, read and I think even (a little) faster to run.. .





        share|improve this answer














        Here is a solution that will zoom in 2x with each click.



        It uses a variable span as the range of values that will be visible in the zoomed view.



        It also moves the clicked position to the next closest DataPoint. You may remove this to allow zooming in on positions between DataPoints.



        Let's see it in action:



        enter image description here



        Note that the first issue with your code is the coordinates you use.



        These are three (!) coordinate systems in MSChart:




        • Pixels as in MouseClick


        • Values as in Axis values


        • Positions as in ElementPositions i.e. percentages related to the next higher container

        Your main issue is that you mix pixels with values which will result in chaos.



        You need to convert the pixels to values as in the code shown.



        Here are the variables I use:



        double span = 0; // axis range in values
        Point lastPoint = Point.Empty; // the clicked position
        PointF clickedValues = PointF.Empty; // the values of the clicked positiom
        DataPoint closePoint = null; // the next closest DataPoint


        Now the click code with the conversion:



        private void chart1_MouseClick(object sender, MouseEventArgs e)

        lastPoint = e.Location;
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;

        if (closePoint != null) closePoint.MarkerColor = chart1.Series[0].MarkerColor;

        clickedValues = new PointF((float)ax.PixelPositionToValue(lastPoint.X),
        (float)ay.PixelPositionToValue(lastPoint.Y));

        closePoint = chart1.Series[0].Points.Where(x => x.XValue >= clickedValues .X).First();
        closePoint.MarkerColor = Color.Red; // optionally mark the point

        // optionally move clicked position to actual datapoint
        nextDPoint = new PointF((float)closePoint.XValue, (float)closePoint.YValues[0]);

        span = ax.Maximum - ax.Minimum; // the full range of values



        Finally the code for the zoom-in button:



        private void button1_Click(object sender, EventArgs e)

        span /= 2; // zoom in 2x each time
        Axis ax = chart1.ChartAreas[0].AxisX;
        Axis ay = chart1.ChartAreas[0].AxisY;
        ax.ScaleView.Zoom(nextDPoint.X - span, nextDPoint.X + span);



        A few notes:



        • The conversion functions are only available after the chart has finished layout.

        • I only zoom the x-axis and I only zoom-in. Adding y-axis zoom and zooming-out should be simple..

        • changing the speed or step size is also not rocket science. Note that the steps should be a factor to allow a good user experience. If you add (instead of multiply) the zooming will not look linear but will either get faster or slower with each step..

        • Of course the markers and the visualization of the clicked points is optional and may not work well with your chart.

        • I use a PointF to store the clicked values. Usually floats will do; this may not be true if the values are DateTime, though. In such a case do use two doubles!

        • Note how I put the Axis into variables. I usually do the same with Series and ChartAreas. Much easier to write, read and I think even (a little) faster to run.. .






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 at 5:38

























        answered Nov 12 at 10:02









        TaW

        40.5k62662




        40.5k62662



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246913%2fzoom-in-zoom-out-from-a-point-mschart%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto