<section id="add-axes-3">
    <h1>Adding axes</h1>
    <p>y axis lines</p>

    <button id="trigger">Load data</button>

    <div class="block">
        <h2>The SVG Output</h2>
        <p>Hover on bars for property title</p>
        <div id="svgOutput"></div>
    </div>

    <div class="block">
        <h2>The text output</h2>
        <div id="outputBox"></div>
    </div>
</section>

<div class="templates">
    <template id="result_box">
        <div class="results_box">
            <div class="result">
                <span class="property"></span>: <span class="count"></span>
            </div>
        </div>
    </template>
</div>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="../../js/components/add-axes-3.js"></script>
<section id="add-axes-3">
    <h1>Adding axes</h1>
    <p>y axis lines</p>

    <button id="trigger">Load data</button>

    <div class="block">
        <h2>The SVG Output</h2>
        <p>Hover on bars for property title</p>
        <div id="svgOutput"></div>
    </div>

    <div class="block">
        <h2>The text output</h2>
        <div id="outputBox"></div>
    </div>
</section>

<div class="templates">
    <template id="result_box">
        <div class="results_box">
            <div class="result">
                <span class="property"></span>: <span class="count"></span>
            </div>
        </div>
    </template>
</div>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="{{ path '/js/components/add-axes-3.js' }}"></script>
/* No context defined. */
  • Content:
    (function () {
      document.addEventListener('DOMContentLoaded', function () {
        console.log("add-axes-3 active");
    
        const button = document.querySelector('#trigger');
        const outputBox = document.querySelector('#outputBox');
        const svgOutput = document.querySelector('#svgOutput');
    
        button.addEventListener('click', function() {
          const spinnerA = document.createElement('div');
          spinnerA.classList.add('spinner');
          outputBox.appendChild(spinnerA);
    
          const spinnerB = document.createElement('div');
          spinnerB.classList.add('spinner');
          svgOutput.appendChild(spinnerB);
    
          fetch('https://my.api.mockaroo.com/chart-data.json?key=0b401b10&count=10')
            .then(response => response.json())
            .then(data => {
              outputBox.removeChild(spinnerA);
              svgOutput.removeChild(spinnerB);
    
              // d3 version of results
    
              const w = 1000;
              const h = 500;
              const padding = 40;
    
              const svg = d3.select("#svgOutput")
                .append("svg");
    
              svg.attr("viewBox", [0, 0, w, h])
                .attr("preserveAspectRatio", "xMidYMid meet");
    
              const yScale = d3.scaleLinear()
                .domain([0, 100])
                .range([h - padding, padding]);
    
              const xScale = d3.scaleLinear()
                .domain([0, data.length])
                .range([padding, w - (0.25 * padding)]);
    
              console.log(xScale(0));
              console.log(xScale(5));
              console.log(xScale(10));
    
              // yAxis - adding first so it appears below the bars?
    
              const yAxis = d3.axisRight(yScale)
                .tickSize(w - (padding * 1.25));
    
              svg.append("g")
                .attr("transform", "translate(" + padding + ", 0)")
                .call(yAxis)
                .call(g => g.selectAll(".tick:not(:first-of-type) line")
                  .attr("stroke-opacity", 0.5)
                  .attr("stroke-dasharray", "4,8"))
                  .attr("stroke", "navy")
                .call(g => g.selectAll(".tick text")
                  .attr("x", -24)
                  .attr("dy", -4));
    
              svg.selectAll("rect.bar")
                .data(data)
                .join('rect')
                  .attr('class', 'bar')
                  .attr('x', (d, i) => {
                    return i * ((w - (1.25 * padding)) / data.length) + padding;
                  })
                  .attr("y", (d) => {
                    return yScale(d.count);
                  })
                  .attr("width", () => {
                    return ((w - (1.25 * padding)) / data.length) - 1;
                  })
                  .attr("height", (d) => {
                    return yScale(0) - yScale(d.count);
                  })
                  .attr("data-count", (d) => (d.count))
                  .append('title')
                  .text((d) => d.property);
    
              svg.selectAll("text.count")
                .data(data)
                .join("text")
                  .attr('class', 'count')
                  .text((d) => (d.count))
                  .attr('x', (d, i) => {
                    return i * ((w - (1.25 * padding)) / data.length) - 4 + padding + 10;
                  })
                  .attr("y", (d) => {
                    return yScale(d.count) - 10;
                  })
                  .style('font-size', '1rem')
                  .style('font-weight', 'bold')
                  .style('font-family', 'Courier New')
                  .style('fill', 'navy');
    
    
              // xAxis
    
              const xAxis = d3.axisBottom(xScale);
    
              svg.append("g")
                .attr("transform", "translate(0, " + (h - padding) + ")")
                .call(xAxis);
    
              // text version of results
    
              data.forEach(function(item) {
                let template = document.querySelector('#result_box');
                let resultBox = document.importNode(template.content, true);
    
                resultBox.querySelector('.property').innerHTML = item.property;
                resultBox.querySelector('.count').innerHTML = item.count;
    
                outputBox.appendChild(resultBox);
              })
            })
        })
      });
    })();
    
  • URL: /components/raw/add-axes-3/add-axes-3.js
  • Filesystem Path: components/01-misc/07-add-axes-3/add-axes-3.js
  • Size: 4 KB
  • Content:
    #add-axes-3 {
      @keyframes spin {
        0% {
          transform: rotate(0);
        }
    
        100% {
          transform: rotate(180deg);
        }
      }
    
      #outputBox {
        margin: 2rem 0;
        padding: 2rem;
        border: 2px solid aliceblue;
      }
    
      .spinner {
        width: 4px;
        height: 40px;
        background-color: darkcyan;
        animation: 0.75s ease-in-out both alternate infinite spin;
      }
    
      .results_box {
        margin: 0 0 1rem 0;
        .property {
          font-weight: bold;
        }
      }
    
      #svgOutput {
        margin: 2rem 0;
      }
    
      svg {
        display: block;
        width: calc(100% - 2px - 2rem);
        margin: auto;
        height: auto;
        border: 1px solid navy;
        background-color: azure;
    
        .bar {
          fill: darkslategray;
          fill-opacity: 0.8;
        }
      }
    
      .block {
        margin: 2rem 0;
        padding: 2rem 0;
        border-top: 1px solid cadetblue;
      }
    }
    
  • URL: /components/raw/add-axes-3/add-axes-3.scss
  • Filesystem Path: components/01-misc/07-add-axes-3/add-axes-3.scss
  • Size: 832 Bytes

No notes defined.