Advertisement
NTahmid

extrema_v2_bar

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