Guidelines for creating numerous self-edges within a node-link diagram using D3

Creating a single self-link on a node within a node-link diagram can be accomplished by following the instructions outlined in this resource: D3 Force Layout Self-Linking Node

If you need to draw multiple links on the same node, what modifications would you make?

I attempted to introduce a 'rotation' aspect based on the number of self-links present. I made adjustments to the code provided in the linked example as follows:

function tick() {
  link.attr("d", function(d) {
  var x1 = d.source.x,
  y1 = d.source.y,
  x2 = d.target.x,
  y2 = d.target.y,
  dx = x2 - x1,
  dy = y2 - y1,
  dr = Math.sqrt(dx * dx + dy * dy),

  // Defaults for regular edge.
  drx = dr,
  dry = dr,
  xRotation = 0, 
  largeArc = 0, 
  sweep = 1; 

  if ( x1 === x2 && y1 === y2 ) {
    var index = getIndexOfDuplicateEdge();
    var degree = 360 / numberOfDuplicateEdges();
    var degreeForIndex = degree * index;

    xRotation = degreeForIndex; 

    largeArc = 1;

    drx = 30;
    dry = 20;

    x2 = x2 + 1;
    y2 = y2 + 1;
  } 

 return "M" + x1 + "," + y1 + "A" + drx + "," + dry + " " + xRotation + "," + largeArc + "," + sweep + " " + x2 + "," + y2;
 }); 

Despite my efforts, the ellipses are not being rendered as anticipated and I'm struggling to find a solution. According to Mozilla's SVG documentation, the large-arc parameter must be 1 while the sweep parameter can toggle between 0 and 1 to create a mirrored effect. I've experimented with different values for xRotation, but I can't achieve the desired positioning for the ellipses.

The quantity of self-links may vary, and I aim to achieve an optimal distribution of ellipses across the nodes.

Ultimately, the goal is to have it resemble the visualization shown here: https://i.sstatic.net/teUxE.png

Answer №1

To create a unique flower design, divide the circle into segments based on the number of petals desired. Calculate the start and end points for each petal on the circle and fit an ellipse using those points.

Try out this custom code snippet to achieve the desired effect: (ensure you have an svg element with the id "svgthing")

function radtodeg(angle) {
    return angle * (180/Math.PI);
}

function generateFlower( center_x, center_y, num_petals, start_angle, end_angle, radius, length ) {

  var angle_sector = end_angle - start_angle;

  var num_points = num_petals * 2;

  var angle_per_point = angle_sector / num_points;
  var angle_per_sector = angle_per_point * 2;

  var str_builder = [];

  for( var angle = start_angle; angle < end_angle; angle += angle_per_sector ) {
    var start_sector_angle = angle;
    var end_sector_angle = angle + angle_per_point;

    var mid_sector_angle = angle + angle_per_point / 2;

    var start_x = center_x + (radius * Math.cos(start_sector_angle));
    var start_y = center_y + (radius * Math.sin(start_sector_angle));
    var end_x = center_x + (radius * Math.cos(end_sector_angle));
    var end_y = center_y + (radius * Math.sin(end_sector_angle));

    var mid_x = center_x + (radius * Math.cos(mid_sector_angle));
    var mid_y = center_y + (radius * Math.sin(mid_sector_angle));

      str_builder.push("<path d='");
    str_builder.push("M" + start_x + " " + start_y + ",");
    str_builder.push("A " + length + " 1 " + radtodeg(mid_sector_angle) + " 0 1 " + end_x + " " + end_y);
    str_builder.push("'/>\n");

    str_builder.push("<circle cx='" + start_x + "' cy='" + start_y + "' r='5' />\n");
    str_builder.push("<circle cx='" + end_x + "' cy='" + end_y + "' r='5'/>\n");
    str_builder.push("<circle cx='" + mid_x + "' cy='" + mid_y + "' r='5'/>\n");
  }

  str_builder.push("<circle cx='" + center_x + "' cy='" + center_y + "' r='" + radius + "' />\n");

  $("#svgthing").html(str_builder.join(""));
}

generateFlower(60, 50, 8, 0, 2 * Math.PI, 50, 10);

By calling the function above, you can easily create a beautiful flower with 8 petals.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions for downloading a .zip file sent through a HTTP response (using an axios PUT request)

When the API responds, it should contain a data property with the .zip file I need. However, the format is unfamiliar to me. The format: https://i.sstatic.net/ruaVR.png I attempted to use .blob() as suggested in similar questions on Stackoverflow, but it ...

Why does the fillText() method in HTML5 Canvas erase everything after using the clearRect() method?

Whenever I use the writeTextToCanvas method before the clearCanvas method, everything works perfectly. However, if I call the clearCanvas method first and then writeTextToCanvas, the drawing functions work fine after clearing the canvas but the fillText fu ...

What sets apart route.use(), route.all(), and route.route() in Express?

Is it possible to replace router.all() with router.use() if the former just matches all methods? Also, what are the differences between using router.use() and router.route()? ...

The debate between ensuring input validity and making fields mandatory on multi-page forms

I am currently working on a multi-page form and using jQuery Validate to validate it. The user has four options: next, prev, save, submit. Save, next, and prev all save the current page within the form; whereas submit is similar to save, but triggers addi ...

Ways to change columns into rows with CSS

Hey there! I'm looking for a way to convert columns into rows and vice versa. In my table, I have both column headers and row headers on the left side. The row headers are simply bold text placed next to each row to describe its content. I want to op ...

Retrieving data from a JSON file depending on the selection made by the user in a dropdown menu

In my project, I have a JSON file named countries.json that contains country and regional information. Users are required to select a country using a Python-generated dropdown list from this file. However, due to the backend nature of Python, I am unable t ...

What are the best practices for storing an array of objects in MongoDB with Mongoose?

For my project, I needed to store data in a mongoDB document as an array of objects using Javascript, Node JS, Express framework, and Mongoose library. After researching online, I found two different methods to achieve this: Creating another sub-schema ...

The functionality to save user likes in React is not properly functioning on the like button

I created a like button in React that saves my choices, but it seems to be not saving the choices of other users. Additionally, it doesn't appear to restrict only authenticated users from marking likes. Can someone please help me identify what I' ...

Discover the art of highlighting errors with React-hook-form and MUI React

My code consists of the following component: const App = () => { const formProps = useForm({ mode: "onBlur", }); const { handleSubmit, formState, register, watch, reset } = formProps; return ( <FormProvider {...formProps}> & ...

Utilizing a search bar with the option to narrow down results by category

I want to develop a search page where users can enter a keyword and get a list of results, along with the option to filter by category if necessary. I have managed to make both the input field and radio buttons work separately, but not together. So, when s ...

Neither the context nor props contain the element 'store' that you are searching for

Just stepping into the world of React can be overwhelming, but I'm determined to get my page to render properly: import React, { Component } from "react"; import { connect } from "react-redux"; import Header from '../components/Header'; imp ...

Retrieving a single object in NEXT.JS and MongoDB can be achieved by returning just a single object

Is there a way to retrieve a single object instead of an array from the API? I am specifically looking for just a single "Event" while using MongoDB and Next.js. Currently, I always receive: [{}] But I would like to receive: {} const fetchWithId = (url ...

What causes tests to fail with an error message SyntaxError: Unexpected token {?

Hey there! I'm encountering an issue with webpack 5 and babel while trying to run tests that were previously written. Despite following the jest documentation for configuration, I haven't been able to find a solution after exploring various forum ...

Encountering difficulty selecting a dropdown sub-menu using Selenium WebDriver

I'm currently working on automating a website with selenium webdriver. The issue I'm encountering is that when I try to click on a menu item, the submenu pops up (actually a misplaced dropdown, UI issue), and although I can locate the element of ...

Managing Data Types in AJAX Requests

Can you help me figure out why my AJAX call is not reaching success after hours of troubleshooting? It seems like the issue lies in the dataType that the AJAX call is expecting or receiving (JavaScript vs JSON). Unfortunately, I'm not sure how to addr ...

Is there a way to prevent an iframe from being recorded in Chrome's browsing history?

I'm in the process of developing a Chrome extension that inserts an Angular application into the user's browser window to create an interactive sidebar. I've been successful in most of my goals by inserting an iframe through a content script ...

"Enhance your web application with dynamic drop-down selection using Spring, Ajax

In my JSP file, I have a script that populates the list of states based on the selected country. The script fetches data from the controller and is supposed to display the list of states. However, after calling the controller method, the alert "received da ...

I am interested in extracting specific data from the JSON response

I am trying to extract the value of the message parameter under the messages array where the parameter name is equal to documentId (highlighted in bold below). However, the code I have tried so far does not achieve this as needed. dynamic obj = JsonConver ...

Exploring the Concept of Callbacks in JavaScript

What happens behind the scenes when a function is passed as a parameter in JavaScript and how does the engine recognize it as a callback? ...

Stop fullscreen mode in Angular after initiating in fullscreen mode

Issue with exiting full screen on Angular when starting Chrome in full-screen mode. HTML: <hello name="{{ name }}"></hello> <a href="https://angular-go-full-screen-f11-key.stackblitz.io" target="_blank" style=& ...