Advertisement
NTahmid

Data Range

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