首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG不能正确呈现为主干视图

SVG不能正确呈现为主干视图
EN

Stack Overflow用户
提问于 2012-03-11 07:37:33
回答 3查看 5.2K关注 0票数 21

我正在使用d3.js在svg中渲染世界地图(使用https://github.com/johan/world.geo.json/blob/master/countries.geo.json实现特性)。我将渲染逻辑封装在Backbone View中。当我呈现视图并将其附加到DOM时,虽然在查看生成的HTML时正确地生成了SVG标记,但浏览器中没有显示任何内容。当不封装在Backbone.View中时,这可以很好地呈现。下面是我使用Backbone.view的代码:

代码语言:javascript
复制
/**
 * 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();

以下是无需使用Backbone.View即可正常工作的代码

代码语言:javascript
复制
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);

我还附上了生成的SVG标记的屏幕截图。你知道这里会出什么问题吗?

Edit --这是一个被覆盖的make方法,根据请求最终解决了这个问题:

代码语言:javascript
复制
/**
 * 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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-11 11:44:08

问题是"svg“元素需要一个名称空间。D3会自动执行此操作;当您附加"svg“元素时,它将使用名称空间"http://www.w3.org/2000/svg”。详情请参见src/core/ns.js。不幸的是,Backbone似乎不支持命名空间元素。您可能想要更改view.make方法。然后,您需要在视图上使用namespaceURI属性来设置适当的名称空间,或者为与HTML5解析器保持一致而自动为SVG元素设置名称空间。

无论如何,解决问题的简单方法是将SVG包装在一个DIV元素中,然后使用D3创建SVG元素。

票数 27
EN

Stack Overflow用户

发布于 2012-07-23 21:57:27

您可以简单地在initialize函数中设置视图的元素,如下所示:

代码语言:javascript
复制
Backbone.View.extend({
    // This is only for informaiton. The node will
    // be raplaced in the initialize function.
    tagName: 'svg',

    initialize: function () {
        this.setElement(
            d3.select($('<div/>')[0]).append('svg')[0]
        );
    }
);

这有一个优势,就是它是明确的。

票数 4
EN

Stack Overflow用户

发布于 2013-10-12 14:41:52

查看来自http://nocircleno.com/blog/svg-with-backbone-js/http://jsfiddle.net/nocircleno/QsEp2/

代码语言:javascript
复制
Backbone.View.extend({
  nameSpace: "http://www.w3.org/2000/svg",
  _ensureElement: function() {
     if (!this.el) {
        var attrs = _.extend({}, _.result(this, 'attributes'));
        if (this.id) attrs.id = _.result(this, 'id');
        if (this.className) attrs['class'] = _.result(this, 'className');
        var $el = $(window.document.createElementNS(_.result(this, 'nameSpace'), _.result(this, 'tagName'))).attr(attrs);
        this.setElement($el, false);
     } else {
        this.setElement(_.result(this, 'el'), false);
     }
 }
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9651167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档