简体   繁体   English

D3js增加X轴之间的空间

[英]D3js increase space between X axis

I'm trying to increase the space between the labels in the X axis and also the X subgroups I'm using the example provided by D3 documentation here我正在尝试增加 X 轴上的标签之间的空间以及我使用的 X 子组之间的空间, 这里使用的是 D3 文档提供的示例

I've been trying to edit this part of the code without look.我一直在尝试不看就编辑这部分代码。 The .padding([]) only makes the bar more slim .padding([])只会让栏更苗条

  // Add X axis
  var x = d3.scaleBand()
      .domain(groups)
      .range([0, width])
      .padding([0.2])
  svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x).tickSize(0));

  //subgroups
  var xSubgroup = d3.scaleBand()
    .domain(subgroups)
    .range([0, x.bandwidth()])
    .padding([0.05])

the idea is to have something like this (this was with excel) image这个想法是有这样的东西(这是用excel)图像

You will be able to increase the space between bars without slimming them if you increase the width of the graph .如果您增加图表的宽度,您将能够增加条形之间的空间而不会缩小它们。 without it there is no extra space for moving them so the bars will get slim by default.没有它,就没有额外的空间来移动它们,所以默认情况下这些条会变细。

In the example you mentioned on the top you have width and height options, increase the width and then padding.在您在顶部提到的示例中,您有宽度和高度选项,增加宽度然后填充。 Eg-例如-

  width = 800 - margin.left - margin.right,
  height = 300 - margin.top - margin.bottom;


  ....

  // Add X axis
  var x = d3.scaleBand()
  .domain(groups)
  .range([0, width])
  .padding([0.2])
svg.append("g")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x).tickSize(0));

  //subgroups
  var xSubgroup = d3.scaleBand()
    .domain(subgroups)
    .range([0, x.bandwidth()])
    .padding([0.2])

Working example - https://codepen.io/puneet2412/pen/GRpQKzQ工作示例 - https://codepen.io/puneet2412/pen/GRpQKzQ

You can then try to change padding as per your requirement.然后,您可以尝试根据您的要求更改填充。

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

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