-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharea.js
More file actions
157 lines (134 loc) · 3.98 KB
/
area.js
File metadata and controls
157 lines (134 loc) · 3.98 KB
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
define(function (require) {
var d3 = require("d3");
var path = require("src/modules/element/svg/path");
var builder = require("builder");
var valuator = require("valuator");
return function area() {
// Private variables
var x = function (d) { return d.x; };
var y = function (d) { return d.y; };
var xScale = d3.time.scale.utc();
var yScale = d3.scale.linear();
var offset = "zero";
var order = "default";
var out = function stackOut(d, y0, y) {
d.y0 = y0;
d.y = y;
};
var interpolate = "linear";
var tension = 0.7;
var defined = function (d) { return d.y !== null; };
var properties = {
class: "area",
fill: function (d, i) { return d3.scale.category10()(i); },
stroke: function (d, i) { return d3.scale.category10()(i); },
strokeWidth: 0,
opacity: 1
};
function component(selection) {
selection.each(function (data) {
var stack = d3.layout.stack()
.x(x)
.y(y)
.offset(offset)
.order(order)
.out(out);
data = stack(data);
xScale.domain(d3.extent(d3.merge(data), xAccessor));
yScale.domain([
Math.min(0, d3.min(d3.merge(data), yAccessor)),
Math.max(0, d3.max(d3.merge(data), yAccessor))
]);
var areas = d3.svg.area()
.x(X)
.y0(Y0)
.y1(Y1)
.interpolate(interpolate)
.tension(tension)
.defined(defined);
var areaPath = path()
.pathGenerator(areas);
d3.select(this)
.data([data])
.append("g")
.call(builder(properties, areaPath));
});
}
function xAccessor(d, i) {
return x.call(this, d, i);
}
function yAccessor(d, i) {
if (offset === "overlap") {
return y.call(this, d, i);
}
return d.y0 + y.call(this, d, i);
}
function X(d, i) {
return xScale(x.call(this, d, i));
}
function Y0(d) {
var min = Math.max(0, yScale.domain()[0]);
if (offset === "overlap") {
return yScale(min);
}
return yScale(d.y0);
}
function Y1(d, i) {
if (offset === "overlap") {
return yScale(y.call(this, d, i));
}
return yScale(d.y0 + y.call(this, d, i));
}
// Public API
component.x = function (_) {
if (!arguments.length) { return x; }
x = valuator(_);
return component;
};
component.y = function (_) {
if (!arguments.length) { return y; }
y = valuator(_);
return component;
};
component.xScale = function (_) {
if (!arguments.length) { return xScale; }
xScale = _;
return component;
};
component.yScale = function (_) {
if (!arguments.length) { return yScale; }
yScale = _;
return component;
};
component.offset = function (_) {
if (!arguments.length) { return offset; }
offset = _;
return component;
};
component.interpolate = function (_) {
if (!arguments.length) { return interpolate; }
interpolate = _;
return component;
};
component.tension = function (_) {
if (!arguments.length) { return tension; }
tension = _;
return component;
};
component.defined = function (_) {
if (!arguments.length) { return defined; }
defined = _;
return component;
};
component.properties = function (_) {
if (!arguments.length) { return properties; }
properties.class = typeof _.class !== "undefined" ? _.class : properties.class;
properties.fill = typeof _.fill !== "undefined" ? _.fill : properties.fill;
properties.stroke = typeof _.stroke !== "undefined" ? _.stroke : properties.stroke;
properties.strokeWidth = typeof _.strokeWidth !== "undefined" ? _.strokeWidth : properties.strokeWidth;
properties.opacity = typeof _.opacity !== "undefined" ? _.opacity : properties.opacity;
return component;
};
return component;
};
});