Compilation errors when drawing a piechart using d3.js and TypeScript

后端 未结 3 1065
暖寄归人
暖寄归人 2021-02-14 21:23

I am attempting to draw a pie chart using the d3.js library and TypeScript. I have the following code:

\"use strict\";
module Chart {
  export class chart {

            


        
3条回答
  •  后悔当初
    2021-02-14 21:57

    Came here because I had the same problem with D3 v5!

    Solution: Use the PieArcDatum interface (Very strange name in my opinion!!!)

    Details:

    import { PieArcDatum } from 'd3-shape';
    
    ... 
    
    type Population = { time: string, population: number; };
    
    ...
    
    const svg = element.append("g")
        .attr("transform", `translate(${300}, ${160})`)
    ;
    
    const pie = d3.pie()
        .sort(null)
        .value((record) => record.population);
    
    const path = d3.arc>()
        .innerRadius(0)
        .outerRadius(150)
    ;
    
    // Beim selectAll kommt eine leere Selection zurück da es noch keinen Circle gibt
    const data = pie(worldPopulation);
    const arch = svg.selectAll(".arc")
        .data(data)
        .enter()
        .append("g")
            .attr("class", "arc")
    ;
    
    arch.append('path')
        .attr("d", path)
    ;
    
    
    

    Sidenote: I'm using WebStorm, WS was not able to find (autoimport) PieArcDatum - I had to import it manually...

提交回复
热议问题