[D3] Animate with the General Update Pattern in D3 v4

In D3, the General Update Pattern is the name given to what happens when a data join is followed by operations on the enter, update, and exit selections. When a chart‘s data changes over time and each update can both create new elements and destroy existing ones, the General Update pattern can help convey meaning to users. This lesson demonstrates the pattern using animated transitions on a column chart.

function render(subject = ‘math‘) {

    // Define a resuable transation variable
    var t = d3.transition().duration(1000);

    var update = svg.selectAll(‘rect‘)
        .data(data.filter(d => d[subject]), d => d.name); //d => d.name is a uniq idientifier

    // First: we want to remove the existing object which doesn‘t have any value
        // Get rid of null value object
        // Also animation y and height attr to produce
        // fade out effect
    update
        .exit()
        .transition(t)
        .attr(‘y‘, height)
        .attr(‘height‘, 0)
        .remove();

    // Second, we want to animate the existing elements height
    update
        .transition(t)
        .delay(1000)
        .attr(‘y‘, d => yScale(d[subject]))
        .attr(‘height‘, d => height - yScale(d[subject]));

    // Last, for the new data which is not in the page before
        // We want to animate
    update
        .enter()
        .append(‘rect‘)
        .attr(‘y‘, height)
        .attr(‘height‘, 0)
        .attr(‘x‘, d => xScale(d.name))
        .attr(‘width‘, d => xScale.bandwidth())
        .transition(t)
        .delay(update.exit().size() ? 2000: 0) // If page refresh, we don‘t want to wait 2000ms
        .attr(‘y‘, d => yScale(d[subject]))
        .attr(‘height‘, d => height - yScale(d[subject]));
}

render();
时间: 2024-08-14 11:20:03

[D3] Animate with the General Update Pattern in D3 v4的相关文章

D3.js系列——动态效果和Update、Enter、Exit的理解

一.动态效果 D3 支持制作动态的图表.有时候,图表的变化需要缓慢的发生,以便于让用户看清楚变化的过程,也能给用户不小的友好感. 1.什么是动态效果 前面制作的图表是一蹴而就地出现,然后绘制完成后不再发生变化的,这是静态的图表. 动态的图表,是指图表在某一时间段会发生某种变化,可能是形状.颜色.位置等,而且用户是可以看到变化的过程的.例如,有一个圆,圆心为 (100, 100).现在我们希望圆的 x 坐标从 100 移到 300,并且移动过程在 2 秒的时间内发生.这种时候就需要用到动态效果,在

D3选择集处理的update,enter及exit模板

可以适应不同的数量. <!DOCTYPE html> <body> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <div id="enter"></div> <div id="exit"> <p><

[D3] SVG Graphics Containers and Text Elements in D3 v4

SVG is a great output format for data visualizations because of its scalability, but it comes with some idiosyncrasies and unique challenges. In this lesson we’ll learn how to use graphics containers, the SVG equivalent of a div, as well as text elem

D3中动画(transition函数)的使用

关于transition的几个基本点: 1. transition()是针对与每个DOM element的,每个DOM element的transition并不会影响其他DOM element的transition操作: 2. 对于每一个DOM element的transition每次只能执行一个,如果在一个DOM element上添加了许多transition操作,只有最后一个会起作用,前面的都会被覆盖掉.但是例外是,当这些transition是chaining的形式连接的时候,这些trans

d3.js学习笔记(1)

很早之前就知道d3,当初看到它的时候,第一反应就是"我去,怎么这么炫酷",但是一直没有学(自己太懒了),最近可能是五月病犯了,不想看书,不想写代码,不想看论文,不想写论文,虽然什么事情都不想做,不过还是找点事情做吧,那就学学d3呗,说不定将来什么时候就用到了呢. 这篇博客主要从源码的角度去学习,所以对于没有接触过d3的朋友,请先看完下面的资料. 学习资料 学习嘛,当然得找到好的资料咯,下面是我昨天看的一些文章,在d3的github上都能够找到,毕竟最好的学习资料就是官网的文档.教程和源

d3.js在vue项目中的安装及案例

1. 首先安装 npm install d3 --save-dev或者 cnpm install d3 --save-dev (需要先安装cnpm,本人喜欢用这个国内镜像比较快) 以防万一,然后看package.json 2. 引入:main.js import * as d3 from "d3"; Vue.prototype.$d3 = d3; window.d3 = d3; //暂时设置为全局变量 3. demo代码:  demo源码参考地址 <template> &l

d3js enter/exit深入了解

在 Data joins 章节我们演示了当data和dom element个数相同时的情况 <div id="content"> <div></div> <div></div> <div></div> </div> 给定下面的数据 var myData = [ 10, 40, 20 ]; 当我们对div元素join上数据时 d3.select('#content') .selectAll('

D3.js Data-Driven Documents

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietar

PatentTips - Safe general purpose virtual machine computing system

BACKGROUND OF THE INVENTION The present invention relates to virtual machine implementations, and in particular to a safe general purpose virtual machine that generates optimized virtual machine computer programs that are executable in a general purp