javascript - d3 日历 View : how to put all into one svg instead of several svgs

标签 javascript svg d3.js calendar grouping

我正在尝试为 D3 Calendar View

但问题是原始代码是为每个月创建一个 svg。然后导出仅导出第一个 svg。我怎样才能让它成为一个大的 svg,所有月份都在里面?

有问题的代码是这样的:

var svg = d3.select("body").selectAll("svg")
.data(d3.range(1990, 2011))
.enter().append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "RdYlGn")
.append("g")
.attr("transform", "translate(" + ((width - cellSize * 53) / 2) + "," + (height - cellSize * 7 - 1) + ")");

感谢任何想法。 gb5256

最佳答案

这是一个快速重构,将它放在一个 svg 中,并每年移动到它自己的 g 中。

尺寸变为:

var width = 960,
    yearHeight = 136, // height of each year
    height = yearHeight * 20, // height of entire svg
    cellSize = 17; // cell size

附加具有较大高度的初始 SVG:

var svg = d3.select("body").append("svg")
    .attr("width",width)
    .attr("height",height);

为每一年创建g并转换为位置:

var g = svg.selectAll(".yearG")
    .data(d3.range(1990, 2011))
    .enter().append("g")
    .attr("class","yearG")
    .attr("class", "RdYlGn")
    .attr("transform", function(d,i){
      return "translate(" + ((width - cellSize * 53) / 2) + "," + ((yearHeight - cellSize * 7 - 1) + (yearHeight * i)) + ")";
    });

将元素追加到那些 g 中:

g.append("text")
...    
var rect = g.selectAll(".day")
...
g.selectAll(".month")
...

这是一个 updated gist .

关于javascript - d3 日历 View : how to put all into one svg instead of several svgs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133487/

相关文章:

svg - ImageMagick将SVG转换为PNG不能在启用RSVG的情况下工作

javascript - D3 : Invalid attribute [object Object]

javascript - d3 缩放 : prevent page zoom when using a touchscreen

javascript - 以编程方式打开 Chrome 插件的 options.html 页面?

javascript - 测试用户点击 true 还是 false

Angular 5 动画 SVG viewBox

javascript - d3 : Data Distribution to Elements Based on Specific Field

javascript - 如何在没有 ctx.bezierCurveTo 的情况下使用原生 Javascript 代码绘制贝塞尔曲线?

javascript - 如何从 Javascript 中的按钮单击调用原始表单

javascript - 单节点图对 forceCenter 没有反应?