Hi @mcastner, I would say you picked a fairly complicated task to start scripting! Below is a way to do what you asked (if I interpreted you correctly), but I wonder if you could make this easier by using graph builder to draw a box plot and line at the same time. How do you come up with values for the reference lines? Any chance that is a summary of the available data (median, quantile, etc), or could you do that in a formula column?
Names default to here(1);
dt = Open( "$Sample_data/big class.jmp" );
//Valeus for ref lines - hopefully populated by your script
reflines = Associative Array({
{"height",
Associative Array({
{ 12, 62 },
{ 13, 63 },
{ 14, 64 },
{ 15, 65 },
{ 16, 66 },
{ 17, 67 }
})
},
{"weight",
Associative Array({
{ 12, 112 },
{ 13, 113 },
{ 14, 114 },
{ 15, 115 },
{ 16, 116 },
{ 17, 117 }
})
}
});
//A window with a single data filter that will be applied to all graphs inside it
New Window("charts",
Data Filter Context Box(
H List Box(
ldf = dt << Data Filter( Local,
Add Filter(
columns( :age ),
Where( :age == 13 ),
Display( :age, N Items( 6 ) )
),
Mode( Select( 0 ), Show( 1 ), Include( 1 ) )
),
vc1 = Variability Chart(
Y( :height ),
X( :sex )
);
vc2 = Variability Chart(
Y( :weight ),
X( :sex )
);
)
)
);
//Function that is called when data filter changes and when the window is first opened, this will redraw lines on charts
UpdateRefLines = function({},
//find out what ages were selected, there is probably a better way to do this too
filteredagelast = filteredage;
filteredage = median(dt:age[ldf << get filtered rows()]);
//detect if there was a change in the age (prevents flickering of the screen)
if(is missing(filteredagelast) | (filteredagelast != filteredage),
//remove any existing ref lines
(vc1 << XPath("//AxisBox[@charID=2]"))[1] << Revert Axis;
(vc2 << XPath("//AxisBox[@charID=2]"))[1] << Revert Axis;
//draw new lines
(vc1 << XPath("//ScaleBox[@ID=2]")) << Add Ref Line( reflines["height"][FilteredAge], "Solid", "Black", "", 1 );
(vc2 << XPath("//ScaleBox[@ID=2]")) << Add Ref Line( reflines["weight"][FilteredAge], "Solid", "Black", "", 1 );
)
);
//function called when data filter changes, here it only redraws reference lines
UpdatesOnRowStateChanges = function({x}, if(is matrix(x), UpdateRefLines ) );
//variable used to keep track of the last filter applied
filteredage = .;
//Assign function to data filter
rsh = vc1 << Make Row State Handler( dt, UpdatesOnRowStateChanges );
//draw initial reference lines
UpdateRefLines();