Column3DSeries.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. import BaseSeries from '../Core/Series/Series.js';
  11. import H from '../Core/Globals.js';
  12. import Math3D from '../Extensions/Math3D.js';
  13. var perspective = Math3D.perspective;
  14. import StackItem from '../Extensions/Stacking.js';
  15. import U from '../Core/Utilities.js';
  16. var addEvent = U.addEvent, pick = U.pick, wrap = U.wrap;
  17. import './ColumnSeries.js';
  18. import '../Series/LineSeries.js';
  19. var Series = H.Series, columnProto = BaseSeries.seriesTypes.column.prototype, svg = H.svg;
  20. /**
  21. * Depth of the columns in a 3D column chart.
  22. *
  23. * @type {number}
  24. * @default 25
  25. * @since 4.0
  26. * @product highcharts
  27. * @requires highcharts-3d
  28. * @apioption plotOptions.column.depth
  29. */
  30. /**
  31. * 3D columns only. The color of the edges. Similar to `borderColor`, except it
  32. * defaults to the same color as the column.
  33. *
  34. * @type {Highcharts.ColorString}
  35. * @product highcharts
  36. * @requires highcharts-3d
  37. * @apioption plotOptions.column.edgeColor
  38. */
  39. /**
  40. * 3D columns only. The width of the colored edges.
  41. *
  42. * @type {number}
  43. * @default 1
  44. * @product highcharts
  45. * @requires highcharts-3d
  46. * @apioption plotOptions.column.edgeWidth
  47. */
  48. /**
  49. * The spacing between columns on the Z Axis in a 3D chart.
  50. *
  51. * @type {number}
  52. * @default 1
  53. * @since 4.0
  54. * @product highcharts
  55. * @requires highcharts-3d
  56. * @apioption plotOptions.column.groupZPadding
  57. */
  58. /* eslint-disable no-invalid-this */
  59. /**
  60. * @private
  61. * @param {Highcharts.Chart} chart
  62. * Chart with stacks
  63. * @param {string} stacking
  64. * Stacking option
  65. * @return {Highcharts.Stack3dDictionary}
  66. */
  67. function retrieveStacks(chart, stacking) {
  68. var series = chart.series, stacks = {};
  69. var stackNumber, i = 1;
  70. series.forEach(function (s) {
  71. stackNumber = pick(s.options.stack, (stacking ? 0 : series.length - 1 - s.index)); // #3841, #4532
  72. if (!stacks[stackNumber]) {
  73. stacks[stackNumber] = { series: [s], position: i };
  74. i++;
  75. }
  76. else {
  77. stacks[stackNumber].series.push(s);
  78. }
  79. });
  80. stacks.totalStacks = i + 1;
  81. return stacks;
  82. }
  83. wrap(columnProto, 'translate', function (proceed) {
  84. proceed.apply(this, [].slice.call(arguments, 1));
  85. // Do not do this if the chart is not 3D
  86. if (this.chart.is3d()) {
  87. this.translate3dShapes();
  88. }
  89. });
  90. // Don't use justifyDataLabel when point is outsidePlot
  91. wrap(Series.prototype, 'justifyDataLabel', function (proceed) {
  92. return !(arguments[2].outside3dPlot) ?
  93. proceed.apply(this, [].slice.call(arguments, 1)) :
  94. false;
  95. });
  96. columnProto.translate3dPoints = function () { };
  97. columnProto.translate3dShapes = function () {
  98. var series = this, chart = series.chart, seriesOptions = series.options, depth = seriesOptions.depth, stack = seriesOptions.stacking ?
  99. (seriesOptions.stack || 0) :
  100. series.index, // #4743
  101. z = stack * (depth + (seriesOptions.groupZPadding || 1)), borderCrisp = series.borderWidth % 2 ? 0.5 : 0, point2dPos; // Position of point in 2D, used for 3D position calculation.
  102. if (chart.inverted && !series.yAxis.reversed) {
  103. borderCrisp *= -1;
  104. }
  105. if (seriesOptions.grouping !== false) {
  106. z = 0;
  107. }
  108. z += (seriesOptions.groupZPadding || 1);
  109. series.data.forEach(function (point) {
  110. // #7103 Reset outside3dPlot flag
  111. point.outside3dPlot = null;
  112. if (point.y !== null) {
  113. var shapeArgs = point.shapeArgs, tooltipPos = point.tooltipPos,
  114. // Array for final shapeArgs calculation.
  115. // We are checking two dimensions (x and y).
  116. dimensions = [['x', 'width'], ['y', 'height']], borderlessBase; // Crisped rects can have +/- 0.5 pixels offset.
  117. // #3131 We need to check if column is inside plotArea.
  118. dimensions.forEach(function (d) {
  119. borderlessBase = shapeArgs[d[0]] - borderCrisp;
  120. if (borderlessBase < 0) {
  121. // If borderLessBase is smaller than 0, it is needed to set
  122. // its value to 0 or 0.5 depending on borderWidth
  123. // borderWidth may be even or odd.
  124. shapeArgs[d[1]] +=
  125. shapeArgs[d[0]] + borderCrisp;
  126. shapeArgs[d[0]] = -borderCrisp;
  127. borderlessBase = 0;
  128. }
  129. if ((borderlessBase + shapeArgs[d[1]] >
  130. series[d[0] + 'Axis'].len) &&
  131. // Do not change height/width of column if 0 (#6708)
  132. shapeArgs[d[1]] !== 0) {
  133. shapeArgs[d[1]] =
  134. series[d[0] + 'Axis'].len -
  135. shapeArgs[d[0]];
  136. }
  137. if (
  138. // Do not remove columns with zero height/width.
  139. (shapeArgs[d[1]] !== 0) &&
  140. (shapeArgs[d[0]] >=
  141. series[d[0] + 'Axis'].len ||
  142. shapeArgs[d[0]] + shapeArgs[d[1]] <=
  143. borderCrisp)) {
  144. // Set args to 0 if column is outside the chart.
  145. for (var key in shapeArgs) { // eslint-disable-line guard-for-in
  146. shapeArgs[key] = 0;
  147. }
  148. // #7103 outside3dPlot flag is set on Points which are
  149. // currently outside of plot.
  150. point.outside3dPlot = true;
  151. }
  152. });
  153. // Change from 2d to 3d
  154. if (point.shapeType === 'rect') {
  155. point.shapeType = 'cuboid';
  156. }
  157. shapeArgs.z = z;
  158. shapeArgs.depth = depth;
  159. shapeArgs.insidePlotArea = true;
  160. // Point's position in 2D
  161. point2dPos = {
  162. x: shapeArgs.x + shapeArgs.width / 2,
  163. y: shapeArgs.y,
  164. z: z + depth / 2 // The center of column in Z dimension
  165. };
  166. // Recalculate point positions for inverted graphs
  167. if (chart.inverted) {
  168. point2dPos.x = shapeArgs.height;
  169. point2dPos.y = point.clientX;
  170. }
  171. // Calculate and store point's position in 3D,
  172. // using perspective method.
  173. point.plot3d = perspective([point2dPos], chart, true, false)[0];
  174. // Translate the tooltip position in 3d space
  175. tooltipPos = perspective([{
  176. x: tooltipPos[0],
  177. y: tooltipPos[1],
  178. z: z + depth / 2 // The center of column in Z dimension
  179. }], chart, true, false)[0];
  180. point.tooltipPos = [tooltipPos.x, tooltipPos.y];
  181. }
  182. });
  183. // store for later use #4067
  184. series.z = z;
  185. };
  186. wrap(columnProto, 'animate', function (proceed) {
  187. if (!this.chart.is3d()) {
  188. proceed.apply(this, [].slice.call(arguments, 1));
  189. }
  190. else {
  191. var args = arguments, init = args[1], yAxis = this.yAxis, series = this, reversed = this.yAxis.reversed;
  192. if (svg) { // VML is too slow anyway
  193. if (init) {
  194. series.data.forEach(function (point) {
  195. if (point.y !== null) {
  196. point.height = point.shapeArgs.height;
  197. point.shapey = point.shapeArgs.y; // #2968
  198. point.shapeArgs.height = 1;
  199. if (!reversed) {
  200. if (point.stackY) {
  201. point.shapeArgs.y =
  202. point.plotY +
  203. yAxis.translate(point.stackY);
  204. }
  205. else {
  206. point.shapeArgs.y =
  207. point.plotY +
  208. (point.negative ?
  209. -point.height :
  210. point.height);
  211. }
  212. }
  213. }
  214. });
  215. }
  216. else { // run the animation
  217. series.data.forEach(function (point) {
  218. if (point.y !== null) {
  219. point.shapeArgs.height = point.height;
  220. point.shapeArgs.y = point.shapey; // #2968
  221. // null value do not have a graphic
  222. if (point.graphic) {
  223. point.graphic.animate(point.shapeArgs, series.options.animation);
  224. }
  225. }
  226. });
  227. // redraw datalabels to the correct position
  228. this.drawDataLabels();
  229. }
  230. }
  231. }
  232. });
  233. // In case of 3d columns there is no sense to add this columns to a specific
  234. // series group - if series is added to a group all columns will have the same
  235. // zIndex in comparison with different series.
  236. wrap(columnProto, 'plotGroup', function (proceed, prop, name, visibility, zIndex, parent) {
  237. if (prop !== 'dataLabelsGroup') {
  238. if (this.chart.is3d()) {
  239. if (this[prop]) {
  240. delete this[prop];
  241. }
  242. if (parent) {
  243. if (!this.chart.columnGroup) {
  244. this.chart.columnGroup =
  245. this.chart.renderer.g('columnGroup').add(parent);
  246. }
  247. this[prop] = this.chart.columnGroup;
  248. this.chart.columnGroup.attr(this.getPlotBox());
  249. this[prop].survive = true;
  250. if (prop === 'group' || prop === 'markerGroup') {
  251. arguments[3] = 'visible';
  252. // For 3D column group and markerGroup should be visible
  253. }
  254. }
  255. }
  256. }
  257. return proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  258. });
  259. // When series is not added to group it is needed to change setVisible method to
  260. // allow correct Legend funcionality. This wrap is basing on pie chart series.
  261. wrap(columnProto, 'setVisible', function (proceed, vis) {
  262. var series = this, pointVis;
  263. if (series.chart.is3d()) {
  264. series.data.forEach(function (point) {
  265. point.visible = point.options.visible = vis =
  266. typeof vis === 'undefined' ?
  267. !pick(series.visible, point.visible) : vis;
  268. pointVis = vis ? 'visible' : 'hidden';
  269. series.options.data[series.data.indexOf(point)] =
  270. point.options;
  271. if (point.graphic) {
  272. point.graphic.attr({
  273. visibility: pointVis
  274. });
  275. }
  276. });
  277. }
  278. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  279. });
  280. columnProto.handle3dGrouping = true;
  281. addEvent(Series, 'afterInit', function () {
  282. if (this.chart.is3d() &&
  283. this.handle3dGrouping) {
  284. var series = this, seriesOptions = this.options, grouping = seriesOptions.grouping, stacking = seriesOptions.stacking, reversedStacks = pick(this.yAxis.options.reversedStacks, true), z = 0;
  285. // @todo grouping === true ?
  286. if (!(typeof grouping !== 'undefined' && !grouping)) {
  287. var stacks = retrieveStacks(this.chart, stacking), stack = seriesOptions.stack || 0, i; // position within the stack
  288. for (i = 0; i < stacks[stack].series.length; i++) {
  289. if (stacks[stack].series[i] === this) {
  290. break;
  291. }
  292. }
  293. z = (10 * (stacks.totalStacks - stacks[stack].position)) +
  294. (reversedStacks ? i : -i); // #4369
  295. // In case when axis is reversed, columns are also reversed inside
  296. // the group (#3737)
  297. if (!this.xAxis.reversed) {
  298. z = (stacks.totalStacks * 10) - z;
  299. }
  300. }
  301. seriesOptions.depth = seriesOptions.depth || 25;
  302. series.z = series.z || 0;
  303. seriesOptions.zIndex = z;
  304. }
  305. });
  306. // eslint-disable-next-line valid-jsdoc
  307. /**
  308. * @private
  309. */
  310. function pointAttribs(proceed) {
  311. var attr = proceed.apply(this, [].slice.call(arguments, 1));
  312. if (this.chart.is3d && this.chart.is3d()) {
  313. // Set the fill color to the fill color to provide a smooth edge
  314. attr.stroke = this.options.edgeColor || attr.fill;
  315. attr['stroke-width'] = pick(this.options.edgeWidth, 1); // #4055
  316. }
  317. return attr;
  318. }
  319. // eslint-disable-next-line valid-jsdoc
  320. /**
  321. * In 3D mode, all column-series are rendered in one main group. Because of that
  322. * we need to apply inactive state on all points.
  323. * @private
  324. */
  325. function setState(proceed, state, inherit) {
  326. var is3d = this.chart.is3d && this.chart.is3d();
  327. if (is3d) {
  328. this.options.inactiveOtherPoints = true;
  329. }
  330. proceed.call(this, state, inherit);
  331. if (is3d) {
  332. this.options.inactiveOtherPoints = false;
  333. }
  334. }
  335. // eslint-disable-next-line valid-jsdoc
  336. /**
  337. * In 3D mode, simple checking for a new shape to animate is not enough.
  338. * Additionally check if graphic is a group of elements
  339. * @private
  340. */
  341. function hasNewShapeType(proceed) {
  342. var args = [];
  343. for (var _i = 1; _i < arguments.length; _i++) {
  344. args[_i - 1] = arguments[_i];
  345. }
  346. return this.series.chart.is3d() ?
  347. this.graphic && this.graphic.element.nodeName !== 'g' :
  348. proceed.apply(this, args);
  349. }
  350. wrap(columnProto, 'pointAttribs', pointAttribs);
  351. wrap(columnProto, 'setState', setState);
  352. wrap(columnProto.pointClass.prototype, 'hasNewShapeType', hasNewShapeType);
  353. if (BaseSeries.seriesTypes.columnRange) {
  354. var columnRangeProto = BaseSeries.seriesTypes.columnrange.prototype;
  355. wrap(columnRangeProto, 'pointAttribs', pointAttribs);
  356. wrap(columnRangeProto, 'setState', setState);
  357. wrap(columnRangeProto.pointClass.prototype, 'hasNewShapeType', hasNewShapeType);
  358. columnRangeProto.plotGroup = columnProto.plotGroup;
  359. columnRangeProto.setVisible = columnProto.setVisible;
  360. }
  361. wrap(Series.prototype, 'alignDataLabel', function (proceed, point, dataLabel, options, alignTo) {
  362. var chart = this.chart;
  363. // In 3D we need to pass point.outsidePlot option to the justifyDataLabel
  364. // method for disabling justifying dataLabels in columns outside plot
  365. options.outside3dPlot = point.outside3dPlot;
  366. // Only do this for 3D columns and it's derived series
  367. if (chart.is3d() &&
  368. this.is('column')) {
  369. var series = this, seriesOptions = series.options, inside = pick(options.inside, !!series.options.stacking), options3d = chart.options.chart.options3d, xOffset = point.pointWidth / 2 || 0;
  370. var dLPosition = {
  371. x: alignTo.x + xOffset,
  372. y: alignTo.y,
  373. z: series.z + seriesOptions.depth / 2
  374. };
  375. if (chart.inverted) {
  376. // Inside dataLabels are positioned according to above
  377. // logic and there is no need to position them using
  378. // non-3D algorighm (that use alignTo.width)
  379. if (inside) {
  380. alignTo.width = 0;
  381. dLPosition.x += point.shapeArgs.height / 2;
  382. }
  383. // When chart is upside down
  384. // (alpha angle between 180 and 360 degrees)
  385. // it is needed to add column width to calculated value.
  386. if (options3d.alpha >= 90 && options3d.alpha <= 270) {
  387. dLPosition.y += point.shapeArgs.width;
  388. }
  389. }
  390. // dLPosition is recalculated for 3D graphs
  391. dLPosition = perspective([dLPosition], chart, true, false)[0];
  392. alignTo.x = dLPosition.x - xOffset;
  393. // #7103 If point is outside of plotArea, hide data label.
  394. alignTo.y = point.outside3dPlot ? -9e9 : dLPosition.y;
  395. }
  396. proceed.apply(this, [].slice.call(arguments, 1));
  397. });
  398. // Added stackLabels position calculation for 3D charts.
  399. wrap(StackItem.prototype, 'getStackBox', function (proceed, chart, stackItem, x, y, xWidth, h, axis) {
  400. var stackBox = proceed.apply(this, [].slice.call(arguments, 1));
  401. // Only do this for 3D graph
  402. if (chart.is3d() && stackItem.base) {
  403. // First element of stackItem.base is an index of base series.
  404. var baseSeriesInd = +(stackItem.base).split(',')[0];
  405. var columnSeries = chart.series[baseSeriesInd];
  406. var options3d = chart.options.chart.options3d;
  407. // Only do this if base series is a column or inherited type,
  408. // use its barW, z and depth parameters
  409. // for correct stackLabels position calculation
  410. if (columnSeries &&
  411. columnSeries instanceof BaseSeries.seriesTypes.column) {
  412. var dLPosition = {
  413. x: stackBox.x + (chart.inverted ? h : xWidth / 2),
  414. y: stackBox.y,
  415. z: columnSeries.options.depth / 2
  416. };
  417. if (chart.inverted) {
  418. // Do not use default offset calculation logic
  419. // for 3D inverted stackLabels.
  420. stackBox.width = 0;
  421. // When chart is upside down
  422. // (alpha angle between 180 and 360 degrees)
  423. // it is needed to add column width to calculated value.
  424. if (options3d.alpha >= 90 && options3d.alpha <= 270) {
  425. dLPosition.y += xWidth;
  426. }
  427. }
  428. dLPosition = perspective([dLPosition], chart, true, false)[0];
  429. stackBox.x = dLPosition.x - xWidth / 2;
  430. stackBox.y = dLPosition.y;
  431. }
  432. }
  433. return stackBox;
  434. });
  435. /*
  436. @merge v6.2
  437. @todo
  438. EXTENSION FOR 3D CYLINDRICAL COLUMNS
  439. Not supported
  440. */
  441. /*
  442. var defaultOptions = H.getOptions();
  443. defaultOptions.plotOptions.cylinder =
  444. merge(defaultOptions.plotOptions.column);
  445. var CylinderSeries = extendClass(seriesTypes.column, {
  446. type: 'cylinder'
  447. });
  448. seriesTypes.cylinder = CylinderSeries;
  449. wrap(seriesTypes.cylinder.prototype, 'translate', function (proceed) {
  450. proceed.apply(this, [].slice.call(arguments, 1));
  451. // Do not do this if the chart is not 3D
  452. if (!this.chart.is3d()) {
  453. return;
  454. }
  455. var series = this,
  456. chart = series.chart,
  457. options = chart.options,
  458. cylOptions = options.plotOptions.cylinder,
  459. options3d = options.chart.options3d,
  460. depth = cylOptions.depth || 0,
  461. alpha = chart.alpha3d;
  462. var z = cylOptions.stacking ?
  463. (this.options.stack || 0) * depth :
  464. series._i * depth;
  465. z += depth / 2;
  466. if (cylOptions.grouping !== false) { z = 0; }
  467. each(series.data, function (point) {
  468. var shapeArgs = point.shapeArgs,
  469. deg2rad = H.deg2rad;
  470. point.shapeType = 'arc3d';
  471. shapeArgs.x += depth / 2;
  472. shapeArgs.z = z;
  473. shapeArgs.start = 0;
  474. shapeArgs.end = 2 * PI;
  475. shapeArgs.r = depth * 0.95;
  476. shapeArgs.innerR = 0;
  477. shapeArgs.depth =
  478. shapeArgs.height * (1 / sin((90 - alpha) * deg2rad)) - z;
  479. shapeArgs.alpha = 90 - alpha;
  480. shapeArgs.beta = 0;
  481. });
  482. });
  483. */