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?