Advertisement
NTahmid

outlier_v3_react

Feb 22nd, 2024
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.19 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.   fill: none;
  34.   stroke-width: 2px;
  35. }
  36. </style>
  37.  
  38. <svg class="chart" width="960" height="590" aria-labelledby="graph-title" aria-describedby="graph-desc">
  39.   <title>GDP Growth Remains Broad Based</title>
  40.   <desc id="graph-desc">GDP Growth Remains Broad Based, 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">GDP Growth Remains Broad Based</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.  
  48. <script src="https://d3js.org/d3.v4.min.js"></script>
  49. <script>
  50. const regression = d3.regressionLinear()
  51.   .x(d => d.x)
  52.   .y(d => d.y)
  53.   .domain([0, 100]);
  54. </script>
  55.  
  56. <script>
  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.     "Category": "Nonresidential investment",
  72.     "2017 Q1": 0.9,
  73.     "2017 Q2": 0.8,
  74.     "2017 Q3 First Estimate": 0.5
  75.   },
  76.   {
  77.     "Category": "Residential investment",
  78.     "2017 Q1": 0.4,
  79.     "2017 Q2": -0.3,
  80.     "2017 Q3 First Estimate": -0.2
  81.   },
  82.   {
  83.     "Category": "Inventories",
  84.     "2017 Q1": -1.5,
  85.     "2017 Q2": 0.1,
  86.     "2017 Q3 First Estimate": 0.7
  87.   },
  88.   {
  89.     "Category": "Net exports",
  90.     "2017 Q1": 0.2,
  91.     "2017 Q2": 0.2,
  92.     "2017 Q3 First Estimate": 0.4
  93.   },
  94.   {
  95.     "Category": "Government",
  96.     "2017 Q1": -0.1,
  97.     "2017 Q2": 0.0,
  98.     "2017 Q3 First Estimate": 0.0
  99.   }
  100. ]
  101.  
  102. var svg = d3.select("svg"),
  103.     margin = {top: 80, right: 10, bottom: 80, left: 25},
  104.     width = svg.attr("width") - margin.left - margin.right,
  105.     height = svg.attr("height") - margin.top - margin.bottom,
  106.     g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  107.  
  108. var y = d3.scaleLinear()
  109.       .domain([-2, 4])
  110.       .range([height, 0]);
  111.  
  112. var x0 = d3.scaleBand()
  113.       .rangeRound([0, width])
  114.       .paddingInner(0.1)
  115.       .paddingOuter(0.1);
  116.  
  117. var x1 = d3.scaleBand()
  118.     .paddingOuter(0.25)
  119.     .paddingInner(0.15);
  120.  
  121. var z = d3.scaleOrdinal()
  122.         .range(["#BC151E", "#D3B178", "#354B5F"]);
  123.  
  124. const yAxis = d3.axisLeft(y).ticks(7);
  125.  
  126. var subCategories = Object.keys(econ2[0]).slice(1);
  127.  
  128. x0.domain(econ2.map( d =>  d.Category ));
  129.  
  130. x1.domain(subCategories).rangeRound([0, x0.bandwidth()])
  131.  
  132. var selection = g.selectAll("g")
  133.     .data(econ2)
  134.     .enter().append("g")
  135.       .attr("transform", d => "translate(" + x0(d.Category) + ",0)" )
  136.     selection.selectAll("rect")
  137.      .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  138.       .enter().append("rect")
  139.       .attr("x", d => x1(d.key) )
  140.       .attr("y", d => (d.value<0 ? y(0) : y(d.value)) )
  141.      .attr("width", x1.bandwidth())
  142.      .attr("height", d => Math.abs(y(d.value) - y(0)) )
  143.       .attr("fill", d => z(d.key) )
  144.     selection.selectAll("text")
  145.        .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  146.         .enter().append("text")
  147.         .attr("x", d => x1(d.key) )
  148.         .attr("y", d => d.value<=0 ? y(0) - (y(4) - (Math.abs(y(d.value) - y(0)) + 20)) : y(d.value) - 10)
  149.        .style('fill', d => z(d.key))
  150.         .style('font-size', '1.25em')
  151.         .text(d => Number.parseFloat(d.value).toFixed(1))
  152.  
  153. g.append("g")
  154.     .attr("class", "axis")
  155.     .attr("transform", "translate(0," + height + ")")
  156.     .call(d3.axisBottom(x0))
  157.     .selectAll(".tick text")
  158.     .call(wrap, x0.bandwidth());
  159.  
  160. g.append('g')
  161. .call(yAxis)
  162.  
  163. g.append("line")
  164.     .attr("y1", y(0))
  165.     .attr("y2", y(0))
  166.     .attr("x1", 0)
  167.     .attr("x2", width)
  168.     .attr("stroke", "black");
  169.  
  170. var legend = g.append("g")
  171.       .attr("font-family", "sans-serif")
  172.       .attr("font-size", 13)
  173.       .attr("text-anchor", "end")
  174.     .selectAll("g")
  175.     .data(subCategories)
  176.     .enter().append("g")
  177.       .attr("transform", function(d, i) { return "translate(0," + i * 24 + ")"; });
  178.   legend.append("rect")
  179.       .attr("x", width - 142)
  180.       .attr("width", 8)
  181.       .attr("height", 8)
  182.       .attr("fill", z);
  183.   legend.append("text")
  184.           .attr("x", d => d.length > 7 ? (width + 5) : (width - 80))
  185.           .attr("y", 5.5)
  186.           .attr("dy", "0.22em")
  187.           .text(d => (d));
  188.  
  189. function wrap(text, width) {
  190.             text.each(function() {
  191.               var text = d3.select(this),
  192.                   words = text.text().split(/\s+/).reverse(),
  193.                   word,
  194.                   line = [],
  195.                   lineNumber = 0,
  196.                   lineHeight = 1.1,
  197.                   y = text.attr("y"),
  198.                   dy = parseFloat(text.attr("dy")),
  199.                   tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  200.               while (word = words.pop()) {
  201.                 line.push(word);
  202.                 tspan.text(line.join(" "));
  203.                 if (tspan.node().getComputedTextLength() > width) {
  204.                   line.pop();
  205.                   tspan.text(line.join(" "));
  206.                   line = [word];
  207.                   tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  208.                 }
  209.               }
  210.             });
  211.           }
  212.  
  213. // Highlight outlier
  214. g.append("circle")
  215.   .attr("class", "outlier")
  216.   .attr("cx", x0("Inventories") + x1("2017 Q1"))
  217.   .attr("cy", y(-1.5))
  218.   .attr("r", x1.bandwidth()/2);
  219. </script>```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement