cylinder.src.js 12 KB

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