Estoy usando d3.js para representar un mapa del mundo en svg (usando https://github.com/johan/world.geo.json/blob/master/countries.geo.json para las características). Estoy encapsulando la lógica de renderizado en una Vista Backbone. Cuando visualizo la vista y la adjunto al DOM, no aparece nada en mi navegador, aunque el marcado SVG se genera correctamente cuando se mira el HTML generado. Esto rinde bien cuando no encapsula dentro de Backbone.View. Aquí está mi código utilizando Backbone.view:SVG no se representa correctamente como una vista de red troncal
/**
* 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();
Aquí está el código que funciona, sin necesidad de utilizar Backbone.View
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);
También he adjuntado una captura de pantalla del marcado SVG generado. ¿Alguna idea de qué podría estar yendo mal aquí?
Editar - Este es el método reemplazado maquillaje que terminó la solución de este, por solicitud:
/**
* 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;
}
Gracias! Sobreescribí 'make' desde mi punto de vista y utilicé createElementNS con 'http://www.w3.org/2000/svg' en lugar de createElement y eso parece ser el truco. Apreciar la guía. –
@rr: ¿podría publicar su método make en alguna parte? –
@PierreSpring: Hecho, no estoy seguro de cuán relevante es esto con las versiones más nuevas de Backbone, no he estado al día. –