D3js V4.13 set min max height/width











up vote
0
down vote

favorite












I'm working on a little app to display some data inside circle in d3js



My problem is, sometimes my circles moves outside of the SVG height-width and we can't see the data anymore.



I've been looking for a function which set min-max height-widht, I found one in the version 3 of D3js but not on version 4



I've tried to use forceX and forceY but it center all of my data, I want them to be able to go everywhere in the svg without the gravity impression that forceX and forceY gives



here is a part of my code :



    const simulation = forceSimulation()
.force('link', forceLink().id((d) => d['id']).distance(1).strength(1))
.force('charge', forceManyBody())
.force('center', forceCenter(width / 2, height / 2))
.force('collide', forceCollide(d => calculCollide(d)))
json('assets/example.json', (err, data) => {
const link = svg.append('g')
.attr('class', 'links')
.selectAll('line')
.data(data['links'])
.enter()
.append('line')
.attr('stroke-width', (d) => Math.sqrt(d['value']));

const node = svg.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(data['nodes'])
.enter()
.append('g')
.attr('class','gcircle')
.append('circle')
.attr('r', (d) => radiusGroup(d))
.attr('fill', (d) => color(d['group']))
.call(drag()
.on('start', dragStarted)
.on('drag', dragged)
.on('end', dragEnded)
);
node.append('title')
.text((d) => d['id']);

let text = svg.selectAll('.gcircle')
.append('text')
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.text(d => d['id']);

simulation
.nodes(data['nodes'])
.on('tick', ticked);

simulation.force<ForceLink<any, any>>('link')
.links(data['links']);

function ticked() {
link
.attr('x1', function(d) { return d['source'].x; })
.attr('y1', function(d) { return d['source'].y; })
.attr('x2', function(d) { return d['target'].x; })
.attr('y2', function(d) { return d['target'].y; });

node
.attr('cx', function(d) { return d['x']; })
.attr('cy', function(d) { return d['y']; });
text
.attr('dx', d => { return d['x'];})
.attr('dy', d => { return d['y'];});
}

});


function dragStarted(d) {
if (!event.active) { simulation.alphaTarget(0.9).restart(); }
d.fx = d.x;
d.fy = d.y;
}

function dragged(d) {
d.fx = event.x;
d.fy = event.y;
}
function radiusGroup(d) {

if (d.group === 1) {
return 15;
} else {
return 10;
}
}

function dragEnded(d) {
if (!event.active) { simulation.alphaTarget(0); }
d.fx = null;
d.fy = null;
}

function calculCollide(d){

if(d.group === 1) {
return 25;
} else {
return 10;
}
}


PS: I'm using d3js with angular, so I use the @types/d3



here is a stackblitz : https://stackblitz.com/edit/angular-tpbq2t , but with more data circle can go outside of the screen ... and that's my problem ...










share|improve this question






















  • what have you found in D3v3?
    – rioV8
    2 days ago










  • a .size function after d3.layout.force, where you set height width
    – Alann
    2 days ago










  • from the docs The size affects two aspects of the force-directed layout: the gravitational center, and the initial random position. It does not constrain the position during the simulation.
    – rioV8
    2 days ago










  • oh I see, so it's like forceX and forceY in the v4 of d3js, but do you know if there a way to solve my problem ?
    – Alann
    2 days ago















up vote
0
down vote

favorite












I'm working on a little app to display some data inside circle in d3js



My problem is, sometimes my circles moves outside of the SVG height-width and we can't see the data anymore.



I've been looking for a function which set min-max height-widht, I found one in the version 3 of D3js but not on version 4



I've tried to use forceX and forceY but it center all of my data, I want them to be able to go everywhere in the svg without the gravity impression that forceX and forceY gives



here is a part of my code :



    const simulation = forceSimulation()
.force('link', forceLink().id((d) => d['id']).distance(1).strength(1))
.force('charge', forceManyBody())
.force('center', forceCenter(width / 2, height / 2))
.force('collide', forceCollide(d => calculCollide(d)))
json('assets/example.json', (err, data) => {
const link = svg.append('g')
.attr('class', 'links')
.selectAll('line')
.data(data['links'])
.enter()
.append('line')
.attr('stroke-width', (d) => Math.sqrt(d['value']));

const node = svg.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(data['nodes'])
.enter()
.append('g')
.attr('class','gcircle')
.append('circle')
.attr('r', (d) => radiusGroup(d))
.attr('fill', (d) => color(d['group']))
.call(drag()
.on('start', dragStarted)
.on('drag', dragged)
.on('end', dragEnded)
);
node.append('title')
.text((d) => d['id']);

let text = svg.selectAll('.gcircle')
.append('text')
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.text(d => d['id']);

simulation
.nodes(data['nodes'])
.on('tick', ticked);

simulation.force<ForceLink<any, any>>('link')
.links(data['links']);

function ticked() {
link
.attr('x1', function(d) { return d['source'].x; })
.attr('y1', function(d) { return d['source'].y; })
.attr('x2', function(d) { return d['target'].x; })
.attr('y2', function(d) { return d['target'].y; });

node
.attr('cx', function(d) { return d['x']; })
.attr('cy', function(d) { return d['y']; });
text
.attr('dx', d => { return d['x'];})
.attr('dy', d => { return d['y'];});
}

});


