cylinder.src.js 16 KB

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