cylinder.src.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * @license Highcharts JS v8.2.0 (2020-08-20)
  3. *
  4. * Highcharts cylinder module
  5. *
  6. * (c) 2010-2019 Kacper Madej
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/cylinder', ['highcharts', 'highcharts/highcharts-3d'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Series/CylinderSeries.js', [_modules['Core/Globals.js'], _modules['Core/Color.js'], _modules['Extensions/Math3D.js'], _modules['Core/Utilities.js']], function (H, Color, Math3D, U) {
  32. /* *
  33. *
  34. * Highcharts cylinder - a 3D series
  35. *
  36. * (c) 2010-2020 Highsoft AS
  37. *
  38. * Author: Kacper Madej
  39. *
  40. * License: www.highcharts.com/license
  41. *
  42. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  43. *
  44. * */
  45. var color = Color.parse;
  46. var perspective = Math3D.perspective;
  47. var merge = U.merge,
  48. pick = U.pick,
  49. seriesType = U.seriesType;
  50. var charts = H.charts,
  51. deg2rad = H.deg2rad,
  52. // Work on H.Renderer instead of SVGRenderer for VML support.
  53. RendererProto = H.Renderer.prototype,
  54. cuboidPath = RendererProto.cuboidPath,
  55. cylinderMethods;
  56. // Check if a path is simplified. The simplified path contains only lineTo
  57. // segments, whereas non-simplified contain curves.
  58. var isSimplified = function (path) {
  59. return !path.some(function (seg) { return seg[0] === 'C'; });
  60. };
  61. /**
  62. * The cylinder series type.
  63. *
  64. * @requires module:highcharts-3d
  65. * @requires module:modules/cylinder
  66. *
  67. * @private
  68. * @class
  69. * @name Highcharts.seriesTypes.cylinder
  70. *
  71. * @augments Highcharts.Series
  72. */
  73. seriesType('cylinder', 'column',
  74. /**
  75. * A cylinder graph is a variation of a 3d column graph. The cylinder graph
  76. * features cylindrical points.
  77. *
  78. * @sample {highcharts} highcharts/demo/cylinder/
  79. * Cylinder graph
  80. *
  81. * @extends plotOptions.column
  82. * @since 7.0.0
  83. * @product highcharts
  84. * @excluding allAreas, boostThreshold, colorAxis, compare, compareBase,
  85. * dragDrop, boostBlending
  86. * @requires modules/cylinder
  87. * @optionparent plotOptions.cylinder
  88. */
  89. {}, {},
  90. /** @lends Highcharts.seriesTypes.cylinder#pointClass# */
  91. {
  92. shapeType: 'cylinder',
  93. hasNewShapeType: H
  94. .seriesTypes.column.prototype
  95. .pointClass.prototype
  96. .hasNewShapeType
  97. });
  98. /**
  99. * A `cylinder` series. If the [type](#series.cylinder.type) option is not
  100. * specified, it is inherited from [chart.type](#chart.type).
  101. *
  102. * @extends series,plotOptions.cylinder
  103. * @since 7.0.0
  104. * @product highcharts
  105. * @excluding allAreas, boostThreshold, colorAxis, compare, compareBase,
  106. * boostBlending
  107. * @requires modules/cylinder
  108. * @apioption series.cylinder
  109. */
  110. /**
  111. * An array of data points for the series. For the `cylinder` series type,
  112. * points can be given in the following ways:
  113. *
  114. * 1. An array of numerical values. In this case, the numerical values will be
  115. * interpreted as `y` options. The `x` values will be automatically
  116. * calculated, either starting at 0 and incremented by 1, or from
  117. * `pointStart` and `pointInterval` given in the series options. If the axis
  118. * has categories, these will be used. Example:
  119. * ```js
  120. * data: [0, 5, 3, 5]
  121. * ```
  122. *
  123. * 2. An array of arrays with 2 values. In this case, the values correspond to
  124. * `x,y`. If the first value is a string, it is applied as the name of the
  125. * point, and the `x` value is inferred.
  126. * ```js
  127. * data: [
  128. * [0, 0],
  129. * [1, 8],
  130. * [2, 9]
  131. * ]
  132. * ```
  133. *
  134. * 3. An array of objects with named values. The following snippet shows only a
  135. * few settings, see the complete options set below. If the total number of
  136. * data points exceeds the series'
  137. * [turboThreshold](#series.cylinder.turboThreshold), this option is not
  138. * available.
  139. *
  140. * ```js
  141. * data: [{
  142. * x: 1,
  143. * y: 2,
  144. * name: "Point2",
  145. * color: "#00FF00"
  146. * }, {
  147. * x: 1,
  148. * y: 4,
  149. * name: "Point1",
  150. * color: "#FF00FF"
  151. * }]
  152. * ```
  153. *
  154. * @sample {highcharts} highcharts/chart/reflow-true/
  155. * Numerical values
  156. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  157. * Arrays of numeric x and y
  158. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  159. * Arrays of datetime x and y
  160. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  161. * Arrays of point.name and y
  162. * @sample {highcharts} highcharts/series/data-array-of-objects/
  163. * Config objects
  164. *
  165. * @type {Array<number|Array<(number|string),(number|null)>|null|*>}
  166. * @extends series.column.data
  167. * @product highcharts highstock
  168. * @apioption series.cylinder.data
  169. */
  170. // cylinder extends cuboid
  171. cylinderMethods = merge(RendererProto.elements3d.cuboid, {
  172. parts: ['top', 'bottom', 'front', 'back'],
  173. pathType: 'cylinder',
  174. fillSetter: function (fill) {
  175. this.singleSetterForParts('fill', null, {
  176. front: fill,
  177. back: fill,
  178. top: color(fill).brighten(0.1).get(),
  179. bottom: color(fill).brighten(-0.1).get()
  180. });
  181. // fill for animation getter (#6776)
  182. this.color = this.fill = fill;
  183. return this;
  184. }
  185. });
  186. RendererProto.elements3d.cylinder = cylinderMethods;
  187. RendererProto.cylinder = function (shapeArgs) {
  188. return this.element3d('cylinder', shapeArgs);
  189. };
  190. // Generates paths and zIndexes.
  191. RendererProto.cylinderPath = function (shapeArgs) {
  192. var renderer = this,
  193. chart = charts[renderer.chartIndex],
  194. // decide zIndexes of parts based on cubiod logic, for consistency.
  195. cuboidData = cuboidPath.call(renderer,
  196. shapeArgs),
  197. isTopFirst = !cuboidData.isTop,
  198. isFronFirst = !cuboidData.isFront,
  199. top = renderer.getCylinderEnd(chart,
  200. shapeArgs),
  201. bottom = renderer.getCylinderEnd(chart,
  202. shapeArgs,
  203. true);
  204. return {
  205. front: renderer.getCylinderFront(top, bottom),
  206. back: renderer.getCylinderBack(top, bottom),
  207. top: top,
  208. bottom: bottom,
  209. zIndexes: {
  210. top: isTopFirst ? 3 : 0,
  211. bottom: isTopFirst ? 0 : 3,
  212. front: isFronFirst ? 2 : 1,
  213. back: isFronFirst ? 1 : 2,
  214. group: cuboidData.zIndexes.group
  215. }
  216. };
  217. };
  218. // Returns cylinder Front path
  219. RendererProto.getCylinderFront = function (topPath, bottomPath) {
  220. var path = topPath.slice(0, 3);
  221. if (isSimplified(bottomPath)) {
  222. var move = bottomPath[0];
  223. if (move[0] === 'M') {
  224. path.push(bottomPath[2]);
  225. path.push(bottomPath[1]);
  226. path.push(['L', move[1], move[2]]);
  227. }
  228. }
  229. else {
  230. var move = bottomPath[0],
  231. curve1 = bottomPath[1],
  232. curve2 = bottomPath[2];
  233. if (move[0] === 'M' && curve1[0] === 'C' && curve2[0] === 'C') {
  234. path.push(['L', curve2[5], curve2[6]]);
  235. path.push(['C', curve2[3], curve2[4], curve2[1], curve2[2], curve1[5], curve1[6]]);
  236. path.push(['C', curve1[3], curve1[4], curve1[1], curve1[2], move[1], move[2]]);
  237. }
  238. }
  239. path.push(['Z']);
  240. return path;
  241. };
  242. // Returns cylinder Back path
  243. RendererProto.getCylinderBack = function (topPath, bottomPath) {
  244. var path = [];
  245. if (isSimplified(topPath)) {
  246. var move = topPath[0],
  247. line2 = topPath[2];
  248. if (move[0] === 'M' && line2[0] === 'L') {
  249. path.push(['M', line2[1], line2[2]]);
  250. path.push(topPath[3]);
  251. // End at start
  252. path.push(['L', move[1], move[2]]);
  253. }
  254. }
  255. else {
  256. if (topPath[2][0] === 'C') {
  257. path.push(['M', topPath[2][5], topPath[2][6]]);
  258. }
  259. path.push(topPath[3], topPath[4]);
  260. }
  261. if (isSimplified(bottomPath)) {
  262. var move = bottomPath[0];
  263. if (move[0] === 'M') {
  264. path.push(['L', move[1], move[2]]);
  265. path.push(bottomPath[3]);
  266. path.push(bottomPath[2]);
  267. }
  268. }
  269. else {
  270. var curve2 = bottomPath[2],
  271. curve3 = bottomPath[3],
  272. curve4 = bottomPath[4];
  273. if (curve2[0] === 'C' && curve3[0] === 'C' && curve4[0] === 'C') {
  274. path.push(['L', curve4[5], curve4[6]]);
  275. path.push(['C', curve4[3], curve4[4], curve4[1], curve4[2], curve3[5], curve3[6]]);
  276. path.push(['C', curve3[3], curve3[4], curve3[1], curve3[2], curve2[5], curve2[6]]);
  277. }
  278. }
  279. path.push(['Z']);
  280. return path;
  281. };
  282. // Retruns cylinder path for top or bottom
  283. RendererProto.getCylinderEnd = function (chart, shapeArgs, isBottom) {
  284. // A half of the smaller one out of width or depth (optional, because
  285. // there's no depth for a funnel that reuses the code)
  286. var depth = pick(shapeArgs.depth,
  287. shapeArgs.width),
  288. radius = Math.min(shapeArgs.width,
  289. depth) / 2,
  290. // Approximated longest diameter
  291. angleOffset = deg2rad * (chart.options.chart.options3d.beta - 90 +
  292. (shapeArgs.alphaCorrection || 0)),
  293. // Could be top or bottom of the cylinder
  294. y = shapeArgs.y + (isBottom ? shapeArgs.height : 0),
  295. // Use cubic Bezier curve to draw a cricle in x,z (y is constant).
  296. // More math. at spencermortensen.com/articles/bezier-circle/
  297. c = 0.5519 * radius,
  298. centerX = shapeArgs.width / 2 + shapeArgs.x,
  299. centerZ = depth / 2 + shapeArgs.z,
  300. // points could be generated in a loop, but readability will plummet
  301. points = [{
  302. x: 0,
  303. y: y,
  304. z: radius
  305. }, {
  306. x: c,
  307. y: y,
  308. z: radius
  309. }, {
  310. x: radius,
  311. y: y,
  312. z: c
  313. }, {
  314. x: radius,
  315. y: y,
  316. z: 0
  317. }, {
  318. x: radius,
  319. y: y,
  320. z: -c
  321. }, {
  322. x: c,
  323. y: y,
  324. z: -radius
  325. }, {
  326. x: 0,
  327. y: y,
  328. z: -radius
  329. }, {
  330. x: -c,
  331. y: y,
  332. z: -radius
  333. }, {
  334. x: -radius,
  335. y: y,
  336. z: -c
  337. }, {
  338. x: -radius,
  339. y: y,
  340. z: 0
  341. }, {
  342. x: -radius,
  343. y: y,
  344. z: c
  345. }, {
  346. x: -c,
  347. y: y,
  348. z: radius
  349. }, {
  350. x: 0,
  351. y: y,
  352. z: radius
  353. }],
  354. cosTheta = Math.cos(angleOffset),
  355. sinTheta = Math.sin(angleOffset),
  356. perspectivePoints,
  357. path,
  358. x,
  359. z;
  360. // rotete to match chart's beta and translate to the shape center
  361. points.forEach(function (point, i) {
  362. x = point.x;
  363. z = point.z;
  364. // x′ = (x * cosθ − z * sinθ) + centerX
  365. // z′ = (z * cosθ + x * sinθ) + centerZ
  366. points[i].x = (x * cosTheta - z * sinTheta) + centerX;
  367. points[i].z = (z * cosTheta + x * sinTheta) + centerZ;
  368. });
  369. perspectivePoints = perspective(points, chart, true);
  370. // check for sub-pixel curve issue, compare front and back edges
  371. if (Math.abs(perspectivePoints[3].y - perspectivePoints[9].y) < 2.5 &&
  372. Math.abs(perspectivePoints[0].y - perspectivePoints[6].y) < 2.5) {
  373. // use simplied shape
  374. path = this.toLinePath([
  375. perspectivePoints[0],
  376. perspectivePoints[3],
  377. perspectivePoints[6],
  378. perspectivePoints[9]
  379. ], true);
  380. }
  381. else {
  382. // or default curved path to imitate ellipse (2D circle)
  383. path = this.getCurvedPath(perspectivePoints);
  384. }
  385. return path;
  386. };
  387. // Returns curved path in format of:
  388. // [ M, x, y, ...[C, cp1x, cp2y, cp2x, cp2y, epx, epy]*n_times ]
  389. // (cp - control point, ep - end point)
  390. RendererProto.getCurvedPath = function (points) {
  391. var path = [['M',
  392. points[0].x,
  393. points[0].y]],
  394. limit = points.length - 2,
  395. i;
  396. for (i = 1; i < limit; i += 3) {
  397. path.push([
  398. 'C',
  399. points[i].x, points[i].y,
  400. points[i + 1].x, points[i + 1].y,
  401. points[i + 2].x, points[i + 2].y
  402. ]);
  403. }
  404. return path;
  405. };
  406. });
  407. _registerModule(_modules, 'masters/modules/cylinder.src.js', [], function () {
  408. });
  409. }));