cylinder.src.js 15 KB

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