我需要一些有关 d3 和 MySQL 的帮助。以下是我的问题:
我正在使用 d3 强制定向布局来可视化存储在 MySQL 中的数据。我现在想使用 d3 对其进行可视化。据我对 d3 的了解,它需要 json 文件作为包含节点和链接的输入。我的问题是:如何从 d3 脚本以指定格式访问此 MySQL 数据库?我想要 d3 工具的这种输入。请帮助我。
{"nodes":[{"name":"Myriel","group":1},
{"name":"Mlle.Baptistine","group":1},
{"name":"Mme.Hucheloup","group":1}],
"links":[{"source":1,"target":0,"value":1},
{"source":2,"target":0,"value":8},
{"source":3,"target":0,"value":10}]}
最佳答案
这里有一篇关于在 D3 中使用 MySQL 数据库的博客文章 http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html .
关键是让 d3 中的数据调用调用 PHP 脚本,该脚本以 d3 期望的格式返回数据。
例如这一行;...
d3.json("php/data2.php", function(error, data) {
...是告诉 d3 以 JSON 格式获取数据,但它正在调用一个 php 脚本,该脚本正在查询数据库并以 JSON 格式返回数据;
这是它调用的脚本(data2.php);
<?php
$username = "homedbuser";
$password = "homedbuser";
$host = "localhost";
$database="homedb";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "
SELECT `date`, `close` FROM `data2`
";
$query = mysql_query($myquery);
if ( ! $myquery ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
关于d3.js - 如何在 d3 工具的强制布局中可视化 mysql 数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795412/