Last active
December 18, 2015 23:39
-
-
Save boertel/5863615 to your computer and use it in GitHub Desktop.
Trend
This file contains hidden or 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> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title></title> | |
| <link rel="stylesheet" href="//punchtab.github.io/Punchdation/static/css/punchdation/fonts.css" /> | |
| <style> | |
| body { | |
| font-family: "Bariol"; | |
| } | |
| .line { | |
| fill: none; | |
| stroke-width: 3px; | |
| } | |
| .line.members { | |
| stroke: #3C83B8; | |
| } | |
| .line.anonymous { | |
| stroke: #00B06D; | |
| } | |
| .label.members { | |
| fill: #3C83B8; | |
| } | |
| .label.anonymous { | |
| fill: #00B06D; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
| <script type="text/javascript"> | |
| var margin = {top: 10, right: 10, bottom: 10, left: 10}, | |
| width = 540 - margin.left - margin.right, | |
| height = 140 - margin.top - margin.bottom; | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width + margin.left + margin.right) | |
| .attr("height", height + margin.top + margin.bottom) | |
| .append("g") | |
| .attr("transform", "translate(" + margin.left + ", " + margin.top + ")"); | |
| var x = d3.time.scale() | |
| .range([0, width - 250]); | |
| var y = d3.scale.linear() | |
| .range([height, 0]); | |
| var line = d3.svg.line() | |
| .interpolate("cardinal") | |
| .x(function (d) { return x(d.date); }) | |
| .y(function (d) { return y(d.value); }) | |
| var parseDate = d3.time.format('%Y/%m').parse; | |
| function random() { | |
| return d3.range(12).map(function(i) { | |
| return { | |
| date: parseDate("2012/" + i), | |
| value: Math.ceil(Math.random(0, 100) * 100) | |
| } | |
| }); | |
| } | |
| var data = [ | |
| {key: "anonymous", values: random()}, | |
| {key: "members", values: random()} | |
| ]; | |
| data.forEach(function (d) { | |
| d.total = d3.sum(d.values, function (d) { return d.value; }) | |
| }); | |
| x.domain([ | |
| d3.min(data, function (d) { return d.values[0].date; }), | |
| d3.max(data, function (d) { return d.values[d.values.length - 1].date; }) | |
| ]); | |
| y.domain([0, d3.max(data, function (d) { | |
| return d3.max(d.values, function (d) { | |
| return d.value; | |
| }); | |
| })]); | |
| svg.selectAll(".line") | |
| .data(data) | |
| .enter() | |
| .append("path") | |
| .attr("class", function (d) { return "line " + d.key; }) | |
| .attr("d", function (d) { return line(d.values); }); | |
| var label = svg.selectAll(".label") | |
| .data(data) | |
| .enter() | |
| .append("g") | |
| .attr("class", function (d) { return "label " + d.key; }) | |
| label.append("text") | |
| .attr("class", function (d) { return "value " + d.key; }) | |
| .attr("x", function (d) { | |
| return x(d.values[d.values.length - 1].date); | |
| }) | |
| .attr("y", function (d) { | |
| return y(d.values[d.values.length - 1].value); | |
| }) | |
| .attr("dx", 10) | |
| .attr("dy", 5) | |
| .text(function (d) { return "$" + d.total; }); | |
| label.append("text") | |
| .attr("class", function (d) { return "title " + d.key; }) | |
| .attr("x", function (d) { | |
| return x(d.values[d.values.length - 1].date); | |
| }) | |
| .attr("y", function (d) { | |
| return y(d.values[d.values.length - 1].value); | |
| }) | |
| .attr("dx", 75) | |
| .attr("dy", 5) | |
| .text(function (d) { return d.key; }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment