<html>
<head>
<meta charset="utf-8">
<title>HelloWorld</title>
</head>
<body>
<p>Hello World 1</p>
<p>Hello World 2</p>
<!-- <p>Hello World 1</p>
<p>Hello World 2</p> -->
<div id="con"></div>
<div id="chart01"></div>
<bottom><button type="button" onclick="myadd()">add</button><button type="button" onclick="mysort()">sort</button></bottom>
<script src="./jquery-2.1.4.min.js" charset="utf-8"></script>
<script src="./d3.js" charset="utf-8"></script>
<script>
// var p = d3.select("body").selectAll("p");
// // p.datum("Thunder").append("span").text(function(d, i) {
// // return d + " " + i;
// // }); // var dataset = [{"id":1, "name":"张三"},
// {"id":2, "name":"张三2"},
// {"id":3, "name":"张三3"},
// {"id":4, "name":"张三4"}];
// var update = p.data(dataset); // update.text(function(d) {
// return d.id + "--" + d.name;
// }); // update.enter().append("p").text(function(d) {
// return d.id + "--" + d.name;
// }); // var condiv = d3.select(document.getElementById("con")); // condiv.selectAll("span").data(dataset).enter().append("span").text(function(d) {
// return d.id + "---" + d.name;
// }); // var numbers = [12, 23, 25, 67, 5, 26, 19, 8];
// console.log(d3.min(numbers, function(d) {return d * 3;}));
// console.log(d3.max(numbers));
// console.log(d3.extent(numbers, function(d) {
// return d % 3;
// }));
// console.log(d3.sum(numbers)); // console.log(d3.mean(numbers)); // console.log(d3.median(numbers));
// console.log(numbers.sort(d3.ascendind));
// console.log(d3.quantile(numbers, 19.0));
// p.data(dataset, function(d) {return d.id;}).text(function(d) {
// return d.id + " " + d.name;
// });
// console.log(update);
// console.log(update.enter());
// console.log(update.exit()); // console.log(p);
var dataset = [30, 45, 23, 69, 160, 55, 99];
var chart01 = d3.select(document.getElementById("chart01"));
var width = 800;
var height = 400;
var padding = {"top": 20, "right": 20, "left": 20, "bottom": 20};
var rectStep = 55;
var rectWidth = 45; var svg = chart01
.append("svg")
.attr("width", width)
.attr("height", height); var rect = svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("fill", "steelblue")
.attr("x", function(d, i) {
return padding.left + i * rectStep;
})
.attr("y", function(d) {
return height - padding.bottom - d;
})
.attr("width", rectWidth)
.attr("height", function(d) {
return d;
}); var text = svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.attr("text-anchor", "middle")
.attr("fill", "white")
.attr("x", function(d, i) {
return padding.left + i * rectStep;
})
.attr("y", function(d) {
return height - padding.bottom - d;
})
.attr("width", rectWidth)
.attr("height", function(d) {
return d;
})
.attr("dx", rectWidth/2)
.attr("dy", "1em")
.text(function(d) {
return d;
}); function redraw(dataset) {
var updateRect = svg.selectAll("rect").data(dataset);
var enterRect = updateRect.enter();
var exitRect = updateRect.exit(); var updateText = svg.selectAll("text").data(dataset);
var enterText = updateText.enter();
var exitText = updateText.exit(); updateRect.attr("fill", "steelblue")
.attr("x", function(d, i) {
return padding.left + i * rectStep;
})
.attr("y", function(d) {
return height - padding.bottom - d;
})
.attr("width", rectWidth)
.attr("height", function(d) {
return d;
}); enterRect.append("rect")
.attr("fill", "steelblue")
.attr("x", function(d, i) {
return padding.left + i * rectStep;
})
.attr("y", function(d) {
return height - padding.bottom - d;
})
.attr("width", rectWidth)
.attr("height", function(d) {
return d;
}); exitRect.remove(); updateText.attr("text-anchor", "middle")
.attr("fill", "white")
.attr("x", function(d, i) {
console.log("de-->" + d + "\t-->" + i + "\te-->" + (padding.left + i * rectStep));
return padding.left + i * rectStep;
})
.attr("y", function(d) {
return height - padding.bottom - d;
})
.attr("width", rectWidth)
.attr("height", function(d) {
return d;
})
.attr("dx", rectWidth/2)
.attr("dy", "1em")
.text(function(d) {
return d;
}); enterText.append("text")
.attr("text-anchor", "middle")
.attr("fill", "white")
.attr("x", function(d, i) { console.log("d-->" + d + "\t-->" + i + "\te-->" + (padding.left + i * rectStep));
return padding.left + i * rectStep;
})
.attr("y", function(d) {
return height - padding.bottom - d;
})
.attr("width", rectWidth)
.attr("height", function(d) {
return d;
})
.attr("dx", rectWidth/2)
.attr("dy", "1em")
.text(function(d) {
return d;
}); exitText.remove();
} function myadd() {
dataset.push(Math.floor(Math.random() * 100));
console.log(dataset);
redraw(dataset);
} function mysort() {
dataset.sort(d3.ascending);
redraw(dataset);
}
</script>
</body>
</html>

