简体   繁体   English

加载时动画 D3 热图单元格?

[英]Animate D3 Heatmap cells on load?

I want to build a heatmap on a static HTML page using D3:我想使用 D3 在静态 HTML 页面上构建热图:

https://codepen.io/ChrisBean/pen/ZEYGYzL https://codepen.io/ChrisBean/pen/ZEYGYzL

Once the page / the heatmap is loaded, I want the individual cells of the map to fade in one after another with some easing and about 200ms of delay between the fade-in of a cell.加载页面/热图后,我希望地图的各个单元格通过一些缓和和单元格淡入之间的大约 200 毫秒的延迟一个接一个地淡入。

While I have understood the basic.transition() and .duration() feature that D3 offers:虽然我已经了解了 D3 提供的 basic.transition() 和 .duration() 功能:

  d3.select("#my_dataviz")
          .transition()
          .duration(1000)
          .style(???);

I'm struggling to trigger a fade-in style animation on load respectively targeting a cell.我正在努力在加载时分别针对一个单元格触发淡入风格的动画。 Would anyone be able to push me in the right direction?有人能把我推向正确的方向吗?

You can use a transition on the rect's opacity and set the delay for the transition using a increment of 200ms.您可以在矩形的不透明度上使用过渡,并使用 200 毫秒的增量设置过渡的延迟。

First, create the rects with opacity = 0:首先,创建不透明度 = 0 的矩形:

let squares = svg.selectAll()
    .data(data, (d) => `${d.group}:${d.variable}`)
    .enter()
    .append('rect')
    .attr('x', (d) => x(d.group))
    .attr('y', (d) => y(d.variable))
    .attr('width', x.bandwidth())
    .attr('height', y.bandwidth())
    .style('fill', (d) => myColor(d.value))
    .style("opacity", 0)

Then transition rects' opacity to 1, with each rect's delay incremented by 200ms:然后将矩形的不透明度转换为 1,每个矩形的延迟增加 200 毫秒:

 squares.transition()
    .duration(3000)
    .delay((d,i) => i*200)
    .style("opacity", 1)

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

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