@d3plus/core 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6219 -0
- package/es/index.js +4 -0
- package/es/src/charts/AreaPlot.js +86 -0
- package/es/src/charts/BarChart.js +93 -0
- package/es/src/charts/BoxWhisker.js +97 -0
- package/es/src/charts/BumpChart.js +148 -0
- package/es/src/charts/Donut.js +84 -0
- package/es/src/charts/Geomap.js +833 -0
- package/es/src/charts/LinePlot.js +84 -0
- package/es/src/charts/Matrix.js +358 -0
- package/es/src/charts/Network.js +787 -0
- package/es/src/charts/Pack.js +318 -0
- package/es/src/charts/Pie.js +242 -0
- package/es/src/charts/Plot.js +2212 -0
- package/es/src/charts/Priestley.js +312 -0
- package/es/src/charts/Radar.js +365 -0
- package/es/src/charts/RadialMatrix.js +393 -0
- package/es/src/charts/Rings.js +777 -0
- package/es/src/charts/Sankey.js +413 -0
- package/es/src/charts/StackedArea.js +80 -0
- package/es/src/charts/Tree.js +312 -0
- package/es/src/charts/Treemap.js +406 -0
- package/es/src/charts/Viz.js +2017 -0
- package/es/src/charts/drawSteps/drawAttribution.js +14 -0
- package/es/src/charts/drawSteps/drawBack.js +23 -0
- package/es/src/charts/drawSteps/drawColorScale.js +69 -0
- package/es/src/charts/drawSteps/drawLegend.js +120 -0
- package/es/src/charts/drawSteps/drawSubtitle.js +31 -0
- package/es/src/charts/drawSteps/drawTimeline.js +80 -0
- package/es/src/charts/drawSteps/drawTitle.js +31 -0
- package/es/src/charts/drawSteps/drawTotal.js +32 -0
- package/es/src/charts/drawSteps/zoomControls.js +254 -0
- package/es/src/charts/events/click.legend.js +76 -0
- package/es/src/charts/events/click.shape.js +26 -0
- package/es/src/charts/events/mouseenter.js +31 -0
- package/es/src/charts/events/mouseleave.js +21 -0
- package/es/src/charts/events/mousemove.legend.js +64 -0
- package/es/src/charts/events/mousemove.shape.js +42 -0
- package/es/src/charts/events/touchstart.body.js +7 -0
- package/es/src/charts/helpers/matrixData.js +104 -0
- package/es/src/charts/helpers/tileAttributions.js +34 -0
- package/es/src/charts/index.js +21 -0
- package/es/src/charts/plotBuffers/Bar.js +65 -0
- package/es/src/charts/plotBuffers/Box.js +60 -0
- package/es/src/charts/plotBuffers/Circle.js +39 -0
- package/es/src/charts/plotBuffers/Line.js +30 -0
- package/es/src/charts/plotBuffers/Rect.js +40 -0
- package/es/src/charts/plotBuffers/discreteBuffer.js +24 -0
- package/es/src/charts/plotBuffers/numericBuffer.js +111 -0
- package/es/src/components/Axis.js +1567 -0
- package/es/src/components/AxisBottom.js +77 -0
- package/es/src/components/AxisLeft.js +77 -0
- package/es/src/components/AxisRight.js +77 -0
- package/es/src/components/AxisTop.js +77 -0
- package/es/src/components/ColorScale.js +958 -0
- package/es/src/components/Legend.js +673 -0
- package/es/src/components/Message.js +95 -0
- package/es/src/components/TextBox.js +752 -0
- package/es/src/components/Timeline.js +760 -0
- package/es/src/components/Tooltip.js +726 -0
- package/es/src/components/index.js +11 -0
- package/es/src/shapes/Area.js +361 -0
- package/es/src/shapes/Bar.js +342 -0
- package/es/src/shapes/Box.js +482 -0
- package/es/src/shapes/Circle.js +201 -0
- package/es/src/shapes/Image.js +255 -0
- package/es/src/shapes/Line.js +289 -0
- package/es/src/shapes/Path.js +186 -0
- package/es/src/shapes/Rect.js +215 -0
- package/es/src/shapes/Shape.js +1156 -0
- package/es/src/shapes/Whisker.js +330 -0
- package/es/src/shapes/index.js +10 -0
- package/es/src/utils/BaseClass.js +204 -0
- package/es/src/utils/RESET.js +4 -0
- package/es/src/utils/accessor.js +19 -0
- package/es/src/utils/configPrep.js +76 -0
- package/es/src/utils/constant.js +15 -0
- package/es/src/utils/getProp.js +9 -0
- package/es/src/utils/index.js +7 -0
- package/es/src/utils/uuid.js +13 -0
- package/package.json +68 -0
- package/umd/d3plus-core.full.js +56459 -0
- package/umd/d3plus-core.full.js.map +1 -0
- package/umd/d3plus-core.full.min.js +7241 -0
- package/umd/d3plus-core.js +14422 -0
- package/umd/d3plus-core.js.map +1 -0
- package/umd/d3plus-core.min.js +4564 -0
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
function _assert_this_initialized(self) {
|
|
2
|
+
if (self === void 0) {
|
|
3
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
4
|
+
}
|
|
5
|
+
return self;
|
|
6
|
+
}
|
|
7
|
+
function _call_super(_this, derived, args) {
|
|
8
|
+
derived = _get_prototype_of(derived);
|
|
9
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
10
|
+
}
|
|
11
|
+
function _class_call_check(instance, Constructor) {
|
|
12
|
+
if (!(instance instanceof Constructor)) {
|
|
13
|
+
throw new TypeError("Cannot call a class as a function");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function _defineProperties(target, props) {
|
|
17
|
+
for(var i = 0; i < props.length; i++){
|
|
18
|
+
var descriptor = props[i];
|
|
19
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
20
|
+
descriptor.configurable = true;
|
|
21
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
22
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
26
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
27
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
28
|
+
return Constructor;
|
|
29
|
+
}
|
|
30
|
+
function _get_prototype_of(o) {
|
|
31
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
32
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
33
|
+
};
|
|
34
|
+
return _get_prototype_of(o);
|
|
35
|
+
}
|
|
36
|
+
function _inherits(subClass, superClass) {
|
|
37
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
38
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
39
|
+
}
|
|
40
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
41
|
+
constructor: {
|
|
42
|
+
value: subClass,
|
|
43
|
+
writable: true,
|
|
44
|
+
configurable: true
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
48
|
+
}
|
|
49
|
+
function _possible_constructor_return(self, call) {
|
|
50
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
|
51
|
+
return call;
|
|
52
|
+
}
|
|
53
|
+
return _assert_this_initialized(self);
|
|
54
|
+
}
|
|
55
|
+
function _set_prototype_of(o, p) {
|
|
56
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
57
|
+
o.__proto__ = p;
|
|
58
|
+
return o;
|
|
59
|
+
};
|
|
60
|
+
return _set_prototype_of(o, p);
|
|
61
|
+
}
|
|
62
|
+
function _type_of(obj) {
|
|
63
|
+
"@swc/helpers - typeof";
|
|
64
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
65
|
+
}
|
|
66
|
+
function _is_native_reflect_construct() {
|
|
67
|
+
try {
|
|
68
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
69
|
+
} catch (_) {}
|
|
70
|
+
return (_is_native_reflect_construct = function() {
|
|
71
|
+
return !!result;
|
|
72
|
+
})();
|
|
73
|
+
}
|
|
74
|
+
import { max, sum } from "d3-array";
|
|
75
|
+
import { select } from "d3-selection";
|
|
76
|
+
import { colorDefaults } from "@d3plus/color";
|
|
77
|
+
import { assign, elem, rtl as detectRTL, textWidth } from "@d3plus/dom";
|
|
78
|
+
import { textWrap } from "@d3plus/text";
|
|
79
|
+
import { TextBox } from "../components/index.js";
|
|
80
|
+
import * as shapes from "../shapes/index.js";
|
|
81
|
+
import { accessor, BaseClass, configPrep, constant } from "../utils/index.js";
|
|
82
|
+
var padding = 5;
|
|
83
|
+
var Legend = /*#__PURE__*/ function(BaseClass) {
|
|
84
|
+
"use strict";
|
|
85
|
+
_inherits(Legend, BaseClass);
|
|
86
|
+
function Legend() {
|
|
87
|
+
_class_call_check(this, Legend);
|
|
88
|
+
var _this;
|
|
89
|
+
_this = _call_super(this, Legend);
|
|
90
|
+
_this._titleClass = new TextBox();
|
|
91
|
+
_this._align = "center";
|
|
92
|
+
_this._data = [];
|
|
93
|
+
_this._direction = "row";
|
|
94
|
+
_this._duration = 600;
|
|
95
|
+
_this._height = 200;
|
|
96
|
+
_this._id = accessor("id");
|
|
97
|
+
_this._label = accessor("id");
|
|
98
|
+
_this._lineData = [];
|
|
99
|
+
_this._outerBounds = {
|
|
100
|
+
width: 0,
|
|
101
|
+
height: 0,
|
|
102
|
+
x: 0,
|
|
103
|
+
y: 0
|
|
104
|
+
};
|
|
105
|
+
_this._padding = 5;
|
|
106
|
+
_this._shape = constant("Rect");
|
|
107
|
+
_this._shapes = [];
|
|
108
|
+
_this._shapeConfig = {
|
|
109
|
+
fill: accessor("color"),
|
|
110
|
+
height: constant(12),
|
|
111
|
+
hitArea: function(dd, i) {
|
|
112
|
+
var d = _this._lineData[i], h = max([
|
|
113
|
+
d.height,
|
|
114
|
+
d.shapeHeight
|
|
115
|
+
]);
|
|
116
|
+
return {
|
|
117
|
+
width: d.width + d.shapeWidth,
|
|
118
|
+
height: h,
|
|
119
|
+
x: -d.shapeWidth / 2,
|
|
120
|
+
y: -h / 2
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
labelBounds: function(dd, i) {
|
|
124
|
+
var d = _this._lineData[i];
|
|
125
|
+
var x = d.shapeWidth / 2;
|
|
126
|
+
if (d.shape === "Circle") x -= d.shapeR / 2;
|
|
127
|
+
var height = max([
|
|
128
|
+
d.shapeHeight,
|
|
129
|
+
d.height
|
|
130
|
+
]);
|
|
131
|
+
var rtlMod = _this._rtl ? d.shapeWidth + d.width + _this._padding * 2 : 0;
|
|
132
|
+
return {
|
|
133
|
+
width: d.width,
|
|
134
|
+
height: height,
|
|
135
|
+
x: x + padding - rtlMod,
|
|
136
|
+
y: -height / 2
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
labelConfig: {
|
|
140
|
+
fontColor: constant(colorDefaults.dark),
|
|
141
|
+
fontFamily: _this._titleClass.fontFamily(),
|
|
142
|
+
fontResize: false,
|
|
143
|
+
fontSize: constant(10),
|
|
144
|
+
verticalAlign: "middle"
|
|
145
|
+
},
|
|
146
|
+
opacity: 1,
|
|
147
|
+
r: constant(6),
|
|
148
|
+
width: constant(12),
|
|
149
|
+
x: function(d, i) {
|
|
150
|
+
var datum = _this._lineData[i];
|
|
151
|
+
var y = datum.y;
|
|
152
|
+
var pad = _this._align === "left" || _this._align === "right" && _this._direction === "column" ? 0 : _this._align === "center" ? (_this._outerBounds.width - _this._rowWidth(_this._lineData.filter(function(l) {
|
|
153
|
+
return y === l.y;
|
|
154
|
+
}))) / 2 : _this._outerBounds.width - _this._rowWidth(_this._lineData.filter(function(l) {
|
|
155
|
+
return y === l.y;
|
|
156
|
+
}));
|
|
157
|
+
var prevWords = _this._lineData.slice(0, i).filter(function(l) {
|
|
158
|
+
return y === l.y;
|
|
159
|
+
});
|
|
160
|
+
var rtlMod = _this._rtl ? datum.width + _this._padding : 0;
|
|
161
|
+
return _this._rowWidth(prevWords) + _this._padding * (prevWords.length ? datum.sentence ? 2 : 1 : 0) + _this._outerBounds.x + datum.shapeWidth / 2 + pad + rtlMod;
|
|
162
|
+
},
|
|
163
|
+
y: function(d, i) {
|
|
164
|
+
var ld = _this._lineData[i];
|
|
165
|
+
return ld.y + _this._titleHeight + _this._outerBounds.y + max(_this._lineData.filter(function(l) {
|
|
166
|
+
return ld.y === l.y;
|
|
167
|
+
}).map(function(l) {
|
|
168
|
+
return l.height;
|
|
169
|
+
}).concat(_this._data.map(function(l, x) {
|
|
170
|
+
return _this._fetchConfig("height", l, x);
|
|
171
|
+
}))) / 2;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
_this._titleConfig = {
|
|
175
|
+
fontSize: 12
|
|
176
|
+
};
|
|
177
|
+
_this._verticalAlign = "middle";
|
|
178
|
+
_this._width = 400;
|
|
179
|
+
return _this;
|
|
180
|
+
}
|
|
181
|
+
_create_class(Legend, [
|
|
182
|
+
{
|
|
183
|
+
key: "_fetchConfig",
|
|
184
|
+
value: function _fetchConfig(key, d, i) {
|
|
185
|
+
var val = this._shapeConfig[key] !== undefined ? this._shapeConfig[key] : this._shapeConfig.labelConfig[key];
|
|
186
|
+
if (!val && key === "lineHeight") return this._fetchConfig("fontSize", d, i) * 1.4;
|
|
187
|
+
return typeof val === "function" ? val(d, i) : val;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
key: "_rowHeight",
|
|
192
|
+
value: function _rowHeight(row) {
|
|
193
|
+
return max(row.map(function(d) {
|
|
194
|
+
return d.height;
|
|
195
|
+
}).concat(row.map(function(d) {
|
|
196
|
+
return d.shapeHeight;
|
|
197
|
+
}))) + this._padding;
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
key: "_rowWidth",
|
|
202
|
+
value: function _rowWidth(row) {
|
|
203
|
+
var _this = this;
|
|
204
|
+
return sum(row.map(function(d, i) {
|
|
205
|
+
var p = _this._padding * (i === row.length - 1 ? 0 : d.width ? 2 : 1);
|
|
206
|
+
return d.shapeWidth + d.width + p;
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
/**
|
|
212
|
+
@memberof Legend
|
|
213
|
+
@desc Renders the current Legend to the page. If a *callback* is specified, it will be called once the legend is done drawing.
|
|
214
|
+
@param {Function} [*callback* = undefined]
|
|
215
|
+
@chainable
|
|
216
|
+
*/ key: "render",
|
|
217
|
+
value: function render(callback) {
|
|
218
|
+
var _this = this;
|
|
219
|
+
if (this._select === void 0) this.select(select("body").append("svg").attr("width", "".concat(this._width, "px")).attr("height", "".concat(this._height, "px")).node());
|
|
220
|
+
// Legend Container <g> Groups
|
|
221
|
+
this._group = elem("g.d3plus-Legend", {
|
|
222
|
+
parent: this._select
|
|
223
|
+
});
|
|
224
|
+
this._titleGroup = elem("g.d3plus-Legend-title", {
|
|
225
|
+
parent: this._group
|
|
226
|
+
});
|
|
227
|
+
this._shapeGroup = elem("g.d3plus-Legend-shape", {
|
|
228
|
+
parent: this._group
|
|
229
|
+
});
|
|
230
|
+
var availableHeight = this._height;
|
|
231
|
+
this._titleHeight = 0;
|
|
232
|
+
this._titleWidth = 0;
|
|
233
|
+
if (this._title) {
|
|
234
|
+
var f = this._titleConfig.fontFamily || this._titleClass.fontFamily()(), s = this._titleConfig.fontSize || this._titleClass.fontSize()();
|
|
235
|
+
var lH = this._titleConfig.lineHeight || this._titleClass.lineHeight();
|
|
236
|
+
lH = lH ? lH() : s * 1.4;
|
|
237
|
+
var res = textWrap().fontFamily(f).fontSize(s).lineHeight(lH).width(this._width).height(this._height)(this._title);
|
|
238
|
+
this._titleHeight = lH + res.lines.length + this._padding;
|
|
239
|
+
this._titleWidth = max(res.widths);
|
|
240
|
+
availableHeight -= this._titleHeight;
|
|
241
|
+
}
|
|
242
|
+
// Calculate Text Sizes
|
|
243
|
+
this._lineData = this._data.map(function(d, i) {
|
|
244
|
+
var label = _this._label(d, i);
|
|
245
|
+
var shape = _this._shape(d, i);
|
|
246
|
+
var r = _this._fetchConfig("r", d, i);
|
|
247
|
+
var res = {
|
|
248
|
+
data: d,
|
|
249
|
+
i: i,
|
|
250
|
+
id: _this._id(d, i),
|
|
251
|
+
shape: shape,
|
|
252
|
+
shapeR: r,
|
|
253
|
+
shapeWidth: shape === "Circle" ? r * 2 : _this._fetchConfig("width", d, i),
|
|
254
|
+
shapeHeight: shape === "Circle" ? r * 2 : _this._fetchConfig("height", d, i),
|
|
255
|
+
y: 0
|
|
256
|
+
};
|
|
257
|
+
if (!label) {
|
|
258
|
+
res.sentence = false;
|
|
259
|
+
res.words = [];
|
|
260
|
+
res.height = 0;
|
|
261
|
+
res.width = 0;
|
|
262
|
+
return res;
|
|
263
|
+
}
|
|
264
|
+
var f = _this._fetchConfig("fontFamily", d, i), lh = _this._fetchConfig("lineHeight", d, i), s = _this._fetchConfig("fontSize", d, i);
|
|
265
|
+
var h = availableHeight - (_this._data.length + 1) * _this._padding, w = _this._width;
|
|
266
|
+
var newRes = textWrap().fontFamily(f).fontSize(s).lineHeight(lh).width(w).height(h)(label);
|
|
267
|
+
res = Object.assign(res, newRes);
|
|
268
|
+
res.width = Math.ceil(max(res.lines.map(function(t) {
|
|
269
|
+
return textWidth(t, {
|
|
270
|
+
"font-family": f,
|
|
271
|
+
"font-size": s
|
|
272
|
+
});
|
|
273
|
+
}))) + padding * 2;
|
|
274
|
+
res.height = Math.ceil(res.lines.length * (lh + 1));
|
|
275
|
+
res.og = {
|
|
276
|
+
height: res.height,
|
|
277
|
+
width: res.width
|
|
278
|
+
};
|
|
279
|
+
res.f = f;
|
|
280
|
+
res.s = s;
|
|
281
|
+
res.lh = lh;
|
|
282
|
+
return res;
|
|
283
|
+
});
|
|
284
|
+
var spaceNeeded;
|
|
285
|
+
var availableWidth = this._width - this._padding * 2;
|
|
286
|
+
spaceNeeded = this._rowWidth(this._lineData);
|
|
287
|
+
if (this._direction === "column" || spaceNeeded > availableWidth) {
|
|
288
|
+
var lines = 1, newRows = [];
|
|
289
|
+
var maxLines = max(this._lineData.map(function(d) {
|
|
290
|
+
return d.words.length;
|
|
291
|
+
}));
|
|
292
|
+
this._wrapLines = function() {
|
|
293
|
+
var _this = this;
|
|
294
|
+
lines++;
|
|
295
|
+
if (lines > maxLines) return;
|
|
296
|
+
var wrappable = lines === 1 ? this._lineData.slice() : this._lineData.filter(function(d) {
|
|
297
|
+
return d.width + d.shapeWidth + _this._padding * (d.width ? 2 : 1) > availableWidth && d.words.length >= lines;
|
|
298
|
+
}).sort(function(a, b) {
|
|
299
|
+
return b.sentence.length - a.sentence.length;
|
|
300
|
+
});
|
|
301
|
+
if (wrappable.length && availableHeight > wrappable[0].height * lines) {
|
|
302
|
+
var _loop = function(x) {
|
|
303
|
+
var label = wrappable[x];
|
|
304
|
+
var h = label.og.height * lines, w = label.og.width * (1.5 * (1 / lines));
|
|
305
|
+
var res = textWrap().fontFamily(label.f).fontSize(label.s).lineHeight(label.lh).width(w).height(h)(label.sentence);
|
|
306
|
+
if (!res.truncated) {
|
|
307
|
+
label.width = Math.ceil(max(res.lines.map(function(t) {
|
|
308
|
+
return textWidth(t, {
|
|
309
|
+
"font-family": label.f,
|
|
310
|
+
"font-size": label.s
|
|
311
|
+
});
|
|
312
|
+
}))) + label.s;
|
|
313
|
+
label.height = res.lines.length * (label.lh + 1);
|
|
314
|
+
} else {
|
|
315
|
+
truncated = true;
|
|
316
|
+
return "break";
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
var truncated = false;
|
|
320
|
+
for(var x = 0; x < wrappable.length; x++){
|
|
321
|
+
var _ret = _loop(x);
|
|
322
|
+
if (_ret === "break") break;
|
|
323
|
+
}
|
|
324
|
+
if (!truncated) this._wrapRows();
|
|
325
|
+
} else {
|
|
326
|
+
newRows = [];
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
this._wrapRows = function() {
|
|
331
|
+
newRows = [];
|
|
332
|
+
var row = 1, rowWidth = 0;
|
|
333
|
+
for(var i = 0; i < this._lineData.length; i++){
|
|
334
|
+
var d = this._lineData[i], w = d.width + this._padding * (d.width ? 2 : 1) + d.shapeWidth;
|
|
335
|
+
if (sum(newRows.map(function(row) {
|
|
336
|
+
return max(row, function(d) {
|
|
337
|
+
return max([
|
|
338
|
+
d.height,
|
|
339
|
+
d.shapeHeight
|
|
340
|
+
]);
|
|
341
|
+
});
|
|
342
|
+
})) > availableHeight) {
|
|
343
|
+
newRows = [];
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
if (w > availableWidth) {
|
|
347
|
+
newRows = [];
|
|
348
|
+
this._wrapLines();
|
|
349
|
+
break;
|
|
350
|
+
} else if (rowWidth + w < availableWidth) {
|
|
351
|
+
rowWidth += w;
|
|
352
|
+
} else if (this._direction !== "column") {
|
|
353
|
+
rowWidth = w;
|
|
354
|
+
row++;
|
|
355
|
+
}
|
|
356
|
+
if (!newRows[row - 1]) newRows[row - 1] = [];
|
|
357
|
+
newRows[row - 1].push(d);
|
|
358
|
+
if (this._direction === "column") {
|
|
359
|
+
rowWidth = 0;
|
|
360
|
+
row++;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
this._wrapRows();
|
|
365
|
+
if (!newRows.length || sum(newRows, this._rowHeight.bind(this)) + this._padding > availableHeight) {
|
|
366
|
+
spaceNeeded = sum(this._lineData.map(function(d) {
|
|
367
|
+
return d.shapeWidth + _this._padding;
|
|
368
|
+
})) - this._padding;
|
|
369
|
+
for(var i = 0; i < this._lineData.length; i++){
|
|
370
|
+
this._lineData[i].width = 0;
|
|
371
|
+
this._lineData[i].height = 0;
|
|
372
|
+
}
|
|
373
|
+
this._wrapRows();
|
|
374
|
+
}
|
|
375
|
+
if (newRows.length && sum(newRows, this._rowHeight.bind(this)) + this._padding < availableHeight) {
|
|
376
|
+
newRows.forEach(function(row, i) {
|
|
377
|
+
row.forEach(function(d) {
|
|
378
|
+
if (i) {
|
|
379
|
+
d.y = sum(newRows.slice(0, i), _this._rowHeight.bind(_this));
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
spaceNeeded = max(newRows, this._rowWidth.bind(this));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
var innerHeight = max(this._lineData, function(d, i) {
|
|
387
|
+
return max([
|
|
388
|
+
d.height,
|
|
389
|
+
_this._fetchConfig("height", d.data, i)
|
|
390
|
+
]) + d.y;
|
|
391
|
+
}) + this._titleHeight, innerWidth = max([
|
|
392
|
+
spaceNeeded,
|
|
393
|
+
this._titleWidth
|
|
394
|
+
]);
|
|
395
|
+
this._outerBounds.width = innerWidth;
|
|
396
|
+
this._outerBounds.height = innerHeight;
|
|
397
|
+
var xOffset = this._padding, yOffset = this._padding;
|
|
398
|
+
if (this._align === "center") xOffset = (this._width - innerWidth) / 2;
|
|
399
|
+
else if (this._align === "right") xOffset = this._width - this._padding - innerWidth;
|
|
400
|
+
if (this._verticalAlign === "middle") yOffset = (this._height - innerHeight) / 2;
|
|
401
|
+
else if (this._verticalAlign === "bottom") yOffset = this._height - this._padding - innerHeight;
|
|
402
|
+
this._outerBounds.x = xOffset;
|
|
403
|
+
this._outerBounds.y = yOffset;
|
|
404
|
+
this._titleClass.data(this._title ? [
|
|
405
|
+
{
|
|
406
|
+
text: this._title
|
|
407
|
+
}
|
|
408
|
+
] : []).duration(this._duration).select(this._titleGroup.node()).textAnchor({
|
|
409
|
+
left: "start",
|
|
410
|
+
center: "middle",
|
|
411
|
+
right: "end"
|
|
412
|
+
}[this._align]).width(this._width - this._padding * 2).x(this._padding).y(this._outerBounds.y).config(this._titleConfig).render();
|
|
413
|
+
this._shapes = [];
|
|
414
|
+
var baseConfig = configPrep.bind(this)(this._shapeConfig, "legend"), config = {
|
|
415
|
+
id: function(d) {
|
|
416
|
+
return d.id;
|
|
417
|
+
},
|
|
418
|
+
label: function(d) {
|
|
419
|
+
return d.label;
|
|
420
|
+
},
|
|
421
|
+
lineHeight: function(d) {
|
|
422
|
+
return d.lH;
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
var data = this._data.map(function(d, i) {
|
|
426
|
+
var obj = {
|
|
427
|
+
__d3plus__: true,
|
|
428
|
+
data: d,
|
|
429
|
+
i: i,
|
|
430
|
+
id: _this._id(d, i),
|
|
431
|
+
label: _this._lineData[i].width ? _this._label(d, i) : false,
|
|
432
|
+
lH: _this._fetchConfig("lineHeight", d, i),
|
|
433
|
+
shape: _this._shape(d, i)
|
|
434
|
+
};
|
|
435
|
+
return obj;
|
|
436
|
+
});
|
|
437
|
+
// Legend Shapes
|
|
438
|
+
this._shapes = [];
|
|
439
|
+
[
|
|
440
|
+
"Circle",
|
|
441
|
+
"Rect"
|
|
442
|
+
].forEach(function(Shape) {
|
|
443
|
+
_this._shapes.push(new shapes[Shape]().parent(_this).data(data.filter(function(d) {
|
|
444
|
+
return d.shape === Shape;
|
|
445
|
+
})).duration(_this._duration).labelConfig({
|
|
446
|
+
padding: 0
|
|
447
|
+
}).select(_this._shapeGroup.node()).verticalAlign("top").config(assign({}, baseConfig, config)).render());
|
|
448
|
+
});
|
|
449
|
+
if (callback) setTimeout(callback, this._duration + 100);
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
/**
|
|
455
|
+
@memberof Legend
|
|
456
|
+
@desc If *value* is specified, sets the active method for all shapes to the specified function and returns the current class instance. If *value* is not specified, returns the current active method.
|
|
457
|
+
@param {Function} [*value*]
|
|
458
|
+
@chainable
|
|
459
|
+
*/ key: "active",
|
|
460
|
+
value: function active(_) {
|
|
461
|
+
this._shapes.forEach(function(s) {
|
|
462
|
+
return s.active(_);
|
|
463
|
+
});
|
|
464
|
+
return this;
|
|
465
|
+
}
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
/**
|
|
469
|
+
@memberof Legend
|
|
470
|
+
@desc If *value* is specified, sets the horizontal alignment to the specified value and returns the current class instance. If *value* is not specified, returns the current horizontal alignment.
|
|
471
|
+
@param {String} [*value* = "center"] Supports `"left"` and `"center"` and `"right"`.
|
|
472
|
+
@chainable
|
|
473
|
+
*/ key: "align",
|
|
474
|
+
value: function align(_) {
|
|
475
|
+
return arguments.length ? (this._align = _, this) : this._align;
|
|
476
|
+
}
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
/**
|
|
480
|
+
@memberof Legend
|
|
481
|
+
@desc If *data* is specified, sets the data array to the specified array and returns the current class instance. If *data* is not specified, returns the current data array. A shape key will be drawn for each object in the array.
|
|
482
|
+
@param {Array} [*data* = []]
|
|
483
|
+
@chainable
|
|
484
|
+
*/ key: "data",
|
|
485
|
+
value: function data(_) {
|
|
486
|
+
return arguments.length ? (this._data = _, this) : this._data;
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
{
|
|
490
|
+
/**
|
|
491
|
+
@memberof Legend
|
|
492
|
+
@desc Sets the flow of the items inside the legend. If no value is passed, the current flow will be returned.
|
|
493
|
+
@param {String} [*value* = "row"]
|
|
494
|
+
@chainable
|
|
495
|
+
*/ key: "direction",
|
|
496
|
+
value: function direction(_) {
|
|
497
|
+
return arguments.length ? (this._direction = _, this) : this._direction;
|
|
498
|
+
}
|
|
499
|
+
},
|
|
500
|
+
{
|
|
501
|
+
/**
|
|
502
|
+
@memberof Legend
|
|
503
|
+
@desc If *value* is specified, sets the transition duration of the legend and returns the current class instance. If *value* is not specified, returns the current duration.
|
|
504
|
+
@param {Number} [*value* = 600]
|
|
505
|
+
@chainable
|
|
506
|
+
*/ key: "duration",
|
|
507
|
+
value: function duration(_) {
|
|
508
|
+
return arguments.length ? (this._duration = _, this) : this._duration;
|
|
509
|
+
}
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
/**
|
|
513
|
+
@memberof Legend
|
|
514
|
+
@desc If *value* is specified, sets the overall height of the legend and returns the current class instance. If *value* is not specified, returns the current height value.
|
|
515
|
+
@param {Number} [*value* = 100]
|
|
516
|
+
@chainable
|
|
517
|
+
*/ key: "height",
|
|
518
|
+
value: function height(_) {
|
|
519
|
+
return arguments.length ? (this._height = _, this) : this._height;
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
/**
|
|
524
|
+
@memberof Legend
|
|
525
|
+
@desc If *value* is specified, sets the hover method for all shapes to the specified function and returns the current class instance. If *value* is not specified, returns the current hover method.
|
|
526
|
+
@param {Function} [*value*]
|
|
527
|
+
@chainable
|
|
528
|
+
*/ key: "hover",
|
|
529
|
+
value: function hover(_) {
|
|
530
|
+
this._shapes.forEach(function(s) {
|
|
531
|
+
return s.hover(_);
|
|
532
|
+
});
|
|
533
|
+
return this;
|
|
534
|
+
}
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
/**
|
|
538
|
+
@memberof Legend
|
|
539
|
+
@desc If *value* is specified, sets the id accessor to the specified function and returns the current class instance. If *value* is not specified, returns the current id accessor.
|
|
540
|
+
@param {Function} [*value*]
|
|
541
|
+
@chainable
|
|
542
|
+
@example
|
|
543
|
+
function value(d) {
|
|
544
|
+
return d.id;
|
|
545
|
+
}
|
|
546
|
+
*/ key: "id",
|
|
547
|
+
value: function id(_) {
|
|
548
|
+
return arguments.length ? (this._id = _, this) : this._id;
|
|
549
|
+
}
|
|
550
|
+
},
|
|
551
|
+
{
|
|
552
|
+
/**
|
|
553
|
+
@memberof Legend
|
|
554
|
+
@desc If *value* is specified, sets the label accessor to the specified function or string and returns the current class instance. If *value* is not specified, returns the current label accessor, which is the [id](#shape.id) accessor by default.
|
|
555
|
+
@param {Function|String} [*value*]
|
|
556
|
+
@chainable
|
|
557
|
+
*/ key: "label",
|
|
558
|
+
value: function label(_) {
|
|
559
|
+
return arguments.length ? (this._label = typeof _ === "function" ? _ : constant(_), this) : this._label;
|
|
560
|
+
}
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
/**
|
|
564
|
+
@memberof Legend
|
|
565
|
+
@desc If called after the elements have been drawn to DOM, will returns the outer bounds of the legend content.
|
|
566
|
+
@example
|
|
567
|
+
{"width": 180, "height": 24, "x": 10, "y": 20}
|
|
568
|
+
*/ key: "outerBounds",
|
|
569
|
+
value: function outerBounds() {
|
|
570
|
+
return this._outerBounds;
|
|
571
|
+
}
|
|
572
|
+
},
|
|
573
|
+
{
|
|
574
|
+
/**
|
|
575
|
+
@memberof Legend
|
|
576
|
+
@desc If *value* is specified, sets the padding between each key to the specified number and returns the current class instance. If *value* is not specified, returns the current padding value.
|
|
577
|
+
@param {Number} [*value* = 10]
|
|
578
|
+
@chainable
|
|
579
|
+
*/ key: "padding",
|
|
580
|
+
value: function padding(_) {
|
|
581
|
+
return arguments.length ? (this._padding = _, this) : this._padding;
|
|
582
|
+
}
|
|
583
|
+
},
|
|
584
|
+
{
|
|
585
|
+
/**
|
|
586
|
+
@memberof Legend
|
|
587
|
+
@desc If *selector* is specified, sets the SVG container element to the specified d3 selector or DOM element and returns the current class instance. If *selector* is not specified, returns the current SVG container element.
|
|
588
|
+
@param {String|HTMLElement} [*selector* = d3.select("body").append("svg")]
|
|
589
|
+
@chainable
|
|
590
|
+
*/ key: "select",
|
|
591
|
+
value: function select1(_) {
|
|
592
|
+
if (arguments.length) {
|
|
593
|
+
this._select = select(_);
|
|
594
|
+
this._rtl = detectRTL(this._select.node());
|
|
595
|
+
return this;
|
|
596
|
+
}
|
|
597
|
+
return this._select;
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
/**
|
|
602
|
+
@memberof Legend
|
|
603
|
+
@desc If *value* is specified, sets the shape accessor to the specified function or string and returns the current class instance. If *value* is not specified, returns the current shape accessor.
|
|
604
|
+
@param {Function|String} [*value* = "Rect"]
|
|
605
|
+
@chainable
|
|
606
|
+
*/ key: "shape",
|
|
607
|
+
value: function shape(_) {
|
|
608
|
+
return arguments.length ? (this._shape = typeof _ === "function" ? _ : constant(_), this) : this._shape;
|
|
609
|
+
}
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
/**
|
|
613
|
+
@memberof Legend
|
|
614
|
+
@desc If *config* is specified, sets the methods that correspond to the key/value pairs for each shape and returns the current class instance. If *config* is not specified, returns the current shape configuration.
|
|
615
|
+
@param {Object} [*config* = {}]
|
|
616
|
+
@chainable
|
|
617
|
+
*/ key: "shapeConfig",
|
|
618
|
+
value: function shapeConfig(_) {
|
|
619
|
+
return arguments.length ? (this._shapeConfig = assign(this._shapeConfig, _), this) : this._shapeConfig;
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
/**
|
|
624
|
+
@memberof Legend
|
|
625
|
+
@desc If *value* is specified, sets the title of the legend and returns the current class instance. If *value* is not specified, returns the current title.
|
|
626
|
+
@param {String} [*value*]
|
|
627
|
+
@chainable
|
|
628
|
+
*/ key: "title",
|
|
629
|
+
value: function title(_) {
|
|
630
|
+
return arguments.length ? (this._title = _, this) : this._title;
|
|
631
|
+
}
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
/**
|
|
635
|
+
@memberof Legend
|
|
636
|
+
@desc If *value* is specified, sets the title configuration of the legend and returns the current class instance. If *value* is not specified, returns the current title configuration.
|
|
637
|
+
@param {Object} [*value*]
|
|
638
|
+
@chainable
|
|
639
|
+
*/ key: "titleConfig",
|
|
640
|
+
value: function titleConfig(_) {
|
|
641
|
+
return arguments.length ? (this._titleConfig = assign(this._titleConfig, _), this) : this._titleConfig;
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
/**
|
|
646
|
+
@memberof Legend
|
|
647
|
+
@desc If *value* is specified, sets the vertical alignment to the specified value and returns the current class instance. If *value* is not specified, returns the current vertical alignment.
|
|
648
|
+
@param {String} [*value* = "middle"] Supports `"top"` and `"middle"` and `"bottom"`.
|
|
649
|
+
@chainable
|
|
650
|
+
*/ key: "verticalAlign",
|
|
651
|
+
value: function verticalAlign(_) {
|
|
652
|
+
return arguments.length ? (this._verticalAlign = _, this) : this._verticalAlign;
|
|
653
|
+
}
|
|
654
|
+
},
|
|
655
|
+
{
|
|
656
|
+
/**
|
|
657
|
+
@memberof Legend
|
|
658
|
+
@desc If *value* is specified, sets the overall width of the legend and returns the current class instance. If *value* is not specified, returns the current width value.
|
|
659
|
+
@param {Number} [*value* = 400]
|
|
660
|
+
@chainable
|
|
661
|
+
*/ key: "width",
|
|
662
|
+
value: function width(_) {
|
|
663
|
+
return arguments.length ? (this._width = _, this) : this._width;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
]);
|
|
667
|
+
return Legend;
|
|
668
|
+
}(BaseClass);
|
|
669
|
+
/**
|
|
670
|
+
@class Legend
|
|
671
|
+
@extends BaseClass
|
|
672
|
+
@desc Creates an SVG scale based on an array of data. If *data* is specified, immediately draws based on the specified array and returns the current class instance. If *data* is not specified on instantiation, it can be passed/updated after instantiation using the [data](#shape.data) method.
|
|
673
|
+
*/ export { Legend as default };
|