From b6feb64d98bb4abf0a2f6ced0482688609d3e425 Mon Sep 17 00:00:00 2001 From: zyansheep Date: Tue, 19 Dec 2023 23:40:50 -0500 Subject: [PATCH 1/2] add support for displaying link types --- core/frontend/graph.js | 34 ++++++++++++++++++++++++++++++++++ core/i18n.yml | 3 +++ static/template/cosmoscope.njk | 6 ++++++ 3 files changed, 43 insertions(+) diff --git a/core/frontend/graph.js b/core/frontend/graph.js index d47459fd..e2740006 100644 --- a/core/frontend/graph.js +++ b/core/frontend/graph.js @@ -87,6 +87,23 @@ simulation.on('tick', function () { .attr('y1', (d) => d.source.y) .attr('x2', (d) => d.target.x) .attr('y2', (d) => d.target.y); + + elts.linkLabels + .attr("x", (d) => (d.source.x + d.target.x) / 2) + .attr("y", (d) => (d.source.y + d.target.y) / 2) + .attr("text-anchor", "middle") + .attr("transform", d => { + var angle = Math.atan2(d.target.y - d.source.y, d.target.x - d.source.x) * 180 / Math.PI; + + // Adjust angle to avoid upside-down text + if (angle > 90 || angle < -90) { + angle = (angle + 180) % 360; + } + + var x = (d.source.x + d.target.x) / 2; + var y = (d.source.y + d.target.y) / 2; + return `rotate(${angle},${x},${y})`; + }); elts.nodes.attr('transform', (d) => 'translate(' + d.x + ',' + d.y + ')'); @@ -130,6 +147,15 @@ if (graphProperties.graph_arrows === true) { elts.links.attr('marker-end', 'url(#arrow)'); } +elts.linkLabels = svgSub + .append('g') + .selectAll("text") + .data(data.edges) + .enter() + .append("text") + .attr('font-size', graphProperties.graph_text_size) + .text(d => d.attributes.type); // Assuming each link has a label + const strokeWidth = 2; /** @type {d3.Selection} */ @@ -493,6 +519,14 @@ window.labelDisplayToggle = function (isChecked) { } }; +window.linkLabelDisplayToggle = function (isChecked) { + if (isChecked) { + elts.linkLabels.style('display', null); + } else { + elts.linkLabels.style('display', 'none'); + } +}; + /** * Change the font size of graph labels */ diff --git a/core/i18n.yml b/core/i18n.yml index fd72c5f1..7c4c3527 100644 --- a/core/i18n.yml +++ b/core/i18n.yml @@ -81,6 +81,9 @@ left_panel: input_labels_show: fr: Afficher les étiquettes en: Display labels + input_link_labels_show: + fr: Afficher les étiquettes liens + en: Display link labels input_highlight_hover_node: fr: Surbrillance au survol en: Highlight on hover diff --git a/static/template/cosmoscope.njk b/static/template/cosmoscope.njk index e33eff4d..cd2002f9 100644 --- a/static/template/cosmoscope.njk +++ b/static/template/cosmoscope.njk @@ -176,6 +176,12 @@ {{ translation.left_panel.menu_controls.input_labels_show[lang] }} +
  • + +