169 lines
4.0 KiB
Java
169 lines
4.0 KiB
Java
|
|
/**
|
|||
|
|
* @fileOverview 工厂类,管理各种类型的 shape
|
|||
|
|
* @author dxq613@gmail.com
|
|||
|
|
*/
|
|||
|
|
var Util = require('../../util');
|
|||
|
|
|
|||
|
|
var PathUtil = require('../util/path');
|
|||
|
|
|
|||
|
|
var GPath = Util.PathUtil;
|
|||
|
|
var Shape = {};
|
|||
|
|
var ShapeBase = {
|
|||
|
|
_coord: null,
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 绘制图形
|
|||
|
|
* @param {Object} cfg 配置项
|
|||
|
|
* @param {Object} container 容器
|
|||
|
|
* @return {Object} shape 创建的 shape
|
|||
|
|
*/
|
|||
|
|
draw: function draw(cfg, container) {
|
|||
|
|
if (this.drawShape) {
|
|||
|
|
return this.drawShape(cfg, container);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取绘制图形需要的点, 可以不定义,则使用默认的
|
|||
|
|
getPoints(cfg) {
|
|||
|
|
if (this.getShapePoints) {
|
|||
|
|
return this.getShapePoints(cfg);
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
},*/
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 设置坐标系
|
|||
|
|
* @param {Coord} coord 坐标系
|
|||
|
|
*/
|
|||
|
|
setCoord: function setCoord(coord) {
|
|||
|
|
this._coord = coord;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 0~1 path 转 画布 path
|
|||
|
|
* @param {path} path 路径
|
|||
|
|
* @param {Boolean} islineToArc 是否转换成圆弧
|
|||
|
|
* @return {path} path 转换到画布坐标的path
|
|||
|
|
*/
|
|||
|
|
parsePath: function parsePath(path, islineToArc) {
|
|||
|
|
var coord = this._coord;
|
|||
|
|
path = GPath.parsePathString(path);
|
|||
|
|
|
|||
|
|
if (coord.isPolar && islineToArc !== false) {
|
|||
|
|
path = PathUtil.convertPolarPath(coord, path);
|
|||
|
|
} else {
|
|||
|
|
path = PathUtil.convertNormalPath(coord, path);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return path;
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 0~1 point 转 画布 point
|
|||
|
|
* @param {point} point 节点
|
|||
|
|
* @return {point} point 转换后的点
|
|||
|
|
*/
|
|||
|
|
parsePoint: function parsePoint(point) {
|
|||
|
|
var coord = this._coord;
|
|||
|
|
return coord.convertPoint(point);
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 0~1 points 转 画布 points
|
|||
|
|
* @param {points} points 节点集合
|
|||
|
|
* @return {points} points 转换后的多个节点
|
|||
|
|
*/
|
|||
|
|
parsePoints: function parsePoints(points) {
|
|||
|
|
var coord = this._coord;
|
|||
|
|
var rst = [];
|
|||
|
|
Util.each(points, function (point) {
|
|||
|
|
rst.push(coord.convertPoint(point));
|
|||
|
|
});
|
|||
|
|
return rst;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
var ShapeFactoryBase = {
|
|||
|
|
defaultShapeType: null,
|
|||
|
|
setCoord: function setCoord(coord) {
|
|||
|
|
this._coord = coord;
|
|||
|
|
},
|
|||
|
|
getShape: function getShape(type) {
|
|||
|
|
var self = this;
|
|||
|
|
|
|||
|
|
if (Util.isArray(type)) {
|
|||
|
|
type = type[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var shape = self[type] || self[self.defaultShapeType];
|
|||
|
|
shape._coord = self._coord;
|
|||
|
|
return shape;
|
|||
|
|
},
|
|||
|
|
getShapePoints: function getShapePoints(type, cfg) {
|
|||
|
|
var shape = this.getShape(type);
|
|||
|
|
var fn = shape.getPoints || shape.getShapePoints || this.getDefaultPoints;
|
|||
|
|
var points = fn(cfg);
|
|||
|
|
return points;
|
|||
|
|
},
|
|||
|
|
getDefaultPoints: function getDefaultPoints()
|
|||
|
|
/* cfg */
|
|||
|
|
{
|
|||
|
|
return [];
|
|||
|
|
},
|
|||
|
|
getMarkerCfg: function getMarkerCfg(type, cfg) {
|
|||
|
|
var shape = this.getShape(type);
|
|||
|
|
|
|||
|
|
if (!shape.getMarkerCfg) {
|
|||
|
|
var defaultShapeType = this.defaultShapeType;
|
|||
|
|
shape = this.getShape(defaultShapeType);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return shape.getMarkerCfg(cfg);
|
|||
|
|
},
|
|||
|
|
getSelectedCfg: function getSelectedCfg()
|
|||
|
|
/* type, cfg */
|
|||
|
|
{
|
|||
|
|
return {};
|
|||
|
|
},
|
|||
|
|
drawShape: function drawShape(type, cfg, container) {
|
|||
|
|
var shape = this.getShape(type);
|
|||
|
|
var gShape = shape.draw(cfg, container);
|
|||
|
|
|
|||
|
|
if (gShape) {
|
|||
|
|
gShape.setSilent('origin', cfg.origin);
|
|||
|
|
gShape._id = cfg.yIndex ? cfg._id + cfg.yIndex : cfg._id;
|
|||
|
|
gShape.name = this.name;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return gShape;
|
|||
|
|
}
|
|||
|
|
}; // 注册 Geometry 获取图形的入口
|
|||
|
|
|
|||
|
|
Shape.registerFactory = function (factoryName, cfg) {
|
|||
|
|
var className = Util.upperFirst(factoryName);
|
|||
|
|
var geomObj = Util.assign({}, ShapeFactoryBase, cfg);
|
|||
|
|
Shape[className] = geomObj;
|
|||
|
|
geomObj.name = factoryName;
|
|||
|
|
return geomObj;
|
|||
|
|
}; // 注册图形
|
|||
|
|
|
|||
|
|
|
|||
|
|
Shape.registerShape = function (factoryName, shapeType, cfg) {
|
|||
|
|
var className = Util.upperFirst(factoryName);
|
|||
|
|
var factory = Shape[className];
|
|||
|
|
var shapeObj = Util.assign({}, ShapeBase, cfg);
|
|||
|
|
factory[shapeType] = shapeObj;
|
|||
|
|
return shapeObj;
|
|||
|
|
}; // 获得Geom 对应的 shapeFactory
|
|||
|
|
|
|||
|
|
|
|||
|
|
Shape.getShapeFactory = function (factoryName) {
|
|||
|
|
var self = this;
|
|||
|
|
factoryName = factoryName || 'point';
|
|||
|
|
var className = Util.upperFirst(factoryName);
|
|||
|
|
return self[className];
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
module.exports = Shape;
|