当前位置:   article > 正文

D3.js的学习_d3js文档

d3js文档

D3.js是一个非常强大的可视化工具,这几天我正在学习相关的知识,主要是我项目里有在使用,觉得D3.js比Echarts强大,和大家一起分享一下,先附上D3.js的官方地址:

https://iowiki.com/d3js/d3js_introduction.html

1.先看一个简单的例子:一个黄色的圆的实现

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <!-- 先去官网把d3.js文件下载下来,然后引入 -->
  7. <!-- 或者直接这么引入也可以 ,不用下载了-->
  8. <script src="https://d3js.org/d3.v6.min.js"></script>
  9. </head>
  10. <body>
  11. <svg width='960' height='500' id='mainsvg' class='svgs'></svg>
  12. <script>
  13. let mainSvg = d3.select('.svgs') //选择了svg这个标签
  14. let maingroup = mainSvg
  15. .append('g') //在我选择标签时,我会在它里面添加一个组 <g></g>
  16. .attr('transform', `translate(${100},${100})`) //把这个组向下和向右各平移100个像素
  17. let circle = maingroup
  18. .append('circle') //attr是设置元素的一个属性,这里代表在组里面添加了一个circle,然后添加了3个属性
  19. .attr('stroke', 'black') //描边 内圈边 设置为黑色
  20. .attr('r', '66') //半径是66px
  21. .attr('fill', 'yellow') //填充颜色是yellow
  22. </script>
  23. </body>
  24. </html>

可以看到一个黄色的圆:

2. 使用D3设置SVG的属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>BarChart!</title>
  8. <script src="https://d3js.org/d3.v6.min.js"></script>
  9. </head>
  10. <body>
  11. <svg width='1600' height='800' id='mainsvg' class='svgs'></svg>
  12. <script>
  13. const data=[{name:'junxi',value:'3'},
  14. {name:'wangyang',value:'5'},{name:'lihe',value:'16'},{name:'makui',value:'6'},
  15. {name:'liwu',value:'6'},{name:'wenliu',value:'8'},{name:'yexuan',value:'3'},
  16. {name:'guangzhi',value:'9'},{name:'lidan',value:'12'},{name:'chizi',value:'8'},
  17. {name:'luixiang',value:'7'},{name:'zhangwei',value:'4'},
  18. {name:'yunpeng',value:'3'},{name:'sunyue',value:'6'}, ]
  19. const svg = d3.select('#mainsvg') //选择了svg这个标签的id
  20. const width=+svg.attr('width')//获取到了SVG标签的宽和高
  21. const height=+svg.attr('height')
  22. const margin={top:60,right:30,bottom:60,left:100}//预先给一个magrin,要不然坐标轴可能显示不出来
  23. const innerWidth=width-margin.left-margin.right//内容宽度
  24. const innerHeight=height-margin.top-margin.bottom//内容高度
  25. const xScale=d3.scaleLinear()//X轴
  26. .domain([0,d3.max(data,d=>d.value)])
  27. .range([0,innerWidth])//最小值是0,宽度就是我们设置的减去magrin后的值
  28. const yScale=d3.scaleBand()//y轴
  29. .domain(data.map(d=>d.name))
  30. .range([0,innerHeight])//最小值是0,高度就是我们设置的减去magrin后的值
  31. .padding(0.1)//每个矩形间距留出1%的位置
  32. const g=svg.append('g').attr('id','maingroup')
  33. .attr('transform',`translate(${margin.left},${margin.right})`)
  34. const yAxis=d3.axisLeft(yScale)//定义y坐标轴
  35. .tickSize(-innerWidth)//对轴线的控制
  36. g.append('g').call(yAxis)//把容器填满
  37. const xAxis=d3.axisBottom(xScale)
  38. g.append('g').call(xAxis)//默认坐标轴在上方显示
  39. .attr('transform',`translate(0,${innerHeight})`)//把X轴放在下边显示
  40. data.forEach(d=>{
  41. g.append('rect')//设置每个人名的柱形图 矩形
  42. .attr('width',xScale(d.value))
  43. .attr('height',yScale.bandwidth())//y轴属性稍显特殊
  44. .attr('fill','green')
  45. .attr('opacity',0.8)//设置透明度
  46. .attr('y',yScale(d.name))
  47. })
  48. d3.selectAll('.tick text').attr('font-size','2em')//设置刻度的字体,单位是em
  49. g.append('text').text('Members of CSCG')//添加一个标题
  50. .attr('font-size','2em')
  51. .attr('transform',`translate(${innerWidth/2},0)`)//设置居中显示
  52. .attr('text-anchor','middle')
  53. </script>
  54. </body>
  55. </html>

