How to get an non-overlapped circle packing d3 js chart?

Simron Thapa
<script type = "text/javascript"> $(document).ready(function () {
    var colorList = [
        'rgb(196, 37, 37)', 'rgb(47, 126, 216)', 'rgb(145, 0, 0)',
        'rgb(242, 143, 67)', 'rgb(26, 173, 206)', 'rgb(73, 41, 112)',
        'rgb(119, 161, 229)', 'rgb(166, 201, 106)', 'rgb(237, 86, 27)',
        'rgb(146, 168, 205)', 'rgb(181, 202, 146)', 'rgb(144, 137, 232)',
        'rgb(164, 125, 124)', 'rgb(96, 188, 179)', 'rgb(86, 97, 54)',
        'rgb(210, 189, 123)', 'rgb(120, 67, 181)', 'rgb(184,0,46)'];

    var json = $ {
        chartMap
    }

    var diameter = 500,
        format = d3.format(",f");

    var pack = d3.layout.pack()
        .size([diameter - 4, diameter - 4])
        .value(function (d) {
            return d.size;
        });

    var svg = d3.select("#healthplanChart").append("svg")
        .attr("width", diameter)
        .attr("height", diameter)
        .append("g")
        .attr("transform", "translate(2,2)");
    var root = json;
    var node = svg.datum(root).selectAll(".node")
        .data(pack.nodes)
        .enter().append("g")
        .attr("transform", function (d) {
            return "translate(" + d.x + "," + d.y + ")";
        });

    node.append("title")
        .text(function (d) {
            return d.name + (d.children ? "" : ": " + format(d.size));
        });

    node.append("circle")
        .attr("fill", function (d) {
            var ind = 0;
            for (var i = 0; i < root.children.length; i++) {
                if (root.children[i] == d) {
                    ind = i;
                }
            };
            return d.children ? "rgb(31, 119, 180)" : colorList[ind];
        })
        .attr("fill-opacity", function (d) {
            return d.children ? "rgb(31, 119, 180)" : colorList[1];
        })
        .attr("r", function (d) {
            return d.r;
        });

    node.filter(function (d) {
            return !d.children;
        })
        .append("text")
        .attr("dy", ".3em")
        .style("text-anchor", "middle")
        .text(function (d) {
            return d.name.substring(0, d.r / 3);
        });

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

JSON:

   {
      "name": "HXYZ",
      "children": [
        {
          "name": "A",
          "size": 828074
        },
        {
          "name": "B",
          "size": 325658.79
        },
        {
          "name": "C",
          "size": 0
        },
        {
          "name": "D",
          "size": 6544.86
        },
        {
          "name": "E",
          "size": 2025.99
        },
        {
          "name": "F",
          "size": 0
        }
      ]
    }
Simron Thapa
var pack = d3.layout.pack() .sort(function(a, b) { return -(a.value - b.value); }) .size([diameter - 4, diameter - 4]) .value(function(d) { return d.size; });

I did research and this code helped me solve my problem. I am sharing if anyone is facing similar issue. Thanks! :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get an non-overlapped circle packing d3 js chart?

From Dev

Zoomable Circle Packing with Automatic Text Sizing in D3.js

From Dev

d3.js circle packing along a line

From Dev

How to remove the outer circle in D3 bubble chart

From Dev

D3 Circle-Packing Clear Labeling Solution

From Dev

d3: svg image in zoom circle packing

From Java

Why the value for r is negative from d3.hierarchy in circle packing in d3.js?

From Dev

d3 line/circle chart

From Dev

How to make a chart in D3 work in node.js

From Dev

how to make chart column with drill down with D3 Js

From Dev

D3 Zoomable Circle Packing in IE11 is expanding beyond the SVG boundary

From Dev

Realtime D3 bubble chart (circle pack)

From Dev

Flot: Stacked line chart bug - points get overlapped

From Dev

Progressbar.js - how to get a second circle?

From Dev

circle packing in excel vba

From Dev

How do I get both a line and area styled in my d3 line chart?

From Dev

how to show bar chart based on multiple filter values in D3 js

From Dev

How to update a D3 pie chart

From Dev

How to implement D3 donut chart?

From Dev

Image within circle object causes pixelation d3 js

From Dev

NVD3 Stacked Bar Chart Plus Line Overlapped

From Dev

How to get a fill below c3.js timeseries chart

From Dev

How to deal with the hover issue of overlapped non-square DIV?

From Dev

How to deal with the hover issue of overlapped non-square DIV?

From Dev

add circle in a spiral chart with d3js with line connecting to center

From Dev

d3js - line chart `circle` placement to the lines are not properly sets

From Dev

d3js - line chart `circle` placement to the lines are not properly sets

From Dev

How to center a List inside a D3 circle

From Dev

How to center a List inside a D3 circle

Related Related

  1. 1

    How to get an non-overlapped circle packing d3 js chart?

  2. 2

    Zoomable Circle Packing with Automatic Text Sizing in D3.js

  3. 3

    d3.js circle packing along a line

  4. 4

    How to remove the outer circle in D3 bubble chart

  5. 5

    D3 Circle-Packing Clear Labeling Solution

  6. 6

    d3: svg image in zoom circle packing

  7. 7

    Why the value for r is negative from d3.hierarchy in circle packing in d3.js?

  8. 8

    d3 line/circle chart

  9. 9

    How to make a chart in D3 work in node.js

  10. 10

    how to make chart column with drill down with D3 Js

  11. 11

    D3 Zoomable Circle Packing in IE11 is expanding beyond the SVG boundary

  12. 12

    Realtime D3 bubble chart (circle pack)

  13. 13

    Flot: Stacked line chart bug - points get overlapped

  14. 14

    Progressbar.js - how to get a second circle?

  15. 15

    circle packing in excel vba

  16. 16

    How do I get both a line and area styled in my d3 line chart?

  17. 17

    how to show bar chart based on multiple filter values in D3 js

  18. 18

    How to update a D3 pie chart

  19. 19

    How to implement D3 donut chart?

  20. 20

    Image within circle object causes pixelation d3 js

  21. 21

    NVD3 Stacked Bar Chart Plus Line Overlapped

  22. 22

    How to get a fill below c3.js timeseries chart

  23. 23

    How to deal with the hover issue of overlapped non-square DIV?

  24. 24

    How to deal with the hover issue of overlapped non-square DIV?

  25. 25

    add circle in a spiral chart with d3js with line connecting to center

  26. 26

    d3js - line chart `circle` placement to the lines are not properly sets

  27. 27

    d3js - line chart `circle` placement to the lines are not properly sets

  28. 28

    How to center a List inside a D3 circle

  29. 29

    How to center a List inside a D3 circle

HotTag

Archive