mirror of
https://github.com/tinygrad/tinygrad.git
synced 2026-01-09 15:08:02 -05:00
simplify viz node labels (#13784)
This commit is contained in:
@@ -76,23 +76,18 @@ const drawGraph = (data) => {
|
|||||||
.attr("x", d => -d.width/2).attr("y", d => -d.height/2);
|
.attr("x", d => -d.width/2).attr("y", d => -d.height/2);
|
||||||
const STROKE_WIDTH = 1.4;
|
const STROKE_WIDTH = 1.4;
|
||||||
const labels = nodes.selectAll("g.label").data(d => [d]).join("g").attr("class", "label");
|
const labels = nodes.selectAll("g.label").data(d => [d]).join("g").attr("class", "label");
|
||||||
const hasLabelDims = data.nodes[0]?.value.labelWidth != null;
|
labels.attr("transform", d => `translate(-${d.labelWidth/2}, -${d.labelHeight/2+STROKE_WIDTH*2})`);
|
||||||
if (hasLabelDims) labels.attr("transform", d => `translate(-${d.labelWidth/2}, -${d.labelHeight/2+STROKE_WIDTH*2})`);
|
|
||||||
labels.selectAll("text").data(d => {
|
labels.selectAll("text").data(d => {
|
||||||
const ret = [[]];
|
const ret = [[]];
|
||||||
for (const { st, color } of parseColors(d.label, defaultColor="initial")) {
|
for (const s of parseColors(d.label, defaultColor="initial")) {
|
||||||
const lines = st.split("\n");
|
const color = darkenHex(s.color, 25);
|
||||||
|
const lines = s.st.split("\n");
|
||||||
ret.at(-1).push({ st:lines[0], color });
|
ret.at(-1).push({ st:lines[0], color });
|
||||||
for (let i=1; i<lines.length; i++) ret.push([{ st:lines[i], color }]);
|
for (let i=1; i<lines.length; i++) ret.push([{ st:lines[i], color }]);
|
||||||
}
|
}
|
||||||
return [ret];
|
return [ret];
|
||||||
}).join("text").selectAll("tspan").data(d => d).join("tspan").attr("x", "0").attr("dy", 14).selectAll("tspan").data(d => d).join("tspan")
|
}).join("text").selectAll("tspan").data(d => d).join("tspan").attr("x", "0").attr("dy", 14).selectAll("tspan").data(d => d).join("tspan")
|
||||||
.attr("fill", d => darkenHex(d.color, 25)).text(d => d.st).attr("xml:space", "preserve");
|
.attr("fill", d => d.color).text(d => d.st).attr("xml:space", "preserve");
|
||||||
// recenter after drawing texts if needed
|
|
||||||
if (!hasLabelDims) labels.attr("transform", (_,i,els) => {
|
|
||||||
const b = els[i].getBBox();
|
|
||||||
return `translate(${-b.x-b.width/2}, ${-b.y-b.height/2})`
|
|
||||||
});
|
|
||||||
addTags(nodes.selectAll("g.tag").data(d => d.tag != null ? [d] : []).join("g").attr("class", "tag")
|
addTags(nodes.selectAll("g.tag").data(d => d.tag != null ? [d] : []).join("g").attr("class", "tag")
|
||||||
.attr("transform", d => `translate(${-d.width/2+8}, ${-d.height/2+8})`).datum(e => e.tag));
|
.attr("transform", d => `translate(${-d.width/2+8}, ${-d.height/2+8})`).datum(e => e.tag));
|
||||||
// draw edges
|
// draw edges
|
||||||
|
|||||||
Reference in New Issue
Block a user