简体   繁体   English

使D3堆叠条形图填充父SVG容器

[英]Make D3 Stacked Bar Chart fill parent SVG container

I have a D3 stacked bar chart component and I am trying to make it fill the available space in the parent SVG. 我有一个D3堆叠的条形图组件,我正在尝试使其填充父SVG中的可用空间。 This will allow me to resize the chart with HTML rather than having to manually input sizes. 这将使我能够使用HTML来调整图表的大小,而不必手动输入大小。

Currently my sizes are hard coded and I have no idea how to change this to a more responsive format. 目前,我的尺寸是硬编码的,我不知道如何将其更改为更具响应性的格式。

Here is the relevant component code slice: 这是相关的组件代码片段:

let data = this.get('data')
    let div = select('body')
      .append("div")
      .attr("class", "stack-tooltip")
    let series = stack()
      .keys(["count1", "count2", "count3"])
      .offset(stackOffsetDiverging)
      (data);

    let svg = select(this.$('svg')[0]),
      margin = {
        top: 20,
        right: 30,
        bottom: 30,
        left: 60
      },
      width = +svg.attr("width"),
      height = +svg.attr("height");

    let x = scaleBand()
      .domain(data.map(function(d) {
        return d.label;
      }))
      .rangeRound([margin.left, width - margin.right])
      .padding(0.1);

    let y = scaleLinear()
      .domain([min(series, stackMin), max(series, stackMax)])
      .rangeRound([height - margin.bottom, margin.top]);

    let z = scaleOrdinal().range(['#DAEAF1', '#99CFE0', '#72BCD4']);

    svg.append("g")
      .selectAll("g")
      .data(series)
      .enter().append("g")
      .attr("fill", function(d) {
        return z(d.key);
      })
      .selectAll("rect")
      .data(function(d) {
        return d;
      })
      .enter().append("rect")
      .attr("width", x.bandwidth)
      .attr("x", function(d) {
        return x(d.data.label);
      })
      .attr("y", function(d) {
        return y(d[1]);
      })
      .attr("height", function(d) {
        return y(d[0]) - y(d[1]);
      })
      .attr('opacity', d => {
        let selected = this.get('selectedLabel');
        return (selected && d.data.label !== selected) ? '0.5' : '1.0';
      })

  function stackMin(h) {
      return min(h, function(d) {
        return d[0];
      });
    }

    function stackMax(h) {
      return max(h, function(d) {
        return d[1];
      });
    }

I did try to change the rangeRound values to a percentage on both the 'X' and 'Y' functions but I was unable to render the bars of the chart when trying to change the values in the X and Y attributes to template literals. 我确实尝试将'X'和'Y'函数上的rangeRound值都更改为百分比,但是当尝试将X和Y属性中的值更改为模板文字时,我无法呈现图表的条形图。

The SVG in the component template is created with a simple <svg width='800' height="300"></svg> 使用简单的<svg width='800' height="300"></svg>创建组件模板中的<svg width='800' height="300"></svg>

Any help is greatly appreciated 任何帮助是极大的赞赏

If you want it to scale to its parent container, you'll need to give the SVG a viewBox , instead of a width and height . 如果要缩放到其父容器,则需要给SVG一个viewBox ,而不是widthheight

The viewBox tells the browser which area of the SVG coordinate space, the contents occupy. viewBox告诉浏览器,内容占据SVG坐标空间的哪个区域。 That way it knows how to scale the contents. 这样,它便知道如何缩放内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM