score:1
Accepted answer
Create arc data records for the angles you want and use the centroid()
for these dummy arcs
You have some illegal html after the g
element.
// Create an arc generator with configuration
var arcGenerator = d3.arc()
.innerRadius(80)
.outerRadius(100);
var arcData = [
{startAngle: 0, endAngle: Math.PI * 2 / 4, degree: 0},
{startAngle: Math.PI * 2 / 4, endAngle: Math.PI, degree: 90},
{startAngle: Math.PI, endAngle: Math.PI * 3 / 2, degree: 180},
{startAngle: Math.PI * 3 / 2, endAngle: Math.PI * 2, degree: 270},
];
var rectData = arcData.map(d => { return {startAngle: d.startAngle, endAngle:d.startAngle}; });
// Create a path element and set its d attribute
d3.select('g')
.selectAll('path')
.data(arcData)
.enter()
.append('path')
.attr('d', arcGenerator);
// Add labels, using .centroid() to position
d3.select('g')
.selectAll('.grect')
.data(rectData)
.enter()
.append('g')
.attr('class', 'grect')
.attr('transform', d => {
var centroid = arcGenerator.centroid(d);
return `translate(${centroid[0]},${centroid[1]})`;
})
.append('rect')
.attr('x', -5)
.attr('y', -17)
.attr('width', 10)
.attr('height', 35);
path {
fill: white;
stroke: black;
}
<meta charset="utf-8">
<head>
<title>Rotate rectangles to arc positions: 0, 90, 180 and 270 </title>
</head>
<body>
<svg width="700" height="220">
<g transform="translate(200, 110)"></g>
</svg>
<script src="https://d3js.org/d3.v5.min.js" charset="utf-8"></script>
</body>
Edit
You can create the rectData
on the fly.
d3.select('g')
.selectAll('.grect')
.data(arcData)
.enter()
.append('g')
.attr('class', 'grect')
.attr('transform', d => {
var centroid = arcGenerator.centroid({startAngle: d.startAngle, endAngle:d.startAngle});
return `translate(${centroid[0]},${centroid[1]})`;
})
.append('rect')
.attr('x', -5)
.attr('y', -17)
.attr('width', 10)
.attr('height', 35);
Source: stackoverflow.com
Related Query
- Rotating rectangles on an arc in D3js
- d3js : how to put circles at the end of an arc
- Aligning text inside circular arc d3js
- D3js arc animation not working when wrapping in object
- How to add svg rectangles as background behind axis tick labels in d3js if ticks are inside chart
- why is arc labels blurred in my d3js pie chart?
- Moving connected rectangles in d3js
- SVG D3JS animation: how to make an arc " self-drawing"?
- Connecting two rectangles in d3js
- How can I draw rectangles with D3js which are placed like wall bricks structure?
- Text rotation within an arc using D3JS
- AngularJS D3JS Donut chart colour change of arc on hover
- d3js arc height tweening
- d3js timeline adding current month renders multiple rectangles and doesn't clear
- d3js link arc filled useless part
- d3js - image not showing in arc
- d3js v4 - J hoop chart - long stem with a curved arc
- D3js take data from an array instead of a file
- Label outside arc (Pie chart) d3.js
- d3js Tree square
- Get one element from d3js selection, by index
- Create database E-R diagram with D3js
- Do AngularJS Directives for D3JS exist?
- D3js how to append 2 children at the same level in .enter context
- Extending d3js hierarchical tree for representing topology
- D3js within Java application
- d3js - d3.scale.category10() not working?
- d3js : How to select nth element of a group?
- Using d3js to make a candlestick or ohlc chart
- d3.js - how to automatically calculate arc lengths in radial dendrogram
More Query from same tag
- D3.js output store in database
- Time axis in 24 hour clock
- Drawing graph with JSNetworkX
- Make d3 bar chart responsive
- drawing a map of the Bronx - d3.geo.path()
- Wrapping long text labels in D3 without extra new lines
- D3: get horizontal position of ticks
- How do I dynamically update nested lists with d3?
- typescript: Attr() function of d3 objects not accepting object / function
- Duration in d3 animation
- ForceX using DOM element as coords reference
- d3js animated pie chart using json
- Why copy D3 source into an Angular Service?
- D3js outer limits
- Count text fields from csv dataset in d3 js
- Single Axis Zoom in D3 & React
- Loop over JSON data to create d3 pie charts
- Update indexes when deleting nodes
- Why are the labels on my D3 axis inconsistent (e.g. month, day)?
- Why are circle objects located in top left corner of an SVG container?
- d3 Hexagonal Binning domain and range
- How do I maintain the aspect ratio while resizing a d3 svg shape by dragging the corner?
- D3 time formatting locale
- How to merge data from D3.js Topojson to defined set to set class
- How to position an object in relation to another
- D3 Zoom and Ionic popover
- event.pageX is working in Chrome but not working in Firefox
- Can't draw rectangular in a barchart
- crossfilter.js with dc.js update charts on callback group change
- How to chain d3 transitions?