Keeping aspect ratio of D3 circle pack - responsive design

Damo

So I'm working on a bubble graph in D3.js: http://bl.ocks.org/mbostock/4063269. The width and height are determined by the diameter. So I ask: how to make this SVG bubble cloud, and any other SVG responsive with as little re coding as possible?

<script>
var diameter = 628,
    format = d3.format(",d"),
    color = d3.scale.ordinal()
        .range(["#FFFFFF", "#7ec0ee", "#0D2A46"]);

var bubble = d3.layout.pack()
    .sort(null)
    .size([diameter, diameter])
    .padding(1.5);

function addChart(file, target){

var svg = d3.select("body").append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
    .attr("class", "bubble");

d3.json(file, function(error, root) {
  var node = svg.selectAll(".node")
      .data(bubble.nodes(classes(root))
      .filter(function(d) { return !d.children; }))
    .enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

  node.append("title")
      .text(function(d) { return d.className + ": " + format(d.value); });

  node.append("circle")
      .attr("r", function(d) { return d.r; })
      .style("fill", function(d) { return color(d.packageName); });

  node.append("text")
      .attr("dy", ".3em")
      .style("text-anchor", "middle")

      .text(function(d) { return d.className.substring(0, d.r / 3); });
});
}
addChart("DataGreatEnglishWriters.json", "dv.foo");
addChart("Dubliner.json", "div.bar");
addChart("2LeftFeet.json", "div.head");
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
  var classes = [];

  function recurse(name, node) {
    if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
    else classes.push({packageName: name, className: node.name, value: node.size});
  }

  recurse(null, root);
  return {children: classes};
}

d3.select(self.frameElement).style("height", diameter + "px");

</script>
FernOfTheAndes

Here is a FIDDLE with Mike's example you mentioned.

var chart = $(".bubble"),
    aspect = chart.width() / chart.height(),
    container = chart.parent();

$(window).on("resize", function() {
    var targetWidth = container.width();
    chart.attr("width", targetWidth);
    chart.attr("height", Math.round(targetWidth / aspect));
}).trigger("resize");

Essentially, you use the viewBox and preserveAspectRatio attributes on the svg and re-size accordingly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Keeping aspect ratio of D3 circle pack - responsive design

From Dev

Responsive video iframes (keeping aspect ratio) with only CSS?

From Dev

Responsive rectangle with image fit to maximum size while keeping aspect ratio

From Dev

Keeping aspect ratio with CSS (if possible)

From Dev

ImageView array not keeping aspect ratio

From Dev

Bootstrap Responsive Image Aspect Ratio

From Dev

Android resize bitmap keeping aspect ratio

From Dev

Resize image with mogrify without keeping aspect ratio

From Dev

Keeping aspect ratio of image in full screen

From Dev

Resize CGSize to the maximum with keeping the aspect-ratio

From Dev

Keeping aspect ratio of image in full screen

From Dev

Keeping aspect ratio and autolayout for different devices

From Dev

Keeping aspect ratio of divs with a background image

From Dev

D3 add zoom to circle pack

From Dev

Flex Box and responsive aspect ratio issue in Firefox

From Dev

d3.js title above responsive chart with fixed aspect ratio

From Dev

Different colors for each circle - D3 circle pack

From Dev

Keeping aspect ratio for images with percentage width & resizing other divs while keeping aspect ration

From Dev

How to convert SVG to PNG while keeping aspect ratio but changing extents?

From Java

Auto Resize Image in CSS FlexBox Layout and keeping Aspect Ratio?

From Dev

Resize and crop image and keeping aspect ratio NodeJS & gm

From Dev

How to increase size of saved figure while keeping aspect ratio in matplotlib?

From Dev

CSS scale several images to fit viewport keeping aspect ratio

From Dev

CSS: Make Canvas as big as possible while keeping aspect ratio

From Dev

How to fit image to frame keeping aspect ratio and center in thumbnail list

From Dev

UIImageView with auto-layout and max size keeping aspect ratio

From Dev

Keeping the aspect ratio of a sub-classed QWidget during resize

From Dev

Print with Margins While Keeping Aspect Ratio of Image C#

From Dev

How to make images fill table cells while keeping aspect ratio?

Related Related

  1. 1

    Keeping aspect ratio of D3 circle pack - responsive design

  2. 2

    Responsive video iframes (keeping aspect ratio) with only CSS?

  3. 3

    Responsive rectangle with image fit to maximum size while keeping aspect ratio

  4. 4

    Keeping aspect ratio with CSS (if possible)

  5. 5

    ImageView array not keeping aspect ratio

  6. 6

    Bootstrap Responsive Image Aspect Ratio

  7. 7

    Android resize bitmap keeping aspect ratio

  8. 8

    Resize image with mogrify without keeping aspect ratio

  9. 9

    Keeping aspect ratio of image in full screen

  10. 10

    Resize CGSize to the maximum with keeping the aspect-ratio

  11. 11

    Keeping aspect ratio of image in full screen

  12. 12

    Keeping aspect ratio and autolayout for different devices

  13. 13

    Keeping aspect ratio of divs with a background image

  14. 14

    D3 add zoom to circle pack

  15. 15

    Flex Box and responsive aspect ratio issue in Firefox

  16. 16

    d3.js title above responsive chart with fixed aspect ratio

  17. 17

    Different colors for each circle - D3 circle pack

  18. 18

    Keeping aspect ratio for images with percentage width & resizing other divs while keeping aspect ration

  19. 19

    How to convert SVG to PNG while keeping aspect ratio but changing extents?

  20. 20

    Auto Resize Image in CSS FlexBox Layout and keeping Aspect Ratio?

  21. 21

    Resize and crop image and keeping aspect ratio NodeJS & gm

  22. 22

    How to increase size of saved figure while keeping aspect ratio in matplotlib?

  23. 23

    CSS scale several images to fit viewport keeping aspect ratio

  24. 24

    CSS: Make Canvas as big as possible while keeping aspect ratio

  25. 25

    How to fit image to frame keeping aspect ratio and center in thumbnail list

  26. 26

    UIImageView with auto-layout and max size keeping aspect ratio

  27. 27

    Keeping the aspect ratio of a sub-classed QWidget during resize

  28. 28

    Print with Margins While Keeping Aspect Ratio of Image C#

  29. 29

    How to make images fill table cells while keeping aspect ratio?

HotTag

Archive