Advertisement
NTahmid

Title_v2_bar

Feb 22nd, 2024
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.17 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3.  
  4. <style>
  5.  
  6. /*hides tick marks on bottom xaxis */
  7. .axis line{
  8.       visibility:hidden;
  9.     }
  10.  
  11. /* hides bottom xaxis line*/
  12. .axis .domain {
  13.   display: none;
  14. }
  15.  
  16. .axis {
  17.       font: 13px sans-serif;
  18.     }
  19.  
  20.   .yUnits {
  21.     font: 14px sans-serif;
  22.   }
  23.  
  24.   .caption {
  25.     font: 12px sans-serif;
  26.   }
  27.  
  28. .chartDisplayTitle{
  29.   fill:#c86984;
  30.   font-weight: bold;
  31.   font: 20px sans-serif;
  32. }
  33. </style>
  34.  
  35. <svg class="chart" width="960" height="590" aria-labelledby="graph-title" aria-describedby="graph-desc">
  36.     <title>GDP Growth Remains Broad Based</title>
  37.     <desc id="graph-desc">GDP Growth Remains Broad Based, with values for 2017 quarters 1-3.</desc>
  38.     <text transform="translate(10, 20)" class="chartDisplayTitle">Chart1</text>
  39.     <text id="graph-title" transform="translate(10, 45)" class="chartDisplayTitle">GDP Growth Remains Broad Based</text>
  40.     <text transform="translate(10, 70)" class="yUnits">Percentage points*</text>
  41.     <text transform="translate(10, 570)" class="caption">*Contribution to total gross domestic product (GDP) growth; seasonally adjusted annualized rate.</text>
  42.     <text transform="translate(10, 585)" class="caption">SOURCE: Bureau of Economic Analysis.</text>
  43.     <rect x="0" y="30" width="300" height="30" fill="none" stroke="#c86984" stroke-width="2"></rect>
  44. </svg>
  45. <script src="https://d3js.org/d3.v4.min.js"></script>
  46. <script>
  47.  
  48. const regression = d3.regressionLinear()
  49.   .x(d => d.x)
  50.   .y(d => d.y)
  51.   .domain([0, 100]);
  52.  
  53. </script>
  54. <script>
  55.  
  56. var econ2 = [
  57.   {
  58.     "Category": "GDP",
  59.     "2017 Q1": 1.2,
  60.     "2017 Q2": 3.1,
  61.     "2017 Q3 First Estimate": 3.0
  62.   },
  63.   {
  64.     "Category": "Consumption",
  65.     "2017 Q1": 1.3,
  66.     "2017 Q2": 2.2,
  67.     "2017 Q3 First Estimate": 1.6
  68.  
  69.   },
  70.   {
  71.     "Category": "Nonresidential investment",
  72.     "2017 Q1": 0.9,
  73.     "2017 Q2": 0.8,
  74.     "2017 Q3 First Estimate": 0.5
  75.  
  76.   },
  77.   {
  78.     "Category": "Residential investment",
  79.     "2017 Q1": 0.4,
  80.     "2017 Q2": -0.3,
  81.     "2017 Q3 First Estimate": -0.2
  82.   },
  83.   {
  84.     "Category": "Inventories",
  85.     "2017 Q1": -1.5,
  86.     "2017 Q2": 0.1,
  87.     "2017 Q3 First Estimate": 0.7
  88.  
  89.   },
  90.   {
  91.     "Category": "Net exports",
  92.     "2017 Q1": 0.2,
  93.     "2017 Q2": 0.2,
  94.     "2017 Q3 First Estimate": 0.4
  95.  
  96.   },
  97.   {
  98.     "Category": "Government",
  99.     "2017 Q1": -0.1,
  100.     "2017 Q2": 0.0,
  101.     "2017 Q3 First Estimate": 0.0
  102.  
  103.   }
  104. ]
  105.  
  106. var svg = d3.select("svg"),
  107.     margin = {top: 80, right: 10, bottom: 80, left: 25},
  108.     width = svg.attr("width") - margin.left - margin.right,
  109.     height = svg.attr("height") - margin.top - margin.bottom,
  110.     g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  111.  
  112. var y = d3.scaleLinear()
  113.       .domain([-2, 4])
  114.       .range([height, 0]);
  115.  
  116. var x0 = d3.scaleBand()  
  117.       .rangeRound([0, width])
  118.       .paddingInner(0.1)
  119.       .paddingOuter(0.1);
  120.  
  121. var x1 = d3.scaleBand()  
  122.     .paddingOuter(0.25)
  123.     .paddingInner(0.15);
  124.  
  125. var z = d3.scaleOrdinal()
  126.         .range(["#BC151E", "#D3B178", "#354B5F"]);
  127.  
  128. const yAxis = d3.axisLeft(y).ticks(7);
  129.  
  130. var subCategories = Object.keys(econ2[0]).slice(1);
  131.  
  132. x0.domain(econ2.map( d =>  d.Category ));
  133.  
  134. x1.domain(subCategories).rangeRound([0, x0.bandwidth()])
  135.  
  136.   var selection = g.selectAll("g")
  137.     .data(econ2)
  138.     .enter().append("g")
  139.       .attr("transform", d => "translate(" + x0(d.Category) + ",0)" )
  140.     selection.selectAll("rect")
  141.      .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  142.       .enter().append("rect")
  143.       .attr("x", d => x1(d.key) )
  144.       .attr("y", d => (d.value<0 ? y(0) : y(d.value)) )
  145.      .attr("width", x1.bandwidth())
  146.      .attr("height", d => Math.abs(y(d.value) - y(0)) )
  147.       .attr("fill", d => z(d.key) )
  148.     selection.selectAll("text")
  149.        .data(function(d) { return subCategories.map(function(key) { return {key: key, value: d[key]}; }); })
  150.         .enter().append("text")
  151.         .attr("x", d => x1(d.key) )
  152.         .attr("y", d => d.value<=0 ? y(0) - (y(4) - (Math.abs(y(d.value) - y(0)) + 20)) : y(d.value) - 10)
  153.        .style('fill', d => z(d.key))
  154.         .style('font-size', '1.25em')
  155.         .text(d => Number.parseFloat(d.value).toFixed(1))
  156.  
  157. g.append("g")
  158.     .attr("class", "axis")
  159.     .attr("transform", "translate(0," + height + ")")
  160.     .call(d3.axisBottom(x0))
  161.     .selectAll(".tick text")
  162.     .call(wrap, x0.bandwidth());
  163.  
  164. g.append('g')
  165. .call(yAxis)
  166.  
  167. g.append("line")
  168.     .attr("y1", y(0))
  169.     .attr("y2", y(0))
  170.     .attr("x1", 0)
  171.     .attr("x2", width)
  172.     .attr("stroke", "black");
  173.  
  174. var legend = g.append("g")
  175.       .attr("font-family", "sans-serif")
  176.       .attr("font-size", 13)
  177.       .attr("text-anchor", "end")
  178.     .selectAll("g")
  179.     .data(subCategories)
  180.     .enter().append("g")
  181.       .attr("transform", function(d, i) { return "translate(0," + i * 24 + ")"; });
  182.   legend.append("rect")
  183.       .attr("x", width - 142)
  184.       .attr("width", 8)
  185.       .attr("height", 8)
  186.       .attr("fill", z);
  187.   legend.append("text")
  188.           .attr("x", d => d.length > 7 ? (width + 5) : (width - 80))
  189.           .attr("y", 5.5)
  190.           .attr("dy", "0.22em")
  191.           .text(d => (d));
  192.  
  193.   function wrap(text, width) {
  194.             text.each(function() {
  195.               var text = d3.select(this),
  196.                   words = text.text().split(/\s+/).reverse(),
  197.                   word,
  198.                   line = [],
  199.                   lineNumber = 0,
  200.                   lineHeight = 1.1, // ems
  201.                   y = text.attr("y"),
  202.                   dy = parseFloat(text.attr("dy")),
  203.                   tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  204.               while (word = words.pop()) {
  205.                 line.push(word);
  206.                 tspan.text(line.join(" "));
  207.                 if (tspan.node().getComputedTextLength() > width) {
  208.                   line.pop();
  209.                   tspan.text(line.join(" "));
  210.                   line = [word];
  211.                   tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  212.                 }
  213.               }
  214.             });
  215.           }
  216.  
  217. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement