From b1dcf45db61673d0c02f81eaf455cdc203bdb3b9 Mon Sep 17 00:00:00 2001 From: Vasek Sraier Date: Mon, 28 Sep 2020 11:40:06 +0200 Subject: [PATCH] graph: zooming and dragging, changed default repulsion force --- frontend/src/Editor.svelte | 26 +++++++++++++++++++++----- frontend/src/Graph.svelte | 29 ++++++++++++++++++++--------- frontend/src/GraphEdge.svelte | 2 -- frontend/src/GraphNode.svelte | 7 +++---- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/frontend/src/Editor.svelte b/frontend/src/Editor.svelte index 0ce6e55..0502bd6 100644 --- a/frontend/src/Editor.svelte +++ b/frontend/src/Editor.svelte @@ -8,10 +8,16 @@ let selectedTask: string | null = null; let clickedTask: string | null = null; - + let repulsionForce: number = -600; let clicked: string[] = []; function clickTask(e: CustomEvent) { + // sanity check + if (selectedTask == null) { + alert("tohle je divny event"); + return; + } + // ukladani seznamu poslednich kliknuti clicked.push(selectedTask); if (clicked.length > 3) @@ -41,6 +47,9 @@ } }); tasks = tasks; + + // run simulation + toggleDivnaPromena(); } else { alert("Nope, prvni musis nekam klikat..."); } @@ -48,7 +57,7 @@ let hovnoDivnaPromenaKteraJeFaktFuj = true; function toggleDivnaPromena() { - hovnoDivnaPromenaKteraJeFaktFuj = ! hovnoDivnaPromenaKteraJeFaktFuj; + hovnoDivnaPromenaKteraJeFaktFuj = !hovnoDivnaPromenaKteraJeFaktFuj; } @@ -62,7 +71,7 @@ display: flex; flex-direction: row; margin: 0; - height: 100vh; + height: 99vh; width: 100%; } @@ -106,7 +115,12 @@
Last clicked: {clicked.join(' | ')}
- +
@@ -115,12 +129,14 @@
+
- + Repulsion force:
{#if currentTask != null} + start

{currentTask}

{taskMap.get(currentTask).comment}
    diff --git a/frontend/src/Graph.svelte b/frontend/src/Graph.svelte index 9718918..9ca8f0e 100644 --- a/frontend/src/Graph.svelte +++ b/frontend/src/Graph.svelte @@ -10,6 +10,7 @@ export let tasks: TasksFile; export let selectedTask: null | string = null; + export let repulsionForce: number = -600; $: nodes = tasks.tasks; $: edges = createLinksFromTaskMap(tasks); @@ -45,7 +46,7 @@ }) // This provide the id of a node .links(edges) // and this the list of links ) - .force("charge", d3.forceManyBody().strength(-400)) // This adds repulsion between nodes. Play with the -400 for the repulsion strength + .force("charge", d3.forceManyBody().strength(repulsionForce)) // This adds repulsion between nodes. Play with the -400 for the repulsion strength .force("x", d3.forceX()) // attracts elements to the zero X coord .force("y", d3.forceY()) // attracts elements to the zero Y coord .on("tick", ticked) @@ -61,19 +62,29 @@ // run on create onMount(() => { // set the dimensions and margins of the graph - var margin = { top: 10, right: 30, bottom: 30, left: 40 }, - width = container.clientWidth - margin.left - margin.right, - height = container.clientHeight - margin.top - margin.bottom; + const width = container.clientWidth; + const height = container.clientHeight; // resize the svg object var svg = d3 .select(container) .select("svg") - .attr("width", width + margin.left + margin.right) - .attr("height", height + margin.top + margin.bottom) + .attr("width", width) + .attr("height", height) .attr("viewBox", [-width / 2, -height / 2, width, height]) - .select("g") - .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); + .select("g"); + + function zoomed(e) { + let { transform } = e; + svg.attr("transform", transform); + } + + d3.select(container).call( + d3 + .zoom() + .scaleExtent([0.1, 2]) + .on("zoom", zoomed) + ); runSimulation(); }); @@ -85,7 +96,6 @@ runSimulation(); } // now it's safe to stop vomitting 🤮 -