1

I have a bubble chart in D3. How do I update the chart when new values come in?

    $scope.getDashboardStats = function() {
            $('#bubble-chart').show();
            axios.post('/fbsidashboard/getDashboardStats', {rsrc:  $scope.selectedResourceName ?  $scope.selectedResourceName : ''})
                .then(function(res) {
                    $('#bubble-chart').hide();
                    var data = res.data;
                    $scope.dashboardStats = data.rows;
                    var svg = d3.select("svg"),
                        width = +svg.attr("width"),
                        height = +svg.attr("height");

                    var format = d3.format(",d");

                    var color = d3.scaleOrdinal(d3.schemeCategory20c);

                    var pack = d3.pack()
                        .size([width, height])
                        .padding(1.5);

                    var root = d3.hierarchy({ children: $scope.dashboardStats })
                        .sum(function(d) { return d.value; })
                        .each(function(d) {
                            if (id = d.data.id) {
                                var id, i = id;
                                d.id = id;
                                d.package = id.slice(0, i);
                                d.class = id.slice(i + 1);
                            }
                        });

                    var node = svg.selectAll(".node")
                        .data(pack(root).leaves())
                        .enter().append("g")
                        .attr("class", "node")
                        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

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

                    node.append("clipPath")
                        .attr("id", function(d) { return "clip-" + d.id; })
                        .append("use")
                        .attr("xlink:href", function(d) { return "#" + d.id; });

                    node.append("text")
                        .attr("clip-path", function(d) { return "url(#clip-" + d.id + ")"; })
                        .selectAll("tspan")
                        .data(function(d) { return d.class.split(/(?=[A-Z][^A-Z])/g); })
                        .enter().append("tspan")
                        .attr("x", 0)
                        .attr("y", function(d, i, nodes) { return 13 + (i - nodes.length / 2 - 0.5) * 10; })
                        .text(function(d) { return d; });

                    node.append("title")
                        .text(function(d) { return d.id + "\n" + format(d.value); });
                });
            };

How do I update the chatt when $scope.dashboardStats are replaced by new values from a post response? I was using highcharts earlier and it does the trick automatically.

arunmmanoharan
  • 2,535
  • 2
  • 29
  • 60
  • aren't you rendering the chart with every post request? – Hany Habib Jun 14 '18 at 18:49
  • I am. I am calling the post request everytime a new value for the post body gets updated. – arunmmanoharan Jun 14 '18 at 18:57
  • so what your exact issue because in the promise success you are rendering the chart again ?.. – Hany Habib Jun 14 '18 at 19:00
  • The chart doesnt re-render on promise success. It just has old data in the dom. – arunmmanoharan Jun 14 '18 at 19:05
  • can you try : d3.select("svg").remove(); before rendering again ? – Hany Habib Jun 14 '18 at 19:10
  • I did. Not working. Removes the whole svg from the dom and doesnt mount the new one. – arunmmanoharan Jun 14 '18 at 19:17
  • Can you post some data ? Or could you please change it to a working snippet? It's pretty easy to update the nodes using #d3-update. – Shashank Jun 14 '18 at 19:54
  • The `axios` client is not integrated with the AngularJS framework. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc. Instead use the [AngularJS $http Service](https://docs.angularjs.org/api/ng/service/$http). – georgeawg Jun 14 '18 at 20:50
  • Hi, I am able to get the new data when I console.log $scope.dashboardStats. The only issue is that the chart doesnt rerender. – arunmmanoharan Jun 15 '18 at 10:02
  • Its not a duplicate. The issue is that the old one from the DOM is not removed and the new one gets overlayed on top of each other. – arunmmanoharan Jun 18 '18 at 13:38

0 Answers0