View Raw SPL
/*****************************************************************************
*                                                                            *
*   COMYLABEL.SPL   Copyright (C) 2015 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:         Randy Race                                               *
*                                                                            *
*   Synopsis:       Labels the Y axis with the series comment and units      *
*                                                                            *
*   Revisions:      27 Apr 2015  RRR  Creation                               *
*                                                                            *
*****************************************************************************/


#if @HELP_COMYLABEL

    COMYLABEL

    Purpose:  Labels the Y axis with the series comment and units.

    Syntax:   COMYLABEL(win, commentonly, nodefault, fmt)

                      win - Optional. The target window. Defaults to the
                            current window.

              commentonly - Optional. An integer, display comment only.

                              0: Display both comment and units (default).
      
                              1: Display comment only.
    
                nodefault - Optional. An integer, do not display default
                            comment.

                              0: Display default comment.
      
                              1: Do not display comment if it is the default
                                 comment (default).

                      fmt - Optional. An integer, the units grouping format.

                              0: Enclose the units in ()'s (default).
  
                              1: Enclose the units in []'s.
 
    Example:
              gnorm(1000, 1/1000);setvunits("V");setcomment("Channel 1");comylabel

              Labels the Y axis with the series comment and units.

    Remarks:
              COMYLABEL is useful when reading external tabular data
              where each series comment is set to the column headers of
              the table.

    See Also:
              Comlabel
              Comxlabel
              Comxylabel
              Getlabel
              Label
              Serylabel
              Setcolheader
              Setcomment
              Setxlabel Setylabel
              Text 
              Wincaption
#endif


static wnum = -1;

comylabel(argv)
{
        local comonly, nodefault, fmt, j, havewin;

        comonly = nodefault = fmt = {};
        havewin = 0;

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

                if (isscalar(v))
                {
                        if (isempty(comonly))
                        {
                                comonly = v;
                        }
                        else if (isempty(nodefault))
                        {
                                nodefault = v;
                        }
                        else if (isempty(fmt))
                        {
                                fmt = v;
                        }
                }
                else if (iswindow(v))
                {
                        havewin = 1;
                }
        }

        if (isempty(comonly))
        {
                comonly = 0;
        }

        if (isempty(nodefault))
        {
                nodefault = 1;
        }

        if (isempty(fmt))
        {
                fmt = 0;
        }

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

                        if (iswindow(v))
                        {
                                comylabel_set(v, comonly, nodefault, fmt);
                        }
                }
        }
        else
        {
                /* set current window */
                comylabel_set(w0, comonly, nodefault, fmt);
        }
}


/* label window Y axis with comment and units */
comylabel_set(w, comonly, nodefault, fmt)
{
        local scom, dcom, j, foc, fmtstr, ustr, pmode;

        /* force window */
        w = castwindow(w);

        foc  = getfocus(w);
        wnum = getwnum(w);

        /* default worksheet comment */
        dcom = getwksattrib("defaultcomment");

        pmode = plotmode(w, 0);

        /* label each overlay */
        loop (j = 1.. numfocus())
        {
                focus(w, j);

                /* series comment */
                scom = getcomment(w, j);

                /* check if we do not use default comment */
                if (nodefault)
                {
                        if (strcmp(scom, dcom) == 0)
                        {
                                scom = "";
                        }
                }

                if (not(comonly))
                {
                        /* format for units */
                        fmtstr  = (strlen(scom) > 0) ? "%s " : "%s";
                        fmtstr  = fmtstr + comlabfmt(fmt);
                        ustr    = getvunits(w, j);

                        if (getconfig("tex_processing") > 0)
                        {
                                /* let TeX format units */
                                ustr = sprintf("$\eunits{%s}$", ustr);
                        }

                        scom = sprintf(fmtstr, scom, ustr);
                }

                if (strlen(scom) > 0)
                {
                        ylabel(w, scom);

                        ylabel_set_scales(w, j);
                }
        }

        focus(w, foc);
        wnum = -1;

        if (pmode)
        {
                plotmode(w, 1, 0);
        }
}


/* error handler - resets plot mode */        
comylabel_error(errnum, errmes)
{
        if (wnum != -1)
        {
                plotmode(castwindow(wnum), 1, 1);
                wnum = -1;
        }

        return(0);
}