score:1
Accepted answer
Since d3.scale.category10()
operates in a first-come first-served basis, that is, it adds values to the domain as new data comes in, you just need to pass the name
in the same order that you painted the circles.
However, for this to work, you'll have to change your function color()
:
function color(d) {
return d.name;
}
Now, both the circles and the legends will have the same domain in the color scale.
Now, create the legends:
var legendGroup = svg.append("g")
.attr("transform", "translate(600,50)");
var legend = legendGroup.selectAll(".legend")
.data(SPX.map(d => d.name))
.enter()
.append("g")
.attr("transform", (d, i) => "translate(0," + 20 * i + ")")
var legendRects = legend.append("rect")
.attr("width", 10)
.attr("height", 10)
.attr("fill", d => colorScale(d));
var legendText = legend.append("text")
.attr("x", 14)
.attr("y", 8)
.text(d => d);
Here is your fiddle, I put the legends in the top-right corner: http://jsfiddle.net/8yd04e0p/
Source: stackoverflow.com
Related Query
- How to add a legend to the motion chart
- how to add legend to a pie chart using D3js? And how to centralise the pie chart?
- How to add a nice legend to a d3 pie chart
- How to add a legend and labels to the pie chart?
- How to position the legend in a d3 chart
- d3.js: How to add value below the label in donut chart
- How to add events to the individual sections(arcs) of a pie chart created out of d3.js?
- How to word wrap the legend text for a donut chart
- How to Increase the width of C3 pie chart Legend only. Not for the whole graph
- Add some gap in between legend and the chart
- How to add a legend to the calendar heatmap
- How to add hover (tooltip) to the D3. bubble motion chart?
- How do you add multiple lines to a D3JS line chart where the depedent variable data sources start at different positions along the range?
- How to position the legend to the right of the donut chart
- How to add legend to the chart?
- How to add a circle in the middle of donut chart and fill it in d3 js?
- How to add legend to a pie chart D3.js?
- How do I extend the margins of my chart so that my legend doesn't get cut off?
- How to align legend dynamically on top-center of the chart in c3.js?
- Using D3, I am trying to add and remove lines from a multi line chart when the legend is clicked
- How to add tooltip with eclipse in legend in line chart in D3.js
- D3.js - How to add the general update pattern to a stacked bar chart
- How to add superscript to the legend name (graph name) in c3.js graph library
- How to position the legend right to donut chart in d3.js?
- How to add a tooltip at the end of the line chart in D3
- How to add text-based legend to D3.js chart
- How to add Legend bar to Row Chart Dc.js
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How to add more metrics on the country_map in Apache-superset?
- How to add drop shadow to d3.js pie or donut chart
More Query from same tag
- How to access the parentNode of a selection to raise an element?
- how do you draw linear line in scatter plot with d3.js
- radar chart with boxplot/ Violin plot
- d3 drawing arrows tips
- how to plot a pie chart in d3 by giving an local geojson file as an input
- D3 Pie Chart: Arctween in mouseOver doesn't work
- D3 time axis like plottable.js
- How can D3 read csv from url?
- If statement on independent variable does not update d3.js svg components
- Reading DOT files in javascript/d3
- d3.js tree layout is bunching everything up at the top
- D3-Updating line chart dynamically
- How to change the names of provinces on the map?
- Move the "Stacked/Grouped" selector on NVD3.js multiBarChart.js
- d3js multigraph auto update
- How to embed bitmap into D3js generated SVG code?
- Bar Chart With View Finder d3.js
- Increasing size of d3 scatterplot
- filtering data and working with scales - d3js
- Last tick not being displayed in d3 charts
- Time in x-axis and data for the y-axis for line chart in d3.js doesn't match/show with what (data) is on JSON
- d3 force layout with node groups
- d3 how to properly pass request body in a post request?
- D3 transition works unexpectedly
- Transform label in reverse order d3 radial chart
- d3.js - reset position variable per sentences
- Adding gauge chart to nvd3
- d3 update old scale when pan and zoom
- D3.js: Starting animations when a section is in view
- Creating a d3.js Treemap zoomable with CSV