[英]Error of Property 'Value' does not exist on type 'number' in d3 Pie-chart in v4 using Typescript
[英]Typings Error when using D3 V4 on Angular 2 and Typescript V2
我正在嘗試使用Typescript集成D3和角度2。 所有類型都沒有產生錯誤,但是有一種情況會在我的代碼中的每個實例中產生錯誤。
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
import * as Moment from 'moment';
@Component({
selector: 'app-select-d3',
templateUrl: './select-d3.component.html',
styleUrls: ['./select-d3.component.css']
})
export class SelectD3Component implements OnInit {
constructor() { }
ngOnInit() {
var data = [];
data[0] = [];
data[1] = [];
data[2] = [];
data[3] = [];
data[0][0] = [1, 2, 3];
data[0][1] = [4, 5, 6];
data[1][0] = [7, 8];
data[1][1] = [9, 10, 11, 12];
data[1][2] = [13, 14, 15];
data[2][0] = [16];
data[3][0] = [17, 18];
var width = 1000;
var height = 240;
var barWidth = 100;
var barGap = 10;
var margin = { left: 50, right: 50, top: 0, bottom: 0 };
var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
var chartGroup = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var firstGroups = chartGroup.selectAll("g")
.data(data)
.enter().append("g")
.attr("class", function (d, i) { return "firstLevelGroup" + i; })
.attr("transform", function (d, i) { return "translate(" + (i * (barWidth + barGap)) + ",0)"; })
//console.log(firstGroups);
var secondGroups = firstGroups.selectAll("g")
.data(function (d) { return d; })
.enter().append("g")
.attr("class", function (d, i, j) { return "secondLevelGroup" + i; })
.attr("transform", function (d, i, j) { return "translate(0," + (height - ((i + 1) * 50)) + ")"; });
//console.log(secondGroups);
secondGroups.append("rect")
.attr("x", function (d, i) { return 0; })
.attr("y", "0")
.attr("width", 100)
.attr("height", 50)
.attr("class", "secondLevelRect");
secondGroups.selectAll("circle")
.data(function (d) { return d; })
.enter().append("circle")
.filter(function (d) { return d > 10; })
.attr("cx", function (d, i) { return ((i * 21) + 10); })
.attr("cy", "25")
.attr("r", "10")
secondGroups.selectAll("text")
.data(function (d) { return d; })
.enter()
.append("text")
.attr("x", function (d, i) { return ((i * 21) + 10); })
.attr("y", "25")
.attr("class", "txt")
.attr("text-anchor", "middle")
.attr("dominant-baseline", "middle")
.text(function (d, i, nodes) { return d; });
}
}
正如您所看到的,每次使用:.data(function(d){return d;})時,函數都以紅色下划線。
產生的錯誤如下:
[ts] Argument of type '(this: BaseType, d: {}) => {}' is not assignable to parameter of type '(this: BaseType, datum: {}, index: number, groups: BaseType[] | ArrayLike<BaseType>) => {}[]'.
Type '{}' is not assignable to type '{}[]'.
我已經嘗試使用所有導出的d3模塊在我的根src文件夾中更新我的global typings.d.ts,如此解決方案中所述: 這里
我有一個tsconfig.json如下:
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es6",
"dom"
],
"mapRoot": "./",
"module": "es6",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
]
},
"exclude": ["../node_modules","src"]
}
我為我的組件創建了一個配置文件,如下所示:
export interface AreaChartConfig {
function: any;
}
我不確定現有的類型是否相互沖突,或者各個d3模塊是否存在正確的定義。
我的問題如下:如果它們不存在,那么我應該更新哪些@ types / d3模塊? 我應該如何更新它?
為什么我的組件界面不起作用?
如果它們是沖突的,那么tsc編譯器會導致沖突的類型?
我是打字稿的新手,雖然我理解打字的概念,但我很難正確解決這個問題。
任何幫助,將不勝感激!
我曾經也有過一樣的問題。 我做的是這樣的:
const myFunc: any = function (d) { return d; };
...
.data(_this.myFunc)
....
和堆積條形圖工作;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.