g6/plugins/tool.mapper/index.js

443 lines
12 KiB
JavaScript
Raw Normal View History

2018-07-10 11:08:57 +08:00
/**
* @fileOverview G6 Mapper Plugin base on d3 tech stack
* d3-scale https://github.com/d3/d3-scale
* d3-legend https://github.com/susielu/d3-legend
* @author shiwu.wyy@antfin.com
*/
const G6 = require('@antv/g6');
const Legend = require('@antv/g2/src/component/legend');
2018-07-13 16:27:12 +08:00
const Color = require('@antv/g2/src/component/legend/color');
const Size = require('@antv/g2/src/component/legend/size');
const Attr = require('@antv/attr');
2018-07-10 11:08:57 +08:00
const Util = G6.Util;
const Scale = require('@antv/scale');
2018-07-13 16:27:12 +08:00
const G = require('@antv/g');
const Canvas = G.canvas.Canvas;
const SVG = G.svg.Canvas;
2018-07-10 11:08:57 +08:00
class Plugin {
2018-07-13 16:27:12 +08:00
constructor(itemType, dim, channel, range, otherCfg) {
2018-07-10 11:08:57 +08:00
Util.mix(this, {
/**
2018-07-13 16:27:12 +08:00
* 子项类型
* @type {String}
*/
2018-07-10 11:08:57 +08:00
itemType: null,
/**
2018-07-13 16:27:12 +08:00
* 数据纬度
* @type {String}
*/
2018-07-10 11:08:57 +08:00
dim: null,
/**
2018-07-13 16:27:12 +08:00
* 映射域
* @type {Array}
*/
range: [ 0, 1 ],
2018-07-10 11:08:57 +08:00
/**
2018-07-13 16:27:12 +08:00
* 视觉通道
* @type {String}
*/
2018-07-10 11:08:57 +08:00
channel: null,
/**
2018-07-13 16:27:12 +08:00
* 度量配置
* @type {object}
*/
2018-07-10 11:08:57 +08:00
scaleCfg: {},
/**
2018-07-13 16:27:12 +08:00
* 图例配置
* @type {object}
*/
legendCfg: {
legendTitle: '',
scale: 1
2018-07-13 16:27:12 +08:00
},
/**
* 是否数据对齐
* @type {boolean}
*/
nice: true
}, {
itemType,
dim,
channel,
range
}, otherCfg);
2018-07-10 11:08:57 +08:00
}
init() {
2018-07-13 16:27:12 +08:00
const graph = this.graph;
graph.on('beforechange', ev => {
if (ev.action === 'changeData') {
const {
data
} = ev;
this._createScale(graph.parseSource(data));
2018-07-10 11:08:57 +08:00
this._mapping();
2018-07-13 16:27:12 +08:00
this.legendCfg && this._createLegend(graph.parseSource(data));
2018-07-10 11:08:57 +08:00
}
});
}
2018-07-13 16:27:12 +08:00
_trainCategoryScale(itemType, data) {
const dim = this.dim;
2018-07-10 11:08:57 +08:00
const domainMap = {};
const domain = [];
2018-07-13 16:27:12 +08:00
data.forEach(model => {
2018-07-10 11:08:57 +08:00
if (!domainMap[model[dim]]) {
domainMap[model[dim]] = true;
domain.push(model[dim]);
}
});
return domain;
}
2018-07-13 16:27:12 +08:00
_trainNumberScale(itemType, data) {
const dim = this.dim;
2018-07-10 11:08:57 +08:00
const domain = [ Infinity, -Infinity ];
2018-07-13 16:27:12 +08:00
data.forEach(model => {
2018-07-10 11:08:57 +08:00
if (domain[0] > model[dim]) {
domain[0] = model[dim];
}
if (domain[1] < model[dim]) {
domain[1] = model[dim];
}
});
return domain;
}
2018-07-13 16:27:12 +08:00
_getScaleType(data) {
const dim = this.dim;
const scaleCfg = this.scaleCfg;
2018-07-10 11:08:57 +08:00
if (!scaleCfg.type) {
if (Util.isNumber(data[0][dim])) {
scaleCfg.type = 'linear';
} else {
scaleCfg.type = 'oridinal';
}
}
2018-07-13 16:27:12 +08:00
return Util.upperFirst(scaleCfg.type);
2018-07-10 11:08:57 +08:00
}
2018-07-13 16:27:12 +08:00
_scaleConsSelector(scaleType, scaleCfg) {
switch (scaleType) {
case 'Linear':
return new Scale.Linear(scaleCfg);
case 'Log':
return new Scale.Log(scaleCfg);
case 'Pow':
return new Scale.Pow(scaleCfg);
default:
return new Scale.Linear(scaleCfg);
}
}
_createScale(sourceData) {
const itemType = this.itemType;
const data = sourceData[itemType + 's'];
const scaleCfg = this.scaleCfg;
const scaleType = this._getScaleType(data);
const scale = this._scaleConsSelector(scaleType, scaleCfg);
const range = this.range;
scale.nice = scaleCfg.nice;
2018-07-10 11:08:57 +08:00
let domain = scaleCfg.domain;
2018-07-13 16:27:12 +08:00
scale.range = range;
2018-07-10 11:08:57 +08:00
if (!domain) {
if (scaleType === 'Ordinal') {
2018-07-13 16:27:12 +08:00
domain = this._trainCategoryScale(itemType, data);
2018-07-10 11:08:57 +08:00
} else {
2018-07-13 16:27:12 +08:00
domain = this._trainNumberScale(itemType, data);
2018-07-10 11:08:57 +08:00
}
}
2018-07-13 16:27:12 +08:00
2018-07-10 11:08:57 +08:00
const rangeLength = range.length;
const domainLength = domain.length;
if (rangeLength !== domainLength) {
const domainStep = (domain[1] - domain[0]) / (rangeLength - 1);
Util.each(range, (v, i) => {
domain[i] = domain[0] + i * domainStep;
});
}
if (domain[0] === domain[1]) {
if (domain[0] > 0) {
domain[0] = 0;
} else if (domain[0] < 0) {
domain[1] = 0;
} else {
domain[0] = -1;
}
}
2018-07-13 16:27:12 +08:00
scale.values = domain;
scale.min = domain[0];
scale.max = domain[domain.length - 1];
2018-07-10 11:08:57 +08:00
Util.isFunction(scaleCfg.callback) && scaleCfg.callback(scale, domain);
2018-07-13 16:27:12 +08:00
this.scale = scale;
2018-07-10 11:08:57 +08:00
}
2018-07-13 16:27:12 +08:00
_createLegend(sourceData) {
const itemType = this.itemType;
const data = sourceData[itemType + 's'];
const scaleType = this._getScaleType(data);
const channel = this.channel;
const graph = this.graph;
const legendContainer = Util.createDOM('<div class="legend-container"></div>', {
position: 'relative'
});
const container = graph.getGraphContainer();
container.appendChild(legendContainer);
const Constructor = graph.getConstructor(Canvas, SVG);
const canvas = new Constructor({
containerId: 'legend',
width: 500,
height: 500
2018-07-10 11:08:57 +08:00
});
let legend;
if (scaleType === 'Ordinal') {
2018-07-13 16:27:12 +08:00
legend = this._createCatLegend(canvas);
2018-07-10 11:08:57 +08:00
} else {
if (channel === 'color') {
legend = this._createContinuousColorLegend(canvas);
} else {
legend = this._createContinuousSizeLegend(canvas);
}
2018-07-13 16:27:12 +08:00
// the listener to filter nodes and edges
const slider = legend.get('slider');
slider.on('sliderchange', Util.throttle(ev => {
const domain = this.scale.values;
const cur_range = ev.range;
const dim = this.dim;
graph.addFilter(item => {
if (item.isNode) {
const val = item.model[dim];
const percent = 100 * (val - domain[0]) / (domain[domain.length - 1] - domain[0]);
if (percent > cur_range[1] || percent < cur_range[0]) return false;
return true;
} else if (item.isEdge) {
const source_val = item.source.model[dim];
const source_percent = 100 * (source_val - domain[0]) / (domain[domain.length - 1] - domain[0]);
const source_visible = (source_percent <= cur_range[1] && source_percent >= cur_range[0]);
const target_val = item.target.model[dim];
const target_percent = 100 * (target_val - domain[0]) / (domain[domain.length - 1] - domain[0]);
const target_visible = (target_percent <= cur_range[1] && target_percent >= cur_range[0]);
if (!source_visible || !target_visible) return false;
return true;
}
});
graph.filter();
}, 100));
2018-07-10 11:08:57 +08:00
}
2018-07-13 16:27:12 +08:00
2018-07-10 11:08:57 +08:00
const bbox = legend.getBBox();
const padding = 6;
const legendWidth = bbox.maxX - bbox.minX;
const legendHeight = bbox.maxY - bbox.minY;
legend.move(-bbox.minX + padding, -bbox.minY + padding);
canvas.changeSize(legendWidth + 2 * padding, legendHeight + 2 * padding);
2018-07-13 16:27:12 +08:00
this.legend = legend;
this.legendCanvas = canvas;
this.legendWidth = legendWidth;
this.legendHeight = legendHeight;
2018-07-10 11:08:57 +08:00
canvas.draw();
}
updateLegendPosition() {
2018-07-13 16:27:12 +08:00
const legend = this.legend;
2018-07-10 11:08:57 +08:00
if (!legend) {
return;
}
2018-07-13 16:27:12 +08:00
const canvas = this.legendCanvas;
const legendCfg = this.legendCfg;
2018-07-10 11:08:57 +08:00
const marginTop = legendCfg.marginTop ? legendCfg.marginTop : 0;
const marginLeft = legendCfg.marginLeft ? legendCfg.marginLeft : 0;
const marginBottom = legendCfg.marginBottom ? legendCfg.marginBottom : 0;
const marginRight = legendCfg.marginRight ? legendCfg.marginRight : 0;
const position = legendCfg.position ? legendCfg.position : 'br';
2018-07-13 16:27:12 +08:00
const graph = this.graph;
2018-07-10 11:08:57 +08:00
const graphWidth = graph.get('width');
const graphHeight = graph.get('height');
const el = canvas.get('el');
2018-07-13 16:27:12 +08:00
const legendWidth = this.legendWidth;
const legendHeight = this.legendHeight;
2018-07-10 11:08:57 +08:00
const tl = Util.getNineBoxPosition(position, {
x: 0,
y: 0,
width: graphWidth,
height: graphHeight
}, legendWidth, legendHeight, [ marginTop, marginRight, marginBottom, marginLeft ]);
el.style.position = 'absolute';
el.style.top = tl.y + 'px';
el.style.left = tl.x + 'px';
}
_createCatLegend(canvas) {
2018-07-13 16:27:12 +08:00
const scale = this.scale;
const range = scale.range;
const domain = scale.values;
const itemType = this.itemType;
const legendCfg = this.legendCfg;
2018-07-10 11:08:57 +08:00
const items = [];
const cfg = Util.mix({
items,
checkable: false
}, legendCfg);
Util.each(range, (value, i) => {
items.push({
name: domain[i],
color: value,
type: itemType === 'node' ? 'circle' : 'line',
layout: 'vertical',
marker: {
symbol: 'circle',
radius: 5,
fill: value
},
checked: true
});
});
const legend = canvas.addGroup(Legend.Category, cfg);
return legend;
}
_createContinuousColorLegend(canvas) {
2018-07-13 16:27:12 +08:00
const itemType = this.itemType;
const scale = this.scale;
const range = scale.range;
const domain = scale.values;
const legendCfg = this.legendCfg;
let lengendTitle = legendCfg.legendTitle;
if (lengendTitle === '') {
lengendTitle = this.dim;
}
if (legendCfg.scale <= 0 || typeof legendCfg.scale === 'undefined') {
legendCfg.scale = 1;
}
2018-07-10 11:08:57 +08:00
const items = [];
2018-07-13 16:27:12 +08:00
Util.each(range, (val, i) => {
const percent = (domain[i] - scale.min) / (scale.max - scale.min);
items.push({
text: domain[i],
attrValue: val,
value: domain[i],
scaleValue: percent
});
});
2018-07-10 11:08:57 +08:00
const cfg = Util.mix({
items,
2018-07-13 16:27:12 +08:00
layout: 'horizontal',
2018-07-10 11:08:57 +08:00
titleText: itemType,
title: {
2018-07-13 16:27:12 +08:00
text: lengendTitle,
2018-07-10 11:08:57 +08:00
fill: '#333',
2018-07-13 16:27:12 +08:00
textBaseline: 'middle'
2018-07-10 11:08:57 +08:00
},
width: 150 * legendCfg.scale,
height: 15 // * legendCfg.scale
2018-07-10 11:08:57 +08:00
}, legendCfg);
2018-07-13 16:27:12 +08:00
const legend = canvas.addGroup(Color, cfg);
2018-07-10 11:08:57 +08:00
return legend;
}
_createContinuousSizeLegend(canvas) {
2018-07-13 16:27:12 +08:00
const itemType = this.itemType;
const scale = this.scale;
const range = scale.range;
const domain = scale.values;
2018-07-10 11:08:57 +08:00
const domainStep = (domain[domain.length - 1] - domain[0]) / (range.length - 1);
2018-07-13 16:27:12 +08:00
const legendCfg = this.legendCfg;
let lengendTitle = legendCfg.legendTitle;
if (lengendTitle === '') {
lengendTitle = this.dim;
}
if (legendCfg.scale <= 0 || typeof legendCfg.scale === 'undefined') {
legendCfg.scale = 1;
}
2018-07-10 11:08:57 +08:00
const items = [];
2018-07-13 16:27:12 +08:00
Util.each(range, (val, i) => {
const dom = domain[0] + domainStep * i;
2018-07-10 11:08:57 +08:00
items.push({
2018-07-13 16:27:12 +08:00
text: dom,
attrValue: val * legendCfg.scale,
2018-07-13 16:27:12 +08:00
value: dom
2018-07-10 11:08:57 +08:00
});
});
const cfg = Util.mix({
items,
2018-07-13 16:27:12 +08:00
layout: 'horizontal',
2018-07-10 11:08:57 +08:00
attrType: 'size',
titleText: itemType,
title: {
2018-07-13 16:27:12 +08:00
text: lengendTitle,
2018-07-10 11:08:57 +08:00
fill: '#333',
2018-07-13 16:27:12 +08:00
textBaseline: 'middle'
2018-07-10 11:08:57 +08:00
},
width: 150 * legendCfg.scale,
height: 15 * legendCfg.scale
2018-07-10 11:08:57 +08:00
}, legendCfg);
2018-07-13 16:27:12 +08:00
const legend = canvas.addGroup(Size, cfg);
2018-07-10 11:08:57 +08:00
return legend;
}
_mapping() {
2018-07-13 16:27:12 +08:00
const graph = this.graph;
const dim = this.dim;
const itemType = this.itemType;
const scale = this.scale;
const channel = this.channel;
const domain = scale.values;
const range = scale.range;
let color;
if (channel === 'color') {
const colorScale = this._scaleSelector(scale.type, domain);
color = new Attr.Color({
scales: [ colorScale ],
values: range
});
}
2018-07-10 11:08:57 +08:00
graph[itemType]()[channel](model => {
if (itemType === 'node' && channel === 'size') {
2018-07-13 16:27:12 +08:00
return scale.scale(model[dim]) * 2;
} else if (channel === 'color') {
return color.mapping(model[dim])[0];
} else if (itemType === 'edge' && channel === 'size') {
return scale.scale(model[dim]);
2018-07-10 11:08:57 +08:00
}
2018-07-13 16:27:12 +08:00
return scale.scale(model[dim]);
2018-07-10 11:08:57 +08:00
});
}
_checkInput() {
2018-07-13 16:27:12 +08:00
const itemType = this.itemType;
const graph = this.graph;
2018-07-10 11:08:57 +08:00
const itemModels = graph.get(itemType + 's');
return graph && itemModels && itemModels.length > 0;
}
2018-07-13 16:27:12 +08:00
_scaleSelector(type, domain) {
const params = {
min: domain[0],
max: domain[domain.length - 1]
};
switch (type) {
case 'Linear':
return Scale.linear({
min: domain[0],
max: domain[domain.length - 1]
});
case 'Identity':
return Scale.identity({
value: 'red'
});
case 'Ordinal':
return Scale.cat({
values: domain
});
default:
return Scale.linear(params);
}
}
2018-07-10 11:08:57 +08:00
}
G6.Plugins['tool.mapper'] = Plugin;
module.exports = Plugin;