javascript - D3js 响应式堆叠条形图 - 其他主题解决方案不起作用

标签 javascript d3.js data-visualization

我对 D3 和 javascript 相当陌生。不过,非常有用的图书馆。但我无法使我的堆积条形图(我从 D3js.org 网站获取代码)做出响应。实际上,当我从头开始时,我在使各种 D3 图表做出响应时遇到了问题。

我尝试使用viewbox属性和preserveAspectRatio,但我可能做错了。

这是我的完整代码:http://codepen.io/voltdatalab/pen/avMoMx

var svg = d3.select("graph").append("svg")
    .attr("width", "100%")
    .attr('preserveAspectRatio','xMinYMid')
    .attr('viewBox','0 100% '+Math.min(width,height)+' '+Math.min(width,height))
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

有人可以帮我解决这个问题吗?

最佳答案

您需要先设置viewbox属性。试试这个:

var svg = d3.select("#chart").append("svg")
    .attr("viewBox", "0 0 " + (width) + " " + (height))
    .attr("preserveAspectRatio", "xMinYMin");

为此,您必须更改 html:

<div id="chart"></div>

关于javascript - D3js 响应式堆叠条形图 - 其他主题解决方案不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802292/

相关文章:

javascript - 上传大文件时 Recaptcha v2 验证过期

javascript - 单击时用颜色从中心填充圆形 Div

javascript - 使用现有对象作为剪切路径的简单方法?

javascript - 即使在 React 中使用 .exit().remove(),D3 也会继续重新渲染

javascript - Google数据可视化使用两个数据集堆叠条形图

python - ValueError形状不匹配: objects cannot be broadcast to a single shape

javascript - Jasmine karma 单元测试 "Error: Unexpected request: GET/Content/json/3420_layer0.json"

javascript - 如何创建使用前缀作为参数的自定义方法

javascript - d3.js 动态附加具有可变力边界的群

python - 访问全息 View 中的 Bokeh (图形)参数