angular - 如何使用 D3js 和 Angular 7 构建一个简单的 donut chart

标签 angular d3.js angular7 donut-chart

我正在使用 D3.js v5 在 Angular 中创建一个 donut chart 。所有其他简单的图形,如条形图、圆、线等都可以正常工作,但是“pie()”函数会出现一些错误。谁能建议我如何在 Angular 中正确使用 pie() 函数?

我在下面提到的场景中遇到错误。

  • 在设置色域时,它不接受数据,它说,它需要ReadOnlyArray<String> ,所以尝试给出硬编码域 ["a","b","c","d","e"]
  • pie() --> 不接受值,不知道为什么,在这里尝试了很多。
  • this.color(d.data.key)) --> this.color不接受访问数据和 key 。

  • 需要解决上述所有问题。

    代码如下:
    export class DonutChartComponent implements OnInit {
    
        width = 450
        height = 450
        margin = 40
        radius;
        svg;
        color;
        pie;
        data_ready;
    
        // Create dummy data
        public data = {a: 9, b: 20, c:30, d:8, e:12}
    
    
        constructor() { }
    
        ngOnInit() {
    
        this.draw();
    
        }
    
    
         draw(){
    
         this.radius = Math.min(this.width, this.height) / 2 - this.margin
    
    
         this.svg = d3.select("app-donut-chart")
                   .append("svg")
                   .attr("width", this.width)
                   .attr("height", this.height)
                   .append("g")
                   .attr("transform", "translate(" + this.width / 2 + "," + 
                    this.height / 2 + ")");
    
    
    
         // set the color scale
         this.color = d3.scaleOrdinal()
                .domain(this.data)             // this.data gives error here
               .range(d3.schemeDark2);
    
         console.log(d3.scaleOrdinal());
    
         // Compute the position of each group on the pie:
         this.pie = d3.pie()
                   .value(function(d) {return  d.values});     //error here
         this.data_ready = this.pie(d3.entries(this.data))
    
          this.svg
         .selectAll('whatever')
         .data(this.data_ready)
         .enter()
         .append('path')
         .attr('d', d3.arc()
         .innerRadius(100)         // This is the size of the donut hole
         .outerRadius(this.radius))
         .attr('fill', function(d){ return(this.color(d.data.key)) })  
                                                               //error here
         .attr("stroke", "black")
         .style("stroke-width", "2px")
         .style("opacity", 0.7)
        }
    }
    

    我希望如果代码运行正常,我可以看到基于给定数据的圆环图。

    最佳答案

    一些错误的地方:

  • 您的色标.domain期待一个字符串列表。修改为:
    .domain(Object.keys(this.data))
  • 您的饼图生成器应定义为:
    this.pie = d3.pie() .value(function (d) { return d.value })

  • 现在它works .

    关于angular - 如何使用 D3js 和 Angular 7 构建一个简单的 donut chart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692979/

    相关文章:

    javascript - 如果我有 keydown,如何停止调用 d3.brush? d3/js

    angular - 单击 Angular 6 中的按钮时调用不同的函数

    javascript - 将 Angular 单页应用程序导出到静态 HTML/CSS/JS

    javascript - d3 : Nodes + Links to make a multi-generation family tree; how to parse data to draw lines?

    angular - 在自定义验证器中访问表单组

    angular 7.0.5 ng new 不问你想添加 Angular 路由吗?....等

    css - 如何将字体文件从 css 文件加载到 Angular 元素中

    angular - 为什么在 for 循环中未定义使用 ngModel 的模板引用变量?

    angular - Ionic 2 - 如何处理后退按钮

    json - 使用来自 API 的 JSON 数据的 D3 线图