Updating the D3 histogram as data arrives

SO community,

I am making a D3 histogram as an Angular directive and I want it to be able to change / update accordingly, like the data it reads in changes. In other words, I am using Angular to view changes in the data and (hopefully) redraw the histogram every time the data changes.

This is mainly a question about D3 update and data binding, because $ watchCollection works fine. Even though I went through this tutorial on adding items to d3 chart, I still cannot apply it on my histogram. I think the way the elements of my histogram are nested is really confusing me ...

Context: Ideally, this histogram will be read from an array that will hold the data returned from multiple Ajax calls. Thus, every time a new dataset arrives, the histogram will become another bar. That is why I would like to know how to properly update the chart as well as the x axis.

Thank!:)

The JS script is here: http://jsfiddle.net/santina/wrtenjny/1/

The code for just the d3 part is here, mostly from the mbostock sorting histogram.

        // Aesthetic settings 
        var margin = {top: 20, right: 50, bottom: 20, left: 50},
            width = document.getElementById('performance').clientWidth - margin.left - margin.right || 
                    940 - margin.left - margin.right,
            height = 500 - margin.top - margin.bottom, 
            barColor = "steelblue", 
            axisColor = "whitesmoke", 
            axisLabelColor = "grey",
            yText = "# QUERIES", 
            xText = "BEACON IDs";

        // Inputs to the d3 graph 
        var data = scope[attrs.data];

        // A formatter for counts.
        var formatCount = d3.format(",.0f");

        // Set the scale, separate the first bar by a bar width from y-axis
        var x = d3.scale.ordinal()
            .rangeRoundBands([0, width], .1, 1);

        var y = d3.scale.linear()
            .range([height, 0]);

        var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

        var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left")
            .tickFormat(formatCount);

        // Initialize histogram 
        var svg = d3.select(".histogram-chart")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + margin.top + margin.bottom)
          .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        function drawAxis(){


            data.forEach(function(d) {
                d.nqueries = +d.nqueries;
            });

            x.domain(data.map(function(d) { return d.name; }));
            y.domain([0, d3.max(data, function(d) { return d.nqueries; })]);

            // Draw x-axis 
            svg.append("g")
                .attr("class", "x-axis")
                .attr("transform", "translate(0," + height + ")")
                .call(xAxis)
                .append("text")
                .attr("y", 6)
                .attr("dy", "-0.71em")
                .attr("x", width )
                .style("text-anchor", "end")
                .style("fill", axisLabelColor)
                .text(xText);

            // Draw y-axis 
            svg.append("g")
                .attr("class", "y-axis")
                .call(yAxis)
                .append("text")
                .attr("transform", "rotate(-90)")
                .attr("y", 6)
                .attr("dy", ".71em")
                .style("text-anchor", "end")
                .style("fill", axisLabelColor)
                .text(yText);

            // Change axis color 
            d3.selectAll("path").attr("fill", axisColor);
        }

        function updateAxis(){
            console.log(data);
            data.forEach(function(d) {
                d.nqueries = +d.nqueries;
            });

            x.domain(data.map(function(d) { return d.name; }));
            y.domain([0, d3.max(data, function(d) { return d.nqueries; })]);

            svg.selectAll("g.y_axis").call(yAxis);
            svg.selectAll("g.x_axis").call(xAxis);

        }


        function drawHistogram(){

            drawAxis();

            var bar = svg.selectAll(".bar")
                .data(data)
                .enter().append("g")
                .attr("class", "barInfo");

            bar.append("rect")
                .attr("class", "bar")
                .attr("x", function(d){ return x(d.name) })
                .attr("width", x.rangeBand())
                .attr("y", function(d){ return y(d.nqueries) })
                .attr("height", function(d) { return height - y(d.nqueries); })
                .attr("fill", barColor);

            bar.append("text")
                .attr("y", function(d){ return y(d.nqueries) })
                .attr("x", function(d){ return x(d.name) })
                .attr("dy", "-1px")
                .attr("dx", x.rangeBand()/2 )
                .attr("text-anchor", "middle")
                .attr("class", "numberLabel")
                .text(function(d) { return formatCount(d.nqueries); });
        }

        // Doesn't work :( 
        function updateHistogram(){
            console.log("updating");

            // Redefine scale and update axis 
            updateAxis(); 

            // Select 
            var bar = svg.selectAll(".barInfo").data(data);

            // Update - rect 
            var rects = bar.selectAll("rect")
                .attr("class", "bar")
                .attr("x", function(d){ return x(d.name) })
                .attr("width", x.rangeBand());

            // Update 
            var texts = bar.selectAll("text")
                .attr("x", function(d){ return x(d.name) })
                .attr("dx", x.rangeBand()/2 );

            // Enter 
            bar.enter().append("g")
                .attr("class", "bar").selectAll("rect").append("rect")
                .attr("class", "bar")
                .attr("x", function(d){ return x(d.name) })
                .attr("width", x.rangeBand())
                .attr("y", function(d){ return y(d.nqueries) })
                .attr("height", function(d) { return height - y(d.nqueries); })
                .attr("fill", barColor);

            bar.enter().append("g")
                .attr("class", "bar").selectAll("text").append("text")
                .attr("y", function(d){ return y(d.nqueries) })
                .attr("x", function(d){ return x(d.name) })
                .attr("dy", "-1px")
                .attr("dx", x.rangeBand()/2 )
                .attr("text-anchor", "middle")
                .attr("class", "numberLabel")
                .text(function(d) { return formatCount(d.nqueries); });

        }

        drawHistogram();

      

+3


source to share


1 answer


First, you got the wrong selector class

when updating the axis:

svg.selectAll("g.y-axis").call(yAxis); //<-- dash not underscore
svg.selectAll("g.x-axis").call(xAxis);

      

Second, you were close to your update, but we can clean it up a bit:

// select on what you originally binded data to
var bar = svg.selectAll(".barInfo").data(data);

// for data entering
var bEnter = bar.enter().append("g")
    .attr("class", "barInfo");    
// append a rect
bEnter.append("rect")
    .attr("class", "bar");
// and the text elements
bEnter.append("text")
    .attr("class","numberLabel");

// now we can update everybody together
bar.select("rect")
    .attr("x", function(d){ return x(d.name) })
    .attr("width", x.rangeBand())
    .attr("y", function(d){ return y(d.nqueries) })
    .attr("height", function(d) { return height - y(d.nqueries); })
    .attr("fill", barColor);

bar.select("text")
    .attr("y", function(d){ return y(d.nqueries) })
    .attr("x", function(d){ return x(d.name) })
    .attr("dy", "-1px")
    .attr("dx", x.rangeBand()/2 )
    .attr("text-anchor", "middle")
    .attr("class", "numberLabel")
    .text(function(d) { return formatCount(d.nqueries); }); 

      

A generalized example is here .

edits

Opps, I am not choosing my updates correctly.



bar.selectAll("rect")

      

Should be:

bar.select("rect")

      

This fixes both updates and sorting ...

Updated fiddle .

Also note that I have ruined your code again. With your angular chat, you don't really need a separate draw and update function, you can do both.

+1


source







All Articles