-
Notifications
You must be signed in to change notification settings - Fork 0
/
fernie_chart.js
158 lines (127 loc) · 4.63 KB
/
fernie_chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as d3v3 from "d3-v3"
// Setup svg using Bostock's margin convention
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3v3.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 + ")");
/* Data in strings like it would be if imported from a csv */
var data = [
{ year: "2001", male: "62", female: "37", mixed: "1" },
{ year: "2002", male: "55", female: "44", mixed: "1" },
{ year: "2003", male: "63", female: "35", mixed: "2" },
{ year: "2004", male: "72", female: "27", mixed: "1" },
{ year: "2005", male: "73", female: "26", mixed: "1" },
{ year: "2006", male: "66", female: "33", mixed: "1" },
{ year: "2007", male: "67", female: "31", mixed: "2" },
{ year: "2008", male: "61", female: "36", mixed: "3" },
{ year: "2009", male: "57", female: "37", mixed: "6" },
{ year: "2010", male: "63", female: "34", mixed: "3" },
{ year: "2011", male: "58", female: "34", mixed: "8" },
{ year: "2012", male: "79", female: "19", mixed: "2" },
{ year: "2013", male: "73", female: "25", mixed: "2" },
{ year: "2014", male: "63", female: "34", mixed: "3" },
{ year: "2015", male: "59", female: "39", mixed: "2" },
{ year: "2016", male: "65", female: "31", mixed: "4" },
{ year: "2017", male: "68", female: "26", mixed: "6" },
{ year: "2018", male: "77", female: "21", mixed: "2" },
{ year: "2019", male: "64", female: "35", mixed: "1" },
];
var formatTime = d3v3.timeFormat("%Y");
// Transpose the data into layers
var dataset = d3v3.stack()(["male", "female", "mixed"].map(function(fruit) {
return data.map(function(d) {
return {x: formatTime(d.year), y: +d[fruit]};
});
}));
console.log(d3v3.stack().keys(["male", "female", "mixed"])(data))
// Set x, y and colors
var x = d3v3.scaleOrdinal()
.domain(dataset[0].map(function(d) { return d.x; }))
.rangeRoundBands([10, width-10], 0.02);
var y = d3v3.scaleLinear()
.domain([0, d3v3.max(dataset, function(d) { return d3v3.max(d, function(d) { return d.y0 + d.y; }); })])
.range([height, 0]);
var colors = ["#6495ED", "#ff1493", "#d9d574"];
// Define and draw axes
var yAxis = d3v3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width, 0, 0)
.tickFormat( function(d) { return d } );
var xAxis = d3v3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3v3.time.format("%Y"));
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Create groups for each series, rects for each segment
var groups = svg.selectAll("g.cost")
.data(dataset)
.enter().append("g")
.attr("class", "cost")
.style("fill", function(d, i) { return colors[i]; });
var rect = groups.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y0 + d.y); })
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
.attr("width", x.rangeBand())
.on("mouseover", function() { tooltip.style("display", null); })
.on("mouseout", function() { tooltip.style("display", "none"); })
.on("mousemove", function(d) {
var xPosition = d3v3.mouse(this)[0] - 15;
var yPosition = d3v3.mouse(this)[1] - 25;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select("text").text(d.y);
});
// Draw legend
var legend = svg.selectAll(".legend")
.data(colors)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) {return colors.slice().reverse()[i];});
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d, i) {
switch (i) {
case 0: return "Mixed";
case 1: return "Female";
case 2: return "Male";
}
});
// Prep the tooltip bits, initial display is hidden
var tooltip = svg.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "white")
.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "12px")
.attr("font-weight", "bold");