当前位置:   article > 正文

数据可视化 地图和音乐可视化_实践6 地图可视化与音乐可视化 csdn

实践6 地图可视化与音乐可视化 csdn

  1. <html>
  2. <head>
  3. <meta charSet="utf-8">
  4. <title>基于GeoJSON绘制北京地图</title>
  5. <style>
  6. .province {
  7. stroke: black;
  8. stroke-width: 1px;
  9. }
  10. .southchinasea {
  11. stroke: black;
  12. stroke-width: 1px;
  13. fill: red;
  14. }
  15. body {
  16. background-image: url("https://img.mp.itc.cn/q_70,c_zoom,w_640/upload/20161019/b2a9d777c3734024bae19464c9f487ba_th.jpeg");
  17. background-size: cover;
  18. background-position: center;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <script src="https://d3js.org/d3.v7.min.js" charSet="utf-8"></script>
  24. <script>
  25. var width = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) * 0.98;
  26. var height = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight) * 2;
  27. var svg = d3.select("body").append("svg")
  28. .attr("width", width)
  29. .attr("height", height);
  30. var projection = d3.geoMercator()
  31. .center([116.3956, 39.93])
  32. .scale(10000)
  33. .translate([width / 2, height / 2]);
  34. //定义地形路径生成器
  35. var geoPath = d3.geoPath()
  36. .projection(projection); //设定投影
  37. console.log(geoPath);
  38. //颜色比例尺
  39. var color = d3.schemePaired; //d3.schemeBlues[9]; //d3.schemeCategory10;
  40. console.log(color);
  41. //请求ChinaGeo.json
  42. d3.json("hebei.json").then(function (data) { //100000_full.json A2_100000_full.json
  43. console.log(data);
  44. var scaleColor = d3.scaleOrdinal()
  45. .domain(d3.range(data.features.length))
  46. .range(d3.schemeCategory10);
  47. var groups = svg.append("g");
  48. groups.selectAll("path")
  49. .data(data.features)
  50. .enter()
  51. .append("path")
  52. .attr("class", "province")
  53. .attr("stroke", "black")
  54. .attr("fill", (d, i) => color[i % 12])
  55. .attr("d", (d, i) => geoPath(d)); //使用路径生成器
  56. var texts = svg.selectAll(".texts")
  57. .data(data.features)
  58. .enter()
  59. .append("text")
  60. .attr("class", "texts")
  61. .text(function (d, i) {
  62. return d.properties.name + "-" + i;
  63. })
  64. .attr("transform", function (d) {
  65. var centroid = geoPath.centroid(d),
  66. x = centroid[0],
  67. y = centroid[1];
  68. return "translate(" + x + ", " + y + ")";
  69. })
  70. .attr('fill', '999')
  71. .attr("font-size", "35px");
  72. svg.append("text")
  73. .attr("font-size", "40px")
  74. .attr("font-family", "Fantasy")
  75. .attr("text-anchor", "middle")
  76. .attr("x", w / 2)
  77. .attr("y", h / 17)
  78. .text("河北省地图");
  79. });
  80. </script>
  81. </body>
  82. </html>

 音乐可视化

  1. <html>
  2. <head>
  3. <meta charSet="utf-8">
  4. <title>基于D3的音乐可视化</title>
  5. </head>
  6. <body>
  7. <audio id="audioElement" src="sb.mp3" controls="controls"></audio>
  8. <script src="d3.v3.min.js" charset="utf-8"></script>
  9. <script>
  10. var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  11. var audioElement = document.getElementById('audioElement');
  12. var audioSrc = audioCtx.createMediaElementSource(audioElement);
  13. var analyser = audioCtx.createAnalyser();
  14. //绑定分析器到音频媒体元素
  15. audioSrc.connect(analyser);
  16. audioSrc.connect(audioCtx.destination);
  17. var frequencyData = new Uint8Array(100);
  18. var width = 1200;
  19. var height =500;
  20. var innerR = 80;//内半径
  21. var r = Math.floor(Math.random() * 255);
  22. var g = Math.floor(Math.random() * 255);
  23. var b = Math.floor(Math.random() * 255);
  24. var dataset=new Array(100);
  25. for(var i=0;i<dataset.length;i++){
  26. dataset[i]=new Array();
  27. dataset[i][0]=120;
  28. dataset[i][1]=100+Math.floor(Math.random()*(255-100));
  29. }
  30. //转化数据为适合生成饼图的对象数组
  31. var pie = d3.layout.pie()
  32. .value(function(d){return d[0];});
  33. var arcPath=d3.svg.arc()//内外半径
  34. .innerRadius(innerR);
  35. var svg=d3.select("body")
  36. .append("svg")
  37. .attr("width",width)
  38. .attr("height",height);
  39. var arcs=svg.selectAll("path")
  40. .data(pie(dataset))//原生数据-->起止角度
  41. .enter()
  42. .append("path")
  43. .attr("transform", "translate(" + width/2 + "," + height/2 + ")")
  44. .attr("fill",function(d,i){//填充颜色
  45. return 'rgb(0,152,'+dataset[i][1]+')';
  46. })
  47. .attr("stroke","#FFF")
  48. .attr("d",function(d,i){
  49. arcPath.outerRadius(dataset[i][1]);
  50. return arcPath(d); //起止角度(内外半径)——>路径的参数
  51. });
  52. // 连续循环更新
  53. function renderChart() {
  54. requestAnimationFrame(renderChart);
  55. analyser.getByteFrequencyData(frequencyData);
  56. //console.log(frequencyData);
  57. svg.selectAll('path')
  58. .data(pie(dataset))
  59. .attr("fill",function(d,i){//填充颜色
  60. return 'rgb(0,0,'+frequencyData[i]+')';
  61. })
  62. .attr("d",function(d,i){
  63. arcPath.outerRadius(frequencyData[i]+ innerR);
  64. return arcPath(d);
  65. });
  66. }
  67. renderChart();
  68. </script>
  69. </body>
  70. </html>

 canvas特效

 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charSet="utf-8">
  5. <title>浮雕和雕刻特效</title>
  6. </head>
  7. <style>
  8. body {
  9. text-align: center;
  10. background-color: #f9f9f9;
  11. font-size: 16px;
  12. font-family: "Microsoft YaHei", sans-serif;
  13. }
  14. button {
  15. margin-top: 20px;
  16. padding: 10px;
  17. background-color: #4CAF50;
  18. color: white;
  19. border: none;
  20. cursor: pointer;
  21. border-radius: 3px;
  22. box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.2);
  23. transition: all 0.2s ease-in-out;
  24. }
  25. button:hover {
  26. transform: scale(1.05);
  27. }
  28. </style>
  29. <body>
  30. <center>
  31. <h2>浮雕和雕刻特效</h2>
  32. <button id="embossBtn">浮雕</button>
  33. <button id="engraveBtn">雕刻</button>
  34. <br/><br/>
  35. <canvas id="myCanvas" width="600" height="400" style="border:1px solid #c3c3c3;"></canvas>
  36. <script type="text/javascript">
  37. var c=document.getElementById("myCanvas");
  38. var cxt=c.getContext("2d");
  39. var img=new Image()
  40. img.src="a.jpg"
  41. img.onload = function () {
  42. cxt.drawImage(img, 0, 0);
  43. }
  44. function emboss() {
  45. var imageData = cxt.getImageData(0, 0, c.width, c.height);
  46. var data = imageData.data;
  47. for (var i = 0; i < data.length; i += 4) {
  48. if (i < c.width * 4 || i > data.length - c.width * 4) {
  49. continue;
  50. }
  51. var x = (i / 4) % c.width;
  52. if (x === 0 || x === c.width - 1) {
  53. continue;
  54. }
  55. var r1 = data[i - c.width * 4 + 0];
  56. var g1 = data[i - c.width * 4 + 1];
  57. var b1 = data[i - c.width * 4 + 2];
  58. var r2 = data[i - 4 + 0];
  59. var g2 = data[i - 4 + 1];
  60. var b2 = data[i - 4 + 2];
  61. var r3 = data[i + 4 + 0];
  62. var g3 = data[i + 4 + 1];
  63. var b3 = data[i + 4 + 2];
  64. var r4 = data[i + c.width * 4 + 0];
  65. var g4 = data[i + c.width * 4 + 1];
  66. var b4 = data[i + c.width * 4 + 2];
  67. var gray1 = (r1 + g1 + b1) / 3;
  68. var gray2 = (r2 + g2 + b2) / 3;
  69. var gray3 = (r3 + g3 + b3) / 3;
  70. var gray4 = (r4 + g4 + b4) / 3;
  71. var xDiff = (gray2 + gray2 + gray2 - gray4 - gray4 - gray4);
  72. var yDiff = (gray1 + gray1 + gray1 - gray3 - gray3 - gray3);
  73. var gray = parseInt(Math.sqrt(xDiff * xDiff + yDiff * yDiff) * 0.8);
  74. if (gray > 255) {
  75. gray = 255;
  76. }
  77. data[i + 0] = gray;
  78. data[i + 1] = gray;
  79. data[i + 2] = gray;
  80. }
  81. cxt.putImageData(imageData, 0, 0);
  82. }
  83. function engrave() {
  84. var imageData = cxt.getImageData(0, 0, c.width, c.height);
  85. var data = imageData.data;
  86. for (var i = 0; i < data.length; i += 4) {
  87. data[i + 0] = 255 - data[i + 0];
  88. data[i + 1] = 255 - data[i + 1];
  89. data[i + 2] = 255 - data[i + 2];
  90. }
  91. cxt.putImageData(imageData, 0, 0);
  92. }
  93. var embossBtn = document.getElementById('embossBtn');
  94. var engraveBtn = document.getElementById('engraveBtn');
  95. embossBtn.addEventListener('click', function() {
  96. emboss();
  97. });
  98. engraveBtn.addEventListener('click', function() {
  99. engrave();
  100. });
  101. </script>
  102. </center>
  103. </body>
  104. </html>

灰度等高线

 

  1. <html>
  2. <head>
  3. <meta charSet="utf-8">
  4. <script src="https://d3js.org/d3.v5.min.js"></script>
  5. </head>
  6. <style>
  7. body {
  8. text-align: center;
  9. background-color: #f9f9f9;
  10. font-size: 16px;
  11. font-family: "Microsoft YaHei", sans-serif;
  12. }
  13. button {
  14. margin-top: 20px;
  15. padding: 10px;
  16. background-color: #4CAF50;
  17. color: white;
  18. border: none;
  19. cursor: pointer;
  20. border-radius: 3px;
  21. box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.2);
  22. transition: all 0.2s ease-in-out;
  23. }
  24. button:hover {
  25. transform: scale(1.05);
  26. }
  27. svg {
  28. display: block;
  29. margin: 0 auto;
  30. border: 1px solid #ddd;
  31. box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.2);
  32. }
  33. </style>
  34. <body>
  35. <center>
  36. <h2>使用D3绘制等高线图</h2>
  37. <hr width=70%></hr>
  38. <button id="gray">原图</button>
  39. <button id="contour">登高线图</button>
  40. <svg id="mySvg" width="600" height="400" style="border:1px solid #c3c3c3;"></svg>
  41. <script type="text/javascript">
  42. var svg=d3.select("#mySvg");
  43. var img=new Image()
  44. img.src="unknow.jpg"
  45. img.onload = function () {
  46. var canvas = document.createElement("canvas");
  47. canvas.width = this.width;
  48. canvas.height = this.height;
  49. var ctx = canvas.getContext("2d");
  50. ctx.drawImage(this, 0, 0);
  51. var data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  52. // 灰度化处理
  53. var grayData = new Array(canvas.width * canvas.height);
  54. for (var i = 0; i < grayData.length; i++) {
  55. grayData[i] = (data[i * 4 + 0] + data[i * 4 + 1] + data[i * 4 + 2]) / 3.0 / 256.0;
  56. }
  57. // 定义颜色比例尺
  58. var colorScale = d3.scaleSequential()
  59. .domain([0, 64])
  60. .interpolator(d3.interpolateGreens);
  61. // 初始化等高线数据
  62. var contours = d3.contours()
  63. .size([canvas.width, canvas.height])
  64. .thresholds(d3.range(0, 1, 0.1))(dataToValues(data));
  65. // 绘制登高线图
  66. function drawContour() {
  67. svg.selectAll("path").remove();
  68. svg.selectAll("path")
  69. .data(contours)
  70. .enter().append("path")
  71. .attr("d", d3.geoPath(d3.geoIdentity().scale(5)))
  72. .attr("fill", function(d) { return colorScale(d.value); })
  73. .attr("stroke", "red")
  74. .attr("stroke-width", 1);
  75. }
  76. // 绘制灰度图
  77. function drawGray() {
  78. svg.selectAll("path").remove();
  79. svg.append("image")
  80. .attr("xlink:href", canvas.toDataURL())
  81. .attr("width", canvas.width)
  82. .attr("height", canvas.height);
  83. }
  84. // 将原始数据转换为等高线数据
  85. function dataToValues(data) {
  86. var values = new Array(canvas.width * canvas.height);
  87. for (var i = 0; i < values.length; i++) {
  88. values[i] = grayData[i];
  89. }
  90. return values;
  91. }
  92. // 监听按钮点击事件
  93. d3.select("#gray").on("click", drawGray);
  94. d3.select("#contour").on("click", drawContour);
  95. // 默认绘制等高线图
  96. drawContour();
  97. }
  98. </script>
  99. </center>
  100. </body>
  101. </html>

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

闽ICP备14008679号