Advertisement
NTahmid

Exception_v2

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