CylinderSeries.js 11 KB

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