SVGRenderer.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /* *
  2. * (c) 2010-2019 Torstein Honsi
  3. *
  4. * Extensions to the SVGRenderer class to enable 3D shapes
  5. *
  6. * License: www.highcharts.com/license
  7. */
  8. 'use strict';
  9. import H from '../parts/Globals.js';
  10. import '../parts/Utilities.js';
  11. import '../parts/Color.js';
  12. import '../parts/SvgRenderer.js';
  13. var cos = Math.cos,
  14. PI = Math.PI,
  15. sin = Math.sin;
  16. var animObject = H.animObject,
  17. charts = H.charts,
  18. color = H.color,
  19. defined = H.defined,
  20. deg2rad = H.deg2rad,
  21. extend = H.extend,
  22. merge = H.merge,
  23. perspective = H.perspective,
  24. pick = H.pick,
  25. SVGElement = H.SVGElement,
  26. SVGRenderer = H.SVGRenderer,
  27. dFactor,
  28. element3dMethods,
  29. cuboidMethods;
  30. /*
  31. EXTENSION TO THE SVG-RENDERER TO ENABLE 3D SHAPES
  32. */
  33. // HELPER METHODS
  34. dFactor = (4 * (Math.sqrt(2) - 1) / 3) / (PI / 2);
  35. // Method to construct a curved path. Can 'wrap' around more then 180 degrees
  36. function curveTo(cx, cy, rx, ry, start, end, dx, dy) {
  37. var result = [],
  38. arcAngle = end - start;
  39. if ((end > start) && (end - start > Math.PI / 2 + 0.0001)) {
  40. result = result.concat(
  41. curveTo(cx, cy, rx, ry, start, start + (Math.PI / 2), dx, dy)
  42. );
  43. result = result.concat(
  44. curveTo(cx, cy, rx, ry, start + (Math.PI / 2), end, dx, dy)
  45. );
  46. return result;
  47. }
  48. if ((end < start) && (start - end > Math.PI / 2 + 0.0001)) {
  49. result = result.concat(
  50. curveTo(cx, cy, rx, ry, start, start - (Math.PI / 2), dx, dy)
  51. );
  52. result = result.concat(
  53. curveTo(cx, cy, rx, ry, start - (Math.PI / 2), end, dx, dy)
  54. );
  55. return result;
  56. }
  57. return [
  58. 'C',
  59. cx + (rx * Math.cos(start)) -
  60. ((rx * dFactor * arcAngle) * Math.sin(start)) + dx,
  61. cy + (ry * Math.sin(start)) +
  62. ((ry * dFactor * arcAngle) * Math.cos(start)) + dy,
  63. cx + (rx * Math.cos(end)) +
  64. ((rx * dFactor * arcAngle) * Math.sin(end)) + dx,
  65. cy + (ry * Math.sin(end)) -
  66. ((ry * dFactor * arcAngle) * Math.cos(end)) + dy,
  67. cx + (rx * Math.cos(end)) + dx,
  68. cy + (ry * Math.sin(end)) + dy
  69. ];
  70. }
  71. SVGRenderer.prototype.toLinePath = function (points, closed) {
  72. var result = [];
  73. // Put "L x y" for each point
  74. points.forEach(function (point) {
  75. result.push('L', point.x, point.y);
  76. });
  77. if (points.length) {
  78. // Set the first element to M
  79. result[0] = 'M';
  80. // If it is a closed line, add Z
  81. if (closed) {
  82. result.push('Z');
  83. }
  84. }
  85. return result;
  86. };
  87. SVGRenderer.prototype.toLineSegments = function (points) {
  88. var result = [],
  89. m = true;
  90. points.forEach(function (point) {
  91. result.push(m ? 'M' : 'L', point.x, point.y);
  92. m = !m;
  93. });
  94. return result;
  95. };
  96. // A 3-D Face is defined by it's 3D vertexes, and is only visible if it's
  97. // vertexes are counter-clockwise (Back-face culling). It is used as a
  98. // polyhedron Element
  99. SVGRenderer.prototype.face3d = function (args) {
  100. var renderer = this,
  101. ret = this.createElement('path');
  102. ret.vertexes = [];
  103. ret.insidePlotArea = false;
  104. ret.enabled = true;
  105. ret.attr = function (hash) {
  106. if (
  107. typeof hash === 'object' &&
  108. (
  109. defined(hash.enabled) ||
  110. defined(hash.vertexes) ||
  111. defined(hash.insidePlotArea)
  112. )
  113. ) {
  114. this.enabled = pick(hash.enabled, this.enabled);
  115. this.vertexes = pick(hash.vertexes, this.vertexes);
  116. this.insidePlotArea = pick(
  117. hash.insidePlotArea,
  118. this.insidePlotArea
  119. );
  120. delete hash.enabled;
  121. delete hash.vertexes;
  122. delete hash.insidePlotArea;
  123. var chart = charts[renderer.chartIndex],
  124. vertexes2d = perspective(
  125. this.vertexes,
  126. chart,
  127. this.insidePlotArea
  128. ),
  129. path = renderer.toLinePath(vertexes2d, true),
  130. area = H.shapeArea(vertexes2d),
  131. visibility = (this.enabled && area > 0) ? 'visible' : 'hidden';
  132. hash.d = path;
  133. hash.visibility = visibility;
  134. }
  135. return SVGElement.prototype.attr.apply(this, arguments);
  136. };
  137. ret.animate = function (params) {
  138. if (
  139. typeof params === 'object' &&
  140. (
  141. defined(params.enabled) ||
  142. defined(params.vertexes) ||
  143. defined(params.insidePlotArea)
  144. )
  145. ) {
  146. this.enabled = pick(params.enabled, this.enabled);
  147. this.vertexes = pick(params.vertexes, this.vertexes);
  148. this.insidePlotArea = pick(
  149. params.insidePlotArea,
  150. this.insidePlotArea
  151. );
  152. delete params.enabled;
  153. delete params.vertexes;
  154. delete params.insidePlotArea;
  155. var chart = charts[renderer.chartIndex],
  156. vertexes2d = perspective(
  157. this.vertexes,
  158. chart,
  159. this.insidePlotArea
  160. ),
  161. path = renderer.toLinePath(vertexes2d, true),
  162. area = H.shapeArea(vertexes2d),
  163. visibility = (this.enabled && area > 0) ? 'visible' : 'hidden';
  164. params.d = path;
  165. this.attr('visibility', visibility);
  166. }
  167. return SVGElement.prototype.animate.apply(this, arguments);
  168. };
  169. return ret.attr(args);
  170. };
  171. // A Polyhedron is a handy way of defining a group of 3-D faces. It's only
  172. // attribute is `faces`, an array of attributes of each one of it's Face3D
  173. // instances.
  174. SVGRenderer.prototype.polyhedron = function (args) {
  175. var renderer = this,
  176. result = this.g(),
  177. destroy = result.destroy;
  178. if (!this.styledMode) {
  179. result.attr({
  180. 'stroke-linejoin': 'round'
  181. });
  182. }
  183. result.faces = [];
  184. // destroy all children
  185. result.destroy = function () {
  186. for (var i = 0; i < result.faces.length; i++) {
  187. result.faces[i].destroy();
  188. }
  189. return destroy.call(this);
  190. };
  191. result.attr = function (hash, val, complete, continueAnimation) {
  192. if (typeof hash === 'object' && defined(hash.faces)) {
  193. while (result.faces.length > hash.faces.length) {
  194. result.faces.pop().destroy();
  195. }
  196. while (result.faces.length < hash.faces.length) {
  197. result.faces.push(renderer.face3d().add(result));
  198. }
  199. for (var i = 0; i < hash.faces.length; i++) {
  200. if (renderer.styledMode) {
  201. delete hash.faces[i].fill;
  202. }
  203. result.faces[i].attr(
  204. hash.faces[i],
  205. null,
  206. complete,
  207. continueAnimation
  208. );
  209. }
  210. delete hash.faces;
  211. }
  212. return SVGElement.prototype.attr.apply(this, arguments);
  213. };
  214. result.animate = function (params, duration, complete) {
  215. if (params && params.faces) {
  216. while (result.faces.length > params.faces.length) {
  217. result.faces.pop().destroy();
  218. }
  219. while (result.faces.length < params.faces.length) {
  220. result.faces.push(renderer.face3d().add(result));
  221. }
  222. for (var i = 0; i < params.faces.length; i++) {
  223. result.faces[i].animate(params.faces[i], duration, complete);
  224. }
  225. delete params.faces;
  226. }
  227. return SVGElement.prototype.animate.apply(this, arguments);
  228. };
  229. return result.attr(args);
  230. };
  231. // Base, abstract prototype member for 3D elements
  232. element3dMethods = {
  233. // The init is used by base - renderer.Element
  234. initArgs: function (args) {
  235. var elem3d = this,
  236. renderer = elem3d.renderer,
  237. paths = renderer[elem3d.pathType + 'Path'](args),
  238. zIndexes = paths.zIndexes;
  239. // build parts
  240. elem3d.parts.forEach(function (part) {
  241. elem3d[part] = renderer.path(paths[part]).attr({
  242. 'class': 'highcharts-3d-' + part,
  243. zIndex: zIndexes[part] || 0
  244. }).add(elem3d);
  245. });
  246. elem3d.attr({
  247. 'stroke-linejoin': 'round',
  248. zIndex: zIndexes.group
  249. });
  250. // store original destroy
  251. elem3d.originalDestroy = elem3d.destroy;
  252. elem3d.destroy = elem3d.destroyParts;
  253. },
  254. // Single property setter that applies options to each part
  255. singleSetterForParts: function (
  256. prop, val, values, verb, duration, complete
  257. ) {
  258. var elem3d = this,
  259. newAttr = {},
  260. optionsToApply = [null, null, (verb || 'attr'), duration, complete],
  261. hasZIndexes = values && values.zIndexes;
  262. if (!values) {
  263. newAttr[prop] = val;
  264. optionsToApply[0] = newAttr;
  265. } else {
  266. H.objectEach(values, function (partVal, part) {
  267. newAttr[part] = {};
  268. newAttr[part][prop] = partVal;
  269. // include zIndexes if provided
  270. if (hasZIndexes) {
  271. newAttr[part].zIndex = values.zIndexes[part] || 0;
  272. }
  273. });
  274. optionsToApply[1] = newAttr;
  275. }
  276. return elem3d.processParts.apply(elem3d, optionsToApply);
  277. },
  278. // Calls function for each part. Used for attr, animate and destroy.
  279. processParts: function (props, partsProps, verb, duration, complete) {
  280. var elem3d = this;
  281. elem3d.parts.forEach(function (part) {
  282. // if different props for different parts
  283. if (partsProps) {
  284. props = H.pick(partsProps[part], false);
  285. }
  286. // only if something to set, but allow undefined
  287. if (props !== false) {
  288. elem3d[part][verb](props, duration, complete);
  289. }
  290. });
  291. return elem3d;
  292. },
  293. // Destroy all parts
  294. destroyParts: function () {
  295. this.processParts(null, null, 'destroy');
  296. return this.originalDestroy();
  297. }
  298. };
  299. // CUBOID
  300. cuboidMethods = H.merge(element3dMethods, {
  301. parts: ['front', 'top', 'side'],
  302. pathType: 'cuboid',
  303. attr: function (args, val, complete, continueAnimation) {
  304. // Resolve setting attributes by string name
  305. if (typeof args === 'string' && typeof val !== 'undefined') {
  306. var key = args;
  307. args = {};
  308. args[key] = val;
  309. }
  310. if (args.shapeArgs || defined(args.x)) {
  311. return this.singleSetterForParts(
  312. 'd',
  313. null,
  314. this.renderer[this.pathType + 'Path'](args.shapeArgs || args)
  315. );
  316. }
  317. return SVGElement.prototype.attr.call(
  318. this, args, undefined, complete, continueAnimation
  319. );
  320. },
  321. animate: function (args, duration, complete) {
  322. if (defined(args.x) && defined(args.y)) {
  323. var paths = this.renderer[this.pathType + 'Path'](args);
  324. this.singleSetterForParts(
  325. 'd', null, paths, 'animate', duration, complete
  326. );
  327. this.attr({
  328. zIndex: paths.zIndexes.group
  329. });
  330. } else if (args.opacity) {
  331. this.processParts(args, null, 'animate', duration, complete);
  332. } else {
  333. SVGElement.prototype.animate.call(this, args, duration, complete);
  334. }
  335. return this;
  336. },
  337. fillSetter: function (fill) {
  338. this.singleSetterForParts('fill', null, {
  339. front: fill,
  340. top: color(fill).brighten(0.1).get(),
  341. side: color(fill).brighten(-0.1).get()
  342. });
  343. // fill for animation getter (#6776)
  344. this.color = this.fill = fill;
  345. return this;
  346. },
  347. opacitySetter: function (opacity) {
  348. return this.singleSetterForParts('opacity', opacity);
  349. }
  350. });
  351. // set them up
  352. SVGRenderer.prototype.elements3d = {
  353. base: element3dMethods,
  354. cuboid: cuboidMethods
  355. };
  356. // return result, generalization
  357. SVGRenderer.prototype.element3d = function (type, shapeArgs) {
  358. // base
  359. var ret = this.g();
  360. // extend
  361. H.extend(ret, this.elements3d[type]);
  362. // init
  363. ret.initArgs(shapeArgs);
  364. // return
  365. return ret;
  366. };
  367. // generelized, so now use simply
  368. SVGRenderer.prototype.cuboid = function (shapeArgs) {
  369. return this.element3d('cuboid', shapeArgs);
  370. };
  371. // Generates a cuboid path and zIndexes
  372. H.SVGRenderer.prototype.cuboidPath = function (shapeArgs) {
  373. var x = shapeArgs.x,
  374. y = shapeArgs.y,
  375. z = shapeArgs.z,
  376. h = shapeArgs.height,
  377. w = shapeArgs.width,
  378. d = shapeArgs.depth,
  379. chart = charts[this.chartIndex],
  380. front,
  381. back,
  382. top,
  383. bottom,
  384. left,
  385. right,
  386. shape,
  387. path1,
  388. path2,
  389. path3,
  390. isFront,
  391. isTop,
  392. isRight,
  393. options3d = chart.options.chart.options3d,
  394. alpha = options3d.alpha,
  395. // Priority for x axis is the biggest,
  396. // because of x direction has biggest influence on zIndex
  397. incrementX = 10000,
  398. // y axis has the smallest priority in case of our charts
  399. // (needs to be set because of stacking)
  400. incrementY = 10,
  401. incrementZ = 100,
  402. zIndex = 0,
  403. // The 8 corners of the cube
  404. pArr = [{
  405. x: x,
  406. y: y,
  407. z: z
  408. }, {
  409. x: x + w,
  410. y: y,
  411. z: z
  412. }, {
  413. x: x + w,
  414. y: y + h,
  415. z: z
  416. }, {
  417. x: x,
  418. y: y + h,
  419. z: z
  420. }, {
  421. x: x,
  422. y: y + h,
  423. z: z + d
  424. }, {
  425. x: x + w,
  426. y: y + h,
  427. z: z + d
  428. }, {
  429. x: x + w,
  430. y: y,
  431. z: z + d
  432. }, {
  433. x: x,
  434. y: y,
  435. z: z + d
  436. }],
  437. pickShape;
  438. // apply perspective
  439. pArr = perspective(pArr, chart, shapeArgs.insidePlotArea);
  440. // helper method to decide which side is visible
  441. function mapPath(i) {
  442. return pArr[i];
  443. }
  444. /* *
  445. * First value - path with specific side
  446. * Second value - added information about side for later calculations.
  447. * Possible second values are 0 for path1, 1 for path2 and -1 for no path
  448. * chosen.
  449. */
  450. pickShape = function (path1, path2) {
  451. var ret = [
  452. [], -1
  453. ];
  454. path1 = path1.map(mapPath);
  455. path2 = path2.map(mapPath);
  456. if (H.shapeArea(path1) < 0) {
  457. ret = [path1, 0];
  458. } else if (H.shapeArea(path2) < 0) {
  459. ret = [path2, 1];
  460. }
  461. return ret;
  462. };
  463. // front or back
  464. front = [3, 2, 1, 0];
  465. back = [7, 6, 5, 4];
  466. shape = pickShape(front, back);
  467. path1 = shape[0];
  468. isFront = shape[1];
  469. // top or bottom
  470. top = [1, 6, 7, 0];
  471. bottom = [4, 5, 2, 3];
  472. shape = pickShape(top, bottom);
  473. path2 = shape[0];
  474. isTop = shape[1];
  475. // side
  476. right = [1, 2, 5, 6];
  477. left = [0, 7, 4, 3];
  478. shape = pickShape(right, left);
  479. path3 = shape[0];
  480. isRight = shape[1];
  481. /* New block used for calculating zIndex. It is basing on X, Y and Z
  482. position of specific columns. All zIndexes (for X, Y and Z values) are
  483. added to the final zIndex, where every value has different priority. The
  484. biggest priority is in X and Z directions, the lowest index is for
  485. stacked columns (Y direction and the same X and Z positions). Big
  486. differences between priorities is made because we need to ensure that
  487. even for big changes in Y and Z parameters all columns will be drawn
  488. correctly. */
  489. if (isRight === 1) {
  490. zIndex += incrementX * (1000 - x);
  491. } else if (!isRight) {
  492. zIndex += incrementX * x;
  493. }
  494. zIndex += incrementY * (
  495. !isTop ||
  496. // Numbers checked empirically
  497. (alpha >= 0 && alpha <= 180 || alpha < 360 && alpha > 357.5) ?
  498. chart.plotHeight - y : 10 + y
  499. );
  500. if (isFront === 1) {
  501. zIndex += incrementZ * (z);
  502. } else if (!isFront) {
  503. zIndex += incrementZ * (1000 - z);
  504. }
  505. return {
  506. front: this.toLinePath(path1, true),
  507. top: this.toLinePath(path2, true),
  508. side: this.toLinePath(path3, true),
  509. zIndexes: {
  510. group: Math.round(zIndex)
  511. },
  512. // additional info about zIndexes
  513. isFront: isFront,
  514. isTop: isTop
  515. }; // #4774
  516. };
  517. // SECTORS //
  518. H.SVGRenderer.prototype.arc3d = function (attribs) {
  519. var wrapper = this.g(),
  520. renderer = wrapper.renderer,
  521. customAttribs = ['x', 'y', 'r', 'innerR', 'start', 'end'];
  522. // Get custom attributes. Don't mutate the original object and return an
  523. // object with only custom attr.
  524. function suckOutCustom(params) {
  525. var hasCA = false,
  526. ca = {},
  527. key;
  528. params = merge(params); // Don't mutate the original object
  529. for (key in params) {
  530. if (customAttribs.indexOf(key) !== -1) {
  531. ca[key] = params[key];
  532. delete params[key];
  533. hasCA = true;
  534. }
  535. }
  536. return hasCA ? ca : false;
  537. }
  538. attribs = merge(attribs);
  539. attribs.alpha = (attribs.alpha || 0) * deg2rad;
  540. attribs.beta = (attribs.beta || 0) * deg2rad;
  541. // Create the different sub sections of the shape
  542. wrapper.top = renderer.path();
  543. wrapper.side1 = renderer.path();
  544. wrapper.side2 = renderer.path();
  545. wrapper.inn = renderer.path();
  546. wrapper.out = renderer.path();
  547. // Add all faces
  548. wrapper.onAdd = function () {
  549. var parent = wrapper.parentGroup,
  550. className = wrapper.attr('class');
  551. wrapper.top.add(wrapper);
  552. // These faces are added outside the wrapper group because the z index
  553. // relates to neighbour elements as well
  554. ['out', 'inn', 'side1', 'side2'].forEach(function (face) {
  555. wrapper[face]
  556. .attr({
  557. 'class': className + ' highcharts-3d-side'
  558. })
  559. .add(parent);
  560. });
  561. };
  562. // Cascade to faces
  563. ['addClass', 'removeClass'].forEach(function (fn) {
  564. wrapper[fn] = function () {
  565. var args = arguments;
  566. ['top', 'out', 'inn', 'side1', 'side2'].forEach(function (face) {
  567. wrapper[face][fn].apply(wrapper[face], args);
  568. });
  569. };
  570. });
  571. // Compute the transformed paths and set them to the composite shapes
  572. wrapper.setPaths = function (attribs) {
  573. var paths = wrapper.renderer.arc3dPath(attribs),
  574. zIndex = paths.zTop * 100;
  575. wrapper.attribs = attribs;
  576. wrapper.top.attr({ d: paths.top, zIndex: paths.zTop });
  577. wrapper.inn.attr({ d: paths.inn, zIndex: paths.zInn });
  578. wrapper.out.attr({ d: paths.out, zIndex: paths.zOut });
  579. wrapper.side1.attr({ d: paths.side1, zIndex: paths.zSide1 });
  580. wrapper.side2.attr({ d: paths.side2, zIndex: paths.zSide2 });
  581. // show all children
  582. wrapper.zIndex = zIndex;
  583. wrapper.attr({ zIndex: zIndex });
  584. // Set the radial gradient center the first time
  585. if (attribs.center) {
  586. wrapper.top.setRadialReference(attribs.center);
  587. delete attribs.center;
  588. }
  589. };
  590. wrapper.setPaths(attribs);
  591. // Apply the fill to the top and a darker shade to the sides
  592. wrapper.fillSetter = function (value) {
  593. var darker = color(value).brighten(-0.1).get();
  594. this.fill = value;
  595. this.side1.attr({ fill: darker });
  596. this.side2.attr({ fill: darker });
  597. this.inn.attr({ fill: darker });
  598. this.out.attr({ fill: darker });
  599. this.top.attr({ fill: value });
  600. return this;
  601. };
  602. // Apply the same value to all. These properties cascade down to the
  603. // children when set to the composite arc3d.
  604. ['opacity', 'translateX', 'translateY', 'visibility'].forEach(
  605. function (setter) {
  606. wrapper[setter + 'Setter'] = function (value, key) {
  607. wrapper[key] = value;
  608. ['out', 'inn', 'side1', 'side2', 'top'].forEach(function (el) {
  609. wrapper[el].attr(key, value);
  610. });
  611. };
  612. }
  613. );
  614. // Override attr to remove shape attributes and use those to set child paths
  615. wrapper.attr = function (params) {
  616. var ca;
  617. if (typeof params === 'object') {
  618. ca = suckOutCustom(params);
  619. if (ca) {
  620. extend(wrapper.attribs, ca);
  621. wrapper.setPaths(wrapper.attribs);
  622. }
  623. }
  624. return SVGElement.prototype.attr.apply(wrapper, arguments);
  625. };
  626. // Override the animate function by sucking out custom parameters related to
  627. // the shapes directly, and update the shapes from the animation step.
  628. wrapper.animate = function (params, animation, complete) {
  629. var ca,
  630. from = this.attribs,
  631. to,
  632. anim,
  633. randomProp = 'data-' + Math.random().toString(26).substring(2, 9);
  634. // Attribute-line properties connected to 3D. These shouldn't have been
  635. // in the attribs collection in the first place.
  636. delete params.center;
  637. delete params.z;
  638. delete params.depth;
  639. delete params.alpha;
  640. delete params.beta;
  641. anim = animObject(pick(animation, this.renderer.globalAnimation));
  642. if (anim.duration) {
  643. ca = suckOutCustom(params);
  644. // Params need to have a property in order for the step to run
  645. // (#5765, #7097, #7437)
  646. wrapper[randomProp] = 0;
  647. params[randomProp] = 1;
  648. wrapper[randomProp + 'Setter'] = H.noop;
  649. if (ca) {
  650. to = ca;
  651. anim.step = function (a, fx) {
  652. function interpolate(key) {
  653. return from[key] +
  654. (pick(to[key], from[key]) - from[key]) * fx.pos;
  655. }
  656. if (fx.prop === randomProp) {
  657. fx.elem.setPaths(merge(from, {
  658. x: interpolate('x'),
  659. y: interpolate('y'),
  660. r: interpolate('r'),
  661. innerR: interpolate('innerR'),
  662. start: interpolate('start'),
  663. end: interpolate('end')
  664. }));
  665. }
  666. };
  667. }
  668. animation = anim; // Only when duration (#5572)
  669. }
  670. return SVGElement.prototype.animate.call(
  671. this,
  672. params,
  673. animation,
  674. complete
  675. );
  676. };
  677. // destroy all children
  678. wrapper.destroy = function () {
  679. this.top.destroy();
  680. this.out.destroy();
  681. this.inn.destroy();
  682. this.side1.destroy();
  683. this.side2.destroy();
  684. SVGElement.prototype.destroy.call(this);
  685. };
  686. // hide all children
  687. wrapper.hide = function () {
  688. this.top.hide();
  689. this.out.hide();
  690. this.inn.hide();
  691. this.side1.hide();
  692. this.side2.hide();
  693. };
  694. wrapper.show = function (inherit) {
  695. this.top.show(inherit);
  696. this.out.show(inherit);
  697. this.inn.show(inherit);
  698. this.side1.show(inherit);
  699. this.side2.show(inherit);
  700. };
  701. return wrapper;
  702. };
  703. // Generate the paths required to draw a 3D arc
  704. SVGRenderer.prototype.arc3dPath = function (shapeArgs) {
  705. var cx = shapeArgs.x, // x coordinate of the center
  706. cy = shapeArgs.y, // y coordinate of the center
  707. start = shapeArgs.start, // start angle
  708. end = shapeArgs.end - 0.00001, // end angle
  709. r = shapeArgs.r, // radius
  710. ir = shapeArgs.innerR || 0, // inner radius
  711. d = shapeArgs.depth || 0, // depth
  712. alpha = shapeArgs.alpha, // alpha rotation of the chart
  713. beta = shapeArgs.beta; // beta rotation of the chart
  714. // Derived Variables
  715. var cs = Math.cos(start), // cosinus of the start angle
  716. ss = Math.sin(start), // sinus of the start angle
  717. ce = Math.cos(end), // cosinus of the end angle
  718. se = Math.sin(end), // sinus of the end angle
  719. rx = r * Math.cos(beta), // x-radius
  720. ry = r * Math.cos(alpha), // y-radius
  721. irx = ir * Math.cos(beta), // x-radius (inner)
  722. iry = ir * Math.cos(alpha), // y-radius (inner)
  723. dx = d * Math.sin(beta), // distance between top and bottom in x
  724. dy = d * Math.sin(alpha); // distance between top and bottom in y
  725. // TOP
  726. var top = ['M', cx + (rx * cs), cy + (ry * ss)];
  727. top = top.concat(curveTo(cx, cy, rx, ry, start, end, 0, 0));
  728. top = top.concat([
  729. 'L', cx + (irx * ce), cy + (iry * se)
  730. ]);
  731. top = top.concat(curveTo(cx, cy, irx, iry, end, start, 0, 0));
  732. top = top.concat(['Z']);
  733. // OUTSIDE
  734. var b = (beta > 0 ? Math.PI / 2 : 0),
  735. a = (alpha > 0 ? 0 : Math.PI / 2);
  736. var start2 = start > -b ? start : (end > -b ? -b : start),
  737. end2 = end < PI - a ? end : (start < PI - a ? PI - a : end),
  738. midEnd = 2 * PI - a;
  739. // When slice goes over bottom middle, need to add both, left and right
  740. // outer side. Additionally, when we cross right hand edge, create sharp
  741. // edge. Outer shape/wall:
  742. //
  743. // -------
  744. // / ^ \
  745. // 4) / / \ \ 1)
  746. // / / \ \
  747. // / / \ \
  748. // (c)=> ==== ==== <=(d)
  749. // \ \ / /
  750. // \ \<=(a)/ /
  751. // \ \ / / <=(b)
  752. // 3) \ v / 2)
  753. // -------
  754. //
  755. // (a) - inner side
  756. // (b) - outer side
  757. // (c) - left edge (sharp)
  758. // (d) - right edge (sharp)
  759. // 1..n - rendering order for startAngle = 0, when set to e.g 90, order
  760. // changes clockwise (1->2, 2->3, n->1) and counterclockwise for negative
  761. // startAngle
  762. var out = ['M', cx + (rx * cos(start2)), cy + (ry * sin(start2))];
  763. out = out.concat(curveTo(cx, cy, rx, ry, start2, end2, 0, 0));
  764. // When shape is wide, it can cross both, (c) and (d) edges, when using
  765. // startAngle
  766. if (end > midEnd && start < midEnd) {
  767. // Go to outer side
  768. out = out.concat([
  769. 'L', cx + (rx * cos(end2)) + dx, cy + (ry * sin(end2)) + dy
  770. ]);
  771. // Curve to the right edge of the slice (d)
  772. out = out.concat(curveTo(cx, cy, rx, ry, end2, midEnd, dx, dy));
  773. // Go to the inner side
  774. out = out.concat([
  775. 'L', cx + (rx * cos(midEnd)), cy + (ry * sin(midEnd))
  776. ]);
  777. // Curve to the true end of the slice
  778. out = out.concat(curveTo(cx, cy, rx, ry, midEnd, end, 0, 0));
  779. // Go to the outer side
  780. out = out.concat([
  781. 'L', cx + (rx * cos(end)) + dx, cy + (ry * sin(end)) + dy
  782. ]);
  783. // Go back to middle (d)
  784. out = out.concat(curveTo(cx, cy, rx, ry, end, midEnd, dx, dy));
  785. out = out.concat([
  786. 'L', cx + (rx * cos(midEnd)), cy + (ry * sin(midEnd))
  787. ]);
  788. // Go back to the left edge
  789. out = out.concat(curveTo(cx, cy, rx, ry, midEnd, end2, 0, 0));
  790. // But shape can cross also only (c) edge:
  791. } else if (end > PI - a && start < PI - a) {
  792. // Go to outer side
  793. out = out.concat([
  794. 'L',
  795. cx + (rx * Math.cos(end2)) + dx,
  796. cy + (ry * Math.sin(end2)) + dy
  797. ]);
  798. // Curve to the true end of the slice
  799. out = out.concat(curveTo(cx, cy, rx, ry, end2, end, dx, dy));
  800. // Go to the inner side
  801. out = out.concat([
  802. 'L', cx + (rx * Math.cos(end)), cy + (ry * Math.sin(end))
  803. ]);
  804. // Go back to the artifical end2
  805. out = out.concat(curveTo(cx, cy, rx, ry, end, end2, 0, 0));
  806. }
  807. out = out.concat([
  808. 'L', cx + (rx * Math.cos(end2)) + dx, cy + (ry * Math.sin(end2)) + dy
  809. ]);
  810. out = out.concat(curveTo(cx, cy, rx, ry, end2, start2, dx, dy));
  811. out = out.concat(['Z']);
  812. // INSIDE
  813. var inn = ['M', cx + (irx * cs), cy + (iry * ss)];
  814. inn = inn.concat(curveTo(cx, cy, irx, iry, start, end, 0, 0));
  815. inn = inn.concat([
  816. 'L', cx + (irx * Math.cos(end)) + dx, cy + (iry * Math.sin(end)) + dy
  817. ]);
  818. inn = inn.concat(curveTo(cx, cy, irx, iry, end, start, dx, dy));
  819. inn = inn.concat(['Z']);
  820. // SIDES
  821. var side1 = [
  822. 'M', cx + (rx * cs), cy + (ry * ss),
  823. 'L', cx + (rx * cs) + dx, cy + (ry * ss) + dy,
  824. 'L', cx + (irx * cs) + dx, cy + (iry * ss) + dy,
  825. 'L', cx + (irx * cs), cy + (iry * ss),
  826. 'Z'
  827. ];
  828. var side2 = [
  829. 'M', cx + (rx * ce), cy + (ry * se),
  830. 'L', cx + (rx * ce) + dx, cy + (ry * se) + dy,
  831. 'L', cx + (irx * ce) + dx, cy + (iry * se) + dy,
  832. 'L', cx + (irx * ce), cy + (iry * se),
  833. 'Z'
  834. ];
  835. // correction for changed position of vanishing point caused by alpha and
  836. // beta rotations
  837. var angleCorr = Math.atan2(dy, -dx),
  838. angleEnd = Math.abs(end + angleCorr),
  839. angleStart = Math.abs(start + angleCorr),
  840. angleMid = Math.abs((start + end) / 2 + angleCorr);
  841. // set to 0-PI range
  842. function toZeroPIRange(angle) {
  843. angle = angle % (2 * Math.PI);
  844. if (angle > Math.PI) {
  845. angle = 2 * Math.PI - angle;
  846. }
  847. return angle;
  848. }
  849. angleEnd = toZeroPIRange(angleEnd);
  850. angleStart = toZeroPIRange(angleStart);
  851. angleMid = toZeroPIRange(angleMid);
  852. // *1e5 is to compensate pInt in zIndexSetter
  853. var incPrecision = 1e5,
  854. a1 = angleMid * incPrecision,
  855. a2 = angleStart * incPrecision,
  856. a3 = angleEnd * incPrecision;
  857. return {
  858. top: top,
  859. // max angle is PI, so this is always higher
  860. zTop: Math.PI * incPrecision + 1,
  861. out: out,
  862. zOut: Math.max(a1, a2, a3),
  863. inn: inn,
  864. zInn: Math.max(a1, a2, a3),
  865. side1: side1,
  866. zSide1: a3 * 0.99, // to keep below zOut and zInn in case of same values
  867. side2: side2,
  868. zSide2: a2 * 0.99
  869. };
  870. };