Advertisement
NTahmid

Complex Trends 3

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