Advertisement
NTahmid

Complex Trends 4

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