Subscribe Bookmark RSS Feed

Graph script to work with different numbers of Y variables

bents

Community Trekker

Joined:

May 28, 2013

I'm curious to see if there is a better way to do the script that is below.  Currently I have one function take the input from the user and make a list of all the possible Y values for the graph or graphs to follow.  Then the following function gets called to create the graphs.  I would like to know if there is some way I could dramatically shorten the amount of code needed by somehow using one or two graph builders instead of the 6 if statements containing their own graph builders

HALTbuildGraph=Function({},{tempcol="", vibcol=""},

      if(N Items(LogCheckBox << Get Selected())==1, //if log files were imported

            for(i=1, i<= n Col(), i++,

                  if(contains(char(column name(i)), "Temp") & contains(char(column name(i)), "Setpoint"),

                        tempcol=char(column name(i)),

                  /*else if*/ contains(char(column name(i)), "Vib") & contains(char(column name(i)), "Setpoint"),

                        vibcol=char(column name(i));

                  );

            );

            if(tempcol=="",  //if the Temperature Setpoint column was not found.

                  print("Error 111a: Temperature Setpoint not found");

                  throw()

            );

            if(vibcol=="",  //if the Vibration Setpoint column was not found.

                  print("Error 111b: Vibration Setpoint not found");

                  throw();

            );

            if(N Items(graphlist)==1,

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[1]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave),

            /*else if*/ N Items(graphlist)==2,

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[1]) ),

                              Y( column(graphlist[2]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1]||" & "||graphlist[2])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave),

            /*else if*/ N Items(graphlist)==3,

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[1]) ),

                              Y( column(graphlist[2]) ),

                              Y( column(graphlist[3]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 5 ),

                              Line( X, Y, Legend( 31 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1]||", "||graphlist[2]||", & "||graphlist[3])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave),

            /*else if*/ N Items(graphlist)==4,

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[1]) ),

                              Y( column(graphlist[2]) ),

                              Y( column(graphlist[3]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 5 ),

                              Line( X, Y, Legend( 31 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1]||", "||graphlist[2]||", & "||graphlist[3])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave);

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[4]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[4])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave),

            /*else if*/ N Items(graphlist)==5,

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[1]) ),

                              Y( column(graphlist[2]) ),

                              Y( column(graphlist[3]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 5 ),

                              Line( X, Y, Legend( 31 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1]||", "||graphlist[2]||", & "||graphlist[3])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave);

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[4]) ),

                              Y( column(graphlist[5]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[4]||" & "||graphlist[5])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave),

            /*else if*/ N Items(graphlist)==6,

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[1]) ),

                              Y( column(graphlist[2]) ),

                              Y( column(graphlist[3]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 5 ),

                              Line( X, Y, Legend( 31 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1]||", "||graphlist[2]||", & "||graphlist[3])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave);

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                              Y( column(graphlist[4]) ),

                              Y( column(graphlist[5]) ),

                              Y( column(graphlist[6]) )

                        ),

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 3 ),

                              Line( X, Y, Legend( 29 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 4 ),

                              Line( X, Y, Legend( 30 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 5 ),

                              Line( X, Y, Legend( 31 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[4]||" & "||graphlist[5])});

                        );

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave);

            ),


It continues but I think you get the picture...

Basically, I've tried using if statements in the Variables section of graph builder but JMP does not seem to like that at all.  I'm just looking for a way to shorten my code and make it easier to read.

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

You could create the graph builder JSL script on the fly as a text string and then run it using eval(parse()).  If there are common elements to your graphs this could simplify things.

Here's an example that shows how to do this.  Note that I'm using evalinsert to make it easier to substitute in variables.  Also I'm using \[ and ]\ so that I can have double quotes inside the string without needing to escape them.

dt = open("$sample_data\Big Class.jmp");

xvar  = ":weight";

yvar  = ":height";

byvar = ":sex";

gb_expr = evalinsert("\[

Graph Builder(

    Show Control Panel( 0 ),

    Variables( X( ^xvar^ ), Y( ^yvar^ ), Overlay( ^byvar^ ) ),

    Elements( Points( X, Y, Legend( 1 ) ), Smoother( X, Y, Legend( 3 ) ) ),

    SendToReport( Dispatch( {}, "Graph Builder", FrameBox, {Marker Size( 3 )} ) )

)]\");

eval(parse(gb_expr));

4 REPLIES
Solution

You could create the graph builder JSL script on the fly as a text string and then run it using eval(parse()).  If there are common elements to your graphs this could simplify things.

Here's an example that shows how to do this.  Note that I'm using evalinsert to make it easier to substitute in variables.  Also I'm using \[ and ]\ so that I can have double quotes inside the string without needing to escape them.

dt = open("$sample_data\Big Class.jmp");

xvar  = ":weight";

yvar  = ":height";

byvar = ":sex";

gb_expr = evalinsert("\[

Graph Builder(

    Show Control Panel( 0 ),

    Variables( X( ^xvar^ ), Y( ^yvar^ ), Overlay( ^byvar^ ) ),

    Elements( Points( X, Y, Legend( 1 ) ), Smoother( X, Y, Legend( 3 ) ) ),

    SendToReport( Dispatch( {}, "Graph Builder", FrameBox, {Marker Size( 3 )} ) )

)]\");

eval(parse(gb_expr));

bents

Community Trekker

Joined:

May 28, 2013

Thank you, I will be trying that out on Monday when I'm back at work.

bents

Community Trekker

Joined:

May 28, 2013

Thank you again for sharing that example and those commands.  Here is what the above code looks like now.  I'd say it is much better than it use to be.

HALTbuildGraph=Function({},{tempcol="", vibcol=""}, //11

      if(N Items(LogCheckBox << Get Selected())==1, //if log files were imported

            for(i=1, i<= n Col(), i++,

                  if(contains(char(column name(i)), "Temp") & contains(char(column name(i)), "Setpoint"),

                        tempcol=char(column name(i)),

                  /*else if*/ contains(char(column name(i)), "Vib") & contains(char(column name(i)), "Setpoint"),

                        vibcol=char(column name(i));

                  );

            );

            if(tempcol=="",  //if the Temperature Setpoint column was not found.

                  print("Error 111a: Temperature Setpoint not found");

                  throw()

            );

            if(vibcol=="",  //if the Vibration Setpoint column was not found.

                  print("Error 111b: Vibration Setpoint not found");

                  throw();

            );

            gb_expr = evalinsert("\[

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

            ]\");

            for(i=1, i<=N Items(graphlist) & i<=3, i++,

                  if(i==N Items(graphlist) | i==3,

                        gb_expr = gb_expr || evalinsert("\[

                              Y( graphlist[^i^] )

                        ),

                        ]\"),

                  /*else*/

                        gb_expr = gb_expr || evalinsert("\[

                              Y( graphlist[^i^] ),

                        ]\");

                  );

            );

            gb_expr = gb_expr || evalinsert("\[

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

            ]\");

            for(i=1, i<=N Items(graphlist) & i<=3, i++,

                  gb_expr = gb_expr || evalinsert("\[

                        Elements(

                              Position( 1, 2+^i^ ),

                              Line( X, Y, Legend( 28+^i^ ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        ]\");

            );

            gb_expr = gb_expr || evalinsert("\[

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[1]

            ]\");

            if(N Items(graphlist)==1,

                  gb_expr = gb_expr || evalinsert("\[)});

                        );

                  ]\");

            );

            for(i=2, i<=N Items(graphlist) & i<=3, i++,

                  if(i==N Items(graphlist) | i==3,

                        gb_expr = gb_expr || evalinsert("\[||", & "||graphlist[^i^])});

                        );

                        ]\"),

                  /*else*/

                        gb_expr = gb_expr || evalinsert("\[||", "||graphlist[^i^]

                        ]\");

                  );

            );

            gb_expr = gb_expr || evalinsert("\[

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave)

            ]\");

            eval(parse(gb_expr));

            if(N Items(graphlist)>3,

                  gb_expr = evalinsert("\[

                  gb=Graph Builder(

                        Variables(

                              X( :Test Time ),

                              Y( column(tempcol) ),

                              Y( column(vibcol) ),

                  ]\");

                  for(i=4, i<=N Items(graphlist), i++,

                        if(i==N Items(graphlist),

                              gb_expr = gb_expr || evalinsert("\[

                              Y( graphlist[^i^] )

                        ),

                              ]\"),

                        /*else*/

                              gb_expr = gb_expr || evalinsert("\[

                              Y( graphlist[^i^] ),

                              ]\");

                        );

                  );

                  gb_expr = gb_expr || evalinsert("\[

                        Elements(

                              Position( 1, 1 ),

                              Line( X, Y, Legend( 27 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        Elements(

                              Position( 1, 2 ),

                              Line( X, Y, Legend( 28 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                  ]\");

                  for(i=4, i<=N Items(graphlist), i++,

                        gb_expr = gb_expr || evalinsert("\[

                        Elements(

                              Position( 1, 2+^i^-3 ),

                              Line( X, Y, Legend( 28+^i^-3 ), Row order( 0 ), Summary Statistic( "Mean" ) )

                        ),

                        ]\");

                  );

                  gb_expr = gb_expr || evalinsert("\[

                        SendToReport(

                              Dispatch({}, "graph title", Text Edit Box, {Set Text (graphlist[4]

                  ]\");

                  if(N Items(graphlist)==4,

                        gb_expr = gb_expr || evalinsert("\[)});

                        );

                        ]\");

                  );

                  for(i=5, i<=N Items(graphlist), i++,

                        if(i==N Items(graphlist),

                              gb_expr = gb_expr || evalinsert("\[||", & "||graphlist[^i^])});

                        );

                              ]\"),

                        /*else*/

                              gb_expr = gb_expr || evalinsert("\[||", "||graphlist[^i^]

                              ]\");

                        );

                  );

                  gb_expr = gb_expr || evalinsert("\[

                  );

                  gb_rpt = report(gb);

                  gb_rpt[LegendBox(1)] << delete;

                  gb_rpt[PictureBox(1)] << Journal Window;

                  gb << close window(nosave)

                  ]\");

                  eval(parse(gb_expr));

            ),

pmroz

Super User

Joined:

Jun 23, 2011

Glad the example was helpful.  BTW you can trim your code a little:

  • You only need to use evalinsert() when you have variables you are substituting in (e.g. ^xcol^).

  • You only need to use the "\[  ]\" construct when you have embedded double quotes (e.g. "\[this is "quoted text" here. ]\" )