View Raw SPL
/*****************************************************************************
*                                                                            *
*   SETYRANGE.SPL Copyright (C) 2015 DSP Development Corporation             *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:       Randy Race                                                 *
*                                                                            *
*   Synopsis:     Sets Y autoscaled plotting range                           *
*                                                                            *
*   Revisions:    10 Sep 2015  RRR  Creation                                 *
*                                                                            *
*****************************************************************************/


#if @HELP_SETYRANGE

    SETYRANGE

    Purpose: Sets Y axis autoscaled plotting range

    Syntax:  SETYRANGE(win1, win2, ..., winN, yb, yt, ntics)

               winN - Optional. Zero or more windows. Defaults to the
                      current window.

                 yb - A real. The bottom window boundary.

                 yt - A real. The top window boundary.

              ntics - Optional. An integer, the number of tic values
                      to display. Defaults to 6 tics.

    Returns: Nothing. The window is scaled to the Y range values. The
             first and last tics are labeled.

    Example:
             W1: integ(gnorm(1000, 1));setyrange(-50, 50)

             W1 contains a series. The Y axis range is set to -50 and
             50 with 6 tic values spaced 20 units apart. The autoscaled
             range is also set to -50 and 50.

    Example:
             W1: integ(gnorm(1000, 1));setyrange(-50, 50, 9)

             Same as above except the Y axis has 9 tic values spaced
             12.5 units apart.

    Remarks: The tic interval is determined such that the first and last
             tics of the range are always labeled.

             SETYRANGE also sets the autoscaled range to the input
             range.

             Use SETYRANGE(-1, -1) to reset the plotting range to the
             automatic defaults.

    See Also:
             Autoscale
             Setxrange
             Sety
             Setyauto
             Ytic
#endif


/* set Y autoscale plotting range */
setyrange(argv)
{
        local j, v, yb, yt, nt, ntics, havewin;

        yb = yt = ntics = {};
        havewin = 0;

        /* get scalar args */
        loop (j = 1..argc)
        {
                v = getargv(j);

                if (isscalar(v))
                {
                        if (isempty(yb))
                        {
                                yb = v;
                        }
                        else if (isempty(yt))
                        {
                                yt = v;
                        }
                        else if (isempty(ntics))
                        {
                                ntics = v;
                        }
                }
                else if (iswindow(v))
                {
                        havewin = 1;
                }
        }

        if (isempty(yb) || isempty(yt))
        {
                error("setyrange - ybottom and ytop required");
        }

        if (havewin)
        {
                /* set each window */
                loop (j = 1..argc)
                {
                        v = getargv(j);

                        if (iswindow(v))
                        {
                                nt = isempty(ntics) ? (getylog(v) ? -1 : 6) : ntics;
                                setyrange_set(v, yb, yt, nt);
                        }
                }
        }
        else
        {
                /* set current window */
                nt = isempty(ntics) ? (getylog(w0) ? -1 : 6) : ntics;
                setyrange_set(w0, yb, yt, nt);
        }
}


/* set range and tics */
setyrange_set(w, yb, yt, ntics)
{
        local r, tics;

        /* range */
        r = abs(yt - yb);

        /* number of tics */
        ntics = (ntics > 0 && ntics < 2) ? 2 : ntics;

        /* tic interval */
        tics = (ntics < 0) ? ntics : r / (ntics - 1);

        if (yb == yt)
        {
                /* reset */
                tics = -1;
        }

        /* y range */
        sety(w, yb, yt);

        /* autoscale */
        setyauto(w, yb, yt);

        /* tic interval */
        setytic(w, tics);

        /* label first tic */
        setyticfirst(w, 1);
}