-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (90 loc) · 2.53 KB
/
app.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
/* Parameters */
var selector = '#bar-chart';
var language = 'fi';
var titleText = {
'fi' : "Julkaisuja",
'en' : "Publications",
'sv' : "Publikationer"
};
var filePath = "sampledata.tsv";
var xName = "Year";
var yName = "Publications"
/* * */
var margin = {top: 50, right: 20, bottom: 60, left: 60},
width = 500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .29);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(6)
.innerTickSize(-width);
var svg = d3.select(selector)
.append("div")
.classed("svg-container", true)
.append("svg")
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", "0 0 " + (width + margin.left + margin.right) + " " + (height + margin.top + margin.bottom))
.classed("svg-content-responsive", true)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("text")
.text(titleText[language])
.attr("x", width/2)
.attr("y", -30)
var tooltip = svg.append("text")
.attr("class", "tooltip")
.style("opacity", 0);
function type(d) {
d[xName] = +d[xName];
d[yName] = +d[yName];
return d;
}
var data = d3.dsv(";", "text/plain")(filePath, type, function(error, data) {
x.domain(data.map(function(d) { return d[xName]; }));
y.domain([0, d3.max(data, function(d) { return d[yName]; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var barGroups = svg.selectAll(".bar")
.data(data)
.enter()
.append("g")
.attr("class", "bar");
var rects = barGroups.append("rect")
.attr("x", function(d) { return x(d[xName]); })
.attr("width", x.rangeBand())
.attr("y", height)
.attr("height", 0)
.transition()
.duration(function(d,i) {
return 500 + i*100;
})
.attr("y", function(d) { return y(d[yName]); })
.attr("height", function (d) {
return height - y(d[yName]);
});
barGroups.on("mouseover", function(d) {
tooltip.text(d[yName])
.attr("x", (x(d[xName]) + x.rangeBand()/2) + 'px')
.attr("y", -10 + y(d[yName]) + 'px')
.transition()
.duration(300)
.style("opacity", .9)
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(600)
.style("opacity", 0);
});
});