Built with blockbuilder.org
Last active
May 19, 2017 12:29
-
-
Save srajabhoj/55773ad625b1a41c8b978c5bc45b0462 to your computer and use it in GitHub Desktop.
line graph with arrow
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
license: mit |
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
date | close | |
---|---|---|
1-May-12 | 58.13 | |
30-Apr-12 | 53.98 | |
27-Apr-12 | 67.00 | |
26-Apr-12 | 89.70 | |
25-Apr-12 | 99.00 | |
24-Apr-12 | 130.28 | |
23-Apr-12 | 166.70 | |
20-Apr-12 | 234.98 | |
19-Apr-12 | 345.44 | |
18-Apr-12 | 443.34 | |
17-Apr-12 | 543.70 | |
16-Apr-12 | 580.13 | |
13-Apr-12 | 605.23 | |
12-Apr-12 | 622.77 | |
11-Apr-12 | 626.20 | |
10-Apr-12 | 628.44 | |
9-Apr-12 | 636.23 | |
5-Apr-12 | 633.68 | |
4-Apr-12 | 624.31 | |
3-Apr-12 | 629.32 | |
2-Apr-12 | 618.63 | |
30-Mar-12 | 599.55 | |
29-Mar-12 | 609.86 | |
28-Mar-12 | 617.62 | |
27-Mar-12 | 614.48 | |
26-Mar-12 | 606.98 |
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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Animating a line with an arrow in d3</title> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<style> | |
.line { | |
stroke: blue; | |
stroke-width: 1.5px; | |
fill: white; | |
} | |
circle { | |
fill: red; | |
} | |
#marker { | |
stroke: black; | |
fill: black; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="chart"> | |
<script language="javascript"> | |
var chartData; | |
"use strict" | |
var svg = d3.select("body").append("svg").append("g").attr("transform", "translate(100,50)") | |
svg.append("svg:defs") | |
.append("svg:marker") | |
.attr("id", "arrow") | |
.attr("refX", 2) | |
.attr("refY", 6) | |
.attr("markerWidth", 13) | |
.attr("markerHeight", 13) | |
.attr("orient", "auto") | |
.append("svg:path") | |
.attr("d", "M2,2 L2,11 L10,6 L2,2"); | |
var line = d3.svg.line() | |
.x( function(point) { return point.lx; }) | |
.y( function(point) { return point.ly; }); | |
function lineData(d){ | |
// i'm assuming here that supplied datum | |
// is a link between 'source' and 'target' | |
var points = [ | |
{lx: d.source.x, ly: d.source.y}, | |
{lx: d.target.x, ly: d.target.y} | |
]; | |
return line(points); | |
} | |
var path = svg.append("path") | |
.data([{source: {x : 0, y : 0}, target: {x : 80, y : 80}}]) | |
.attr("class", "line") | |
//.style("marker-end", "url(#arrow)") | |
.attr("d", lineData); | |
//var arrow = svg.append("svg:path") | |
//.attr("d", "M2,2 L2,11 L10,6 L2,2"); | |
console.log(d3.svg.symbol()) | |
var arrow = svg.append("svg:path") | |
.attr("d", d3.svg.symbol().type("triangle-down")(10,1)); | |
arrow.transition() | |
.duration(2000) | |
.ease("linear") | |
.attrTween("transform", translateAlong(path.node())) | |
//.each("end", transition); | |
// Returns an attrTween for translating along the specified path element. | |
function translateAlong(path) { | |
var l = path.getTotalLength(); | |
var ps = path.getPointAtLength(0); | |
var pe = path.getPointAtLength(l); | |
var angl = Math.atan2(pe.y - ps.y, pe.x - ps.x) * (180 / Math.PI) - 90; | |
var rot_tran = "rotate(" + angl + ")"; | |
return function(d, i, a) { | |
console.log(d); | |
return function(t) { | |
var p = path.getPointAtLength(t * l); | |
return "translate(" + p.x + "," + p.y + ") " + rot_tran; | |
}; | |
}; | |
} | |
var totalLength = path.node().getTotalLength(); | |
path | |
.attr("stroke-dasharray", totalLength + " " + totalLength) | |
.attr("stroke-dashoffset", totalLength) | |
.transition() | |
.duration(2000) | |
.ease("linear") | |
.attr("stroke-dashoffset", 0); | |
d3.csv("data.csv", data => { | |
chartData = (data); | |
console.log(chartData); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment