score:0
Accepted answer
Ok so I figured it out after multiple manual tests. Not sure it is the most efficient formula but at least it works! Hope it helps someone in the same case!
//Draw initial svg
const svg = d3.select('body').append('svg').attr('width', 1000).attr('height', 1000)
// Data for the initial group with one rectangle inside
const groupData = {width: 200, height: 100, translateX: 200, translateY: 150, rotation: 45}
//Create the initial group
let group1 = svg.append('g').attr('id', 'group1')
//Append a rect to the initial group, translate and rotate
group1.append('rect').attr('width', groupData.width).attr('height', groupData.height).attr('fill', 'blue')
group1.attr('transform', `translate(${groupData.translateX}, ${groupData.translateY}) rotate(${groupData.rotation}, ${groupData.width / 2}, ${groupData.height / 2})`)
const rows = 3
const columns = 2
// Create row and column array to dynamically split the initial group based on the rows and columns numbers
const rowArray = [...Array(+rows).keys()]
const columnArray = [...Array(+columns).keys()]
rowArray.forEach(row => {
columnArray.forEach( column => {
let newGroup = svg.append('g').attr('id', `group${column}${row}`)
newGroup.append('rect')
.attr('width', groupData.width / columns)
.attr('height', groupData.height / rows)
.attr('fill', 'orange')
const cx = groupData.translateX + (groupData.width / columns / 2) * (columns - 1);
const cy = groupData.translateY + (groupData.height / rows / 2) * (rows - 1);
const tmpX = groupData.translateX + (groupData.width / columns) * column - cx;
const tmpY = groupData.translateY + (groupData.height / rows) * row - cy;
const pointX = tmpX * Math.cos((groupData.rotation * Math.PI) / 180) - tmpY * Math.sin((groupData.rotation * Math.PI) / 180) + cx;
const pointY = tmpX * Math.sin((groupData.rotation * Math.PI) / 180) + tmpY * Math.cos((groupData.rotation * Math.PI) / 180) + cy;
newGroup.attr('transform', `translate(${groupData.rotation != 0 ? pointX : groupData.translateX + (groupData.width / columns) * column}, ${groupData.rotation != 0 ? pointY : groupData.translateY + (groupData.height / rows) * row}) rotate(${groupData.rotation}, ${groupData.width / columns / 2}, ${groupData.height / rows / 2})`)
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- d3.js split group with rotate and translate applied and calculate new translate coordinates
- Combining translate and rotate with D3
- Rotate every arc of pie chart 180 (like sun) with D3 JS. How to calculate translate parameters
- Calculate height and with of a group in D3
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- How to replace a d3js nest function with group and rollup
- New layout after opening a group not base on the last layout with cola.js
- What is the proper way to both rotate and translate text in d3?
- How to summarize an array with group and rollup from d3-array?
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- d3 line and area charts not updating with new data array
- What Transformation values to calculate for scale and translate if you want to zoom
- Reconstructing D3 Animated Choropleth with a new Map and Data
- how to find all leaves of group with javascript and json
- how create a circle group a organization circle image and connect with curve line with D3.js?
- D3 x and y coordinates applied to last appened element instead of the group
- SVG Translate Group positioning on window resize with d3
- trouble with D3js world map and svg group element width
- On button click open a new window and draw multiple large circles with D3
- Split a string in object property and make new dataset
- On button click open a new window and draw a circle with D3
- d3 v4 Issue with tree.nodeSize . Root is set to 0,0, even after translate being applied
- D3, having trouble adding new group element and line
- Mongo Query: flatten field and split into a new document
- dc.js chart redrawing with new group function with click event
- On button click open a new window and draw a large circle with D3
- How do I create and write to a new file (csv) and have the browser download it with javascript or D3.js?
- rotate and zoom svg with d3 javascript
- D3 Sankey Diagram: Adding New Node and Link with Transition
- D3.js adding a new object with nest and key
More Query from same tag
- Code adapted from D3 v3 to v5 needs an extra update to render correctly
- Why does separating a d3 method chain on enter alter outcome?
- D3 Transitions between classes
- Typescript/d3, can't dynamically provide symbol types
- Problems converting from shape to topojson
- How to append multiple rectangles in D3?
- Leaflet with D3 polylines
- Invert y axis on d3 on console command
- Exapandable Stacked bar charts using d3.js
- Making a D3 bar chart sorted by JSON string?
- d3 pie chart not loading
- D3 reading promise data into a constructor
- My D3.js code is not showing seconds in the time axis?
- Rickshaw Graphs rotate ticks by -90 degrees
- D3 parcoords axis scale not updating correctly
- Three.js Points Rotation
- Stop zooming of bubbles in D3 World Map
- d3 autospace overlapping tick labels
- D3 force directed graph with images on canvas
- D3 js Drag and Drop Zoomable Tree help in collapsing nodes connected by a certain link
- D3 DataMaps (or jvectormap): How to put a custom object on map by coords
- Making bar chart adjust to data passed in
- Append HTML to existing tooltip in d3
- D3 tickSizeOuter(0) to remove end-ticks NOT working
- How can I use D3 (or just javascript) to set a form option as being "selected"
- Fill a grid with empty rectangles in D3.js?
- D3 selection no longer updates after supplying new data
- NVD3 Stacked Bar Chart Plus Line Overlapped
- D3- multiple onclick events
- Fisheye effect on timeline