Advertisement
NTahmid

outlier_v2_final

Feb 21st, 2024
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.15 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. .outlier {
  32.   stroke: #c86984;
  33.   stroke-width: 2px;
  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. const regression = d3.regressionLinear()
  50.   .x(d => d.x)
  51.   .y(d => d.y)
  52.   .domain([0, 100]);
  53.  
  54. </script>
  55. <script>
  56.  
  57. var econ2 = [
  58.   {
  59.     "Category": "GDP",
  60.     "2017 Q1": 1.2,
  61.     "2017 Q2": 3.1,
  62.     "2017 Q3 First Estimate": 3.0
  63.   },
  64.   {
  65.     "Category": "Consumption",
  66.     "2017 Q1": 1.3,
  67.     "2017 Q2": 2.2,
  68.     "2017 Q3 First Estimate": 1.6
  69.  
  70.   },
  71.   {
  72.     "Category": "Nonresidential investment",
  73.     "2017 Q1": 0.9,
  74.     "2017 Q2": 0.8,
  75.     "2017 Q3 First Estimate": 0.5
  76.  
  77.   },
  78.   {
  79.     "Category": "Residential investment",
  80.     "2017 Q1": 0.4,
  81.     "2017 Q2": -0.3,
  82.     "2017 Q3 First Estimate": -0.2
  83.   },
  84.   {
  85.     "Category": "Inventories",
  86.     "2017 Q1": -1.5,
  87.     "2017 Q2": 0.1,
  88.     "2017 Q3 First Estimate": 0.7
  89.  
  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.   {
  99.     "Category": "Government",
  100.     "2017 Q1": -0.1,
  101.     "2017 Q2": 0.0,
  102.     "2017 Q3 First Estimate": 0.0
  103.  
  104.   }
  105. ]
  106.  
  107. var svg = d3.select("svg"),
  108.     margin = {top: 80, right: 10, bottom: 80, left: 25},
  109.     width = svg.attr("width") - margin.left - margin.right,
  110.     height = svg.attr("height") - margin.top - margin.bottom,
  111.     g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  112.  
  113. var y = d3.scaleLinear()
  114.       .domain([-2, 4])
  115.       .range([height, 0]);
  116.  
  117. var x0 = d3.scaleBand()  
  118.       .rangeRound([0, width])
  119.       .paddingInner(0.1)
  120.       .paddingOuter(0.1);
  121.  
  122. var x1 = d3.scaleBand()  
  123.     .paddingOuter(0.25)
  124.     .paddingInner(0.15);
  125.  
  126. var z = d3.scaleOrdinal()
  127.         .range(["#BC151E", "#D3B178", "#354B5F"]);
  128.  
  129. const yAxis = d3.axisLeft(y).ticks(7);
  130.  
  131. var subCategories = Object.keys(econ2[0]).slice(1);
  132.  
  133. x0.domain(econ2.map( d =>  d.Category ));
  134.  
  135. x1.domain(subCategories).rangeRound([0, x0.bandwidth()])
  136.  
  137. var selection = g.selectAll("g")
  138.     .data(econ2)
  139.     .enter().append("g")
  140.       .attr("transform", d => "translate(" + x0(d.Category) + ",0)" )
  141.     selection.selectAll("rect")
  142.      .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  143.       .enter().append("rect")
  144.       .attr("x", d => x1(d.key) )
  145.       .attr("y", d => (d.value<0 ? y(0) : y(d.value)) )
  146.      .attr("width", x1.bandwidth())
  147.      .attr("height", d => Math.abs(y(d.value) - y(0)) )
  148.       .attr("fill", d => z(d.key) )
  149.       .classed("outlier", d => d.value > 4 || d.value < -1) // Add class to outliers
  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, // ems
  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. </script>```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement