【发布时间】:2017-01-09 08:10:28
【问题描述】:
我有一个使用d3.js 创建的图表,如果您将鼠标悬停在月份上,则会找到每周得分。
小提琴链接到我在这里谈论的图表:https://jsfiddle.net/qp7L1hob/1/
我的数据库中还有一个表,我在其中维护员工每周的得分记录。您在这里看到的只是四个星期 -(第 1 周到第 4 周),但在我的数据库中,我记录了所有每个月的几个星期。这仅供参考。
桌分评分: 用户登录后,我只想显示该人的相关分数。假设 Rob 登录,他的第 1 周分数是 47,第 2 周是 44,第 3 周是 44,第 4 周是 43。
问题:我是来自SQL 服务器的Using php to extract json。在PHP 文件下面就是这样做的。
问题是我如何将JSON 放入d3.js 文件中,即上面的小提琴。
请帮忙。
json 文件(让我们将其命名为data.php):我知道我需要在上面的d3.js 文件中包含这个data.php。但不确定如何。
<?php
session_start();
$servername="xxxxxxx";
$connectioninfo=array('Database'=>'xxxxxx');
$conn=sqlsrv_connect($servername,$connectioninfo);
if($conn)
{
echo 'connection established';
}
else
{
echo 'connection failure';
die(print_r(sqlsrv_errors(),TRUE));
}
$q1="SELECT WeekNumber,pointsRewarded,EmployeeID FROM pointsBadgeTable WHERE EmployeeID = '" . $_SESSION['id'] . "' ";
$stmt=sqlsrv_query($conn,$q1);
if($stmt==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
$result[] = $row;
}
} while (sqlsrv_next_result($stmt));
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first
echo json_encode($result); //You will get the encoded array variable
?>
【问题讨论】:
-
我不确定您将数据导入 D3.js 是什么意思。如果您仔细观察,您会在显示工具提示(周信息)时引用数组“dataWeeks”。一种方法是首先填充数组并生成图形。将 dataWeeks(填充来自端点的数据)传递给生成函数。
-
@FarooqKhan 感谢您的意见。我是 d3.js 的新手,所以在这里需要很少的帮助来将数据周传递给生成函数。我无法填充数组,因为数据是动态的。但是是否可以将 json 中的变量回显到数据周中。你会怎么做?
标签: javascript php sql-server json d3.js