spec = {
const baseSpec = {
"width": visWidthMin,
"height": .4*visWidthMin,
"data": {"values": processedData},
"encoding": {
"x": {"field": "Age", "type": "quantitative", "title": "Age"}
},
"config": {
"legend": {"orient": "top", "layout": {"top": {"anchor": "middle"}}, "title": null}
},
"layer": [
{
"transform": [{"filter": "datum.Type === 'Survival Probability'"}],
"mark": "line",
"encoding": {
"y": {
"field": "Probability",
"type": "quantitative",
"title": "Survival Probability",
"axis": {"format": "%"}
},
"color": {
"field": "Type",
"type": "nominal",
"scale": {
"domain": ["Survival Probability", "Death Probability Density"],
"range": ["blue", "red"]
}
},
"tooltip": [
{"field": "Age", "type": "quantitative", "title": "Age"},
{"field": "Probability", "type": "quantitative", "title": "Probability", "format": ".1%"},
{"field": "Type", "type": "nominal"}
]
}
},
{
// Death probability density
"transform": [{"filter": "datum.Type === 'Death Probability Density'"}],
"mark": {"type": "area", "opacity": 0.3},
"encoding": {
"y": {
"field": "Probability",
"type": "quantitative",
"title": "Death Probability Density",
"axis": {"format": "%"},
"scale": {"zero": true}
},
"color": {"field": "Type", "type": "nominal"},
"tooltip": [
{"field": "Age", "type": "quantitative", "title": "Age"},
{"field": "Probability", "type": "quantitative", "title": "Probability", "format": ".2%"},
{"field": "Type", "type": "nominal"}
]
}
},
{
// Mean age line
"mark": {"type": "rule", "color": "#00AA00", "size": 2},
"encoding": {
"x": {"datum": stats.meanAge}
}
},
{
// Mean age label
"mark": {"type": "text", "align": "left", "dx": 5, "fill": "#00AA00"},
"encoding": {
"x": {"datum": stats.meanAge},
"y": {"value": 20}, // Position from top
"text": {"value": `Mean Age: ${stats.meanAge.toFixed(1)}`}
}
},
{
// Median age line
"mark": {"type": "rule", "color": "#FF4500", "size": 2},
"encoding": {
"x": {"datum": stats.medianAge}
}
},
{
// Median age label
"mark": {"type": "text", "align": "left", "dx": 5, "fill": "#FF4500"},
"encoding": {
"x": {"datum": stats.medianAge},
"y": {"value": 40}, // Position from top
"text": {"value": `Median Age: ${stats.medianAge.toFixed(1)}`}
}
}
],
"resolve": {
"scale": {
"y": "independent" // Make y-scales independent
}
}
};
return baseSpec;
}