使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法全部,
我想詢問編寫具有多個參數的 GB 圖腳本的最佳方法。
假設我有一個這樣的表:
New Table( "Untitled 140",
Add Rows( 12 ),
New Column( "Category",
Character,
"Nominal",
Set Values( {"A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D", "D"} )
),
New Column( "Value1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 4, 2, 3, 6, 2, 3, 5, 4, 1, 3, 6] )
),
New Column( "Value2",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [6, 3, 5, 2, 8, 6, 5, 2, 6, 3, 5, 4] )
)
)我想編寫這樣的情節:

自動生成的腳本如下所示:
;Graph Builder(
Size( 522, 450 ),
Show Control Panel( 0 ),
Variables( X( :Category ), Y( :Value1 ), Y( :Value2, Position( 1 ) ) ),
Elements( Bar( X, Y( 1 ), Y( 2 ), Legend( 4 ), Summary Statistic( "Range" ) ) )
)
問題是 Y 列的數量是任意的,並且名稱也是任意的。
對於像變異圖這樣的圖,我會這樣:
parameters = {:"Value1"n, :"Value2"n};
yExpr = Expr( Y() );
For Each( {value, index}, parameters, Insert Into( yExpr, Name Expr( value ), 1 ) );
Eval(
Substitute(
Expr(
Variability Chart( placeholder, X( :Category ), Std Dev Chart( 0 ) )
),
Expr( placeholder ), Name Expr( yExpr )
)
);我在其中接收列名稱作為列表,然後構造一個 Y 表達式,然後將其放入圖表的腳本中。 它會給我一個變異圖列表,但這對我來說沒問題。
如果我想在 GB 的情況下採取相同的路線,看起來我將不得不構造兩個表達式:
Y( :Value1 ), Y( :Value2, Position( 1 ) )
和
Elements( Bar( X, Y( 1 ), Y( 2 ), Legend( 4 ), Summary Statistic( "Range" ) ) )
這基本上就是整個腳本。
我的問題:有沒有更好的方法而不是用表達式進行一堆替換來組裝這兩行?
謝謝!
消費者和市場研究回复:使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法好的!感謝您添加這種感覺。希望我自己也嘗試過!
回复:使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法@pmroz這是沒有 Eval(Parse()) 的相同代碼。我同意這更乾淨!
Names default to here(1);
dt = New Table( "Test",
Add Rows( 12 ),
New Column( "Category", Character, "Nominal",
Set Values( {"A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D", "D"} ) ),
New Column( "Value1", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, 4, 2, 3, 6, 2, 3, 5, 4, 1, 3, 6] ) ),
New Column( "Value2", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [6, 3, 5, 2, 8, 6, 5, 2, 6, 3, 5, 4] ) ),
New Column( "Value3", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, 4, 3, 6, 5, 7, 3, 4, 2, 8, 6, 1] ) )
);
dt_cols = dt << get column references();
remove from(dt_cols, 1); // Remove Category
dts = dt << Stack(invisible,
columns( dt_cols ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" )
);
dts << Graph Builder(
Size( 518, 448 ),
Show Control Panel( 0 ),
Variables( X( :Category ), Y( :Data ), Overlay( :Label ) ),
Elements( Bar( X, Y, Legend( 7 ) ) )
);
回复:使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法不要忘記堆命令。需要 eval(parse()) 但它在其他方面非常乾淨。這是一個要繪製三列的示例。
dt = New Table( "Test",
Add Rows( 12 ),
New Column( "Category",
Character,
"Nominal",
Set Values( {"A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D", "D"} )
),
New Column( "Value1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 4, 2, 3, 6, 2, 3, 5, 4, 1, 3, 6] )
),
New Column( "Value2",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [6, 3, 5, 2, 8, 6, 5, 2, 6, 3, 5, 4] )
),
New Column( "Value3",
Numeric,
"Continuous",
Format( "Best", 12 ),
Set Values( [1, 4, 3, 6, 5, 7, 3, 4, 2, 8, 6, 1] )
)
);
dt_cols = dt << get column names(string);
removefrom(dt_cols, 1); // Remove Category
// Create the list of columns, like this: ":Value1, :Value2, :Value3"
col_list = ":" || concat items(dt_cols, ", :");
stack_str = evalinsert(
"\[dts = dt << Stack(invisible,
columns( ^col_list^ ),
Source Label Column( "Label" ),
Stacked Data Column( "Data" ))]\");
eval(parse(stack_str));
dts << Graph Builder(
Size( 518, 448 ),
Show Control Panel( 0 ),
Variables( X( :Category ), Y( :Data ), Overlay( :Label ) ),
Elements( Bar( X, Y, Legend( 7 ) ) )
);

