Created
July 3, 2017 18:17
-
-
Save ricalanis/a2530edfc147bc0f784b1942104410a8 to your computer and use it in GitHub Desktop.
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
date | value | |
---|---|---|
20111001-12:00 | 0.0 | |
20111002-12:00 | 11.0 | |
20111003-12:00 | 15.0 | |
20111004-12:00 | 21.0 | |
20111005-12:00 | 14.0 | |
20111006-12:00 | 13.0 | |
20111007-12:00 | 16.4 | |
20111008-12:00 | 11.2 | |
20111009-12:00 | 8.7 | |
20111010-12:00 | 16.0 |
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> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
margin: auto; | |
width: 960px; | |
} | |
text { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
shape-rendering: crispEdges; | |
stroke: #d3d3d3; | |
fill: #d3d3d3; | |
} | |
.tick { | |
display: none | |
} | |
.line { | |
fill: none; | |
stroke: url(#value-gradient); | |
stroke-width: 10px; | |
stroke-linecap: round ; | |
} | |
.domain { | |
stroke: #FFF; | |
display: none | |
} | |
.xAxis text { | |
fill: #a9a9a9; | |
font-size: 16px; | |
} | |
.yAxis text { | |
fill: #a9a9a9; | |
font-size: 16px; | |
} | |
.lineY { | |
stroke-width:2px; | |
stroke:#d3d3d3; | |
} | |
.lineX { | |
stroke-width:2px; | |
stroke:#d3d3d3; | |
} | |
div.tooltip { | |
position: absolute; | |
text-align: center; | |
width: auto; | |
height: 28px; | |
padding: 2px; | |
font: 12px sans-serif; | |
background: lightsteelblue; | |
border: 0px; | |
border-radius: 8px; | |
pointer-events: none; | |
padding: 10px; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 50, left: 50}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var parseDate = d3.timeParse("%Y%m%d-%H:%M"); | |
// Change this for better format | |
var formatTime = d3.timeFormat("%Y%m%d-%H:%M") | |
var x = d3.scaleTime() | |
.range([0, width]); | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
var xAxis = d3 | |
.axisBottom(x) | |
.ticks([0]); | |
var yAxis = d3 | |
.axisLeft(y) | |
.ticks([0]); | |
var line = d3.line() | |
.x(function(d) { return x(d.date); }) | |
.y(function(d) { return y(d.value); }); | |
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 div = d3.select("body").append("div") | |
.attr("class", "tooltip") | |
.style("opacity", 0); | |
d3.tsv("data.tsv", function(error, data) { | |
if (error) throw error; | |
data.forEach(function(d) { | |
d.date = parseDate(d.date); | |
d.value = +d.value; | |
}); | |
maxValue = d3.max(data, function(d) { return d.value; }) | |
x.domain([data[0].date, data[data.length - 1].date]); | |
y.domain([0, maxValue]); | |
svg.append("g") | |
.attr("transform", "translate(0," + height + ")") | |
.attr("class","xAxis") | |
.call(d3.axisBottom(x)) | |
.append("text") | |
.attr("fill", "#000") | |
.attr("y", 6) | |
.attr("dy", "2.0em") | |
.attr("dx", "5.0em") | |
.attr("text-anchor", "end") | |
.text("tiempo") | |
svg.append("g") | |
.attr("class","yAxis") | |
.call(d3.axisLeft(y)) | |
.append("text") | |
.attr("fill", "#000") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", "-2.0em") | |
.attr("dx", "-3.0em") | |
.attr("text-anchor", "end") | |
.text("usuarios") | |
svg.append("linearGradient") | |
.attr("id", "value-gradient") | |
.attr("gradientUnits", "userSpaceOnUse") | |
.attr("x1", 0).attr("y1", y(0)) | |
.attr("x2", 0).attr("y2", y(maxValue)) | |
.selectAll("stop") | |
.data([ | |
{offset: "0%", color: "#CEC6FF"}, | |
{offset: "50%", color: "#9480FF"}, | |
{offset: "100%", color: "#312695"} | |
]) | |
.enter().append("stop") | |
.attr("offset", function(d) { return d.offset; }) | |
.attr("stop-color", function(d) { return d.color; }); | |
svg.append("line") | |
.attr("x1", 0) | |
.attr("x2", width) | |
.attr("y1", height) | |
.attr("y2", height) | |
.attr("class","lineX") | |
svg.append("line") | |
.attr("x1", 0) | |
.attr("x2", 0) | |
.attr("y1", 0) | |
.attr("y2", height) | |
.attr("class","lineY") | |
svg.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("d", line); | |
svg.selectAll("dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("r", 5) | |
.attr("cx", function(d) { return x(d.date); }) | |
.attr("cy", function(d) { return y(d.value); }) | |
.style("fill","transparent") | |
.on("mouseover", function(d) { | |
div.transition() | |
.duration(200) | |
.style("opacity", .9); | |
div .html("Date:"+ formatTime(d.date) + "<br/> Value:" + d.value) | |
.style("left", (d3.event.pageX) + "px") | |
.style("top", (d3.event.pageY - 28) + "px"); | |
}) | |
.on("mouseout", function(d) { | |
div.transition() | |
.duration(500) | |
.style("opacity", 0); | |
}); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment