Page 1 of 1

Zooming with ZoomRect

Posted: Wed May 07, 2014 7:26 am
by 15658695
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 13016 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

Re: Zooming with ZoomRect

Posted: Wed May 07, 2014 7:40 am
by Christopher
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

Re: Zooming with ZoomRect

Posted: Wed May 07, 2014 8:02 am
by 15658695
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

Re: Zooming with ZoomRect

Posted: Wed May 07, 2014 9:45 am
by 15658695
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 12986 times
Very strange zooming :)

Re: Zooming with ZoomRect

Posted: Wed May 07, 2014 1:19 pm
by Christopher
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.

Re: Zooming with ZoomRect

Posted: Thu May 08, 2014 8:09 am
by 15658695
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

Re: Zooming with ZoomRect

Posted: Thu May 08, 2014 2:08 pm
by Christopher
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.