web.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!--
  2. <html>
  3. <body>
  4. sdasdasd
  5. </body>
  6. </html>
  7. -->
  8. <meta charset="utf-8">
  9. <html>
  10. <header><title>This is title</title></header>
  11. <style type="text/css">
  12. /* 13. Basic Styling with CSS */
  13. /* Style the lines by removing the fill and applying a stroke */
  14. .line {
  15. fill: none;
  16. stroke: #ffab00;
  17. stroke-width: 3;
  18. }
  19. .overlay {
  20. fill: none;
  21. pointer-events: all;
  22. }
  23. /* Style the dots by assigning a fill and stroke */
  24. .dot {
  25. fill: #ffab00;
  26. stroke: #fff;
  27. }
  28. .focus circle {
  29. fill: none;
  30. stroke: steelblue;
  31. }
  32. .dot-focus{
  33. fill: #ffffff;
  34. stroke: steelblue;
  35. }
  36. </style>
  37. <body>
  38. <script src="d3.v5.min.js"></script>
  39. <script>
  40. var margin = {top: 10, right: 50, bottom: 50, left: 50}
  41. , width = window.innerWidth - margin.left - margin.right // Use the window's width
  42. , height = window.innerHeight - margin.top - margin.bottom; // Use the window's height
  43. // The number of datapoints
  44. var n = 101;
  45. // 5. X scale will use the index of our data
  46. var xScale = d3.scaleLinear()
  47. .domain([0, n-1]) // input
  48. .range([0, width]); // output
  49. // 6. Y scale will use the randomly generate number
  50. var yScale = d3.scaleLinear()
  51. .domain([0, 1]) // input
  52. .range([height, 0]); // output
  53. // 7. d3's line generator
  54. var line = d3.line()
  55. .x(function(d, i) { return xScale(i); }) // set the x values for the line generator
  56. .y(function(d) { return yScale(d.y); }) // set the y values for the line generator
  57. .curve(d3.curveMonotoneX) // apply smoothing to the line
  58. // 8. An array of objects of length N. Each object has key -> value pair, the key being "y" and the value is a random number
  59. var dataset = d3.range(n).map(function(d) { return {"y": d3.randomUniform(1)() } })
  60. // 1. Add the SVG to the page and employ #2
  61. var svg = d3.select("body").append("svg")
  62. .attr("width", width + margin.left + margin.right -25)
  63. .attr("height", height + margin.top + margin.bottom -25)
  64. //.attr("preserveAspectRatio", "xMinYMin meet")
  65. //.attr("viewBox", "0 0 " + width + " " + height)
  66. .append("g")
  67. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  68. // 3. Call the x axis in a group tag
  69. svg.append("g")
  70. .attr("class", "x axis")
  71. .attr("transform", "translate(0," + height + ")")
  72. .call(d3.axisBottom(xScale)); // Create an axis component with d3.axisBottom
  73. // 4. Call the y axis in a group tag
  74. svg.append("g")
  75. .attr("class", "y axis")
  76. .call(d3.axisLeft(yScale)); // Create an axis component with d3.axisLeft
  77. // 9. Append the path, bind the data, and call the line generator
  78. var path = svg.append("path")
  79. .datum(dataset) // 10. Binds data to the line
  80. .attr("class", "line") // Assign a class for styling
  81. .attr("d", line); // 11. Calls the line generator
  82. // 12. Appends a circle for each datapoint
  83. svg.selectAll(".dot")
  84. .data(dataset)
  85. .enter().append("circle") // Uses the enter().append() method
  86. .attr("class", "dot") // Assign a class for styling
  87. .attr("cx", function(d, i) { return xScale(i) })
  88. .attr("cy", function(d) { return yScale(d.y) })
  89. .attr("r", 5)
  90. .on("mouseover", function(a, b, c) {
  91. console.log(a)
  92. d3.select(this).attr('class', 'dot-focus')
  93. })
  94. .on("mouseout", function(a, b, c) {
  95. console.log(a)
  96. d3.select(this).attr('class', 'dot')
  97. })
  98. function redraw(){
  99. width = window.innerWidth - margin.left - margin.right;
  100. height = window.innerHeight - margin.top - margin.bottom;
  101. console.log("redraw w:" + width + " h:" + height);
  102. //path.attr("d", line)
  103. //.attr("transform", null)
  104. //.transition()
  105. //.attr("transform", "translate(" + "0" + ",100)")
  106. //.duration(300);
  107. }
  108. window.addEventListener("resize", redraw);
  109. </script>
  110. </body>
  111. </html>