类型参数不能分配给字符串 [英] Argument of type is not assignable to string

查看:158
本文介绍了类型参数不能分配给字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a formatted json data that I want to use in d3 to draw a hierarchy. It was working with old data but after adding more dimension in the json data, its giving following error.

类型'{儿童:{组:数字;名称:字符串; } [];组:数字; } []'不能分配给'readonly string []'类型的参数. 输入'{name:string;儿童:{组:数字;名称:字符串; } [];组:数字; }"不能分配给字符串"类型.

Argument of type '{ name: string; children: { group: number; name: string; }[]; group: number; }[]' is not assignable to parameter of type 'readonly string[]'. Type '{ name: string; children: { group: number; name: string; }[]; group: number; }' is not assignable to type 'string'.

下面是我重新格式化的数据的输出,它是通过使用@Dacre Denny的代码生成的,该代码来自

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

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

My existing code snippet for d3 is following:

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():序数标度需要具有 plain 数组作为域...更为重要的是,一个数组,即基元,例如字符串(如果您传递数字,它们将仍然被字符串化...).这就解释了您的错误:类型参数无法分配给 string " .

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, i.e., primitives, like strings (if you pass numbers they will be stringified anyway...). That explains your error: "Argument of type is not assignable to string".

话虽如此,您可以使用root.descendants()获取所有子项,并使用map获得其所需的属性.在您的情况下,我将假定所需的字符串是子代的name属性.

That being said, you can use root.descendants() to get all the children and map to get their desired properties. In your case, I'll assume that the string you want is the children's name property.

在此演示中,myNames是要传递给有序刻度的数组(如果您不希望根名称,请将其删除):

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

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>

这篇关于类型参数不能分配给字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