【问题标题】:How to dynamically append text to svg using d3 and Ajax?如何使用 d3 和 Ajax 将文本动态附加到 svg?
【发布时间】:2017-12-16 00:44:19
【问题描述】:

我对 Ajax 调用一无所知,已经阅读了几个小时,但所有教程都提到了加载方法和像 click 这样的监听器。

我有一个函数 (drawThreat) 在我的 svg 上添加一些图标 (fontawesome),有一个带有一堆 x 和 ys 的 json 文件,我需要做的就是有一个 ajax 调用来运行这个函数json 文件中的所有 x 和 ys 每 5 秒更新一次页面上的 svg 元素。这是函数:

function drawThreat (x, y){
  var canvas = d3.select("#map")
                 .append("svg")
                 .attr("width", 500)
                 .attr("height", 500)
                 .attr("id", "Threat");

  var Threat = canvas.append('svg')
                    .append('text')
                    .attr('text-anchor', 'middle')
                    .attr('dominant-baseline', 'central')
                    .attr("x", x)
                    .attr("y", y)
                    .attr("class", "threat")
                    .style('font-family','FontAwesome')
                    .style('font-size','20px')
                    .style('fill', 'red')
                    .text(function (d) {
                    return '\uf2be'
                    });
        return Threat
};

任何帮助都将不胜感激:) 即使它是指向您发现与该问题相关的教程的链接。到目前为止,我看了 6 或 7 个教程,但都没有成功。

【问题讨论】:

  • 能否提供json文件的预览?
  • json 文件还没有制作,目前的目标是用一组数据来伪造函数,例如:{ "d1":[125, 250], "d2":[180, 250], "d3":[40, 115]},当 json 文件中的这些 x 和 y 之一被更新时,svg 应该更新图标的位置而不刷新。现在我希望函数 drawThreat 每 5 秒运行一次,即使数据没有改变......希望我说得通

标签: javascript jquery ajax d3.js svg


【解决方案1】:

如果我正确理解您的需求,您基本上可以使用类似于以下的代码:

var dataFileUrl = "url-to-your.json";          // assign the url to a variable
var canvas = d3.select("#map")                 // get a ref from the SVG element
                  .append("svg")               // you might want to define it only one time
                      .attr("width", 500)
                      .attr("height", 500)
                      .attr("id", "Threat");


var updateInterval = setInterval(function(){  
    d3.json(dataFileUrl , function (jsonData) { // AJAX call
        drawThreat(jsonData);                   // calls main function
    });                                    
},5000);                                        // every 5 * 1000ms 


function drawThreat (jsonData){ 
    canvas.selectAll('text.threat').remove();  // makes sure we don't have old icons
    canvas.selectAll('text')
        .data(jsonData)                        // for each set in json
            .enter()
                .append('text')                // create a text and append it to svg canvas
                .attr("class", "threat")       // with class of threat
                .attr("x", function (d) {  return d[0]; })  // first element of each set
                .attr("y", function (d) {  return d[1]; })  // second element of each set
                .text('\uf2be');                

 };

为了减少 js 代码,我建议使用 CSS 添加样式和属性:

.threat{                   
    text-anchor:middle;
    dominant-baseline:central;
    font-family:FontAwesome;
    font-size:20px;
    fill:red;
}

你也可以在这里看到它的实际效果:https://codepen.io/FaridNaderi/pen/awPBwm

希望它能帮助你明白这一点。

【讨论】:

  • 非常感谢!!我已将您提供的代码放在一个函数中,并在我的文档中调用了该函数,但我得到: Uncaught TypeError: canvas.selectAll(...).data(...).enter is not a function ....一遍又一遍。
  • 很高兴听到这个消息?,我认为您应该在这里提供您的新代码,在 jsfiddle 或 codepen 等中。通常会有一些小问题导致此类错误。
  • 搞定了!每当 json 文件出现问题时,它都会抛出该错误...再次感谢您的帮助!
猜你喜欢
  • 2014-01-05
  • 2014-03-10
  • 2016-12-05
  • 2017-12-18
  • 2021-06-27
  • 2016-07-22
  • 2015-07-25
  • 1970-01-01
相关资源
最近更新 更多