Untitled conversation
- User
[{locationId:"someLocationId ", id:"someId", title:"название кнопки" content:"текст",choices:[someId, someId]},{locationId:"someLocationId", id:"someId", title:"название кнопки" content: "text",choices:[someId, someId]},...] ///////////////////////// write JS func to visualize data format, we need it to show rectangles connected with lines, so we can move them with mouse to see the whole structure
- Assistant
To visualize the data format and create an interactive structure with rectangles and lines, you can use an HTML file with JavaScript and an external library called D3.js to make the manipulation and visualization easier. Here's an example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://d3js.org/d3.v5.js"></script> <title>Data Visualizer</title> <style> .node { cursor: move; fill: #ccc; stroke: #000; } .link { stroke: #000; stroke-width: 2px; } .node-text { font-family: "Arial"; font-size: 12px; pointer-events: none; } </style> </head> <body> <script> // Your data const data = [ { locationId: "someLocationId", id: "someId1", title: "название кнопки", content: "текст", choices: ["someId2", "someId3"], }, { locationId: "someLocationId", id: "someId2", title: "название кнопки", content: "text", choices: ["someId4", "someId5"], }, { locationId: "someLocationId", id: "someId3", title: "название кнопки", content: "text", choices: ["someId4", "someId6"], }, ]; // Constants const width = window.innerWidth; const height = window.innerHeight; const text_padding = 10; // Create the SVG container const svg = d3 .select("body") .append("svg") .attr("width", width) .attr("height", height); // Create the node and link containers const nodes = svg.append("g").selectAll(".node"); const links = svg.append("g").selectAll(".link"); // Create the drag behavior const drag = d3 .drag() .on("start", onDragStart) .on("drag", onDrag) .on("end", onDragEnd); // Update the visualization function update() { // JOIN: Associate the data to the elements const node = nodes.data(data, (d) => d.id); const link = links.data(data.filter((d) => d.choices.length > 0)); // EXIT: Remove exiting elements node.exit().remove(); node.selectAll("text").exit().remove(); link.exit().remove(); // UPDATE: Update elements if needed (not necessary in this case) // ENTER: Create new elements const nodeEnter = node .enter() .append("g") .attr("class", "node") .call(drag); nodeEnter.append("rect").attr("fill", "white"); nodeEnter.append("text").attr("class", "node-text"); // MERGE: To apply operations to both ENTER and UPDATE nodeEnter .merge(node) .select("rect") .attr("x", (d) => d.x) .attr("y", (d) => d.y) .attr("width", (d) => d.title.length * 10 + 2 * text_padding) .attr("height", 30); nodeEnter .merge(node) .select("text") .attr("x", (d) => d.x + text_padding) .attr("y", (d) => d.y + 20) .text((d) => d.title); link .enter() .append("line") .attr("class", "link") .attr("x1", (d) => d.x + d.title.length * 5 + text_padding) .attr("y1", (d) => d.y + 15) .attr("x2", (d) => d.x + 100) // Here you need to calculate the x2 and y2 position based on the target node .attr("y2", (d) => d.y); } // Drag behavior callbacks function onDragStart(d) { d3.select(this).raise(); d3.event.sourceEvent.stopPropagation(); } function onDrag(d) { d.x += d3.event.dx; d.y += d3.event.dy; update(); } function onDragEnd() {} // Initialize the visualization update(); </script> </body> </html>This example creates an SVG container with draggable nodes and links between them. You can adapt it according to your needs.
Note: This example uses d3.js v5, and you may need to adjust the code if you are using a different version. In addition, the calculation of x2 and y2 for links is not provided in the example, and you need to implement it according to your requirements.
- User
it makes copy of dragging object every movement while dragging
- Assistant
I apologize for that mistake. I found the issue and fixed it. The problem was due to using separate containers for nodes and links while updating them. To resolve this, I updated the code to manage nodes and links together and update them properly during dragging. Here's the corrected version:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://d3js.org/d3.v5.js"></script> <title>Data Visualizer</title> <style> .node { cursor: move; fill: #ccc; stroke: #000; } .link { stroke: #000; stroke-width: 2px; } .node-text { font-family: "Arial"; font-size: 12px; pointer-events: none; } </style> </head> <body> <script> // Your data const data = [ { locationId: "someLocationId", id: "someId1", title: "название кнопки", content: "текст", choices: ["someId2", "someId3"], }, { locationId: "someLocationId", id: "someId2", title: "название кнопки", content: "text", choices: ["someId4", "someId5"], }, { locationId: "someLocationId", id: "someId3", title: "название кнопки", content: "text", choices: ["someId4", "someId6"], }, ]; // Constants const width = window.innerWidth; const height = window.innerHeight; const text_padding = 10; // Create the SVG container const svg = d3 .select("body") .append("svg") .attr("width", width) .attr("height", height); // Create the node and link containers const g = svg.append("g"); // Create the drag behavior const drag = d3 .drag() .on("start", onDragStart) .on("drag", onDrag) .on("end", onDragEnd); // Get nodeById function nodeById(id) { return data.find((node) => node.id === id); } // Update the visualization function update() { // Perform a JOIN of the data with the existing elements const gs = g.selectAll("g").data(data, (d) => d.id); // UPDATE: Remove EXITing elements gs.exit().remove(); // Append elements for the new ENTERing data const enterGs = gs.enter().append("g"); enterGs.append("rect").attr("class", "node").call(drag); enterGs.append("text").attr("class", "node-text"); // MERGE the ENTER and UPDATE elements const merged = enterGs.merge(gs); // Update the attributes of the merged elements based on the data merged .select(".node") .attr("x", (d) => d.x) .attr("y", (d) => d.y) .attr("width", (d) => d.title.length * 10 + 2 * text_padding) .attr("height", 30); merged .select(".node-text") .attr("x", (d) => d.x + text_padding) .attr("y", (d) => d.y + 20) .text((d) => d.title); // Update the links (lines) based on the data mergeLinks(); } function mergeLinks() { const links = data.filter((d) => d.choices.length > 0).map((d) => { return d.choices.map((choice) => ({ x1: d.x, y1: d.y, title: d.title, x2: nodeById(choice).x, y2: nodeById(choice).y, })); }); const flattenedLinks = links.flat(); // Perform a JOIN of the flattened links with the existing lines const lines = g.selectAll("line").data(flattenedLinks); // Remove all EXITing lines lines.exit().remove(); // Append lines for the new ENTERing data const enterLines = lines.enter().append("line").attr("class", "link"); // Merge ENTER and UPDATE elements const mergedLines = enterLines.merge(lines); // Update the attributes of the merged lines based on the data mergedLines .attr("x1", (d) => d.x1 + d.title.length * 5 + text_padding) .attr("y1", (d) => d.y1 + 15) .attr("x2", (d) => d.x2 + d.title.length * 5) .attr("y2", (d) => d.y2 + 15); } // Drag behavior callbacks function onDragStart(d) { d3.select(this.parentNode).raise(); d3.event.sourceEvent.stopPropagation(); } function onDrag(d) { d.x += d3.event.dx; d.y += d3.event.dy; update(); } function onDragEnd() {} // Set initial positions and render the visualization data.forEach((d, index) => { d.x = 100 * index; d.y = 100 * index; }); update(); </script> </body> </html>With this updated code, the nodes will no longer duplicate while dragging.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.