分层边缘捆绑或径向收敛图

时间:2015-02-22 20:15:11

标签: php json d3.js bundle-layout

我希望可视化数据样本@ https://gist.github.com/mbostock/1044242

但是,我想从SQL DB中提取的值中这样做。 DB目前保存与奥运相关的样本数据,源自http://msftdbprodsamples.codeplex.com/releases/view/97636。里面有4张桌子。为了举例说明,表格'medalist'中的行与表'sport'和'discipline'相关。

我知道我必须编写一个PHP脚本来格式化JSON中的数据,由d3.js通过Eclipse读取,它在上面的链接中不会产生任何接近'readme-flare-imports.json'的内容。请参阅我对以下内容的尝试(我知道这远远不能接受)

尝试:

<?php
    $db_host    = "localhost";
    $db_user    = "root";
    $password   = "password";
    $database   = "olympics";

    $conn       = @mysqli_connect ($db_host, $db_user, $password, $database);

    if (! $conn) {
        trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
    }

    $data       = array();
    $myquery    = "SELECT * from medalist";
    $result     = mysqli_query ($conn, $myquery);

    while ($row = mysqli_fetch_array ($result)) {   //fetch row instead of entire objs
        $medalistID = $row ['medalistID'];
        $edition    = $row ['edition'];
        $athlete    = $row ['athlete'];
        $noc        = $row ['noc'];
        $gender     = $row ['gender'];
        $event      = $row ['event'];
        $medal      = $row ['medal'];
        $season     = $row ['season'];
        $medalValue = $row ['medalValue'];
        $sportID    = $row ['sportID'];
        $disciplineID   = $row ['disciplineID'];

        echo '{"medalistID":'.json_encode($medalistID).'}';
        echo '{"edition":'.json_encode($edition).'}';       
        echo '{"athlete":'.json_encode($athlete).'}';
        echo '{"noc":'.json_encode($noc).'}';
        echo '{"gender":'.json_encode($gender).'}';
        echo '{"event":'.json_encode($event).'}';
        echo '{"medal":'.json_encode($medal).'}';
        echo '{"season":'.json_encode($season).'}';
        echo '{"medalValue":'.json_encode($medalValue).'}';
        echo '{"sportID":'.json_encode($sportID).'}';
        echo '{"disciplineID":'.json_encode($disciplineID).'},<br>';

        $data[] = array ('medalistID'   => $medalistID,
                 'edition'  => $edition,
                 'athlete'  => $athlete,
                 'noc'      => $noc,
                 'gender'   => $gender,
                 'event'    => $event,
                 'medal'    => $medal,
                 'season'   => $season,
                 'medalValue'   => $medalValue,
                 'sportID'  => $sportID,
                 'disciplineID' => $disciplineID);              
    }
    $fp = fopen ('sample_Olympics.json', 'w');
    fwrite ($fp, json_encode ($data));
    fclose ($fp);   

    mysqli_close ($conn);
?>

我的问题是: - 参考上面的链接和我的尝试,如何更改代码以正确显示'sample_Olympics.json'?我不确定数据库中是否涉及任何层次结构,但是层次边缘捆绑是我在径向收敛图旁边找到的最接近的。

0 个答案:

没有答案