MapSeries.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 ColorMapMixin from '../Mixins/ColorMapSeries.js';
  12. var colorMapPointMixin = ColorMapMixin.colorMapPointMixin, colorMapSeriesMixin = ColorMapMixin.colorMapSeriesMixin;
  13. import H from '../Core/Globals.js';
  14. var noop = H.noop;
  15. import LegendSymbolMixin from '../Mixins/LegendSymbol.js';
  16. import mapModule from '../Maps/Map.js';
  17. var maps = mapModule.maps, splitPath = mapModule.splitPath;
  18. import Point from '../Core/Series/Point.js';
  19. import SVGRenderer from '../Core/Renderer/SVG/SVGRenderer.js';
  20. import U from '../Core/Utilities.js';
  21. var extend = U.extend, fireEvent = U.fireEvent, getNestedProperty = U.getNestedProperty, isArray = U.isArray, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, pick = U.pick, splat = U.splat;
  22. import '../Core/Options.js';
  23. import '../Series/LineSeries.js';
  24. import './ScatterSeries.js';
  25. var Series = H.Series, seriesTypes = BaseSeries.seriesTypes;
  26. /**
  27. * @private
  28. * @class
  29. * @name Highcharts.seriesTypes.map
  30. *
  31. * @augments Highcharts.Series
  32. */
  33. BaseSeries.seriesType('map', 'scatter',
  34. /**
  35. * The map series is used for basic choropleth maps, where each map area has
  36. * a color based on its value.
  37. *
  38. * @sample maps/demo/all-maps/
  39. * Choropleth map
  40. *
  41. * @extends plotOptions.scatter
  42. * @excluding marker, cluster
  43. * @product highmaps
  44. * @optionparent plotOptions.map
  45. */
  46. {
  47. animation: false,
  48. dataLabels: {
  49. crop: false,
  50. formatter: function () {
  51. return this.point.value;
  52. },
  53. inside: true,
  54. overflow: false,
  55. padding: 0,
  56. verticalAlign: 'middle'
  57. },
  58. /**
  59. * @ignore-option
  60. *
  61. * @private
  62. */
  63. marker: null,
  64. /**
  65. * The color to apply to null points.
  66. *
  67. * In styled mode, the null point fill is set in the
  68. * `.highcharts-null-point` class.
  69. *
  70. * @sample maps/demo/all-areas-as-null/
  71. * Null color
  72. *
  73. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  74. *
  75. * @private
  76. */
  77. nullColor: '#f7f7f7',
  78. /**
  79. * Whether to allow pointer interaction like tooltips and mouse events
  80. * on null points.
  81. *
  82. * @type {boolean}
  83. * @since 4.2.7
  84. * @apioption plotOptions.map.nullInteraction
  85. *
  86. * @private
  87. */
  88. stickyTracking: false,
  89. tooltip: {
  90. followPointer: true,
  91. pointFormat: '{point.name}: {point.value}<br/>'
  92. },
  93. /**
  94. * @ignore-option
  95. *
  96. * @private
  97. */
  98. turboThreshold: 0,
  99. /**
  100. * Whether all areas of the map defined in `mapData` should be rendered.
  101. * If `true`, areas which don't correspond to a data point, are rendered
  102. * as `null` points. If `false`, those areas are skipped.
  103. *
  104. * @sample maps/plotoptions/series-allareas-false/
  105. * All areas set to false
  106. *
  107. * @type {boolean}
  108. * @default true
  109. * @product highmaps
  110. * @apioption plotOptions.series.allAreas
  111. *
  112. * @private
  113. */
  114. allAreas: true,
  115. /**
  116. * The border color of the map areas.
  117. *
  118. * In styled mode, the border stroke is given in the `.highcharts-point`
  119. * class.
  120. *
  121. * @sample {highmaps} maps/plotoptions/series-border/
  122. * Borders demo
  123. *
  124. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  125. * @default #cccccc
  126. * @product highmaps
  127. * @apioption plotOptions.series.borderColor
  128. *
  129. * @private
  130. */
  131. borderColor: '#cccccc',
  132. /**
  133. * The border width of each map area.
  134. *
  135. * In styled mode, the border stroke width is given in the
  136. * `.highcharts-point` class.
  137. *
  138. * @sample maps/plotoptions/series-border/
  139. * Borders demo
  140. *
  141. * @type {number}
  142. * @default 1
  143. * @product highmaps
  144. * @apioption plotOptions.series.borderWidth
  145. *
  146. * @private
  147. */
  148. borderWidth: 1,
  149. /**
  150. * @type {string}
  151. * @default value
  152. * @apioption plotOptions.map.colorKey
  153. */
  154. /**
  155. * What property to join the `mapData` to the value data. For example,
  156. * if joinBy is "code", the mapData items with a specific code is merged
  157. * into the data with the same code. For maps loaded from GeoJSON, the
  158. * keys may be held in each point's `properties` object.
  159. *
  160. * The joinBy option can also be an array of two values, where the first
  161. * points to a key in the `mapData`, and the second points to another
  162. * key in the `data`.
  163. *
  164. * When joinBy is `null`, the map items are joined by their position in
  165. * the array, which performs much better in maps with many data points.
  166. * This is the recommended option if you are printing more than a
  167. * thousand data points and have a backend that can preprocess the data
  168. * into a parallel array of the mapData.
  169. *
  170. * @sample maps/plotoptions/series-border/
  171. * Joined by "code"
  172. * @sample maps/demo/geojson/
  173. * GeoJSON joined by an array
  174. * @sample maps/series/joinby-null/
  175. * Simple data joined by null
  176. *
  177. * @type {string|Array<string>}
  178. * @default hc-key
  179. * @product highmaps
  180. * @apioption plotOptions.series.joinBy
  181. */
  182. joinBy: 'hc-key',
  183. /**
  184. * Define the z index of the series.
  185. *
  186. * @type {number}
  187. * @product highmaps
  188. * @apioption plotOptions.series.zIndex
  189. */
  190. /**
  191. * @apioption plotOptions.series.states
  192. *
  193. * @private
  194. */
  195. states: {
  196. /**
  197. * @apioption plotOptions.series.states.hover
  198. */
  199. hover: {
  200. /** @ignore-option */
  201. halo: null,
  202. /**
  203. * The color of the shape in this state.
  204. *
  205. * @sample maps/plotoptions/series-states-hover/
  206. * Hover options
  207. *
  208. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  209. * @product highmaps
  210. * @apioption plotOptions.series.states.hover.color
  211. */
  212. /**
  213. * The border color of the point in this state.
  214. *
  215. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  216. * @product highmaps
  217. * @apioption plotOptions.series.states.hover.borderColor
  218. */
  219. /**
  220. * The border width of the point in this state
  221. *
  222. * @type {number}
  223. * @product highmaps
  224. * @apioption plotOptions.series.states.hover.borderWidth
  225. */
  226. /**
  227. * The relative brightness of the point when hovered, relative
  228. * to the normal point color.
  229. *
  230. * @type {number}
  231. * @product highmaps
  232. * @default 0.2
  233. * @apioption plotOptions.series.states.hover.brightness
  234. */
  235. brightness: 0.2
  236. },
  237. /**
  238. * @apioption plotOptions.series.states.normal
  239. */
  240. normal: {
  241. /**
  242. * @productdesc {highmaps}
  243. * The animation adds some latency in order to reduce the effect
  244. * of flickering when hovering in and out of for example an
  245. * uneven coastline.
  246. *
  247. * @sample {highmaps} maps/plotoptions/series-states-animation-false/
  248. * No animation of fill color
  249. *
  250. * @apioption plotOptions.series.states.normal.animation
  251. */
  252. animation: true
  253. },
  254. /**
  255. * @apioption plotOptions.series.states.select
  256. */
  257. select: {
  258. /**
  259. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  260. * @default #cccccc
  261. * @product highmaps
  262. * @apioption plotOptions.series.states.select.color
  263. */
  264. color: '#cccccc'
  265. },
  266. inactive: {
  267. opacity: 1
  268. }
  269. }
  270. // Prototype members
  271. }, merge(colorMapSeriesMixin, {
  272. type: 'map',
  273. getExtremesFromAll: true,
  274. useMapGeometry: true,
  275. forceDL: true,
  276. searchPoint: noop,
  277. // When tooltip is not shared, this series (and derivatives) requires
  278. // direct touch/hover. KD-tree does not apply.
  279. directTouch: true,
  280. // X axis and Y axis must have same translation slope
  281. preserveAspectRatio: true,
  282. pointArrayMap: ['value'],
  283. // Extend setOptions by picking up the joinBy option and applying it
  284. // to a series property
  285. setOptions: function (itemOptions) {
  286. var options = Series.prototype.setOptions.call(this, itemOptions), joinBy = options.joinBy, joinByNull = joinBy === null;
  287. if (joinByNull) {
  288. joinBy = '_i';
  289. }
  290. joinBy = this.joinBy = splat(joinBy);
  291. if (!joinBy[1]) {
  292. joinBy[1] = joinBy[0];
  293. }
  294. return options;
  295. },
  296. // Get the bounding box of all paths in the map combined.
  297. getBox: function (paths) {
  298. var MAX_VALUE = Number.MAX_VALUE, maxX = -MAX_VALUE, minX = MAX_VALUE, maxY = -MAX_VALUE, minY = MAX_VALUE, minRange = MAX_VALUE, xAxis = this.xAxis, yAxis = this.yAxis, hasBox;
  299. // Find the bounding box
  300. (paths || []).forEach(function (point) {
  301. if (point.path) {
  302. if (typeof point.path === 'string') {
  303. point.path = splitPath(point.path);
  304. // Legacy one-dimensional array
  305. }
  306. else if (point.path[0] === 'M') {
  307. point.path = SVGRenderer.prototype.pathToSegments(point.path);
  308. }
  309. var path = point.path || [], pointMaxX = -MAX_VALUE, pointMinX = MAX_VALUE, pointMaxY = -MAX_VALUE, pointMinY = MAX_VALUE, properties = point.properties;
  310. // The first time a map point is used, analyze its box
  311. if (!point._foundBox) {
  312. path.forEach(function (seg) {
  313. var x = seg[seg.length - 2];
  314. var y = seg[seg.length - 1];
  315. if (typeof x === 'number' && typeof y === 'number') {
  316. pointMinX = Math.min(pointMinX, x);
  317. pointMaxX = Math.max(pointMaxX, x);
  318. pointMinY = Math.min(pointMinY, y);
  319. pointMaxY = Math.max(pointMaxY, y);
  320. }
  321. });
  322. // Cache point bounding box for use to position data
  323. // labels, bubbles etc
  324. point._midX = (pointMinX + (pointMaxX - pointMinX) * pick(point.middleX, properties &&
  325. properties['hc-middle-x'], 0.5));
  326. point._midY = (pointMinY + (pointMaxY - pointMinY) * pick(point.middleY, properties &&
  327. properties['hc-middle-y'], 0.5));
  328. point._maxX = pointMaxX;
  329. point._minX = pointMinX;
  330. point._maxY = pointMaxY;
  331. point._minY = pointMinY;
  332. point.labelrank = pick(point.labelrank, (pointMaxX - pointMinX) * (pointMaxY - pointMinY));
  333. point._foundBox = true;
  334. }
  335. maxX = Math.max(maxX, point._maxX);
  336. minX = Math.min(minX, point._minX);
  337. maxY = Math.max(maxY, point._maxY);
  338. minY = Math.min(minY, point._minY);
  339. minRange = Math.min(point._maxX - point._minX, point._maxY - point._minY, minRange);
  340. hasBox = true;
  341. }
  342. });
  343. // Set the box for the whole series
  344. if (hasBox) {
  345. this.minY = Math.min(minY, pick(this.minY, MAX_VALUE));
  346. this.maxY = Math.max(maxY, pick(this.maxY, -MAX_VALUE));
  347. this.minX = Math.min(minX, pick(this.minX, MAX_VALUE));
  348. this.maxX = Math.max(maxX, pick(this.maxX, -MAX_VALUE));
  349. // If no minRange option is set, set the default minimum zooming
  350. // range to 5 times the size of the smallest element
  351. if (xAxis && typeof xAxis.options.minRange === 'undefined') {
  352. xAxis.minRange = Math.min(5 * minRange, (this.maxX - this.minX) / 5, xAxis.minRange || MAX_VALUE);
  353. }
  354. if (yAxis && typeof yAxis.options.minRange === 'undefined') {
  355. yAxis.minRange = Math.min(5 * minRange, (this.maxY - this.minY) / 5, yAxis.minRange || MAX_VALUE);
  356. }
  357. }
  358. },
  359. // Define hasData function for non-cartesian series.
  360. // Returns true if the series has points at all.
  361. hasData: function () {
  362. return !!this.processedXData.length; // != 0
  363. },
  364. getExtremes: function () {
  365. // Get the actual value extremes for colors
  366. var _a = Series.prototype.getExtremes
  367. .call(this, this.valueData), dataMin = _a.dataMin, dataMax = _a.dataMax;
  368. // Recalculate box on updated data
  369. if (this.chart.hasRendered && this.isDirtyData) {
  370. this.getBox(this.options.data);
  371. }
  372. if (isNumber(dataMin)) {
  373. this.valueMin = dataMin;
  374. }
  375. if (isNumber(dataMax)) {
  376. this.valueMax = dataMax;
  377. }
  378. // Extremes for the mock Y axis
  379. return { dataMin: this.minY, dataMax: this.maxY };
  380. },
  381. // Translate the path, so it automatically fits into the plot area box
  382. translatePath: function (path) {
  383. var series = this, xAxis = series.xAxis, yAxis = series.yAxis, xMin = xAxis.min, xTransA = xAxis.transA, xMinPixelPadding = xAxis.minPixelPadding, yMin = yAxis.min, yTransA = yAxis.transA, yMinPixelPadding = yAxis.minPixelPadding, ret = []; // Preserve the original
  384. // Do the translation
  385. if (path) {
  386. path.forEach(function (seg) {
  387. if (seg[0] === 'M') {
  388. ret.push([
  389. 'M',
  390. (seg[1] - (xMin || 0)) * xTransA + xMinPixelPadding,
  391. (seg[2] - (yMin || 0)) * yTransA + yMinPixelPadding
  392. ]);
  393. }
  394. else if (seg[0] === 'L') {
  395. ret.push([
  396. 'L',
  397. (seg[1] - (xMin || 0)) * xTransA + xMinPixelPadding,
  398. (seg[2] - (yMin || 0)) * yTransA + yMinPixelPadding
  399. ]);
  400. }
  401. else if (seg[0] === 'C') {
  402. ret.push([
  403. 'C',
  404. (seg[1] - (xMin || 0)) * xTransA + xMinPixelPadding,
  405. (seg[2] - (yMin || 0)) * yTransA + yMinPixelPadding,
  406. (seg[3] - (xMin || 0)) * xTransA + xMinPixelPadding,
  407. (seg[4] - (yMin || 0)) * yTransA + yMinPixelPadding,
  408. (seg[5] - (xMin || 0)) * xTransA + xMinPixelPadding,
  409. (seg[6] - (yMin || 0)) * yTransA + yMinPixelPadding
  410. ]);
  411. }
  412. else if (seg[0] === 'Q') {
  413. ret.push([
  414. 'Q',
  415. (seg[1] - (xMin || 0)) * xTransA + xMinPixelPadding,
  416. (seg[2] - (yMin || 0)) * yTransA + yMinPixelPadding,
  417. (seg[3] - (xMin || 0)) * xTransA + xMinPixelPadding,
  418. (seg[4] - (yMin || 0)) * yTransA + yMinPixelPadding
  419. ]);
  420. }
  421. else if (seg[0] === 'Z') {
  422. ret.push(['Z']);
  423. }
  424. });
  425. }
  426. return ret;
  427. },
  428. // Extend setData to join in mapData. If the allAreas option is true,
  429. // all areas from the mapData are used, and those that don't correspond
  430. // to a data value are given null values.
  431. setData: function (data, redraw, animation, updatePoints) {
  432. var options = this.options, chartOptions = this.chart.options.chart, globalMapData = chartOptions && chartOptions.map, mapData = options.mapData, joinBy = this.joinBy, pointArrayMap = options.keys || this.pointArrayMap, dataUsed = [], mapMap = {}, mapPoint, mapTransforms = this.chart.mapTransforms, props, i;
  433. // Collect mapData from chart options if not defined on series
  434. if (!mapData && globalMapData) {
  435. mapData = typeof globalMapData === 'string' ?
  436. maps[globalMapData] :
  437. globalMapData;
  438. }
  439. // Pick up numeric values, add index
  440. // Convert Array point definitions to objects using pointArrayMap
  441. if (data) {
  442. data.forEach(function (val, i) {
  443. var ix = 0;
  444. if (isNumber(val)) {
  445. data[i] = {
  446. value: val
  447. };
  448. }
  449. else if (isArray(val)) {
  450. data[i] = {};
  451. // Automatically copy first item to hc-key if there is
  452. // an extra leading string
  453. if (!options.keys &&
  454. val.length > pointArrayMap.length &&
  455. typeof val[0] === 'string') {
  456. data[i]['hc-key'] = val[0];
  457. ++ix;
  458. }
  459. // Run through pointArrayMap and what's left of the
  460. // point data array in parallel, copying over the values
  461. for (var j = 0; j < pointArrayMap.length; ++j, ++ix) {
  462. if (pointArrayMap[j] &&
  463. typeof val[ix] !== 'undefined') {
  464. if (pointArrayMap[j].indexOf('.') > 0) {
  465. Point.prototype.setNestedProperty(data[i], val[ix], pointArrayMap[j]);
  466. }
  467. else {
  468. data[i][pointArrayMap[j]] =
  469. val[ix];
  470. }
  471. }
  472. }
  473. }
  474. if (joinBy && joinBy[0] === '_i') {
  475. data[i]._i = i;
  476. }
  477. });
  478. }
  479. this.getBox(data);
  480. // Pick up transform definitions for chart
  481. this.chart.mapTransforms = mapTransforms =
  482. chartOptions && chartOptions.mapTransforms ||
  483. mapData && mapData['hc-transform'] ||
  484. mapTransforms;
  485. // Cache cos/sin of transform rotation angle
  486. if (mapTransforms) {
  487. objectEach(mapTransforms, function (transform) {
  488. if (transform.rotation) {
  489. transform.cosAngle = Math.cos(transform.rotation);
  490. transform.sinAngle = Math.sin(transform.rotation);
  491. }
  492. });
  493. }
  494. if (mapData) {
  495. if (mapData.type === 'FeatureCollection') {
  496. this.mapTitle = mapData.title;
  497. mapData = H.geojson(mapData, this.type, this);
  498. }
  499. this.mapData = mapData;
  500. this.mapMap = {};
  501. for (i = 0; i < mapData.length; i++) {
  502. mapPoint = mapData[i];
  503. props = mapPoint.properties;
  504. mapPoint._i = i;
  505. // Copy the property over to root for faster access
  506. if (joinBy[0] && props && props[joinBy[0]]) {
  507. mapPoint[joinBy[0]] = props[joinBy[0]];
  508. }
  509. mapMap[mapPoint[joinBy[0]]] = mapPoint;
  510. }
  511. this.mapMap = mapMap;
  512. // Registered the point codes that actually hold data
  513. if (data && joinBy[1]) {
  514. var joinKey_1 = joinBy[1];
  515. data.forEach(function (pointOptions) {
  516. var mapKey = getNestedProperty(joinKey_1, pointOptions);
  517. if (mapMap[mapKey]) {
  518. dataUsed.push(mapMap[mapKey]);
  519. }
  520. });
  521. }
  522. if (options.allAreas) {
  523. this.getBox(mapData);
  524. data = data || [];
  525. // Registered the point codes that actually hold data
  526. if (joinBy[1]) {
  527. var joinKey_2 = joinBy[1];
  528. data.forEach(function (pointOptions) {
  529. dataUsed.push(getNestedProperty(joinKey_2, pointOptions));
  530. });
  531. }
  532. // Add those map points that don't correspond to data, which
  533. // will be drawn as null points
  534. dataUsed = ('|' + dataUsed.map(function (point) {
  535. return point && point[joinBy[0]];
  536. }).join('|') + '|'); // Faster than array.indexOf
  537. mapData.forEach(function (mapPoint) {
  538. if (!joinBy[0] ||
  539. dataUsed.indexOf('|' + mapPoint[joinBy[0]] + '|') === -1) {
  540. data.push(merge(mapPoint, { value: null }));
  541. // #5050 - adding all areas causes the update
  542. // optimization of setData to kick in, even though
  543. // the point order has changed
  544. updatePoints = false;
  545. }
  546. });
  547. }
  548. else {
  549. this.getBox(dataUsed); // Issue #4784
  550. }
  551. }
  552. Series.prototype.setData.call(this, data, redraw, animation, updatePoints);
  553. },
  554. // No graph for the map series
  555. drawGraph: noop,
  556. // We need the points' bounding boxes in order to draw the data labels,
  557. // so we skip it now and call it from drawPoints instead.
  558. drawDataLabels: noop,
  559. // Allow a quick redraw by just translating the area group. Used for
  560. // zooming and panning in capable browsers.
  561. doFullTranslate: function () {
  562. return (this.isDirtyData ||
  563. this.chart.isResizing ||
  564. this.chart.renderer.isVML ||
  565. !this.baseTrans);
  566. },
  567. // Add the path option for data points. Find the max value for color
  568. // calculation.
  569. translate: function () {
  570. var series = this, xAxis = series.xAxis, yAxis = series.yAxis, doFullTranslate = series.doFullTranslate();
  571. series.generatePoints();
  572. series.data.forEach(function (point) {
  573. // Record the middle point (loosely based on centroid),
  574. // determined by the middleX and middleY options.
  575. if (isNumber(point._midX) && isNumber(point._midY)) {
  576. point.plotX = xAxis.toPixels(point._midX, true);
  577. point.plotY = yAxis.toPixels(point._midY, true);
  578. }
  579. if (doFullTranslate) {
  580. point.shapeType = 'path';
  581. point.shapeArgs = {
  582. d: series.translatePath(point.path)
  583. };
  584. }
  585. });
  586. fireEvent(series, 'afterTranslate');
  587. },
  588. // Get presentational attributes. In the maps series this runs in both
  589. // styled and non-styled mode, because colors hold data when a colorAxis
  590. // is used.
  591. pointAttribs: function (point, state) {
  592. var attr = point.series.chart.styledMode ?
  593. this.colorAttribs(point) :
  594. seriesTypes.column.prototype.pointAttribs.call(this, point, state);
  595. // Set the stroke-width on the group element and let all point
  596. // graphics inherit. That way we don't have to iterate over all
  597. // points to update the stroke-width on zooming.
  598. attr['stroke-width'] = pick(point.options[(this.pointAttrToOptions &&
  599. this.pointAttrToOptions['stroke-width']) || 'borderWidth'], 'inherit');
  600. return attr;
  601. },
  602. // Use the drawPoints method of column, that is able to handle simple
  603. // shapeArgs. Extend it by assigning the tooltip position.
  604. drawPoints: function () {
  605. var series = this, xAxis = series.xAxis, yAxis = series.yAxis, group = series.group, chart = series.chart, renderer = chart.renderer, scaleX, scaleY, translateX, translateY, baseTrans = this.baseTrans, transformGroup, startTranslateX, startTranslateY, startScaleX, startScaleY;
  606. // Set a group that handles transform during zooming and panning in
  607. // order to preserve clipping on series.group
  608. if (!series.transformGroup) {
  609. series.transformGroup = renderer.g()
  610. .attr({
  611. scaleX: 1,
  612. scaleY: 1
  613. })
  614. .add(group);
  615. series.transformGroup.survive = true;
  616. }
  617. // Draw the shapes again
  618. if (series.doFullTranslate()) {
  619. // Individual point actions.
  620. if (chart.hasRendered && !chart.styledMode) {
  621. series.points.forEach(function (point) {
  622. // Restore state color on update/redraw (#3529)
  623. if (point.shapeArgs) {
  624. point.shapeArgs.fill = series.pointAttribs(point, point.state).fill;
  625. }
  626. });
  627. }
  628. // Draw them in transformGroup
  629. series.group = series.transformGroup;
  630. seriesTypes.column.prototype.drawPoints.apply(series);
  631. series.group = group; // Reset
  632. // Add class names
  633. series.points.forEach(function (point) {
  634. if (point.graphic) {
  635. var className = '';
  636. if (point.name) {
  637. className +=
  638. 'highcharts-name-' +
  639. point.name.replace(/ /g, '-').toLowerCase();
  640. }
  641. if (point.properties &&
  642. point.properties['hc-key']) {
  643. className +=
  644. ' highcharts-key-' +
  645. point.properties['hc-key'].toLowerCase();
  646. }
  647. if (className) {
  648. point.graphic.addClass(className);
  649. }
  650. // In styled mode, apply point colors by CSS
  651. if (chart.styledMode) {
  652. point.graphic.css(series.pointAttribs(point, point.selected && 'select' || void 0));
  653. }
  654. }
  655. });
  656. // Set the base for later scale-zooming. The originX and originY
  657. // properties are the axis values in the plot area's upper left
  658. // corner.
  659. this.baseTrans = {
  660. originX: (xAxis.min -
  661. xAxis.minPixelPadding / xAxis.transA),
  662. originY: (yAxis.min -
  663. yAxis.minPixelPadding / yAxis.transA +
  664. (yAxis.reversed ? 0 : yAxis.len / yAxis.transA)),
  665. transAX: xAxis.transA,
  666. transAY: yAxis.transA
  667. };
  668. // Reset transformation in case we're doing a full translate
  669. // (#3789)
  670. this.transformGroup.animate({
  671. translateX: 0,
  672. translateY: 0,
  673. scaleX: 1,
  674. scaleY: 1
  675. });
  676. // Just update the scale and transform for better performance
  677. }
  678. else {
  679. scaleX = xAxis.transA / baseTrans.transAX;
  680. scaleY = yAxis.transA / baseTrans.transAY;
  681. translateX = xAxis.toPixels(baseTrans.originX, true);
  682. translateY = yAxis.toPixels(baseTrans.originY, true);
  683. // Handle rounding errors in normal view (#3789)
  684. if (scaleX > 0.99 &&
  685. scaleX < 1.01 &&
  686. scaleY > 0.99 &&
  687. scaleY < 1.01) {
  688. scaleX = 1;
  689. scaleY = 1;
  690. translateX = Math.round(translateX);
  691. translateY = Math.round(translateY);
  692. }
  693. /* Animate or move to the new zoom level. In order to prevent
  694. flickering as the different transform components are set out
  695. of sync (#5991), we run a fake animator attribute and set
  696. scale and translation synchronously in the same step.
  697. A possible improvement to the API would be to handle this in
  698. the renderer or animation engine itself, to ensure that when
  699. we are animating multiple properties, we make sure that each
  700. step for each property is performed in the same step. Also,
  701. for symbols and for transform properties, it should induce a
  702. single updateTransform and symbolAttr call. */
  703. transformGroup = this.transformGroup;
  704. if (chart.renderer.globalAnimation) {
  705. startTranslateX = transformGroup.attr('translateX');
  706. startTranslateY = transformGroup.attr('translateY');
  707. startScaleX = transformGroup.attr('scaleX');
  708. startScaleY = transformGroup.attr('scaleY');
  709. transformGroup
  710. .attr({ animator: 0 })
  711. .animate({
  712. animator: 1
  713. }, {
  714. step: function (now, fx) {
  715. transformGroup.attr({
  716. translateX: (startTranslateX +
  717. (translateX - startTranslateX) * fx.pos),
  718. translateY: (startTranslateY +
  719. (translateY - startTranslateY) * fx.pos),
  720. scaleX: (startScaleX +
  721. (scaleX - startScaleX) *
  722. fx.pos),
  723. scaleY: (startScaleY +
  724. (scaleY - startScaleY) * fx.pos)
  725. });
  726. }
  727. });
  728. // When dragging, animation is off.
  729. }
  730. else {
  731. transformGroup.attr({
  732. translateX: translateX,
  733. translateY: translateY,
  734. scaleX: scaleX,
  735. scaleY: scaleY
  736. });
  737. }
  738. }
  739. /* Set the stroke-width directly on the group element so the
  740. children inherit it. We need to use setAttribute directly,
  741. because the stroke-widthSetter method expects a stroke color also
  742. to be set. */
  743. if (!chart.styledMode) {
  744. group.element.setAttribute('stroke-width', (pick(series.options[(series.pointAttrToOptions &&
  745. series.pointAttrToOptions['stroke-width']) || 'borderWidth'], 1 // Styled mode
  746. ) / (scaleX || 1)));
  747. }
  748. this.drawMapDataLabels();
  749. },
  750. // Draw the data labels. Special for maps is the time that the data
  751. // labels are drawn (after points), and the clipping of the
  752. // dataLabelsGroup.
  753. drawMapDataLabels: function () {
  754. Series.prototype.drawDataLabels.call(this);
  755. if (this.dataLabelsGroup) {
  756. this.dataLabelsGroup.clip(this.chart.clipRect);
  757. }
  758. },
  759. // Override render to throw in an async call in IE8. Otherwise it chokes
  760. // on the US counties demo.
  761. render: function () {
  762. var series = this, render = Series.prototype.render;
  763. // Give IE8 some time to breathe.
  764. if (series.chart.renderer.isVML && series.data.length > 3000) {
  765. setTimeout(function () {
  766. render.call(series);
  767. });
  768. }
  769. else {
  770. render.call(series);
  771. }
  772. },
  773. // The initial animation for the map series. By default, animation is
  774. // disabled. Animation of map shapes is not at all supported in VML
  775. // browsers.
  776. animate: function (init) {
  777. var chart = this.chart, animation = this.options.animation, group = this.group, xAxis = this.xAxis, yAxis = this.yAxis, left = xAxis.pos, top = yAxis.pos;
  778. if (chart.renderer.isSVG) {
  779. if (animation === true) {
  780. animation = {
  781. duration: 1000
  782. };
  783. }
  784. // Initialize the animation
  785. if (init) {
  786. // Scale down the group and place it in the center
  787. group.attr({
  788. translateX: left + xAxis.len / 2,
  789. translateY: top + yAxis.len / 2,
  790. scaleX: 0.001,
  791. scaleY: 0.001
  792. });
  793. // Run the animation
  794. }
  795. else {
  796. group.animate({
  797. translateX: left,
  798. translateY: top,
  799. scaleX: 1,
  800. scaleY: 1
  801. }, animation);
  802. }
  803. }
  804. },
  805. // Animate in the new series from the clicked point in the old series.
  806. // Depends on the drilldown.js module
  807. animateDrilldown: function (init) {
  808. var toBox = this.chart.plotBox, level = this.chart.drilldownLevels[this.chart.drilldownLevels.length - 1], fromBox = level.bBox, animationOptions = this.chart.options.drilldown.animation, scale;
  809. if (!init) {
  810. scale = Math.min(fromBox.width / toBox.width, fromBox.height / toBox.height);
  811. level.shapeArgs = {
  812. scaleX: scale,
  813. scaleY: scale,
  814. translateX: fromBox.x,
  815. translateY: fromBox.y
  816. };
  817. this.points.forEach(function (point) {
  818. if (point.graphic) {
  819. point.graphic
  820. .attr(level.shapeArgs)
  821. .animate({
  822. scaleX: 1,
  823. scaleY: 1,
  824. translateX: 0,
  825. translateY: 0
  826. }, animationOptions);
  827. }
  828. });
  829. }
  830. },
  831. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  832. // When drilling up, pull out the individual point graphics from the
  833. // lower series and animate them into the origin point in the upper
  834. // series.
  835. animateDrillupFrom: function (level) {
  836. seriesTypes.column.prototype
  837. .animateDrillupFrom.call(this, level);
  838. },
  839. // When drilling up, keep the upper series invisible until the lower
  840. // series has moved into place
  841. animateDrillupTo: function (init) {
  842. seriesTypes.column.prototype
  843. .animateDrillupTo.call(this, init);
  844. }
  845. // Point class
  846. }), extend({
  847. // Extend the Point object to split paths
  848. applyOptions: function (options, x) {
  849. var series = this.series, point = Point.prototype.applyOptions.call(this, options, x), joinBy = series.joinBy, mapPoint;
  850. if (series.mapData && series.mapMap) {
  851. var joinKey = joinBy[1];
  852. var mapKey = Point.prototype.getNestedProperty.call(point, joinKey);
  853. mapPoint = typeof mapKey !== 'undefined' &&
  854. series.mapMap[mapKey];
  855. if (mapPoint) {
  856. // This applies only to bubbles
  857. if (series.xyFromShape) {
  858. point.x = mapPoint._midX;
  859. point.y = mapPoint._midY;
  860. }
  861. extend(point, mapPoint); // copy over properties
  862. }
  863. else {
  864. point.value = point.value || null;
  865. }
  866. }
  867. return point;
  868. },
  869. // Stop the fade-out
  870. onMouseOver: function (e) {
  871. U.clearTimeout(this.colorInterval);
  872. if (this.value !== null || this.series.options.nullInteraction) {
  873. Point.prototype.onMouseOver.call(this, e);
  874. }
  875. else {
  876. // #3401 Tooltip doesn't hide when hovering over null points
  877. this.series.onMouseOut(e);
  878. }
  879. },
  880. // eslint-disable-next-line valid-jsdoc
  881. /**
  882. * Highmaps only. Zoom in on the point using the global animation.
  883. *
  884. * @sample maps/members/point-zoomto/
  885. * Zoom to points from butons
  886. *
  887. * @requires modules/map
  888. *
  889. * @function Highcharts.Point#zoomTo
  890. */
  891. zoomTo: function () {
  892. var point = this, series = point.series;
  893. series.xAxis.setExtremes(point._minX, point._maxX, false);
  894. series.yAxis.setExtremes(point._minY, point._maxY, false);
  895. series.chart.redraw();
  896. }
  897. }, colorMapPointMixin));
  898. /**
  899. * A map data object containing a `path` definition and optionally additional
  900. * properties to join in the data as per the `joinBy` option.
  901. *
  902. * @sample maps/demo/category-map/
  903. * Map data and joinBy
  904. *
  905. * @type {Array<Highcharts.SeriesMapDataOptions>|*}
  906. * @product highmaps
  907. * @apioption series.mapData
  908. */
  909. /**
  910. * A `map` series. If the [type](#series.map.type) option is not specified, it
  911. * is inherited from [chart.type](#chart.type).
  912. *
  913. * @extends series,plotOptions.map
  914. * @excluding dataParser, dataURL, marker
  915. * @product highmaps
  916. * @apioption series.map
  917. */
  918. /**
  919. * An array of data points for the series. For the `map` series type, points can
  920. * be given in the following ways:
  921. *
  922. * 1. An array of numerical values. In this case, the numerical values will be
  923. * interpreted as `value` options. Example:
  924. * ```js
  925. * data: [0, 5, 3, 5]
  926. * ```
  927. *
  928. * 2. An array of arrays with 2 values. In this case, the values correspond to
  929. * `[hc-key, value]`. Example:
  930. * ```js
  931. * data: [
  932. * ['us-ny', 0],
  933. * ['us-mi', 5],
  934. * ['us-tx', 3],
  935. * ['us-ak', 5]
  936. * ]
  937. * ```
  938. *
  939. * 3. An array of objects with named values. The following snippet shows only a
  940. * few settings, see the complete options set below. If the total number of
  941. * data points exceeds the series'
  942. * [turboThreshold](#series.map.turboThreshold),
  943. * this option is not available.
  944. * ```js
  945. * data: [{
  946. * value: 6,
  947. * name: "Point2",
  948. * color: "#00FF00"
  949. * }, {
  950. * value: 6,
  951. * name: "Point1",
  952. * color: "#FF00FF"
  953. * }]
  954. * ```
  955. *
  956. * @type {Array<number|Array<string,(number|null)>|null|*>}
  957. * @product highmaps
  958. * @apioption series.map.data
  959. */
  960. /**
  961. * Individual color for the point. By default the color is either used
  962. * to denote the value, or pulled from the global `colors` array.
  963. *
  964. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  965. * @product highmaps
  966. * @apioption series.map.data.color
  967. */
  968. /**
  969. * Individual data label for each point. The options are the same as
  970. * the ones for [plotOptions.series.dataLabels](
  971. * #plotOptions.series.dataLabels).
  972. *
  973. * @sample maps/series/data-datalabels/
  974. * Disable data labels for individual areas
  975. *
  976. * @type {Highcharts.DataLabelsOptions}
  977. * @product highmaps
  978. * @apioption series.map.data.dataLabels
  979. */
  980. /**
  981. * The `id` of a series in the [drilldown.series](#drilldown.series)
  982. * array to use for a drilldown for this point.
  983. *
  984. * @sample maps/demo/map-drilldown/
  985. * Basic drilldown
  986. *
  987. * @type {string}
  988. * @product highmaps
  989. * @apioption series.map.data.drilldown
  990. */
  991. /**
  992. * An id for the point. This can be used after render time to get a
  993. * pointer to the point object through `chart.get()`.
  994. *
  995. * @sample maps/series/data-id/
  996. * Highlight a point by id
  997. *
  998. * @type {string}
  999. * @product highmaps
  1000. * @apioption series.map.data.id
  1001. */
  1002. /**
  1003. * When data labels are laid out on a map, Highmaps runs a simplified
  1004. * algorithm to detect collision. When two labels collide, the one with
  1005. * the lowest rank is hidden. By default the rank is computed from the
  1006. * area.
  1007. *
  1008. * @type {number}
  1009. * @product highmaps
  1010. * @apioption series.map.data.labelrank
  1011. */
  1012. /**
  1013. * The relative mid point of an area, used to place the data label.
  1014. * Ranges from 0 to 1\. When `mapData` is used, middleX can be defined
  1015. * there.
  1016. *
  1017. * @type {number}
  1018. * @default 0.5
  1019. * @product highmaps
  1020. * @apioption series.map.data.middleX
  1021. */
  1022. /**
  1023. * The relative mid point of an area, used to place the data label.
  1024. * Ranges from 0 to 1\. When `mapData` is used, middleY can be defined
  1025. * there.
  1026. *
  1027. * @type {number}
  1028. * @default 0.5
  1029. * @product highmaps
  1030. * @apioption series.map.data.middleY
  1031. */
  1032. /**
  1033. * The name of the point as shown in the legend, tooltip, dataLabel
  1034. * etc.
  1035. *
  1036. * @sample maps/series/data-datalabels/
  1037. * Point names
  1038. *
  1039. * @type {string}
  1040. * @product highmaps
  1041. * @apioption series.map.data.name
  1042. */
  1043. /**
  1044. * For map and mapline series types, the SVG path for the shape. For
  1045. * compatibily with old IE, not all SVG path definitions are supported,
  1046. * but M, L and C operators are safe.
  1047. *
  1048. * To achieve a better separation between the structure and the data,
  1049. * it is recommended to use `mapData` to define that paths instead
  1050. * of defining them on the data points themselves.
  1051. *
  1052. * @sample maps/series/data-path/
  1053. * Paths defined in data
  1054. *
  1055. * @type {string}
  1056. * @product highmaps
  1057. * @apioption series.map.data.path
  1058. */
  1059. /**
  1060. * The numeric value of the data point.
  1061. *
  1062. * @type {number|null}
  1063. * @product highmaps
  1064. * @apioption series.map.data.value
  1065. */
  1066. /**
  1067. * Individual point events
  1068. *
  1069. * @extends plotOptions.series.point.events
  1070. * @product highmaps
  1071. * @apioption series.map.data.events
  1072. */
  1073. ''; // adds doclets above to the transpiled file