I am trying to use D3 v4 with Angular 2 (Typescript). I am currently looking into D3 v4. I was able to follow some of the answers here in stackoverflow with similar issues with no success. I have imported most of the D3 libraries and its typings ( I am using TS 2.0) and in my component file I require import * as d3 from 'd3';
. This issue to me might be Angular 2, Typescript and 3rd party libraries... so far a mess.
In my component file I have some code like this:
let arc = d3.arc()
.outerRadius(chartHeight / 2)
.innerRadius(chartHeight / 4)
.padAngle(0.03)
.cornerRadius(8);
let pieG = chartLayer.selectAll("g")
.data([data])
.enter()
.append("g")
.attr("transform", "translate(" + [chartWidth / 2, chartHeight / 2] + ")");
let block = pieG.selectAll(".arc")
.data(arcs);
let newBlock = block.enter().append("g").classed("arc", true);
newBlock.append("path")
.attr("d", arc)
.attr("id", function (d, i) {
return "arc-" + i
})
.attr("stroke", "gray")
.attr("fill", function (d, i) {
return d3.interpolateCool(Math.random())
});
As you can see I have defined arc on the first line and using it in line 19 but I get an error:
[at-loader] src\piechart\piechart.component.ts:19:28
Argument of type 'Arc<any, DefaultArcObject>' is not assignable to parameter of type '(this: BaseType, datum: PieArcDatum<number | { valueOf(): number; }>, index: num
ber, groups: Base...'.
Types of parameters 'd' and 'datum' are incompatible.
Type 'PieArcDatum<number | { valueOf(): number; }>' is not assignable to type 'DefaultArcObject'.
Property 'innerRadius' is missing in type 'PieArcDatum<number | { valueOf(): number; }>'.
The Arch and arc seem to be defined in the d3-shape types and also in the d3-path types.
Anyone that can help me... I have spent days trying to do a POC with angular 2, TS and D3 v4 and so far no luck... I have seen all the articles online about it and most of them have older version or not working. It seems too me that this is a typing issue. Angular 2 and third party libraries are a nightmare.
.attr("d", arc)
to.attr("d", function(d) { console.log(d); arc(d); }
update your question with the output of thatconsole.log
...newBlock.append("path") .attr("d", (d) => { console.log(d); arc(d); }) .attr("id", (d, i) => { return "arc-" + i })
I get two ts errors ... and the 4 logs like below:Log: {"data":{"name":"hoge","value":100},"index":0,"value":100,"startAngle":0,"endAngle":0.7075659129706742,"padAngle":0}