我可能被问了很多次,并且我已经阅读了很多关于它的线程,但简单地无法弄清楚如何将 json 数据 pbject 从 php 传递到 java 脚本。
我正在尝试从数据库中获取两个一维 json 数据对象数组(?)并将它们合并到一个两个 dinemsional json 数据对象数组中,然后将其进一步传递给 sigma.js 以进行进一步处理。
从数据库中读取数据:
<?php require 'sort.php'; $db = db(); $rawNodes = $db->select('node', '*', ['ico_id' => $_GET['id']]); $rawEdges = $db->select("edge", "*", ["ico_id" => $_GET['id']]); $nodes = array(); foreach ($rawNodes as $node) { array_push($out, $node); } $edges = array(); foreach ($rawEdges as $edge) { array_push($out, $edge); } ?>
现在在 Java Script 中访问它:
<html> <head> <title>Dynamic Transaction Visualization </title> <style type="text/css"> #container { max-width: 1200px; height: 800px; margin: auto; } </style> <script src="build/sigma.min.js"></script> <script src="build/plugins/sigma.parsers.json.min.js"></script> <script src="build/plugins/sigma.renderers.edgeLabels.min.js"></script> <script> function getData () { var gnodes = <?php echo json_encode($nodes); ?>; var gedges = <?php echo json_encode($edges); ?>; alert(gnodes[0].adress); // <- Doesnt work? Why? var g = {nodes: [], edges: []}; //<- i think this sytanx is correct for making a 2 dim json data object array- is it? var xpos = 0; var ypos = 0; var n; for (n in gnodes) { //<- Does not iterate through any Elements alert(gnodes[n].adress); //<- Doest work either? Why? xpos = n.block_number - 4400000; ypos += 100; if (ypos > 10000) { ypos = 0 } g.nodes.push( { "id": n.adress, "label": n.adress, "size": 1, //n.size, "x": xpos, "y": ypos } ) } var edgecount = 0; var m; for (m in gedges) { g.edges.push( { "id": edgecount++, "source": m.node_adress1, "target": m.node_adress2, "label": m.erc20_value, "type": "arrow" } ) } //g.nodes = gnodes; geht nicht benennung nicht passend //g.edges = gedges; return g; } function dispGraph() { var gdata = getData(); s = new sigma({ graph: gdata, container: 'container', renderer: { container: "container", type: "canvas" }, settings: { edgeLabelSize: 'fixed', nodeLabelSize: 'fixed', defaultNodeColor: '#ec5148', maxNodeSize: 15, minNodeSize: 5, minEdgeSize: 4, maxEdgeSize: 4, minArrowSize: 4, //edgeLabelSize: 'fixed', // {string} The opposite power ratio between the font size of the label and // the edge size: // Math.pow(size, -1 / edgeLabelSizePowRatio) * size * defaultEdgeLabelSize edgeLabelSizePowRatio: 1, // {number} The minimum size an edge must have to see its label displayed. edgeLabelThreshold: 1, } }); } </script> </head> <body> <div id="container"></div> <script>dispGraph()</script> </body> </html>
我认为这很容易。在 html 文件中(扩展名为 .php):
<?php $json = json_encode($data); ?> <script> var json = <?=$json?>; </script>