In backbone.js, a view's render function generates unattached html which can be later attached to the dom.
Currently, I have to have an existing target in the HTML for me to append a svg to. I then use the data/enter pattern to insert elements into the svg. Is there a way to get d3.js to generate the svg without attaching it to the dom?
var svg = d3.select("#target").append('svg')
.attr("viewBox","0 0 100 100");
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("r", 10)
.style("fill", "black");
Alternatively, is it possible to provide d3 with a unattached dom element to append stuff to? Something like this? D3.js documentation suggests that select can accept nodes, but the following does not work for me either
var svg = d3.select(this.$el).append('svg') // Uncaught TypeError: Object [object Object] has no method 'appendChild'
.attr("viewBox","0 0 100 100");
svg.selectAll("circle")
.data([1,2,3])
.enter().append("circle")
.attr("r", 10)
.style("fill", "black");
To create SVG using D3. js, let us follow the steps given below. Step 1 − Create a container to hold the SVG image as given below. Step 2 − Select the SVG container using the select() method and inject the SVG element using the append() method.
render() The Backbone. js render method overrides itself with your code that renders the view template from model data and updates this with new HTML. It contains the logic for rendering the template which constructs the view.
D3 is a JavaScript library and framework for creating visualizations. D3 creates visualizations by binding the data and graphical elements to the Document Object Model. D3 associates (binding) the data (stuff you want to visualize) with the DOM. This allows the user to manipulate, change or add to the DOM.
Install D3 by running npm install d3 --save . Import D3 to App. js by adding import * as d3 from d3 . You need to use import * (“import everything”) since D3 has no default exported module.
I found the answer in this post SVG not rendering properly as a backbone view
d3.select(this.el)
does the trick!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With