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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?