“类型 {} 上不存在属性"使用带有 D3 SVG 符号的匿名函数时出错 [英] "Property does not exist on type {}" error when using anonymous function with D3 SVG Symbol

查看:34
本文介绍了“类型 {} 上不存在属性"使用带有 D3 SVG 符号的匿名函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建 D3 SVG 符号,符号形状根据数据中的类别属性动态设置.这将是 PowerBI 中自定义视觉对象的一部分,因此我使用的是 Typings 库.

I'm trying to create D3 SVG Symbols, with the symbol shape set dynamically based on a category property in the data. This will be part of a custom visual in PowerBI, so I'm using Typings library.

从我在网上看到的例子来看,下面的代码应该可以工作.

From the examples I've seen online, the below code should work.

var milestoneSymbols = this.milestoneContainer.selectAll('.path').data(viewModel.milestones);
milestoneSymbols.enter().append('path');
milestoneSymbols.attr('d', d3.svg.symbol() 
                            .size(25) 
                            .type( function(d){ return d.typeSymbol})
    );
milestoneSymbols.attr("transform", function(d,i) {
        return "translate("+ xScale(d.date) + "," 
            + ((i % heightGroups) * (milestoneContainerHeight/heightGroups) + margins.top )
            + ")";
        });
milestoneSymbols.style("stroke", "function(d) {  return findTaskTypeShape(d.label).color; }
                .style("stroke-width", 1)
                .style("fill", function(d) {  return findTaskTypeShape(d.label).color; });

但我得到错误 Property 'typeSymbol' does not exist on type '{}' for code .type(function(d){ return d.typeSymbol}).d 似乎在 type() 中不可用,因为它正在 d3.svg.symbol() 代码中使用.如果我用square"之类的字符串文字替换匿名函数,它就可以工作.

But I get the error Property 'typeSymbol' does not exist on type '{}' for the code .type( function(d){ return d.typeSymbol}). It appears that d is not available inside type() because it's being used in the d3.svg.symbol() code. If I replace the anonymous function with a string literal like "square", it works.

如何解决这个问题?

推荐答案

Typescript 对您的数据对象类型一无所知.您可以定义数据对象类型,也可以尝试使用任何类型:

Typescript does not know anything about your data object type. You can define the data object type or you could try to use the type any:

milestoneSymbols.attr('d', d3.svg.symbol() 
                            .size(25) 
                            .type( function(d: any){ return d.typeSymbol;})

这篇关于“类型 {} 上不存在属性"使用带有 D3 SVG 符号的匿名函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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