Make D3 graphs responsive












0















I have a couple of graphs made with D3 but I have problems when I want to display them on small devices. They are cut off on left and right sides and I cannot see them properly.



I have no idea how to fix it, I have tried to change the dimensions of the canvas through the CSS when the screen size is small but I could not. I hope you can help me. I guess I have to introduce some change in the CSS and in the bit of code where I set the size of the graph.



var margin = {top: 100, right: 111, bottom: 50, left: 120},
width = 960 - margin.left - margin.right,
height = 560 - margin.top - margin.bottom;


var svgbar = d3.select("#barforest").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")


Thanks










share|improve this question























  • Possible duplicate of responsive D3 chart

    – ksav
    Nov 13 '18 at 8:18
















0















I have a couple of graphs made with D3 but I have problems when I want to display them on small devices. They are cut off on left and right sides and I cannot see them properly.



I have no idea how to fix it, I have tried to change the dimensions of the canvas through the CSS when the screen size is small but I could not. I hope you can help me. I guess I have to introduce some change in the CSS and in the bit of code where I set the size of the graph.



var margin = {top: 100, right: 111, bottom: 50, left: 120},
width = 960 - margin.left - margin.right,
height = 560 - margin.top - margin.bottom;


var svgbar = d3.select("#barforest").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")


Thanks










share|improve this question























  • Possible duplicate of responsive D3 chart

    – ksav
    Nov 13 '18 at 8:18














0












0








0








I have a couple of graphs made with D3 but I have problems when I want to display them on small devices. They are cut off on left and right sides and I cannot see them properly.



I have no idea how to fix it, I have tried to change the dimensions of the canvas through the CSS when the screen size is small but I could not. I hope you can help me. I guess I have to introduce some change in the CSS and in the bit of code where I set the size of the graph.



var margin = {top: 100, right: 111, bottom: 50, left: 120},
width = 960 - margin.left - margin.right,
height = 560 - margin.top - margin.bottom;


var svgbar = d3.select("#barforest").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")


Thanks










share|improve this question














I have a couple of graphs made with D3 but I have problems when I want to display them on small devices. They are cut off on left and right sides and I cannot see them properly.



I have no idea how to fix it, I have tried to change the dimensions of the canvas through the CSS when the screen size is small but I could not. I hope you can help me. I guess I have to introduce some change in the CSS and in the bit of code where I set the size of the graph.



var margin = {top: 100, right: 111, bottom: 50, left: 120},
width = 960 - margin.left - margin.right,
height = 560 - margin.top - margin.bottom;


var svgbar = d3.select("#barforest").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")


Thanks







javascript css d3.js responsive-design responsive






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:55









Michael RoMichael Ro

42




42













  • Possible duplicate of responsive D3 chart

    – ksav
    Nov 13 '18 at 8:18



















  • Possible duplicate of responsive D3 chart

    – ksav
    Nov 13 '18 at 8:18

















Possible duplicate of responsive D3 chart

– ksav
Nov 13 '18 at 8:18





Possible duplicate of responsive D3 chart

– ksav
Nov 13 '18 at 8:18












1 Answer
1






active

oldest

votes


















0














In your code, you have set the width, height and margins fixed. If you want your graph responsive, you have to register a resize event and redraw with appropriate width and height.






share|improve this answer
























  • How can I do that?

    – Michael Ro
    Nov 14 '18 at 0:48











  • Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

    – Hari Prathap
    Nov 14 '18 at 9:38











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%2f53276270%2fmake-d3-graphs-responsive%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














In your code, you have set the width, height and margins fixed. If you want your graph responsive, you have to register a resize event and redraw with appropriate width and height.






share|improve this answer
























  • How can I do that?

    – Michael Ro
    Nov 14 '18 at 0:48











  • Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

    – Hari Prathap
    Nov 14 '18 at 9:38
















0














In your code, you have set the width, height and margins fixed. If you want your graph responsive, you have to register a resize event and redraw with appropriate width and height.






share|improve this answer
























  • How can I do that?

    – Michael Ro
    Nov 14 '18 at 0:48











  • Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

    – Hari Prathap
    Nov 14 '18 at 9:38














0












0








0







In your code, you have set the width, height and margins fixed. If you want your graph responsive, you have to register a resize event and redraw with appropriate width and height.






share|improve this answer













In your code, you have set the width, height and margins fixed. If you want your graph responsive, you have to register a resize event and redraw with appropriate width and height.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 8:09









Hari PrathapHari Prathap

705




705













  • How can I do that?

    – Michael Ro
    Nov 14 '18 at 0:48











  • Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

    – Hari Prathap
    Nov 14 '18 at 9:38



















  • How can I do that?

    – Michael Ro
    Nov 14 '18 at 0:48











  • Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

    – Hari Prathap
    Nov 14 '18 at 9:38

















How can I do that?

– Michael Ro
Nov 14 '18 at 0:48





How can I do that?

– Michael Ro
Nov 14 '18 at 0:48













Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

– Hari Prathap
Nov 14 '18 at 9:38





Refer this solution bl.ocks.org/enactdev/a647e60b209e67602304

– Hari Prathap
Nov 14 '18 at 9:38


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276270%2fmake-d3-graphs-responsive%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







Popular posts from this blog

Coverage of Google Street View

Full-time equivalent

Surfing