Skip to content

Instantly share code, notes, and snippets.

@infotanka
Created April 28, 2014 20:12
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 infotanka/11382716 to your computer and use it in GitHub Desktop.
Save infotanka/11382716 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="i/favicon.ico">
<title>Евро и доллар</title>
<meta charset="utf-8">
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 12px;
margin: auto;
position: relative;
width: 900px;
}
text {
font: 10px sans-serif;
}
.xAaxis path {
display: none;
}
.xAxis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
margin-top:20px;
}
.xAxis path,
.xAxis line {
fill: none;
stroke: white;
opacity: 0.25;
}
.xAxis text {
font-family: sans-serif;
font-size: 11px;
}
.yAxis1 path {
display: none;
}
.yAxis1 line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
margin-top:20px;
}
.yAxis path,
.yAxis line {
fill: none;
stroke: white;
opacity: 0.25;
}
.yAxis text {
font-family: sans-serif;
font-size: 11px;
}
.yAxis path {
display: none;
}
.line {
fill: none;
stroke: #ccc;
stroke-width: 1px;
}
div.tooltip {
position: absolute;
text-align: left;
padding: 3px;
font: 11px sans-serif;
background: #eee;
pointer-events: none;
}
img.geo {
position: absolute;
}
form {
position: absolute;
top: 0px;
left:80px;
}
h1 {
font-size: 20px;
margin-bottom: 5px;
margin-top:10px;
}
</style>
<div></div>
<script src="http://d3js.org/d3.v3.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/lang/ru.js"></script>
<script>
var parseDate = d3.time.format("%m/%d/%Y").parse
var formatDate = d3.time.format("%d %b %Y")
var div = d3.select("body").append("div")
.classed("tooltip", true)
.style("opacity", 0);
var opac = 1;
function chOpac (opct) {
opac = Math.abs(opct - 1.5);
return opac;
};
var margin = {top: 50, right: 80, bottom: 20, left: 80},
width = 1100 - margin.left - margin.right,
height = 550 - margin.top - margin.bottom;
//var color = d3.scale.category20();
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 + ")");
d3.csv("rub-usd-eur.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
console.log(d.date)
});
var x = d3.time.scale().domain(d3.extent(data, function(d) {return d.eur;}))
.range([0, width-300])
var y = d3.scale.linear().domain(d3.extent(data, function(d) {return d.usd;}))
.range([height, 0])
var color = d3.scale.linear().domain(d3.extent(data, function(d) {return d.date;}))
.range(["steelblue", "brown"]).interpolate(d3.interpolateHcl)
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.format("d"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format("d"));;
var line = d3.svg.line()
.x(function(d) { return x(d.eur); })
.y(function(d) { return y(d.usd); });
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.style("fill", function(d) { return color(d.date); })
.attr("cx", function(d) { return x(d.eur); })
.attr("cy", function(d) { return y(d.usd); })
.attr("r", 2)
.on('mouseover', function(d, i){
d3.selectAll("circle").transition().delay(50)
.attr('opacity', function(d, j){
return i==j && 1 || 1
});
div.transition()
.duration(100)
.style("opacity", 0.9);
div.html(formatDate(d.date) + ": EUR = " + d3.round(d.eur, 2) + " руб., USD = " + d3.round(d.usd, 2) + " руб.")
.style("left", (d3.event.pageX) - 260 + "px")
.style("top", (d3.event.pageY) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(100)
.style("opacity", 0);
});
svg.append("g")
.attr("class", "xAxis")
.attr("transform", "translate(0," + 480 + ")")
.call(xAxis);
svg.append("g")
.attr("class", "yAxis")
.attr("transform", "translate(0,0)")
.call(yAxis);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment