doutuo3575 2017-04-11 17:18
浏览 439
已采纳

将JSON数据传递给d3.json时出错

When I click a button, I pass an array of data from a viewmodel. On the d3.json function, I use json_encode against the viewmodel to tranform it to a JSON object. When I inspect it on the page, it does convert it but I am getting an

"Uncaught SyntaxError: missing ) after argument list".

Can someone tell me what I am doing wrong?

d3.json("<?php echo json_encode($viewmodel) ?>", function(error,data){
        data.forEach(function(d) {
          d.projectdate = parseDate(d.projectdate);
          d.Cost = +d.Cost;
        });

          var svg = d3.select("body")
                      .append("svg")
                      .attr("id", "chart")
                      .attr("width", w)
                      .attr("height", h);

          svg.selectAll("bar")
              .data(data)
              .enter()
                .append("rect")
                .classed("bar", true)
                .attr("x", 0)
                .attr("y", function(d, i){
                    return i * 20
                })
                .attr("width", function(d,i){
                    return d;
                })
                .attr("height", 20);


    });
  • 写回答

1条回答 默认 最新

  • dongpan1308 2017-04-11 23:10
    关注

    d3.json() expects a url as its first parameter, not a JSON string. This will in turn request the specified file at that particular url to load the JSON from.

    In your case, however, it might be easier to directly assign the JSON to the data variable.

    var data = JSON.parse("<?php echo json_encode($viewmodel) ?>");
    
    data.forEach(function(d) {
      //...
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我调试idea基于spring boot项目
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 分析下图所示同步时序逻辑电路的逻辑功能。
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合