如何从 mysql 数据库中提取数据并使用 D3.JS 进行可视化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15384774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pull data from mysql database and visualize with D3.JS?
提问by BaRd
I have a database in MySQL
which I want to visualize in D3.JS
. In order to do that, first I want to parse
the data in JSON
format, then write a basic code which pulls data from database and visualize with D3.JS
. I look around but couldn't find what I want since I'm new to D3.JS
.
我有一个数据库MySQL
,我想在其中可视化D3.JS
. 为了做到这一点,首先我想要格式化parse
数据JSON
,然后编写一个基本代码,从数据库中提取数据并使用D3.JS
. 我环顾四周,但找不到我想要的东西,因为我是D3.JS
.
How can I achieve that? Any help or clue appreciated.
我怎样才能做到这一点?任何帮助或线索表示赞赏。
回答by d3noob
The following is a php script that you should be able to save somewhere as a file (let's say you call it 'getdata.php') accessible from your HTML file with your D3 code in it. When called it will return data from your MySQL database in a json format (so long as the database server isn't outside your domain);
以下是一个 php 脚本,您应该能够将其保存为一个文件(假设您将其称为“getdata.php”),该文件可以从您的 HTML 文件中访问,其中包含您的 D3 代码。调用时,它将以 json 格式从您的 MySQL 数据库返回数据(只要数据库服务器不在您的域之外);
<?php
$username = "******";
$password = "******";
$host = "******";
$database="***dbase_name***";
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
$myquery = "
query here
";
$query = mysql_query($myquery);
if ( ! $query ) {
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);
?>
Obviously you would need to enter appropriate details for username, password, host and database. You would also need to include an appropriate query for your data so that it returned what you were looking for. Something along the lines of (and this is only a guess);
显然,您需要为用户名、密码、主机和数据库输入适当的详细信息。您还需要为您的数据包含一个适当的查询,以便它返回您要查找的内容。类似的东西(这只是一个猜测);
SELECT `dateTimeTaken`, `reading` FROM `tablename`
Which would return a list of time stamps and values from a table called tablename
with columns called dateTimeTaken
and reading
.
Then when you go to read in your json file you would use the following syntax for the code where you would be reading in your json;
这将从tablename
名为dateTimeTaken
and 的列调用的表中返回时间戳和值的列表reading
。然后,当您读取 json 文件时,您将使用以下语法来读取您将在 json 中读取的代码;
d3.json("getdata.php", function(error, data) {
Hopefully that's close to what you're looking for. I've tested it locally and it all seems to work..
希望这与您要查找的内容相近。我已经在本地进行了测试,似乎一切正常..
I've put together a post to go over local installation of a simple WAMP server and setting up a query on the MySQL database from d3.js here http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html
我已经整理了一篇文章来介绍一个简单的 WAMP 服务器的本地安装,并在此处从 d3.js 设置对 MySQL 数据库的查询http://www.d3noob.org/2013/02/using-mysql-database -as-source-of-data.html
This is pretty much the same situation as Accessing MySQL database in d3 visualization
这与在 d3 可视化中访问 MySQL 数据库的情况几乎相同
回答by Marjancek
Short answer: Web Service.
简短回答:Web 服务。
Basically, you'd want to make a web service that will return json data (for instance) to connect to d3.json()
calls.
基本上,您希望创建一个 Web 服务,该服务将返回 json 数据(例如)以连接到d3.json()
调用。
I would recommend you to use Python to quickly do something extending SimpleHTTPServer
, or go with a web service framework such as web2py.
我建议你使用 Python 来快速扩展一些东西SimpleHTTPServer
,或者使用 web 服务框架,比如web2py。
回答by Rich O'Kelly
AFAIK there aren't any libraries that allow javascript to connect to a MySQL database directly. Instead consider exposing your data via a web service (using any number of server side technologies: ASP.Net, PHP, Ruby on Rails etc) and serialise your data to JSON in the response
AFAIK 没有任何库允许 javascript 直接连接到 MySQL 数据库。而是考虑通过 Web 服务公开您的数据(使用任意数量的服务器端技术:ASP.Net、PHP、Ruby on Rails 等)并将您的数据序列化为响应中的 JSON
回答by Danial
I have faced problem myself with json_encode, in the end I wrote my own code instead of json_encode. Just you need to set the sql and yoru connection info. Hope you find it useful.
我自己也遇到了 json_encode 的问题,最后我写了自己的代码而不是 json_encode。只需要设置 sql 和 yoru 连接信息。希望你觉得它有用。
$conn = new mysqli(DB_Host, DB_User, DB_Password, DB_Name) or die("Connection failed: " . $conn->connect_error);
$sql = "select AID as id, deDate_Value as date, nterTime AS counter
from xxxx";
//Get the Date and store it in array
$records = array();
if ( $result=mysqli_query($conn,$sql) )
while ( $obj=mysqli_fetch_object($result) )
$records[] = $obj;
//echo in json format on screen
echo "[";
$comma_1 = "";
foreach($records as $obj)
{
echo $comma_1."{";
$comma_2 = "";
foreach($obj as $key => $value)
{
echo $comma_2.'"'.$key."\": \"". $value . "\"";
$comma_2 = ", ";
}
echo "}";
$comma_1 = ", \n";
}
回答by Kevin Weinrich
To get d3.json to work, I had to call it the following way (my PHP file generates the JSON data):
为了让 d3.json 工作,我必须用以下方式调用它(我的 PHP 文件生成 JSON 数据):
d3.json("InventoryData.php").then(function(data){
console.log(data);
});
回答by M.Reza
You may use jQuery which is more popular as follows:
您可以使用更流行的 jQuery,如下所示:
$.getJSON( "ajax/test.json", function( data ) {
console.log(data);
}