Advertisement
NTahmid

Pointwise Comparison

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