forked from sghall/d3-multi-series-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-line.html
76 lines (64 loc) · 2.44 KB
/
multi-line.html
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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<h2 style="padding-left:60px;">Total Funding Rounds in CrunchBase by Quarter</h2>
<script src="lib/d3.js"></script>
<script>
var margin = {top: 20, right: 55, bottom: 30, left: 40},
width = 1000 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.rangeRound([height, 0]);
var line = d3.svg.line()
.interpolate("cardinal")
.x(function (d) { return x(d.label) + x.rangeBand() / 2; })
.y(function (d) { return y(d.value); });
var color = d3.scale.ordinal()
.range(["#001c9c","#101b4d","#475003","#9c8305","#d3c47c"]);
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("data/crunchbase-quarters.csv", function (error, data) {
console.log("Inital Data", data);
var labelVar = 'quarter';
var varNames = d3.keys(data[0])
.filter(function (key) { return key !== labelVar;});
color.domain(varNames);
var seriesData = varNames.map(function (name) {
return {
name: name,
values: data.map(function (d) {
return {name: name, label: d[labelVar], value: +d[name]};
})
};
});
console.log("seriesData", seriesData);
x.domain(data.map(function (d) { return d.quarter; }));
y.domain([
d3.min(seriesData, function (c) {
return d3.min(c.values, function (d) { return d.value; });
}),
d3.max(seriesData, function (c) {
return d3.max(c.values, function (d) { return d.value; });
})
]);
var series = svg.selectAll(".series")
.data(seriesData)
.enter().append("g")
.attr("class", "series");
series.append("path")
.attr("class", "line")
.attr("d", function (d) { return line(d.values); })
.style("stroke", function (d) { return color(d.name); })
.style("stroke-width", "4px")
.style("fill", "none");
});
</script>
</body>
</html>