Advertisement
NTahmid

Untitled

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