Advertisement
NTahmid

Exception

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