score:1
Accepted answer
All you need to do is create a new color scale
var colors = d3.scale.category20()
Associate each layer point with a fruit
var keys = ["redDelicious", "mcintosh", "oranges", "pears", "cherries", "plum","grapes", "melons"]
var dataset = d3.layout.stack()(keys.map(function(fruit) {
return data.map(function(d) {
return {x: parse(d.year), y: +d[fruit], name: fruit};
});
}));
And then start passing in fruit names during the data join.
var rect = groups.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
...
.style("fill", d=> colors(d.name))
The first time you pass in "apple"
like var randomColor = colors('apple')
, apple will be assigned a color. The next time you pass in apple, you will retrieve the same color.
For the legend, do adata join to create the rectangles, using the previously discussed color scale.
var legend = svg.selectAll(".legend")
.data(keys)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", d=>colors(d));
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(d=>d);
Source: stackoverflow.com
Related Query
- automate color pick for the stacked bar plots
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- How do I know which bar the mouse is in for D3 stacked bar?
- Changing the colors of each of the Stacked Bar chart with different Color in D3
- d3.js Stacked Bar Chart working for one dataset but not the other
- Changing color for each stacked bar in D3.js
- D3 to CSS color for stacked bar graph
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- Using an ordinal scale ('d3.scale.ordinal') for the x-axis in a bar chart
- Looking for a better way to alter the color of axis components in d3.js v4
- D3.js: Changing the color of the bar depending on the value
- Sum of all the stacks of stacked bar chart in d3
- Stacked Bar Chart using d3.js stack layout; the bar sections not always in correct place
- d3v4 checkout chart - making sure the color scheme is correct for the legend and circles
- Identifying the function used in this d3 stacked bar chart example
- Flip the x-axis of a D3 Stacked Bar Chart
- How do I define a value accessor for a stacked bar graph?
- D3 transition from stacked bar to bar chart only works the first time
- Transitioning a bar chart with negative values for the width
- I am facing problems while making a dragable stacked bar chart in d3 v4. The major issue I am facing is in the translation of svg when dragging
- d3.js - How to do data mapping and filter for Stacked Bar graph
- Transition for grouped bar chart in d3 when updating the data values
- Maintain color mapping for Stacked/Grouped Multi-Bar Chart using NVD3 when the series count fluxuates
- Prepare nested JSON data for D3 stacked bar chart
- Elastic X axis for stacked barchart, removing the empty bins [dc.js]
- Is this JSON structure suitable for a stacked bar chart using d3.js?
- Change the color of the legend text in forceNetwork for networkD3
- Can't set the number of ticks on time-based x-axis to custom amount for bar chart
- how to give different color for the lines in the Sankey plot to show different groups?
More Query from same tag
- How to make labels appear only when the chart is zoomed in
- Looping transition and showing countries in a given order
- getting error for d3-tip in angular2
- AngularJS, D3 and JQuery in the same page. D3 can't read the correct DOM dimensions
- In D3 Sankey, how can I load the from objects or arrays instead of either json or csv files?
- Log the x,y coordinates of nodes in a converged D3 force layout
- Update the context line upon loading new data d3
- d3.js and multidimensional array
- Duration in d3 animation
- How to inform Angular directive that the scope values have changed?
- Insert js in a Hugo post
- Draw D3 Simple Line chart With an Array
- Append svg icon always right behind the text
- Issue with summing and data manipulation in d3
- Pivoting data in D3.js
- Multiple pie charts update with only one input d3.js
- Only one SVG component rendered when calling a method twice
- Updating the table multiple times using D3 does not work
- display K and M in Angular nvd3 chart Yaxis
- hide X and Y axes on charts with n3-line-chart
- AngularJS directive not being called
- populating the JSON data from spring mvc to d3 chart using request body
- Clustering timeline data in d3
- for loop within d3.json
- Plugging in d3-force-3d calculations into a 3-D graph renderer
- Adding new data point entries to a line chart
- Running GDAL from the command line on Windows 10 x64
- Creating a Text Labeled x-Axis with an Ordinal Scale in D3.js
- How to access original event object when d3 event binding
- SVG links not working after transition executed in d3.js