dates | Number of days between events | Cell 5 | Search

This code snippet uses the D3.js library to generate a line graph visualizing time-series data from an array of events. It sets up scales, defines a line generator, creates an SVG element, binds data, and renders axes to display the graph.

Run example

npm run import -- "display recurrence line graph"

display recurrence line graph

var D3Node = require('d3-node');

function graphDates(events) {
    var d3n = new D3Node(); // initializes D3 with container element 
    var d3 = d3n.d3;

    // set the dimensions and margins of the graph
    var margin = {top: 20, right: 20, bottom: 30, left: 50},
        width = 700 - margin.left - margin.right,
        height = 500 - margin.top - margin.bottom;

    // set the ranges
    var x = d3.scaleTime().range([0, width]);
    var y = d3.scaleLinear().range([height, 0]);

    // define the line
    var valueline = d3.line()
        .x(function (d) {
            return x(d.start);
        })
        .y(function (d) {
            return y(d.days);
        });

    // append the svg obgect to the body of the page
    // appends a 'group' element to 'svg'
    // moves the 'group' element to the top left margin
    var svg = d3n.createSVG(
        width + margin.left + margin.right,
        height + margin.top + margin.bottom)
        .append('g')
        .attr(
            'transform',
            'translate(' + margin.left + ',' + margin.top + ')');

    // Scale the range of the data
    x.domain(d3.extent(events, function (d) {
        return d.start;
    }));
    y.domain([0, d3.max(events, function (d) {
        return d.days;
    })]);

    // Add the valueline path.
    svg.append('path')
        .data([events])
        .attr('class', 'line')
        .attr('fill', 'none')
        .attr('stroke', '#000')
        .attr('d', valueline);

    // Add the X Axis
    svg.append('g')
        .attr('transform', 'translate(0,' + height + ')')
        .call(d3.axisBottom(x));

    // Add the Y Axis
    svg.append('g')
        .call(d3.axisLeft(y));

    return d3n.svgString();
};
module.exports = graphDates;

What the code could have been:

'use strict';

const D3Node = require('d3-node');

/**
 * Creates a graph of events with dates on the x-axis and days on the y-axis.
 *
 * @param {Object[]} events - An array of objects with start date and days properties.
 * @returns {string} The SVG string representing the graph.
 */
function graphDates(events) {
    // Initialize D3 with a container element
    const d3n = new D3Node();
    const d3 = d3n.d3;

    // TODO: Consider using a const for the margin object
    const margin = { top: 20, right: 20, bottom: 30, left: 50 };
    const width = 700 - margin.left - margin.right;
    const height = 500 - margin.top - margin.bottom;

    // Set the ranges for the x and y scales
    const x = d3.scaleTime().range([0, width]);
    const y = d3.scaleLinear().range([height, 0]);

    // Define the line generator
    const valueline = d3.line()
       .x((d) => x(d.start))
       .y((d) => y(d.days));

    // Create the SVG element and append it to the body
    const svgContainer = d3n.createSVG(width + margin.left + margin.right, height + margin.top + margin.bottom);
    const svg = svgContainer
       .append('g')
       .attr('transform', `translate(${margin.left}, ${margin.top})`);

    // Set the domains for the x and y scales
    x.domain(d3.extent(events, (d) => d.start));
    y.domain([0, d3.max(events, (d) => d.days)]);

    // Add the valueline path to the SVG
    svg.append('path')
       .datum(events)
       .attr('class', 'line')
       .attr('fill', 'none')
       .attr('stroke', '#000')
       .attr('d', valueline);

    // Add the X and Y axes
    svg.append('g')
       .attr('transform', `translate(0, ${height})`)
       .call(d3.axisBottom(x));
    svg.append('g')
       .call(d3.axisLeft(y));

    // Return the SVG string
    return d3n.svgString();
}

module.exports = graphDates;

This code snippet generates a line graph visualizing time-series data using the D3.js library.

Here's a breakdown:

  1. Initialization:

  2. Setting up the Graph:

  3. Scales and Line Definition:

  4. Creating the SVG Element:

  5. Data Binding and Rendering:

  6. Axes:

  7. Returning SVG String:

In essence, this code takes an array of events, creates a D3.js line graph visualizing the time series of these events, and returns the SVG representation of the graph.