上一节中我们学会了如何旋转x轴标签以及自定义标签内容,在这一节中,我们将接触动画(transition)

首先,我们要在页面上添加一个按钮,当我们点击这个按钮时,调用我们的动画。所以,我们还需要在原来的基础上添加两个东西。

添加一个按钮

<div id="option">
<input name="updateButton"
    type="button"
    value="Update"
    onclick="updateData()"
/>
</div>

添加一个动画函数

function updateData() {
  //再次获取数据
  d3.tsv("../data/data-alt.tsv", function(error, data){
    data.forEach(function(d){
      d.date = parseDate(d.date);
      d.close = +d.close;
    });
 
    //设置数据的规模
    x.domain(d3.extent(data, function(d){ return d.date }));
    y.domain([0, d3.max(data, function(d){ return d.close })]);
 
    //选择我们想要应用变化的部分
    var svg = d3.select("body").transition();
 
    //变化
    svg.select(".line")
      .duration(750)
      .attr("d", valueline(data));
    svg.select(".x.axis")
      .duration(750)
      .call(xAxis);
    svg.select(".y.axis")
      .duration(750)
      .call(yAxis);
  });
}

在上面的代码中, 我们首先要获取一个组先的数据,所以,我们从新的数据文件(data-alt.tsv)中读取新的数据。然后,仿造前面绘制图表的方法来进行绘制,不同的是,这里加入一个新的方法-transition()。

transiton(int): 使图表从一个状态过渡到另一个状态。括号里面可以是一个整数,表示动画执行的时长,当然也可以是一个ease(type[, arguments…])方法,来表示丰富的运动。

目前的代码为:

 <!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */ body { font: 12px Arial;} path {
stroke: steelblue;
stroke-width: 2;
fill: none;
} .axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
} </style>
<body> <div id="option">
<input name="updateButton"
type="button"
value="Update"
onclick="updateData()" />
</div> <!-- load the d3.js library -->
<script type="text/javascript" src="d3/d3.v3.js"></script> <script> // Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom; // Parse the date / time
var parseDate = d3.time.format("%d-%b-%y").parse; // Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]); // Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5); var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5); // Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); }); // Adds the svg canvas
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // Get the initial data
d3.tsv("data/data.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
}); // Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]); // Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data)); // Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis); // Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis); }); // ** Update data section (Called from the onclick)
function updateData() { // Get the data again
d3.tsv("data/data-alt.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
}); // Scale the range of the data again
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]); // Select the section we want to apply our changes to
var svg = d3.select("body").transition(); // Make the changes
svg.select(".line") // change the line
.duration(750)
.attr("d", valueline(data));
svg.select(".x.axis") // change the x axis
.duration(750)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(750)
.call(yAxis); });
} </script>
</body>

下节我们将把图表中的曲线图变成散点图,以及添加提示框(Tooltips)效果。

最新文章

  1. jquery 图片轮播demo实现
  2. Android系统启动过程
  3. js json 对象相互转换
  4. activiti查看流程图,有中文乱码
  5. 解决Selenium与firefox浏览器版本不兼容问题
  6. 转:EClipse 10个最有用的快捷键
  7. PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)
  8. 安装pgadmin3
  9. OC—设计模式-通知的使用
  10. DispatcherServlet--Spring的前置控制器作用简介
  11. GDB-Dashboard-GDB可视化界面
  12. 前端新人学习笔记-------html/css/js基础知识点(三)
  13. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
  14. 【LeetCode】112. Path Sum
  15. elasticsearch2.3.3安装
  16. 几款实力很强的小工具,提高Windows使用效率
  17. 关于C++11右值引用和移动语义的探究
  18. Java 环境配置 与 碰到的问题
  19. 大数据【一】集群配置及ssh免密认证
  20. CentOS7下安装Scrapy

热门文章

  1. Swift与C#的基础语法比较
  2. Ubuntu 16.10 安装KolourPaint 4画图工具
  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(55)-Web打印
  4. C++中的引用
  5. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
  6. JS的内建函数reduce
  7. Android实现TCP断点上传,后台C#服务实现接收
  8. Extjs 让combobox写起来更简单
  9. 常用 meta 整理
  10. css_02之盒模型、渐变