提交 5628eda4 编写于 作者: K kener

命名改变

上级 2b44ffe0
/**
* zrender
*
* @author Kener (@Kener-林峰, linzhifeng@baidu.com)
*
* shape类:蜡烛
* 可配图形属性:
{
// 基础属性
shape : 'candle', // 必须,shape类标识,需要显式指定
id : {string}, // 必须,图形唯一标识,可通过'zrender/tool/guid'方法生成
zlevel : {number}, // 默认为0,z层level,决定绘画在哪层canvas中
invisible : {boolean}, // 默认为false,是否可见
// 样式属性,默认状态样式样式属性
style : {
x : {number}, // 必须,横坐标
y : {Array}, // 必须,纵坐标数组
},
// 样式属性,高亮样式属性,当不存在highlightStyle时使用基于默认样式扩展显示
highlightStyle : {
// 同style
}
// 交互属性,详见shape.Base
// 事件属性,详见shape.Base
}
例子:
{
shape : 'candle',
id : '123456',
zlevel : 1,
style : {
x : 200,
y : [100,123,90,125],
width : 150,
color : '#eee',
text : 'Baidu'
},
myName : 'kener', // 可自带任何有效自定义属性
clickable : true,
onClick : function(eventPacket) {
alert(eventPacket.target.myName);
}
}
*/
define(
function(require) {
var Base = require('zrender/shape/Base');
var matrix = require('zrender/tool/matrix');
function Candle(options) {
Base.call(this, options);
}
Candle.prototype = {
type: 'candle',
_numberOrder : function(a, b) {
return b - a;
},
/**
* 创建矩形路径
* @param {Context2D} ctx Canvas 2D上下文
* @param {Object} style 样式
*/
buildPath : function(ctx, style) {
style.y.sort(this._numberOrder);
ctx.moveTo(style.x, style.y[3]);
ctx.lineTo(style.x, style.y[2]);
ctx.moveTo(style.x - style.width / 2, style.y[2]);
ctx.rect(
style.x - style.width / 2,
style.y[2],
style.width,
style.y[1] - style.y[2]
);
ctx.moveTo(style.x, style.y[1]);
ctx.lineTo(style.x, style.y[0]);
return;
},
/**
* 返回矩形区域,用于局部刷新和文字定位
* @param {Object} style
*/
getRect : function(style) {
if (style.__rect) {
return style.__rect;
}
var lineWidth;
if (style.brushType == 'stroke' || style.brushType == 'fill') {
lineWidth = style.lineWidth || 1;
}
else {
lineWidth = 0;
}
style.__rect = {
x : Math.round(style.x - style.width / 2 - lineWidth / 2),
y : Math.round(style.y[3] - lineWidth / 2),
width : style.width + lineWidth,
height : style.y[0] - style.y[3] + lineWidth
};
return style.__rect;
},
isCover : function(x, y) {
//对鼠标的坐标也做相同的变换
if(this.needTransform && this._transform){
var inverseMatrix = [];
matrix.invert(inverseMatrix, this._transform);
var originPos = [x, y];
matrix.mulVector(originPos, inverseMatrix, [x, y, 1]);
if (x == originPos[0] && y == originPos[1]) {
// 避免外部修改导致的__needTransform不准确
this.updateNeedTransform();
}
x = originPos[0];
y = originPos[1];
}
// 快速预判并保留判断矩形
var rect = this.style.__rect;
if (!rect) {
rect = this.style.__rect = this.getRect(this.style);
}
if (x >= rect.x
&& x <= (rect.x + rect.width)
&& y >= rect.y
&& y <= (rect.y + rect.height)
) {
// 矩形内
return true;
}
return false;
}
};
require('zrender/tool/util').inherits(Candle, Base);
return Candle;
}
);
\ No newline at end of file
/**
* zrender
*
* @author pissang (https://github.com/pissang)
*
* shape类:chord
* 可配图形属性:
{
// 基础属性
shape : 'chord', // 必须,shape类标识,需要显式指定
id : {string}, // 必须,图形唯一标识,可通过'zrender/tool/guid'方法生成
zlevel : {number}, // 默认为0,z层level,决定绘画在哪层canvas中
invisible : {boolean}, // 默认为false,是否可见
// 样式属性,默认状态样式样式属性
style : {
center : {array},
source0 : {number},
source1 : {number},
target0 : {number},
target1 : {number},
r : {number},
},
// 样式属性,高亮样式属性,当不存在highlightStyle时使用基于默认样式扩展显示
highlightStyle : {
// 同style
}
// 交互属性,详见shape.Base
// 事件属性,详见shape.Base
}
*/
define(function(require) {
var Base = require('zrender/shape/Base');
var matrix = require('zrender/tool/matrix');
var util = require('zrender/tool/util');
var _ctx = util.getContext();
function ChordShape(options) {
Base.call(this, options);
}
ChordShape.prototype = {
type : 'chord',
// center, source0, source1, target0, target1, r
buildPath : function(ctx, style) {
var PI2 = Math.PI * 2;
var cx = style.center[0];
var cy = style.center[1];
var r = style.r;
var s0 = style.source0 / 180 * Math.PI;
var s1 = style.source1 / 180 * Math.PI;
var t0 = style.target0 / 180 * Math.PI;
var t1 = style.target1 / 180 * Math.PI;
var sx0 = cx + Math.cos(PI2 - s0) * r;
var sy0 = cy - Math.sin(PI2 - s0) * r;
var sx1 = cx + Math.cos(PI2 - s1) * r;
var sy1 = cy - Math.sin(PI2 - s1) * r;
var tx0 = cx + Math.cos(PI2 - t0) * r;
var ty0 = cy - Math.sin(PI2 - t0) * r;
var tx1 = cx + Math.cos(PI2 - t1) * r;
var ty1 = cy - Math.sin(PI2 - t1) * r;
ctx.moveTo(sx0, sy0);
ctx.arc(cx, cy, style.r, s0, s1, false);
ctx.bezierCurveTo(
(cx - sx1) * 0.70 + sx1,
(cy - sy1) * 0.70 + sy1,
(cx - tx0) * 0.70 + tx0,
(cy - ty0) * 0.70 + ty0,
tx0, ty0
);
// Chord to self
if (style.source0 === style.target0 &&
style.source1 === style.target1) {
return;
}
ctx.arc(cx, cy, style.r, t0, t1, false);
ctx.bezierCurveTo(
(cx - tx1) * 0.70 + tx1,
(cy - ty1) * 0.70 + ty1,
(cx - sx0) * 0.70 + sx0,
(cy - sy0) * 0.70 + sy0,
sx0, sy0
);
},
getRect : function(){
return {
x : 0,
y : 0,
width : 0,
height : 0
};
},
isCover : function(x, y) {
if (!_ctx.isPointInPath) { // In ie
return false;
}
//对鼠标的坐标也做相同的变换
if(this.needTransform && this._transform){
var inverseMatrix = [];
matrix.invert(inverseMatrix, this._transform);
var originPos = [x, y];
matrix.mulVector(originPos, inverseMatrix, [x, y, 1]);
if (x == originPos[0] && y == originPos[1]) {
// 避免外部修改导致的__needTransform不准确
this.updateNeedTransform();
}
x = originPos[0];
y = originPos[1];
}
_ctx.beginPath();
ChordShape.prototype.buildPath.call(null, _ctx, this.style);
_ctx.closePath();
return _ctx.isPointInPath(x, y);
}
};
require('zrender/tool/util').inherits(ChordShape, Base);
return ChordShape;
});
\ No newline at end of file
/**
* zrender
*
* @author Kener (@Kener-林峰, linzhifeng@baidu.com)
*
* shape类:支持半平滑的polygon,折线面积图使用
* 可配图形属性:
{
// 基础属性
shape : 'halfSmoothPolygon', // 必须,shape类标识,需要显式指定
id : {string}, // 必须,图形唯一标识,可通过'zrender/tool/guid'方法生成
zlevel : {number}, // 默认为0,z层level,决定绘画在哪层canvas中
invisible : {boolean}, // 默认为false,是否可见
// 样式属性,默认状态样式样式属性
style : {
pointList : {Array}, // 必须,多边形各个顶角坐标
},
// 样式属性,高亮样式属性,当不存在highlightStyle时使用基于默认样式扩展显示
highlightStyle : {
// 同style
}
// 交互属性,详见shape.Base
// 事件属性,详见shape.Base
}
例子:
{
shape : 'halfSmoothPolygon',
id : '123456',
zlevel : 1,
style : {
pointList : [[10, 10], [300, 20], [298, 400], [50, 450]]
color : '#eee',
text : 'Baidu'
},
myName : 'kener', // 可自带任何有效自定义属性
clickable : true,
onClick : function(eventPacket) {
alert(eventPacket.target.myName);
}
}
*/
define(
function(require) {
var Base = require('zrender/shape/Base');
function HalfSmoothPolygon(options) {
Base.call(this, options);
}
HalfSmoothPolygon.prototype = {
type : 'half-smooth-polygon',
/**
* 创建多边形路径
* @param {Context2D} ctx Canvas 2D上下文
* @param {Object} style 样式
*/
buildPath : function(ctx, style) {
var pointList = style.pointList;
if (pointList.length < 2) {
// 少于2个点就不画了~
return;
}
if (style.smooth) {
var controlPoints = this.smoothBezier(
pointList.slice(0, -2), style.smooth
);
ctx.moveTo(pointList[0][0], pointList[0][1]);
var cp1;
var cp2;
var p;
var l = pointList.length;
for (var i = 0; i < l - 3; i++) {
cp1 = controlPoints[i * 2];
cp2 = controlPoints[i * 2 + 1];
p = pointList[i + 1];
ctx.bezierCurveTo(
cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]
);
}
ctx.lineTo(pointList[l - 2][0], pointList[l - 2][1]);
ctx.lineTo(pointList[l - 1][0], pointList[l - 1][1]);
ctx.lineTo(pointList[0][0], pointList[0][1]);
}
else {
require('zrender/shape/Polygon').prototype.buildPath(
ctx, style
);
}
return;
}
};
require('zrender/tool/util').inherits(HalfSmoothPolygon, Base);
return HalfSmoothPolygon;
}
);
\ No newline at end of file
/**
* zrender
*
* @author Kener (@Kener-林峰, linzhifeng@baidu.com)
*
* shape类:handlePolygon,dataRange手柄
*/
define(
function(require) {
var Base = require('zrender/shape/Base');
var matrix = require('zrender/tool/matrix');
function HandlePolygon(options) {
Base.call(this, options);
}
HandlePolygon.prototype = {
type : 'handle-polygon',
/**
* 创建多边形路径
* @param {Context2D} ctx Canvas 2D上下文
* @param {Object} style 样式
*/
buildPath : function(ctx, style) {
require('zrender/shape').get('polygon').buildPath(
ctx, style
);
return;
},
isCover : function(x, y) {
//对鼠标的坐标也做相同的变换
if(this.needTransform && this._transform){
var inverseMatrix = [];
matrix.invert(inverseMatrix, this._transform);
var originPos = [x, y];
matrix.mulVector(originPos, inverseMatrix, [x, y, 1]);
if (x == originPos[0] && y == originPos[1]) {
// 避免外部修改导致的__needTransform不准确
this.updateNeedTransform();
}
x = originPos[0];
y = originPos[1];
}
var rect = e.style.rect;
if (x >= rect.x
&& x <= (rect.x + rect.width)
&& y >= rect.y
&& y <= (rect.y + rect.height)
) {
// 矩形内
return true;
}
else {
return false;
}
}
};
require('zrender/tool/util').inherits(HandlePolygon, Base);
return HandlePolygon;
}
);
\ No newline at end of file
此差异已折叠。
/**
* zrender
*
* @author Kener (@Kener-林峰, linzhifeng@baidu.com)
*
* shape类:标线
*/
define(
function(require) {
var Base = require('zrender/shape/Base');
var matrix = require('zrender/tool/matrix');
var area = require('zrender/tool/area');
function MarkLine(options) {
Base.call(this, options);
}
MarkLine.prototype = {
type : 'mark-line',
/**
* 画刷
* @param ctx 画布句柄
* @param e 形状实体
* @param isHighlight 是否为高亮状态
* @param updateCallback 需要异步加载资源的shape可以通过这个callback(e)
* 让painter更新视图,base.brush没用,需要的话重载brush
*/
brush : function(ctx, isHighlight) {
var style = this.style;
if (isHighlight) {
// 根据style扩展默认高亮样式
style = this.getHighlightStyle(
style,
this.highlightStyle || {}
);
}
ctx.save();
this.setContext(ctx, style);
// 设置transform
this.updateTransform(ctx);
ctx.beginPath();
this.buildLinePath(ctx, style);
ctx.stroke();
this.brushSymbol(ctx, style, 0);
this.brushSymbol(ctx, style, 1);
if (typeof style.text != 'undefined') {
this.drawText(ctx, style, this.style);
}
ctx.restore();
return;
},
/**
* 创建线条路径
* @param {Context2D} ctx Canvas 2D上下文
* @param {Object} style 样式
*/
buildLinePath : function(ctx, style) {
var pointList = style.pointList || this.getPointList(style);
style.pointList = pointList;
if (typeof style.pointListLength == 'undefined') {
style.pointListLength = pointList.length;
}
var len = Math.round(style.pointListLength);
if (!style.lineType || style.lineType == 'solid') {
//默认为实线
ctx.moveTo(pointList[0][0],pointList[0][1]);
for (var i = 1; i < len; i++) {
ctx.lineTo(pointList[i][0],pointList[i][1]);
}
}
else if (style.lineType == 'dashed'
|| style.lineType == 'dotted'
) {
if (style.smooth !== 'spline') {
// 直线
var dashLength = (style.lineWidth || 1)
* (style.lineType == 'dashed' ? 5 : 1);
ctx.moveTo(pointList[0][0],pointList[0][1]);
for (var i = 1; i < len; i++) {
this.dashedLineTo(
ctx,
pointList[i - 1][0], pointList[i - 1][1],
pointList[i][0], pointList[i][1],
dashLength
);
}
}
else {
// 曲线
for (var i = 0; i < len - 1; i += 2) {
ctx.moveTo(pointList[i][0],pointList[i][1]);
ctx.lineTo(pointList[i + 1][0],pointList[i + 1][1]);
}
}
}
},
/**
* 标线始末标注
*/
brushSymbol : function(ctx, style, idx) {
if (style.symbol[idx] == 'none') {
return;
}
ctx.save();
ctx.beginPath();
ctx.lineWidth = style.symbolBorder;
ctx.strokeStyle = style.symbolBorderColor;
// symbol
style.iconType = style.symbol[idx].replace('empty', '')
.toLowerCase();
if (style.symbol[idx].match('empty')) {
ctx.fillStyle = '#fff'; //'rgba(0, 0, 0, 0)';
}
// symbolRotate
var len = Math.round(style.pointListLength || style.pointList.length);
var x = idx === 0 ? style.pointList[0][0] : style.pointList[len - 1][0];
var y = idx === 0 ? style.pointList[0][1] : style.pointList[len - 1][1];
var rotate = typeof style.symbolRotate[idx] != 'undefined'
? (style.symbolRotate[idx] - 0) : 0;
var transform;
if (rotate !== 0) {
transform = matrix.create();
matrix.identity(transform);
if (x || y ) {
matrix.translate(transform, transform, [-x, -y]);
}
matrix.rotate(
transform, transform,
rotate * Math.PI / 180
);
if (x || y ) {
matrix.translate(transform, transform, [x, y]);
}
ctx.transform.apply(ctx, transform);
}
if (style.iconType == 'arrow' && rotate === 0) {
// 箭头自动旋转,手动画
this.buildArrawPath(ctx, style, idx);
}
else {
// symbolSize
var symbolSize = style.symbolSize[idx];
style.x = x - symbolSize;
style.y = y - symbolSize,
style.width = symbolSize * 2;
style.height = symbolSize * 2;
require('zrender/shape').get('icon').buildPath(ctx, style);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.restore();
},
buildArrawPath : function (ctx, style, idx) {
var len = Math.round(style.pointListLength || style.pointList.length);
var symbolSize = style.symbolSize[idx] * 2;
var xStart = style.pointList[0][0];
var xEnd = style.pointList[len - 1][0];
var yStart = style.pointList[0][1];
var yEnd = style.pointList[len - 1][1];
var delta = 0;
if (style.smooth === 'spline') {
delta = 0.2; // 偏移0.2弧度
}
// 原谅我吧,这三角函数实在没想明白,只能这么笨了
var rotate = Math.atan(
Math.abs((yEnd - yStart) / (xStart - xEnd)
));
if (idx === 0) {
if (xEnd > xStart) {
if (yEnd > yStart) {
rotate = Math.PI * 2 - rotate + delta;
}
else {
rotate += delta;
}
}
else {
if (yEnd > yStart) {
rotate += Math.PI - delta;
}
else {
rotate = Math.PI - rotate - delta;
}
}
}
else {
if (xStart > xEnd) {
if (yStart > yEnd) {
rotate = Math.PI * 2 - rotate + delta;
}
else {
rotate += delta;
}
}
else {
if (yStart > yEnd) {
rotate += Math.PI - delta;
}
else {
rotate = Math.PI - rotate - delta;
}
}
}
var halfRotate = Math.PI / 8; // 夹角
var x = idx === 0 ? xStart : xEnd;
var y = idx === 0 ? yStart : yEnd;
var point= [
[
x + symbolSize * Math.cos(rotate - halfRotate),
y - symbolSize * Math.sin(rotate - halfRotate),
],
[
x + symbolSize * 0.6 * Math.cos(rotate),
y - symbolSize * 0.6 * Math.sin(rotate),
],
[
x + symbolSize * Math.cos(rotate + halfRotate),
y - symbolSize * Math.sin(rotate + halfRotate),
]
];
ctx.moveTo(x, y);
for (var i = 0, l = point.length; i <l; i++) {
ctx.lineTo(point[i][0], point[i][1]);
}
ctx.lineTo(x, y);
},
getPointList : function(style) {
var pointList = [
[style.xStart, style.yStart],
[style.xEnd, style.yEnd]
];
if (style.smooth === 'spline') {
var lastPointX = pointList[1][0];
var lastPointY = pointList[1][1];
pointList[3] = [lastPointX, lastPointY];
pointList[1] = this.getOffetPoint(pointList[0], pointList[3]);
pointList[2] = this.getOffetPoint(pointList[3], pointList[0]);
pointList = this.smoothSpline(pointList, false);
// 修正最后一点在插值产生的偏移
pointList[pointList.length - 1] = [lastPointX, lastPointY];
}
return pointList;
},
/**
* {Array} start point
* {Array} end point
*/
getOffetPoint : function(sp, ep) {
var distance = Math.sqrt(Math.round(
(sp[0] - ep[0]) * (sp[0] - ep[0]) + (sp[1] - ep[1]) * (sp[1] - ep[1])
)) / 3;
//console.log(delta);
var mp = [sp[0], sp[1]];
var angle;
var deltaAngle = 0.2; // 偏移0.2弧度
if (sp[0] != ep[0] && sp[1] != ep[1]) {
// 斜率存在
var k = (ep[1] - sp[1]) / (ep[0] - sp[0]);
angle = Math.atan(k);
}
else if (sp[0] == ep[0]){
// 垂直线
angle = (sp[1] <= ep[1] ? 1 : -1) * Math.PI / 2;
}
else {
// 水平线
angle = 0;
}
var dX;
var dY;
if (sp[0] <= ep[0]) {
angle -= deltaAngle;
dX = Math.round(Math.cos(angle) * distance);
dY = Math.round(Math.sin(angle) * distance);
mp[0] += dX;
mp[1] += dY;
}
else {
angle += deltaAngle;
dX = Math.round(Math.cos(angle) * distance);
dY = Math.round(Math.sin(angle) * distance);
mp[0] -= dX;
mp[1] -= dY;
}
return mp;
},
/**
* 返回矩形区域,用于局部刷新和文字定位
* @param {Object} style
*/
getRect : function(style) {
if (style.__rect) {
return style.__rect;
}
var lineWidth = style.lineWidth || 1;
style.__rect = {
x : Math.min(style.xStart, style.xEnd) - lineWidth,
y : Math.min(style.yStart, style.yEnd) - lineWidth,
width : Math.abs(style.xStart - style.xEnd)
+ lineWidth,
height : Math.abs(style.yStart - style.yEnd)
+ lineWidth
};
return style.__rect;
},
isCover : function(x, y) {
return this.style.smooth !== 'spline'
? area.isInside(require('zrender/shape/line'), this.style, x, y)
: area.isInside(require('zrender/shape/brokenLine'), this.style, x, y);
}
};
require('zrender/tool/util').inherits(MarkLine, Base);
return MarkLine;
}
);
\ No newline at end of file
/**
* zrender
*
* @author Kener (@Kener-林峰, linzhifeng@baidu.com)
*
* shape类:大规模散点图图形
* 可配图形属性:
{
// 基础属性
shape : 'symbol', // 必须,shape类标识,需要显式指定
id : {string}, // 必须,图形唯一标识,可通过'zrender/tool/guid'方法生成
zlevel : {number}, // 默认为0,z层level,决定绘画在哪层canvas中
invisible : {boolean}, // 默认为false,是否可见
// 样式属性,默认状态样式样式属性
style : {
pointList : {Array}, // 必须,二维数组,二维内容如下
x : {number}, // 必须,横坐标
y : {number}, // 必须,纵坐标数组
size : {number}, // 必须,半宽
type : {string=}, // 默认为'circle',图形类型
},
// 样式属性,高亮样式属性,当不存在highlightStyle时使用基于默认样式扩展显示
highlightStyle : {
// 同style
}
// 交互属性,详见shape.Base
// 事件属性,详见shape.Base
}
*/
define(
function(require) {
var Base = require('zrender/shape/Base');
function Symbol(options) {
Base.call(this, options);
}
Symbol.prototype = {
type : 'symbol',
/**
* 创建矩形路径
* @param {Context2D} ctx Canvas 2D上下文
* @param {Object} style 样式
*/
buildPath : function(ctx, style) {
var pointList = style.pointList;
var rect = this.getRect(style);
var ratio = window.devicePixelRatio || 1;
// console.log(rect)
// var ti = new Date();
// bbox取整
rect = {
x : Math.floor(rect.x),
y : Math.floor(rect.y),
width : Math.floor(rect.width),
height : Math.floor(rect.height)
};
var pixels = ctx.getImageData(
rect.x * ratio, rect.y * ratio,
rect.width * ratio, rect.height * ratio
);
var data = pixels.data;
var idx;
var zrColor = require('zrender/tool/color');
var color = zrColor.toArray(style.color);
var r = color[0];
var g = color[1];
var b = color[2];
var width = rect.width;
for (var i = 1, l = pointList.length; i < l; i++) {
idx = ((Math.floor(pointList[i][0]) - rect.x) * ratio
+ (Math.floor(pointList[i][1])- rect.y) * width * ratio * ratio
) * 4;
data[idx] = r;
data[idx + 1] = g;
data[idx + 2] = b;
data[idx + 3] = 255;
}
ctx.putImageData(pixels, rect.x * ratio, rect.y * ratio);
// console.log(new Date() - ti);
return;
},
/**
* 返回矩形区域,用于局部刷新和文字定位
* @param {Object} style
*/
getRect : function(style) {
return require('zrender/shape/polygon').getRect(style);
},
isCover : function() {
return false;
}
};
require('zrender/tool/util').inherits(Symbol, Base);
return Symbol;
}
);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册