d3.js <一>的更多相关文章

  1. D3.js学习(七)

    上一节中我们学会了如何旋转x轴标签以及自定义标签内容,在这一节中,我们将接触动画(transition) 首先,我们要在页面上添加一个按钮,当我们点击这个按钮时,调用我们的动画.所以,我们还需要在原来 ...

  2. D3.js学习(一)

    从今天开始我将和大家一起学习D3.js(Data-Driven Documents),由于国内关于D3的学习资料少之又少,所以我觉得很有必要把自己学习过程记录下来,供同学们参考,如果文章有有哪些表达有 ...

  3. D3.js学习记录

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. D3.js部署node环境开发

    总结一段D3.js部署node环境的安装过程 准备阶段: 首先电脑上要安装node环境,这个阶段过滤掉,如果node环境都不会装,那就别玩基于node环境搞的其他东西了. 搭建环境: 我在自己的F:系 ...

  5. d3.js读书笔记-1

    d3.js入门 d3入门 D3是一个强大的数据可视化工具,它是基于Javascript库的,用于创建数据可视化图形.在生成可视化图形的过程中,需要以下几步: 把数据加载到浏览器的内存空间: 把数据绑定 ...

  6. 【 D3.js 进阶系列 — 6.1 】 缩放的应用(Zoom)

    缩放(Zoom)是另一种重要的可视化操作,主要是使用鼠标的滚轮进行. 1. zoom 的定义 缩放是由 d3.behavior.zoom() 定义的. var zoom = d3.behavior.z ...

  7. [资料搜集狂]D3.js数据可视化开发库

    偶然看到一个强大的D3.js,存档之. D3.js 是近年来十分流行的一个数据可视化开发库. 采用BSD协议 源码:https://github.com/mbostock/d3 官网:http://d ...

  8. D3.js 用层画条形图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 【 D3.js 入门系列 --- 3 】 做一个简单的图表!

    前面说了几节,都是对文字进行处理,这一节中将用 D3.js 做一个简单的柱形图. 做柱形图有很多种方法,比如用 HTML 的 div 标签,或用 svg . 推荐用 SVG 来做各种图形.SVG 意为 ...

  10. 【 D3.js 入门系列 --- 2.1 】 关于如何选择,插入,删除元素

    在D3.js中,选择元素的函数有两个:select 和 selectAll . 先说明一下它们的区别: select 是选择所有指定元素的第一个 selectAll 是选择指定元素的全部(以用于后面同 ...

随机推荐

  1. FontMetrics ----- 绘制文本,获取文本高度

    Canvas 绘制文本时,使用FontMetrics对象,计算位置的坐标. public static class FontMetrics { /** * The maximum distance a ...

  2. 十六进制字符串转化成字符串输出HexToStr(Delphi版、C#版)

    //注意:Delphi2010以下版本默认的字符编码是ANSI,VS2010的默认编码是UTF-8,delphi版得到的字符串须经过Utf8ToAnsi()转码才能跟C#版得到的字符串显示结果一致. ...

  3. 将cocos2dx项目从Visual Studio 迁移到 xcode

    因为Visual Studio和XCode的巨大差异性,一开始选择任何一个IDE,都会有一个迁移的过程,XCode的迁移到Visual Studio相对非常简单,不用再介绍.将项目从Visual St ...

  4. Dyslexic Gollum

    题意: 求长度是n的二进制串中,不含长度大于等于k的回文串的个数 分析: dp[i][j][k]表示长度i,后11位状态是j不含长度大于等于k的回文串的个数(因为k最大是10,所把后11位状态压缩,d ...

  5. db file sequential read (数据文件顺序读取)

    转载:http://www.dbtan.com/2010/04/db-file-sequential-read.html db file sequential read (数据文件顺序读取): db ...

  6. R工作空间

    工作空间,指的是你现有的R语言工作环境,它包括了任何一个用户定义的对象,比如:向量,矩阵,数据结构,列表,方法等.在一个R会话结束的时候,你可以保存现有的工作空间的映像,在下一次R启动的时候,该工作空 ...

  7. 时间日期Date类型

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  8. Chapter 7 Windows下pycaffe的使用之draw_net.py

    Chapter 6 中完成了在Windows下,对pycaffe的编译,如果编译存在问题,请参考:http://www.cnblogs.com/xiaopanlyu/p/6158902.html 本文 ...

  9. 动软代码生成与 EntityFramework 实体生成模板

    有用到EntityFrameWork的同学们,可以用用. 实体工程中添加EF6的dll 还有 ValidBox4Mvc.ValidRules.dll应用到项目中,此dll下载地址:http://www ...

  10. JXSE and Equinox Tutorial, Part 2

    http://java.dzone.com/articles/jxse-and-equinox-tutorial-part-0 ———————————————————————————————————— ...