Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修改此D3.js以接受不同的JSON结构?_Json_D3.js - Fatal编程技术网

如何修改此D3.js以接受不同的JSON结构?

如何修改此D3.js以接受不同的JSON结构?,json,d3.js,Json,D3.js,我想使用这个D3.js: 目前,它只能使用类似于“flare label.json”(可在repo中找到)的结构 但我想使用这样的结构: [ { "ID": "10414713", "Case Number": "HZ152928", "Date": "02/16/2016 11:02:00 PM", "Block": "130XX S ELLIS AVE", "IUCR": "041A", "Primary Type": "BATTERY

我想使用这个D3.js:

目前,它只能使用类似于“flare label.json”(可在repo中找到)的结构

但我想使用这样的结构:

[
    {
    "ID": "10414713",
    "Case Number": "HZ152928",
    "Date": "02/16/2016 11:02:00 PM",
    "Block": "130XX S ELLIS AVE",
    "IUCR": "041A",
    "Primary Type": "BATTERY",
    "Description": "AGGRAVATED: HANDGUN",
    "Location Description": "CHA PARKING LOT/GROUNDS",
    "Arrest": "false",
    "Domestic": "false",
    "Beat": "0533",
    "District": "005",
    "Ward": "9",
    "Community Area": "54",
    "FBI Code": "04B",
    "X Coordinate": "",
    "Y Coordinate": "",
    "Year": "2016",
    "Updated On": "02/19/2016 03:45:53 PM",
    "Latitude": "",
    "Longitude": "",
    "Location": ""
    },
    {
    "ID": "10414628",
    "Case Number": "HZ152506",
    "Date": "02/13/2016 11:45:00 PM",
    "Block": "017XX W DIVISION ST",
    "IUCR": "0890",
    "Primary Type": "THEFT",
    "Description": "FROM BUILDING",
    "Location Description": "TAVERN/LIQUOR STORE",
    "Arrest": "false",
    "Domestic": "false",
    "Beat": "1213",
    "District": "012",
    "Ward": "1",
    "Community Area": "24",
    "FBI Code": "06",
    "X Coordinate": "1164361",
    "Y Coordinate": "1908039",
    "Year": "2016",
    "Updated On": "02/20/2016 03:53:39 PM",
    "Latitude": "41.903278103",
    "Longitude": "-87.671706965",
    "Location": "(41.903278103, -87.671706965)"
    }
]
我想要内环上的“主要类型”和外环上的“描述”以及外环上的“位置描述”


这可能吗?

我不会碰d3插件。相反,我会将数据转换为相似的

假设您提供的数据位于名为
data
的变量中,您可以编写:

var input = {
  "name": "flare",
  "description": "flare",
  "children": []
}

for (var i = 0; i < data.length; i++) {
  var incident = data[i];
  var primary = incident["Primary Type"];
  var description = incident["Description"];
  var locDesc = incident["Location Description"];
  var primaryIndex = -1;
  for (var j = 0; j < input.children.length; j++) {
    if (input.children[j].name == primary) {
      primaryIndex = j;
      break;
    }
  }
  if (primaryIndex == -1) {
    input.children.push({
      "name": primary,
      "description": primary,
      "children": []
    })
    primaryIndex = input.children.length - 1;
  }
  var node = input.children[primaryIndex];
  var descriptionIndex = -1;
  for (var j = 0; j < node.children.length; j++) {
    if (node.children[j].name == description) {
      descriptionIndex = j;
      break;
    }
  }
  if (descriptionIndex == -1) {
    node.children.push({
      "name": description,
      "description": description,
      "children": []
    })
    descriptionIndex = node.children.length - 1;
  }
  var node = node.children[descriptionIndex];
  var locDescIndex = -1;
  for (var j = 0; j < node.children.length; j++) {
    if (node.children[j].name == locDesc) {
      locDescIndex = j;
      break;
    }
  }
  if (locDescIndex == -1) {
    node.children.push({
      "name": locDesc,
      "description": locDesc,
      "size": 0
    })
    locDescIndex = node.children.length - 1;
  }
  node.children[locDescIndex].size++;
}
var输入={
“名称”:“flare”,
“说明”:“照明弹”,
“儿童”:[]
}
对于(变量i=0;i

现在d3需要的输入是
input

我不会碰d3插件。相反,我会将数据转换为相似的

假设您提供的数据位于名为
data
的变量中,您可以编写:

var input = {
  "name": "flare",
  "description": "flare",
  "children": []
}

for (var i = 0; i < data.length; i++) {
  var incident = data[i];
  var primary = incident["Primary Type"];
  var description = incident["Description"];
  var locDesc = incident["Location Description"];
  var primaryIndex = -1;
  for (var j = 0; j < input.children.length; j++) {
    if (input.children[j].name == primary) {
      primaryIndex = j;
      break;
    }
  }
  if (primaryIndex == -1) {
    input.children.push({
      "name": primary,
      "description": primary,
      "children": []
    })
    primaryIndex = input.children.length - 1;
  }
  var node = input.children[primaryIndex];
  var descriptionIndex = -1;
  for (var j = 0; j < node.children.length; j++) {
    if (node.children[j].name == description) {
      descriptionIndex = j;
      break;
    }
  }
  if (descriptionIndex == -1) {
    node.children.push({
      "name": description,
      "description": description,
      "children": []
    })
    descriptionIndex = node.children.length - 1;
  }
  var node = node.children[descriptionIndex];
  var locDescIndex = -1;
  for (var j = 0; j < node.children.length; j++) {
    if (node.children[j].name == locDesc) {
      locDescIndex = j;
      break;
    }
  }
  if (locDescIndex == -1) {
    node.children.push({
      "name": locDesc,
      "description": locDesc,
      "size": 0
    })
    locDescIndex = node.children.length - 1;
  }
  node.children[locDescIndex].size++;
}
var输入={
“名称”:“flare”,
“说明”:“照明弹”,
“儿童”:[]
}
对于(变量i=0;i
现在,d3所需的输入位于
input