【D3.js数据可视化系列教程】--(十五)SVG基本图形绘制

本文详细介绍了如何使用D3.js在SVG中绘制各种图形元素,包括路径、折线、多边形、直线、椭圆等,为数据可视化提供实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.youkuaiyun.com/zhang__tianxu/article/details/11784011

1. 路径


    
    
  1. // 在 body 中插入一個 svg
  2. var svg = d3. select( 'body'). append( 'svg');
  3. // 在 svg 中插入一個 path
  4. svg. append( 'path').attr({
  5. d: 'M50 150Q300 50 300 150T450 150'
  6. }).style({
  7. fill: 'none',
  8. stroke: 'purple',
  9. 'stroke-width': 5
  10. });

  2. 折线


    
    
  1. // 在 body 中插入一個 svg
  2. var svg = d3. select( 'body'). append( 'svg');
  3. // 在 svg 中插入 polyline
  4. svg. append( 'polyline').attr({
  5. points: '100,10 40,180 190,60 10,60 160,180 100,10'
  6. }).style({
  7. fill: 'black',
  8. stroke: 'green',
  9. 'stroke-width': 4
  10. });
  11. // 在 svg 中插入 polyline
  12. svg. append( 'polyline').attr({
  13. points: '200,160 240,160 240,120 280,120 280,80 320,80 320,40 360,40 360,160 240,160'
  14. }).style({
  15. fill: 'none',
  16. stroke: 'green',
  17. 'stroke-width': 4
  18. });

  3. 多边形


    
    
  1. // 在 body 中插入一個 svg
  2. var svg = d3. select( 'body'). append( 'svg');
  3. // 在 svg 中插入 polygon
  4. svg. append( 'polygon').attr({
  5. points: '50,10 20,50 80,50'
  6. }).style({
  7. fill: 'none',
  8. stroke: '#f0f',
  9. 'stroke-width': 4
  10. });
  11. // 在 svg 中插入 polygon
  12. svg. append( 'polygon').attr({
  13. points: '70,10 130,10 100,50 '
  14. }).style({
  15. fill: 'none',
  16. stroke: '#520',
  17. 'stroke-width': 4
  18. });
  19. // 在 svg 中插入 polygon
  20. svg. append( 'polygon').attr({
  21. points: '150,10 120,50 180,50'
  22. }).style({
  23. fill: 'none',
  24. stroke: '#f0f',
  25. 'stroke-width': 4
  26. });

  4. 直线


    
    
  1. // 在 body 中插入一個 svg
  2. var svg = d3. select( 'body'). append( 'svg');
  3. // 在 svg 中插入 line
  4. svg. append( 'line').attr({
  5. x1: 40,
  6. y1: 70,
  7. x2: 250,
  8. y2: 70
  9. }).style({
  10. stroke: 'black',
  11. 'stroke-width': 5
  12. });
  13. // 在 svg 中插入 line
  14. svg. append( 'line').attr({
  15. x1: 40,
  16. y1: 140,
  17. x2: 250,
  18. y2: 140
  19. }).style({
  20. stroke: 'black',
  21. 'stroke-width': 5
  22. });
  23. // 在 svg 中插入 line
  24. svg. append( 'line').attr({
  25. x1: 100,
  26. y1: 10,
  27. x2: 100,
  28. y2: 200
  29. }).style({
  30. stroke: 'black',
  31. 'stroke-width': 5
  32. });
  33. // 在 svg 中插入 line
  34. svg. append( 'line').attr({
  35. x1: 180,
  36. y1: 10,
  37. x2: 180,
  38. y2: 200
  39. }).style({
  40. stroke: 'black',
  41. 'stroke-width': 5
  42. });
  43. // 在 svg 中插入 circle
  44. svg. append( 'circle').attr({
  45. cx: 140,
  46. cy: 105,
  47. r: 20
  48. }).style({
  49. fill: 'none',
  50. stroke: 'red',
  51. 'stroke-width': 4
  52. });
  53. // 在 svg 中插入 line
  54. svg. append( 'line').attr({
  55. x1: 50,
  56. y1: 20,
  57. x2: 80,
  58. y2: 50
  59. }).style({
  60. stroke: 'black',
  61. 'stroke-width': 5
  62. });
  63. // 在 svg 中插入 line
  64. svg. append( 'line').attr({
  65. x1: 80,
  66. y1: 20,
  67. x2: 50,
  68. y2: 50
  69. }).style({
  70. stroke: 'black',
  71. 'stroke-width': 5
  72. });
  73. // 在 svg 中插入 circle
  74. svg. append( 'circle').attr({
  75. cx: 220,
  76. cy: 180,
  77. r: 20
  78. }).style({
  79. fill: 'none',
  80. stroke: 'red',
  81. 'stroke-width': 4
  82. });

  5. 椭圆


    
    
  1. // 在 body 中插入一個 svg
  2. var svg = d3. select( 'body'). append( 'svg');
  3. // 在 svg 中插入 ellipse
  4. svg. append( 'ellipse').attr({
  5. cx: 100,
  6. cy: 60,
  7. rx: 30,
  8. ry: 50
  9. }).style({
  10. fill: 'pink',
  11. stroke: 'green',
  12. 'stroke-width': 10
  13. });
  14. // 在 svg 中插入 ellipse
  15. svg. append( 'ellipse').attr({
  16. cx: 200,
  17. cy: 60,
  18. rx: 30,
  19. ry: 50
  20. }).style({
  21. fill: 'pink',
  22. stroke: 'green',
  23. 'stroke-width': 10,
  24. 'fill-opacity': .6
  25. });
  26. // 在 svg 中插入 ellipse
  27. svg. append( 'ellipse').attr({
  28. cx: 145,
  29. cy: 180,
  30. rx: 110,
  31. ry: 40
  32. }).style({
  33. fill: 'pink',
  34. stroke: 'green',
  35. 'stroke-width': 5,
  36. opacity: .6
  37. });


 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值