問題描述
我正在嘗試創建 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 符號的匿名函數時出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!