function dragStarted(d) {
if (!event.active) { simulation.alphaTarget(0.9).restart(); }
d.fx = d.x;
d.fy = d.y;
}

function dragged(d) {
d.fx = event.x;
d.fy = event.y;
}
function radiusGroup(d) {

if (d.group === 1) {
return 15;
} else {
return 10;
}
}

function dragEnded(d) {
if (!event.active) { simulation.alphaTarget(0); }
d.fx = null;
d.fy = null;
}

function calculCollide(d){

if(d.group === 1) {
return 25;
} else {
return 10;
}
}


PS: I'm using d3js with angular, so I use the @types/d3



here is a stackblitz : https://stackblitz.com/edit/angular-tpbq2t , but with more data circle can go outside of the screen ... and that's my problem ...










share|improve this question






















  • what have you found in D3v3?
    – rioV8
    2 days ago










  • a .size function after d3.layout.force, where you set height width
    – Alann
    2 days ago










  • from the docs The size affects two aspects of the force-directed layout: the gravitational center, and the initial random position. It does not constrain the position during the simulation.
    – rioV8
    2 days ago










  • oh I see, so it's like forceX and forceY in the v4 of d3js, but do you know if there a way to solve my problem ?
    – Alann
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm working on a little app to display some data inside circle in d3js



My problem is, sometimes my circles moves outside of the SVG height-width and we can't see the data anymore.



I've been looking for a function which set min-max height-widht, I found one in the version 3 of D3js but not on version 4



I've tried to use forceX and forceY but it center all of my data, I want them to be able to go everywhere in the svg without the gravity impression that forceX and forceY gives



here is a part of my code :



    const simulation = forceSimulation()
.force('link', forceLink().id((d) => d['id']).distance(1).strength(1))
.force('charge', forceManyBody())
.force('center', forceCenter(width / 2, height / 2))
.force('collide', forceCollide(d => calculCollide(d)))
json('assets/example.json', (err, data) => {
const link = svg.append('g')
.attr('class', 'links')
.selectAll('line')
.data(data['links'])
.enter()
.append('line')
.attr('stroke-width', (d) => Math.sqrt(d['value']));

const node = svg.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(data['nodes'])
.enter()
.append('g')
.attr('class','gcircle')
.append('circle')
.attr('r', (d) => radiusGroup(d))
.attr('fill', (d) => color(d['group']))
.call(drag()
.on('start', dragStarted)
.on('drag', dragged)
.on('end', dragEnded)
);
node.append('title')
.text((d) => d['id']);

let text = svg.selectAll('.gcircle')
.append('text')
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.text(d => d['id']);

simulation
.nodes(data['nodes'])
.on('tick', ticked);

simulation.force<ForceLink<any, any>>('link')
.links(data['links']);

function ticked() {
link
.attr('x1', function(d) { return d['source'].x; })
.attr('y1', function(d) { return d['source'].y; })
.attr('x2', function(d) { return d['target'].x; })
.attr('y2', function(d) { return d['target'].y; });

node
.attr('cx', function(d) { return d['x']; })
.attr('cy', function(d) { return d['y']; });
text
.attr('dx', d => { return d['x'];})
.attr('dy', d => { return d['y'];});
}

});


function dragStarted(d) {
if (!event.active) { simulation.alphaTarget(0.9).restart(); }
d.fx = d.x;
d.fy = d.y;
}

function dragged(d) {
d.fx = event.x;
d.fy = event.y;
}
function radiusGroup(d) {

if (d.group === 1) {
return 15;
} else {
return 10;
}
}

function dragEnded(d) {
if (!event.active) { simulation.alphaTarget(0); }
d.fx = null;
d.fy = null;
}

function calculCollide(d){

if(d.group === 1) {
return 25;
} else {
return 10;
}
}


PS: I'm using d3js with angular, so I use the @types/d3



here is a stackblitz : https://stackblitz.com/edit/angular-tpbq2t , but with more data circle can go outside of the screen ... and that's my problem ...










share|improve this question













I'm working on a little app to display some data inside circle in d3js



