ColumnSeries.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 A from '../Core/Animation/AnimationUtilities.js';
  11. var animObject = A.animObject;
  12. import BaseSeries from '../Core/Series/Series.js';
  13. import Color from '../Core/Color/Color.js';
  14. var color = Color.parse;
  15. import H from '../Core/Globals.js';
  16. var noop = H.noop;
  17. import LegendSymbolMixin from '../Mixins/LegendSymbol.js';
  18. import LineSeries from './LineSeries.js';
  19. import U from '../Core/Utilities.js';
  20. var clamp = U.clamp, defined = U.defined, extend = U.extend, isArray = U.isArray, isNumber = U.isNumber, merge = U.merge, pick = U.pick, objectEach = U.objectEach;
  21. /**
  22. * Adjusted width and x offset of the columns for grouping.
  23. *
  24. * @private
  25. * @interface Highcharts.ColumnMetricsObject
  26. */ /**
  27. * Width of the columns.
  28. * @name Highcharts.ColumnMetricsObject#width
  29. * @type {number}
  30. */ /**
  31. * Offset of the columns.
  32. * @name Highcharts.ColumnMetricsObject#offset
  33. * @type {number}
  34. */
  35. ''; // detach doclets above
  36. /**
  37. * The column series type.
  38. *
  39. * @private
  40. * @class
  41. * @name Highcharts.seriesTypes.column
  42. *
  43. * @augments Highcharts.Series
  44. */
  45. var ColumnSeries = BaseSeries.seriesType('column', 'line',
  46. /**
  47. * Column series display one column per value along an X axis.
  48. *
  49. * @sample {highcharts} highcharts/demo/column-basic/
  50. * Column chart
  51. * @sample {highstock} stock/demo/column/
  52. * Column chart
  53. *
  54. * @extends plotOptions.line
  55. * @excluding connectEnds, connectNulls, gapSize, gapUnit, linecap,
  56. * lineWidth, marker, step, useOhlcData
  57. * @product highcharts highstock
  58. * @optionparent plotOptions.column
  59. */
  60. {
  61. /**
  62. * The corner radius of the border surrounding each column or bar.
  63. *
  64. * @sample {highcharts} highcharts/plotoptions/column-borderradius/
  65. * Rounded columns
  66. *
  67. * @product highcharts highstock gantt
  68. *
  69. * @private
  70. */
  71. borderRadius: 0,
  72. /**
  73. * When using automatic point colors pulled from the global
  74. * [colors](colors) or series-specific
  75. * [plotOptions.column.colors](series.colors) collections, this option
  76. * determines whether the chart should receive one color per series or
  77. * one color per point.
  78. *
  79. * In styled mode, the `colors` or `series.colors` arrays are not
  80. * supported, and instead this option gives the points individual color
  81. * class names on the form `highcharts-color-{n}`.
  82. *
  83. * @see [series colors](#plotOptions.column.colors)
  84. *
  85. * @sample {highcharts} highcharts/plotoptions/column-colorbypoint-false/
  86. * False by default
  87. * @sample {highcharts} highcharts/plotoptions/column-colorbypoint-true/
  88. * True
  89. *
  90. * @type {boolean}
  91. * @default false
  92. * @since 2.0
  93. * @product highcharts highstock gantt
  94. * @apioption plotOptions.column.colorByPoint
  95. */
  96. /**
  97. * A series specific or series type specific color set to apply instead
  98. * of the global [colors](#colors) when [colorByPoint](
  99. * #plotOptions.column.colorByPoint) is true.
  100. *
  101. * @type {Array<Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject>}
  102. * @since 3.0
  103. * @product highcharts highstock gantt
  104. * @apioption plotOptions.column.colors
  105. */
  106. /**
  107. * When `true`, the columns will center in the category, ignoring null
  108. * or missing points. When `false`, space will be reserved for null or
  109. * missing points.
  110. *
  111. * @sample {highcharts} highcharts/series-column/centerincategory/
  112. * Center in category
  113. *
  114. * @since 8.0.1
  115. * @product highcharts highstock gantt
  116. *
  117. * @private
  118. */
  119. centerInCategory: false,
  120. /**
  121. * Padding between each value groups, in x axis units.
  122. *
  123. * @sample {highcharts} highcharts/plotoptions/column-grouppadding-default/
  124. * 0.2 by default
  125. * @sample {highcharts} highcharts/plotoptions/column-grouppadding-none/
  126. * No group padding - all columns are evenly spaced
  127. *
  128. * @product highcharts highstock gantt
  129. *
  130. * @private
  131. */
  132. groupPadding: 0.2,
  133. /**
  134. * Whether to group non-stacked columns or to let them render
  135. * independent of each other. Non-grouped columns will be laid out
  136. * individually and overlap each other.
  137. *
  138. * @sample {highcharts} highcharts/plotoptions/column-grouping-false/
  139. * Grouping disabled
  140. * @sample {highstock} highcharts/plotoptions/column-grouping-false/
  141. * Grouping disabled
  142. *
  143. * @type {boolean}
  144. * @default true
  145. * @since 2.3.0
  146. * @product highcharts highstock gantt
  147. * @apioption plotOptions.column.grouping
  148. */
  149. /**
  150. * @ignore-option
  151. * @private
  152. */
  153. marker: null,
  154. /**
  155. * The maximum allowed pixel width for a column, translated to the
  156. * height of a bar in a bar chart. This prevents the columns from
  157. * becoming too wide when there is a small number of points in the
  158. * chart.
  159. *
  160. * @see [pointWidth](#plotOptions.column.pointWidth)
  161. *
  162. * @sample {highcharts} highcharts/plotoptions/column-maxpointwidth-20/
  163. * Limited to 50
  164. * @sample {highstock} highcharts/plotoptions/column-maxpointwidth-20/
  165. * Limited to 50
  166. *
  167. * @type {number}
  168. * @since 4.1.8
  169. * @product highcharts highstock gantt
  170. * @apioption plotOptions.column.maxPointWidth
  171. */
  172. /**
  173. * Padding between each column or bar, in x axis units.
  174. *
  175. * @sample {highcharts} highcharts/plotoptions/column-pointpadding-default/
  176. * 0.1 by default
  177. * @sample {highcharts} highcharts/plotoptions/column-pointpadding-025/
  178. * 0.25
  179. * @sample {highcharts} highcharts/plotoptions/column-pointpadding-none/
  180. * 0 for tightly packed columns
  181. *
  182. * @product highcharts highstock gantt
  183. *
  184. * @private
  185. */
  186. pointPadding: 0.1,
  187. /**
  188. * A pixel value specifying a fixed width for each column or bar point.
  189. * When `null`, the width is calculated from the `pointPadding` and
  190. * `groupPadding`. The width effects the dimension that is not based on
  191. * the point value. For column series it is the hoizontal length and for
  192. * bar series it is the vertical length.
  193. *
  194. * @see [maxPointWidth](#plotOptions.column.maxPointWidth)
  195. *
  196. * @sample {highcharts} highcharts/plotoptions/column-pointwidth-20/
  197. * 20px wide columns regardless of chart width or the amount of
  198. * data points
  199. *
  200. * @type {number}
  201. * @since 1.2.5
  202. * @product highcharts highstock gantt
  203. * @apioption plotOptions.column.pointWidth
  204. */
  205. /**
  206. * A pixel value specifying a fixed width for the column or bar.
  207. * Overrides pointWidth on the series.
  208. *
  209. * @see [series.pointWidth](#plotOptions.column.pointWidth)
  210. *
  211. * @type {number}
  212. * @default undefined
  213. * @since 7.0.0
  214. * @product highcharts highstock gantt
  215. * @apioption series.column.data.pointWidth
  216. */
  217. /**
  218. * The minimal height for a column or width for a bar. By default,
  219. * 0 values are not shown. To visualize a 0 (or close to zero) point,
  220. * set the minimal point length to a pixel value like 3\. In stacked
  221. * column charts, minPointLength might not be respected for tightly
  222. * packed values.
  223. *
  224. * @sample {highcharts} highcharts/plotoptions/column-minpointlength/
  225. * Zero base value
  226. * @sample {highcharts} highcharts/plotoptions/column-minpointlength-pos-and-neg/
  227. * Positive and negative close to zero values
  228. *
  229. * @product highcharts highstock gantt
  230. *
  231. * @private
  232. */
  233. minPointLength: 0,
  234. /**
  235. * When the series contains less points than the crop threshold, all
  236. * points are drawn, event if the points fall outside the visible plot
  237. * area at the current zoom. The advantage of drawing all points
  238. * (including markers and columns), is that animation is performed on
  239. * updates. On the other hand, when the series contains more points than
  240. * the crop threshold, the series data is cropped to only contain points
  241. * that fall within the plot area. The advantage of cropping away
  242. * invisible points is to increase performance on large series.
  243. *
  244. * @product highcharts highstock gantt
  245. *
  246. * @private
  247. */
  248. cropThreshold: 50,
  249. /**
  250. * The X axis range that each point is valid for. This determines the
  251. * width of the column. On a categorized axis, the range will be 1
  252. * by default (one category unit). On linear and datetime axes, the
  253. * range will be computed as the distance between the two closest data
  254. * points.
  255. *
  256. * The default `null` means it is computed automatically, but this
  257. * option can be used to override the automatic value.
  258. *
  259. * This option is set by default to 1 if data sorting is enabled.
  260. *
  261. * @sample {highcharts} highcharts/plotoptions/column-pointrange/
  262. * Set the point range to one day on a data set with one week
  263. * between the points
  264. *
  265. * @type {number|null}
  266. * @since 2.3
  267. * @product highcharts highstock gantt
  268. *
  269. * @private
  270. */
  271. pointRange: null,
  272. states: {
  273. /**
  274. * Options for the hovered point. These settings override the normal
  275. * state options when a point is moused over or touched.
  276. *
  277. * @extends plotOptions.series.states.hover
  278. * @excluding halo, lineWidth, lineWidthPlus, marker
  279. * @product highcharts highstock gantt
  280. */
  281. hover: {
  282. /** @ignore-option */
  283. halo: false,
  284. /**
  285. * A specific border color for the hovered point. Defaults to
  286. * inherit the normal state border color.
  287. *
  288. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  289. * @product highcharts gantt
  290. * @apioption plotOptions.column.states.hover.borderColor
  291. */
  292. /**
  293. * A specific color for the hovered point.
  294. *
  295. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  296. * @product highcharts gantt
  297. * @apioption plotOptions.column.states.hover.color
  298. */
  299. /**
  300. * How much to brighten the point on interaction. Requires the
  301. * main color to be defined in hex or rgb(a) format.
  302. *
  303. * In styled mode, the hover brightening is by default replaced
  304. * with a fill-opacity set in the `.highcharts-point:hover`
  305. * rule.
  306. *
  307. * @sample {highcharts} highcharts/plotoptions/column-states-hover-brightness/
  308. * Brighten by 0.5
  309. *
  310. * @product highcharts highstock gantt
  311. */
  312. brightness: 0.1
  313. },
  314. /**
  315. * Options for the selected point. These settings override the
  316. * normal state options when a point is selected.
  317. *
  318. * @extends plotOptions.series.states.select
  319. * @excluding halo, lineWidth, lineWidthPlus, marker
  320. * @product highcharts highstock gantt
  321. */
  322. select: {
  323. /**
  324. * A specific color for the selected point.
  325. *
  326. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  327. * @default #cccccc
  328. * @product highcharts highstock gantt
  329. */
  330. color: '#cccccc',
  331. /**
  332. * A specific border color for the selected point.
  333. *
  334. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  335. * @default #000000
  336. * @product highcharts highstock gantt
  337. */
  338. borderColor: '#000000'
  339. }
  340. },
  341. dataLabels: {
  342. align: void 0,
  343. verticalAlign: void 0,
  344. /**
  345. * The y position offset of the label relative to the point in
  346. * pixels.
  347. *
  348. * @type {number}
  349. */
  350. y: void 0
  351. },
  352. // false doesn't work well: https://jsfiddle.net/highcharts/hz8fopan/14/
  353. /**
  354. * @ignore-option
  355. * @private
  356. */
  357. startFromThreshold: true,
  358. stickyTracking: false,
  359. tooltip: {
  360. distance: 6
  361. },
  362. /**
  363. * The Y axis value to serve as the base for the columns, for
  364. * distinguishing between values above and below a threshold. If `null`,
  365. * the columns extend from the padding Y axis minimum.
  366. *
  367. * @type {number|null}
  368. * @since 2.0
  369. * @product highcharts
  370. *
  371. * @private
  372. */
  373. threshold: 0,
  374. /**
  375. * The width of the border surrounding each column or bar. Defaults to
  376. * `1` when there is room for a border, but to `0` when the columns are
  377. * so dense that a border would cover the next column.
  378. *
  379. * In styled mode, the stroke width can be set with the
  380. * `.highcharts-point` rule.
  381. *
  382. * @sample {highcharts} highcharts/plotoptions/column-borderwidth/
  383. * 2px black border
  384. *
  385. * @type {number}
  386. * @default undefined
  387. * @product highcharts highstock gantt
  388. * @apioption plotOptions.column.borderWidth
  389. */
  390. /**
  391. * The color of the border surrounding each column or bar.
  392. *
  393. * In styled mode, the border stroke can be set with the
  394. * `.highcharts-point` rule.
  395. *
  396. * @sample {highcharts} highcharts/plotoptions/column-bordercolor/
  397. * Dark gray border
  398. *
  399. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  400. * @default #ffffff
  401. * @product highcharts highstock gantt
  402. *
  403. * @private
  404. */
  405. borderColor: '#ffffff'
  406. },
  407. /**
  408. * @lends seriesTypes.column.prototype
  409. */
  410. {
  411. cropShoulder: 0,
  412. // When tooltip is not shared, this series (and derivatives) requires
  413. // direct touch/hover. KD-tree does not apply.
  414. directTouch: true,
  415. trackerGroups: ['group', 'dataLabelsGroup'],
  416. // use separate negative stacks, unlike area stacks where a negative
  417. // point is substracted from previous (#1910)
  418. negStacks: true,
  419. /* eslint-disable valid-jsdoc */
  420. /**
  421. * Initialize the series. Extends the basic Series.init method by
  422. * marking other series of the same type as dirty.
  423. *
  424. * @private
  425. * @function Highcharts.seriesTypes.column#init
  426. * @return {void}
  427. */
  428. init: function () {
  429. LineSeries.prototype.init.apply(this, arguments);
  430. var series = this, chart = series.chart;
  431. // if the series is added dynamically, force redraw of other
  432. // series affected by a new column
  433. if (chart.hasRendered) {
  434. chart.series.forEach(function (otherSeries) {
  435. if (otherSeries.type === series.type) {
  436. otherSeries.isDirty = true;
  437. }
  438. });
  439. }
  440. },
  441. /**
  442. * Return the width and x offset of the columns adjusted for grouping,
  443. * groupPadding, pointPadding, pointWidth etc.
  444. *
  445. * @private
  446. * @function Highcharts.seriesTypes.column#getColumnMetrics
  447. * @return {Highcharts.ColumnMetricsObject}
  448. */
  449. getColumnMetrics: function () {
  450. var series = this, options = series.options, xAxis = series.xAxis, yAxis = series.yAxis, reversedStacks = xAxis.options.reversedStacks,
  451. // Keep backward compatibility: reversed xAxis had reversed
  452. // stacks
  453. reverseStacks = (xAxis.reversed && !reversedStacks) ||
  454. (!xAxis.reversed && reversedStacks), stackKey, stackGroups = {}, columnCount = 0;
  455. // Get the total number of column type series. This is called on
  456. // every series. Consider moving this logic to a chart.orderStacks()
  457. // function and call it on init, addSeries and removeSeries
  458. if (options.grouping === false) {
  459. columnCount = 1;
  460. }
  461. else {
  462. series.chart.series.forEach(function (otherSeries) {
  463. var otherYAxis = otherSeries.yAxis, otherOptions = otherSeries.options, columnIndex;
  464. if (otherSeries.type === series.type &&
  465. (otherSeries.visible ||
  466. !series.chart.options.chart
  467. .ignoreHiddenSeries) &&
  468. yAxis.len === otherYAxis.len &&
  469. yAxis.pos === otherYAxis.pos) { // #642, #2086
  470. if (otherOptions.stacking && otherOptions.stacking !== 'group') {
  471. stackKey = otherSeries.stackKey;
  472. if (typeof stackGroups[stackKey] ===
  473. 'undefined') {
  474. stackGroups[stackKey] = columnCount++;
  475. }
  476. columnIndex = stackGroups[stackKey];
  477. }
  478. else if (otherOptions.grouping !== false) { // #1162
  479. columnIndex = columnCount++;
  480. }
  481. otherSeries.columnIndex = columnIndex;
  482. }
  483. });
  484. }
  485. var categoryWidth = Math.min(Math.abs(xAxis.transA) * ((xAxis.ordinal && xAxis.ordinal.slope) ||
  486. options.pointRange ||
  487. xAxis.closestPointRange ||
  488. xAxis.tickInterval ||
  489. 1), // #2610
  490. xAxis.len // #1535
  491. ), groupPadding = categoryWidth * options.groupPadding, groupWidth = categoryWidth - 2 * groupPadding, pointOffsetWidth = groupWidth / (columnCount || 1), pointWidth = Math.min(options.maxPointWidth || xAxis.len, pick(options.pointWidth, pointOffsetWidth * (1 - 2 * options.pointPadding))), pointPadding = (pointOffsetWidth - pointWidth) / 2,
  492. // #1251, #3737
  493. colIndex = (series.columnIndex || 0) + (reverseStacks ? 1 : 0), pointXOffset = pointPadding +
  494. (groupPadding +
  495. colIndex * pointOffsetWidth -
  496. (categoryWidth / 2)) * (reverseStacks ? -1 : 1);
  497. // Save it for reading in linked series (Error bars particularly)
  498. series.columnMetrics = {
  499. width: pointWidth,
  500. offset: pointXOffset,
  501. paddedWidth: pointOffsetWidth,
  502. columnCount: columnCount
  503. };
  504. return series.columnMetrics;
  505. },
  506. /**
  507. * Make the columns crisp. The edges are rounded to the nearest full
  508. * pixel.
  509. *
  510. * @private
  511. * @function Highcharts.seriesTypes.column#crispCol
  512. * @param {number} x
  513. * @param {number} y
  514. * @param {number} w
  515. * @param {number} h
  516. * @return {Highcharts.BBoxObject}
  517. */
  518. crispCol: function (x, y, w, h) {
  519. var chart = this.chart, borderWidth = this.borderWidth, xCrisp = -(borderWidth % 2 ? 0.5 : 0), yCrisp = borderWidth % 2 ? 0.5 : 1, right, bottom, fromTop;
  520. if (chart.inverted && chart.renderer.isVML) {
  521. yCrisp += 1;
  522. }
  523. // Horizontal. We need to first compute the exact right edge, then
  524. // round it and compute the width from there.
  525. if (this.options.crisp) {
  526. right = Math.round(x + w) + xCrisp;
  527. x = Math.round(x) + xCrisp;
  528. w = right - x;
  529. }
  530. // Vertical
  531. bottom = Math.round(y + h) + yCrisp;
  532. fromTop = Math.abs(y) <= 0.5 && bottom > 0.5; // #4504, #4656
  533. y = Math.round(y) + yCrisp;
  534. h = bottom - y;
  535. // Top edges are exceptions
  536. if (fromTop && h) { // #5146
  537. y -= 1;
  538. h += 1;
  539. }
  540. return {
  541. x: x,
  542. y: y,
  543. width: w,
  544. height: h
  545. };
  546. },
  547. /**
  548. * Adjust for missing columns, according to the `centerInCategory`
  549. * option. Missing columns are either single points or stacks where the
  550. * point or points are either missing or null.
  551. *
  552. * @private
  553. * @function Highcharts.seriesTypes.column#adjustForMissingColumns
  554. * @param {number} x
  555. * The x coordinate of the column, left side
  556. * @param {number} pointWidth
  557. * The pointWidth, already computed upstream
  558. * @param {Highcharts.ColumnPoint} point
  559. * The point instance
  560. * @param {Highcharts.ColumnMetricsObject} metrics
  561. * The series-wide column metrics
  562. * @return {number}
  563. * The adjusted x position, or the original if not adjusted
  564. */
  565. adjustForMissingColumns: function (x, pointWidth, point, metrics) {
  566. var _this = this;
  567. var stacking = this.options.stacking;
  568. if (!point.isNull && metrics.columnCount > 1) {
  569. var indexInCategory_1 = 0;
  570. var totalInCategory_1 = 0;
  571. // Loop over all the stacks on the Y axis. When stacking is
  572. // enabled, these are real point stacks. When stacking is not
  573. // enabled, but `centerInCategory` is true, there is one stack
  574. // handling the grouping of points in each category. This is
  575. // done in the `setGroupedPoints` function.
  576. objectEach(this.yAxis.stacking && this.yAxis.stacking.stacks, function (stack) {
  577. if (typeof point.x === 'number') {
  578. var stackItem = stack[point.x.toString()];
  579. if (stackItem) {
  580. var pointValues = stackItem.points[_this.index], total = stackItem.total;
  581. // If true `stacking` is enabled, count the
  582. // total number of non-null stacks in the
  583. // category, and note which index this point is
  584. // within those stacks.
  585. if (stacking) {
  586. if (pointValues) {
  587. indexInCategory_1 = totalInCategory_1;
  588. }
  589. if (stackItem.hasValidPoints) {
  590. totalInCategory_1++;
  591. }
  592. // If `stacking` is not enabled, look for the
  593. // index and total of the `group` stack.
  594. }
  595. else if (isArray(pointValues)) {
  596. indexInCategory_1 = pointValues[1];
  597. totalInCategory_1 = total || 0;
  598. }
  599. }
  600. }
  601. });
  602. // Compute the adjusted x position
  603. var boxWidth = (totalInCategory_1 - 1) * metrics.paddedWidth +
  604. pointWidth;
  605. x = (point.plotX || 0) + boxWidth / 2 - pointWidth -
  606. indexInCategory_1 * metrics.paddedWidth;
  607. }
  608. return x;
  609. },
  610. /**
  611. * Translate each point to the plot area coordinate system and find
  612. * shape positions
  613. *
  614. * @private
  615. * @function Highcharts.seriesTypes.column#translate
  616. */
  617. translate: function () {
  618. var series = this, chart = series.chart, options = series.options, dense = series.dense =
  619. series.closestPointRange * series.xAxis.transA < 2, borderWidth = series.borderWidth = pick(options.borderWidth, dense ? 0 : 1 // #3635
  620. ), xAxis = series.xAxis, yAxis = series.yAxis, threshold = options.threshold, translatedThreshold = series.translatedThreshold =
  621. yAxis.getThreshold(threshold), minPointLength = pick(options.minPointLength, 5), metrics = series.getColumnMetrics(), seriesPointWidth = metrics.width,
  622. // postprocessed for border width
  623. seriesBarW = series.barW =
  624. Math.max(seriesPointWidth, 1 + 2 * borderWidth), seriesXOffset = series.pointXOffset = metrics.offset, dataMin = series.dataMin, dataMax = series.dataMax;
  625. if (chart.inverted) {
  626. translatedThreshold -= 0.5; // #3355
  627. }
  628. // When the pointPadding is 0, we want the columns to be packed
  629. // tightly, so we allow individual columns to have individual sizes.
  630. // When pointPadding is greater, we strive for equal-width columns
  631. // (#2694).
  632. if (options.pointPadding) {
  633. seriesBarW = Math.ceil(seriesBarW);
  634. }
  635. LineSeries.prototype.translate.apply(series);
  636. // Record the new values
  637. series.points.forEach(function (point) {
  638. var yBottom = pick(point.yBottom, translatedThreshold), safeDistance = 999 + Math.abs(yBottom), pointWidth = seriesPointWidth, plotX = point.plotX || 0,
  639. // Don't draw too far outside plot area (#1303, #2241,
  640. // #4264)
  641. plotY = clamp(point.plotY, -safeDistance, yAxis.len + safeDistance), barX = plotX + seriesXOffset, barW = seriesBarW, barY = Math.min(plotY, yBottom), up, barH = Math.max(plotY, yBottom) - barY;
  642. // Handle options.minPointLength
  643. if (minPointLength && Math.abs(barH) < minPointLength) {
  644. barH = minPointLength;
  645. up = (!yAxis.reversed && !point.negative) ||
  646. (yAxis.reversed && point.negative);
  647. // Reverse zeros if there's no positive value in the series
  648. // in visible range (#7046)
  649. if (isNumber(threshold) &&
  650. isNumber(dataMax) &&
  651. point.y === threshold &&
  652. dataMax <= threshold &&
  653. // and if there's room for it (#7311)
  654. (yAxis.min || 0) < threshold &&
  655. // if all points are the same value (i.e zero) not draw
  656. // as negative points (#10646)
  657. dataMin !== dataMax) {
  658. up = !up;
  659. }
  660. // If stacked...
  661. barY = (Math.abs(barY - translatedThreshold) > minPointLength ?
  662. // ...keep position
  663. yBottom - minPointLength :
  664. // #1485, #4051
  665. translatedThreshold -
  666. (up ? minPointLength : 0));
  667. }
  668. // Handle point.options.pointWidth
  669. // @todo Handle grouping/stacking too. Calculate offset properly
  670. if (defined(point.options.pointWidth)) {
  671. pointWidth = barW =
  672. Math.ceil(point.options.pointWidth);
  673. barX -= Math.round((pointWidth - seriesPointWidth) / 2);
  674. }
  675. // Adjust for null or missing points
  676. if (options.centerInCategory) {
  677. barX = series.adjustForMissingColumns(barX, pointWidth, point, metrics);
  678. }
  679. // Cache for access in polar
  680. point.barX = barX;
  681. point.pointWidth = pointWidth;
  682. // Fix the tooltip on center of grouped columns (#1216, #424,
  683. // #3648)
  684. point.tooltipPos = chart.inverted ?
  685. [
  686. yAxis.len + yAxis.pos - chart.plotLeft - plotY,
  687. xAxis.len + xAxis.pos - chart.plotTop - (plotX || 0) - seriesXOffset - barW / 2,
  688. barH
  689. ] :
  690. [barX + barW / 2, plotY + yAxis.pos -
  691. chart.plotTop, barH];
  692. // Register shape type and arguments to be used in drawPoints
  693. // Allow shapeType defined on pointClass level
  694. point.shapeType =
  695. series.pointClass.prototype.shapeType || 'rect';
  696. point.shapeArgs = series.crispCol.apply(series, point.isNull ?
  697. // #3169, drilldown from null must have a position to work
  698. // from #6585, dataLabel should be placed on xAxis, not
  699. // floating in the middle of the chart
  700. [barX, translatedThreshold, barW, 0] :
  701. [barX, barY, barW, barH]);
  702. });
  703. },
  704. getSymbol: noop,
  705. /**
  706. * Use a solid rectangle like the area series types
  707. *
  708. * @private
  709. * @function Highcharts.seriesTypes.column#drawLegendSymbol
  710. *
  711. * @param {Highcharts.Legend} legend
  712. * The legend object
  713. *
  714. * @param {Highcharts.Series|Highcharts.Point} item
  715. * The series (this) or point
  716. */
  717. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  718. /**
  719. * Columns have no graph
  720. *
  721. * @private
  722. * @function Highcharts.seriesTypes.column#drawGraph
  723. */
  724. drawGraph: function () {
  725. this.group[this.dense ? 'addClass' : 'removeClass']('highcharts-dense-data');
  726. },
  727. /**
  728. * Get presentational attributes
  729. *
  730. * @private
  731. * @function Highcharts.seriesTypes.column#pointAttribs
  732. *
  733. * @param {Highcharts.ColumnPoint} point
  734. *
  735. * @param {string} state
  736. *
  737. * @return {Highcharts.SVGAttributes}
  738. */
  739. pointAttribs: function (point, state) {
  740. var options = this.options, stateOptions, ret, p2o = this.pointAttrToOptions || {}, strokeOption = p2o.stroke || 'borderColor', strokeWidthOption = p2o['stroke-width'] || 'borderWidth', fill = (point && point.color) || this.color,
  741. // set to fill when borderColor null:
  742. stroke = ((point && point[strokeOption]) ||
  743. options[strokeOption] ||
  744. this.color ||
  745. fill), strokeWidth = (point && point[strokeWidthOption]) ||
  746. options[strokeWidthOption] ||
  747. this[strokeWidthOption] || 0, dashstyle = (point && point.options.dashStyle) || options.dashStyle, opacity = pick(point && point.opacity, options.opacity, 1), zone, brightness;
  748. // Handle zone colors
  749. if (point && this.zones.length) {
  750. zone = point.getZone();
  751. // When zones are present, don't use point.color (#4267).
  752. // Changed order (#6527), added support for colorAxis (#10670)
  753. fill = (point.options.color ||
  754. (zone && (zone.color || point.nonZonedColor)) ||
  755. this.color);
  756. if (zone) {
  757. stroke = zone.borderColor || stroke;
  758. dashstyle = zone.dashStyle || dashstyle;
  759. strokeWidth = zone.borderWidth || strokeWidth;
  760. }
  761. }
  762. // Select or hover states
  763. if (state && point) {
  764. stateOptions = merge(options.states[state],
  765. // #6401
  766. point.options.states &&
  767. point.options.states[state] ||
  768. {});
  769. brightness = stateOptions.brightness;
  770. fill =
  771. stateOptions.color || (typeof brightness !== 'undefined' &&
  772. color(fill)
  773. .brighten(stateOptions.brightness)
  774. .get()) || fill;
  775. stroke = stateOptions[strokeOption] || stroke;
  776. strokeWidth =
  777. stateOptions[strokeWidthOption] || strokeWidth;
  778. dashstyle = stateOptions.dashStyle || dashstyle;
  779. opacity = pick(stateOptions.opacity, opacity);
  780. }
  781. ret = {
  782. fill: fill,
  783. stroke: stroke,
  784. 'stroke-width': strokeWidth,
  785. opacity: opacity
  786. };
  787. if (dashstyle) {
  788. ret.dashstyle = dashstyle;
  789. }
  790. return ret;
  791. },
  792. /**
  793. * Draw the columns. For bars, the series.group is rotated, so the same
  794. * coordinates apply for columns and bars. This method is inherited by
  795. * scatter series.
  796. *
  797. * @private
  798. * @function Highcharts.seriesTypes.column#drawPoints
  799. */
  800. drawPoints: function () {
  801. var series = this, chart = this.chart, options = series.options, renderer = chart.renderer, animationLimit = options.animationLimit || 250, shapeArgs;
  802. // draw the columns
  803. series.points.forEach(function (point) {
  804. var plotY = point.plotY, graphic = point.graphic, hasGraphic = !!graphic, verb = graphic && chart.pointCount < animationLimit ?
  805. 'animate' : 'attr';
  806. if (isNumber(plotY) && point.y !== null) {
  807. shapeArgs = point.shapeArgs;
  808. // When updating a series between 2d and 3d or cartesian and
  809. // polar, the shape type changes.
  810. if (graphic && point.hasNewShapeType()) {
  811. graphic = graphic.destroy();
  812. }
  813. // Set starting position for point sliding animation.
  814. if (series.enabledDataSorting) {
  815. point.startXPos = series.xAxis.reversed ?
  816. -(shapeArgs ? shapeArgs.width : 0) :
  817. series.xAxis.width;
  818. }
  819. if (!graphic) {
  820. point.graphic = graphic =
  821. renderer[point.shapeType](shapeArgs)
  822. .add(point.group || series.group);
  823. if (graphic &&
  824. series.enabledDataSorting &&
  825. chart.hasRendered &&
  826. chart.pointCount < animationLimit) {
  827. graphic.attr({
  828. x: point.startXPos
  829. });
  830. hasGraphic = true;
  831. verb = 'animate';
  832. }
  833. }
  834. if (graphic && hasGraphic) { // update
  835. graphic[verb](merge(shapeArgs));
  836. }
  837. // Border radius is not stylable (#6900)
  838. if (options.borderRadius) {
  839. graphic[verb]({
  840. r: options.borderRadius
  841. });
  842. }
  843. // Presentational
  844. if (!chart.styledMode) {
  845. graphic[verb](series.pointAttribs(point, (point.selected && 'select')))
  846. .shadow(point.allowShadow !== false && options.shadow, null, options.stacking && !options.borderRadius);
  847. }
  848. graphic.addClass(point.getClassName(), true);
  849. }
  850. else if (graphic) {
  851. point.graphic = graphic.destroy(); // #1269
  852. }
  853. });
  854. },
  855. /**
  856. * Animate the column heights one by one from zero.
  857. *
  858. * @private
  859. * @function Highcharts.seriesTypes.column#animate
  860. *
  861. * @param {boolean} init
  862. * Whether to initialize the animation or run it
  863. */
  864. animate: function (init) {
  865. var series = this, yAxis = this.yAxis, options = series.options, inverted = this.chart.inverted, attr = {}, translateProp = inverted ? 'translateX' : 'translateY', translateStart, translatedThreshold;
  866. if (init) {
  867. attr.scaleY = 0.001;
  868. translatedThreshold = clamp(yAxis.toPixels(options.threshold), yAxis.pos, yAxis.pos + yAxis.len);
  869. if (inverted) {
  870. attr.translateX = translatedThreshold - yAxis.len;
  871. }
  872. else {
  873. attr.translateY = translatedThreshold;
  874. }
  875. // apply finnal clipping (used in Highstock) (#7083)
  876. // animation is done by scaleY, so cliping is for panes
  877. if (series.clipBox) {
  878. series.setClip();
  879. }
  880. series.group.attr(attr);
  881. }
  882. else { // run the animation
  883. translateStart = series.group.attr(translateProp);
  884. series.group.animate({ scaleY: 1 }, extend(animObject(series.options.animation), {
  885. // Do the scale synchronously to ensure smooth
  886. // updating (#5030, #7228)
  887. step: function (val, fx) {
  888. if (series.group) {
  889. attr[translateProp] = translateStart +
  890. fx.pos * (yAxis.pos - translateStart);
  891. series.group.attr(attr);
  892. }
  893. }
  894. }));
  895. }
  896. },
  897. /**
  898. * Remove this series from the chart
  899. *
  900. * @private
  901. * @function Highcharts.seriesTypes.column#remove
  902. */
  903. remove: function () {
  904. var series = this, chart = series.chart;
  905. // column and bar series affects other series of the same type
  906. // as they are either stacked or grouped
  907. if (chart.hasRendered) {
  908. chart.series.forEach(function (otherSeries) {
  909. if (otherSeries.type === series.type) {
  910. otherSeries.isDirty = true;
  911. }
  912. });
  913. }
  914. LineSeries.prototype.remove.apply(series, arguments);
  915. }
  916. });
  917. /* eslint-enable valid-jsdoc */
  918. /**
  919. * A `column` series. If the [type](#series.column.type) option is
  920. * not specified, it is inherited from [chart.type](#chart.type).
  921. *
  922. * @extends series,plotOptions.column
  923. * @excluding connectNulls, dataParser, dataURL, gapSize, gapUnit, linecap,
  924. * lineWidth, marker, connectEnds, step
  925. * @product highcharts highstock
  926. * @apioption series.column
  927. */
  928. /**
  929. * An array of data points for the series. For the `column` series type,
  930. * points can be given in the following ways:
  931. *
  932. * 1. An array of numerical values. In this case, the numerical values will be
  933. * interpreted as `y` options. The `x` values will be automatically
  934. * calculated, either starting at 0 and incremented by 1, or from
  935. * `pointStart` and `pointInterval` given in the series options. If the axis
  936. * has categories, these will be used. Example:
  937. * ```js
  938. * data: [0, 5, 3, 5]
  939. * ```
  940. *
  941. * 2. An array of arrays with 2 values. In this case, the values correspond to
  942. * `x,y`. If the first value is a string, it is applied as the name of the
  943. * point, and the `x` value is inferred.
  944. * ```js
  945. * data: [
  946. * [0, 6],
  947. * [1, 2],
  948. * [2, 6]
  949. * ]
  950. * ```
  951. *
  952. * 3. An array of objects with named values. The following snippet shows only a
  953. * few settings, see the complete options set below. If the total number of
  954. * data points exceeds the series'
  955. * [turboThreshold](#series.column.turboThreshold), this option is not
  956. * available.
  957. * ```js
  958. * data: [{
  959. * x: 1,
  960. * y: 9,
  961. * name: "Point2",
  962. * color: "#00FF00"
  963. * }, {
  964. * x: 1,
  965. * y: 6,
  966. * name: "Point1",
  967. * color: "#FF00FF"
  968. * }]
  969. * ```
  970. *
  971. * @sample {highcharts} highcharts/chart/reflow-true/
  972. * Numerical values
  973. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  974. * Arrays of numeric x and y
  975. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  976. * Arrays of datetime x and y
  977. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  978. * Arrays of point.name and y
  979. * @sample {highcharts} highcharts/series/data-array-of-objects/
  980. * Config objects
  981. *
  982. * @type {Array<number|Array<(number|string),(number|null)>|null|*>}
  983. * @extends series.line.data
  984. * @excluding marker
  985. * @product highcharts highstock
  986. * @apioption series.column.data
  987. */
  988. /**
  989. * The color of the border surrounding the column or bar.
  990. *
  991. * In styled mode, the border stroke can be set with the `.highcharts-point`
  992. * rule.
  993. *
  994. * @sample {highcharts} highcharts/plotoptions/column-bordercolor/
  995. * Dark gray border
  996. *
  997. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  998. * @product highcharts highstock
  999. * @apioption series.column.data.borderColor
  1000. */
  1001. /**
  1002. * The width of the border surrounding the column or bar.
  1003. *
  1004. * In styled mode, the stroke width can be set with the `.highcharts-point`
  1005. * rule.
  1006. *
  1007. * @sample {highcharts} highcharts/plotoptions/column-borderwidth/
  1008. * 2px black border
  1009. *
  1010. * @type {number}
  1011. * @product highcharts highstock
  1012. * @apioption series.column.data.borderWidth
  1013. */
  1014. /**
  1015. * A name for the dash style to use for the column or bar. Overrides
  1016. * dashStyle on the series.
  1017. *
  1018. * In styled mode, the stroke dash-array can be set with the same classes as
  1019. * listed under [data.color](#series.column.data.color).
  1020. *
  1021. * @see [series.pointWidth](#plotOptions.column.dashStyle)
  1022. *
  1023. * @type {Highcharts.DashStyleValue}
  1024. * @apioption series.column.data.dashStyle
  1025. */
  1026. /**
  1027. * A pixel value specifying a fixed width for the column or bar. Overrides
  1028. * pointWidth on the series. The width effects the dimension that is not based
  1029. * on the point value.
  1030. *
  1031. * @see [series.pointWidth](#plotOptions.column.pointWidth)
  1032. *
  1033. * @type {number}
  1034. * @apioption series.column.data.pointWidth
  1035. */
  1036. /**
  1037. * @excluding halo, lineWidth, lineWidthPlus, marker
  1038. * @product highcharts highstock
  1039. * @apioption series.column.states.hover
  1040. */
  1041. /**
  1042. * @excluding halo, lineWidth, lineWidthPlus, marker
  1043. * @product highcharts highstock
  1044. * @apioption series.column.states.select
  1045. */
  1046. ''; // includes above doclets in transpilat
  1047. export default ColumnSeries;