wordCloud.js 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. define('echarts/chart/wordCloud', [
  2. 'require',
  3. './base',
  4. 'zrender/shape/Text',
  5. '../layout/WordCloud',
  6. '../component/grid',
  7. '../component/dataRange',
  8. '../config',
  9. '../util/ecData',
  10. 'zrender/tool/util',
  11. 'zrender/tool/color',
  12. '../chart'
  13. ], function (require) {
  14. var ChartBase = require('./base');
  15. var TextShape = require('zrender/shape/Text');
  16. var CloudLayout = require('../layout/WordCloud');
  17. require('../component/grid');
  18. require('../component/dataRange');
  19. var ecConfig = require('../config');
  20. var ecData = require('../util/ecData');
  21. var zrUtil = require('zrender/tool/util');
  22. var zrColor = require('zrender/tool/color');
  23. ecConfig.wordCloud = {
  24. zlevel: 0,
  25. z: 2,
  26. clickable: true,
  27. center: [
  28. '50%',
  29. '50%'
  30. ],
  31. size: [
  32. '40%',
  33. '40%'
  34. ],
  35. textRotation: [
  36. 0,
  37. 90
  38. ],
  39. textPadding: 0,
  40. autoSize: {
  41. enable: true,
  42. minSize: 12
  43. },
  44. itemStyle: {
  45. normal: {
  46. textStyle: {
  47. fontSize: function (data) {
  48. return data.value;
  49. }
  50. }
  51. }
  52. }
  53. };
  54. function Cloud(ecTheme, messageCenter, zr, option, myChart) {
  55. ChartBase.call(this, ecTheme, messageCenter, zr, option, myChart);
  56. this.refresh(option);
  57. }
  58. Cloud.prototype = {
  59. type: ecConfig.CHART_TYPE_WORDCLOUD,
  60. refresh: function (newOption) {
  61. if (newOption) {
  62. this.option = newOption;
  63. this.series = newOption.series;
  64. }
  65. this._init();
  66. },
  67. _init: function () {
  68. var series = this.series;
  69. this.backupShapeList();
  70. var legend = this.component.legend;
  71. for (var i = 0; i < series.length; i++) {
  72. if (series[i].type === ecConfig.CHART_TYPE_WORDCLOUD) {
  73. series[i] = this.reformOption(series[i]);
  74. var serieName = series[i].name || '';
  75. this.selectedMap[serieName] = legend ? legend.isSelected(serieName) : true;
  76. if (!this.selectedMap[serieName]) {
  77. continue;
  78. }
  79. this.buildMark(i);
  80. this._initSerie(series[i]);
  81. }
  82. }
  83. },
  84. _initSerie: function (serie) {
  85. var textStyle = serie.itemStyle.normal.textStyle;
  86. var size = [
  87. this.parsePercent(serie.size[0], this.zr.getWidth()) || 200,
  88. this.parsePercent(serie.size[1], this.zr.getHeight()) || 200
  89. ];
  90. var center = this.parseCenter(this.zr, serie.center);
  91. var layoutConfig = {
  92. size: size,
  93. wordletype: { autoSizeCal: serie.autoSize },
  94. center: center,
  95. rotate: serie.textRotation,
  96. padding: serie.textPadding,
  97. font: textStyle.fontFamily,
  98. fontSize: textStyle.fontSize,
  99. fontWeight: textStyle.fontWeight,
  100. fontStyle: textStyle.fontStyle,
  101. text: function (d) {
  102. return d.name;
  103. },
  104. data: serie.data
  105. };
  106. var clouds = new CloudLayout(layoutConfig);
  107. var self = this;
  108. clouds.end(function (d) {
  109. self._buildShapes(d);
  110. });
  111. clouds.start();
  112. },
  113. _buildShapes: function (data) {
  114. var len = data.length;
  115. for (var i = 0; i < len; i++) {
  116. this._buildTextShape(data[i], 0, i);
  117. }
  118. this.addShapeList();
  119. },
  120. _buildTextShape: function (oneText, seriesIndex, dataIndex) {
  121. var series = this.series;
  122. var serie = series[seriesIndex];
  123. var serieName = serie.name || '';
  124. var data = serie.data[dataIndex];
  125. var queryTarget = [
  126. data,
  127. serie
  128. ];
  129. var legend = this.component.legend;
  130. var defaultColor = legend ? legend.getColor(serieName) : this.zr.getColor(seriesIndex);
  131. var normal = this.deepMerge(queryTarget, 'itemStyle.normal') || {};
  132. var emphasis = this.deepMerge(queryTarget, 'itemStyle.emphasis') || {};
  133. var normalColor = this.getItemStyleColor(normal.color, seriesIndex, dataIndex, data) || defaultColor;
  134. var emphasisColor = this.getItemStyleColor(emphasis.color, seriesIndex, dataIndex, data) || (typeof normalColor === 'string' ? zrColor.lift(normalColor, -0.2) : normalColor);
  135. var textShape = new TextShape({
  136. zlevel: serie.zlevel,
  137. z: serie.z,
  138. hoverable: true,
  139. clickable: this.deepQuery(queryTarget, 'clickable'),
  140. style: {
  141. x: 0,
  142. y: 0,
  143. text: oneText.text,
  144. color: normalColor,
  145. textFont: [
  146. oneText.style,
  147. oneText.weight,
  148. oneText.size + 'px',
  149. oneText.font
  150. ].join(' '),
  151. textBaseline: 'alphabetic',
  152. textAlign: 'center'
  153. },
  154. highlightStyle: {
  155. brushType: emphasis.borderWidth ? 'both' : 'fill',
  156. color: emphasisColor,
  157. lineWidth: emphasis.borderWidth || 0,
  158. strokeColor: emphasis.borderColor
  159. },
  160. position: [
  161. oneText.x,
  162. oneText.y
  163. ],
  164. rotation: [
  165. -oneText.rotate / 180 * Math.PI,
  166. 0,
  167. 0
  168. ]
  169. });
  170. ecData.pack(textShape, serie, seriesIndex, data, dataIndex, data.name);
  171. this.shapeList.push(textShape);
  172. }
  173. };
  174. zrUtil.inherits(Cloud, ChartBase);
  175. require('../chart').define('wordCloud', Cloud);
  176. return Cloud;
  177. });define('echarts/layout/WordCloud', [
  178. 'require',
  179. '../layout/WordCloudRectZero',
  180. 'zrender/tool/util'
  181. ], function (require) {
  182. var ZeroArray = require('../layout/WordCloudRectZero');
  183. var zrUtil = require('zrender/tool/util');
  184. function CloudLayout(option) {
  185. this._init(option);
  186. }
  187. CloudLayout.prototype = {
  188. start: function () {
  189. var board = null;
  190. var maxWit = 0;
  191. var maxHit = 0;
  192. var maxArea = 0;
  193. var i = -1;
  194. var tags = [];
  195. var maxBounds = null;
  196. var data = this.wordsdata;
  197. var dfop = this.defaultOption;
  198. var wordletype = dfop.wordletype;
  199. var size = dfop.size;
  200. var that = this;
  201. var zeroArrayObj = new ZeroArray({
  202. type: wordletype.type,
  203. width: size[0],
  204. height: size[1]
  205. });
  206. zeroArrayObj.calculate(function (options) {
  207. board = options.initarr;
  208. maxWit = options.maxWit;
  209. maxHit = options.maxHit;
  210. maxArea = options.area;
  211. maxBounds = options.imgboard;
  212. startStep();
  213. }, this);
  214. return this;
  215. function startStep() {
  216. that.totalArea = maxArea;
  217. if (wordletype.autoSizeCal.enable) {
  218. that._autoCalTextSize(data, maxArea, maxWit, maxHit, wordletype.autoSizeCal.minSize);
  219. }
  220. if (dfop.timer) {
  221. clearInterval(dfop.timer);
  222. }
  223. dfop.timer = setInterval(step, 0);
  224. step();
  225. }
  226. function step() {
  227. var start = +new Date();
  228. var n = data.length;
  229. var d;
  230. while (+new Date() - start < dfop.timeInterval && ++i < n && dfop.timer) {
  231. d = data[i];
  232. d.x = size[0] >> 1;
  233. d.y = size[1] >> 1;
  234. that._cloudSprite(d, data, i);
  235. if (d.hasText && that._place(board, d, maxBounds)) {
  236. tags.push(d);
  237. d.x -= size[0] >> 1;
  238. d.y -= size[1] >> 1;
  239. }
  240. }
  241. if (i >= n) {
  242. that.stop();
  243. that._fixTagPosition(tags);
  244. dfop.endcallback(tags);
  245. }
  246. }
  247. },
  248. _fixTagPosition: function (tags) {
  249. var center = this.defaultOption.center;
  250. for (var i = 0, len = tags.length; i < len; i++) {
  251. tags[i].x += center[0];
  252. tags[i].y += center[1];
  253. }
  254. },
  255. stop: function () {
  256. if (this.defaultOption.timer) {
  257. clearInterval(this.defaultOption.timer);
  258. this.defaultOption.timer = null;
  259. }
  260. return this;
  261. },
  262. end: function (v) {
  263. if (v) {
  264. this.defaultOption.endcallback = v;
  265. }
  266. return this;
  267. },
  268. _init: function (option) {
  269. this.defaultOption = {};
  270. this._initProperty(option);
  271. this._initMethod(option);
  272. this._initCanvas();
  273. this._initData(option.data);
  274. },
  275. _initData: function (datas) {
  276. var that = this;
  277. var thatop = that.defaultOption;
  278. this.wordsdata = datas.map(function (d, i) {
  279. d.text = thatop.text.call(that, d, i);
  280. d.font = thatop.font.call(that, d, i);
  281. d.style = thatop.fontStyle.call(that, d, i);
  282. d.weight = thatop.fontWeight.call(that, d, i);
  283. d.rotate = thatop.rotate.call(that, d, i);
  284. d.size = ~~thatop.fontSize.call(that, d, i);
  285. d.padding = thatop.padding.call(that, d, i);
  286. return d;
  287. }).sort(function (a, b) {
  288. return b.value - a.value;
  289. });
  290. },
  291. _initMethod: function (option) {
  292. var dfop = this.defaultOption;
  293. dfop.text = option.text ? functor(option.text) : cloudText;
  294. dfop.font = option.font ? functor(option.font) : cloudFont;
  295. dfop.fontSize = option.fontSize ? functor(option.fontSize) : cloudFontSize;
  296. dfop.fontStyle = option.fontStyle ? functor(option.fontStyle) : cloudFontNormal;
  297. dfop.fontWeight = option.fontWeight ? functor(option.fontWeight) : cloudFontNormal;
  298. dfop.rotate = option.rotate ? newCloudRotate(option.rotate) : cloudRotate;
  299. dfop.padding = option.padding ? functor(option.padding) : cloudPadding;
  300. dfop.center = option.center;
  301. dfop.spiral = archimedeanSpiral;
  302. dfop.endcallback = function () {
  303. };
  304. dfop.rectangularSpiral = rectangularSpiral;
  305. dfop.archimedeanSpiral = archimedeanSpiral;
  306. function cloudText(d) {
  307. return d.name;
  308. }
  309. function cloudFont() {
  310. return 'sans-serif';
  311. }
  312. function cloudFontNormal() {
  313. return 'normal';
  314. }
  315. function cloudFontSize(d) {
  316. return d.value;
  317. }
  318. function cloudRotate() {
  319. return 0;
  320. }
  321. function newCloudRotate(rotate) {
  322. return function () {
  323. return rotate[Math.round(Math.random() * (rotate.length - 1))];
  324. };
  325. }
  326. function cloudPadding() {
  327. return 0;
  328. }
  329. function archimedeanSpiral(size) {
  330. var e = size[0] / size[1];
  331. return function (t) {
  332. return [
  333. e * (t *= 0.1) * Math.cos(t),
  334. t * Math.sin(t)
  335. ];
  336. };
  337. }
  338. function rectangularSpiral(size) {
  339. var dy = 4;
  340. var dx = dy * size[0] / size[1];
  341. var x = 0;
  342. var y = 0;
  343. return function (t) {
  344. var sign = t < 0 ? -1 : 1;
  345. switch (Math.sqrt(1 + 4 * sign * t) - sign & 3) {
  346. case 0:
  347. x += dx;
  348. break;
  349. case 1:
  350. y += dy;
  351. break;
  352. case 2:
  353. x -= dx;
  354. break;
  355. default:
  356. y -= dy;
  357. break;
  358. }
  359. return [
  360. x,
  361. y
  362. ];
  363. };
  364. }
  365. function functor(v) {
  366. return typeof v === 'function' ? v : function () {
  367. return v;
  368. };
  369. }
  370. },
  371. _initProperty: function (option) {
  372. var dfop = this.defaultOption;
  373. dfop.size = option.size || [
  374. 256,
  375. 256
  376. ];
  377. dfop.wordletype = option.wordletype;
  378. dfop.words = option.words || [];
  379. dfop.timeInterval = Infinity;
  380. dfop.timer = null;
  381. dfop.spirals = {
  382. archimedean: dfop.archimedeanSpiral,
  383. rectangular: dfop.rectangularSpiral
  384. };
  385. zrUtil.merge(dfop, {
  386. size: [
  387. 256,
  388. 256
  389. ],
  390. wordletype: {
  391. type: 'RECT',
  392. areaPresent: 0.058,
  393. autoSizeCal: {
  394. enable: true,
  395. minSize: 12
  396. }
  397. }
  398. });
  399. },
  400. _initCanvas: function () {
  401. var cloudRadians = Math.PI / 180;
  402. var cw = 1 << 11 >> 5;
  403. var ch = 1 << 11;
  404. var canvas;
  405. var ratio = 1;
  406. if (typeof document !== 'undefined') {
  407. canvas = document.createElement('canvas');
  408. canvas.width = 1;
  409. canvas.height = 1;
  410. ratio = Math.sqrt(canvas.getContext('2d').getImageData(0, 0, 1, 1).data.length >> 2);
  411. canvas.width = (cw << 5) / ratio;
  412. canvas.height = ch / ratio;
  413. } else {
  414. canvas = new Canvas(cw << 5, ch);
  415. }
  416. var c = canvas.getContext('2d');
  417. c.fillStyle = c.strokeStyle = 'red';
  418. c.textAlign = 'center';
  419. this.defaultOption.c = c;
  420. this.defaultOption.cw = cw;
  421. this.defaultOption.ch = ch;
  422. this.defaultOption.ratio = ratio;
  423. this.defaultOption.cloudRadians = cloudRadians;
  424. },
  425. _cloudSprite: function (d, data, di) {
  426. if (d.sprite) {
  427. return;
  428. }
  429. var cw = this.defaultOption.cw;
  430. var ch = this.defaultOption.ch;
  431. var c = this.defaultOption.c;
  432. var ratio = this.defaultOption.ratio;
  433. var cloudRadians = this.defaultOption.cloudRadians;
  434. c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
  435. var x = 0;
  436. var y = 0;
  437. var maxh = 0;
  438. var n = data.length;
  439. --di;
  440. while (++di < n) {
  441. d = data[di];
  442. c.save();
  443. c.font = d.style + ' ' + d.weight + ' ' + ~~((d.size + 1) / ratio) + 'px ' + d.font;
  444. var w = c.measureText(d.text + 'm').width * ratio;
  445. var h = d.size << 1;
  446. if (d.rotate) {
  447. var sr = Math.sin(d.rotate * cloudRadians);
  448. var cr = Math.cos(d.rotate * cloudRadians);
  449. var wcr = w * cr;
  450. var wsr = w * sr;
  451. var hcr = h * cr;
  452. var hsr = h * sr;
  453. w = Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 31 >> 5 << 5;
  454. h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
  455. } else {
  456. w = w + 31 >> 5 << 5;
  457. }
  458. if (h > maxh) {
  459. maxh = h;
  460. }
  461. if (x + w >= cw << 5) {
  462. x = 0;
  463. y += maxh;
  464. maxh = 0;
  465. }
  466. if (y + h >= ch) {
  467. break;
  468. }
  469. c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio);
  470. if (d.rotate) {
  471. c.rotate(d.rotate * cloudRadians);
  472. }
  473. c.fillText(d.text, 0, 0);
  474. if (d.padding) {
  475. c.lineWidth = 2 * d.padding;
  476. c.strokeText(d.text, 0, 0);
  477. }
  478. c.restore();
  479. d.width = w;
  480. d.height = h;
  481. d.xoff = x;
  482. d.yoff = y;
  483. d.x1 = w >> 1;
  484. d.y1 = h >> 1;
  485. d.x0 = -d.x1;
  486. d.y0 = -d.y1;
  487. d.hasText = true;
  488. x += w;
  489. }
  490. var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data;
  491. var sprite = [];
  492. while (--di >= 0) {
  493. d = data[di];
  494. if (!d.hasText) {
  495. continue;
  496. }
  497. var w = d.width;
  498. var w32 = w >> 5;
  499. var h = d.y1 - d.y0;
  500. for (var i = 0; i < h * w32; i++) {
  501. sprite[i] = 0;
  502. }
  503. x = d.xoff;
  504. if (x == null) {
  505. return;
  506. }
  507. y = d.yoff;
  508. var seen = 0;
  509. var seenRow = -1;
  510. for (var j = 0; j < h; j++) {
  511. for (var i = 0; i < w; i++) {
  512. var k = w32 * j + (i >> 5);
  513. var m = pixels[(y + j) * (cw << 5) + (x + i) << 2] ? 1 << 31 - i % 32 : 0;
  514. sprite[k] |= m;
  515. seen |= m;
  516. }
  517. if (seen) {
  518. seenRow = j;
  519. } else {
  520. d.y0++;
  521. h--;
  522. j--;
  523. y++;
  524. }
  525. }
  526. d.y1 = d.y0 + seenRow;
  527. d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32);
  528. }
  529. },
  530. _place: function (board, tag, maxBounds) {
  531. var size = this.defaultOption.size;
  532. var perimeter = [
  533. {
  534. x: 0,
  535. y: 0
  536. },
  537. {
  538. x: size[0],
  539. y: size[1]
  540. }
  541. ];
  542. var startX = tag.x;
  543. var startY = tag.y;
  544. var maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]);
  545. var s = this.defaultOption.spiral(size);
  546. var dt = Math.random() < 0.5 ? 1 : -1;
  547. var t = -dt;
  548. var dxdy;
  549. var dx;
  550. var dy;
  551. while (dxdy = s(t += dt)) {
  552. dx = ~~dxdy[0];
  553. dy = ~~dxdy[1];
  554. if (Math.min(dx, dy) > maxDelta) {
  555. break;
  556. }
  557. tag.x = startX + dx;
  558. tag.y = startY + dy;
  559. if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 || tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) {
  560. continue;
  561. }
  562. if (!cloudCollide(tag, board, size[0])) {
  563. if (collideRects(tag, maxBounds)) {
  564. var sprite = tag.sprite;
  565. var w = tag.width >> 5;
  566. var sw = size[0] >> 5;
  567. var lx = tag.x - (w << 4);
  568. var sx = lx & 127;
  569. var msx = 32 - sx;
  570. var h = tag.y1 - tag.y0;
  571. var x = (tag.y + tag.y0) * sw + (lx >> 5);
  572. var last;
  573. for (var j = 0; j < h; j++) {
  574. last = 0;
  575. for (var i = 0; i <= w; i++) {
  576. board[x + i] |= last << msx | (i < w ? (last = sprite[j * w + i]) >>> sx : 0);
  577. }
  578. x += sw;
  579. }
  580. delete tag.sprite;
  581. return true;
  582. }
  583. }
  584. }
  585. return false;
  586. function cloudCollide(tag, board, sw) {
  587. sw >>= 5;
  588. var sprite = tag.sprite;
  589. var w = tag.width >> 5;
  590. var lx = tag.x - (w << 4);
  591. var sx = lx & 127;
  592. var msx = 32 - sx;
  593. var h = tag.y1 - tag.y0;
  594. var x = (tag.y + tag.y0) * sw + (lx >> 5);
  595. var last;
  596. for (var j = 0; j < h; j++) {
  597. last = 0;
  598. for (var i = 0; i <= w; i++) {
  599. if ((last << msx | (i < w ? (last = sprite[j * w + i]) >>> sx : 0)) & board[x + i]) {
  600. return true;
  601. }
  602. }
  603. x += sw;
  604. }
  605. return false;
  606. }
  607. function collideRects(a, maxBounds) {
  608. return maxBounds.row[a.y] && maxBounds.cloumn[a.x] && a.x >= maxBounds.row[a.y].start && a.x <= maxBounds.row[a.y].end && a.y >= maxBounds.cloumn[a.x].start && a.y <= maxBounds.cloumn[a.x].end;
  609. }
  610. },
  611. _autoCalTextSize: function (data, shapeArea, maxwidth, maxheight, minSize) {
  612. var sizesum = sum(data, function (k) {
  613. return k.size;
  614. });
  615. var i = data.length;
  616. var maxareapre = 0.25;
  617. var minTextSize = minSize;
  618. var cw = this.defaultOption.cw;
  619. var ch = this.defaultOption.ch;
  620. var c = this.defaultOption.c;
  621. var ratio = this.defaultOption.ratio;
  622. var cloudRadians = this.defaultOption.cloudRadians;
  623. var d;
  624. var dpre;
  625. while (i--) {
  626. d = data[i];
  627. dpre = d.size / sizesum;
  628. if (maxareapre) {
  629. d.areapre = dpre < maxareapre ? dpre : maxareapre;
  630. } else {
  631. d.areapre = dpre;
  632. }
  633. d.area = shapeArea * d.areapre;
  634. d.totalarea = shapeArea;
  635. measureTextWitHitByarea(d);
  636. }
  637. function measureTextWitHitByarea(d) {
  638. c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio);
  639. c.save();
  640. c.font = d.style + ' ' + d.weight + ' ' + ~~((d.size + 1) / ratio) + 'px ' + d.font;
  641. var w = c.measureText(d.text + 'm').width * ratio, h = d.size << 1;
  642. w = w + 31 >> 5 << 5;
  643. c.restore();
  644. d.aw = w;
  645. d.ah = h;
  646. var k, rw, rh;
  647. if (d.rotate) {
  648. var sr = Math.sin(d.rotate * cloudRadians);
  649. var cr = Math.cos(d.rotate * cloudRadians);
  650. var wcr = w * cr;
  651. var wsr = w * sr;
  652. var hcr = h * cr;
  653. var hsr = h * sr;
  654. rw = Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 31 >> 5 << 5;
  655. rh = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr));
  656. }
  657. if (d.size <= minTextSize || d.rotate && w * h <= d.area && rw <= maxwidth && rh <= maxheight || w * h <= d.area && w <= maxwidth && h <= maxheight) {
  658. d.area = w * h;
  659. return;
  660. }
  661. if (d.rotate && rw > maxwidth && rh > maxheight) {
  662. k = Math.min(maxwidth / rw, maxheight / rh);
  663. } else if (w > maxwidth || h > maxheight) {
  664. k = Math.min(maxwidth / w, maxheight / h);
  665. } else {
  666. k = Math.sqrt(d.area / (d.aw * d.ah));
  667. }
  668. d.size = ~~(k * d.size);
  669. if (d.size < minSize) {
  670. d.size = minSize;
  671. return;
  672. }
  673. return measureTextWitHitByarea(d);
  674. }
  675. function sum(dts, callback) {
  676. var j = dts.length;
  677. var ressum = 0;
  678. while (j--) {
  679. ressum += callback(dts[j]);
  680. }
  681. return ressum;
  682. }
  683. }
  684. };
  685. return CloudLayout;
  686. });define('echarts/component/dataRange', [
  687. 'require',
  688. './base',
  689. 'zrender/shape/Text',
  690. 'zrender/shape/Rectangle',
  691. '../util/shape/HandlePolygon',
  692. '../config',
  693. 'zrender/tool/util',
  694. 'zrender/tool/event',
  695. 'zrender/tool/area',
  696. 'zrender/tool/color',
  697. '../component'
  698. ], function (require) {
  699. var Base = require('./base');
  700. var TextShape = require('zrender/shape/Text');
  701. var RectangleShape = require('zrender/shape/Rectangle');
  702. var HandlePolygonShape = require('../util/shape/HandlePolygon');
  703. var ecConfig = require('../config');
  704. ecConfig.dataRange = {
  705. zlevel: 0,
  706. z: 4,
  707. show: true,
  708. orient: 'vertical',
  709. x: 'left',
  710. y: 'bottom',
  711. backgroundColor: 'rgba(0,0,0,0)',
  712. borderColor: '#ccc',
  713. borderWidth: 0,
  714. padding: 5,
  715. itemGap: 10,
  716. itemWidth: 20,
  717. itemHeight: 14,
  718. precision: 0,
  719. splitNumber: 5,
  720. splitList: null,
  721. calculable: false,
  722. selectedMode: true,
  723. hoverLink: true,
  724. realtime: true,
  725. color: [
  726. '#006edd',
  727. '#e0ffff'
  728. ],
  729. textStyle: { color: '#333' }
  730. };
  731. var zrUtil = require('zrender/tool/util');
  732. var zrEvent = require('zrender/tool/event');
  733. var zrArea = require('zrender/tool/area');
  734. var zrColor = require('zrender/tool/color');
  735. function DataRange(ecTheme, messageCenter, zr, option, myChart) {
  736. Base.call(this, ecTheme, messageCenter, zr, option, myChart);
  737. var self = this;
  738. self._ondrift = function (dx, dy) {
  739. return self.__ondrift(this, dx, dy);
  740. };
  741. self._ondragend = function () {
  742. return self.__ondragend();
  743. };
  744. self._dataRangeSelected = function (param) {
  745. return self.__dataRangeSelected(param);
  746. };
  747. self._dispatchHoverLink = function (param) {
  748. return self.__dispatchHoverLink(param);
  749. };
  750. self._onhoverlink = function (params) {
  751. return self.__onhoverlink(params);
  752. };
  753. this._selectedMap = {};
  754. this._range = {};
  755. this.refresh(option);
  756. messageCenter.bind(ecConfig.EVENT.HOVER, this._onhoverlink);
  757. }
  758. DataRange.prototype = {
  759. type: ecConfig.COMPONENT_TYPE_DATARANGE,
  760. _textGap: 10,
  761. _buildShape: function () {
  762. this._itemGroupLocation = this._getItemGroupLocation();
  763. this._buildBackground();
  764. if (this._isContinuity()) {
  765. this._buildGradient();
  766. } else {
  767. this._buildItem();
  768. }
  769. if (this.dataRangeOption.show) {
  770. for (var i = 0, l = this.shapeList.length; i < l; i++) {
  771. this.zr.addShape(this.shapeList[i]);
  772. }
  773. }
  774. this._syncShapeFromRange();
  775. },
  776. _buildItem: function () {
  777. var data = this._valueTextList;
  778. var dataLength = data.length;
  779. var itemName;
  780. var itemShape;
  781. var textShape;
  782. var font = this.getFont(this.dataRangeOption.textStyle);
  783. var lastX = this._itemGroupLocation.x;
  784. var lastY = this._itemGroupLocation.y;
  785. var itemWidth = this.dataRangeOption.itemWidth;
  786. var itemHeight = this.dataRangeOption.itemHeight;
  787. var itemGap = this.dataRangeOption.itemGap;
  788. var textHeight = zrArea.getTextHeight('国', font);
  789. var color;
  790. if (this.dataRangeOption.orient == 'vertical' && this.dataRangeOption.x == 'right') {
  791. lastX = this._itemGroupLocation.x + this._itemGroupLocation.width - itemWidth;
  792. }
  793. var needValueText = true;
  794. if (this.dataRangeOption.text) {
  795. needValueText = false;
  796. if (this.dataRangeOption.text[0]) {
  797. textShape = this._getTextShape(lastX, lastY, this.dataRangeOption.text[0]);
  798. if (this.dataRangeOption.orient == 'horizontal') {
  799. lastX += zrArea.getTextWidth(this.dataRangeOption.text[0], font) + this._textGap;
  800. } else {
  801. lastY += textHeight + this._textGap;
  802. textShape.style.y += textHeight / 2 + this._textGap;
  803. textShape.style.textBaseline = 'bottom';
  804. }
  805. this.shapeList.push(new TextShape(textShape));
  806. }
  807. }
  808. for (var i = 0; i < dataLength; i++) {
  809. itemName = data[i];
  810. color = this.getColorByIndex(i);
  811. itemShape = this._getItemShape(lastX, lastY, itemWidth, itemHeight, this._selectedMap[i] ? color : '#ccc');
  812. itemShape._idx = i;
  813. itemShape.onmousemove = this._dispatchHoverLink;
  814. if (this.dataRangeOption.selectedMode) {
  815. itemShape.clickable = true;
  816. itemShape.onclick = this._dataRangeSelected;
  817. }
  818. this.shapeList.push(new RectangleShape(itemShape));
  819. if (needValueText) {
  820. textShape = {
  821. zlevel: this.getZlevelBase(),
  822. z: this.getZBase(),
  823. style: {
  824. x: lastX + itemWidth + 5,
  825. y: lastY,
  826. color: this._selectedMap[i] ? this.dataRangeOption.textStyle.color : '#ccc',
  827. text: data[i],
  828. textFont: font,
  829. textBaseline: 'top'
  830. },
  831. highlightStyle: { brushType: 'fill' }
  832. };
  833. if (this.dataRangeOption.orient == 'vertical' && this.dataRangeOption.x == 'right') {
  834. textShape.style.x -= itemWidth + 10;
  835. textShape.style.textAlign = 'right';
  836. }
  837. textShape._idx = i;
  838. textShape.onmousemove = this._dispatchHoverLink;
  839. if (this.dataRangeOption.selectedMode) {
  840. textShape.clickable = true;
  841. textShape.onclick = this._dataRangeSelected;
  842. }
  843. this.shapeList.push(new TextShape(textShape));
  844. }
  845. if (this.dataRangeOption.orient == 'horizontal') {
  846. lastX += itemWidth + (needValueText ? 5 : 0) + (needValueText ? zrArea.getTextWidth(itemName, font) : 0) + itemGap;
  847. } else {
  848. lastY += itemHeight + itemGap;
  849. }
  850. }
  851. if (!needValueText && this.dataRangeOption.text[1]) {
  852. if (this.dataRangeOption.orient == 'horizontal') {
  853. lastX = lastX - itemGap + this._textGap;
  854. } else {
  855. lastY = lastY - itemGap + this._textGap;
  856. }
  857. textShape = this._getTextShape(lastX, lastY, this.dataRangeOption.text[1]);
  858. if (this.dataRangeOption.orient != 'horizontal') {
  859. textShape.style.y -= 5;
  860. textShape.style.textBaseline = 'top';
  861. }
  862. this.shapeList.push(new TextShape(textShape));
  863. }
  864. },
  865. _buildGradient: function () {
  866. var itemShape;
  867. var textShape;
  868. var font = this.getFont(this.dataRangeOption.textStyle);
  869. var lastX = this._itemGroupLocation.x;
  870. var lastY = this._itemGroupLocation.y;
  871. var itemWidth = this.dataRangeOption.itemWidth;
  872. var itemHeight = this.dataRangeOption.itemHeight;
  873. var textHeight = zrArea.getTextHeight('国', font);
  874. var mSize = 10;
  875. var needValueText = true;
  876. if (this.dataRangeOption.text) {
  877. needValueText = false;
  878. if (this.dataRangeOption.text[0]) {
  879. textShape = this._getTextShape(lastX, lastY, this.dataRangeOption.text[0]);
  880. if (this.dataRangeOption.orient == 'horizontal') {
  881. lastX += zrArea.getTextWidth(this.dataRangeOption.text[0], font) + this._textGap;
  882. } else {
  883. lastY += textHeight + this._textGap;
  884. textShape.style.y += textHeight / 2 + this._textGap;
  885. textShape.style.textBaseline = 'bottom';
  886. }
  887. this.shapeList.push(new TextShape(textShape));
  888. }
  889. }
  890. var zrColor = require('zrender/tool/color');
  891. var per = 1 / (this.dataRangeOption.color.length - 1);
  892. var colorList = [];
  893. for (var i = 0, l = this.dataRangeOption.color.length; i < l; i++) {
  894. colorList.push([
  895. i * per,
  896. this.dataRangeOption.color[i]
  897. ]);
  898. }
  899. if (this.dataRangeOption.orient == 'horizontal') {
  900. itemShape = {
  901. zlevel: this.getZlevelBase(),
  902. z: this.getZBase(),
  903. style: {
  904. x: lastX,
  905. y: lastY,
  906. width: itemWidth * mSize,
  907. height: itemHeight,
  908. color: zrColor.getLinearGradient(lastX, lastY, lastX + itemWidth * mSize, lastY, colorList)
  909. },
  910. hoverable: false
  911. };
  912. lastX += itemWidth * mSize + this._textGap;
  913. } else {
  914. itemShape = {
  915. zlevel: this.getZlevelBase(),
  916. z: this.getZBase(),
  917. style: {
  918. x: lastX,
  919. y: lastY,
  920. width: itemWidth,
  921. height: itemHeight * mSize,
  922. color: zrColor.getLinearGradient(lastX, lastY, lastX, lastY + itemHeight * mSize, colorList)
  923. },
  924. hoverable: false
  925. };
  926. lastY += itemHeight * mSize + this._textGap;
  927. }
  928. this.shapeList.push(new RectangleShape(itemShape));
  929. this._calculableLocation = itemShape.style;
  930. if (this.dataRangeOption.calculable) {
  931. this._buildFiller();
  932. this._bulidMask();
  933. this._bulidHandle();
  934. }
  935. this._buildIndicator();
  936. if (!needValueText && this.dataRangeOption.text[1]) {
  937. textShape = this._getTextShape(lastX, lastY, this.dataRangeOption.text[1]);
  938. this.shapeList.push(new TextShape(textShape));
  939. }
  940. },
  941. _buildIndicator: function () {
  942. var x = this._calculableLocation.x;
  943. var y = this._calculableLocation.y;
  944. var width = this._calculableLocation.width;
  945. var height = this._calculableLocation.height;
  946. var size = 5;
  947. var pointList;
  948. var textPosition;
  949. if (this.dataRangeOption.orient == 'horizontal') {
  950. if (this.dataRangeOption.y != 'bottom') {
  951. pointList = [
  952. [
  953. x,
  954. y + height
  955. ],
  956. [
  957. x - size,
  958. y + height + size
  959. ],
  960. [
  961. x + size,
  962. y + height + size
  963. ]
  964. ];
  965. textPosition = 'bottom';
  966. } else {
  967. pointList = [
  968. [
  969. x,
  970. y
  971. ],
  972. [
  973. x - size,
  974. y - size
  975. ],
  976. [
  977. x + size,
  978. y - size
  979. ]
  980. ];
  981. textPosition = 'top';
  982. }
  983. } else {
  984. if (this.dataRangeOption.x != 'right') {
  985. pointList = [
  986. [
  987. x + width,
  988. y
  989. ],
  990. [
  991. x + width + size,
  992. y - size
  993. ],
  994. [
  995. x + width + size,
  996. y + size
  997. ]
  998. ];
  999. textPosition = 'right';
  1000. } else {
  1001. pointList = [
  1002. [
  1003. x,
  1004. y
  1005. ],
  1006. [
  1007. x - size,
  1008. y - size
  1009. ],
  1010. [
  1011. x - size,
  1012. y + size
  1013. ]
  1014. ];
  1015. textPosition = 'left';
  1016. }
  1017. }
  1018. this._indicatorShape = {
  1019. style: {
  1020. pointList: pointList,
  1021. color: '#fff',
  1022. __rect: {
  1023. x: Math.min(pointList[0][0], pointList[1][0]),
  1024. y: Math.min(pointList[0][1], pointList[1][1]),
  1025. width: size * (this.dataRangeOption.orient == 'horizontal' ? 2 : 1),
  1026. height: size * (this.dataRangeOption.orient == 'horizontal' ? 1 : 2)
  1027. }
  1028. },
  1029. highlightStyle: {
  1030. brushType: 'fill',
  1031. textPosition: textPosition,
  1032. textColor: this.dataRangeOption.textStyle.color
  1033. },
  1034. hoverable: false
  1035. };
  1036. this._indicatorShape = new HandlePolygonShape(this._indicatorShape);
  1037. },
  1038. _buildFiller: function () {
  1039. this._fillerShape = {
  1040. zlevel: this.getZlevelBase(),
  1041. z: this.getZBase() + 1,
  1042. style: {
  1043. x: this._calculableLocation.x,
  1044. y: this._calculableLocation.y,
  1045. width: this._calculableLocation.width,
  1046. height: this._calculableLocation.height,
  1047. color: 'rgba(255,255,255,0)'
  1048. },
  1049. highlightStyle: {
  1050. strokeColor: 'rgba(255,255,255,0.5)',
  1051. lineWidth: 1
  1052. },
  1053. draggable: true,
  1054. ondrift: this._ondrift,
  1055. ondragend: this._ondragend,
  1056. onmousemove: this._dispatchHoverLink,
  1057. _type: 'filler'
  1058. };
  1059. this._fillerShape = new RectangleShape(this._fillerShape);
  1060. this.shapeList.push(this._fillerShape);
  1061. },
  1062. _bulidHandle: function () {
  1063. var x = this._calculableLocation.x;
  1064. var y = this._calculableLocation.y;
  1065. var width = this._calculableLocation.width;
  1066. var height = this._calculableLocation.height;
  1067. var font = this.getFont(this.dataRangeOption.textStyle);
  1068. var textHeight = zrArea.getTextHeight('国', font);
  1069. var textWidth = Math.max(zrArea.getTextWidth(this._textFormat(this.dataRangeOption.max), font), zrArea.getTextWidth(this._textFormat(this.dataRangeOption.min), font)) + 2;
  1070. var pointListStart;
  1071. var textXStart;
  1072. var textYStart;
  1073. var coverRectStart;
  1074. var pointListEnd;
  1075. var textXEnd;
  1076. var textYEnd;
  1077. var coverRectEnd;
  1078. if (this.dataRangeOption.orient == 'horizontal') {
  1079. if (this.dataRangeOption.y != 'bottom') {
  1080. pointListStart = [
  1081. [
  1082. x,
  1083. y
  1084. ],
  1085. [
  1086. x,
  1087. y + height + textHeight
  1088. ],
  1089. [
  1090. x - textHeight,
  1091. y + height + textHeight
  1092. ],
  1093. [
  1094. x - 1,
  1095. y + height
  1096. ],
  1097. [
  1098. x - 1,
  1099. y
  1100. ]
  1101. ];
  1102. textXStart = x - textWidth / 2 - textHeight;
  1103. textYStart = y + height + textHeight / 2 + 2;
  1104. coverRectStart = {
  1105. x: x - textWidth - textHeight,
  1106. y: y + height,
  1107. width: textWidth + textHeight,
  1108. height: textHeight
  1109. };
  1110. pointListEnd = [
  1111. [
  1112. x + width,
  1113. y
  1114. ],
  1115. [
  1116. x + width,
  1117. y + height + textHeight
  1118. ],
  1119. [
  1120. x + width + textHeight,
  1121. y + height + textHeight
  1122. ],
  1123. [
  1124. x + width + 1,
  1125. y + height
  1126. ],
  1127. [
  1128. x + width + 1,
  1129. y
  1130. ]
  1131. ];
  1132. textXEnd = x + width + textWidth / 2 + textHeight;
  1133. textYEnd = textYStart;
  1134. coverRectEnd = {
  1135. x: x + width,
  1136. y: y + height,
  1137. width: textWidth + textHeight,
  1138. height: textHeight
  1139. };
  1140. } else {
  1141. pointListStart = [
  1142. [
  1143. x,
  1144. y + height
  1145. ],
  1146. [
  1147. x,
  1148. y - textHeight
  1149. ],
  1150. [
  1151. x - textHeight,
  1152. y - textHeight
  1153. ],
  1154. [
  1155. x - 1,
  1156. y
  1157. ],
  1158. [
  1159. x - 1,
  1160. y + height
  1161. ]
  1162. ];
  1163. textXStart = x - textWidth / 2 - textHeight;
  1164. textYStart = y - textHeight / 2 - 2;
  1165. coverRectStart = {
  1166. x: x - textWidth - textHeight,
  1167. y: y - textHeight,
  1168. width: textWidth + textHeight,
  1169. height: textHeight
  1170. };
  1171. pointListEnd = [
  1172. [
  1173. x + width,
  1174. y + height
  1175. ],
  1176. [
  1177. x + width,
  1178. y - textHeight
  1179. ],
  1180. [
  1181. x + width + textHeight,
  1182. y - textHeight
  1183. ],
  1184. [
  1185. x + width + 1,
  1186. y
  1187. ],
  1188. [
  1189. x + width + 1,
  1190. y + height
  1191. ]
  1192. ];
  1193. textXEnd = x + width + textWidth / 2 + textHeight;
  1194. textYEnd = textYStart;
  1195. coverRectEnd = {
  1196. x: x + width,
  1197. y: y - textHeight,
  1198. width: textWidth + textHeight,
  1199. height: textHeight
  1200. };
  1201. }
  1202. } else {
  1203. textWidth += textHeight;
  1204. if (this.dataRangeOption.x != 'right') {
  1205. pointListStart = [
  1206. [
  1207. x,
  1208. y
  1209. ],
  1210. [
  1211. x + width + textHeight,
  1212. y
  1213. ],
  1214. [
  1215. x + width + textHeight,
  1216. y - textHeight
  1217. ],
  1218. [
  1219. x + width,
  1220. y - 1
  1221. ],
  1222. [
  1223. x,
  1224. y - 1
  1225. ]
  1226. ];
  1227. textXStart = x + width + textWidth / 2 + textHeight / 2;
  1228. textYStart = y - textHeight / 2;
  1229. coverRectStart = {
  1230. x: x + width,
  1231. y: y - textHeight,
  1232. width: textWidth + textHeight,
  1233. height: textHeight
  1234. };
  1235. pointListEnd = [
  1236. [
  1237. x,
  1238. y + height
  1239. ],
  1240. [
  1241. x + width + textHeight,
  1242. y + height
  1243. ],
  1244. [
  1245. x + width + textHeight,
  1246. y + textHeight + height
  1247. ],
  1248. [
  1249. x + width,
  1250. y + 1 + height
  1251. ],
  1252. [
  1253. x,
  1254. y + height + 1
  1255. ]
  1256. ];
  1257. textXEnd = textXStart;
  1258. textYEnd = y + height + textHeight / 2;
  1259. coverRectEnd = {
  1260. x: x + width,
  1261. y: y + height,
  1262. width: textWidth + textHeight,
  1263. height: textHeight
  1264. };
  1265. } else {
  1266. pointListStart = [
  1267. [
  1268. x + width,
  1269. y
  1270. ],
  1271. [
  1272. x - textHeight,
  1273. y
  1274. ],
  1275. [
  1276. x - textHeight,
  1277. y - textHeight
  1278. ],
  1279. [
  1280. x,
  1281. y - 1
  1282. ],
  1283. [
  1284. x + width,
  1285. y - 1
  1286. ]
  1287. ];
  1288. textXStart = x - textWidth / 2 - textHeight / 2;
  1289. textYStart = y - textHeight / 2;
  1290. coverRectStart = {
  1291. x: x - textWidth - textHeight,
  1292. y: y - textHeight,
  1293. width: textWidth + textHeight,
  1294. height: textHeight
  1295. };
  1296. pointListEnd = [
  1297. [
  1298. x + width,
  1299. y + height
  1300. ],
  1301. [
  1302. x - textHeight,
  1303. y + height
  1304. ],
  1305. [
  1306. x - textHeight,
  1307. y + textHeight + height
  1308. ],
  1309. [
  1310. x,
  1311. y + 1 + height
  1312. ],
  1313. [
  1314. x + width,
  1315. y + height + 1
  1316. ]
  1317. ];
  1318. textXEnd = textXStart;
  1319. textYEnd = y + height + textHeight / 2;
  1320. coverRectEnd = {
  1321. x: x - textWidth - textHeight,
  1322. y: y + height,
  1323. width: textWidth + textHeight,
  1324. height: textHeight
  1325. };
  1326. }
  1327. }
  1328. this._startShape = {
  1329. style: {
  1330. pointList: pointListStart,
  1331. text: this._textFormat(this.dataRangeOption.max),
  1332. textX: textXStart,
  1333. textY: textYStart,
  1334. textFont: font,
  1335. color: this.getColor(this.dataRangeOption.max),
  1336. rect: coverRectStart,
  1337. x: pointListStart[0][0],
  1338. y: pointListStart[0][1],
  1339. _x: pointListStart[0][0],
  1340. _y: pointListStart[0][1]
  1341. }
  1342. };
  1343. this._startShape.highlightStyle = {
  1344. strokeColor: this._startShape.style.color,
  1345. lineWidth: 1
  1346. };
  1347. this._endShape = {
  1348. style: {
  1349. pointList: pointListEnd,
  1350. text: this._textFormat(this.dataRangeOption.min),
  1351. textX: textXEnd,
  1352. textY: textYEnd,
  1353. textFont: font,
  1354. color: this.getColor(this.dataRangeOption.min),
  1355. rect: coverRectEnd,
  1356. x: pointListEnd[0][0],
  1357. y: pointListEnd[0][1],
  1358. _x: pointListEnd[0][0],
  1359. _y: pointListEnd[0][1]
  1360. }
  1361. };
  1362. this._endShape.highlightStyle = {
  1363. strokeColor: this._endShape.style.color,
  1364. lineWidth: 1
  1365. };
  1366. this._startShape.zlevel = this._endShape.zlevel = this.getZlevelBase();
  1367. this._startShape.z = this._endShape.z = this.getZBase() + 1;
  1368. this._startShape.draggable = this._endShape.draggable = true;
  1369. this._startShape.ondrift = this._endShape.ondrift = this._ondrift;
  1370. this._startShape.ondragend = this._endShape.ondragend = this._ondragend;
  1371. this._startShape.style.textColor = this._endShape.style.textColor = this.dataRangeOption.textStyle.color;
  1372. this._startShape.style.textAlign = this._endShape.style.textAlign = 'center';
  1373. this._startShape.style.textPosition = this._endShape.style.textPosition = 'specific';
  1374. this._startShape.style.textBaseline = this._endShape.style.textBaseline = 'middle';
  1375. this._startShape.style.width = this._endShape.style.width = 0;
  1376. this._startShape.style.height = this._endShape.style.height = 0;
  1377. this._startShape.style.textPosition = this._endShape.style.textPosition = 'specific';
  1378. this._startShape = new HandlePolygonShape(this._startShape);
  1379. this._endShape = new HandlePolygonShape(this._endShape);
  1380. this.shapeList.push(this._startShape);
  1381. this.shapeList.push(this._endShape);
  1382. },
  1383. _bulidMask: function () {
  1384. var x = this._calculableLocation.x;
  1385. var y = this._calculableLocation.y;
  1386. var width = this._calculableLocation.width;
  1387. var height = this._calculableLocation.height;
  1388. this._startMask = {
  1389. zlevel: this.getZlevelBase(),
  1390. z: this.getZBase() + 1,
  1391. style: {
  1392. x: x,
  1393. y: y,
  1394. width: this.dataRangeOption.orient == 'horizontal' ? 0 : width,
  1395. height: this.dataRangeOption.orient == 'horizontal' ? height : 0,
  1396. color: '#ccc'
  1397. },
  1398. hoverable: false
  1399. };
  1400. this._endMask = {
  1401. zlevel: this.getZlevelBase(),
  1402. z: this.getZBase() + 1,
  1403. style: {
  1404. x: this.dataRangeOption.orient == 'horizontal' ? x + width : x,
  1405. y: this.dataRangeOption.orient == 'horizontal' ? y : y + height,
  1406. width: this.dataRangeOption.orient == 'horizontal' ? 0 : width,
  1407. height: this.dataRangeOption.orient == 'horizontal' ? height : 0,
  1408. color: '#ccc'
  1409. },
  1410. hoverable: false
  1411. };
  1412. this._startMask = new RectangleShape(this._startMask);
  1413. this._endMask = new RectangleShape(this._endMask);
  1414. this.shapeList.push(this._startMask);
  1415. this.shapeList.push(this._endMask);
  1416. },
  1417. _buildBackground: function () {
  1418. var padding = this.reformCssArray(this.dataRangeOption.padding);
  1419. this.shapeList.push(new RectangleShape({
  1420. zlevel: this.getZlevelBase(),
  1421. z: this.getZBase(),
  1422. hoverable: false,
  1423. style: {
  1424. x: this._itemGroupLocation.x - padding[3],
  1425. y: this._itemGroupLocation.y - padding[0],
  1426. width: this._itemGroupLocation.width + padding[3] + padding[1],
  1427. height: this._itemGroupLocation.height + padding[0] + padding[2],
  1428. brushType: this.dataRangeOption.borderWidth === 0 ? 'fill' : 'both',
  1429. color: this.dataRangeOption.backgroundColor,
  1430. strokeColor: this.dataRangeOption.borderColor,
  1431. lineWidth: this.dataRangeOption.borderWidth
  1432. }
  1433. }));
  1434. },
  1435. _getItemGroupLocation: function () {
  1436. var data = this._valueTextList;
  1437. var dataLength = data.length;
  1438. var itemGap = this.dataRangeOption.itemGap;
  1439. var itemWidth = this.dataRangeOption.itemWidth;
  1440. var itemHeight = this.dataRangeOption.itemHeight;
  1441. var totalWidth = 0;
  1442. var totalHeight = 0;
  1443. var font = this.getFont(this.dataRangeOption.textStyle);
  1444. var textHeight = zrArea.getTextHeight('国', font);
  1445. var mSize = 10;
  1446. if (this.dataRangeOption.orient == 'horizontal') {
  1447. if (this.dataRangeOption.text || this._isContinuity()) {
  1448. totalWidth = (this._isContinuity() ? itemWidth * mSize + itemGap : dataLength * (itemWidth + itemGap)) + (this.dataRangeOption.text && typeof this.dataRangeOption.text[0] != 'undefined' ? zrArea.getTextWidth(this.dataRangeOption.text[0], font) + this._textGap : 0) + (this.dataRangeOption.text && typeof this.dataRangeOption.text[1] != 'undefined' ? zrArea.getTextWidth(this.dataRangeOption.text[1], font) + this._textGap : 0);
  1449. } else {
  1450. itemWidth += 5;
  1451. for (var i = 0; i < dataLength; i++) {
  1452. totalWidth += itemWidth + zrArea.getTextWidth(data[i], font) + itemGap;
  1453. }
  1454. }
  1455. totalWidth -= itemGap;
  1456. totalHeight = Math.max(textHeight, itemHeight);
  1457. } else {
  1458. var maxWidth;
  1459. if (this.dataRangeOption.text || this._isContinuity()) {
  1460. totalHeight = (this._isContinuity() ? itemHeight * mSize + itemGap : dataLength * (itemHeight + itemGap)) + (this.dataRangeOption.text && typeof this.dataRangeOption.text[0] != 'undefined' ? this._textGap + textHeight : 0) + (this.dataRangeOption.text && typeof this.dataRangeOption.text[1] != 'undefined' ? this._textGap + textHeight : 0);
  1461. maxWidth = Math.max(zrArea.getTextWidth(this.dataRangeOption.text && this.dataRangeOption.text[0] || '', font), zrArea.getTextWidth(this.dataRangeOption.text && this.dataRangeOption.text[1] || '', font));
  1462. totalWidth = Math.max(itemWidth, maxWidth);
  1463. } else {
  1464. totalHeight = (itemHeight + itemGap) * dataLength;
  1465. itemWidth += 5;
  1466. maxWidth = 0;
  1467. for (var i = 0; i < dataLength; i++) {
  1468. maxWidth = Math.max(maxWidth, zrArea.getTextWidth(data[i], font));
  1469. }
  1470. totalWidth = itemWidth + maxWidth;
  1471. }
  1472. totalHeight -= itemGap;
  1473. }
  1474. var padding = this.reformCssArray(this.dataRangeOption.padding);
  1475. var x;
  1476. var zrWidth = this.zr.getWidth();
  1477. switch (this.dataRangeOption.x) {
  1478. case 'center':
  1479. x = Math.floor((zrWidth - totalWidth) / 2);
  1480. break;
  1481. case 'left':
  1482. x = padding[3] + this.dataRangeOption.borderWidth;
  1483. break;
  1484. case 'right':
  1485. x = zrWidth - totalWidth - padding[1] - this.dataRangeOption.borderWidth;
  1486. break;
  1487. default:
  1488. x = this.parsePercent(this.dataRangeOption.x, zrWidth);
  1489. x = isNaN(x) ? 0 : x;
  1490. break;
  1491. }
  1492. var y;
  1493. var zrHeight = this.zr.getHeight();
  1494. switch (this.dataRangeOption.y) {
  1495. case 'top':
  1496. y = padding[0] + this.dataRangeOption.borderWidth;
  1497. break;
  1498. case 'bottom':
  1499. y = zrHeight - totalHeight - padding[2] - this.dataRangeOption.borderWidth;
  1500. break;
  1501. case 'center':
  1502. y = Math.floor((zrHeight - totalHeight) / 2);
  1503. break;
  1504. default:
  1505. y = this.parsePercent(this.dataRangeOption.y, zrHeight);
  1506. y = isNaN(y) ? 0 : y;
  1507. break;
  1508. }
  1509. if (this.dataRangeOption.calculable) {
  1510. var handlerWidth = Math.max(zrArea.getTextWidth(this.dataRangeOption.max, font), zrArea.getTextWidth(this.dataRangeOption.min, font)) + textHeight;
  1511. if (this.dataRangeOption.orient == 'horizontal') {
  1512. if (x < handlerWidth) {
  1513. x = handlerWidth;
  1514. }
  1515. if (x + totalWidth + handlerWidth > zrWidth) {
  1516. x -= handlerWidth;
  1517. }
  1518. } else {
  1519. if (y < textHeight) {
  1520. y = textHeight;
  1521. }
  1522. if (y + totalHeight + textHeight > zrHeight) {
  1523. y -= textHeight;
  1524. }
  1525. }
  1526. }
  1527. return {
  1528. x: x,
  1529. y: y,
  1530. width: totalWidth,
  1531. height: totalHeight
  1532. };
  1533. },
  1534. _getTextShape: function (x, y, text) {
  1535. return {
  1536. zlevel: this.getZlevelBase(),
  1537. z: this.getZBase(),
  1538. style: {
  1539. x: this.dataRangeOption.orient == 'horizontal' ? x : this._itemGroupLocation.x + this._itemGroupLocation.width / 2,
  1540. y: this.dataRangeOption.orient == 'horizontal' ? this._itemGroupLocation.y + this._itemGroupLocation.height / 2 : y,
  1541. color: this.dataRangeOption.textStyle.color,
  1542. text: text,
  1543. textFont: this.getFont(this.dataRangeOption.textStyle),
  1544. textBaseline: this.dataRangeOption.orient == 'horizontal' ? 'middle' : 'top',
  1545. textAlign: this.dataRangeOption.orient == 'horizontal' ? 'left' : 'center'
  1546. },
  1547. hoverable: false
  1548. };
  1549. },
  1550. _getItemShape: function (x, y, width, height, color) {
  1551. return {
  1552. zlevel: this.getZlevelBase(),
  1553. z: this.getZBase(),
  1554. style: {
  1555. x: x,
  1556. y: y + 1,
  1557. width: width,
  1558. height: height - 2,
  1559. color: color
  1560. },
  1561. highlightStyle: {
  1562. strokeColor: color,
  1563. lineWidth: 1
  1564. }
  1565. };
  1566. },
  1567. __ondrift: function (shape, dx, dy) {
  1568. var x = this._calculableLocation.x;
  1569. var y = this._calculableLocation.y;
  1570. var width = this._calculableLocation.width;
  1571. var height = this._calculableLocation.height;
  1572. if (this.dataRangeOption.orient == 'horizontal') {
  1573. if (shape.style.x + dx <= x) {
  1574. shape.style.x = x;
  1575. } else if (shape.style.x + dx + shape.style.width >= x + width) {
  1576. shape.style.x = x + width - shape.style.width;
  1577. } else {
  1578. shape.style.x += dx;
  1579. }
  1580. } else {
  1581. if (shape.style.y + dy <= y) {
  1582. shape.style.y = y;
  1583. } else if (shape.style.y + dy + shape.style.height >= y + height) {
  1584. shape.style.y = y + height - shape.style.height;
  1585. } else {
  1586. shape.style.y += dy;
  1587. }
  1588. }
  1589. if (shape._type == 'filler') {
  1590. this._syncHandleShape();
  1591. } else {
  1592. this._syncFillerShape(shape);
  1593. }
  1594. if (this.dataRangeOption.realtime) {
  1595. this._dispatchDataRange();
  1596. }
  1597. return true;
  1598. },
  1599. __ondragend: function () {
  1600. this.isDragend = true;
  1601. },
  1602. ondragend: function (param, status) {
  1603. if (!this.isDragend || !param.target) {
  1604. return;
  1605. }
  1606. status.dragOut = true;
  1607. status.dragIn = true;
  1608. if (!this.dataRangeOption.realtime) {
  1609. this._dispatchDataRange();
  1610. }
  1611. status.needRefresh = false;
  1612. this.isDragend = false;
  1613. return;
  1614. },
  1615. _syncShapeFromRange: function () {
  1616. var range = this.dataRangeOption.range || {};
  1617. var optRangeStart = range.start;
  1618. var optRangeEnd = range.end;
  1619. if (optRangeEnd < optRangeStart) {
  1620. optRangeStart = [
  1621. optRangeEnd,
  1622. optRangeEnd = optRangeStart
  1623. ][0];
  1624. }
  1625. this._range.end = optRangeStart != null ? optRangeStart : this._range.end != null ? this._range.end : 0;
  1626. this._range.start = optRangeEnd != null ? optRangeEnd : this._range.start != null ? this._range.start : 100;
  1627. if (this._range.start != 100 || this._range.end !== 0) {
  1628. if (this.dataRangeOption.orient == 'horizontal') {
  1629. var width = this._fillerShape.style.width;
  1630. this._fillerShape.style.x += width * (100 - this._range.start) / 100;
  1631. this._fillerShape.style.width = width * (this._range.start - this._range.end) / 100;
  1632. } else {
  1633. var height = this._fillerShape.style.height;
  1634. this._fillerShape.style.y += height * (100 - this._range.start) / 100;
  1635. this._fillerShape.style.height = height * (this._range.start - this._range.end) / 100;
  1636. }
  1637. this.zr.modShape(this._fillerShape.id);
  1638. this._syncHandleShape();
  1639. }
  1640. },
  1641. _syncHandleShape: function () {
  1642. var x = this._calculableLocation.x;
  1643. var y = this._calculableLocation.y;
  1644. var width = this._calculableLocation.width;
  1645. var height = this._calculableLocation.height;
  1646. if (this.dataRangeOption.orient == 'horizontal') {
  1647. this._startShape.style.x = this._fillerShape.style.x;
  1648. this._startMask.style.width = this._startShape.style.x - x;
  1649. this._endShape.style.x = this._fillerShape.style.x + this._fillerShape.style.width;
  1650. this._endMask.style.x = this._endShape.style.x;
  1651. this._endMask.style.width = x + width - this._endShape.style.x;
  1652. this._range.start = Math.ceil(100 - (this._startShape.style.x - x) / width * 100);
  1653. this._range.end = Math.floor(100 - (this._endShape.style.x - x) / width * 100);
  1654. } else {
  1655. this._startShape.style.y = this._fillerShape.style.y;
  1656. this._startMask.style.height = this._startShape.style.y - y;
  1657. this._endShape.style.y = this._fillerShape.style.y + this._fillerShape.style.height;
  1658. this._endMask.style.y = this._endShape.style.y;
  1659. this._endMask.style.height = y + height - this._endShape.style.y;
  1660. this._range.start = Math.ceil(100 - (this._startShape.style.y - y) / height * 100);
  1661. this._range.end = Math.floor(100 - (this._endShape.style.y - y) / height * 100);
  1662. }
  1663. this._syncShape();
  1664. },
  1665. _syncFillerShape: function (e) {
  1666. var x = this._calculableLocation.x;
  1667. var y = this._calculableLocation.y;
  1668. var width = this._calculableLocation.width;
  1669. var height = this._calculableLocation.height;
  1670. var a;
  1671. var b;
  1672. if (this.dataRangeOption.orient == 'horizontal') {
  1673. a = this._startShape.style.x;
  1674. b = this._endShape.style.x;
  1675. if (e.id == this._startShape.id && a >= b) {
  1676. b = a;
  1677. this._endShape.style.x = a;
  1678. } else if (e.id == this._endShape.id && a >= b) {
  1679. a = b;
  1680. this._startShape.style.x = a;
  1681. }
  1682. this._fillerShape.style.x = a;
  1683. this._fillerShape.style.width = b - a;
  1684. this._startMask.style.width = a - x;
  1685. this._endMask.style.x = b;
  1686. this._endMask.style.width = x + width - b;
  1687. this._range.start = Math.ceil(100 - (a - x) / width * 100);
  1688. this._range.end = Math.floor(100 - (b - x) / width * 100);
  1689. } else {
  1690. a = this._startShape.style.y;
  1691. b = this._endShape.style.y;
  1692. if (e.id == this._startShape.id && a >= b) {
  1693. b = a;
  1694. this._endShape.style.y = a;
  1695. } else if (e.id == this._endShape.id && a >= b) {
  1696. a = b;
  1697. this._startShape.style.y = a;
  1698. }
  1699. this._fillerShape.style.y = a;
  1700. this._fillerShape.style.height = b - a;
  1701. this._startMask.style.height = a - y;
  1702. this._endMask.style.y = b;
  1703. this._endMask.style.height = y + height - b;
  1704. this._range.start = Math.ceil(100 - (a - y) / height * 100);
  1705. this._range.end = Math.floor(100 - (b - y) / height * 100);
  1706. }
  1707. this._syncShape();
  1708. },
  1709. _syncShape: function () {
  1710. this._startShape.position = [
  1711. this._startShape.style.x - this._startShape.style._x,
  1712. this._startShape.style.y - this._startShape.style._y
  1713. ];
  1714. this._startShape.style.text = this._textFormat(this._gap * this._range.start + this.dataRangeOption.min);
  1715. this._startShape.style.color = this._startShape.highlightStyle.strokeColor = this.getColor(this._gap * this._range.start + this.dataRangeOption.min);
  1716. this._endShape.position = [
  1717. this._endShape.style.x - this._endShape.style._x,
  1718. this._endShape.style.y - this._endShape.style._y
  1719. ];
  1720. this._endShape.style.text = this._textFormat(this._gap * this._range.end + this.dataRangeOption.min);
  1721. this._endShape.style.color = this._endShape.highlightStyle.strokeColor = this.getColor(this._gap * this._range.end + this.dataRangeOption.min);
  1722. this.zr.modShape(this._startShape.id);
  1723. this.zr.modShape(this._endShape.id);
  1724. this.zr.modShape(this._startMask.id);
  1725. this.zr.modShape(this._endMask.id);
  1726. this.zr.modShape(this._fillerShape.id);
  1727. this.zr.refreshNextFrame();
  1728. },
  1729. _dispatchDataRange: function () {
  1730. this.messageCenter.dispatch(ecConfig.EVENT.DATA_RANGE, null, {
  1731. range: {
  1732. start: this._range.end,
  1733. end: this._range.start
  1734. }
  1735. }, this.myChart);
  1736. },
  1737. __dataRangeSelected: function (param) {
  1738. if (this.dataRangeOption.selectedMode === 'single') {
  1739. for (var k in this._selectedMap) {
  1740. this._selectedMap[k] = false;
  1741. }
  1742. }
  1743. var idx = param.target._idx;
  1744. this._selectedMap[idx] = !this._selectedMap[idx];
  1745. var valueMax;
  1746. var valueMin;
  1747. if (this._useCustomizedSplit()) {
  1748. valueMax = this._splitList[idx].max;
  1749. valueMin = this._splitList[idx].min;
  1750. } else {
  1751. valueMax = (this._colorList.length - idx) * this._gap + this.dataRangeOption.min;
  1752. valueMin = valueMax - this._gap;
  1753. }
  1754. this.messageCenter.dispatch(ecConfig.EVENT.DATA_RANGE_SELECTED, param.event, {
  1755. selected: this._selectedMap,
  1756. target: idx,
  1757. valueMax: valueMax,
  1758. valueMin: valueMin
  1759. }, this.myChart);
  1760. this.messageCenter.dispatch(ecConfig.EVENT.REFRESH, null, null, this.myChart);
  1761. },
  1762. __dispatchHoverLink: function (param) {
  1763. var valueMin;
  1764. var valueMax;
  1765. if (this.dataRangeOption.calculable) {
  1766. var totalValue = this.dataRangeOption.max - this.dataRangeOption.min;
  1767. var curValue;
  1768. if (this.dataRangeOption.orient == 'horizontal') {
  1769. curValue = (1 - (zrEvent.getX(param.event) - this._calculableLocation.x) / this._calculableLocation.width) * totalValue;
  1770. } else {
  1771. curValue = (1 - (zrEvent.getY(param.event) - this._calculableLocation.y) / this._calculableLocation.height) * totalValue;
  1772. }
  1773. valueMin = curValue - totalValue * 0.05;
  1774. valueMax = curValue + totalValue * 0.05;
  1775. } else if (this._useCustomizedSplit()) {
  1776. var idx = param.target._idx;
  1777. valueMax = this._splitList[idx].max;
  1778. valueMin = this._splitList[idx].min;
  1779. } else {
  1780. var idx = param.target._idx;
  1781. valueMax = (this._colorList.length - idx) * this._gap + this.dataRangeOption.min;
  1782. valueMin = valueMax - this._gap;
  1783. }
  1784. this.messageCenter.dispatch(ecConfig.EVENT.DATA_RANGE_HOVERLINK, param.event, {
  1785. valueMin: valueMin,
  1786. valueMax: valueMax
  1787. }, this.myChart);
  1788. },
  1789. __onhoverlink: function (param) {
  1790. if (this.dataRangeOption.show && this.dataRangeOption.hoverLink && this._indicatorShape && param && param.seriesIndex != null && param.dataIndex != null) {
  1791. var curValue = param.value;
  1792. if (curValue === '' || isNaN(curValue)) {
  1793. return;
  1794. }
  1795. if (curValue < this.dataRangeOption.min) {
  1796. curValue = this.dataRangeOption.min;
  1797. } else if (curValue > this.dataRangeOption.max) {
  1798. curValue = this.dataRangeOption.max;
  1799. }
  1800. if (this.dataRangeOption.orient == 'horizontal') {
  1801. this._indicatorShape.position = [
  1802. (this.dataRangeOption.max - curValue) / (this.dataRangeOption.max - this.dataRangeOption.min) * this._calculableLocation.width,
  1803. 0
  1804. ];
  1805. } else {
  1806. this._indicatorShape.position = [
  1807. 0,
  1808. (this.dataRangeOption.max - curValue) / (this.dataRangeOption.max - this.dataRangeOption.min) * this._calculableLocation.height
  1809. ];
  1810. }
  1811. this._indicatorShape.style.text = this._textFormat(param.value);
  1812. this._indicatorShape.style.color = this.getColor(curValue);
  1813. this.zr.addHoverShape(this._indicatorShape);
  1814. }
  1815. },
  1816. _textFormat: function (valueStart, valueEnd) {
  1817. var dataRangeOption = this.dataRangeOption;
  1818. if (valueStart !== -Number.MAX_VALUE) {
  1819. valueStart = (+valueStart).toFixed(dataRangeOption.precision);
  1820. }
  1821. if (valueEnd != null && valueEnd !== Number.MAX_VALUE) {
  1822. valueEnd = (+valueEnd).toFixed(dataRangeOption.precision);
  1823. }
  1824. if (dataRangeOption.formatter) {
  1825. if (typeof dataRangeOption.formatter == 'string') {
  1826. return dataRangeOption.formatter.replace('{value}', valueStart === -Number.MAX_VALUE ? 'min' : valueStart).replace('{value2}', valueEnd === Number.MAX_VALUE ? 'max' : valueEnd);
  1827. } else if (typeof dataRangeOption.formatter == 'function') {
  1828. return dataRangeOption.formatter.call(this.myChart, valueStart, valueEnd);
  1829. }
  1830. }
  1831. if (valueEnd == null) {
  1832. return valueStart;
  1833. } else {
  1834. if (valueStart === -Number.MAX_VALUE) {
  1835. return '< ' + valueEnd;
  1836. } else if (valueEnd === Number.MAX_VALUE) {
  1837. return '> ' + valueStart;
  1838. } else {
  1839. return valueStart + ' - ' + valueEnd;
  1840. }
  1841. }
  1842. },
  1843. _isContinuity: function () {
  1844. var dataRangeOption = this.dataRangeOption;
  1845. return !(dataRangeOption.splitList ? dataRangeOption.splitList.length > 0 : dataRangeOption.splitNumber > 0) || dataRangeOption.calculable;
  1846. },
  1847. _useCustomizedSplit: function () {
  1848. var dataRangeOption = this.dataRangeOption;
  1849. return dataRangeOption.splitList && dataRangeOption.splitList.length > 0;
  1850. },
  1851. _buildColorList: function (splitNumber) {
  1852. this._colorList = zrColor.getGradientColors(this.dataRangeOption.color, Math.max((splitNumber - this.dataRangeOption.color.length) / (this.dataRangeOption.color.length - 1), 0) + 1);
  1853. if (this._colorList.length > splitNumber) {
  1854. var len = this._colorList.length;
  1855. var newColorList = [this._colorList[0]];
  1856. var step = len / (splitNumber - 1);
  1857. for (var i = 1; i < splitNumber - 1; i++) {
  1858. newColorList.push(this._colorList[Math.floor(i * step)]);
  1859. }
  1860. newColorList.push(this._colorList[len - 1]);
  1861. this._colorList = newColorList;
  1862. }
  1863. if (this._useCustomizedSplit()) {
  1864. var splitList = this._splitList;
  1865. for (var i = 0, len = splitList.length; i < len; i++) {
  1866. if (splitList[i].color) {
  1867. this._colorList[i] = splitList[i].color;
  1868. }
  1869. }
  1870. }
  1871. },
  1872. _buildGap: function (splitNumber) {
  1873. if (!this._useCustomizedSplit()) {
  1874. var precision = this.dataRangeOption.precision;
  1875. this._gap = (this.dataRangeOption.max - this.dataRangeOption.min) / splitNumber;
  1876. while (this._gap.toFixed(precision) - 0 != this._gap && precision < 5) {
  1877. precision++;
  1878. }
  1879. this.dataRangeOption.precision = precision;
  1880. this._gap = ((this.dataRangeOption.max - this.dataRangeOption.min) / splitNumber).toFixed(precision) - 0;
  1881. }
  1882. },
  1883. _buildDataList: function (splitNumber) {
  1884. var valueTextList = this._valueTextList = [];
  1885. var dataRangeOption = this.dataRangeOption;
  1886. var useCustomizedSplit = this._useCustomizedSplit();
  1887. for (var i = 0; i < splitNumber; i++) {
  1888. this._selectedMap[i] = true;
  1889. var text = '';
  1890. if (useCustomizedSplit) {
  1891. var splitListItem = this._splitList[splitNumber - 1 - i];
  1892. if (splitListItem.label != null) {
  1893. text = splitListItem.label;
  1894. } else if (splitListItem.single != null) {
  1895. text = this._textFormat(splitListItem.single);
  1896. } else {
  1897. text = this._textFormat(splitListItem.min, splitListItem.max);
  1898. }
  1899. } else {
  1900. text = this._textFormat(i * this._gap + dataRangeOption.min, (i + 1) * this._gap + dataRangeOption.min);
  1901. }
  1902. valueTextList.unshift(text);
  1903. }
  1904. },
  1905. _buildSplitList: function () {
  1906. if (!this._useCustomizedSplit()) {
  1907. return;
  1908. }
  1909. var splitList = this.dataRangeOption.splitList;
  1910. var splitRangeList = this._splitList = [];
  1911. for (var i = 0, len = splitList.length; i < len; i++) {
  1912. var splitListItem = splitList[i];
  1913. if (!splitListItem || splitListItem.start == null && splitListItem.end == null) {
  1914. throw new Error('Empty item exists in splitList!');
  1915. }
  1916. var reformedItem = {
  1917. label: splitListItem.label,
  1918. color: splitListItem.color
  1919. };
  1920. reformedItem.min = splitListItem.start;
  1921. reformedItem.max = splitListItem.end;
  1922. if (reformedItem.min > reformedItem.max) {
  1923. reformedItem.min = [
  1924. reformedItem.max,
  1925. reformedItem.max = reformedItem.min
  1926. ][0];
  1927. }
  1928. if (reformedItem.min === reformedItem.max) {
  1929. reformedItem.single = reformedItem.max;
  1930. }
  1931. if (reformedItem.min == null) {
  1932. reformedItem.min = -Number.MAX_VALUE;
  1933. }
  1934. if (reformedItem.max == null) {
  1935. reformedItem.max = Number.MAX_VALUE;
  1936. }
  1937. splitRangeList.push(reformedItem);
  1938. }
  1939. },
  1940. refresh: function (newOption) {
  1941. if (newOption) {
  1942. this.option = newOption;
  1943. this.option.dataRange = this.reformOption(this.option.dataRange);
  1944. var dataRangeOption = this.dataRangeOption = this.option.dataRange;
  1945. if (!this._useCustomizedSplit() && (dataRangeOption.min == null || dataRangeOption.max == null)) {
  1946. throw new Error('option.dataRange.min or option.dataRange.max has not been defined.');
  1947. }
  1948. if (!this.myChart.canvasSupported) {
  1949. dataRangeOption.realtime = false;
  1950. }
  1951. var splitNumber = this._isContinuity() ? 100 : this._useCustomizedSplit() ? dataRangeOption.splitList.length : dataRangeOption.splitNumber;
  1952. this._buildSplitList();
  1953. this._buildColorList(splitNumber);
  1954. this._buildGap(splitNumber);
  1955. this._buildDataList(splitNumber);
  1956. }
  1957. this.clear();
  1958. this._buildShape();
  1959. },
  1960. getColor: function (value) {
  1961. if (isNaN(value)) {
  1962. return null;
  1963. }
  1964. var idx;
  1965. if (!this._useCustomizedSplit()) {
  1966. if (this.dataRangeOption.min == this.dataRangeOption.max) {
  1967. return this._colorList[0];
  1968. }
  1969. if (value < this.dataRangeOption.min) {
  1970. value = this.dataRangeOption.min;
  1971. } else if (value > this.dataRangeOption.max) {
  1972. value = this.dataRangeOption.max;
  1973. }
  1974. if (this.dataRangeOption.calculable) {
  1975. if (value - (this._gap * this._range.start + this.dataRangeOption.min) > 0.00005 || value - (this._gap * this._range.end + this.dataRangeOption.min) < -0.00005) {
  1976. return null;
  1977. }
  1978. }
  1979. idx = this._colorList.length - Math.ceil((value - this.dataRangeOption.min) / (this.dataRangeOption.max - this.dataRangeOption.min) * this._colorList.length);
  1980. if (idx == this._colorList.length) {
  1981. idx--;
  1982. }
  1983. } else {
  1984. var splitRangeList = this._splitList;
  1985. for (var i = 0, len = splitRangeList.length; i < len; i++) {
  1986. if (splitRangeList[i].min <= value && splitRangeList[i].max >= value) {
  1987. idx = i;
  1988. break;
  1989. }
  1990. }
  1991. }
  1992. if (this._selectedMap[idx]) {
  1993. return this._colorList[idx];
  1994. } else {
  1995. return null;
  1996. }
  1997. },
  1998. getColorByIndex: function (idx) {
  1999. if (idx >= this._colorList.length) {
  2000. idx = this._colorList.length - 1;
  2001. } else if (idx < 0) {
  2002. idx = 0;
  2003. }
  2004. return this._colorList[idx];
  2005. },
  2006. onbeforDispose: function () {
  2007. this.messageCenter.unbind(ecConfig.EVENT.HOVER, this._onhoverlink);
  2008. }
  2009. };
  2010. zrUtil.inherits(DataRange, Base);
  2011. require('../component').define('dataRange', DataRange);
  2012. return DataRange;
  2013. });define('echarts/layout/WordCloudRectZero', ['require'], function (require) {
  2014. function ZeroArray(option) {
  2015. this.defaultOption = { type: 'RECT' };
  2016. this._init(option);
  2017. }
  2018. ZeroArray.prototype = {
  2019. RECT: '_calculateRect',
  2020. _init: function (option) {
  2021. this._initOption(option);
  2022. this._initCanvas();
  2023. },
  2024. _initOption: function (option) {
  2025. for (k in option) {
  2026. this.defaultOption[k] = option[k];
  2027. }
  2028. },
  2029. _initCanvas: function () {
  2030. var canvas = document.createElement('canvas');
  2031. canvas.width = 1;
  2032. canvas.height = 1;
  2033. var ratio = Math.sqrt(canvas.getContext('2d').getImageData(0, 0, 1, 1).data.length >> 2);
  2034. canvas.width = this.defaultOption.width;
  2035. canvas.height = this.defaultOption.height;
  2036. if (canvas.getContext) {
  2037. var ctx = canvas.getContext('2d');
  2038. }
  2039. this.canvas = canvas;
  2040. this.ctx = ctx;
  2041. this.ratio = ratio;
  2042. },
  2043. calculate: function (callback, callbackObj) {
  2044. var calType = this.defaultOption.type, calmethod = this[calType];
  2045. this[calmethod].call(this, callback, callbackObj);
  2046. },
  2047. _calculateReturn: function (result, callback, callbackObj) {
  2048. callback.call(callbackObj, result);
  2049. },
  2050. _calculateRect: function (callback, callbackObj) {
  2051. var result = {}, width = this.defaultOption.width >> 5 << 5, height = this.defaultOption.height;
  2052. result.initarr = this._rectZeroArray(width * height);
  2053. result.area = width * height;
  2054. result.maxHit = height;
  2055. result.maxWit = width;
  2056. result.imgboard = this._rectBoard(width, height);
  2057. this._calculateReturn(result, callback, callbackObj);
  2058. },
  2059. _rectBoard: function (width, height) {
  2060. var row = [];
  2061. for (var i = 0; i < height; i++) {
  2062. row.push({
  2063. y: i,
  2064. start: 0,
  2065. end: width
  2066. });
  2067. }
  2068. var cloumn = [];
  2069. for (var i = 0; i < width; i++) {
  2070. cloumn.push({
  2071. x: i,
  2072. start: 0,
  2073. end: height
  2074. });
  2075. }
  2076. return {
  2077. row: row,
  2078. cloumn: cloumn
  2079. };
  2080. },
  2081. _rectZeroArray: function (num) {
  2082. var a = [], n = num, i = -1;
  2083. while (++i < n)
  2084. a[i] = 0;
  2085. return a;
  2086. }
  2087. };
  2088. return ZeroArray;
  2089. });define('echarts/util/shape/HandlePolygon', [
  2090. 'require',
  2091. 'zrender/shape/Base',
  2092. 'zrender/shape/Polygon',
  2093. 'zrender/tool/util'
  2094. ], function (require) {
  2095. var Base = require('zrender/shape/Base');
  2096. var PolygonShape = require('zrender/shape/Polygon');
  2097. var zrUtil = require('zrender/tool/util');
  2098. function HandlePolygon(options) {
  2099. Base.call(this, options);
  2100. }
  2101. HandlePolygon.prototype = {
  2102. type: 'handle-polygon',
  2103. buildPath: function (ctx, style) {
  2104. PolygonShape.prototype.buildPath(ctx, style);
  2105. },
  2106. isCover: function (x, y) {
  2107. var originPos = this.transformCoordToLocal(x, y);
  2108. x = originPos[0];
  2109. y = originPos[1];
  2110. var rect = this.style.rect;
  2111. if (x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height) {
  2112. return true;
  2113. } else {
  2114. return false;
  2115. }
  2116. }
  2117. };
  2118. zrUtil.inherits(HandlePolygon, Base);
  2119. return HandlePolygon;
  2120. });