Advertisement
NTahmid

Complex Trends 2

Feb 7th, 2024
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 8.49 KB | None | 0 0
  1. Here is the modified HTML code with annotations to highlight the complex trends in the data:
  2.  
  3.  
  4. ```html
  5. <!DOCTYPE html>
  6. <meta charset="utf-8">
  7.  
  8.  
  9. <style>
  10. .axis line{
  11.       visibility:hidden;
  12.     }
  13.  
  14.  
  15. .axis .domain {
  16.   display: none;
  17. }
  18.  
  19.  
  20. .axis {
  21.       font: 13px sans-serif;
  22.     }
  23.  
  24.  
  25.   .yUnits {
  26.     font: 14px sans-serif;
  27.   }
  28.  
  29.  
  30.   .caption {
  31.     font: 12px sans-serif;
  32.   }
  33.  
  34.  
  35. .chartDisplayTitle{
  36.   fill:#354B5F;
  37.   font-weight: bold;
  38.   font: 20px sans-serif;
  39. }
  40.  
  41.  
  42. .annotation {
  43.   fill: red;
  44.   font: 14px sans-serif;
  45. }
  46. </style>
  47.  
  48.  
  49. <svg class="chart" width="960" height="590" aria-labelledby="graph-title" aria-describedby="graph-desc">
  50.     <title>GDP Growth Remains Broad Based</title>
  51.     <desc id="graph-desc">GDP Growth Remains Broad Based, with values for 2017 quarters 1-3.</desc>
  52.     <text transform="translate(10, 20)" class="chartDisplayTitle">Chart1</text>
  53.     <text id="graph-title" transform="translate(10, 45)" class="chartDisplayTitle">GDP Growth Remains Broad Based</text>
  54.     <text transform="translate(10, 70)" class="yUnits">Percentage points*</text>
  55.     <text transform="translate(10, 570)" class="caption">*Contribution to total gross domestic product (GDP) growth; seasonally adjusted annualized rate.</text>
  56.     <text transform="translate(10, 585)" class="caption">SOURCE: Bureau of Economic Analysis.</text>
  57. </svg>
  58.  
  59.  
  60. <script src="https://d3js.org/d3.v4.min.js"></script>
  61. <script>
  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.  
  108. var svg = d3.select("svg"),
  109.     margin = {top: 80, right: 10, bottom: 80, left: 25},
  110.     width = svg.attr("width") - margin.left - margin.right,
  111.     height = svg.attr("height") - margin.top - margin.bottom,
  112.     g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  113.  
  114.  
  115. var y = d3.scaleLinear()
  116.       .domain([-2, 4])
  117.       .range([height, 0]);
  118.  
  119.  
  120. var x0 = d3.scaleBand()
  121.       .rangeRound([0, width])
  122.       .paddingInner(0.1)
  123.       .paddingOuter(0.1);
  124.  
  125.  
  126. var x1 = d3.scaleBand()
  127.     .paddingOuter(0.25)
  128.     .paddingInner(0.15);
  129.  
  130.  
  131. var z = d3.scaleOrdinal()
  132.         .range(["#BC151E", "#D3B178", "#354B5F"]);
  133.  
  134.  
  135. const yAxis = d3.axisLeft(y).ticks(7);
  136.  
  137.  
  138. var subCategories = Object.keys(econ2[0]).slice(1);
  139.  
  140.  
  141. x0.domain(econ2.map( d =>  d.Category ));
  142.  
  143.  
  144. x1.domain(subCategories).rangeRound([0, x0.bandwidth()])
  145.  
  146.  
  147. var selection = g.selectAll("g")
  148.     .data(econ2)
  149.     .enter().append("g")
  150.       .attr("transform", d => "translate(" + x0(d.Category) + ",0)" )
  151.     selection.selectAll("rect")
  152.      .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  153.       .enter().append("rect")
  154.       .attr("x", d => x1(d.key) )
  155.       .attr("y", d => (d.value<0 ? y(0) : y(d.value)) )
  156.      .attr("width", x1.bandwidth())
  157.      .attr("height", d => Math.abs(y(d.value) - y(0)) )
  158.       .attr("fill", d => z(d.key) )
  159.     selection.selectAll("text")
  160.        .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  161.         .enter().append("text")
  162.         .attr("x", d => x1(d.key) )
  163.         .attr("y", d => d.value<=0 ? y(0) - (y(4) - (Math.abs(y(d.value) - y(0)) + 20)) : y(d.value) - 10)
  164.        .style('fill', d => z(d.key))
  165.         .style('font-size', '1.25em')
  166.         .text(d => Number.parseFloat(d.value).toFixed(1))
  167.  
  168.  
  169. g.append("g")
  170.     .attr("class", "axis")
  171.     .attr("transform", "translate(0," + height + ")")
  172.     .call(d3.axisBottom(x0))
  173.     .selectAll(".tick text")
  174.     .call(wrap, x0.bandwidth());
  175.  
  176.  
  177. g.append('g')
  178. .call(yAxis)
  179.  
  180.  
  181. g.append("line")
  182.     .attr("y1", y(0))
  183.     .attr("y2", y(0))
  184.     .attr("x1", 0)
  185.     .attr("x2", width)
  186.     .attr("stroke", "black");
  187.  
  188.  
  189. var legend = g.append("g")
  190.       .attr("font-family", "sans-serif")
  191.       .attr("font-size", 13)
  192.       .attr("text-anchor", "end")
  193.     .selectAll("g")
  194.     .data(subCategories)
  195.     .enter().append("g")
  196.       .attr("transform", function(d, i) { return "translate(0," + i * 24 + ")"; });
  197.   legend.append("rect")
  198.       .attr("x", width - 142)
  199.       .attr("width", 8)
  200.       .attr("height", 8)
  201.       .attr("fill", z);
  202.   legend.append("text")
  203.           .attr("x", d => d.length > 7 ? (width + 5) : (width - 80))
  204.           .attr("y", 5.5)
  205.           .attr("dy", "0.22em")
  206.           .text(d => (d));
  207.  
  208.  
  209.   function wrap(text, width) {
  210.             text.each(function() {
  211.               var text = d3.select(this),
  212.                   words = text.text().split(/\s+/).reverse(),
  213.                   word,
  214.                   line = [],
  215.                   lineNumber = 0,
  216.                   lineHeight = 1.1, // ems
  217.                   y = text.attr("y"),
  218.                   dy = parseFloat(text.attr("dy")),
  219.                   tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  220.               while (word = words.pop()) {
  221.                 line.push(word);
  222.                 tspan.text(line.join(" "));
  223.                 if (tspan.node().getComputedTextLength() > width) {
  224.                   line.pop();
  225.                   tspan.text(line.join(" "));
  226.                   line = [word];
  227.                   tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  228.                 }
  229.               }
  230.             });
  231.           }
  232.  
  233.  
  234. // Annotations
  235. g.append("line")
  236.     .attr("x1", x0("GDP") + x1("2017 Q1"))
  237.     .attr("y1", y(1.2))
  238.     .attr("x2", x0("GDP") + x1("2017 Q3 First Estimate"))
  239.     .attr("y2", y(3.0))
  240.     .attr("stroke", "red")
  241.     .attr("stroke-width", 2);
  242.  
  243.  
  244. g.append("text")
  245.     .attr("x", x0("GDP") + x1("2017 Q2"))
  246.     .attr("y", y(2.2))
  247.     .attr("class", "annotation")
  248.     .text("GDP shows a positive trend");
  249.  
  250.  
  251. g.append("circle")
  252.     .attr("cx", x0("Consumption") + x1("2017 Q1"))
  253.     .attr("cy", y(1.3))
  254.     .attr("r", 5)
  255.     .attr("fill", "red");
  256.  
  257.  
  258. g.append("text")
  259.     .attr("x", x0("Consumption") + x1("2017 Q1"))
  260.     .attr("y", y(1.3) - 10)
  261.     .attr("class", "annotation")
  262.     .text("Consumption shows a declining trend");
  263.  
  264.  
  265. g.append("circle")
  266.     .attr("cx", x0("Nonresidential investment") + x1("2017 Q1"))
  267.     .attr("cy", y(0.9))
  268.     .attr("r", 5)
  269.     .attr("fill", "red");
  270.  
  271.  
  272. g.append("text")
  273.     .attr("x", x0("Nonresidential investment") + x1("2017 Q1"))
  274.     .attr("y", y(0.9) - 10)
  275.     .attr("class", "annotation")
  276.     .text("Nonresidential investment shows a decreasing trend");
  277.  
  278.  
  279. g.append("circle")
  280.     .attr("cx", x0("Inventories") + x1("2017 Q1"))
  281.     .attr("cy", y(-1.5))
  282.     .attr("r", 5)
  283.     .attr("fill", "red");
  284.  
  285.  
  286. g.append("text")
  287.     .attr("x", x0("Inventories") + x1("2017 Q1"))
  288.     .attr("y", y(-1.5) - 10)
  289.     .attr("class", "annotation")
  290.     .text("Inventories show an increasing trend");
  291.  
  292.  
  293. g.append("circle")
  294.     .attr("cx", x0("Net exports") + x1("2017 Q1"))
  295.     .attr("cy", y(0.2))
  296.     .attr("r", 5)
  297.     .attr("fill", "red");
  298.  
  299.  
  300. g.append("text")
  301.     .attr("x", x0("Net exports") + x1("2017 Q1"))
  302.     .attr("y", y(0.2) - 10)
  303.     .attr("class", "annotation")
  304.     .text("Net exports show an increasing trend");
  305.  
  306.  
  307. g.append("circle")
  308.     .attr("cx", x0("Government") + x1("2017 Q1"))
  309.     .attr("cy", y(-0.1))
  310.     .attr("r", 5)
  311.     .attr("fill", "red");
  312.  
  313.  
  314. g.append("text")
  315.     .attr("x", x0("Government") + x1("2017 Q1"))
  316.     .attr("y", y(-0.1) - 10)
  317.     .attr("class", "annotation")
  318.     .text("Government's contribution remains almost constant");
  319. </script>
  320. ```
  321.  
  322.  
  323. This code adds annotations to the chart to highlight the complex trends in the data. The annotations include lines, circles, and text to highlight the trends in GDP, Consumption, Nonresidential investment, Inventories, Net exports, and Government's contribution. The annotations are colored red for visibility.
  324.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement