Advertisement
NTahmid

Descriptive Statistics

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