A set of snippets for Visual Studio Code helping performing some common operations with D3.js. These snippets work both with TypeScript and Javacript (ES6 /ES2015 or above).
All commands start with the d3 prefix
Creates a linear scale.
Output:
let scale = d3.scaleLinear()
.domain([0, 1])
.range([0,width]);
Creates a linear axis in a selection.
Output:
let scale = d3.scaleLinear()
.domain([0, 1])
.range([0,width]);
let axis = d3.axisBottom(scale);
let axisGroup = container.append('g')
.classed('axis', true)
.call(axis);
Creates a linear scale.
Output:
var scale = d3.scale.linear()
.domain([0, 1])
.range([0,width])
Creates a linear axis in a selection.
Output:
var scale = d3.scale.linear()
.domain([0, 1])
.range([0,width])
var axis = d3.svg.axis()
.orient('bottom')
.scale(scale);
var axisGroup = container.append('g')
.classed('axis', true)
.call(axis);
Appends the transform
attribute in situ.
Output:
.attr('transform', (d, i) => `translate(${},${})`)
Appends a rectangle to a selection.
Output:
var rect = container.append('rect')
.attr('x', )
.attr('y', )
.attr('width', )
.attr('height', )
.style('stroke','none')
.style('fill','lightblue');
Appends a circle to a selection.
Output:
var circle = container.append('circle')
.attr('cx', )
.attr('cy', )
.attr('r', )
.style('stroke','none')
.style('fill','lightblue');
Creates an exit-enter stub.
Output:
var dataBound = container.selectAll('.classed')
.data(data);
dataBound
.exit()
.remove();
var enterSelection = dataBound
.enter()
.append('g')
.classed('classed', true);
Creates a stub to read a csv file.
Output:
d3.csv('filename.csv',
(error, data) => {
if (error) {
console.error(error);
} else {
console.log(data);
}
});
Creates an svg,and a group with margins as a container for plotting purpose.
Output:
let svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height);
let plotMargins = {
top: 30,
bottom: 30,
left: 150,
right: 30
};
let plotGroup = svg.append('g')
.classed('plot', true)
.attr('transform', `translate(${plotMargins.left},${plotMargins.top})`);
let plotWidth = width - plotMargins.left - plotMargins.right;
let plotHeight = height - plotMargins.top - plotMargins.bottom;