林子雨编著《大数据基础编程、实验和案例教程(第2版)》教材第11章的代码

大数据学习路线图

林子雨编著《大数据基础编程、实验和案例教程(第2版)》(教材官网)教材中的命令行和代码,在纸质教材中的印刷效果不是很好,可能会影响读者对命令行和代码的理解,为了方便读者正确理解命令行和代码或者直接拷贝命令行和代码用于上机实验,这里提供全书配套的所有命令行和代码。
查看教材所有章节的代码

第11章 典型可视化工具的使用方法

教材第174页

(温馨提示:代码框上方的复制代码按钮,也就是“两张A4纸图标”,用鼠标点击复制代码按钮,就可以把代码框中的代码复制到粘贴板,粘贴到其他地方。但是,有的浏览器可能不支持该功能)

<script src="http://d3js.org/d3.v5.min.js" charset="utf-8"></script>
d3.select("body").append("p").text("New paragraph!");
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>D3测试</title>
  5. <script type="text/javascript" src="http://d3js.org/d3.v5.min.js"> </script>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. d3.select("body").append("p").text("New paragraph!");
  10. </script>
  11. </body>
  12. </html>
HTML

教材第175页

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>testD3-1.html</title>
  5. <script type="text/javascript" src="http://d3js.org/d3.v5.min.js"></script>
  6. </head>
  7. <body>
  8. This is my HTML page. <br>
  9. </body>
  10. <script type="text/javascript">
  11. var dataset = [ 5, 10, 15, 20, 25 ];
  12. d3.select("body").selectAll("p")
  13. .data(dataset)
  14. .enter()
  15. .append("p")
  16. .text("New paragraph!");
  17. </script>
  18. </html>
HTML

教材第176页

div.bar {
    display: inline-block;
    width: 20px;
    height: 75px;   /* We'll override this later */
    background-color: teal;
}
.attr("class", "bar")
.style("height", function(d) {
    var barHeight = d * 5;  //Scale up by factor of 5
    return barHeight + "px";
});
margin-right: 2px;
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>testD3-3-drawingDivBar</title>
  6. <script type="text/javascript" src="http://d3js.org/d3.v5.min.js"> </script>
  7. <style type="text/css">
  8. div.bar {
  9. display: inline-block;
  10. width: 20px;
  11. height: 75px; /* Gets overriden by D3-assigned height below */
  12. margin-right: 2px;
  13. background-color: teal;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script type="text/javascript">
  19. var dataset = [ 5, 10, 15, 20, 25 ];
  20. d3.select("body").selectAll("div")
  21. .data(dataset)
  22. .enter()
  23. .append("div")
  24. .attr("class", "bar")
  25. .style("height", function(d) {
  26. var barHeight = d * 5;
  27. return barHeight + "px";
  28. });
  29. </script>
  30. </body>
  31. </html>
HTML

教材第177页

<rect x="0" y="0" width="500" height="50"/>
<circle cx="250" cy="25" r="25"/>

教材第178页

<ellipse cx="250" cy="25" rx="100" ry="25"/>
<line x1="0" y1="0" x2="500" y2="50" stroke="black"/>
<text x="250" y="25">Easy-peasy</text>
<text x="250" y="155" font-family="sans-serif" font-size="25" fill="gray">Easy-peasy</text>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>testD3-6-SVG.html</title>
  6. <script type="text/javascript" src="http://d3js.org/d3.v5.min.js"></script>
  7. <style type="text/css">
  8. .pumpkin {
  9. fill: yellow;
  10. stroke: orange;
  11. stroke-width: 5;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="text/javascript"></script>
  17. <svg width=500 height=960>
  18. <rect x="0" y="0" width="500" height="50"/>
  19. <ellipse cx="250" cy="225" rx="100" ry="25"/>
  20. <line x1="0" y1="120" x2="500" y2="50" stroke="black"/>
  21. <text x="250" y="155" font-family="sans-serif"
  22. font-size="25" fill="gray">Easy-peasy</text>
  23. <circle cx="25" cy="80" r="20"
  24. fill="rgba(128, 0, 128, 0.75)"
  25. stroke="rgba(0, 255, 0, 0.25)"
  26. stroke-width="100"/>
  27. <circle cx="75" cy="80" r="20"
  28. fill="rgba(0, 255, 0, 0.75)"
  29. stroke="rgba(0, 0, 255, 0.25)" stroke-width="10"/>
  30. <circle cx="125" cy="80" r="20"
  31. fill="rgba(255, 255, 0, 0.75)"
  32. stroke="rgba(255, 0, 0, 0.25)" stroke-width="10"/>
  33. <rect x="0" y="300" width="30" height="30" fill="purple"/>
  34. <rect x="20" y="305" width="30" height="30" fill="blue"/>
  35. <rect x="40" y="310" width="30" height="30" fill="green"/>
  36. <rect x="60" y="315" width="30" height="30" fill="yellow"/>
  37. <rect x="80" y="320" width="30" height="30" fill="red"/>
  38. <circle cx="25" cy="425" r="22" class="pumpkin"/>
  39. <circle cx="25" cy="525" r="20" fill="rgba(128, 0, 128, 1.0)"/>
  40. <circle cx="50" cy="525" r="20" fill="rgba(0, 0, 255, 0.75)"/>
  41. <circle cx="75" cy="525" r="20" fill="rgba(0, 255, 0, 0.5)"/>
  42. <circle cx="100" cy="525" r="20" fill="rgba(255, 255, 0, 0.25)"/>
  43. <circle cx="125" cy="525" r="20" fill="rgba(255, 0, 0, 0.1)"/>
  44. <circle cx="25" cy="625" r="20" fill="purple"
  45. stroke="green" stroke-width="10"
  46. opacity="0.9"/>
  47. <circle cx="65" cy="625" r="20" fill="green"
  48. stroke="blue" stroke-width="10"
  49. opacity="0.5"/>
  50. <circle cx="105" cy="625" r="20" fill="yellow"
  51. stroke="red" stroke-width="10"
  52. opacity="0.1"/>
  53. </svg>
  54. </body>
  55. </html>
HTML

教材第180页

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>testD3-9-drawScatterLot.html</title>
  6. <script type="text/javascript" src="http://d3js.org/d3.v5.min.js"></script>
  7. <style type="text/css">
  8. </style>
  9. </head>
  10. <body>
  11. <script type="text/javascript">
  12. //Width and height
  13. var w = 600;
  14. var h = 100;
  15. var dataset = [
  16. [5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
  17. [410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
  18. ];
  19. //Create SVG element
  20. var svg = d3.select("body")
  21. .append("svg")
  22. .attr("width", w)
  23. .attr("height", h);
  24. svg.selectAll("circle")
  25. .data(dataset)
  26. .enter()
  27. .append("circle")
  28. .attr("cx", function(d) {
  29. return d[0];
  30. })
  31. .attr("cy", function(d) {
  32. return d[1];
  33. })
  34. .attr("r", function(d) {
  35. return Math.sqrt(h - d[1]);
  36. });
  37. svg.selectAll("text")
  38. .data(dataset)
  39. .enter()
  40. .append("text")
  41. .text(function(d) {
  42. return d[0] + "," + d[1];
  43. })
  44. .attr("x", function(d) {
  45. return d[0];
  46. })
  47. .attr("y", function(d) {
  48. return d[1];
  49. })
  50. .attr("font-family", "sans-serif")
  51. .attr("font-size", "11px")
  52. .attr("fill", "red");
  53. </script>
  54. </body>
  55. </html>
HTML

教材第182页

  1. <!DOCTYPE html>
  2. <html>
  3. <header>
  4. <meta charset="utf-8">
  5. <!-- 引入 ECharts 文件 -->
  6. <script src="echarts.js"></script>
  7. </header>
  8. </html>
HTML

教材第183页

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>ECharts</title>
  6. <!-- 引入 echarts.js -->
  7. <script src="echarts.js"></script>
  8. </head>
  9. <body>
  10. <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
  11. <div id="main" style="width: 600px;height:400px;"></div>
  12. <script type="text/javascript">
  13. // 基于准备好的dom,初始化echarts实例
  14. var myChart = echarts.init(document.getElementById('main'));
  15. // 指定图表的配置项和数据
  16. var option = {
  17. title: {
  18. text: 'ECharts 入门示例'
  19. },
  20. tooltip: {},
  21. legend: {
  22. data:['销量']
  23. },
  24. xAxis: {
  25. data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
  26. },
  27. yAxis: {},
  28. series: [{
  29. name: '销量',
  30. type: 'bar',
  31. data: [5, 20, 36, 10, 10, 20]
  32. }]
  33. };
  34. // 使用刚指定的配置项和数据显示图表。
  35. myChart.setOption(option);
  36. </script>
  37. </body>
  38. </html>
HTML

教材第184页

toolbox: {
        show : true,
        feature : {
            dataZoom: {},
            dataView: {readOnly: false},
            magicType: {type: ['line', 'bar']},
            restore: {},
            saveAsImage: {}
        }
}