Skip to content Skip to sidebar Skip to footer

D3 - Dougnut Bubble Pie Chart

I am trying to create a merged pie/bubble chart. -- looking for a starting bubble chart base - maybe this one. http://jsfiddle.net/xsafy/ ^ need to cluster these bubbles - maybe s

Solution 1:

This is as close I have got so far.. I've merged the two charts together as such - although there is a bug still trying to update the bubbles -- at least get them to scale/morph/move/animate. Ideally I want them to stick close to their group segments..

here is the fiddle. https://jsfiddle.net/tk5xog0g/1/

enter image description here

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>doughpie</title><linkhref="css/generic.css"><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script><scriptsrc="http://d3js.org/d3.v3.min.js"></script></head><body><buttonclass="randomize">randomize</button><divdata-role="doughpie"data-width="450"data-height="450"id="graph"></div><script>//__invoke pie chart
            $('[data-role="doughpie"]').each(function(index) {
                createDoughnut(this);
            });


            functionbubbledata(data){
                //loop through data -- and MERGE childrenvar childs = [];
                $.each(data, function( index, value ) {
                    childs.push(value.children);                    
                });
                var merged = [].concat.apply([], childs);//flatterns multidimensional arrayreturn $.extend(true, {}, {"children": merged});// return deep clone
            }


            functioncreateDoughnut(el){

                  var width = $(el).data("width"),
                    height = $(el).data("height"),
                    radius = Math.min(width, height) / 2;

                  var svg = d3.select($(el)[0])
                    .append("svg")
                    .attr("width", width)
                    .attr("height", height)



                  //_create doughpie shellvar doughpie = svg.append("g")
                            .attr("class", "doughpie");

                      doughpie.append("g")
                        .attr("class", "slices");
                      doughpie.append("g")
                        .attr("class", "labels");
                      doughpie.append("g")
                        .attr("class", "lines");


                      var pie = d3.layout.pie()
                        .sort(null)
                        .value(function(d) {
                          return d.value;
                        });

                      var arc = d3.svg.arc()
                        .outerRadius(radius * 0.85)
                        .innerRadius(radius * 0.83);

                      var outerArc = d3.svg.arc()
                        .innerRadius(radius * 0.9)
                        .outerRadius(radius * 0.9);

                      doughpie.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

                      var key = function(d) {
                        return d.data.label;
                      };

                      var color = d3.scale.ordinal()
                        .range(["#46a2de", "#7b3cce", "#31d99c", "#de5942", "#ffa618"]);
                  //_create doughpie shell//_create bubblevar diameter = width/2;//take half/widthvar bubs = svg.append("g")
                            .attr("class", "bubs");

                    bubs.attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");

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

                  //_create bubblefunctionrandomData() {
                    var data1 = [{
                        "label": "AA",
                        "value": 0.911035425558026,
                        "children": [
                            {
                                name: "some text aa",
                                group: "AA",
                                size: 120
                            }
                        ]
                    }, {
                        "label": "BB",
                        "value": 0.08175111844879179,
                        "children": [
                            {
                                name: "some text bb",
                                group: "BB",
                                size: 123
                            }
                        ]
                    }, {
                        "label": "CC",
                        "value": 0.25262439557273275,
                        "children": [
                            {
                                name: "some text cc",
                                group: "CC",
                                size: 193
                            }
                        ]
                    }, {
                        "label": "DD",
                        "value": 0.8301366989535612,
                        "children": [
                            {
                                name: "some text dd",
                                group: "DD",
                                size: 29
                            },
                            {
                                name: "some text dd",
                                group: "DD",
                                size: 289
                            }
                        ]
                    }, {
                        "label": "EE",
                        "value": 0.0517762265780517,
                        "children": [
                            {
                                name: "some text ee",
                                group: "EE",
                                size: 389
                            },
                            {
                                name: "some text ee",
                                group: "EE",
                                size: 89
                            }
                        ]
                    }];

                    var data2 = [{
                        "label": "AA",
                        "value": 0.243879179,
                        "children": [
                            {
                                name: "some text aa",
                                group: "AA",
                                size: 120
                            }
                        ]
                    }, {
                        "label": "BB",
                        "value": 0.243879179,
                        "children": [
                            {
                                name: "some text bb",
                                group: "BB",
                                size: 123
                            }
                        ]
                    }, {
                        "label": "CC",
                        "value": 0.2342439557273275,
                        "children": [
                            {
                                name: "some text cc",
                                group: "CC",
                                size: 193
                            }
                        ]
                    }, {
                        "label": "DD",
                        "value": 0.2349535612,
                        "children": [
                            {
                                name: "some text dd",
                                group: "DD",
                                size: 29
                            },
                            {
                                name: "some text dd",
                                group: "DD",
                                size: 289
                            }
                        ]
                    }, {
                        "label": "EE",
                        "value": 0.2345780517,
                        "children": [
                            {
                                name: "some text ee",
                                group: "EE",
                                size: 389
                            },
                            {
                                name: "some text ee",
                                group: "EE",
                                size: 89
                            }
                        ]
                    }];

                    var j = Math.floor((Math.random() * 2) + 1);

                    if (j == 1) {
                      return data1;
                    } else {
                      return data2;
                    }
                  }


                  change(randomData());

                  d3.select(".randomize")
                    .on("click", function() {
                      change(randomData());
                    });

                  functionchange(data) {


                    /* ------- ANIMATE PIE SLICES -------*/var slice = doughpie.select(".slices").selectAll("path.slice")
                      .data(pie(data), key);

                    slice.enter()
                      .insert("path")
                      .style("fill", function(d) {
                        returncolor(d.data.label);
                      })
                      .style("transform", function(d, i){
                        //return "translate(0, 0)";
                      })
                      .attr("class", "slice");

                    slice
                      .transition().duration(1000)
                      .attrTween("d", function(d) {
                        this._current = this._current || d;
                        var interpolate = d3.interpolate(this._current, d);
                        this._current = interpolate(0);
                        returnfunction(t) {
                          returnarc(interpolate(t));
                        };
                      })

                    slice.exit()
                      .remove();
                    /* ------- ANIMATE PIE SLICES -------*//* ------- ANIMATE BUBBLES -------*/// generate data with calculated layout valuesvar data = bubbledata(data);

                        var nodes = bubble.nodes(data)
                          .filter(function(d) {
                             return !d.children;
                          }); // filter out the outer bubblevar bubbles = bubs.selectAll('circle')
                          .data(nodes);

                        bubbles.enter()
                          .insert("circle")
                          .attr('transform', function(d) {
                            return'translate(' + d.x + ',' + d.y + ')';
                          })
                          .attr('r', function(d) {
                            return d.r;
                          })
                          .style("fill", function(d) {
                            returncolor(d.group);
                          });

                        bubbles
                          .transition().duration(1000)

                        bubbles.exit()
                          .remove();

                        /* ------- ANIMATE BUBBLES -------*/

                  };
            }
        </script></body></html>

Solution 2:

I thought I will try and first plot mid dot points on the arcs -- and maybe from THOSE -- start to get a batch of zone coordinates to master control the coloured bubbles.

I've been trying to follow this -- How to get coordinates of slices along the edge of a pie chart?

enter image description here

https://jsfiddle.net/tk5xog0g/28/

/*placing mid dots*/var midDotsArrayCooridnates = [];

 slice
   .each(function(d) {
     x = 0 + (radius * 0.85) * Math.cos(d.startAngle);
     y = 0 + (radius * 0.85) * Math.sin(d.startAngle);

     var obj = {
       "x": x,
       "y": y
     }
     midDotsArrayCooridnates.push(obj);
   });

 $.each(midDotsArrayCooridnates, function(index, value) {
   var dot = doughpie.select(".slicedots").append('circle')
     .attr('cx', value.x)
     .attr('cy', value.y)
     .attr('r', 5)
     .style("fill", "red")
     .attr('class', "ccc")
 });

 /*placing mid dots*/

Post a Comment for "D3 - Dougnut Bubble Pie Chart"