【问题标题】:Convert JSON-LD to d3.js node/links format?将 JSON-LD 转换为 d3.js 节点/链接格式?
【发布时间】:2015-09-20 05:49:50
【问题描述】:

我正在尝试对 OWL 本体进行可视化。我已将其转换为 JSON-LD,但现在需要将其转换为 D3 的节点/链接格式吗?作为参考,格式如下所示:

{
  "nodes": [
    {"x": 469, "y": 410},
    {"x": 493, "y": 364},
    {"x": 442, "y": 365},
    {"x": 467, "y": 314},
    {"x": 477, "y": 248},
    {"x": 425, "y": 207},
    {"x": 402, "y": 155},
    {"x": 369, "y": 196},
    {"x": 350, "y": 148},
    {"x": 539, "y": 222},
    {"x": 594, "y": 235},
    {"x": 582, "y": 185},
    {"x": 633, "y": 200}
  ],
  "links": [
    {"source":  0, "target":  1},
    {"source":  1, "target":  2},
    {"source":  2, "target":  0},
    {"source":  1, "target":  3},
    {"source":  3, "target":  2},
    {"source":  3, "target":  4},
    {"source":  4, "target":  5},
    {"source":  5, "target":  6},
    {"source":  5, "target":  7},
    {"source":  6, "target":  7},
    {"source":  6, "target":  8},
    {"source":  7, "target":  8},
    {"source":  9, "target":  4},
    {"source":  9, "target": 11},
    {"source":  9, "target": 10},
    {"source": 10, "target": 11},
    {"source": 11, "target": 12},
    {"source": 12, "target": 10}
  ]
}

我知道我可以使用 d3.json 打开一个 json 文件,但我不确定如何从 JSON-LD 转换为上述格式。有什么解决办法吗?谢谢。

编辑:

我的猫头鹰本体可在此处的 pastebin 上找到(json-ld 格式):http://pastebin.com/g7ggDyFX。如果您在上下文中达到高峰,您可以看到我正在通过“isSubClass”属性对本体建模。

我知道要在 D3 中读取 json 文件,我可以使用 d3.json。我不确定如何准确转换为上述格式。

我发现这个 github 存储库 https://github.com/uf6/ottograf 说它应该能够转换为格式,但是我在运行它时遇到了很多麻烦,我也想将它保存在 javascript 中。任何提示/解决方案?谢谢!

【问题讨论】:

  • 您能分享一份 JSON-LD 格式的数据样本吗?
  • 您的 JSON-LD 数据子集:{ "@id": "schema:Person", "@type": "owl:Class", "subClassOf": { "@id": "gr:BusinessEntity" } } 是否可以转换为两个节点 schema:Persongr:BusinessEntity 之间的一条边?

标签: javascript json node.js d3.js


【解决方案1】:

假设你想要做的是转换这个:

{
  "@id": "schema:Person"
  , "@type": "owl:Class"
  , "subClassOf": { 
    "@id": "gr:BusinessEntity"
  }
}

进入

{
  "nodes": [
    { "@id": "schema:Person", "@type": "owl:Class"}
    { "@id": "gr:BusinessEntity"}
  ]
  , "links": [
    {"source":  0, "target":  1}
  ]
}

你需要做的是:

加载 json 数据后,通过您将编写的转换函数对其进行处理:

  1. 循环遍历@graph 数组

在这个循环中,记录每个节点,也记录每个链接。对于节点,请确保使用对象以便获得唯一节点。

  1. 循环遍历节点对象,并基于它创建您的“节点”数组

  2. 通过查找节点数组中每个节点的索引来创建“链接”数组...

你可能想看看 d3.js 邮件列表的档案,有些人已经问过和你类似的问题了。

【讨论】:

    猜你喜欢
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 2012-06-20
    • 2023-03-20
    相关资源
    最近更新 更多