Skip to content

Commit 693dfe9

Browse files
authored
feat: support display video (#23)
1 parent 11747e8 commit 693dfe9

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

lib/d3-tree.js

+56-41
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class D3Tree {
2020
this.container = null;
2121
this.layerDepth = null;
2222
this.maxLayerDepth = 0;
23-
this.imageHeight = 100;
24-
this.imageWidth = 100;
23+
this.resourceHeight = 100;
24+
this.resourceWidth = 100;
2525
this.transition = null;
2626
this.textLength = 0;
2727
this.fontSize = 14;
@@ -34,8 +34,8 @@ class D3Tree {
3434
selector: 'body',
3535
prefixClass: 'd3-tree',
3636
data: {},
37-
imageHeight: 200,
38-
imageWidth: 200,
37+
resourceHeight: 200,
38+
resourceWidth: 200,
3939
imageMargin: 10
4040
}, options);
4141
}
@@ -103,15 +103,15 @@ class D3Tree {
103103
return maxLayerDepth;
104104
}
105105

106-
checkImageHeight(root) {
106+
checkResourceHeight(root) {
107107
if (root.children) {
108108
let imageMargin = root.children.length > 1 ? root.children[1].x - root.children[0].x : 0;
109-
if (this.imageHeight + this.options.imageMargin > imageMargin && imageMargin) {
110-
this.imageHeight = imageMargin - this.options.imageMargin;
111-
this.imageWidth = this.imageHeight * this.options.imageWidth / this.options.imageHeight;
109+
if (this.resourceHeight + this.options.imageMargin > imageMargin && imageMargin) {
110+
this.resourceHeight = imageMargin - this.options.imageMargin;
111+
this.resourceWidth = this.resourceHeight * this.options.resourceWidth / this.options.resourceHeight;
112112
}
113113
root.children.forEach(child => {
114-
this.checkImageHeight(child);
114+
this.checkResourceHeight(child);
115115
});
116116
}
117117
}
@@ -121,16 +121,16 @@ class D3Tree {
121121
this.root = d3.hierarchy(this.options.data);
122122
this.layerDepth = Array(this.root.height + 1).fill(0);
123123
this.maxLayerDepth = this.findMaxLayer(this.root, 0, this.layerDepth);
124-
this.imageHeight = this.options.height / Math.max(...this.maxLayerDepth) >= this.options.imageHeight ?
125-
this.options.imageHeight :
124+
this.resourceHeight = this.options.height / Math.max(...this.maxLayerDepth) >= this.options.resourceHeight ?
125+
this.options.resourceHeight :
126126
this.options.height / Math.max(...this.maxLayerDepth) - 10;
127-
this.imageWidth = this.imageHeight * this.options.imageWidth / this.options.imageHeight;
127+
this.resourceWidth = this.resourceHeight * this.options.resourceWidth / this.options.resourceHeight;
128128
this.tree = d3.tree().size([
129129
this.options.height,
130-
this.options.width - this.imageWidth - this.fontSize * this.textLength
130+
this.options.width - this.resourceWidth - this.fontSize * this.textLength
131131
]);
132132
this.tree(this.root);
133-
this.checkImageHeight(this.root);
133+
this.checkResourceHeight(this.root);
134134

135135
this
136136
.container
@@ -140,7 +140,7 @@ class D3Tree {
140140
.append('path')
141141
.attr('class', `${this.options.prefixClass}-link`)
142142
.attr('d', d => {
143-
return `M${d.y},${d.x}C${d.parent.y + this.imageWidth},${d.x} ${d.parent.y + this.imageWidth + 110},${d.parent.x} ${d.parent.y + this.imageWidth + 10},${d.parent.x}`;
143+
return `M${d.y},${d.x}C${d.parent.y + this.resourceWidth},${d.x} ${d.parent.y + this.resourceWidth + 110},${d.parent.x} ${d.parent.y + this.resourceWidth + 10},${d.parent.x}`;
144144
});
145145

146146
const node = this
@@ -154,32 +154,47 @@ class D3Tree {
154154
return `translate(${d.y},${d.x})`;
155155
})
156156
.each(function(d) {
157-
let imageList = d.data.image;
158-
if (imageList && !!~imageList.indexOf('[')) {
159-
imageList = imageList
157+
let resourceList = d.data.image;
158+
if (resourceList && !!~resourceList.indexOf('[')) {
159+
resourceList = resourceList
160160
.replace(/(\[\n|\n\]| )/g, '')
161161
.split('\n');
162162
} else {
163-
imageList = [imageList];
163+
resourceList = [resourceList];
164164
}
165165

166-
const imageWidth = that.imageWidth / imageList.length;
167-
const imageHeight = that.imageHeight / imageList.length;
168-
169-
imageList.forEach((image, index) => {
170-
d3.select(this)
171-
.append('image')
172-
.attr('xlink:href', image)
173-
.attr('data-index', index)
174-
.attr('height', d => {
175-
return imageHeight;
176-
})
177-
.attr('transform', d => {
178-
return `translate(${imageWidth * index}, -${imageHeight / 2})`;
179-
})
180-
.attr('width', d => {
181-
return imageWidth;
182-
});
166+
const resourceWidth = that.resourceWidth / resourceList.length;
167+
const resourceHeight = that.resourceHeight / resourceList.length;
168+
169+
resourceList.forEach((resource, index) => {
170+
if (resource && resource.endsWith('.webm')) {
171+
d3.select(this)
172+
.append('video')
173+
.attr('src', resource)
174+
.attr('type', 'video/webm')
175+
.attr('controls', 'controls')
176+
.attr('data-index', index)
177+
.attr('style', d => {
178+
return `width: ${resourceWidth}px; height: ${resourceHeight}px`;
179+
})
180+
.attr('transform', d => {
181+
return `translate(${resourceWidth * index}, -${resourceHeight / 2})`;
182+
});
183+
} else {
184+
d3.select(this)
185+
.append('image')
186+
.attr('xlink:href', resource)
187+
.attr('data-index', index)
188+
.attr('height', d => {
189+
return resourceHeight;
190+
})
191+
.attr('transform', d => {
192+
return `translate(${resourceWidth * index}, -${resourceHeight / 2})`;
193+
})
194+
.attr('width', d => {
195+
return resourceWidth;
196+
});
197+
}
183198
});
184199
});
185200

@@ -193,10 +208,10 @@ class D3Tree {
193208
const isMulti = lineCount !== 1;
194209
let heightHalf = isMulti ? this.fontSize * this.lineHeight * (lineCount + 1) / 2 : 0;
195210
if (d.data.image) {
196-
return `translate(${that.imageWidth + 10}, ${-heightHalf})`;
211+
return `translate(${that.resourceWidth + 10}, ${-heightHalf})`;
197212
} else {
198213
const textLength = d.data && d.data.text && d.data.text.length || 0;
199-
return `translate(${that.imageWidth / 2 - 2 * textLength}, ${-heightHalf})`;
214+
return `translate(${that.resourceWidth / 2 - 2 * textLength}, ${-heightHalf})`;
200215
}
201216
});
202217
};
@@ -207,7 +222,7 @@ class D3Tree {
207222
this.transition = this.container.transition().duration(this.options.duration);
208223
this.transition.selectAll(`.${this.options.prefixClass}-link`)
209224
.attr('d', d => {
210-
return `M${d.y},${d.x}C${d.parent.y + this.imageWidth},${d.x} ${d.parent.y + this.imageWidth + 110},${d.parent.x} ${d.parent.y + this.imageWidth + 10},${d.parent.x}`;
225+
return `M${d.y},${d.x}C${d.parent.y + this.resourceWidth},${d.x} ${d.parent.y + this.resourceWidth + 110},${d.parent.x} ${d.parent.y + this.resourceWidth + 10},${d.parent.x}`;
211226
});
212227
this.transition.selectAll(`.${this.options.prefixClass}-node`)
213228
.attr('transform', d => {
@@ -216,10 +231,10 @@ class D3Tree {
216231
this.transition.selectAll('text')
217232
.attr('transform', d => {
218233
if (d.data.image) {
219-
return `translate(${this.imageWidth + 10}, 4)`;
234+
return `translate(${this.resourceWidth + 10}, 4)`;
220235
} else {
221236
const textLength = d.data && d.data.text && d.data.text.length || 0;
222-
return `translate(${this.imageWidth / 2 - 2 * textLength}, 4)`;
237+
return `translate(${this.resourceWidth / 2 - 2 * textLength}, 4)`;
223238
}
224239
});
225240
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "d3-tree",
3-
"version": "1.0.24",
3+
"version": "1.0.25",
44
"description": "tree view based on d3",
55
"keywords": [
66
"d3",

0 commit comments

Comments
 (0)