Skip to content

Instantly share code, notes, and snippets.

@classiemilio
Last active July 26, 2016 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save classiemilio/5d12fed93df306dd1a3fc60648aaa3d6 to your computer and use it in GitHub Desktop.
Save classiemilio/5d12fed93df306dd1a3fc60648aaa3d6 to your computer and use it in GitHub Desktop.
/* Implementation */
function renderLines(xScale, yScale, data) {
var lineGenerator = getLineGenerator(xScale, yScale);
d3.selectAll('path')
.data(data)
.enter()
.append('path')
.attr('d', lineGenerator);
}
function getLineGenerator(xScale, yScale) {
return d3.svg.line()
.x(lineX.bind(undefined, xScale))
.y(lineY.bind(undefined, yScale));
}
function lineX(xScale, datum) {
return xScale(datum.x);
}
function lineY(yScale, datum) {
return yScale(datum.y);
}
/* Test */
describe('#getLineGenerator', function() {
var generator;
beforeEach(function() {
var xScale = d3.scale.linear().domain([0, 10]).range([0, 100]);
var yScale = d3.scale.linear().domain([0, 10]).range([0, 1000]);
generator = getLineGenerator(xScale, yScale);
});
it('returns the correct x-values', function() {
var generatorX = generator.x(); // Returns the accessor function for x
assert.strictEqual(generatorX({x: 0, y: 0}), 0);
assert.strictEqual(generator({x: 10, y: 0}), 100);
});
it('returns the correct y-values', function() {
var generatorY = generator.y(); // Returns the accessor function for y
assert.strictEqual(generatorY({x: 0, y: 0}), 0);
assert.strictEqual(generatorY({x: 0, y: 10}), 1000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment