BookmarkSubscribeSubscribe to RSS Feed

Parallel Plot using Graph Builder - Script that is flexible with user-chosen variables

Community Trekker

Joined:

Nov 2, 2016

Hello.  I'm hoping somebody can help me with a small amount of scripting....

Using a script, I would like to be able to take a list of column variables (taken from the column dialog) and create a Parallel plot of these using the new graph builder.  The issue I’m having is that the column variable list can change each time the script is run and also be a different number each time (from 2 to 10 chosen variables). So, I have something like:   varlist=list(:Var1, :Var2, :Var3, :Var4, :Var5, :Var6, :Var7)  and was hoping to simply integrate Y(varlist) into the graph builder code.  However, it appears that each Y-value needs to be almost itemized (see code below).  Is there a fairly simple way to take my list and integrate it into the code, knowing that the list is continually changing each time the script is run?    Any help is appreciated!  Thanks, Jim Pappas

```varlist=list(:Var1, :Var2, :Var3, :Var4, :Var5, :Var6, :Var7);
Graph Builder(
Show Control Panel( 0 ),
Variables(
Y( :Var1 ),
Y( :Var2, Position( 1 ) ),
Y( :Var3, Position( 1 ) ),
Y( :Var4, Position( 1 ) ),
Y( :Var5, Position( 1 ) ),
Y( :Var6, Position( 1 ) ),
Y( :Var7, Position( 1 ) ),

),
Elements(
Parallel(
Y( 1 ),
Y( 2 ),
Y( 3 ),
Y( 4 ),
Y( 5 ),
Y( 6 ),
Y( 7 ),
Legend( 3 )
)
)
);

```

1 ACCEPTED SOLUTION

Accepted Solutions

Super User

Joined:

Jun 23, 2011

Solution

This will do it.  My example uses three variables.

``````dt = New Table( "TestVar", Add Rows( 5 ),
New Column( "Var1", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, 2, 3, 4, 5] ) ),
New Column( "Var2", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [3, 4, 5, 2, 1] ) ),
New Column( "Var3", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [5, 4, 3, 2, 1] ) )
);

varlist=list(:Var1, :Var2, :Var3);

gb_string = "dt << Graph Builder( Show Control Panel( 0 ), Variables(";

elements_string = "	Elements( Parallel(";

for (i = 1, i <= nitems(varlist), i++,

one_col = ":" || char(varlist[i]);

if (i == 1,
gb_string = gb_string || evalinsert(" Y(^one_col^)");
elements_string = elements_string || evalinsert("Y(^i^)");
,
gb_string = gb_string || evalinsert(", Y(^one_col^, Position(1))");
elements_string = elements_string || evalinsert(", Y(^i^)");
);
);

gb_string = gb_string || ")," || elements_string ||  " ,Legend( 3 ) ) ) )";

eval(parse(gb_string));``````
4 REPLIES

Super User

Joined:

Jun 23, 2011

I would loop over your selected column variables and build a dynamic graph builder string.  Then execute it with eval(parse()).

Super User

Joined:

Jun 23, 2011

Solution

This will do it.  My example uses three variables.

``````dt = New Table( "TestVar", Add Rows( 5 ),
New Column( "Var1", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, 2, 3, 4, 5] ) ),
New Column( "Var2", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [3, 4, 5, 2, 1] ) ),
New Column( "Var3", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [5, 4, 3, 2, 1] ) )
);

varlist=list(:Var1, :Var2, :Var3);

gb_string = "dt << Graph Builder( Show Control Panel( 0 ), Variables(";

elements_string = "	Elements( Parallel(";

for (i = 1, i <= nitems(varlist), i++,

one_col = ":" || char(varlist[i]);

if (i == 1,
gb_string = gb_string || evalinsert(" Y(^one_col^)");
elements_string = elements_string || evalinsert("Y(^i^)");
,
gb_string = gb_string || evalinsert(", Y(^one_col^, Position(1))");
elements_string = elements_string || evalinsert(", Y(^i^)");
);
);

gb_string = gb_string || ")," || elements_string ||  " ,Legend( 3 ) ) ) )";

eval(parse(gb_string));``````

Community Trekker

Joined:

Nov 2, 2016

Thank you so much!

Staff

Joined:

Jun 23, 2011

As an alternative to Peter's solution, you can always build the expression you need directly (see below). You may or may not regard this as 'simpler'.

``````Names Default To Here( 1 );
ClearLog();

// Example table
dt = New Table( "TestVar",
Add Rows( 5 ),
New Column( "Var1", Numeric, "Continuous", Set Values( [1, 2, 3, 4, 5] ) ),
New Column( "Var2", Numeric, "Continuous", Set Values( [3, 4, 5, 2, 1] ) ),
New Column( "Var3", Numeric, "Continuous", Set Values( [5, 4, 3, 2, 1] ) ),
New Column( "Var4", Numeric, "Continuous", Set Values( [5, 3, 1, 2, 4] ) )
);

// Dynamically build required subexpressions by looping over columns
vList = dt << getColumnNames;
varExpr = Expr( Variables() );
eleExpr = Expr( Parallel() );
For( v = 1, v <= N Items( vList ), v++,
varExpr = Insert( NameExpr(varExpr), Insert( Insert( Expr(Y()), NameExpr(vList[v])), Expr(Position(1))) );
eleExpr = Insert( NameExpr(eleExpr), Insert( Expr(Y()), v ) );
);
Print( varExpr, eleExpr );

// Assemble the final Graph Builder script
gbExpr = Expr( GraphBuilder() );
Insert Into( gbExpr, Name Expr( varExpr ) );
Insert Into( gbExpr, Insert( Expr( Elements() ), Name Expr( eleExpr ) ) );
Print( gbExpr );

// Run the script
gbExpr;``````