看一下运行的效果:

3.data的join属性:使用给定的数据集映射现有文档的元素

(1)先看一个简单的例子

  1. <body>
  2. <ul id="list">
  3. <li></li>
  4. <li></li>
  5. </ul>
  6. <script>
  7. d3.select("#list").selectAll("li").data([10, 20, 30, 25, 15])
  8. .text(function (d) {
  9. return "此时映射的数据是" + d; //返回前两个数据 10 20 对应文档中定义的两个li元素
  10. })
  11. .enter() //表示对对剩余数据的访问
  12. .append("li") //用于从相应数据创建新元素
  13. .text(function (d) {
  14. return "后三个数据是" + d; //返回 30 25 15
  15. })
  16. </script>
  17. </body>

 (2)exit()和remove()方法

  1. <body>
  2. <ul id="list">
  3. <li></li>
  4. <li></li>
  5. </ul>
  6. <input type="button" name="remove" value="移除第四项" onclick="remove()" />
  7. <script>
  8. d3.select("#list").selectAll("li")
  9. .data([10, 20, 30, 25, 15])
  10. .text(function (d) {
  11. return "此时映射的数据是 " + d;
  12. })
  13. .enter()
  14. .append("li")
  15. .text(function (d) {
  16. return "剩余三个数据是 " + d;
  17. });
  18. function remove() {
  19. d3.selectAll("li")
  20. .data([10, 20, 30, 15])
  21. .exit() //从数据集中动态删除的数据项
  22. .remove() //25这一项值删除了
  23. }
  24. </script>
  25. </body>

(3)datum()方法

  1. <body>
  2. <p></p>
  3. <div></div>
  4. <script>
  5. d3.select("p")
  6. .datum(50)//datum()方法用于为HTML文档中的单个元素设置值,这里为p标签设置了50
  7. .text(function(d) {
  8. return "p的值是 " + d ;
  9. });
  10. d3.select("div")
  11. .datum(100)//这里为div设置了100
  12. .append("p")
  13. .text(function(d) {
  14. return "div的值是 " + d ;
  15. });
  16. </script>
  17. </body>

4.Paths :路径用于绘制矩形,圆形,椭圆形,折线,多边形,直线和曲线。

(1)绘制一条简单的线

  1. <!DOCTYPE html>
  2. <meta charset = "UTF-8">
  3. <head>
  4. <title>SVG path line Generator</title>
  5. </head>
  6. <style>
  7. path {
  8. fill: green;
  9. stroke: #aaa;
  10. }
  11. </style>
  12. <body>
  13. <svg width = "600" height = "100">
  14. <path transform = "translate(200, 0)" />
  15. </svg>
  16. <script src = "https://d3js.org/d3.v4.min.js"></script>
  17. <script>
  18. var data = [[0, 20], [50, 30], [100, 50], [200, 60], [300, 90]];
  19. var lineGenerator = d3.line();//用于创建从当前点到定义的x,y值的线
  20. var pathString = lineGenerator(data);
  21. d3.select('path').attr('d', pathString);
  22. </script>
  23. </body>
  24. </html>

看下运行效果:

 5.Selections

(1)append()方法

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script>
  5. </head>
  6. <body>
  7. <div class = "myclass">
  8. Hello World!
  9. </div>
  10. <script>
  11. d3.select("div.myclass").append("span");//append()方法将新元素作为当前选择中元素的最后一个子元素附加
  12. </script>
  13. </body>
  14. </html>

效果展示:

(2) html()方法:用于设置所选元素的html内容

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script>
  5. </head>
  6. <body>
  7. <div class = "myclass">
  8. Hello World!
  9. </div>
  10. <script>
  11. //html()方法用于设置所选/附加元素的html内容。
  12. d3.select(".myclass").html("Hello World! <span>from D3.js</span>");
  13. </script>
  14. </body>
  15. </html>

看下效果:

(3)attr()方法:用于添加或更新所选元素的属性

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script>
  5. </head>
  6. <body>
  7. <div class = "myclass">
  8. Hello World!
  9. </div>
  10. <script>
  11. //attr()方法用于添加或更新所选元素的属性
  12. // d3.select(".myclass").attr("style", "color: red");
  13. d3.select(".myclass").style("color", "red");//等价于上面的代码
  14. </script>
  15. </body>
  16. </html>

看下效果:

(4)  classed()方法:用于设置HTML元素的“class”属性。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script>
  5. </head>
  6. <body>
  7. <div class = "myclass">
  8. Hello World!
  9. </div>
  10. <script>
  11. d3.select(".myclass").classed("myanotherclass", true);//Add class - 要添加类,必须将分类方法的第二个参数设置为true
  12. d3.select(".myclass").classed("myanotherclass", false);//要删除类,必须将分类方法的第二个参数设置为false
  13. </script>
  14. </body>
  15. </html>

(5)selectAll():用于选择HTML文档中的多个元素

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script>
  5. </head>
  6. <body>
  7. <h2 class = "myclass">Message</h2>
  8. <div class = "myclass">
  9. Hello World!
  10. </div>
  11. <script>
  12. d3.selectAll(".myclass").attr("style", "color: red");
  13. </script>
  14. </body>
  15. </html>

看下效果:

6.Drawing Charts

(1)条形图

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
  4. <style>
  5. svg rect {
  6. fill: gray;
  7. /* 设置矩形颜色 */
  8. }
  9. svg text {
  10. fill: yellow;
  11. /* 文本颜色 */
  12. font: 12px sans-serif;
  13. text-anchor: end;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script>
  19. var data = [10, 5, 12, 15];
  20. var width = 300 //SVG的宽度
  21. scaleFactor = 20, //缩放到屏幕上可见的像素值
  22. barHeight = 30; //水平条的静态高度
  23. var graph = d3.select("body")
  24. .append("svg") //添加SVG元素
  25. .attr("width", width)
  26. .attr("height", barHeight * data.length);//高度计算为条形高度*数据值的数量
  27. var bar = graph.selectAll("g")//附加组元素
  28. .data(data)
  29. .enter()
  30. .append("g")
  31. .attr("transform", function (d, i) {
  32. return "translate(0," + i * barHeight + ")";
  33. });
  34. bar.append("rect").attr("width", function (d) {//将rect元素添加到栏中
  35. return d * scaleFactor;
  36. })
  37. .attr("height", barHeight - 1);
  38. bar.append("text")
  39. .attr("x", function (d) {
  40. return (d * scaleFactor);
  41. })
  42. .attr("y", barHeight / 2)//宽度定义为(数据值*比例因子)
  43. .attr("dy", ".35em")//文本元素不支持填充或边距,设置一个“dy”偏移量
  44. .text(function (d) {
  45. return d;
  46. });
  47. </script>
  48. </body>
  49. </html>

 看下效果:

(2) 圆图

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
  4. </head>
  5. <body>
  6. <script>
  7. var width = 400; //SVG的宽度
  8. var height = 400; //SVG的高度
  9. var data = [10, 20, 30]; //数据元素数组
  10. var colors = ['green', 'purple', 'yellow']; //将颜色应用于圆形元素
  11. var svg = d3
  12. .select("body") //添加SVG元素
  13. .append("svg")
  14. .attr("width", width)
  15. .attr("height", height);
  16. var g = svg.selectAll("g") //创建用于保持圆圈的组元素
  17. .data(data) //将我们的数据数组绑定到组元素
  18. .enter() // 为我们的组元素创建占位符
  19. .append("g") //将组元素添加到我们的页面
  20. .attr("transform", function (d, i) {
  21. return "translate(0,0)"; //相对于原点定位元素
  22. })
  23. g.append("circle") //将圆形元素附加到组中
  24. .attr("cx", function (d, i) { //将属性添加到组中
  25. return i * 75 + 50;
  26. })
  27. .attr("cy", function (d, i) {
  28. return 75;
  29. })
  30. .attr("r", function (d) { //设置每个圆的半径
  31. return d * 1.5; //半径乘以数据值以及常数“1.5”以增加圆的大小
  32. })
  33. .attr("fill", function (d, i) {
  34. return colors[i]; //填充每个圆圈的颜色
  35. })
  36. g.append("text").attr("x", function (d, i) { //显示每个圆圈上的数据
  37. return i * 75 + 25;
  38. })
  39. .attr("y", 80)
  40. .attr("stroke", "teal")
  41. .attr("font-size", "10px")
  42. .attr("font-family", "sans-serif").text(function (d) {
  43. return d;
  44. });
  45. </script>
  46. </body>
  47. </html>

看下效果:

就先写这么多,具体的各位还是看官方教程,挺详细的。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/750290
推荐阅读
相关标签
  

闽ICP备14008679号