spec2 = ({
width: 400,
height: 400,
view: {stroke: null},
data: {values: links_directed},
transform: [
{ lookup: "source",
from: { data: {values: nodes}, key: "index", fields: ["posX", "posY"]},
as: ["sourceX", "sourceY"]
},
{ lookup: "target",
from: { data: {values: nodes}, key: "index", fields: ["posX", "posY"]},
as: ["targetX", "targetY"]
},
{ fold: ["source", "target"], as: ["edgeType", "nodeIndex"]},
{ lookup: "nodeIndex",
from: { data: {values: nodes}, key: "index", fields: ["posX", "posY"]},
as: ["posX", "posY"]
}
],
layer: [
{ mark: "point",
selection: { "highlightNode": { type: "single", on: "mouseover", nearest: true,
field: "nodeIndex", init: {"nodeIndex": 2}}},
encoding: {
x: {field: "posX", type: "quantitative", scale: {domain: [0, 1]}, axis: null},
y: {field: "posY", type: "quantitative", scale: {domain: [0, 1]}, axis: null},
opacity: {value: 1}
}
},
{
mark: {type: "rule", stroke: "grey", strokeOpacity: 0.5},
encoding: {
x: {field: "sourceX", type: "quantitative"},
x2: {field: "targetX", type: "quantitative"},
y: {field: "sourceY", type: "quantitative"},
y2: {field: "targetY", type: "quantitative"},
size: {field: "weight", type: "quantitative", legend: null}
}
},
{
transform: [{filter: {selection: "highlightNode"}}],
mark: {type: "rule"},
encoding: {
x: {field: "sourceX", type: "quantitative"},
x2: {field: "targetX", type: "quantitative"},
y: {field: "sourceY", type: "quantitative"},
y2: {field: "targetY", type: "quantitative"},
size: {field: "weight", type: "quantitative"},
color: {field: "edgeType", type: "nominal", scale: {range: ["blue", "red"]}}
}
},
{ mark: {type: "circle", size: 600},
encoding: {
x: {field: "posX", type: "quantitative"},
y: {field: "posY", type: "quantitative"},
fill: {condition: { selection: "highlightNode", value: "tan"}, value: "yellow"},
stroke: {condition: { selection: "highlightNode", value: "maroon"}, value: "grey"},
strokeWidth: {condition: { selection: "highlightNode", value: 3}, value: 1}
}
},
{ mark: {type: "text", size: 14},
encoding: {
x: {field: "posX", type: "quantitative"},
y: {field: "posY", type: "quantitative"},
text: {field: "nodeIndex"}
}
}
]
})