Last active
January 4, 2016 03:59
-
-
Save rbonvall/8565121 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var width = 500, height = 200, padding = 40; | |
var max = 10; | |
var dataset = d3.range(10) | |
.map(function () { return 1 + Math.random() * (max - 1); }); | |
// Coordinate scaling functions | |
var x = d3.scale.linear() | |
.domain([0, dataset.length]) | |
.range([padding, width - padding]); | |
var y = d3.scale.linear() | |
.domain([0, max]) | |
.range([height - padding, padding]); | |
var svg = d3.select('#chart-after').append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
// Define axes | |
var xAxis = d3.svg.axis().scale(x).ticks(dataset.length); | |
var yAxis = d3.svg.axis().scale(y).ticks(5).orient('left'); | |
/****************************************************************/ | |
// Draw axes | |
svg.append('g') | |
.attr('class', 'axis') | |
.attr('transform', 'translate(0, Y)'.replace('Y', height - padding)) | |
.call(xAxis); | |
svg.append('g') | |
.attr('class', 'axis') | |
.attr('transform', 'translate(X, 0)'.replace('X', padding)) | |
.call(yAxis); | |
// Write labels | |
svg.selectAll('text') | |
.data(dataset) | |
.enter() | |
.append('text') | |
.text(Math.floor) | |
.attr('class', 'data-label') | |
.attr('x', function (d, i) { return x(i); }) | |
.attr('y', function (d, i) { return y(d + 1); }); | |
/****************************************************************/ | |
// Draw data line | |
var line = d3.svg.line() | |
.x(function (d, i) { return x(i); }) | |
.y(function (d, i) { return y(d); }); | |
svg.append('path') | |
.attr('class', 'dataLine') | |
.attr('d', line(dataset)); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
var width = 500, height = 200, padding = 40; | |
var max = 10; | |
var dataset = d3.range(10) | |
.map(function () { return 1 + Math.random() * (max - 1); }); | |
// Coordinate scaling functions | |
var x = d3.scale.linear() | |
.domain([0, dataset.length]) | |
.range([padding, width - padding]); | |
var y = d3.scale.linear() | |
.domain([0, max]) | |
.range([height - padding, padding]); | |
var svg = d3.select('#chart-before').append('svg') | |
.attr('width', width) | |
.attr('height', height); | |
// Define axes | |
var xAxis = d3.svg.axis().scale(x).ticks(dataset.length); | |
var yAxis = d3.svg.axis().scale(y).ticks(5).orient('left'); | |
/****************************************************************/ | |
// Write labels | |
svg.selectAll('text') | |
.data(dataset) | |
.enter() | |
.append('text') | |
.text(Math.floor) | |
.attr('class', 'data-label') | |
.attr('x', function (d, i) { return x(i); }) | |
.attr('y', function (d, i) { return y(d + 1); }); | |
// Draw axes | |
svg.append('g') | |
.attr('class', 'axis') | |
.attr('transform', 'translate(0, Y)'.replace('Y', height - padding)) | |
.call(xAxis); | |
svg.append('g') | |
.attr('class', 'axis') | |
.attr('transform', 'translate(X, 0)'.replace('X', padding)) | |
.call(yAxis); | |
/****************************************************************/ | |
// Draw data line | |
var line = d3.svg.line() | |
.x(function (d, i) { return x(i); }) | |
.y(function (d, i) { return y(d); }); | |
svg.append('path') | |
.attr('class', 'dataLine') | |
.attr('d', line(dataset)); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset="utf-8"> | |
<title>Simple line chart</title> | |
<style> | |
svg { | |
background-color: hsl(40, 50%, 80%); | |
} | |
svg .dataLine { | |
fill: none; | |
stroke-width: 3px; | |
stroke: maroon; | |
} | |
svg .axis path, svg .axis line { | |
fill: none; | |
stroke: black; | |
shape-rendering: crispEdges; | |
} | |
svg .data-label { | |
text-anchor: middle; | |
} | |
</style> | |
<div id="chart-before"></div> | |
<div id="chart-after"></div> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="before.js"></script> | |
<script src="after.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment