Subscribe Bookmark RSS Feed

Using variables in the SendToReport(Dispatch(...)) command

jbakri21

Community Trekker

Joined:

Jul 8, 2016

Hello all,

I am writing a JMP script that creates a window with check boxes and drop down menus. These menus are used to select conditions which can then be used to run reports and generate graphs. One of the graphs I am making has a different number of lines on it depending on how many check boxes are selected. I've successfully been able to generate the graph with all of the lines no matter which check boxes are selected. The problem I am having is with the legend. The first data set always appears with points showing up in the legend as well as the line. All the rest of the data sets on that graph have online their line represented on the legend. To fix this, normally I would double click on the legend, manually uncheck the points on the legend that I do not want displayed, and then copy the script to make this happen automatically. When there are 4 sets of data on the graph the script will look something like this after i make my changes to the legendbox:

SendToReport(

     Dispatch(

          {},

          "400",

          LegendBox,

          {Position( {-1, -3, -3, -3, 0, 1, 2, 3} )}

     )

     ... //(other Dispatch() commands)

)


I have other commands going to the graph such as axis label name changes so this appears among other Dispatch() commands within the SendToReport() command. The problem I am having is that I cannot get the list inside of the Postion() command to change depending on the number of check boxes selected. I can hard code the correct list but then the code only works for that number of selected check boxes. For example if i set the list as {-1, -3, -3, 0, 1, 2} then the code only works correctly when I have 3 check boxes selected. I created a variable that changes to the correct list depending on the number of check boxes selected (and verified that it is being assigned correctly) but when I put the variable inside the Position() function the code does not make the change I want it to. The code below assigns the correct list to the variable PL depending on how many check boxes are clicked (the variables inside the SUM() function represent the outputs of the checkboxes).


PL = If(

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 1, {-1, 0 },

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 2, {-1, -3, 0, 1},

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 3, {-1, -3, -3, 0, 1, 2},

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 4, {-1, -3, -3, -3, 0, 1, 2, 3},

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 5, {-1, -3, -3, -3, -3, 0, 1, 2, 3, 4},

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 6, {-1, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5},

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 7, {-1, -3, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5, 6},

  Sum(IW27, II09, II44, II08, II42, IW29, CC23, IW24) == 8, {-1, -3, -3, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5, 6, 7},

);

I have verified that this code is assigning the correct list to the variable. When I plug in the variable as follows it does not remove the points label from the legend box as I would like it to, and when I copy the script of the graph that I've generated the Dispatch() command to the legendbox is missing.

Dispatch(

     {},

     "400",

     LegendBox,

     {Position( PL )}

)


I have used variables in similar ways before, but I always reference one number in the list rather than try to insert an entire list (e.g. PL[1]). I have tried many variations of this but none have worked so far (PL[], {PL}, {PL[]}).


Please help me figure out what I am doing wrong. Please let me know if anything is unclear and I can provide any more detail that is needed to help answer the question. This is a very minor problem in the grand scheme of things but it is driving me crazy and I am having trouble moving past it without finding a solution. Help!

1 ACCEPTED SOLUTION

Accepted Solutions
Phil_Brown

Super User

Joined:

Mar 20, 2012

Solution

@jbakri21

In additions to Ian's suggestion. Here's another approach.

First if you have an IF statement as you do, I would use Choose() instead. The arguments of the conditions are a sequence of integers starting at 1, so the equality clauses ( expr==1, expr==2, expr==3) are built-in. (See JSL Syntax Reference for more)..

Having said that, this would be your new conditional statement:

PL = Choose( Sum( IW27, II09, II44, II08, II42, IW29, CC23, IW24 ),

{-1, 0},                           // 1

{-1, -3, 0, 1},                           // 2

{-1, -3, -3, 0, 1, 2},                           // 3

{-1, -3, -3, -3, 0, 1, 2, 3},                           // 4

{-1, -3, -3, -3, -3, 0, 1, 2, 3, 4},           // 5

{-1, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5},           // 6

{-1, -3, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5, 6},   // 7

{-1, -3, -3, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5, 6, 7}, // 8

     );

In order to use PL within another JSL statement, as you're trying to do with Dispatch(), the parameter variable needs to be wrapped within Expr(). So,

Dispatch(

     {},

     "400",

     LegendBox,

     {Position( Expr(PL) )}

)



This allows you to evaluate (or expand) the contents of PL, before the entire thing is evaluated. In order of this to work, you then have to wrap the entire charting function with Eval( Eval Expr ()). A complete example appears below:



dt = Open( "$SAMPLE_DATA/Australian Tourism.JMP" );

PL = {0, -1, 1, -1, 2, -1};

//PL =  {-1, 0, -1, 1, -1, 2};

Eval(

     Eval Expr(

        dt << Graph Builder(

        Show Control Panel( 0 ),

        Variables(

X( :Date ),

Y( :Name( "Revenue ($'000)" ) ),

Y( :Name( "Bed occupancy rate (%)" ) ),

Y( :Name( "Average length of stay (days)" ) )

    ),

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

    Elements( Position( 1, 2 ), Points( X, Y, Legend( 1 ) ), Smoother( X, Y, Legend( 2 ) ) ),

    Elements( Position( 1, 3 ), Points( X, Y, Legend( 6 ) ), Smoother( X, Y, Legend( 5 ) ) ),

SendToReport(

Dispatch( {}, "graph title", TextEditBox, {Set Text( "Tourism Trends" )} ),

Dispatch( {}, "400", LegendBox, {Position( Expr( PL ) )} )

    )

      )

    )

);



Hope this helps!

PDB
5 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

(Adapting a simpler example from the Scripting Index) you probably need to use this kind of approach (or maybe a slight variant):

Names Default To Here( 1 );

//This message applies to all display box objects

Open( "$SAMPLE_DATA/Big Class.jmp" );

biv = bivariate( y( :weight ), x( :height ) );

rbiv = biv << report;

ms = 5;

addMarkers = Expr( rbiv << Dispatch( {}, "Bivar Plot", FrameBox, {Marker Size( msTBD )} ) );

SubstituteInto(addMarkers, Expr(msTBD), ms);

addMarkers;

jbakri21

Community Trekker

Joined:

Jul 8, 2016

Thanks for your reply Ian. I understood what was going on until msTBD came up. I'm assuming this is the variable that will change depending on what check boxes are selected, but I can't see where the number of check boxes comes into play.

Phil_Brown

Super User

Joined:

Mar 20, 2012

Solution

@jbakri21

In additions to Ian's suggestion. Here's another approach.

First if you have an IF statement as you do, I would use Choose() instead. The arguments of the conditions are a sequence of integers starting at 1, so the equality clauses ( expr==1, expr==2, expr==3) are built-in. (See JSL Syntax Reference for more)..

Having said that, this would be your new conditional statement:

PL = Choose( Sum( IW27, II09, II44, II08, II42, IW29, CC23, IW24 ),

{-1, 0},                           // 1

{-1, -3, 0, 1},                           // 2

{-1, -3, -3, 0, 1, 2},                           // 3

{-1, -3, -3, -3, 0, 1, 2, 3},                           // 4

{-1, -3, -3, -3, -3, 0, 1, 2, 3, 4},           // 5

{-1, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5},           // 6

{-1, -3, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5, 6},   // 7

{-1, -3, -3, -3, -3, -3, -3, -3, 0, 1, 2, 3, 4, 5, 6, 7}, // 8

     );

In order to use PL within another JSL statement, as you're trying to do with Dispatch(), the parameter variable needs to be wrapped within Expr(). So,

Dispatch(

     {},

     "400",

     LegendBox,

     {Position( Expr(PL) )}

)



This allows you to evaluate (or expand) the contents of PL, before the entire thing is evaluated. In order of this to work, you then have to wrap the entire charting function with Eval( Eval Expr ()). A complete example appears below:



dt = Open( "$SAMPLE_DATA/Australian Tourism.JMP" );

PL = {0, -1, 1, -1, 2, -1};

//PL =  {-1, 0, -1, 1, -1, 2};

Eval(

     Eval Expr(

        dt << Graph Builder(

        Show Control Panel( 0 ),

        Variables(

X( :Date ),

Y( :Name( "Revenue ($'000)" ) ),

Y( :Name( "Bed occupancy rate (%)" ) ),

Y( :Name( "Average length of stay (days)" ) )

    ),

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

    Elements( Position( 1, 2 ), Points( X, Y, Legend( 1 ) ), Smoother( X, Y, Legend( 2 ) ) ),

    Elements( Position( 1, 3 ), Points( X, Y, Legend( 6 ) ), Smoother( X, Y, Legend( 5 ) ) ),

SendToReport(

Dispatch( {}, "graph title", TextEditBox, {Set Text( "Tourism Trends" )} ),

Dispatch( {}, "400", LegendBox, {Position( Expr( PL ) )} )

    )

      )

    )

);



Hope this helps!

PDB
jbakri21

Community Trekker

Joined:

Jul 8, 2016

Hi Philip,

This solution worked for me! Thanks a lot for you  help!!

Phil_Brown

Super User

Joined:

Mar 20, 2012

You're very welcome!

PDB