cylinder.src.js 11 KB

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