D3js V4.13 set min max height/width










0














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
    Nov 12 '18 at 17:53










  • a .size function after d3.layout.force, where you set height width
    – Alann
    Nov 12 '18 at 21:15










  • 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
    Nov 13 '18 at 0:14










  • 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
    Nov 13 '18 at 8:19















0














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
    Nov 12 '18 at 17:53










  • a .size function after d3.layout.force, where you set height width
    – Alann
    Nov 12 '18 at 21:15










  • 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
    Nov 13 '18 at 0:14










  • 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
    Nov 13 '18 at 8:19













0












0








0







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 Nov 12 '18 at 16:17









Alann

15011




15011











  • what have you found in D3v3?
    – rioV8
    Nov 12 '18 at 17:53










  • a .size function after d3.layout.force, where you set height width
    – Alann
    Nov 12 '18 at 21:15










  • 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
    Nov 13 '18 at 0:14










  • 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
    Nov 13 '18 at 8:19
















  • what have you found in D3v3?
    – rioV8
    Nov 12 '18 at 17:53










  • a .size function after d3.layout.force, where you set height width
    – Alann
    Nov 12 '18 at 21:15










  • 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
    Nov 13 '18 at 0:14










  • 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
    Nov 13 '18 at 8:19















what have you found in D3v3?
– rioV8
Nov 12 '18 at 17:53




what have you found in D3v3?
– rioV8
Nov 12 '18 at 17:53












a .size function after d3.layout.force, where you set height width
– Alann
Nov 12 '18 at 21:15




a .size function after d3.layout.force, where you set height width
– Alann
Nov 12 '18 at 21:15












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
Nov 13 '18 at 0:14




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
Nov 13 '18 at 0:14












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
Nov 13 '18 at 8:19




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
Nov 13 '18 at 8:19

















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',
autoActivateHeartbeat: false,
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















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




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















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3