HeatmapSeries.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. import '../parts/Point.js';
  11. import '../parts/Series.js';
  12. import '../parts/Legend.js';
  13. import './ColorSeriesMixin.js';
  14. var colorPointMixin = H.colorPointMixin,
  15. colorSeriesMixin = H.colorSeriesMixin,
  16. LegendSymbolMixin = H.LegendSymbolMixin,
  17. merge = H.merge,
  18. noop = H.noop,
  19. pick = H.pick,
  20. Series = H.Series,
  21. seriesType = H.seriesType,
  22. seriesTypes = H.seriesTypes;
  23. /**
  24. * @private
  25. * @class
  26. * @name Highcharts.seriesTypes.heatmap
  27. *
  28. * @augments Highcharts.Series
  29. */
  30. seriesType(
  31. 'heatmap',
  32. 'scatter'
  33. /**
  34. * A heatmap is a graphical representation of data where the individual
  35. * values contained in a matrix are represented as colors.
  36. *
  37. * @sample highcharts/demo/heatmap/
  38. * Simple heatmap
  39. * @sample highcharts/demo/heatmap-canvas/
  40. * Heavy heatmap
  41. *
  42. * @extends plotOptions.scatter
  43. * @excluding animationLimit, connectEnds, connectNulls, dashStyle,
  44. * findNearestPointBy, getExtremesFromAll, jitter, linecap,
  45. * lineWidth, marker, pointInterval, pointIntervalUnit,
  46. * pointRange, pointStart, shadow, softThreshold, stacking,
  47. * step, threshold
  48. * @product highcharts highmaps
  49. * @optionparent plotOptions.heatmap
  50. */
  51. , {
  52. /**
  53. * Animation is disabled by default on the heatmap series.
  54. */
  55. animation: false,
  56. /**
  57. * The border width for each heat map item.
  58. */
  59. borderWidth: 0,
  60. /**
  61. * Padding between the points in the heatmap.
  62. *
  63. * @type {number}
  64. * @default 0
  65. * @since 6.0
  66. * @apioption plotOptions.heatmap.pointPadding
  67. */
  68. /**
  69. * The main color of the series. In heat maps this color is rarely used,
  70. * as we mostly use the color to denote the value of each point. Unless
  71. * options are set in the [colorAxis](#colorAxis), the default value
  72. * is pulled from the [options.colors](#colors) array.
  73. *
  74. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  75. * @since 4.0
  76. * @product highcharts
  77. * @apioption plotOptions.heatmap.color
  78. */
  79. /**
  80. * The column size - how many X axis units each column in the heatmap
  81. * should span.
  82. *
  83. * @sample {highcharts} maps/demo/heatmap/
  84. * One day
  85. * @sample {highmaps} maps/demo/heatmap/
  86. * One day
  87. *
  88. * @type {number}
  89. * @default 1
  90. * @since 4.0
  91. * @product highcharts highmaps
  92. * @apioption plotOptions.heatmap.colsize
  93. */
  94. /**
  95. * The row size - how many Y axis units each heatmap row should span.
  96. *
  97. * @sample {highcharts} maps/demo/heatmap/
  98. * 1 by default
  99. * @sample {highmaps} maps/demo/heatmap/
  100. * 1 by default
  101. *
  102. * @type {number}
  103. * @default 1
  104. * @since 4.0
  105. * @product highcharts highmaps
  106. * @apioption plotOptions.heatmap.rowsize
  107. */
  108. /**
  109. * The color applied to null points. In styled mode, a general CSS class
  110. * is applied instead.
  111. *
  112. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  113. */
  114. nullColor: '#f7f7f7',
  115. dataLabels: {
  116. formatter: function () { // #2945
  117. return this.point.value;
  118. },
  119. inside: true,
  120. verticalAlign: 'middle',
  121. crop: false,
  122. overflow: false,
  123. padding: 0 // #3837
  124. },
  125. /** @ignore */
  126. marker: null,
  127. /**
  128. * @ignore
  129. */
  130. pointRange: null, // dynamically set to colsize by default
  131. tooltip: {
  132. pointFormat: '{point.x}, {point.y}: {point.value}<br/>'
  133. },
  134. states: {
  135. hover: {
  136. /** @ignore */
  137. halo: false, // #3406, halo is disabled on heatmaps by default
  138. /**
  139. * How much to brighten the point on interaction. Requires the main
  140. * color to be defined in hex or rgb(a) format.
  141. *
  142. * In styled mode, the hover brightening is by default replaced with
  143. * a fill-opacity set in the `.highcharts-point:hover` rule.
  144. */
  145. brightness: 0.2
  146. }
  147. }
  148. }, merge(colorSeriesMixin, {
  149. pointArrayMap: ['y', 'value'],
  150. hasPointSpecificOptions: true,
  151. getExtremesFromAll: true,
  152. directTouch: true,
  153. /**
  154. * Override the init method to add point ranges on both axes.
  155. *
  156. * @private
  157. * @function Highcharts.seriesTypes.heatmap#init
  158. */
  159. init: function () {
  160. var options;
  161. seriesTypes.scatter.prototype.init.apply(this, arguments);
  162. options = this.options;
  163. // #3758, prevent resetting in setData
  164. options.pointRange = pick(options.pointRange, options.colsize || 1);
  165. // general point range
  166. this.yAxis.axisPointRange = options.rowsize || 1;
  167. },
  168. /**
  169. * @private
  170. * @function Highcharts.seriesTypes.heatmap#translate
  171. */
  172. translate: function () {
  173. var series = this,
  174. options = series.options,
  175. xAxis = series.xAxis,
  176. yAxis = series.yAxis,
  177. seriesPointPadding = options.pointPadding || 0,
  178. between = function (x, a, b) {
  179. return Math.min(Math.max(a, x), b);
  180. },
  181. pointPlacement = series.pointPlacementToXValue(); // #7860
  182. series.generatePoints();
  183. series.points.forEach(function (point) {
  184. var xPad = (options.colsize || 1) / 2,
  185. yPad = (options.rowsize || 1) / 2,
  186. x1 = between(
  187. Math.round(
  188. xAxis.len -
  189. xAxis.translate(point.x - xPad,
  190. 0,
  191. 1,
  192. 0,
  193. 1,
  194. -pointPlacement)
  195. ),
  196. -xAxis.len, 2 * xAxis.len
  197. ),
  198. x2 = between(
  199. Math.round(
  200. xAxis.len -
  201. xAxis.translate(point.x + xPad,
  202. 0,
  203. 1,
  204. 0,
  205. 1,
  206. -pointPlacement)
  207. ),
  208. -xAxis.len, 2 * xAxis.len
  209. ),
  210. y1 = between(
  211. Math.round(yAxis.translate(point.y - yPad, 0, 1, 0, 1)),
  212. -yAxis.len, 2 * yAxis.len
  213. ),
  214. y2 = between(
  215. Math.round(yAxis.translate(point.y + yPad, 0, 1, 0, 1)),
  216. -yAxis.len, 2 * yAxis.len
  217. ),
  218. pointPadding = pick(point.pointPadding, seriesPointPadding);
  219. // Set plotX and plotY for use in K-D-Tree and more
  220. point.plotX = point.clientX = (x1 + x2) / 2;
  221. point.plotY = (y1 + y2) / 2;
  222. point.shapeType = 'rect';
  223. point.shapeArgs = {
  224. x: Math.min(x1, x2) + pointPadding,
  225. y: Math.min(y1, y2) + pointPadding,
  226. width: Math.abs(x2 - x1) - pointPadding * 2,
  227. height: Math.abs(y2 - y1) - pointPadding * 2
  228. };
  229. });
  230. series.translateColors();
  231. },
  232. /**
  233. * @private
  234. * @function Highcharts.seriesTypes.heatmap#drawPoints
  235. */
  236. drawPoints: function () {
  237. // In styled mode, use CSS, otherwise the fill used in the style
  238. // sheet will take precedence over the fill attribute.
  239. var func = this.chart.styledMode ? 'css' : 'attr';
  240. seriesTypes.column.prototype.drawPoints.call(this);
  241. this.points.forEach(function (point) {
  242. point.graphic[func](this.colorAttribs(point));
  243. }, this);
  244. },
  245. /**
  246. * @ignore
  247. * @deprecated
  248. * @function Highcharts.seriesTypes.heatmap#animate
  249. */
  250. animate: noop,
  251. /**
  252. * @ignore
  253. * @deprecated
  254. * @function Highcharts.seriesTypes.heatmap#getBox
  255. */
  256. getBox: noop,
  257. /**
  258. * @private
  259. * @borrows Highcharts.LegendSymbolMixin.drawRectangle as Highcharts.seriesTypes.heatmap#drawLegendSymbol
  260. */
  261. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  262. /**
  263. * @private
  264. * @borrows Highcharts.seriesTypes.column#alignDataLabel as Highcharts.seriesTypes.heatmap#alignDataLabel
  265. */
  266. alignDataLabel: seriesTypes.column.prototype.alignDataLabel,
  267. /**
  268. * @private
  269. * @function Highcharts.seriesTypes.heatmap#getExtremes
  270. */
  271. getExtremes: function () {
  272. // Get the extremes from the value data
  273. Series.prototype.getExtremes.call(this, this.valueData);
  274. this.valueMin = this.dataMin;
  275. this.valueMax = this.dataMax;
  276. // Get the extremes from the y data
  277. Series.prototype.getExtremes.call(this);
  278. }
  279. }), H.extend({
  280. /**
  281. * @private
  282. * @function Highcharts.Point#haloPath
  283. *
  284. * @param {number} size
  285. *
  286. * @return {Highcharts.SVGPathArray}
  287. */
  288. haloPath: function (size) {
  289. if (!size) {
  290. return [];
  291. }
  292. var rect = this.shapeArgs;
  293. return [
  294. 'M', rect.x - size, rect.y - size,
  295. 'L', rect.x - size, rect.y + rect.height + size,
  296. rect.x + rect.width + size, rect.y + rect.height + size,
  297. rect.x + rect.width + size, rect.y - size,
  298. 'Z'
  299. ];
  300. }
  301. }, colorPointMixin)
  302. );
  303. /**
  304. * A `heatmap` series. If the [type](#series.heatmap.type) option is
  305. * not specified, it is inherited from [chart.type](#chart.type).
  306. *
  307. * @extends series,plotOptions.heatmap
  308. * @excluding dataParser, dataURL, marker, pointRange, stack
  309. * @product highcharts highmaps
  310. * @apioption series.heatmap
  311. */
  312. /**
  313. * An array of data points for the series. For the `heatmap` series
  314. * type, points can be given in the following ways:
  315. *
  316. * 1. An array of arrays with 3 or 2 values. In this case, the values
  317. * correspond to `x,y,value`. If the first value is a string, it is
  318. * applied as the name of the point, and the `x` value is inferred.
  319. * The `x` value can also be omitted, in which case the inner arrays
  320. * should be of length 2\. Then the `x` value is automatically calculated,
  321. * either starting at 0 and incremented by 1, or from `pointStart`
  322. * and `pointInterval` given in the series options.
  323. *
  324. * ```js
  325. * data: [
  326. * [0, 9, 7],
  327. * [1, 10, 4],
  328. * [2, 6, 3]
  329. * ]
  330. * ```
  331. *
  332. * 2. An array of objects with named values. The following snippet shows only a
  333. * few settings, see the complete options set below. If the total number of data
  334. * points exceeds the series' [turboThreshold](#series.heatmap.turboThreshold),
  335. * this option is not available.
  336. *
  337. * ```js
  338. * data: [{
  339. * x: 1,
  340. * y: 3,
  341. * value: 10,
  342. * name: "Point2",
  343. * color: "#00FF00"
  344. * }, {
  345. * x: 1,
  346. * y: 7,
  347. * value: 10,
  348. * name: "Point1",
  349. * color: "#FF00FF"
  350. * }]
  351. * ```
  352. *
  353. * @sample {highcharts} highcharts/chart/reflow-true/
  354. * Numerical values
  355. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  356. * Arrays of numeric x and y
  357. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  358. * Arrays of datetime x and y
  359. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  360. * Arrays of point.name and y
  361. * @sample {highcharts} highcharts/series/data-array-of-objects/
  362. * Config objects
  363. *
  364. * @type {Array<Array<number>|*>}
  365. * @extends series.line.data
  366. * @excluding marker
  367. * @product highcharts highmaps
  368. * @apioption series.heatmap.data
  369. */
  370. /**
  371. * The color of the point. In heat maps the point color is rarely set
  372. * explicitly, as we use the color to denote the `value`. Options for
  373. * this are set in the [colorAxis](#colorAxis) configuration.
  374. *
  375. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  376. * @product highcharts highmaps
  377. * @apioption series.heatmap.data.color
  378. */
  379. /**
  380. * The value of the point, resulting in a color controled by options
  381. * as set in the [colorAxis](#colorAxis) configuration.
  382. *
  383. * @type {number}
  384. * @product highcharts highmaps
  385. * @apioption series.heatmap.data.value
  386. */
  387. /**
  388. * The x value of the point. For datetime axes,
  389. * the X value is the timestamp in milliseconds since 1970.
  390. *
  391. * @type {number}
  392. * @product highcharts highmaps
  393. * @apioption series.heatmap.data.x
  394. */
  395. /**
  396. * The y value of the point.
  397. *
  398. * @type {number}
  399. * @product highcharts highmaps
  400. * @apioption series.heatmap.data.y
  401. */
  402. /**
  403. * Point padding for a single point.
  404. *
  405. * @sample maps/plotoptions/tilemap-pointpadding
  406. * Point padding on tiles
  407. *
  408. * @type {number}
  409. * @product highcharts highmaps
  410. * @apioption series.heatmap.data.pointPadding
  411. */