BoxPlotSeries.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* *
  2. * (c) 2010-2019 Torstein Honsi
  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/Options.js';
  10. var noop = H.noop,
  11. pick = H.pick,
  12. seriesType = H.seriesType,
  13. seriesTypes = H.seriesTypes;
  14. /**
  15. * The boxplot series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes#boxplot
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. /**
  24. * A box plot is a convenient way of depicting groups of data through their
  25. * five-number summaries: the smallest observation (sample minimum), lower
  26. * quartile (Q1), median (Q2), upper quartile (Q3), and largest observation
  27. * (sample maximum).
  28. *
  29. * @sample highcharts/demo/box-plot/
  30. * Box plot
  31. *
  32. * @extends plotOptions.column
  33. * @excluding borderColor, borderRadius, borderWidth, groupZPadding, states
  34. * @product highcharts
  35. * @optionparent plotOptions.boxplot
  36. */
  37. seriesType('boxplot', 'column', {
  38. threshold: null,
  39. tooltip: {
  40. pointFormat:
  41. '<span style="color:{point.color}">\u25CF</span> <b> ' +
  42. '{series.name}</b><br/>' +
  43. 'Maximum: {point.high}<br/>' +
  44. 'Upper quartile: {point.q3}<br/>' +
  45. 'Median: {point.median}<br/>' +
  46. 'Lower quartile: {point.q1}<br/>' +
  47. 'Minimum: {point.low}<br/>'
  48. },
  49. /**
  50. * The length of the whiskers, the horizontal lines marking low and
  51. * high values. It can be a numerical pixel value, or a percentage
  52. * value of the box width. Set `0` to disable whiskers.
  53. *
  54. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  55. * True by default
  56. *
  57. * @type {number|string}
  58. * @since 3.0
  59. * @product highcharts
  60. */
  61. whiskerLength: '50%',
  62. /**
  63. * The fill color of the box.
  64. *
  65. * In styled mode, the fill color can be set with the
  66. * `.highcharts-boxplot-box` class.
  67. *
  68. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  69. * Box plot styling
  70. *
  71. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  72. * @default #ffffff
  73. * @since 3.0
  74. * @product highcharts
  75. */
  76. fillColor: '#ffffff',
  77. /**
  78. * The width of the line surrounding the box. If any of
  79. * [stemWidth](#plotOptions.boxplot.stemWidth),
  80. * [medianWidth](#plotOptions.boxplot.medianWidth)
  81. * or [whiskerWidth](#plotOptions.boxplot.whiskerWidth) are `null`,
  82. * the lineWidth also applies to these lines.
  83. *
  84. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  85. * Box plot styling
  86. * @sample {highcharts} highcharts/plotoptions/error-bar-styling/
  87. * Error bar styling
  88. *
  89. * @since 3.0
  90. * @product highcharts
  91. */
  92. lineWidth: 1,
  93. /**
  94. * The color of the median line. If `undefined`, the general series color
  95. * applies.
  96. *
  97. * In styled mode, the median stroke width can be set with the
  98. * `.highcharts-boxplot-median` class.
  99. *
  100. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  101. * Box plot styling
  102. * @sample {highcharts} highcharts/css/boxplot/
  103. * Box plot in styled mode
  104. * @sample {highcharts} highcharts/plotoptions/error-bar-styling/
  105. * Error bar styling
  106. *
  107. * @type {Highcharts.ColorString|Highcharts.GradientColorObject}
  108. * @since 3.0
  109. * @product highcharts
  110. * @apioption plotOptions.boxplot.medianColor
  111. */
  112. /**
  113. * The pixel width of the median line. If `null`, the
  114. * [lineWidth](#plotOptions.boxplot.lineWidth) is used.
  115. *
  116. * In styled mode, the median stroke width can be set with the
  117. * `.highcharts-boxplot-median` class.
  118. *
  119. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  120. * Box plot styling
  121. * @sample {highcharts} highcharts/css/boxplot/
  122. * Box plot in styled mode
  123. *
  124. * @since 3.0
  125. * @product highcharts
  126. */
  127. medianWidth: 2,
  128. /*
  129. // States are not working and are removed from docs.
  130. // Refer to: #2340
  131. states: {
  132. hover: {
  133. brightness: -0.3
  134. }
  135. },
  136. /**
  137. * The color of the stem, the vertical line extending from the box to
  138. * the whiskers. If `undefined`, the series color is used.
  139. *
  140. * In styled mode, the stem stroke can be set with the
  141. * `.highcharts-boxplot-stem` class.
  142. *
  143. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  144. * Box plot styling
  145. * @sample {highcharts} highcharts/css/boxplot/
  146. * Box plot in styled mode
  147. * @sample {highcharts} highcharts/plotoptions/error-bar-styling/
  148. * Error bar styling
  149. *
  150. * @type {Highcharts.ColorString}
  151. * @since 3.0
  152. * @product highcharts
  153. * @apioption plotOptions.boxplot.stemColor
  154. */
  155. /**
  156. * The dash style of the stem, the vertical line extending from the
  157. * box to the whiskers.
  158. *
  159. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  160. * Box plot styling
  161. * @sample {highcharts} highcharts/css/boxplot/
  162. * Box plot in styled mode
  163. * @sample {highcharts} highcharts/plotoptions/error-bar-styling/
  164. * Error bar styling
  165. *
  166. * @type {Highcharts.DashStyleType}
  167. * @default Solid
  168. * @since 3.0
  169. * @product highcharts
  170. * @apioption plotOptions.boxplot.stemDashStyle
  171. */
  172. /**
  173. * The width of the stem, the vertical line extending from the box to
  174. * the whiskers. If `undefined`, the width is inherited from the
  175. * [lineWidth](#plotOptions.boxplot.lineWidth) option.
  176. *
  177. * In styled mode, the stem stroke width can be set with the
  178. * `.highcharts-boxplot-stem` class.
  179. *
  180. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  181. * Box plot styling
  182. * @sample {highcharts} highcharts/css/boxplot/
  183. * Box plot in styled mode
  184. * @sample {highcharts} highcharts/plotoptions/error-bar-styling/
  185. * Error bar styling
  186. *
  187. * @type {number}
  188. * @since 3.0
  189. * @product highcharts
  190. * @apioption plotOptions.boxplot.stemWidth
  191. */
  192. /**
  193. * The color of the whiskers, the horizontal lines marking low and high
  194. * values. When `undefined`, the general series color is used.
  195. *
  196. * In styled mode, the whisker stroke can be set with the
  197. * `.highcharts-boxplot-whisker` class .
  198. *
  199. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  200. * Box plot styling
  201. * @sample {highcharts} highcharts/css/boxplot/
  202. * Box plot in styled mode
  203. *
  204. * @type {Highcharts.ColorString}
  205. * @since 3.0
  206. * @product highcharts
  207. * @apioption plotOptions.boxplot.whiskerColor
  208. */
  209. /**
  210. * The line width of the whiskers, the horizontal lines marking low and
  211. * high values. When `undefined`, the general
  212. * [lineWidth](#plotOptions.boxplot.lineWidth) applies.
  213. *
  214. * In styled mode, the whisker stroke width can be set with the
  215. * `.highcharts-boxplot-whisker` class.
  216. *
  217. * @sample {highcharts} highcharts/plotoptions/box-plot-styling/
  218. * Box plot styling
  219. * @sample {highcharts} highcharts/css/boxplot/
  220. * Box plot in styled mode
  221. *
  222. * @since 3.0
  223. * @product highcharts
  224. */
  225. whiskerWidth: 2
  226. }, /** @lends Highcharts.seriesTypes.boxplot */ {
  227. // array point configs are mapped to this
  228. pointArrayMap: ['low', 'q1', 'median', 'q3', 'high'],
  229. toYData: function (point) { // return a plain array for speedy calculation
  230. return [point.low, point.q1, point.median, point.q3, point.high];
  231. },
  232. // defines the top of the tracker
  233. pointValKey: 'high',
  234. // Get presentational attributes
  235. pointAttribs: function () {
  236. // No attributes should be set on point.graphic which is the group
  237. return {};
  238. },
  239. // Disable data labels for box plot
  240. drawDataLabels: noop,
  241. // Translate data points from raw values x and y to plotX and plotY
  242. translate: function () {
  243. var series = this,
  244. yAxis = series.yAxis,
  245. pointArrayMap = series.pointArrayMap;
  246. seriesTypes.column.prototype.translate.apply(series);
  247. // do the translation on each point dimension
  248. series.points.forEach(function (point) {
  249. pointArrayMap.forEach(function (key) {
  250. if (point[key] !== null) {
  251. point[key + 'Plot'] = yAxis.translate(
  252. point[key], 0, 1, 0, 1
  253. );
  254. }
  255. });
  256. });
  257. },
  258. // Draw the data points
  259. drawPoints: function () {
  260. var series = this,
  261. points = series.points,
  262. options = series.options,
  263. chart = series.chart,
  264. renderer = chart.renderer,
  265. q1Plot,
  266. q3Plot,
  267. highPlot,
  268. lowPlot,
  269. medianPlot,
  270. medianPath,
  271. crispCorr,
  272. crispX = 0,
  273. boxPath,
  274. width,
  275. left,
  276. right,
  277. halfWidth,
  278. // error bar inherits this series type but doesn't do quartiles
  279. doQuartiles = series.doQuartiles !== false,
  280. pointWiskerLength,
  281. whiskerLength = series.options.whiskerLength;
  282. points.forEach(function (point) {
  283. var graphic = point.graphic,
  284. verb = graphic ? 'animate' : 'attr',
  285. shapeArgs = point.shapeArgs,
  286. boxAttr = {},
  287. stemAttr = {},
  288. whiskersAttr = {},
  289. medianAttr = {},
  290. color = point.color || series.color;
  291. if (point.plotY !== undefined) {
  292. // crisp vector coordinates
  293. width = shapeArgs.width;
  294. left = Math.floor(shapeArgs.x);
  295. right = left + width;
  296. halfWidth = Math.round(width / 2);
  297. q1Plot = Math.floor(doQuartiles ? point.q1Plot : point.lowPlot);
  298. q3Plot = Math.floor(doQuartiles ? point.q3Plot : point.lowPlot);
  299. highPlot = Math.floor(point.highPlot);
  300. lowPlot = Math.floor(point.lowPlot);
  301. if (!graphic) {
  302. point.graphic = graphic = renderer.g('point')
  303. .add(series.group);
  304. point.stem = renderer.path()
  305. .addClass('highcharts-boxplot-stem')
  306. .add(graphic);
  307. if (whiskerLength) {
  308. point.whiskers = renderer.path()
  309. .addClass('highcharts-boxplot-whisker')
  310. .add(graphic);
  311. }
  312. if (doQuartiles) {
  313. point.box = renderer.path(boxPath)
  314. .addClass('highcharts-boxplot-box')
  315. .add(graphic);
  316. }
  317. point.medianShape = renderer.path(medianPath)
  318. .addClass('highcharts-boxplot-median')
  319. .add(graphic);
  320. }
  321. if (!chart.styledMode) {
  322. // Stem attributes
  323. stemAttr.stroke =
  324. point.stemColor || options.stemColor || color;
  325. stemAttr['stroke-width'] = pick(
  326. point.stemWidth,
  327. options.stemWidth,
  328. options.lineWidth
  329. );
  330. stemAttr.dashstyle =
  331. point.stemDashStyle || options.stemDashStyle;
  332. point.stem.attr(stemAttr);
  333. // Whiskers attributes
  334. if (whiskerLength) {
  335. whiskersAttr.stroke =
  336. point.whiskerColor || options.whiskerColor || color;
  337. whiskersAttr['stroke-width'] = pick(
  338. point.whiskerWidth,
  339. options.whiskerWidth,
  340. options.lineWidth
  341. );
  342. point.whiskers.attr(whiskersAttr);
  343. }
  344. if (doQuartiles) {
  345. boxAttr.fill = (
  346. point.fillColor ||
  347. options.fillColor ||
  348. color
  349. );
  350. boxAttr.stroke = options.lineColor || color;
  351. boxAttr['stroke-width'] = options.lineWidth || 0;
  352. point.box.attr(boxAttr);
  353. }
  354. // Median attributes
  355. medianAttr.stroke =
  356. point.medianColor || options.medianColor || color;
  357. medianAttr['stroke-width'] = pick(
  358. point.medianWidth,
  359. options.medianWidth,
  360. options.lineWidth
  361. );
  362. point.medianShape.attr(medianAttr);
  363. }
  364. // The stem
  365. crispCorr = (point.stem.strokeWidth() % 2) / 2;
  366. crispX = left + halfWidth + crispCorr;
  367. point.stem[verb]({
  368. d: [
  369. // stem up
  370. 'M',
  371. crispX, q3Plot,
  372. 'L',
  373. crispX, highPlot,
  374. // stem down
  375. 'M',
  376. crispX, q1Plot,
  377. 'L',
  378. crispX, lowPlot
  379. ]
  380. });
  381. // The box
  382. if (doQuartiles) {
  383. crispCorr = (point.box.strokeWidth() % 2) / 2;
  384. q1Plot = Math.floor(q1Plot) + crispCorr;
  385. q3Plot = Math.floor(q3Plot) + crispCorr;
  386. left += crispCorr;
  387. right += crispCorr;
  388. point.box[verb]({
  389. d: [
  390. 'M',
  391. left, q3Plot,
  392. 'L',
  393. left, q1Plot,
  394. 'L',
  395. right, q1Plot,
  396. 'L',
  397. right, q3Plot,
  398. 'L',
  399. left, q3Plot,
  400. 'z'
  401. ]
  402. });
  403. }
  404. // The whiskers
  405. if (whiskerLength) {
  406. crispCorr = (point.whiskers.strokeWidth() % 2) / 2;
  407. highPlot = highPlot + crispCorr;
  408. lowPlot = lowPlot + crispCorr;
  409. pointWiskerLength = (/%$/).test(whiskerLength) ?
  410. halfWidth * parseFloat(whiskerLength) / 100 :
  411. whiskerLength / 2;
  412. point.whiskers[verb]({
  413. d: [
  414. // High whisker
  415. 'M',
  416. crispX - pointWiskerLength,
  417. highPlot,
  418. 'L',
  419. crispX + pointWiskerLength,
  420. highPlot,
  421. // Low whisker
  422. 'M',
  423. crispX - pointWiskerLength,
  424. lowPlot,
  425. 'L',
  426. crispX + pointWiskerLength,
  427. lowPlot
  428. ]
  429. });
  430. }
  431. // The median
  432. medianPlot = Math.round(point.medianPlot);
  433. crispCorr = (point.medianShape.strokeWidth() % 2) / 2;
  434. medianPlot = medianPlot + crispCorr;
  435. point.medianShape[verb]({
  436. d: [
  437. 'M',
  438. left,
  439. medianPlot,
  440. 'L',
  441. right,
  442. medianPlot
  443. ]
  444. });
  445. }
  446. });
  447. },
  448. setStackedPoints: noop // #3890
  449. });
  450. /**
  451. * A `boxplot` series. If the [type](#series.boxplot.type) option is
  452. * not specified, it is inherited from [chart.type](#chart.type).
  453. *
  454. * @extends series,plotOptions.boxplot
  455. * @excluding dataParser, dataURL, marker, stack, stacking, states
  456. * @product highcharts
  457. * @apioption series.boxplot
  458. */
  459. /**
  460. * An array of data points for the series. For the `boxplot` series
  461. * type, points can be given in the following ways:
  462. *
  463. * 1. An array of arrays with 6 or 5 values. In this case, the values correspond
  464. * to `x,low,q1,median,q3,high`. If the first value is a string, it is
  465. * applied as the name of the point, and the `x` value is inferred. The `x`
  466. * value can also be omitted, in which case the inner arrays should be of
  467. * length 5. Then the `x` value is automatically calculated, either starting
  468. * at 0 and incremented by 1, or from `pointStart` and `pointInterval` given
  469. * in the series options.
  470. * ```js
  471. * data: [
  472. * [0, 3, 0, 10, 3, 5],
  473. * [1, 7, 8, 7, 2, 9],
  474. * [2, 6, 9, 5, 1, 3]
  475. * ]
  476. * ```
  477. *
  478. * 2. An array of objects with named values. The following snippet shows only a
  479. * few settings, see the complete options set below. If the total number of
  480. * data points exceeds the series'
  481. * [turboThreshold](#series.boxplot.turboThreshold), this option is not
  482. * available.
  483. * ```js
  484. * data: [{
  485. * x: 1,
  486. * low: 4,
  487. * q1: 9,
  488. * median: 9,
  489. * q3: 1,
  490. * high: 10,
  491. * name: "Point2",
  492. * color: "#00FF00"
  493. * }, {
  494. * x: 1,
  495. * low: 5,
  496. * q1: 7,
  497. * median: 3,
  498. * q3: 6,
  499. * high: 2,
  500. * name: "Point1",
  501. * color: "#FF00FF"
  502. * }]
  503. * ```
  504. *
  505. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  506. * Arrays of numeric x and y
  507. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  508. * Arrays of datetime x and y
  509. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  510. * Arrays of point.name and y
  511. * @sample {highcharts} highcharts/series/data-array-of-objects/
  512. * Config objects
  513. *
  514. * @type {Array<Array<(number|string),number,number,number,number>|Array<(number|string),number,number,number,number,number>|*>}
  515. * @extends series.line.data
  516. * @excluding marker
  517. * @product highcharts
  518. * @apioption series.boxplot.data
  519. */
  520. /**
  521. * The `high` value for each data point, signifying the highest value
  522. * in the sample set. The top whisker is drawn here.
  523. *
  524. * @type {number}
  525. * @product highcharts
  526. * @apioption series.boxplot.data.high
  527. */
  528. /**
  529. * The `low` value for each data point, signifying the lowest value
  530. * in the sample set. The bottom whisker is drawn here.
  531. *
  532. * @type {number}
  533. * @product highcharts
  534. * @apioption series.boxplot.data.low
  535. */
  536. /**
  537. * The median for each data point. This is drawn as a line through the
  538. * middle area of the box.
  539. *
  540. * @type {number}
  541. * @product highcharts
  542. * @apioption series.boxplot.data.median
  543. */
  544. /**
  545. * The lower quartile for each data point. This is the bottom of the
  546. * box.
  547. *
  548. * @type {number}
  549. * @product highcharts
  550. * @apioption series.boxplot.data.q1
  551. */
  552. /**
  553. * The higher quartile for each data point. This is the top of the box.
  554. *
  555. * @type {number}
  556. * @product highcharts
  557. * @apioption series.boxplot.data.q3
  558. */