回复:使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法這仍然使用循環來構建這些表達式,但它連接參數列表而不是替換它們:
Names default to here(1);
//call another function (f) with the arguments provided in a list (l)
do.call = function({l,f},
Substitute(
Name Expr(l),
Expr({}),
Name Expr(f)
);
);
dt = New Table( "Data",
Add Rows( 12 ),
New Column( "Category", Character, "Nominal",
Set Values( {"A", "A", "A", "B", "B", "B", "C", "C", "C", "D", "D", "D"} )
),
New Column( "Value1", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [1, 4, 2, 3, 6, 2, 3, 5, 4, 1, 3, 6] )
),
New Column( "Value2", Numeric, "Continuous", Format( "Best", 12 ),
Set Values( [6, 3, 5, 2, 8, 6, 5, 2, 6, 3, 5, 4] )
)
);
vars = {:Value1, :Value2};
VarExpr = do.call(
{ X( :Category ) } || transform each({v}, vars, eval expr( Y( Expr( Name Expr( v ) ), Position( 1 ) ) ) ),
expr(Variables())
);
BarExpr = do.call(
{ X } || transform each({v, i}, vars, eval expr( Y( Expr( i ) ) ) ) || { Legend( 4 ), Summary Statistic( "Range" ) },
expr(Bar())
);
Eval( Eval Expr(
dt << Graph Builder(
Size( 522, 450 ),
Show Control Panel( 0 ),
Expr( Name Expr( VarExpr ) ),
Elements( Expr( Name Expr( BarExpr ) ) )
)
) );回复:使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法我通常使用與您類似的方法來構建此類圖形生成器,這可能是一本很好的讀物回复:您可以在不將表達式寫入字符串的情況下構造這個嗎?
回复:使用 JSL 繪製具有多個 Y 變量的 GraphBuilder 圖的最佳方法好吧,我就是這樣解決的,但是有沒有更簡單的方法呢? 不敢相信要編寫如此簡單的腳本,我需要考慮這種複雜性......:
parameters = {:"Value1"n, :"Value2"n};
yExpr = Expr( Y() );
yExprPos = Expr( Y( Position( 1 ) ) );
barExpr = Expr(
Bar( X, Summary Statistic( "Range" ) )
);
yNExpr = Expr( Y() );
variablesExpr = Expr(
Variables( X( :Category ) )
);
For Each( {value, index}, parameters,
Insert Into( yExpr, Name Expr( value ), 1 );
yExprPos = Expr( Y( Position( 1 ) ) );
Insert Into( yExprPos, Name Expr( value ), 1 );
Insert Into( variablesExpr, Name Expr( yExprPos ) );
//This is for Y(1) etc. in Bar()
yNExpr = Expr( Y() );
Insert Into( yNExpr, Name Expr( index ) );
Insert Into( barExpr, Name Expr( yNExpr ), 2 );
);
//Name Expr( yExpr );
Show(Name Expr( variablesExpr ));
Show(Name Expr( barExpr ));
gbExpr = Expr(
Graph Builder( Size( 522, 450 ), Show Control Panel( 0 ), placeholder1, Elements( placeholder2 ) )
);
Eval(
Substitute(
Substitute(
Name Expr(gbExpr),
Expr( placeholder1 ),
Name Expr( variablesExpr )
),
Expr( placeholder2 ),
Name Expr( barExpr )
)
);