問(wèn)題描述
我正在嘗試創(chuàng)建 D3 SVG 符號(hào),符號(hào)形狀根據(jù)數(shù)據(jù)中的類別屬性動(dòng)態(tài)設(shè)置.這將是 PowerBI 中自定義視覺(jué)對(duì)象的一部分,因此我使用的是 Typings 庫(kù).
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.
從我在網(wǎng)上看到的例子來(lái)看,下面的代碼應(yīng)該可以工作.
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; });
但我得到錯(cuò)誤 Property 'typeSymbol' does not exist on type '{}'
for code .type(function(d){ return d.typeSymbol})代碼>.
d
似乎在 type()
中不可用,因?yàn)樗?d3.svg.symbol()
代碼中使用.如果我用square"之類的字符串文字替換匿名函數(shù),它就可以工作.
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.
如何解決這個(gè)問(wèn)題?
推薦答案
Typescript 對(duì)您的數(shù)據(jù)對(duì)象類型一無(wú)所知.您可以定義數(shù)據(jù)對(duì)象類型,也可以嘗試使用任何類型:
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;})
這篇關(guān)于“類型 {} 上不存在屬性"使用帶有 D3 SVG 符號(hào)的匿名函數(shù)時(shí)出錯(cuò)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!