Advertisement
NTahmid

dsds

Mar 28th, 2024
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 7.35 KB | None | 0 0
  1. ```<!DOCTYPE html>
  2. <meta charset="utf-8">
  3.  
  4. <style>
  5. /*hides tick marks on bottom xaxis */
  6. .axis line{
  7.       visibility:hidden;
  8.     }
  9.  
  10. /* hides bottom xaxis line*/
  11. .axis .domain {
  12.   display: none;
  13. }
  14.  
  15. .axis {
  16.       font: 13px sans-serif;
  17.     }
  18.  
  19. .yUnits {
  20.   font: 14px sans-serif;
  21. }
  22.  
  23. .caption {
  24.   font: 12px sans-serif;
  25. }
  26.  
  27. .chartDisplayTitle{
  28.   fill:#354B5F;
  29.   font-weight: bold;
  30.   font: 20px sans-serif;
  31. }
  32.  
  33. .annotation-group {
  34.   fill: #c86984;
  35.   stroke: #c86984;
  36. }
  37.  
  38. .annotation-note-label {
  39.   fill: #c86984;
  40. }
  41.  
  42. .annotation-note-title {
  43.   fill: #c86984;
  44. }
  45. </style>
  46.  
  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://d3-annotation.s3.amazonaws.com/d3-annotation.min.js"></script>
  60. <script>
  61.  
  62. var econ2 = [
  63.   {
  64.     "Category": "GDP",
  65.     "2017 Q1": 1.2,
  66.     "2017 Q2": 3.1,
  67.     "2017 Q3 First Estimate": 3.0
  68.   },
  69.   {
  70.     "Category": "Consumption",
  71.     "2017 Q1": 1.3,
  72.     "2017 Q2": 2.2,
  73.     "2017 Q3 First Estimate": 1.6
  74.   },
  75.   {
  76.     "Category": "Nonresidential investment",
  77.     "2017 Q1": 0.9,
  78.     "2017 Q2": 0.8,
  79.     "2017 Q3 First Estimate": 0.5
  80.   },
  81.   {
  82.     "Category": "Residential investment",
  83.     "2017 Q1": 0.4,
  84.     "2017 Q2": -0.3,
  85.     "2017 Q3 First Estimate": -0.2
  86.   },
  87.   {
  88.     "Category": "Inventories",
  89.     "2017 Q1": -1.5,
  90.     "2017 Q2": 0.1,
  91.     "2017 Q3 First Estimate": 0.7
  92.   },
  93.   {
  94.     "Category": "Net exports",
  95.     "2017 Q1": 0.2,
  96.     "2017 Q2": 0.2,
  97.     "2017 Q3 First Estimate": 0.4
  98.   },
  99.   {
  100.     "Category": "Government",
  101.     "2017 Q1": -0.1,
  102.     "2017 Q2": 0.0,
  103.     "2017 Q3 First Estimate": 0.0
  104.   }
  105. ]
  106.  
  107. var svg = d3.select("svg"),
  108.     margin = {top: 80, right: 10, bottom: 80, left: 25},
  109.     width = svg.attr("width") - margin.left - margin.right,
  110.     height = svg.attr("height") - margin.top - margin.bottom,
  111.     g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  112.  
  113. var y = d3.scaleLinear()
  114.       .domain([-2, 4])
  115.       .range([height, 0]);
  116.  
  117. var x0 = d3.scaleBand()
  118.       .rangeRound([0, width])
  119.       .paddingInner(0.1)
  120.       .paddingOuter(0.1);
  121.  
  122. var x1 = d3.scaleBand()
  123.     .paddingOuter(0.25)
  124.     .paddingInner(0.15);
  125.  
  126. var z = d3.scaleOrdinal()
  127.         .range(["#BC151E", "#D3B178", "#354B5F"]);
  128.  
  129. const yAxis = d3.axisLeft(y).ticks(7);
  130.  
  131. var subCategories = Object.keys(econ2[0]).slice(1);
  132.  
  133. x0.domain(econ2.map( d =>  d.Category ));
  134.  
  135. x1.domain(subCategories).rangeRound([0, x0.bandwidth()])
  136.  
  137. var selection = g.selectAll("g")
  138.     .data(econ2)
  139.     .enter().append("g")
  140.       .attr("transform", d => "translate(" + x0(d.Category) + ",0)" )
  141.     selection.selectAll("rect")
  142.      .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  143.       .enter().append("rect")
  144.       .attr("x", d => x1(d.key) )
  145.       .attr("y", d => (d.value<0 ? y(0) : y(d.value)) )
  146.      .attr("width", x1.bandwidth())
  147.      .attr("height", d => Math.abs(y(d.value) - y(0)) )
  148.       .attr("fill", d => z(d.key) )
  149.     selection.selectAll("text")
  150.        .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  151.         .enter().append("text")
  152.         .attr("x", d => x1(d.key) )
  153.         .attr("y", d => d.value<=0 ? y(0) - (y(4) - (Math.abs(y(d.value) - y(0)) + 20)) : y(d.value) - 10)
  154.        .style('fill', d => z(d.key))
  155.         .style('font-size', '1.25em')
  156.         .text(d => Number.parseFloat(d.value).toFixed(1))
  157.  
  158. g.append("g")
  159.     .attr("class", "axis")
  160.     .attr("transform", "translate(0," + height + ")")
  161.     .call(d3.axisBottom(x0))
  162.     .selectAll(".tick text")
  163.     .call(wrap, x0.bandwidth());
  164.  
  165. g.append('g')
  166. .call(yAxis)
  167.  
  168. g.append("line")
  169.     .attr("y1", y(0))
  170.     .attr("y2", y(0))
  171.     .attr("x1", 0)
  172.     .attr("x2", width)
  173.     .attr("stroke", "black");
  174.  
  175. var legend = g.append("g")
  176.       .attr("font-family", "sans-serif")
  177.       .attr("font-size", 13)
  178.       .attr("text-anchor", "end")
  179.     .selectAll("g")
  180.     .data(subCategories)
  181.     .enter().append("g")
  182.       .attr("transform", function(d, i) { return "translate(0," + i * 24 + ")"; });
  183.   legend.append("rect")
  184.       .attr("x", width - 142)
  185.       .attr("width", 8)
  186.       .attr("height", 8)
  187.       .attr("fill", z);
  188.   legend.append("text")
  189.           .attr("x", d => d.length > 7 ? (width + 5) : (width - 80))
  190.           .attr("y", 5.5)
  191.           .attr("dy", "0.22em")
  192.           .text(d => (d));
  193.  
  194. function wrap(text, width) {
  195.             text.each(function() {
  196.               var text = d3.select(this),
  197.                   words = text.text().split(/\s+/).reverse(),
  198.                   word,
  199.                   line = [],
  200.                   lineNumber = 0,
  201.                   lineHeight = 1.1, // ems
  202.                   y = text.attr("y"),
  203.                   dy = parseFloat(text.attr("dy")),
  204.                   tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  205.               while (word = words.pop()) {
  206.                 line.push(word);
  207.                 tspan.text(line.join(" "));
  208.                 if (tspan.node().getComputedTextLength() > width) {
  209.                   line.pop();
  210.                   tspan.text(line.join(" "));
  211.                   line = [word];
  212.                   tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  213.                 }
  214.               }
  215.             });
  216.           }
  217.  
  218. // Annotations
  219. const annotations = [
  220.   {
  221.     note: {
  222.       label: "Significant growth in Q2 and Q3",
  223.       title: "GDP Growth",
  224.       wrap: 200
  225.     },
  226.     x: x0("GDP") + x1("2017 Q2") + x1.bandwidth() / 2,
  227.     y: y(3.1),
  228.     dy: -30,
  229.     dx: -60,
  230.     color: "#c86984"
  231.   },
  232.   {
  233.     note: {
  234.       label: "Decrease in residential investment in Q2 and Q3",
  235.       title: "Investment Drop",
  236.       wrap: 200
  237.     },
  238.     x: x0("Residential investment") + x1("2017 Q2") + x1.bandwidth() / 2,
  239.     y: y(-0.3),
  240.     dy: 30,
  241.     dx: -60,
  242.     color: "#c86984"
  243.   }
  244. ];
  245.  
  246. const makeAnnotations = d3.annotation()
  247.   .annotations(annotations)
  248.   .type(d3.annotationCalloutElbow)
  249.   .accessors({
  250.     x: d => d.x,
  251.     y: d => d.y
  252.   })
  253.   .accessorsInverse({
  254.     x: d => d.x,
  255.     y: d => d.y
  256.   })
  257.   .on('subjectover', function(annotation) {
  258.     annotation.type.a.selectAll("g.annotation-connector, g.annotation-note")
  259.       .classed("hidden", false);
  260.   })
  261.   .on('subjectout', function(annotation) {
  262.     annotation.type.a.selectAll("g.annotation-connector, g.annotation-note")
  263.       .classed("hidden", true);
  264.   });
  265.  
  266. g.append("g")
  267.   .attr("class", "annotation-group")
  268.   .call(makeAnnotations);
  269.  
  270. </script>
  271. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement