index.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>IsoBands example</title>
  6. <script src="https://d3js.org/d3.v3.min.js"></script>
  7. <script src="../../dist/marchingsquares-isolines.js"></script>
  8. </head>
  9. <body>
  10. <div id="isocontours"></div>
  11. <script>
  12. var intervals = [-5, -4.5, -4.1, -4.05, -4, -3.9, -3.8, -3.7, -3.6, -3.5, -3.4, -3.3, -3.2, -3.1, -3.0, -2.5, -2, -1.5, -1, -0.5, 0];
  13. var data = [
  14. [0.0, 0.0, 0.0, 0.0, 0.0],
  15. [0.0, -3.57, -4.12, -3.59, 0.0],
  16. [0.0, -3.64, -4.21, -3.62, 0.0],
  17. [-10.0, -3.57, -3.99, -3.79, 0.0],
  18. [0.0, -3.75, -4.11, -3.94, 0.0],
  19. [0.0, -4.0, -3.98, -4.08, 0.0],
  20. [0.0, -4.09, -4.04, 0.0, 0.0],
  21. [0.0, 0.0, 0.0, 0.0, 0.0]
  22. ];
  23. var xs = d3.range(0, data[0].length);
  24. var ys = d3.range(0, data.length);
  25. var isoLines = [];
  26. MarchingSquaresJS
  27. .isoLines(data,
  28. intervals,
  29. {
  30. polygons: false,
  31. linearRing: false
  32. }
  33. )
  34. .forEach(function(isolines, i) {
  35. isoLines.push({
  36. "coords": isolines,
  37. "level": i + 1,
  38. "val": intervals[i]});
  39. });
  40. drawLines('#isocontours', isoLines, intervals);
  41. // helper function
  42. function drawLines(divId, lines, intervals) {
  43. var marginBottomLabel = 0;
  44. var width = 300;
  45. var height = width * (ys.length / xs.length);
  46. var xScale = d3.scale.linear()
  47. .range([0, width])
  48. .domain([Math.min.apply(null, xs), Math.max.apply(null, xs)]);
  49. var yScale = d3.scale.linear()
  50. .range([0, height])
  51. .domain([Math.min.apply(null, ys), Math.max.apply(null, ys)]);
  52. var colours = d3.scale.linear().domain([intervals[0], intervals[intervals.length - 1]])
  53. .range([d3.rgb(0, 0, 0),
  54. d3.rgb(200, 200, 200)]);
  55. var svg = d3.select(divId)
  56. .append("svg")
  57. .attr("width", width)
  58. .attr("height", height + marginBottomLabel);
  59. svg.selectAll("path")
  60. .data(lines)
  61. .enter().append("path")
  62. .style("fill", "none")
  63. .style("stroke", function (d) {
  64. return colours(d.val);
  65. })
  66. .style("stroke-width", 1)
  67. .style('opacity', 1.0)
  68. .attr("d", function (d) {
  69. var p = "";
  70. d.coords.forEach(function (aa) {
  71. p += (d3.svg.line()
  72. .x(function (dat) {
  73. return xScale(dat[0]);
  74. })
  75. .y(function (dat) {
  76. return yScale(dat[1]);
  77. })
  78. .interpolate("linear")
  79. )(aa) + "";
  80. });
  81. return p;
  82. })
  83. .on('mouseover', function () {
  84. d3.select(this)
  85. .style('stroke', d3.rgb(204, 185, 116))
  86. .style("stroke-width", 3);
  87. })
  88. .on('mouseout', function () {
  89. d3.select(this)
  90. .style('stroke', function (d1) {
  91. return colours(d1.val);
  92. })
  93. .style("stroke-width", 1);
  94. });
  95. }
  96. </script>
  97. </body>
  98. </html>