How can one maintain the same color for a child in a sunburst sequence as its parent?

Seeking advice on how to create a lighter color for a child element based on the parent's color. The code I currently use generates random colors as shown below:

  var color = d3.scale.category20b();

 .style("fill", function(d) { return color((d.children ? d : d.parent).name); })

This results in a random color for each node, like the first image linked here: first image : random colour

However, what I am aiming for is a gradient effect where the child's color is lighter than the parent's color, as seen in this example image: Gradient colour

If anyone has suggestions on achieving this effect, it would be greatly appreciated. I have included links to my result images due to restrictions on posting images directly. Thank you.

Answer №1

This project presented an intriguing challenge. The majority of the effort went into setting up the color schemes and various webpage templates. I encountered some unexpected issues while attempting to utilize d3.interpolateString(), so I decided to postpone that for further investigation. Here is how I prepared for this task:

var brewerColors = d3.entries(colorbrewer);
// colors 1-5 seemed too similar
// colors 6-13 provided the best contrast
// colors 4 and above were not suitable
var brewerOffset = 9;
var pageTypes = ["home","product","search","account","other","end"];
var colors = [];
var pages = [];
for (var ct=0; ct<pageTypes.length; ct++) {
    var colorBucket = brewerColors[ct + brewerOffset].value[pageTypes.length];
    for (var ct2=0; ct2<colorBucket.length; ct2++) {
        pages.push(pageTypes[ct] + (ct2 + 1));
        colors.push(colorBucket[ct2]);
    }
}

var ramps = d3.scale.ordinal()
    // ensure center colors are darker than edge colors
    .domain(pages.reverse())
    .range(colors);

Following that, assigning the appropriate colors to the shapes was a straightforward process:

var path = vis.data([json]).selectAll("path")
    .data(nodes)
    .enter().append("svg:path")
    ...
    .style("fill", function(d) {
        return ramps(d.name + d.depth); // e.g. product1, home2, etc.
    })
    ...

For a live demonstration, you can access the complete working code on PLUNK.

NOTE: I recommend making a copy of the plunk to prevent accidental deletion in the future.

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

New elements can be inserted at the rear of the queue while older elements are removed from the front of the queue

I'm new to JavaScript and currently working on a task involving queues. Here is the task description: Create a function called nextInLine that takes an array (arr) and a number (item) as parameters. The function should add the number to the end of ...

Exploring the process of implementing a filter search feature using Vue.js and JavaScript

Is it possible to create a filter search code using just data and method functions? I attempted to do the following: var fil = new Vue ({ el: '#app', data: { search: '', proL: [&a ...

I am encountering an issue stating "indexRouter has not been defined"

Encountering an error "indexRouter is not defined" while attempting to run the code below. Despite attempts to remove the line, additional errors persist. Can anyone clarify the rationale behind using the common variable router for both index.js and user.j ...

"Performing validation on a number input by using the ng-change event

Im using a number input that dynamically sets the min and max values based on another form field. I have two scenarios: Level 1: Min = 2, Max = 50 Level 2: Min = 5, Max = 1000 I've set up an ng-change event on the input field to check if the entere ...

Hovering over a td tag and adding a border using CSS can cause the entire table to

While experimenting on a coding platform: <table> <tr> <td class="changeme">something</td> <td>something</td> <td>something</td> <td>something</td> < ...

What is the best way to access a variable in one file and then utilize it in multiple other files?

My question may seem straightforward, but I'm having trouble finding the right solution. Within my index.html file, I have the following content: <html> <head> <script src="scalar.js"></script> </head> <body> < ...

A guide to exporting a PDF in A4 size landscape mode with jspdf

As a novice in UI development, I am currently trying to export HTML content to PDF using the JSPDF library. However, I have encountered difficulties in generating the PDF in A4 size landscape mode. The HTML code includes data with charts (canvasjs/chartjs) ...

Creating a Dynamic Navigation Bar with HTML and CSS

I've been exploring the code on w3schools.com, and while there are some great examples, I'm struggling to understand it all. Unfortunately, there aren't any instructions on customizing the code for a third level of menus. Can someone simplif ...

Having trouble running the development environment with npm or pnpm due to permission problems

I encounter this issue when running the command without sudo, but everything works fine with elevated permissions. What could be causing this discrepancy? I've searched for similar questions on various platforms and encountered the same problem with b ...

Backdrop styling for Material-UI dialogs/modals

Is there a way to customize the semi-transparent overlay of a material-ui dialog or modal? I am currently using material-ui with React and Typescript. https://i.stack.imgur.com/ODQvN.png Instead of a dark transparent overlay, I would like it to be transp ...

Encountering an Unexpected Identifier Error in the JavaScript Console

const blogPosts = [{ title: "Dogs are just okay", author: "Jake", comments: ["Great Post", "You're awesome!"] }, { title: "Dogs are truly amazing", author: "Dog Lover" comments: ["<3", "Get lost, fool"] } ] I am aware ...

How can I resolve the issue of "require is not defined" in the code?

tag, you can find the following code snippet: Upon importing the 'mysql' module, an issue persists in the browser matching the error indicated by the title. Starting my journey into programming, I aim to develop a membership registration functio ...

What is the best way to use useCallback within loops?

I am currently exploring the concepts of memo and useCallback, and to better grasp them, I have created a simple example: import { memo, useCallback, useState } from "react"; import { Button, Text, TouchableOpacity, SafeAreaView } from "reac ...

ReactJS Form: Updates in Parent Component State Result in Child Field Being Cleared and Props Remaining Unchanged

I have devised a ReactJS code for an interactive form that allows the collection of student details. The beauty of this form is that it can accommodate any number of students with ease. In this setup, the parent component is known as App and the child comp ...

Do I need to use the "--save" flag in npm to add dependencies to the "package.json" file?

Do I really need to use the "--save" flag in order to add an installed dependency to the "package.json" file? I conducted a test without the "save" flag and found that the package was still added to the "dependencies" section. It seems like it is the defa ...

React's Material-UI AppBar is failing to register click events

I'm experimenting with React, incorporating the AppBar and Drawer components from v0 Material-UI as functional components. I've defined the handleDrawerClick function within the class and passed it as a prop to be used as a click event in the fun ...

Issues with dependencies in npx create react app

After initiating a new react project with npx create-react-app client, I have encountered an issue where the react-scripts command is not found, preventing me from running the start script. Is there a way to resolve this problem? Furthermore, when attempt ...

Creating a canvas that adjusts proportionally to different screen sizes

Currently, I am developing a pong game using Angular for the frontend, and the game is displayed inside an HTML canvas. Check out the HTML code below: <div style="height: 70%; width: 70%;" align="center"> <canvas id=&q ...

How can you position the nav collapse toggle to the far right while still allowing it to collapse downwards on smaller devices with Bootstrap 5?

I am in need of a navigation section that is always visible on all devices, with a collapsible toggle aligned to the right. In Bootstrap 5.2, if I place the collapse toggle after the <div class="collapse navbar-collapse ..., the collapsible conten ...

How to Handle Duplicate Elements in #each Svelte Block

I've encountered an issue with the related posts component on my Svelte website. While it functions correctly, there is a problem with duplicate articles showing up in the block. For example, if two articles contain three identical tags each, those ar ...