【问题标题】:Argument of type is not assignable to string类型参数不可分配给字符串
【发布时间】:2019-12-23 03:40:46
【问题描述】:

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

类型参数 '{ name: string;孩子:{组:号码;名称:字符串; }[];组:号码; }[]' 不可分配给“只读字符串 []”类型的参数。 键入'{名称:字符串;孩子:{组:号码;名称:字符串; }[];组:号码; }' 不可分配给类型 'string'。

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

{
  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 代码 sn-p 如下:

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);

非常感谢您的帮助。

【问题讨论】:

    标签: javascript json string object d3.js


    【解决方案1】:

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

    话虽如此,您可以使用root.descendants() 获取所有孩子,使用map 获取他们想要的属性。在您的情况下,我假设您想要的字符串是孩子的 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>

    【讨论】:

    • 非常感谢您的帮助。我认为这个嵌套数据是问题,但不确定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2021-10-21
    • 2021-10-13
    • 1970-01-01
    • 2021-09-04
    相关资源
    最近更新 更多