score:4
Accepted answer
There are two issues related to this result
1) The gradient definition is missing the .enter() method
This is necessary to create a linearGradient element for each data point. Instead of:
var gradient = svg.append("defs")
.data(data)
.append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "00%")
.attr("spreadMethod", "pad");
You could use:
var gradient = svg.append("defs")
.selectAll("linearGradient") // Creates the initial selection of linear gradients
.data(data)
.enter() // Binds new linearGradient elements for each data point
.append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "00%")
.attr("spreadMethod", "pad");
Now, instead of having only one linearGradient element, you have one for each color. However, you will notice the problem persists, which leads to the second issue:
2) If all the linear gradients have the same ID, the code can not differentiate between colors.
Different linearGradient elements need different IDs in order to reference the data they represent. Continuing the previous example, instead of:
var gradient = svg.append("defs")
.selectAll("linearGradient") // Creates the initial selection of linear gradients
.data(data)
.enter() // Binds new linearGradient elements for each data point
.append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "00%")
.attr("spreadMethod", "pad");
You could use:
var gradient = svg.append("defs")
.selectAll("linearGradient") // Creates the initial selection of linear gradients
.data(data)
.enter() // Binds new linearGradient elements for each data point
.append("linearGradient")
.attr("id", d => `gradient${d.name}`) // Create a unique data-driven id for each linearGradient
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "00%")
.attr("spreadMethod", "pad");
And in the bars, the code can now reference the correct linearGradient according to the data:
bars.append("rect")
...
.style("fill", d => `url(#gradient${d.name})`); // picks the gradient that match the data
Source: stackoverflow.com
Related Query
- D3 Gradient Fill on Bar Chart - with colour scale
- dc.js bar chart with ordinal x axis scale doesn't render correctly
- Align x-axis with zero scale d3 bar chart
- Fill bar with gradient colors depending on the value
- DC bar chart Y axis with integer scale (not multiplicated)
- Using a scale in d3.js to fill up a vertical bar with 3 colors
- d3.js - Bar chart won't display with scale
- D3 fill for a horizontal stacked bar chart in Angular with typeScript: .attr("fill", ..) gets error "No overload matches this call" in VSC
- Bar chart with negative values
- Using an ordinal scale ('d3.scale.ordinal') for the x-axis in a bar chart
- Fill SVG element with a repeating linear gradient color
- How to update d3.js bar chart with new data
- Make simple bar chart using C3 with separate columns on the x axis
- d3 bar chart with range selector in x-axis (like dygraphs)
- d3.js bar chart sorting: can't figure out how to sort x-axis labels along with bars
- Adding Error Bars to Grouped Bar Chart with D3.js
- How to use x and width in a bar chart with scaleTime?
- C3 / D3 bar chart with horizontal scroll
- DC.js bar chart with date axis
- d3.js stacked bar chart with positive and negative values
- Need help lining up x axis ticks with bars in D3.js bar chart
- How to Make a bar chart with rounded corners with extended grid in d3.js?
- Sorting the bars in a bar chart with dc.js
- How to fill area with our choice colour in plotly area grap
- NVD3.js: Stacked and grouped bar chart with two y-axis
- D3 bar chart background with dots
- d3 show labels only for ticks with data in a bar chart
- D3 - How to loop through an object with keys for a bar chart
- d3.js Stacked bar chart with Logarithmic Scaling
- Grouped bar chart with zoom and updateable data error
More Query from same tag
- d3 radar chart -- radialLine creates path but without coordinates
- d3, transitioning a scatterplot into a bar graph
- d3.timer() frame - slowing down
- Using CSS Grid, scroll a div from anywhere (w/out using jQuery plugins)
- object HTMLImageElement cannot be "saved as" image in Internet Explorer
- How to display a text when mouseover a cilcle
- d3.json does not load file from App_Data folder of ASP.NET MVC
- d3js: d3.csv and it's data disappears
- d3: Smoothly animate a hand drawn line?
- D3 v4 Force Simulation 'grouped'
- How to remove NVD3 chart resize/update delay
- Error: <circle> attribute cx: Expected length, "NaN" and Error: <circle> attribute cy: Expected length, "NaN"
- unresponsive bars in otherwise responsive grouped d3 bar chart
- Ho do I capture ctrl + click on MacOS with d3js
- A better way to get all y values on hover in a multiline chart d3js
- D3.js - Is the "svg:" in "svg:rect" a must?
- Nvd3: How prevent to display chart between -1 and 1 if have all y values 0?
- D3 - not all children nodes are shown at the same time
- d3.js Update Bar Chart Tooltip with Buttons
- Creating DC charts dynamically
- change range for date in d3 graph
- How can I dispatch a 'zoom' event after setting scale (d3, zoom.behavior)
- d3js unit testing removal of elements
- style text based on the context of the text in d3
- Three.js ugly rendering effect
- How to draw nodes in D3 Javascript library based on JSON file
- d3.js How to pass parameter to php for query condition
- connect javascript with html (a href)
- binding of array elements in d3.js
- Merging multiple paths into one D3(SVG)