javascript - 类型的参数不可分配给字符串

标签 javascript json string object d3.js

我有一个格式化的 json 数据,我想在 d3 中使用它来绘制层次结构。它使用旧数据,但在 json 数据中添加更多维度后,出现以下错误。

'{ name: string; 类型的参数 children :{ 组:数字;名称:字符串; }[];组:数字; }[]' 不可分配给类型为“readonly string[]”的参数。 输入'{名称:字符串; children :{ 组:数字;名称:字符串; }[];组:数字; }' 不可分配给类型 'string'。

我的重新格式化数据的输出如下,它是使用@Dacre Denny 在 Change Json data into new format by JavaScript 的回答中的代码生成的链接

{
  name: "program",
  children: (1)[
    {
      name: "file1",
      children: (1)[
        {
          name: "function1",
          calls: (2)[
            {
              line: 105,
              file: "file2",
              function: "function5"
            },
            {
              line: 106,
              file: "file2",
              function: "function6"
            }
          ],
          point: (2)[
            102,
            105
          ],
          point2: (3)[
            (2)[
              102,
              102
            ],
            (3)[
              105,
              106,
              107
            ],
            (2)[
              106,
              107
            ]
          ],
          group: 1
        }
      ],
      group: 1
    }
  ],
  group: 0
}

我现有的 d3 代码片段如下:

const data = TestFunction.test(); //from here i am calling the reformatted output
const root = d3.hierarchy(data);

const colorScale = d3.scaleOrdinal()
        .domain(data.children) // here its showing the error.
        .range(d3.schemeCategory10);

非常感谢您的帮助。

最佳答案

您不能将嵌套数组传递给 ordinal.domain():序数标度需要有一个普通数组作为域……更重要的是,一个values 数组,即基元,如字符串(如果您传递数字,它们无论如何都会被字符串化...)。这解释了您的错误:“类型参数不可分配给 string

也就是说,您可以使用 root.descendants() 获取所有子项并使用 map 获取所需的属性。在你的例子中,我假设你想要的字符串是 child 的 name 属性。

在此演示中,myNames 是您将传递给序数比例的数组(如果您不想要根的名称,请将其删除):

const data = {
  "name": "program",
  "children": [{
    "name": "file1",
    "children": [{
      "name": "function1",
      "calls": [{
          "line": 105,
          "file": "file2",
          "function": "function5"
        },
        {
          "line": 106,
          "file": "file2",
          "function": "function6"
        }
      ],
      "lines1": [
        102,
        105
      ],
      "lines2": [
        [
          102,
          102
        ],
        [
          105,
          106,
          107
        ],
        [
          106,
          107
        ]
      ],
      "group": 1
    }],
    "group": 1
  }],
  "group": 0
};

const root = d3.hierarchy(data);

const myNames = root.descendants().map(d => d.data.name);

console.log(myNames)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

关于javascript - 类型的参数不可分配给字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449930/

相关文章:

将 const char* 转换为 LPCTSTR

javascript - 主组件比其子组件晚一步获取数据

javascript - Angular:将输入数字限制为两位小数的更好方法?

javascript - 如果用户单击后退按钮,我该怎么做才能防止出现咆哮消息

iphone - 帮助解析嵌套的 JSON 对象

android - Android基于字符串加载和播放声音

javascript - 显示/隐藏附加元素内的输入字段

java - Spring REST - RequestBody 内的数组仅包含一个元素

json - 在 Go 中正确解析 JSON

c - strtok - 向前移动一位