简体   繁体   English

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

[英]Argument of type is not assignable to string

I have a formatted json data that I want to use in d3 to draw a hierarchy.我有一个格式化的 json 数据,我想在 d3 中使用它来绘制层次结构。 It was working with old data but after adding more dimension in the json data, its giving following error.它正在处理旧数据,但在 json 数据中添加更多维度后,出现以下错误。

Argument of type '{ name: string; '{ name: string; 类型的参数children: { group: number;儿童:{组:编号; name: string;名称:字符串; }[]; }[]; group: number;组:数量; }[]' is not assignable to parameter of type 'readonly string[]'. }[]' 不能分配给类型为 'readonly string[]' 的参数。 Type '{ name: string;输入'{名称:字符串; children: { group: number;儿童:{组:编号; name: string;名称:字符串; }[]; }[]; group: number;组:数量; }' is not assignable to type 'string'. }' 不可分配给类型 'string'。

The output of my reformatted data is following which is generated by using the code from @Dacre Denny's answer from Change Json data into new format by JavaScript link我重新格式化的数据的输出如下,它是通过使用@Dacre Denny's answer from Change Json data into new format by JavaScript link 中的代码生成的

{
  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
}

My existing code snippet for d3 is following:我现有的 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);

I would really appreciate your help.我将衷心感谢您的帮助。

You cannot pass a nested array to ordinal.domain() : an ordinal scale needs to have a plain array as the domain... even more important, an array of values , ie, primitives, like strings (if you pass numbers they will be stringified anyway...).您不能将嵌套数组传递给ordinal.domain() :序数刻度需要有一个普通数组作为域……更重要的是,一个数组,即基元,如字符串(如果您传递数字,它们将无论如何都要字符串化......)。 That explains your error: "Argument of type is not assignable to string " .这解释了您的错误: “类型参数不可分配给字符串

That being said, you can use root.descendants() to get all the children and map to get their desired properties.话虽如此,您可以使用root.descendants()获取所有子项并map以获取他们想要的属性。 In your case, I'll assume that the string you want is the children's name property.在您的情况下,我假设您想要的字符串是孩子的name属性。

In this demo, myNames is the array you'll pass to the ordinal scale (if you don't want the root's name, remove it):在此演示中, 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' 日期类型的参数不能分配给字符串类型的参数 - Argument of type date is not assignable to parameter of type string “文件”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'File' is not assignable to parameter of type 'string' '[string]' 类型的参数不可分配给 'U' 类型的参数 - Argument of type '[string]' is not assignable to parameter of type 'U' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' “T”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'T' is not assignable to parameter of type 'string' “RegExp”类型的参数不能分配给“string”类型的参数 - Argument of type 'RegExp' is not assignable to parameter of type 'string' “字符串”类型的参数不可分配给“日期”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'Date' 角4-string []类型的参数不能分配给&#39;string&#39;类型的参数 - Angular 4 - argument of type string[] is not assignable to type parameter of type 'string' &#39;string | 类型的参数 number&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “数字”类型不能分配给“字符串”类型 - Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM