问题描述
我正在尝试创建 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;})