d3test.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4. body {
  5. font: 10px sans-serif;
  6. }
  7. .axis path,
  8. .axis line {
  9. fill: none;
  10. stroke: #000;
  11. shape-rendering: crispEdges;
  12. }
  13. .grid path,
  14. .grid line {
  15. fill: none;
  16. stroke: rgba(0, 0, 0, 0.25);
  17. shape-rendering: crispEdges;
  18. }
  19. .x.axis path {
  20. display: none;
  21. }
  22. .line {
  23. fill: none;
  24. stroke-width: 2.5px;
  25. }
  26. </style>
  27. <body>
  28. <script src="libs/d3.min.js"></script>
  29. <script>
  30. var data = [ { label: "Data Set 1",
  31. x: [0, 1, 2, 3, 4],
  32. y: [0, 1, 2, 3, 4] },
  33. { label: "Data Set 2",
  34. x: [0, 1, 2, 3, 4],
  35. y: [0, 1, 4, 9, 16] } ] ;
  36. var xy_chart = d3_xy_chart()
  37. .width(960)
  38. .height(500)
  39. .xlabel("X Axis")
  40. .ylabel("Y Axis") ;
  41. var svg = d3.select("body").append("svg")
  42. .datum(data)
  43. .call(xy_chart) ;
  44. function d3_xy_chart() {
  45. var width = 640,
  46. height = 480,
  47. xlabel = "X Axis Label",
  48. ylabel = "Y Axis Label" ;
  49. function chart(selection) {
  50. selection.each(function(datasets) {
  51. //
  52. // Create the plot.
  53. //
  54. var margin = {top: 20, right: 80, bottom: 30, left: 50},
  55. innerwidth = width - margin.left - margin.right,
  56. innerheight = height - margin.top - margin.bottom ;
  57. var x_scale = d3.scale.linear()
  58. .range([0, innerwidth])
  59. .domain([ d3.min(datasets, function(d) { return d3.min(d.x); }),
  60. d3.max(datasets, function(d) { return d3.max(d.x); }) ]) ;
  61. var y_scale = d3.scale.linear()
  62. .range([innerheight, 0])
  63. .domain([ d3.min(datasets, function(d) { return d3.min(d.y); }),
  64. d3.max(datasets, function(d) { return d3.max(d.y); }) ]) ;
  65. var color_scale = d3.scale.category10()
  66. .domain(d3.range(datasets.length)) ;
  67. var x_axis = d3.svg.axis()
  68. .scale(x_scale)
  69. .orient("bottom") ;
  70. var y_axis = d3.svg.axis()
  71. .scale(y_scale)
  72. .orient("left") ;
  73. var x_grid = d3.svg.axis()
  74. .scale(x_scale)
  75. .orient("bottom")
  76. .tickSize(-innerheight)
  77. .tickFormat("") ;
  78. var y_grid = d3.svg.axis()
  79. .scale(y_scale)
  80. .orient("left")
  81. .tickSize(-innerwidth)
  82. .tickFormat("") ;
  83. var draw_line = d3.svg.line()
  84. .interpolate("basis")
  85. .x(function(d) { return x_scale(d[0]); })
  86. .y(function(d) { return y_scale(d[1]); }) ;
  87. var svg = d3.select(this)
  88. .attr("width", width)
  89. .attr("height", height)
  90. .append("g")
  91. .attr("transform", "translate(" + margin.left + "," + margin.top + ")") ;
  92. svg.append("g")
  93. .attr("class", "x grid")
  94. .attr("transform", "translate(0," + innerheight + ")")
  95. .call(x_grid) ;
  96. svg.append("g")
  97. .attr("class", "y grid")
  98. .call(y_grid) ;
  99. svg.append("g")
  100. .attr("class", "x axis")
  101. .attr("transform", "translate(0," + innerheight + ")")
  102. .call(x_axis)
  103. .append("text")
  104. .attr("dy", "-.71em")
  105. .attr("x", innerwidth)
  106. .style("text-anchor", "end")
  107. .text(xlabel) ;
  108. svg.append("g")
  109. .attr("class", "y axis")
  110. .call(y_axis)
  111. .append("text")
  112. .attr("transform", "rotate(-90)")
  113. .attr("y", 6)
  114. .attr("dy", "0.71em")
  115. .style("text-anchor", "end")
  116. .text(ylabel) ;
  117. var data_lines = svg.selectAll(".d3_xy_chart_line")
  118. .data(datasets.map(function(d) {return d3.zip(d.x, d.y);}))
  119. .enter().append("g")
  120. .attr("class", ".d3_xy_chart_line") ;
  121. data_lines.append("path")
  122. .attr("class", "line")
  123. .attr("d", function(d) {return draw_line(d); })
  124. .attr("stroke", function(_, i) {return color_scale(i);}) ;
  125. data_lines.append("text")
  126. .datum(function(d, i) { return {name: datasets[i].label, final: d[d.length-1]}; })
  127. .attr("transform", function(d) {
  128. return ( "translate(" + x_scale(d.final[0]) + "," +
  129. y_scale(d.final[1]) + ")" ) ; })
  130. .attr("x", 3)
  131. .attr("dy", ".35em")
  132. .attr("fill", function(_, i) { return color_scale(i); })
  133. .text(function(d) { return d.name; }) ;
  134. }) ;
  135. }
  136. chart.width = function(value) {
  137. if (!arguments.length) return width;
  138. width = value;
  139. return chart;
  140. };
  141. chart.height = function(value) {
  142. if (!arguments.length) return height;
  143. height = value;
  144. return chart;
  145. };
  146. chart.xlabel = function(value) {
  147. if(!arguments.length) return xlabel ;
  148. xlabel = value ;
  149. return chart ;
  150. } ;
  151. chart.ylabel = function(value) {
  152. if(!arguments.length) return ylabel ;
  153. ylabel = value ;
  154. return chart ;
  155. } ;
  156. return chart;
  157. }
  158. </script>