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


#if @HELP_COMXLABEL

    COMXLABEL

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

    Syntax:   COMXLABEL(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:

              W1: gnorm(1000,1/1000);setvunits("V");setcomment("Channel 1")
              W2: sort(gnorm(1000,1/1000),1);setvunits("W");setcomment("Channel 2")
              W3: xy(W2, W1);comxlabel

              Labels the X axis with the comment and units of the X series.

    Remarks:
              COMXLABEL is useful when reading external tabular data to
              produce one or more XY series and each series comment is
              set to the column headers of the table.

    See Also:
              Comlabel
              Comxylabel
              Comylabel
              Getlabel
              Label
              Serxlabel
              Setcolheader
              Setcomment
              Setxlabel Setylabel
              Text 
              Wincaption
#endif


static wnum = -1;


/* label X axis with comment and units */
comxlabel(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))
                        {
                                comxlabel_set(v, comonly, nodefault, fmt);
                        }
                }
        }
        else
        {
                /* set current window */
                comxlabel_set(w0, comonly, nodefault, fmt);
        }
}


/* label X axis with comment and units */
comxlabel_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 */
                if (isxy(w))
                {
                        scom = getcomment(xvals(w));
                }
                else
                {
                        /* interval series, just use units */
                        scom = gethunits(w, j);
                } 

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

                if (not(comonly))
                {
                        ustr = gethunits(w, j);

                        /* ignore units if same as comment */
                        if (strcmp(scom, ustr) != 0)
                        {
                                /* format for units */
                                fmtstr  = (strlen(scom) > 0) ? "%s " : "%s";
                                fmtstr  = fmtstr + comlabfmt(fmt);

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

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

                if (strlen(scom) > 0)
                {
                        xlabel(w, scom);
                        xlabel_set_scales(w, j);
                }
        }

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

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


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

        return(0);
}