score:1
You need to make couple of changes in the d3 code to overcome overlapping.
1) Increase the width of SVG.
2) Translate pie chart to the center of SVG.
3) Also translate the legend group towards right.
Please find attached updated code.
<!DOCTYPE html>
<html>
<head>
<script src="http://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<div class="custom-chart"></div>
<script>
var details = [{ "name": "GRAND DING HOUSE Hong Kong HK", "label": "72" }, { "name": "MI-NE SUSHI HONG KONG HK", "label": "10" }, { "name": "TNS INVESTMENT LIMITED", "label": "6" }, { "name": "GRAND DING HOUSE Hong Kong HK", "label": "2" }, { "name": "MI-NE SUSHI HONG KONG HK", "label": "1" }, { "name": "Other", "label": "9" }];
chartScript(details, '.custom-chart');
function chartScript(details, clsName) {
var lblName = new Array(6); var i = 0;
for (var index in details) {
lblName[i] = details[index];
i++;
}
var rectWidth = 650;
var width = rectWidth, height = 350;
let colors = d3.scaleOrdinal().domain(lblName).range(['#A4A4A4', '#0073AA', '#009DD9', '#005D87', '#00A76D', '#6DC067']);
let svg = d3.select(clsName).append('svg').attr('width', width).attr('height', height);
let data = d3.pie().sort(null).value(function (d) {return d.label;})(details);
let segments = d3.arc().innerRadius(55).outerRadius(120).padAngle(0.05).padRadius(30);
let sections = svg.append('g').attr('transform', 'translate('+rectWidth/2+',145)').selectAll('path').data(data);
sections.enter().append('path').attr('d', segments).attr('fill', function (d, i) {return colors(i);});
let content = d3
.select(clsName)
.select('g')
.selectAll('text')
.data(data);
content
.enter()
.append('text')
.classed('inside', true)
.each(function (d) {
let center = segments.centroid(d);
d3.select(this)
.attr('x', center[0] - 10)
.attr('y', center[1])
.text(d.data.label + '%');
});
let legends = svg.append('g').attr('transform', 'translate(30, 270)').selectAll('.legends').data(data);
let legend = legends.enter().append('g').classed('legends', true).attr('transform', function (d, i) {
if (i < 3) {return 'translate(0,' + (i + 1) * 20 + ')';}
else { return 'translate('+rectWidth/2+',' + (i - 3 + 1) * 20 + ')'; }
});
legend.append('rect').attr('width', 10).attr('height', 10).attr('fill', function (d, i) {return colors(i);})
.attr('x', -20)
.attr('y', 0);
legend.append('text').classed('label', true).text(function (d) {return d.data.name;})
.attr('fill', function (d, i) {return colors(i); }).attr('x', 15).attr('y', 10);
}
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- d3 label are overlaping
- Label names are not displayed from input data in pie transition chart of d3.js
- Label outside arc (Pie chart) d3.js
- Add text label to d3 node in Force directed Graph and resize on hover
- What are unit testing strategies for D3JS?
- d3 click coordinates are relative to page not svg - how to translate them (Chrome error)
- How are svg, d3, dagre, dagre-d3 and graphlib dependent on each other?
- d3.js & nvd3.js axis and label precision formatting
- custom no data label in nvd3 scatter chart
- Why are d3's select() and selectAll() behaving differently here?
- How are input parameters filled in javascript method chains?
- Adding label on a D3 bar chart
- Transform label in reverse order d3 radial chart
- JS based influxdb graph editors like Grafana, Influga, are there reusable libraries?
- Draw imbricated blocks that are rotated and fliped with d3.js
- Show every other tick label on d3 time axis?
- D3.js force layout - edge label placement/rotation
- Are there any tutorials or examples for cubism.js + WebSocket?
- Add text label to d3 node in Force layout
- How to add Label to each state in d3.js (albersUsa)?
- Small Box to display where we are hovering in force layout (D3.js)
- Change label text color in row chart
- How to have a text label on links in d3 force directed graphs
- Why are .domain, tickFormat and tickValues not recognised inside dimensions variable? (d3, parallel coordinates)
- Hierarchial Edge Bundling: adding parent group label
- d3.js nvd3 date on x axis: only some dates are show
- NVD3 Line Chart X Axis Ticks Are Missing
- d3 - Change text label when data updates
- How to avoid the overlapping of text elements on the TreeMap when child elements are opened in D3.js?
- How to add label to edges in d3 graph
More Query from same tag
- d3 v4: Using stack with histogram data?
- D3.js column chart inversing when adding text
- How to allow Duplicate Tick Labels in Plotly?
- d3 - Round number from nest() and sum()
- Ionic D3.js piechart does not render
- C3.js: Assigning a chart height independently of the legend height
- (Flask, d3) Uncaught ReferenceError: d3 is not defined
- US Zipcode TOPOJson for D3
- Tooltip visibility based on condition d3.js
- Using JSON data with identical keys (in D3)
- How would you create a JSON file based on different cols of your database (PostgreSQL/SQLAlchemy) in Flask to use it in your JavaScript code?
- bullet chart in Angular with d3
- Just Change Tick labels
- Difference between angularjs-nvd3-directives vs angular-nvd3
- How to append an svg loaded by d3.xml into an svg
- d3js - my line chart is not working
- Limitting x and y axis ticks in bar graph with d3.js
- D3.js: What is the (current) standard way of using force layout?
- D3.3: Unable to represent time correctly into X-axis as Hour:Min:Sec format?
- Meteor, where to put d3 code?
- D3.js text in middle of svg circle not appearing
- Recreate Cluster Force Layout using d3 v4.0
- Function to create key-value pairs from two object properties
- arrow d3 graph svg not rendering
- How to draw a exponential function y=ab^x using d3.js axis functionality
- Appending complex SVG elements to a selection
- How to plot svg circles on curved line in d3 js?
- Dots persist on D3 graph for angularjs
- D3 v5: "d" attribute in <path> tag not populating with values
- d3.geo.path rectangle wrapping the wrong way