score:1
Accepted answer
As I commented. <text>
should be inside <svg>
not inside <circle>
.. then position it accordingly..
I have transformed text as you have transformed circle ... making inside circle. given color red. You can now edit your code as you wish.
Modified js code.
const json = { companyA: 500, companyB: 200, companyC: 150, companyD: 100, companyE: 50 };
const colors = [ '#FF0000', '#03900F', '#0000FF', '#FF00FF', '#00FF00' ];
const diameter = 400;
const processData = data => {
const obj = data;
const newDataSet = [];
for(let prop in obj) {
newDataSet.push({name: prop, size: obj[prop]});
}
return {children: newDataSet};
}
const svg = d3.select('#graph').append('svg')
.attr('width', diameter)
.attr('height', diameter);
const bubble = d3.layout.pack()
.size([diameter, diameter])
.value( d => d.size )
.padding(30);
// generate data with calculated layout values
const nodes = bubble.nodes(processData(json)).filter(function(d) { return !d.children; }); // filter out the outer bubble
const vis = svg.selectAll('circle').data(nodes);
vis.enter().append('circle')
.attr('transform', d => 'translate(' + d.x + ',' + d.y + ')')
.attr('r', d => d.r)
.attr('fill', (d, i) => `${colors[i]}`);
vis.enter().append('text')
.attr('dx', d => 20)
.attr('transform', d => 'translate(' + d.x + ',' + d.y + ')')
.attr('fill',"red")
.attr('text-anchor', 'middle')
.attr('font-size', d => d.r / ((d.r * 10) / 100))
.attr('dy', d => d.r / ((d.r * 25) / 100))
.text(d => d.name)
Source: stackoverflow.com
Related Query
- How can I add text to SVG elements in D3 using the example below
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- D3.js - How can I add a new line to the text in this Collapsible Tree?
- How can i get the startAngle and endAngle of each arc in the sunburst example using d3.js?
- How can I add images in the nodes of a D3 Sankey diagram, using the rcv and networkD3 packages in R?
- How can the elements in SVG can't be dragged out of the scope of SVG?
- How can add text to the center of a D3 sunburst diagram
- How can I animate the top of my svg element like in this gif using d3.js?
- How can I recreate svg <line> elements using d34raphael?
- How to simultaneously update the color of elements in a SVG when I mouseover elements of a second SVG using D3
- how can we add text to a svg circle in d3js
- How can I get the bounding box values of all the text elements in a selection?
- How to add text on top the heatmap to properly show value for each rectangle when using D3.js?
- How can I toggle the value of an svg text element with D3.js when user clicks the text?
- How can I add an image as the fill for a circle dynamically in javascript using d3
- How to add an image to an svg container using D3.js
- How to add border/outline/stroke to SVG elements in CSS?
- How do I get the width of an svg element using d3js?
- How can I dynamically resize an SVG rect based on text width?
- How to update the fill color on existing svg elements with d3.js?
- D3, SVG and textPath: How to move the text down?
- How can I toggle the class of all elements in a selection?
- How do I associate SVG elements generated by graphviz to elements in the DOT source code
- How to avoid the overlapping of text elements on the TreeMap when child elements are opened in D3.js?
- How can I set the each line color or width in SVG path
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- How can I center text on a D3.js SVG grid rect?
- How can I display a placeholder image in my SVG until the real image is loaded?
- How to update elements of an HTML that the elements are created using data from a CSV file?
- how to add legend to a pie chart using D3js? And how to centralise the pie chart?
More Query from same tag
- Use a global variable inside promise javascript
- Printing out nested dictionary in specific format and outputting to json file
- Why does a <h2> Header clip the succeeding SVG element?
- Last element added to a D3 bar chart doesn't sort
- D3 pick nodes needed for graphics
- How do I use D3js in Wordpress?
- Center a map in d3 given a geoJSON object
- Accessing nested JSON structure in D3
- How to add Custom value in timeFormat d3?
- D3.js: How to highlight nodes in Dendogram with particular color?
- d3 js basic line chart, data issues
- Can one specify a custom force function for a force-directed layout?
- Why text append outside svg element d3js?
- How to use "on click" before the creation of my element
- d3js: Elements lingering even after .exit().remove()
- Property 'features' does not exist on type 'Feature<Point, { [name: string]: any; }>'
- Pie chart data not get updated over REST call
- d3.js - multiple charts on same page from different parts of same JSON object
- Using d3.json to query a sinatra app works fine on localhost, but fails with forbidden when deployed
- How to move an svg-object to new coordinates
- Collapsible Tree Hierarchy d3
- Inserting new line in .html() using D3
- D3.js stacked bar chart broken transition
- JSON to SVG plan drawing with D3js
- How add text in D3 mouseover?
- Pass D3.js data based on ID specified in data
- D3.js nodes not appearing on page load
- geochart in d3.js
- Mouseover event on two charts at the same time d3.js
- C3 - Timeseries chart with JSON and categories