使用下面的示例表,我想显示:
我的主要问题:如何才能最好地创建包含所有连接类型的所有边缘的列表?
假设我有一张电影和流派的表格:
GENRE | MOVIE
--------------------------
Drama | A
Action | A
Comedy | A
Documentary | B
Romantic | B
Action | B
Drama | B
Drama | C
Romantic | C
Action | C
---------------------------
我没有对可视化框架的偏好,但以下内容接近我的想法: http://visjs.org/examples/network/09_sizing.html
非常欢迎其他可视化建议!
根据我的电影示例,节点和边缘可能如下所示: http://jsfiddle.net/wivaku/90oef0pg/
在此示例中,边缘是硬编码的。在现实生活中,我想动态创建它们。 如何最好地创建边缘JSON,最好是使用PHP?
我目前的PHP代码段:
<?php
//the SQL rows (normally from SQL, now static):
$rows = json_decode('[["Drama","A"],["Action","A"],["Comedy","A"],["Documentary","B"],["Romantic","B"],["Action","B"],["Drama","B"],["Drama","C"],["Romantic","C"],["Action","C"]]');
$nodes = array();
$edges = array();
// create nodes
$genres = array_count_values(array_map(function($i) {return $i[0]; }, $rows));
foreach ($genres as $key => $value) {
$nodes[] = array("id"=>$key, "value"=>$value);
}
// create edges
// helpful to have genres grouped by movie? (normally from SQL, now static)
$movieGenres = json_decode('[{"movie":"A","genres":["Drama","Action","Comedy"]},{"movie":"B","genres":["Documentary","Romantic","Action","Drama"]},{"movie":"C","genres":["Drama","Romantic","Action"]}]');
// ...
print json_encode(["nodes"=>$nodes, "edges"=>$edges], JSON_NUMERIC_CHECK);
?>
提前致谢!
更新:关于SQL详情/选项的评论。我所拥有的表几乎与列出的一样。所以:genreId和contentId。 我正在探索的一个选项(作为PHP代码的快捷方式):连接每部电影的类型。
SELECT GROUP_CONCAT(genreId SEPARATOR "|") AS genres
FROM contentGenres
GROUP BY contentId
ORDER BY count(genreId) DESC
使用示例数据:
Drama|Action|Comedy
Documentary|Romantic|Action|Drama
Drama|Romantic|Action
或使用流派ID:
1|2|3
4|5|2|1
1|5|2
我的真实数据集的结果是±11000行,有些电影有8种类型。
答案 0 :(得分:3)
您可以在SQL级别进行处理,例如使用此查询:
SELECT a.genreId,b.genreId,count(*)
FROM genres as a, genres as b
WHERE a.contentId = b.contentId AND a.genreId < b.genreId
GROUP BY a.genreId, b.genreId
在您的示例中,ID被编号为类型:
1 Drama
2 Action
3 Comedy
4 Documentary
5 Romantic