View Raw SPL
/*****************************************************************************
*                                                                            *
*   SETSTRIPCHARTY.SPL   Copyright (C) 2011 DSP Development Corporation      *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Sets the y range of a stripchart window                     *
*                                                                            *
*   Revisions:   19 Jan 2011  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_SETSTRIPCHARTY

    SETSTRIPCHARTY

    Purpose: Sets the y-axis range for one or more traces of a stripchart.

    Syntax:  SETSTRIPCHARTY(win, ybottom, ytop, trace)

                  win - Optional window. Defaults to the current window.

              ybottom - A Real. Bottom coordinate boundary.

                 ytop - A Real. Top coordinate boundary.

                trace - A series or integer, the target trace or traces.

                          -1: all traces
                           0: current focus (default)
                         > 0: the target trace

    Returns: Nothing, the specified y coordinates are changed.

    Example:
             W1: stripchart(randn(100, 3));setstripcharty(-3, 3, -1)

             W1 contains a stripchart of 3 traces. The y-axis for all three
             traces is set to -3 to 3.

    Example:
             setstripcharty(W1, -10, 10, 2)

             Sets the y-axis range of the 2nd trace in W1 to -10 to 10.

    Example:
             setstripcharty(W1, -10, 10, {2, 3})

             Sets the y-axis range of the 2nd and 3rd traces in W1 to
             -10 to 10.

    Remarks:
             SETSTRIPCHARTY expands or compresses the target trace y
             axis of a stripchart. The remaining stripchart attributes
             are preserved.

    See Also:
             Setstripchartalt
             Setstripchartdir
             Setstripchartgap
             Setx
             Sety
             Stripchart
#endif


static strip_winnum, strip_pmode;

/* set y range for stripchart display */
setstripcharty(win, ybottom, ytop, trace)
{
        local winnum, pmode, numfoc, foc;

        /* args, returns window number */
        (winnum, ybottom, ytop, trace) = setstripcharty_parse_args(win, ybottom, ytop, trace);

        /* target window */
        win = castwindow(winnum);

        /* plot disabled */
        pmode = plotmode(win, 0);

        /* save for error handling */
        strip_winnum = winnum;
        strip_pmode  = pmode;

        if (length(trace) == 1)
        {
                /* cast one element array to int to test -1 value */
                trace = castint(trace);
        }

        if (not(isarray(trace)))
        {
                if (trace < 0)
                {
                        /* set all foci (traces) */
                        numfoc = getfocus(castwindow(winnum), -1);
                        trace = 1..numfoc;
                }
                else
                {
                        trace = {trace};
                }
        }
        
        loop (foc = trace)
        {
                /* set y axis for each target trace */
                setstripcharty_sety(winnum, ybottom, ytop, foc);
        }

        /* replot */
        plotmode(win, pmode);

}


/* set span and y range for stripchart display */
setstripcharty_sety(win, ybottom, ytop, n)
{
        local gap, nfoc, dir, f, yt, rt, rb, off, s, k, minval, maxval;
        local yt, yb;

        /* convert to window */
        win = castwindow(win);

        /* use current focus if no specified */
        if (n == 0) n = getfocus(win);

        /* number of overlays */
        nfoc = getfocus(win, -1);

        /* if trace in range ... */
        if (n >= 1 && n <= nfoc)
        {
                /* gap */
                gap = getstripchartgap(win);

                /* direction */
                dir = getstripchartdir(win);

                /* get current focus and sync */
                f = getfocus(win);
                s = getsync(win);

                /* focus on current trace with no syncing */
                focus(win, 1);
                sync(win, 0);

                /* prevent item processing for scaling */
                itemprocess(refseries(win, n), 0);

                /* focus on target */
                focus(win, n);

                /* current autocoords */
                (yb, yt) = setyauto(win);

                /* reset ytics and autoscaling */
                setytic(win, -1);
                setyauto(win, -1, -1);

                /* user scaling */
                sety(win, ybottom, ytop);
        
                /* stagger and span for this focus */
                staggery(win, 0);

                /* save tic interval */
                yt = ytic(win);

                /* user range */
                minval = ybottom;
                maxval = ytop;

                if (getylog(win))
                {
                        /* handle log scales */
                        spany(win, getyb(win), getyt(win));

                        /* use automatic range */
                        rt = log10(getyt(win));
                        rb = log10(getyb(win));
                }
                else
                {
                        /* linear scales */
                        if (minval == maxval)
                        {
                                minval -= 2 * yt;
                                maxval += 2 * yt;
                        }

                        /* set span */
                        spany(win, minval, maxval);

                        rt = ceil(maxval / yt)  * yt;
                        rb = floor(minval / yt) * yt;
                }

                /* total plot range including gap percentage */
                rg = (rt - rb) * (1 + gap / 100);

                /* plot factors for this plot */
                k = (nfoc - 1) / 2;

                /* direction 1:top-down, 0:bottom-up */
                off = (dir) ? n - nfoc : 1 - n;

                /* set y range to position this focus */
                if (getylog(win))
                {
                        sety(win, 10 ^ (rb + off * rg), 10 ^ (rt + (2 * k + off) * rg));
                }
                else
                {
                        sety(win, rb + off * rg, rt + (2 * k + off) * rg);
                }

                /* tics */
                setytic(win, yt);

                /* set Y autoscale parameters */
                setyauto(win, -1, -1);

                focus(win, f);
                sync(win, s);

                itemprocess(win, 1);
        }

        return();
}


/* parse args - returns window number */
setstripcharty_parse_args(win, ybottom, ytop, trace)
{
        if (argc < 4)
        {
                if (argc < 3)
                {
                        if (argc < 2) error("setstripcharty - ybottom and ytop required");
                        
                        if (iswindow(win))
                        {
                                error("setstripcharty - ybottom and ytop required");
                        }
                        
                        trace   = 0;
                        ytop    = ybottom;
                        ybottom = win;
                        win     = 0;
                }
                else
                {
                        if (iswindow(win))
                        {
                                trace = 0;
                                win   = getwnum(win);
                        }
                        else
                        {
                                trace   = ytop;
                                ytop    = ybottom;
                                ybottom = win;
                                win     = 0;
                        }
                }
        }

        if (iswindow(win))
        {
                win = getwnum(win);
        }

        return(win, ybottom, ytop, trace);
}


/* error handler - reset stripchart and plotmode */
setstripcharty_error(errnum, errmes)
{
        stripchart(castwindow(strip_winnum));
        plotmode(castwindow(strip_winnum), strip_pmode);
        error(errmes);
}