r2d3: d3.js bar chart disappears on resize

355 Views Asked by At

I'm rendering this d3 chart in an RMarkdown document:

Javascript (test.js):

// !preview r2d3 data=readr::read_tsv("X:/D3 Practice/data.tsv"), d3_version = "3", container="div"
//
// r2d3: https://rstudio.github.io/r2d3


var margin = {top: 40, right: 20, bottom: 30, left: 40},
    width = width - margin.left - margin.right,
    height = height - margin.top - margin.bottom;

var formatPercent = d3.format(".0%");

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .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(formatPercent);


var svg = div.append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");



r2d3.onRender(function(data, s, w, h, options) {
  x.domain(data.map(function(d) { return d.letter; }));
  y.domain([0, d3.max(data, function(d) { return d.frequency; })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  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")
      .text("Frequency");

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.letter); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency); })
      .attr("height", function(d) { return height - y(d.frequency); })


});

function type(d) {
  d.frequency = +d.frequency;
  return d;
}

Data (data.tsv):

letter  frequency
A   .08167
B   .01492
C   .02780
D   .04253
E   .12702
F   .02288
G   .02022
H   .06094
I   .06973
J   .00153
K   .00747
L   .04025
M   .02517
N   .06749
O   .07507
P   .01929
Q   .00098
R   .05987
S   .06333
T   .09056
U   .02758
V   .01037
W   .02465
X   .00150
Y   .01971
Z   .00074

R Code:

library(r2d3)
r2d3(data = readr::read_tsv("X:/D3 Practice/data.tsv"),
     script = "X:/D3 Practice/test.js",
     d3_version = "3",
     container="div")

Chart looks fine in R preview, it also looks fine in the output HMTL document. But when I resize the window up to a certain point, the chart disappears. In the console, I can see a message that says:

Node cannot be found in the current page.

Here's the initial HTML:

enter image description here

Here's when I resize (note the "error" div? No idea what that is).:

enter image description here

1

There are 1 best solutions below

9
SmokeyShakers On

It's my understanding that r2d3 has already declared and set width and height on init or resize. So you could give a minimum by changing the top of your js:

var margin = {top: 40, right: 20, bottom: 30, left: 40},
    min_width = 250, /// The smallest width your plot area (exluding margins) should have
    min_height = 480; /// The smallest height your plot area (exluding margins) should have
    width = d3.max([width, min_width]) - margin.left - margin.right;
    height = d3.max([height, min_height]) - margin.top - margin.bottom;