g6/plugins/tool.highlightSubgraph/index.js

100 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-07-13 16:27:12 +08:00
/**
* @fileOverview fisheye zoom
* @author shiwu.wyy@antfin.com
*/
const G6 = require('@antv/g6');
const Util = G6.Util;
function getMaskRect(graph) {
const width = graph.getWidth();
const height = graph.getHeight();
const tl = graph.getPointByDom({
x: 0,
y: 0
});
const br = graph.getPointByDom({
x: width,
y: height
});
return {
x: tl.x,
y: tl.y,
width: br.x - tl.x,
height: br.y - tl.y
};
}
2018-07-13 16:27:12 +08:00
G6.registerGuide('mask', {
draw(item) {
const group = item.getGraphicGroup();
const graph = item.getGraph();
const maskRect = getMaskRect(graph);
2018-09-17 22:22:42 +08:00
const model = item.getModel();
const { style } = model;
2018-07-13 16:27:12 +08:00
return group.addShape('rect', {
attrs: {
...maskRect,
2018-09-17 22:22:42 +08:00
...style
2018-07-13 16:27:12 +08:00
},
capture: false
});
},
bboxCalculation: false
2018-07-13 16:27:12 +08:00
});
class Plugin {
constructor(options) {
2018-09-17 22:22:42 +08:00
Util.mix(this, {
maskStyle: {
fill: 'rgb(255, 255, 255)',
fillOpacity: 0.8
}
}, options);
2018-07-13 16:27:12 +08:00
}
init() {
2018-08-31 20:58:48 +08:00
const graph = this.graph;
graph.on('afterinit', () => {
2018-07-13 16:27:12 +08:00
this.graph.highlightSubgraph = this.highlightSubgraph;
2018-08-08 10:08:34 +08:00
this.graph.unhighlightGraph = this.unhighlightGraph;
});
graph.on('afterchange', () => {
let mask = graph.find('mask');
if (!mask) {
mask = graph.add('guide', {
shape: 'mask',
2018-09-17 22:22:42 +08:00
id: 'mask',
style: this.maskStyle
});
mask.hide();
}
});
graph.on('afterviewportchange', () => {
const mask = graph.find('mask');
if (mask) {
mask.forceUpdate();
}
2018-07-13 16:27:12 +08:00
});
}
2018-08-31 20:58:48 +08:00
highlightSubgraph(items) {
2018-08-08 10:08:34 +08:00
this.unhighlightGraph();
const mask = this.find('mask');
mask.toFront();
mask.show();
2018-09-03 17:40:47 +08:00
items.forEach(item => {
const group = item.getGraphicGroup();
group.toFront();
2018-07-13 16:27:12 +08:00
});
this.draw();
2018-07-13 16:27:12 +08:00
}
2018-08-08 10:08:34 +08:00
unhighlightGraph() {
2018-07-13 16:27:12 +08:00
// hide the mask
this.find('mask').hide();
this.draw();
2018-07-13 16:27:12 +08:00
}
}
G6.Plugins['tool.highlightSubgraph'] = Plugin;
module.exports = Plugin;