/** * zrender * Copyright 2013 Baidu Inc. All rights reserved. * * @author Kener (@Kener-林峰, linzhifeng@baidu.com) * * shape类:大规模散点图图形 * 可配图形属性: { // 基础属性 shape : 'symbol', // 必须,shape类标识,需要显式指定 id : {string}, // 必须,图形唯一标识,可通过zrender实例方法newShapeId生成 zlevel : {number}, // 默认为0,z层level,决定绘画在哪层canvas中 invisible : {boolean}, // 默认为false,是否可见 // 样式属性,默认状态样式样式属性 style : { pointList : {Array}, // 必须,二维数组,二维内容如下 x : {number}, // 必须,横坐标 y : {number}, // 必须,纵坐标数组 size : {number}, // 必须,半宽 type : {string=}, // 默认为'circle',图形类型 color : {color}, // 默认为'#000',填充颜色,支持rgba strokeColor : {color}, // 默认为'#000',描边颜色(轮廓),支持rgba lineWidth : {number}, // 默认为1,线条宽度,描边下有效 opacity : {number}, // 默认为1,透明度设置,如果color为rgba,则最终透明度效果叠加 shadowBlur : {number}, // 默认为0,阴影模糊度,大于0有效 shadowColor : {color}, // 默认为'#000',阴影色彩,支持rgba shadowOffsetX : {number}, // 默认为0,阴影横向偏移,正值往右,负值往左 shadowOffsetY : {number}, // 默认为0,阴影纵向偏移,正值往下,负值往上 text : {string}, // 默认为null,附加文本 textFont : {string}, // 默认为null,附加文本样式,eg:'bold 18px verdana' textPosition : {string}, // 默认为top,附加文本位置。 // inside | left | right | top | bottom textAlign : {string}, // 默认根据textPosition自动设置,附加文本水平对齐。 // start | end | left | right | center textBaseline : {string}, // 默认根据textPosition自动设置,附加文本垂直对齐。 // top | bottom | middle | // alphabetic | hanging | ideographic textColor : {color}, // 默认根据textPosition自动设置,默认策略如下,附加文本颜色 // 'inside' ? '#fff' : color }, // 样式属性,高亮样式属性,当不存在highlightStyle时使用基于默认样式扩展显示 highlightStyle : { // 同style } // 交互属性,详见shape.Base // 事件属性,详见shape.Base } 例子: { shape : 'symbol', 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) { function Symbol() { this.type = 'symbol'; } Symbol.prototype = { /* * pointlist=[ * 0 x, * 1 y, * 2 图形大小 * 3 图形类型 * 4 数据index * 5 名称 * ] */ _buildSinglePoint : function(ctx, singlePoint) { switch (singlePoint[3]) { case 'circle' : case 'emptyCircle' : ctx.arc( singlePoint[0], singlePoint[1], singlePoint[2], 0, Math.PI * 2, true ); break; case 'rectangle' : case 'emptyRectangle' : ctx.rect( singlePoint[0] - singlePoint[2], singlePoint[1] - singlePoint[2], singlePoint[2] * 2, singlePoint[2] * 2 ); break; case 'triangle' : case 'emptyTriangle' : itemShape = { shape : 'polygon', style : { pointList : [ [x, y - symbolSize], [x + symbolSize, y + symbolSize], [x - symbolSize, y + symbolSize] ], brushType : symbolType == 'triangle' ? 'fill' : 'stroke' } }; break; case 'diamond' : case 'emptyDiamond' : itemShape = { shape : 'polygon', style : { pointList : [ [x, y - symbolSize], [x + symbolSize, y], [x, y + symbolSize], [x - symbolSize, y] ], brushType : symbolType == 'diamond' ? 'fill' : 'stroke' } }; break; default: itemShape = { shape : 'circle', style : { x : x, y : y, r : symbolSize, brushType : 'fill' } }; break; } }, /** * 创建矩形路径 * @param {Context2D} ctx Canvas 2D上下文 * @param {Object} style 样式 */ buildPath : function(ctx, style) { var pointList = style.pointList; var rect = this.getRect(style); // console.log(rect) // var ti = new Date(); var pixels = ctx.getImageData( rect.x, rect.y, rect.width, rect.height ); 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 = (pointList[i][0] - rect.x + (pointList[i][1]- rect.y) * width ) * 4; data[idx] = r; data[idx + 1] = g; data[idx + 2] = b; data[idx + 3] = 255; } ctx.putImageData(pixels, rect.x, rect.y); // console.log(new Date() - ti); return; }, /** * 返回矩形区域,用于局部刷新和文字定位 * @param {Object} style */ getRect : function(style) { var shape = require('zrender/shape'); return shape.get('polygon').getRect(style); }, isCover : function() { return false; } }; var base = require('zrender/shape/base'); base.derive(Symbol); return Symbol; } );