Advertisement
NTahmid

Data_Range_v2_bar

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