viewof glucoseEvent = Inputs.select(
["Meal spike", "Hypoglycemic drop", "Exercise drop", "Stable"],
{label: "Blood Glucose Event", value: "Hypoglycemic drop"}
)
viewof lagTime = Inputs.range([5, 45], {
value: 20,
step: 5,
label: "Tear Lag Time (minutes)"
})
// Generate time series data
bloodData = {
const times = d3.range(0, 121, 2); // 0-120 minutes
const baseGlucose = 100;
return times.map(t => {
let value;
if (glucoseEvent === "Meal spike") {
value = baseGlucose + Math.max(0, 60 * Math.exp(-((t-40)**2)/400));
} else if (glucoseEvent === "Hypoglycemic drop") {
value = t < 30 ? baseGlucose : baseGlucose - Math.min(50, (t-30) * 1.5);
} else if (glucoseEvent === "Exercise drop") {
value = t < 20 ? baseGlucose : baseGlucose - Math.min(30, (t-20) * 0.8);
} else {
value = baseGlucose + Math.sin(t/20) * 5;
}
return {time: t, glucose: Math.max(40, value), type: 'Blood'};
});
}
tearData = bloodData.map(d => ({
time: d.time,
glucose: bloodData[Math.max(0, Math.floor((d.time - lagTime)/2))]?.glucose || d.glucose,
type: 'Tear'
}))
combinedData = [...bloodData, ...tearData]
Plot.plot({
width: Math.min(700, width),
height: 400,
marginLeft: 60,
style: {background: "#F8F9FA"},
x: {label: "Time (minutes)", domain: [0, 120]},
y: {label: "Glucose (mg/dL)", domain: [40, 180]},
color: {
domain: ["Blood", "Tear"],
range: ["#E67E22", "#3498DB"]
},
marks: [
Plot.ruleY([70, 140], {stroke: "#7F8C8D", strokeDasharray: "4,4"}),
Plot.lineY(combinedData, {x: "time", y: "glucose", stroke: "type", strokeWidth: 3}),
Plot.text([{x: 110, y: 145, text: "High"}], {x: "x", y: "y", text: "text", fill: "#7F8C8D"}),
Plot.text([{x: 110, y: 65, text: "Low"}], {x: "x", y: "y", text: "text", fill: "#7F8C8D"})
]
})