score:1
Accepted answer
I used two different algorithms. The first is calculating the position of each node on D3 cycle, the second is calculating the positions in advance via initData
function.
Here is what I have:
When it overflows, it jumps a line
var width = 500;
var height = 500;
var rectHeight = 20;
var posX = 0;
var posY = 0;
var rectSpace = 0.5;
var dataArray = [20, 78, 40, 60, 370, 42];
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
rects = d3.select("svg").selectAll("rect")
.data(dataArray)
.enter()
.append("rect")
.attr("width", function(d) {
return d;
})
.attr("height", rectHeight)
.attr("x", function(d, i) {
return getPosX(d, i);
})
.attr("y", function(d, i) {
return getPosY(d, i);
});
// Calculate PosX
function getPosX(rectWidth, index) {
// If rect goes out of the SVG, restart at posX = 0
if (posX + rectWidth > width) {
posX = 0;
}
newPosX = posX; // Return this position
posX += rectWidth + 1; // prepare position for next node
return newPosX;
}
// Calculate PosY
function getPosY(rectWidth, index) {
// reset posX at first calculation of posY
if (index == 0) {
posX = 0;
}
// If rect goes out of the SVG increment Y position of rect
if (posX + rectWidth > width) {
posY += rectHeight + 1;
posX = 0;
}
posX += rectWidth + 1; // Calculate position for next node
return posY;
}
svg {
border: 1px solid gray;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
When it overflows, the node is cut and the rest is added to a new line
var width = 500;
var height = 500;
var rectHeight = 20;
var rectSpace = 0.5;
var dataArray = [20, 78, 40, 60, 1370, 42];
var nodes = [];
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
rects = d3.select("svg").selectAll("rect")
.data(initData(dataArray))
.enter()
.append("rect")
.attr("width", function(d) {
return d.value;
})
.attr("height", rectHeight)
.attr("x", function(d, i) {
return d.x;
})
.attr("y", function(d, i) {
return d.y;
});
// Build displayed data below
function initData(data) {
var posX = 0;
var posY = 0;
// Claculate position of each node
for (var i in data) {
var node = addNode(data[i], posX, posY);
// If there is an overflow
if (node.x + node.value > width) {
var overflowValue = node.x + node.value - width;
// Add nodes until there is no more overflow
while (overflowValue > 0) {
// Update current node value
node.value = width - node.x;
// Calculate new node posX and posY
posX = 0;
posY += rectHeight + 1;
node = addNode(overflowValue, posX, posY);
// Claculate new overflow
overflowValue = node.x + node.value - width;
}
}
posX += node.value + 1;
}
return nodes;
}
function addNode(value, x, y) {
var newNode = {
value: value,
x: x,
y: y
};
nodes.push(newNode);
return newNode;
}
svg {
border: 1px solid gray;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- drawing a specific number of rects (d3.js)
- Drawing a Discrete number of rects in an svg element.using D3.js
- javascript: generate random number that are separated by a specific "margin"
- Count number of items in an array of objects with a specific property value (JavaScript)
- D3 Line Chart: Setting Y.domain values to centre on a specific number
- d3.js x axis specific number of string ticks
- Split children in n th rows specific number of children par each row
- Drawing line in d3js results in d3.v5.min.js:2 Error: <path> attribute d: Expected number
- removing magic number in drawing rows of svg rect
- Select specific number of items from a list
- D3.js linear scale [0,1] with a specific number of string tick values [A,B,C,...]
- Drawing Multiple Lines in D3.js
- Calculate width of text before drawing the text
- Dynamically resize the d3 tree layout based on number of childnodes
- Drawing multiple edges between two nodes with d3
- Drawing non-continuous lines with d3
- Changing number displayed as svg text gradually, with D3 transition
- drawing circle with radius specified in meters on a map
- Tree drawing orientation
- d3 drawing arrows tips
- Proper format for drawing polygon data in D3
- idiomatic way of drawing a triangle in d3
- Format a number with SI Prefix, with fixed number of decimals
- Drawing heatmap with d3
- d3 show number instead of percentages on pie chart
- D3.js Specific Tick Styling
- maximum number of svg elements for the browser in a map
- Compilation errors when drawing a piechart using d3.js and TypeScript
- How can I keep tick marks from repeating when I have a small number of dates on an nvd3 chart
- Putting an arrow (marker) at specific point on a path when using the d3 javascript library
More Query from same tag
- Date format issue in d3 js
- Specifying a number of nodes in each cluster of clustered force layout in d3js
- angular-datamaps not filling proper colour if "id" of a location has space
- how can i use correctly the functions which have been already developed in javascript into an application in angular 4?
- How to coordinate interactions between multiple data visualizations, particularly when one of them uses nesting? JavaScript and D3
- Binding D3 Tree Node Position to X Axis
- make x axis and ticks disappear in d3
- D3 Javascript -- Example SVG is declared in HTML cannot embed with HTML Tags
- d3.select("#element") not working when code above the html element
- How can you select and highlight all points on a line(s) using d3?
- Loading data with vanilla D3.js
- Issue attaching javascript events using variation on module pattern
- Disable a button using d3 / jquery until the action is complete
- Trying to make a stacked area chart
- d3 is undefined error
- Gaussian curve for a given stock price and IV with d3.js
- Round edges at the ends of radial progress bar
- d3.js - calculating width of a previously drawn element
- Changing zoom in D3.js from mousewheel to control+mousewheel
- D3 js line chart with tooltip on hover with multiple line chart
- How to make axis ticks disappear in c3js?
- D3.js Choropleth Map - Change Color Scheme On Selection
- Enter, update and exit selections in a treemap
- Adding URLs to the text of the pie chart
- d3.js collapsible force layout with all the nodes collapsed
- D3: "Invalid argument" error using d3.js in IE9
- D3: call() not working for appended text element (Type error)
- Force directed graphs nodes stick to the center
- I have a set of string data that I want plot in desired partitions with the string neatly near the plotted circles
- d3.js add a label in the center of a path