d3.js <一>
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了d3.js <一>,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5557字,纯文字阅读大概需要8分钟。
内容图文

1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>HelloWorld</title> 5 </head> 6 <body> 7 <p>Hello World 1</p> 8 <p>Hello World 2</p> 9 <!-- <p>Hello World 1</p> 10 <p>Hello World 2</p> --> 11 <div id="con"></div> 12 <div id="chart01"></div> 13 <bottom><button type="button" onclick="myadd()">add</button><button type="button" onclick="mysort()">sort</button></bottom> 14 <script src="./jquery-2.1.4.min.js" charset="utf-8"></script> 15 <script src="./d3.js" charset="utf-8"></script> 16 <script> 17// var p = d3.select("body").selectAll("p"); 18// // p.datum("Thunder").append("span").text(function(d, i) { 19// // return d + " " + i; 20// // }); 21 22// var dataset = [{"id":1, "name":"张三"}, 23// {"id":2, "name":"张三2"}, 24// {"id":3, "name":"张三3"}, 25// {"id":4, "name":"张三4"}]; 26// var update = p.data(dataset); 27 28// update.text(function(d) { 29// return d.id + "--" + d.name; 30// }); 31 32// update.enter().append("p").text(function(d) { 33// return d.id + "--" + d.name; 34// }); 35 36// var condiv = d3.select(document.getElementById("con")); 37 38// condiv.selectAll("span").data(dataset).enter().append("span").text(function(d) { 39// return d.id + "---" + d.name; 40// }); 41 42// var numbers = [12, 23, 25, 67, 5, 26, 19, 8]; 43// console.log(d3.min(numbers, function(d) {return d * 3;})); 44// console.log(d3.max(numbers)); 45// console.log(d3.extent(numbers, function(d) { 46// return d % 3; 47// })); 48// console.log(d3.sum(numbers)); 49 50// console.log(d3.mean(numbers)); 51 52// console.log(d3.median(numbers)); 53// console.log(numbers.sort(d3.ascendind)); 54// console.log(d3.quantile(numbers, 19.0)); 55// p.data(dataset, function(d) {return d.id;}).text(function(d) { 56// return d.id + " " + d.name; 57// }); 58// console.log(update); 59// console.log(update.enter()); 60// console.log(update.exit()); 61 62// console.log(p); 63var dataset = [30, 45, 23, 69, 160, 55, 99]; 64var chart01 = d3.select(document.getElementById("chart01")); 65var width = 800; 66var height = 400; 67var padding = {"top": 20, "right": 20, "left": 20, "bottom": 20}; 68var rectStep = 55; 69var rectWidth = 45; 70 71var svg = chart01 72 .append("svg") 73 .attr("width", width) 74 .attr("height", height); 75 76var rect = svg.selectAll("rect") 77 .data(dataset) 78 .enter() 79 .append("rect") 80 .attr("fill", "steelblue") 81 .attr("x", function(d, i) { 82return padding.left + i * rectStep; 83 }) 84 .attr("y", function(d) { 85return height - padding.bottom - d; 86 }) 87 .attr("width", rectWidth) 88 .attr("height", function(d) { 89return d; 90 }); 91 92var text = svg.selectAll("text") 93 .data(dataset) 94 .enter() 95 .append("text") 96 .attr("text-anchor", "middle") 97 .attr("fill", "white") 98 .attr("x", function(d, i) { 99return padding.left + i * rectStep; 100 }) 101 .attr("y", function(d) { 102return height - padding.bottom - d; 103 }) 104 .attr("width", rectWidth) 105 .attr("height", function(d) { 106return d; 107 }) 108 .attr("dx", rectWidth/2) 109 .attr("dy", "1em") 110 .text(function(d) { 111return d; 112 }); 113114function redraw(dataset) { 115var updateRect = svg.selectAll("rect").data(dataset); 116var enterRect = updateRect.enter(); 117var exitRect = updateRect.exit(); 118119var updateText = svg.selectAll("text").data(dataset); 120var enterText = updateText.enter(); 121var exitText = updateText.exit(); 122123 updateRect.attr("fill", "steelblue") 124 .attr("x", function(d, i) { 125return padding.left + i * rectStep; 126 }) 127 .attr("y", function(d) { 128return height - padding.bottom - d; 129 }) 130 .attr("width", rectWidth) 131 .attr("height", function(d) { 132return d; 133 }); 134135 enterRect.append("rect") 136 .attr("fill", "steelblue") 137 .attr("x", function(d, i) { 138return padding.left + i * rectStep; 139 }) 140 .attr("y", function(d) { 141return height - padding.bottom - d; 142 }) 143 .attr("width", rectWidth) 144 .attr("height", function(d) { 145return d; 146 }); 147148 exitRect.remove(); 149150 updateText.attr("text-anchor", "middle") 151 .attr("fill", "white") 152 .attr("x", function(d, i) { 153 console.log("de-->" + d + "\t-->" + i + "\te-->" + (padding.left + i * rectStep)); 154return padding.left + i * rectStep; 155 }) 156 .attr("y", function(d) { 157return height - padding.bottom - d; 158 }) 159 .attr("width", rectWidth) 160 .attr("height", function(d) { 161return d; 162 }) 163 .attr("dx", rectWidth/2) 164 .attr("dy", "1em") 165 .text(function(d) { 166return d; 167 }); 168169 enterText.append("text") 170 .attr("text-anchor", "middle") 171 .attr("fill", "white") 172 .attr("x", function(d, i) { 173174 console.log("d-->" + d + "\t-->" + i + "\te-->" + (padding.left + i * rectStep)); 175return padding.left + i * rectStep; 176 }) 177 .attr("y", function(d) { 178return height - padding.bottom - d; 179 }) 180 .attr("width", rectWidth) 181 .attr("height", function(d) { 182return d; 183 }) 184 .attr("dx", rectWidth/2) 185 .attr("dy", "1em") 186 .text(function(d) { 187return d; 188 }); 189190 exitText.remove(); 191 } 192193function myadd() { 194 dataset.push(Math.floor(Math.random() * 100)); 195 console.log(dataset); 196 redraw(dataset); 197 } 198199function mysort() { 200 dataset.sort(d3.ascending); 201 redraw(dataset); 202 } 203 </script> 204 </body> 205 </html>
原文:http://www.cnblogs.com/linkong1081/p/5026708.html
内容总结
以上是互联网集市为您收集整理的d3.js <一>全部内容,希望文章能够帮你解决d3.js <一>所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。