在 GraphBox 中,if/Loop 语句不起作用。尊敬的 JMP 用户:以下是我在编写脚本时遇到的一个问题的简化描述:
我正在尝试将创建图形框的过程编码到一个函数(这里是“ 测试 ”)中,以便我以后可以轻松地循环执行此过程。
然而,这段代码片段无法正常工作,因为“ 如果 ”语句中的变量( 显示星座线 、 参考线数量 )似乎没有被正确求值,甚至根本没有被求值。即使我在函数参数中使用的其他变量( 宽度 、 高度 )都能正常工作,情况依然如此。这让我得出结论:问题出在图表框中的“ 如果 ”语句上。
Clear Globals();
Names Default To Here( 1 );
Test = Function({width, height, showHorLines, numReferenceLines}, {Default Local},
gb = GraphBox(
FrameSize(width, height),
XScale(0, width),
YScale(0, height),
XName("split"),
YName("color"),
//[] Generate grey horizontal lines
If(showHorLines == 1,
PenColor(65);
For(h = 0, h <= numReferenceLines, h++,
HLine(h*height/numReferenceLines)
);
PenColor(0),
);
);
win = NewWindow("new window", gb);
);
Test(200, 200, 1, 10);
有没有办法在不将图表框更改为其他对象的情况下解决此问题?
如果可能的话,最好能保留这个函数,因为我也曾经尝试过类似的方法,但使用了 Eval、EvalExpr 和 Expr,结果也失败了,遭遇了类似的命运。
此致
自动化和脚本编写基础数据分析与建模数据探索与可视化回复:在 GraphBox 中 if/loop 语句不起作用我建议使用 Eval( Eval Expr() )。对我来说,这个方法有效。
Names Default To Here( 1 );
Test = Function( {width, height, showHorLines, numReferenceLines},
{Default Local},
Eval(
Eval Expr(
gb = Graph Box(
FrameSize( Expr( width ), Expr( height ) ),
X Scale( 0, Expr( width ) ),
Y Scale( 0, Expr( height ) ),
XName( "split" ),
YName( "color" ),
If( Expr( showHorLines ) == 1,
Pen Color( 65 );
For( h = 0, h <= Expr( numReferenceLines ), h++,
H Line( h * Expr( height ) / Expr( numReferenceLines ) )
);
Pen Color( 0 );
,
)
);
win = New Window( "new window", gb );
)
)
);
Test( 200, 200, 1, 10 );
回复:在 GraphBox 中 if/loop 语句不起作用@nozellot简单解释一下:即使函数 Test 退出后,Graphbox 仍然会存在很长时间。Test 的参数会在函数退出时消失。附加到 Graphicbox 的脚本之后将无法找到这些参数。
@mmarchandFSLR正在使用我推荐的解决方案将参数转换为它们的值。通过识别它们
Expr( showHorLines )
并用……包裹起来
Eval Expr( ... )
它会计算所有 expr() 的值,并将计算结果保留下来,然后使用
Eval( ... )
要使用包含常量而不是变量的表达式来运行 GraphBox() 函数,您将得到一个不依赖于不再可用的参数的图形框。