dqybeh2884 2014-07-18 07:31
浏览 296

JSON字符串上的D3.js NaN错误

I'm getting a NaN error when trying to set the width of a d3 rect from a JSON encoded string, and I'm not seeing where I'm mucking things up.

Some background of the problem:

HTML Page with checkboxes in a form makes AJAX call to PHP on check and uncheck

PHP builds mySQL query based on status of checkboxes

PHP encodes mySQL's return to JSON

PHP then does: $json = preg_replace('/"(-?\d+\.?\d*)"/', '$1', $toecho);

/*^(this is done to remove double quotes around ints/floats)^*/

PHP echos $json to HTML div

Javascript assigns innerHTML of div to variable named res

var res = document.getElementById("result").innerHTML;

Then, part of the script attempts:

var bars = canvas.selectAll("rect")
        .data(res)
        .enter()
            .append("rect")
            .attr("width", function(d) {return parseInt(d.value1)})
            .attr("height", 10)
            .attr("y", function(d, i) {return i *100}); 
            }

I get a NaN error and I'm struggling to find why this is the case. The preg_replaceed JSON data is as such:

[{"value1":60},{"value1":14},{"value1":3},{"value1":7}]

which appears to be correct.

Things work up to the point of attempting to assign the int value of value1 to the width of the bar. My approach may be convoluted, and I'm always up for suggestions of how I might do this better, but I'd also really like to know that even if I'm not doing this in the best way (which the time it takes on roughly 5000 rows in the database tells me this is pretty process hungry)

Anybody know what I'm screwing up, how to fix this, and/or do this more efficiently?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器