Zooming with ZoomRect

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Zooming with ZoomRect

Post by moelski.net » Wed May 07, 2014 7:26 am

Hi !

I have a problem in using the ZoomRect function.

For reproduction just place a chart on a form, add one FastLine Series and this code in FormLoad:

Code: Select all

            ((FastLine)this.tChart1[0]).DrawAllPoints = true;
            ((FastLine)this.tChart1[0]).FillSampleValues(10000);
Add a Button and insert this buttonclick code:

Code: Select all

            var strgPressed = ModifierKeys == Keys.Control;

            var addon = -5;
            if (strgPressed)
            {
                addon = 5;
            }

            var r = new Rectangle(
                        this.tChart1.DisplayRectangle.Left + addon,
                        this.tChart1.DisplayRectangle.Top + addon,
                        this.tChart1.DisplayRectangle.Right - addon,
                        this.tChart1.DisplayRectangle.Bottom - addon);

            this.tChart1.Zoom.ZoomRect(r);
What I want to do is zoom in (or zoom out) the chart.
leftright.png
leftright.png (44.87 KiB) Viewed 13080 times
If you click the button (without pressing STRG) you will zoom out. First question ... Why is the left space bigger then the right one (marked with red). Why is it not the same on both sides. Mayby DisplayRectangle is the wrong rectangle I should use for calculation?

Now hold the STRG key and press the button again. Now it should be zoom in (to the previous position). But now it Zooms out again. :shock:

Actually I have no idea what happend here :roll:

Dominik

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Zooming with ZoomRect

Post by Christopher » Wed May 07, 2014 7:40 am

moelski.net wrote: If you click the button (without pressing STRG) you will zoom out. First question ... Why is the left space bigger then the right one (marked with red). Why is it not the same on both sides. Mayby DisplayRectangle is the wrong rectangle I should use for calculation?
Instead of:

Code: Select all

this.tChart1.DisplayRectangle
Try:

Code: Select all

this.tChart1.Chart.ChartRect
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Re: Zooming with ZoomRect

Post by moelski.net » Wed May 07, 2014 8:02 am

Hi Christopher,

u made my day :D

Just in case someone else need a working sample:

Code: Select all

            var strgPressed = ModifierKeys == Keys.Control;

            var addon = -5;
            if (strgPressed)
            {
                addon = 5;
            }

            var x = this.tChart1.Chart.ChartRect.Left + addon;
            var y = this.tChart1.Chart.ChartRect.Top + addon;
            var w = (this.tChart1.Chart.ChartRect.Right - addon) - x;
            var h = (this.tChart1.Chart.ChartRect.Bottom - addon) - y;
            
            var r = new Rectangle(x, y, w, h);

            this.tChart1.Zoom.ZoomRect(r);
Dominik

moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Re: Zooming with ZoomRect

Post by moelski.net » Wed May 07, 2014 9:45 am

Hi Christopher,

I have one question left. I use the zoomed Event from the chart to Zoom, the custom Axes, too.
Now we have a layout where we stack the Y axes. The sample app has a TEE file for demonstrating.

If you use the Zoom button the X axis works like expected. But the Y Axes always do a Zoom out.
Any hint why this happens?
ProblemZoomout.png
ProblemZoomout.png (20.24 KiB) Viewed 13050 times
Very strange zooming :)
Attachments
CursorTest.zip
Sample Application
(11.62 KiB) Downloaded 663 times

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Zooming with ZoomRect

Post by Christopher » Wed May 07, 2014 1:19 pm

moelski.net wrote: Very strange zooming :)
This is because the call to:
this.tChart1.Zoom.ZoomRect(r);
does not use custom axes. You will have to set the Axis.SetMinMax() "manually" for each axes you want to zoom in the case of using custom axes.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

moelski.net
Newbie
Newbie
Posts: 19
Joined: Mon Mar 07, 2011 12:00 am

Re: Zooming with ZoomRect

Post by moelski.net » Thu May 08, 2014 8:09 am

Hi Christopher,
You will have to set the Axis.SetMinMax() "manually" for each axes you want to zoom in the case of using custom axes.
That is whar I did. Take a look at my code / or my sample:

Code: Select all

        private void tChart1_Zoomed(object sender, EventArgs e)
        {
            if (this.tChart1.Zoom.Direction == ZoomDirections.Both ||
                this.tChart1.Zoom.Direction == ZoomDirections.Vertical)
            {
                for (int i = 0; i < this.tChart1.Axes.Count; i++)
                {
                    var axis = this.tChart1.Axes[i];
                    if (!axis.Horizontal && axis.IsCustom())
                    {
                        axis.SetMinMax(
                            axis.CalcPosPoint(((TChart)sender).Zoom.y1),
                            axis.CalcPosPoint(((TChart)sender).Zoom.y0));
                    }
                }
            }
        }
But it did not work as expected for me. Maybe the calculation for the SetMinMax values is wrong?

Dominik

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Zooming with ZoomRect

Post by Christopher » Thu May 08, 2014 2:08 pm

Hello Dominik,
moelski.net wrote: But it did not work as expected for me. Maybe the calculation for the SetMinMax values is wrong?
You can try something like:

Code: Select all

        void tChart1_UndoneZoom(object sender, EventArgs e)
        {
          for (int i = 0; i < this.tChart1.Axes.Count; i++)
          {
            var axis = this.tChart1.Axes[i];


            if (!axis.Horizontal && axis.IsCustom())
            {
              axis.Automatic = true;
            }
          }
        }


        private void tChart1_Zoomed(object sender, EventArgs e)
        {
            if (this.tChart1.Zoom.Direction == ZoomDirections.Both ||
                this.tChart1.Zoom.Direction == ZoomDirections.Vertical)
            {
              Rectangle rect = Utils.FromLTRB(((TChart)sender).Zoom.x0, ((TChart)sender).Zoom.y0, ((TChart)sender).Zoom.x1, ((TChart)sender).Zoom.y1);

              Rectangle chartRect = tChart1.Chart.ChartRect;

              double ratio = 1.0 / ((double)chartRect.Height / (double)rect.Height);


              for (int i = 0; i < this.tChart1.Axes.Count; i++)
              {
                var axis = this.tChart1.Axes[i];
                if (!axis.Horizontal && axis.IsCustom() && (axis.IStartPos < rect.Bottom))
                {
                  axis.Automatic = false;
                  axis.Maximum *= ratio;
                  axis.Minimum *= ratio;
                }
              }
            }
        }
Here the axis is only being zoom if the bottom of the zoom rectangle is below the beginning of the axis. More "intelligence" can be added here to ensure the right axes are being zoomed at the right time.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply