i tring use php script generate json string , import d3 force directed graph script follows:
<!doctype html> <meta http-equiv="content-type" content="text/html; charset=utf8"> <style> .links line { stroke: #999; stroke-opacity: 0.6; } .nodes circle { stroke: #fff; stroke-width: 1.5px; } </style> <svg width="960" height="600"></svg> <script src="https://d3js.org/d3.v4.min.js" charset="urf-8"></script> <script> var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); var color = d3.scaleordinal(d3.schemecategory20); var simulation = d3.forcesimulation() .force("link", d3.forcelink().id(function(d) { return d.id; })) .force("charge", d3.forcemanybody()) .force("center", d3.forcecenter(width / 2, height / 2)); d3.json("getneighborhood.php", function(error, graph) { if (error) throw error; var link = svg.append("g") .attr("class", "links") .selectall("line") .data(graph.links) .enter().append("line") .attr("stroke-width", function(d) { return math.sqrt(d.value); }); var node = svg.append("g") .attr("class", "nodes") .selectall("circle") .data(graph.nodes) .enter().append("circle") .attr("r", 5) .attr("fill", function(d) { return color(d.group); }) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); node.append("title") .text(function(d) { return d.id; }); simulation .nodes(graph.nodes) .on("tick", ticked); simulation.force("link") .links(graph.links); function ticked() { link .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); } }); function dragstarted(d) { if (!d3.event.active) simulation.alphatarget(0.3).restart(); d.fx = d.x; d.fy = d.y; } function dragged(d) { d.fx = d3.event.x; d.fy = d3.event.y; } function dragended(d) { if (!d3.event.active) simulation.alphatarget(0); d.fx = null; d.fy = null; } </script>
getneighborhood.php looks this:
<?php error_reporting(e_error|e_warning); // create connection $con=mysqli_connect("localhost","****","****","****"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $focusid = 5885; if (!isset($focusid)) { echo "no focusid"; die; } $links = array(); $nodes = array(); $result = $con->query("select a.personid ego, a.personid source, b.personid target messagexperson a, messagexperson b a.personid = $focusid , a.personid <> b.personid , a.messageid = b.messageid union select a.personid ego, b.personid source, c.personid target messagexperson a, messagexperson b,messagexperson c a.personid = $focusid , a.personid <> b.personid , a.personid <> c.personid , b.personid < c.personid , a.messageid = b.messageid , a.messageid = c.messageid , b.messageid = c.messageid"); $nodectr = 0; while($row = $result->fetch_row()) { $from = $row[1]; if (!isset($nodes[$from])) { $presult = $con->query("select name person id = $from") or die(mysql_error()); $temp = $presult->fetch_row(); $nodes[$from]['name'] = $temp[0]; $nodes[$from]['idx'] = $nodectr; $fromidx = $nodectr; $nodectr++; } else { $fromidx = $nodes[$from]['idx']; } $to = $row[2]; if (!isset($nodes[$to])) { $presult = $con->query("select name person id = $to") or die(mysql_error()); $temp = $presult->fetch_row(); $nodes[$to]['name'] = $temp[0]; $nodes[$to]['idx'] = $nodectr; $toidx = $nodectr; $nodectr++; } else { $toidx = $nodes[$to]['idx']; } $data[$from][$to]++; } $nodectr = 0; foreach ($nodes $id => $val) { $name = $val['name']; $d3['nodes'][$nodectr]['id'] = $name; $d3['nodes'][$nodectr]['group'] = 1; $nodectr++; $d3['nodes']; } $linkctr = 0; foreach ($data $from => $list){ $fromname = $nodes[$from]['name']; foreach ($list $to => $count) { $toname = $nodes[$to]['name']; $d3['links'][$linkctr]['source'] = $fromname; $d3['links'][$linkctr]['target'] = $toname; $d3['links'][$linkctr]['value'] = 1; $linkctr++; } } echo json_encode($d3); mysql_close($con); ?>
when run this, in console (same chrome , firefox):
mailgraph.htm:32 uncaught syntaxerror: unexpected token < in json @ position 2519 @ json.parse (<anonymous>) @ object.<anonymous> (d3.v4.min.js:7) @ xmlhttprequest.e (d3.v4.min.js:7)
the offending line "if (error) throw error", , poition 2519 second last character of json string.
when save output of getneighborhood.php json file , use in d3.json function instead, script runs fine , produces expected output. have searched around , found advice specify utf-8 encoding html , .js, can see did, didn't fix problem. php script running on xampp, if important.
any ideas problem be?
Comments
Post a Comment