My problem is, sometimes my circles moves outside of the SVG height-width and we can't see the data anymore.



I've been looking for a function which set min-max height-widht, I found one in the version 3 of D3js but not on version 4



I've tried to use forceX and forceY but it center all of my data, I want them to be able to go everywhere in the svg without the gravity impression that forceX and forceY gives



here is a part of my code :



    const simulation = forceSimulation()
.force('link', forceLink().id((d) => d['id']).distance(1).strength(1))
.force('charge', forceManyBody())
.force('center', forceCenter(width / 2, height / 2))
.force('collide', forceCollide(d => calculCollide(d)))
json('assets/example.json', (err, data) => {
const link = svg.append('g')
.attr('class', 'links')
.selectAll('line')
.data(data['links'])
.enter()
.append('line')
.attr('stroke-width', (d) => Math.sqrt(d['value']));

const node = svg.append('g')
.attr('class', 'nodes')
.selectAll('circle')
.data(data['nodes'])
.enter()
.append('g')
.attr('class','gcircle')
.append('circle')
.attr('r', (d) => radiusGroup(d))
.attr('fill', (d) => color(d['group']))
.call(drag()
.on('start', dragStarted)
.on('drag', dragged)
.on('end', dragEnded)
);
node.append('title')
.text((d) => d['id']);

let text = svg.selectAll('.gcircle')
.append('text')
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.text(d => d['id']);

simulation
.nodes(data['nodes'])
.on('tick', ticked);

simulation.force<ForceLink<any, any>>('link')
.links(data['links']);

function ticked() {
link
.attr('x1', function(d) { return d['source'].x; })
.attr('y1', function(d) { return d['source'].y; })
.attr('x2', function(d) { return d['target'].x; })
.attr('y2', function(d) { return d['target'].y; });

node
.attr('cx', function(d) { return d['x']; })
.attr('cy', function(d) { return d['y']; });
text
.attr('dx', d => { return d['x'];})
.attr('dy', d => { return d['y'];});
}

});


function dragStarted(d) {
if (!event.active) { simulation.alphaTarget(0.9).restart(); }
d.fx = d.x;
d.fy = d.y;
}

function dragged(d) {
d.fx = event.x;
d.fy = event.y;
}
function radiusGroup(d) {

if (d.group === 1) {
return 15;
} else {
return 10;
}
}

function dragEnded(d) {
if (!event.active) { simulation.alphaTarget(0); }
d.fx = null;
d.fy = null;
}

function calculCollide(d){

if(d.group === 1) {
return 25;
} else {
return 10;
}
}


PS: I'm using d3js with angular, so I use the @types/d3



here is a stackblitz : https://stackblitz.com/edit/angular-tpbq2t , but with more data circle can go outside of the screen ... and that's my problem ...







javascript css angular d3.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Alann

5310




5310












  • what have you found in D3v3?
    – rioV8
    2 days ago










  • a .size function after d3.layout.force, where you set height width
    – Alann
    2 days ago










  • from the docs The size affects two aspects of the force-directed layout: the gravitational center, and the initial random position. It does not constrain the position during the simulation.
    – rioV8
    2 days ago










  • oh I see, so it's like forceX and forceY in the v4 of d3js, but do you know if there a way to solve my problem ?
    – Alann
    2 days ago


















  • what have you found in D3v3?
    – rioV8
    2 days ago










  • a .size function after d3.layout.force, where you set height width
    – Alann
    2 days ago










  • from the docs The size affects two aspects of the force-directed layout: the gravitational center, and the initial random position. It does not constrain the position during the simulation.
    – rioV8
    2 days ago










  • oh I see, so it's like forceX and forceY in the v4 of d3js, but do you know if there a way to solve my problem ?
    – Alann
    2 days ago
















what have you found in D3v3?
– rioV8
2 days ago




what have you found in D3v3?
– rioV8
2 days ago












a .size function after d3.layout.force, where you set height width
– Alann
2 days ago




a .size function after d3.layout.force, where you set height width
– Alann
2 days ago












from the docs The size affects two aspects of the force-directed layout: the gravitational center, and the initial random position. It does not constrain the position during the simulation.
– rioV8
2 days ago




from the docs The size affects two aspects of the force-directed layout: the gravitational center, and the initial random position. It does not constrain the position during the simulation.
– rioV8
2 days ago












oh I see, so it's like forceX and forceY in the v4 of d3js, but do you know if there a way to solve my problem ?
– Alann
2 days ago




oh I see, so it's like forceX and forceY in the v4 of d3js, but do you know if there a way to solve my problem ?
– Alann
2 days ago

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266135%2fd3js-v4-13-set-min-max-height-width%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266135%2fd3js-v4-13-set-min-max-height-width%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?