如何使用d3图表创建堆积条形图?

问题描述 投票:0回答:1

我正在使用d3创建堆积条形图。我在前端有按钮。单击按钮图表时应显示。但图表未显示,并提供以下警告。

'd3'中未找到“出口'比例'(导入为'd3')”

'd3'中未找到“出口'比例'(导入为'd3')”

'd3'中未找到“导出'时间'(导入为'd3')”

import * as d3 from 'd3';

export class stackedbarchart {

    sbar(box_id){
      const data=[{"country":"India","sales":100,"profit":100}, 
                  {"country":"US","sales":70,"profit":100}, 
                  {"country":"USA","sales":65,"profit":100}];
    const svgWidth = 500, svgHeight = 200;
    const margin = { top: 10, right: 20, bottom: 70, left: 40 };
    const width = svgWidth - margin.left - margin.right;
    const height = svgHeight - margin.top - margin.bottom;
    const contentWidth = 400;
    const contentHeight = 120;
    const headers=["sales", "profit"]

    const svg = d3
      .select(box_id)
      .append('svg')
      .attr("width", svgWidth)
      .attr("height", svgHeight);

      const dataset = d3.layout.stack()(headers.map(function(values) 
           { 
        return data.map(function(d)
                          { return {x: d.country, y: +d[values]};
        });
      }));

      var x = d3.scaleOrdinal()   
                .domain(dataset[0].map(function(d) { return d.x; }))   
                .rangeRoundBands([10, width-10], 0.02);

      var y = d3.scaleLinear()   
                .domain([0, d3.max(dataset, function(d) {   
                 return d3.max(d, function(d) { return d.y0 + d.y; 
                 });  })])     
                .range([height, 0]);

     var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574"];

     var yAxis = d3.svg.axis()   
                   .scale(y)  
                   .orient("left")   

         var xAxis = d3.svg.axis()  
                       .scale(x)  
                       .orient("bottom")

         svg.append("g") 
            .attr("class", "y axis")   
            .call(yAxis);

        svg.append("g")  
           .attr("class", "x axis")  
           .attr("transform", "translate(0," + height + ")") 
           .call(xAxis);

        var groups = svg.selectAll("g.cost")  
                        .data(dataset)  
                        .enter()
                        .append("g") 
                        .attr("class", "cost")  
                        .style("fill", function(d, i)  
                                           { return colors[i]; });

        var rect = groups.selectAll("rect")   
                         .data(function(d)
                                 { return d; }) 
                         .enter()   
                         .append("rect")  
                         .attr("x", function(d)
                                      { return x(d.x); })   
                         .attr("y", function(d) 
                                      { return y(d.y0 + d.y); })   
                         .attr("height", function(d)
                                           { return y(d.y0) - y(d.y0 + d.y); })   
                          .attr("width", x.rangeBand())   
                          .on("mouseover", function()
                                            { tooltip.style("display", null); })  
                          .on("mouseout", function() 
                                            { tooltip.style("display", "none"); })  
                          .on("mousemove", function(d) {
       var xPosition = d3.mouse(this)[0] - 15;
       var yPosition = d3.mouse(this)[1] - 25;
           tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
           tooltip.select("text").text(d.y);   });

      var legend = svg.selectAll(".legend")  
                      .data(colors)  
                      .enter().append("g")   
                      .attr("class", "legend") 
                      .attr("transform", function(d, i)
                                           { return "translate(30," + i * 19 + ")"; });  
                legend.append("rect") 
                      .attr("x", width - 18)   
                      .attr("width", 18)  
                      .attr("height", 18)  
                      .style("fill", function(d, i)
                                       {return colors.slice().reverse()[i];});  
                legend.append("text") 
                      .attr("x", width + 5)  
                      .attr("y", 9) 
                      .attr("dy", ".35em")  
                      .style("text-anchor", "start")   
                      .text(function(d, i) { 
                              switch (i) {
                                 case 0: return "sales";
                                 case 1: return "profit";
                               }   });

       var tooltip = svg.append("g") 
                        .attr("class", "tooltip") 
                        .style("display", "none");
                     tooltip.append("rect") 
                            .attr("width", 30)  
                            .attr("height", 20) 
                            .attr("fill", "white")   
                            .style("opacity", 0.5);

                     tooltip.append("text") 
                            .attr("x", 15) 
                            .attr("dy", "1.2em") 
                            .style("text-anchor", "middle")   
                            .attr("font-size", "12px")  
                            .attr("font-weight", "bold");
    }
}

任何帮助或暗示正确的方向将非常感激。

angular typescript d3.js
1个回答
0
投票

您似乎已将d3.js V5.x安装到开发环境中,而您正在使用D3.js V3语法/ api编写代码。

例如,

d3.layout.stack()now d3.stack()

d3.svg.axis().scale(x).orient('bottom')现在是d3.axisBottom(x)

d3.svg.axis().scale(y).orient('left')现在是d3.axisLeft(y);

你应该在版本之间迁移时查看最新的documentation以及changelog

© www.soinside.com 2019 - 2024. All rights reserved.