Advertisement
NTahmid

Untitled

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