Skip to content

Instantly share code, notes, and snippets.

@vincentei
Last active April 9, 2016 17:35
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 vincentei/58bd33cc25dc32aa169177a4057087ba to your computer and use it in GitHub Desktop.
Save vincentei/58bd33cc25dc32aa169177a4057087ba to your computer and use it in GitHub Desktop.
Scatter plot
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.tooltip {
position: absolute;
width: 100px;
height: 20px;
pointer-events: none;
background: white;
border-radius: 2px;
padding: 6px;
color: black;
font-weight: bold;
}
</style>
<script type="text/javascript">
function draw(data) {
//Define dimensions of the graph
var margin = {top: 30, right: 20, bottom: 60, left: 100},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// Create axis scale mapping values -> pixels
var x = d3.scale.linear()
.range([0, width])
.domain([0,30]);
var y = d3.scale.linear()
.range([height, 0])
.domain([0,60]);
// /definition of axes
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
// Add the svg canvas
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 + ")");
//Add area for tooltip
var tooltip = d3.select('body')
.append('div')
.attr('class', 'tooltip')
//.style("opacity", 1);
//Draw circles
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.wind); })
.attr("cy", function(d) { return y(d.price); })
.style("fill", 'rgb(49,130,189)') //colorbrwer2.org
// Add tooltip and colo change
.on("mouseover",function(d) {
tooltip.transition()
.duration(0)
.style("opacity",0.9);
tooltip.html("Wind: " + d.wind.toFixed([1])+ " GW" + "<br/>" + "Price: " + d.price + " EUR/MWh")
.style("left",(d3.event.pageX)+"px")
.style("top",(d3.event.pageY-30)+"px")
d3.select(this).attr("r", 3.5).style("fill", "red");
})
.on("mouseout",function(d) {
tooltip.transition()
.duration(0)
.style("opacity",0);
d3.select(this).attr("r", 3.5).style("fill", 'rgb(49,130,189)');
});
// Add the x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
// Add the y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
// Add the text label for the x axis
svg.append("text")
.attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom/2) + ")")
.style("text-anchor", "middle")
.text("Average daily wind production (GW)");
// Add the text label for the y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left/2)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Price (EUR/MWh)");
};
</script>
</head>
<body>
<script type="text/javascript">
//Read the data
d3.tsv("wind_price.tsv", function(d) {
d['wind'] = +d['wind']/1000;
d['price'] = +d['price'];
return d;
}, draw);
</script>
</body>
<h1>Impact of German wind energy on market prices for electricity</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
</html>
wind price
19587.0 16.3
22797.8 4.5
2669.9 33.9
7083.4 38.7
14116.3 28.5
24720.5 24.3
18631.7 24.8
14216.1 27.2
20501.3 25.4
7815.0 33.1
1339.2 47
1928.3 45
3790.4 43.8
2087.3 42.4
8355.9 32.9
17005.9 28.2
14541.9 26.2
3944.2 38.2
3410.9 45.5
2025.2 48.3
4309.6 44
5581.7 39.3
4397.1 44.2
1258.4 48.3
1758.3 48.4
3451.7 38.7
2221.5 42.9
3659.5 43.2
7412.7 38.1
10231.7 30.8
11522.1 34.6
4223.1 44.3
6400.0 37.3
4930.5 38.9
15181.8 27.2
14845.8 26.1
5611.7 36.4
3480.7 39.9
3514.6 37.9
3980.5 34.9
3313.3 39.5
5452.1 36.7
5486.9 31.9
3018.4 36.4
1759.2 37.2
1941.7 35.3
1542.0 48.3
3377.2 39.5
5843.8 40.7
4594.7 33.7
27779.6 17.8
23871.4 19.1
14047.9 32.2
7258.4 23.7
4295.5 36.4
4324.2 42
1331.4 42.4
1931.8 35.5
6725.8 32.6
8926.0 27
4415.7 34.3
3936.9 37.3
7497.2 30.4
3827.6 35.7
3161.0 34.7
2947.2 33.4
4831.3 37.1
4139.9 32.4
4811.9 35
3515.3 23.2
13403.7 26.8
10099.6 26
10447.9 27.9
1761.6 29.9
8054.2 29.4
11966.5 26.4
4824.4 20.2
3050.8 30.6
8490.2 28.7
2784.7 34.8
4167.8 34
1961.1 31.2
8317.1 33.2
6429.2 32
8301.8 30.5
4871.8 31.1
16855.2 22
14446.4 27.8
1767.8 28.9
4416.0 29.6
5191.5 33.8
3030.6 32.4
2349.9 35.8
2000.8 35.2
4208.2 37.5
3876.1 31.4
8598.8 32.6
9353.6 32.4
6790.3 31.9
8123.7 30.6
2666.4 36.5
1540.7 36.3
1981.9 35.6
4244.4 33.6
7788.4 33.9
3095.9 44.8
3991.5 49
21393.3 29.6
21704.4 28.7
10708.0 32.1
4366.6 41.6
2502.6 47.7
1607.3 45.6
5924.8 38
7116.5 45.6
3209.7 46.7
2560.5 47.2
2807.6 45.5
18989.9 24.6
18408.6 27.4
18080.6 24.5
4754.0 30.5
4287.1 33.2
2937.4 34.8
3194.7 34.4
2041.1 34.2
4189.8 38.8
3631.5 37.6
6788.8 34.2
8925.4 30.5
5143.9 37.6
2009.2 39.5
1202.2 37.4
1716.2 31.9
14983.8 26
7769.6 29.3
8286.8 30.9
2378.3 41
8773.2 39.1
7406.3 37.6
2760.4 40.8
7577.6 33
2856.2 35.5
2683.1 34.5
4539.8 33.7
4648.5 33.5
12841.7 28.3
7948.9 33.9
14895.4 27.7
5589.3 36.5
4520.2 33.4
1746.8 43.2
5944.4 32
2311.1 35.4
3920.3 34.4
3380.3 37
2308.9 39
1973.6 38
8552.9 37.2
6301.5 40.5
3082.5 47.1
1984.8 44.3
4490.0 42.2
4831.1 44.7
7324.0 39.9
2151.6 47.3
1310.4 48.4
3349.3 41.7
9074.2 37.2
5656.5 37.8
4426.9 40.4
3596.1 42.2
1547.9 49.6
2301.2 43.2
1538.8 41.6
2232.8 42.3
2505.6 44
6192.9 36.7
22743.4 28.8
13131.6 35.1
9238.5 36.4
16047.1 32.1
13750.9 31.7
27325.5 20.4
27448.7 24.8
11507.9 38.2
13321.8 35.2
6414.5 41.7
2559.4 51.3
8350.5 36.8
11290.6 35.7
11888.1 36.4
7660.6 42.5
17565.9 30.7
6056.1 34.7
9726.2 34.9
15362.6 29.5
18008.4 26.4
2592.5 42.6
6074.1 37
9555.8 32.5
8183.7 32.6
24632.7 17
21428.8 19.5
13131.9 17.3
11983.2 13.3
10393.6 31
11006.1 26.9
9362.1 21.9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment