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.
npm run import -- "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;
'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:
Initialization:
var D3Node = require('d3-node');
imports the d3-node
library, which allows using D3.js in a Node.js environment.function graphDates(events) { ... }
defines a function graphDates
that takes an array of events
as input.Setting up the Graph:
var d3n = new D3Node();
creates a new D3Node instance.var d3 = d3n.d3;
accesses the D3.js object from the d3n
instance.Scales and Line Definition:
x
and y
scales are defined using d3.scaleTime
and d3.scaleLinear
respectively, to map data values to visual positions on the graph.valueline
is a line generator function that defines how data points are connected in the line graph.Creating the SVG Element:
svg = d3n.createSVG(...)
creates an SVG element with the specified dimensions and appends it to the document.g
(group) element is added to the SVG to hold the graph elements and is translated to the top-left margin.Data Binding and Rendering:
x
and y
scales are set using the data's domain (extremes) using d3.extent
and d3.max
.data([events])
, and the valueline
function is used to generate the path's d
attribute.Axes:
d3.axisBottom
and d3.axisLeft
respectively, and positioned accordingly.Returning SVG String:
return d3n.svgString();
returns the SVG string representation of the generated graph, which can be used to display it in a web page or other output.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.