NTahmid

Untitled

Feb 8th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. ```<!DOCTYPE html>
  2. <meta charset="utf-8">
  3.  
  4. <style>
  5. .axis line{
  6. visibility:hidden;
  7. }
  8.  
  9. .axis .domain {
  10. display: none;
  11. }
  12.  
  13. .axis {
  14. font: 13px sans-serif;
  15. }
  16.  
  17. .yUnits {
  18. font: 14px sans-serif;
  19. }
  20.  
  21. .caption {
  22. font: 12px sans-serif;
  23. }
  24.  
  25. .chartDisplayTitle{
  26. fill:#BC151E;
  27. font-weight: bold;
  28. font: 20px sans-serif;
  29. }
  30. </style>
  31.  
  32. <svg class="chart" width="960" height="590" aria-labelledby="graph-title" aria-describedby="graph-desc">
  33. <title>GDP Growth Remains Broad Based</title>
  34. <desc id="graph-desc">GDP Growth Remains Broad Based, with values for 2017 quarters 1-3.</desc>
  35. <text transform="translate(10, 20)" class="chartDisplayTitle">Chart1</text>
  36. <text id="graph-title" transform="translate(10, 45)" class="chartDisplayTitle">GDP Growth Remains Broad Based</text>
  37. <text transform="translate(10, 70)" class="yUnits">Percentage points*</text>
  38. <text transform="translate(10, 570)" class="caption">*Contribution to total gross domestic product (GDP) growth; seasonally adjusted annualized rate.</text>
  39. <text transform="translate(10, 585)" class="caption">SOURCE: Bureau of Economic Analysis.</text>
  40. </svg>
  41. <script src="https://d3js.org/d3.v4.min.js"></script>
  42. <script>
  43.  
  44. var econ2 = [
  45. {
  46. "Category": "GDP",
  47. "2017 Q1": 1.2,
  48. "2017 Q2": 3.1,
  49. "2017 Q3 First Estimate": 3.0
  50. },
  51. {
  52. "Category": "Consumption",
  53. "2017 Q1": 1.3,
  54. "2017 Q2": 2.2,
  55. "2017 Q3 First Estimate": 1.6
  56.  
  57. },
  58. {
  59. "Category": "Nonresidential investment",
  60. "2017 Q1": 0.9,
  61. "2017 Q2": 0.8,
  62. "2017 Q3 First Estimate": 0.5
  63.  
  64. },
  65. {
  66. "Category": "Residential investment",
  67. "2017 Q1": 0.4,
  68. "2017 Q2": -0.3,
  69. "2017 Q3 First Estimate": -0.2
  70. },
  71. {
  72. "Category": "Inventories",
  73. "2017 Q1": -1.5,
  74. "2017 Q2": 0.1,
  75. "2017 Q3 First Estimate": 0.7
  76.  
  77. },
  78. {
  79. "Category": "Net exports",
  80. "2017 Q1": 0.2,
  81. "2017 Q2": 0.2,
  82. "2017 Q3 First Estimate": 0.4
  83.  
  84. },
  85. {
  86. "Category": "Government",
  87. "2017 Q1": -0.1,
  88. "2017 Q2": 0.0,
  89. "2017 Q3 First Estimate": 0.0
  90.  
  91. }
  92. ]
  93.  
  94. var svg = d3.select("svg"),
  95. margin = {top: 80, right: 10, bottom: 80, left: 25},
  96. width = svg.attr("width") - margin.left - margin.right,
  97. height = svg.attr("height") - margin.top - margin.bottom,
  98. g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  99.  
  100. var y = d3.scaleLinear()
  101. .domain([-2, 4])
  102. .range([height, 0]);
  103.  
  104. var x0 = d3.scaleBand()
  105. .rangeRound([0, width])
  106. .paddingInner(0.1)
  107. .paddingOuter(0.1);
  108.  
  109. var x1 = d3.scaleBand()
  110. .paddingOuter(0.25)
  111. .paddingInner(0.15);
  112.  
  113. var z = d3.scaleOrdinal()
  114. .range(["#BC151E", "#D3B178", "#354B5F"]);
  115.  
  116. const yAxis = d3.axisLeft(y).ticks(7);
  117.  
  118. var subCategories = Object.keys(econ2[0]).slice(1);
  119.  
  120. x0.domain(econ2.map( d => d.Category ));
  121.  
  122. x1.domain(subCategories).rangeRound([0, x0.bandwidth()])
  123.  
  124. var selection = g.selectAll("g")
  125. .data(econ2)
  126. .enter().append("g")
  127. .attr("transform", d => "translate(" + x0(d.Category) + ",0)" )
  128. selection.selectAll("rect")
  129. .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  130. .enter().append("rect")
  131. .attr("x", d => x1(d.key) )
  132. .attr("y", d => (d.value<0 ? y(0) : y(d.value)) )
  133. .attr("width", x1.bandwidth())
  134. .attr("height", d => Math.abs(y(d.value) - y(0)) )
  135. .attr("fill", d => z(d.key) )
  136. selection.selectAll("text")
  137. .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  138. .enter().append("text")
  139. .attr("x", d => x1(d.key) )
  140. .attr("y", d => d.value<=0 ? y(0) - (y(4) - (Math.abs(y(d.value) - y(0)) + 20)) : y(d.value) - 10)
  141. .style('fill', d => z(d.key))
  142. .style('font-size', '1.25em')
  143. .text(d => Number.parseFloat(d.value).toFixed(1))
  144.  
  145. g.append("g")
  146. .attr("class", "axis")
  147. .attr("transform", "translate(0," + height + ")")
  148. .call(d3.axisBottom(x0))
  149. .selectAll(".tick text")
  150. .call(wrap, x0.bandwidth());
  151.  
  152. g.append('g')
  153. .call(yAxis)
  154.  
  155. g.append("line")
  156. .attr("y1", y(0))
  157. .attr("y2", y(0))
  158. .attr("x1", 0)
  159. .attr("x2", width)
  160. .attr("stroke", "black");
  161.  
  162. var legend = g.append("g")
  163. .attr("font-family", "sans-serif")
  164. .attr("font-size", 13)
  165. .attr("text-anchor", "end")
  166. .selectAll("g")
  167. .data(subCategories)
  168. .enter().append("g")
  169. .attr("transform", function(d, i) { return "translate(0," + i * 24 + ")"; });
  170. legend.append("rect")
  171. .attr("x", width - 142)
  172. .attr("width", 8)
  173. .attr("height", 8)
  174. .attr("fill", z);
  175. legend.append("text")
  176. .attr("x", d => d.length > 7 ? (width + 5) : (width - 80))
  177. .attr("y", 5.5)
  178. .attr("dy", "0.22em")
  179. .text(d => (d));
  180.  
  181. function wrap(text, width) {
  182. text.each(function() {
  183. var text = d3.select(this),
  184. words = text.text().split(/\s+/).reverse(),
  185. word,
  186. line = [],
  187. lineNumber = 0,
  188. lineHeight = 1.1,
  189. y = text.attr("y"),
  190. dy = parseFloat(text.attr("dy")),
  191. tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  192. while (word = words.pop()) {
  193. line.push(word);
  194. tspan.text(line.join(" "));
  195. if (tspan.node().getComputedTextLength() > width) {
  196. line.pop();
  197. tspan.text(line.join(" "));
  198. line = [word];
  199. tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  200. }
  201. }
  202. });
  203. }
  204.  
  205. </script>```
  206.  
Add Comment
Please, Sign In to add comment