wcsPlotly = require("plotly.js-dist-min@2.35.2")
wcsRedraw = {
const g = wcsGrid
const axis = (t) => ({title: {text: t}})
const config = {displaylogo: false, responsive: true}
const defaultCamera = {eye: {x: 2, y: -2, z: 1.1}}
// This reads plotly internals: re-check the rotation when bumping plotly.
const camera = (div) => div._fullLayout?.scene?._scene?.getCamera?.() ?? defaultCamera
const flat = (color) => [[0, color], [1, color]]
const sheet = (z, color, name) => ({type: "surface", x: g.ages, y: g.bmis, z, name,
colorscale: flat(color), showscale: false, opacity: 0.75,
hovertemplate: `${name}<br>β_age %{x:.3f}<br>β_BMI %{y:.3f}<br>%{z:.1f}<extra></extra>`})
const point = (zs) => ({type: "scatter3d", mode: "markers", hoverinfo: "skip",
x: zs.map(() => wcsB.age), y: zs.map(() => wcsB.bmi), z: zs,
marker: {size: 5, color: "#000000"}})
const draw = (div, traces, zTitle) => wcsPlotly.react(div, traces, {
uirevision: "keep", paper_bgcolor: "#fbfbfb", showlegend: false,
margin: {l: 0, r: 0, t: 10, b: 0}, scene: {
xaxis: axis("β_age"), yaxis: axis("β_BMI"), zaxis: axis(zTitle),
aspectmode: "cube", camera: camera(div)}}, config)
const pt = wcsPoint
const zero = g.bmis.map(() => g.ages.map(() => 0))
draw(wcsCostDiv, [sheet(g.field("cost"), "#56B4E9", "C"), point([pt.cost])], "C")
draw(wcsGradDiv, [
sheet(g.field("gAge"), "#0072B2", "∂C/∂β_age"),
sheet(g.field("gBmi"), "#D55E00", "∂C/∂β_BMI"),
{...sheet(zero, "#999999", "0"), opacity: 0.3},
point([pt.gAge, pt.gBmi])], "gradient")
draw(wcsHessDiv, [
sheet(g.field("hAge"), "#0072B2", "∂²C/∂β_age²"),
sheet(g.field("hBmi"), "#D55E00", "∂²C/∂β_BMI²"),
sheet(g.field("hCross"), "#009E73", "∂²C/∂β_age∂β_BMI"),
point([pt.hAge, pt.hBmi, pt.hCross])], "Hessian")
// Turning one surface turns the others with it, while the reader drags.
// A surface that is following ignores its own camera events, so they never loop.
const divs = [wcsCostDiv, wcsGradDiv, wcsHessDiv]
const follow = (from) => (event) => {
const cam = event["scene.camera"]
if (!cam || from._wcsFollowing) return
for (const to of divs.filter((d) => d !== from)) {
to._wcsFollowing = true
wcsPlotly.relayout(to, {"scene.camera": cam}).finally(() => { to._wcsFollowing = false })
}
}
if (!wcsCostDiv._wcsLinked) {
for (const d of divs) {
d.on("plotly_relayouting", follow(d))
d.on("plotly_relayout", follow(d))
}
wcsCostDiv._wcsLinked = true
}
// A div drawn before it joins the page gets plotly's default width; fit it once it is shown.
requestAnimationFrame(() => divs.forEach((d) => wcsPlotly.Plots.resize(d)))
}