Note that there are some explanatory texts on larger screens.

plurals
  1. POSVG not rendering properly as a backbone view
    primarykey
    data
    text
    <p>I'm using d3.js to render a map of the world in svg (using <a href="https://github.com/johan/world.geo.json/blob/master/countries.geo.json" rel="noreferrer">https://github.com/johan/world.geo.json/blob/master/countries.geo.json</a> for the features). I am encapsulating the rendering logic in a Backbone View. When I render the view and attach it to the DOM, nothing displays in my browser, though the SVG markup is correctly generated when looking at the generated HTML. This renders fine when not encapsulating within a Backbone.View. Here's my code using Backbone.view:</p> <pre><code>/** * SVG Map view */ var MapView = Backbone.View.extend({ tagName: 'svg', translationOffset: [480, 500], zoomLevel: 1000, /** * Sets up the map projector and svg path generator */ initialize: function() { this.projector = d3.geo.mercator(); this.path = d3.geo.path().projection(this.projector); this.projector.translate(this.translationOffset); this.projector.scale(this.zoomLevel); }, /** * Renders the map using the supplied features collection */ render: function() { d3.select(this.el) .selectAll('path') .data(this.options.featureCollection.features) .enter().append('path') .attr('d', this.path); }, /** * Updates the zoom level */ zoom: function(level) { this.projector.scale(this.zoomLevel = level); }, /** * Updates the translation offset */ pan: function(x, y) { this.projector.translate([ this.translationOffset[0] += x, this.translationOffset[1] += y ]); }, /** * Refreshes the map */ refresh: function() { d3.select(this.el) .selectAll('path') .attr('d', this.path); } }); var map = new MapView({featureCollection: countryFeatureCollection}); map.$el.appendTo('body'); map.render(); </code></pre> <p>Here's the code that works, without using Backbone.View</p> <pre><code>var projector = d3.geo.mercator(), path = d3.geo.path().projection(projector), countries = d3.select('body').append('svg'), zoomLevel = 1000; coords = [480, 500]; projector.translate(coords); projector.scale(zoomLevel); countries.selectAll('path') .data(countryFeatureCollection.features) .enter().append('path') .attr('d', path); </code></pre> <p>I've also attached a screenshot of the generated SVG markup. Any idea what could be going wrong here?</p> <p><img src="https://i.stack.imgur.com/M3ulh.png" alt="enter image description here"></p> <p>Edit - Here's the overridden make method that ended up solving this, per request:</p> <pre><code>/** * Custom make method needed as backbone does not support creation of * namespaced HTML elements. */ make: function(tagName, attributes, content) { var el = document.createElementNS('http://www.w3.org/2000/svg', tagName); if (attributes) $(el).attr(attributes); if (content) $(el).html(content); return el; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload