You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18744 lines
494 KiB

9 years ago
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v4.1.8 (2015-08-20)
  5. *
  6. * (c) 2009-2014 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. // JSLint options:
  11. /*global Highcharts, HighchartsAdapter, document, window, navigator, setInterval, clearInterval, clearTimeout, setTimeout, location, jQuery, $, console, each, grep */
  12. /*jslint ass: true, sloppy: true, forin: true, plusplus: true, nomen: true, vars: true, regexp: true, newcap: true, browser: true, continue: true, white: true */
  13. (function () {
  14. // encapsulated variables
  15. var UNDEFINED,
  16. doc = document,
  17. win = window,
  18. math = Math,
  19. mathRound = math.round,
  20. mathFloor = math.floor,
  21. mathCeil = math.ceil,
  22. mathMax = math.max,
  23. mathMin = math.min,
  24. mathAbs = math.abs,
  25. mathCos = math.cos,
  26. mathSin = math.sin,
  27. mathPI = math.PI,
  28. deg2rad = mathPI * 2 / 360,
  29. // some variables
  30. userAgent = navigator.userAgent,
  31. isOpera = win.opera,
  32. isIE = /(msie|trident)/i.test(userAgent) && !isOpera,
  33. docMode8 = doc.documentMode === 8,
  34. isWebKit = /AppleWebKit/.test(userAgent),
  35. isFirefox = /Firefox/.test(userAgent),
  36. isTouchDevice = /(Mobile|Android|Windows Phone)/.test(userAgent),
  37. SVG_NS = 'http://www.w3.org/2000/svg',
  38. hasSVG = !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  39. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  40. useCanVG = !hasSVG && !isIE && !!doc.createElement('canvas').getContext,
  41. Renderer,
  42. hasTouch,
  43. symbolSizes = {},
  44. idCounter = 0,
  45. garbageBin,
  46. defaultOptions,
  47. dateFormat, // function
  48. pathAnim,
  49. timeUnits,
  50. noop = function () { return UNDEFINED; },
  51. charts = [],
  52. chartCount = 0,
  53. PRODUCT = 'Highcharts',
  54. VERSION = '4.1.8',
  55. // some constants for frequently used strings
  56. DIV = 'div',
  57. ABSOLUTE = 'absolute',
  58. RELATIVE = 'relative',
  59. HIDDEN = 'hidden',
  60. PREFIX = 'highcharts-',
  61. VISIBLE = 'visible',
  62. PX = 'px',
  63. NONE = 'none',
  64. M = 'M',
  65. L = 'L',
  66. numRegex = /^[0-9]+$/,
  67. NORMAL_STATE = '',
  68. HOVER_STATE = 'hover',
  69. SELECT_STATE = 'select',
  70. marginNames = ['plotTop', 'marginRight', 'marginBottom', 'plotLeft'],
  71. // Object for extending Axis
  72. AxisPlotLineOrBandExtension,
  73. // constants for attributes
  74. STROKE_WIDTH = 'stroke-width',
  75. // time methods, changed based on whether or not UTC is used
  76. Date, // Allow using a different Date class
  77. makeTime,
  78. timezoneOffset,
  79. getTimezoneOffset,
  80. getMinutes,
  81. getHours,
  82. getDay,
  83. getDate,
  84. getMonth,
  85. getFullYear,
  86. setMilliseconds,
  87. setSeconds,
  88. setMinutes,
  89. setHours,
  90. setDate,
  91. setMonth,
  92. setFullYear,
  93. // lookup over the types and the associated classes
  94. seriesTypes = {},
  95. Highcharts;
  96. // The Highcharts namespace
  97. Highcharts = win.Highcharts = win.Highcharts ? error(16, true) : {};
  98. Highcharts.seriesTypes = seriesTypes;
  99. /**
  100. * Extend an object with the members of another
  101. * @param {Object} a The object to be extended
  102. * @param {Object} b The object to add to the first one
  103. */
  104. var extend = Highcharts.extend = function (a, b) {
  105. var n;
  106. if (!a) {
  107. a = {};
  108. }
  109. for (n in b) {
  110. a[n] = b[n];
  111. }
  112. return a;
  113. };
  114. /**
  115. * Deep merge two or more objects and return a third object. If the first argument is
  116. * true, the contents of the second object is copied into the first object.
  117. * Previously this function redirected to jQuery.extend(true), but this had two limitations.
  118. * First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
  119. * it copied properties from extended prototypes.
  120. */
  121. function merge() {
  122. var i,
  123. args = arguments,
  124. len,
  125. ret = {},
  126. doCopy = function (copy, original) {
  127. var value, key;
  128. // An object is replacing a primitive
  129. if (typeof copy !== 'object') {
  130. copy = {};
  131. }
  132. for (key in original) {
  133. if (original.hasOwnProperty(key)) {
  134. value = original[key];
  135. // Copy the contents of objects, but not arrays or DOM nodes
  136. if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]' &&
  137. key !== 'renderTo' && typeof value.nodeType !== 'number') {
  138. copy[key] = doCopy(copy[key] || {}, value);
  139. // Primitives and arrays are copied over directly
  140. } else {
  141. copy[key] = original[key];
  142. }
  143. }
  144. }
  145. return copy;
  146. };
  147. // If first argument is true, copy into the existing object. Used in setOptions.
  148. if (args[0] === true) {
  149. ret = args[1];
  150. args = Array.prototype.slice.call(args, 2);
  151. }
  152. // For each argument, extend the return
  153. len = args.length;
  154. for (i = 0; i < len; i++) {
  155. ret = doCopy(ret, args[i]);
  156. }
  157. return ret;
  158. }
  159. /**
  160. * Shortcut for parseInt
  161. * @param {Object} s
  162. * @param {Number} mag Magnitude
  163. */
  164. function pInt(s, mag) {
  165. return parseInt(s, mag || 10);
  166. }
  167. /**
  168. * Check for string
  169. * @param {Object} s
  170. */
  171. function isString(s) {
  172. return typeof s === 'string';
  173. }
  174. /**
  175. * Check for object
  176. * @param {Object} obj
  177. */
  178. function isObject(obj) {
  179. return obj && typeof obj === 'object';
  180. }
  181. /**
  182. * Check for array
  183. * @param {Object} obj
  184. */
  185. function isArray(obj) {
  186. return Object.prototype.toString.call(obj) === '[object Array]';
  187. }
  188. /**
  189. * Check for number
  190. * @param {Object} n
  191. */
  192. function isNumber(n) {
  193. return typeof n === 'number';
  194. }
  195. function log2lin(num) {
  196. return math.log(num) / math.LN10;
  197. }
  198. function lin2log(num) {
  199. return math.pow(10, num);
  200. }
  201. /**
  202. * Remove last occurence of an item from an array
  203. * @param {Array} arr
  204. * @param {Mixed} item
  205. */
  206. function erase(arr, item) {
  207. var i = arr.length;
  208. while (i--) {
  209. if (arr[i] === item) {
  210. arr.splice(i, 1);
  211. break;
  212. }
  213. }
  214. //return arr;
  215. }
  216. /**
  217. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  218. * @param {Object} obj
  219. */
  220. function defined(obj) {
  221. return obj !== UNDEFINED && obj !== null;
  222. }
  223. /**
  224. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  225. * it attempts to set expando properties on the SVG element, which is not allowed.
  226. *
  227. * @param {Object} elem The DOM element to receive the attribute(s)
  228. * @param {String|Object} prop The property or an abject of key-value pairs
  229. * @param {String} value The value if a single property is set
  230. */
  231. function attr(elem, prop, value) {
  232. var key,
  233. ret;
  234. // if the prop is a string
  235. if (isString(prop)) {
  236. // set the value
  237. if (defined(value)) {
  238. elem.setAttribute(prop, value);
  239. // get the value
  240. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  241. ret = elem.getAttribute(prop);
  242. }
  243. // else if prop is defined, it is a hash of key/value pairs
  244. } else if (defined(prop) && isObject(prop)) {
  245. for (key in prop) {
  246. elem.setAttribute(key, prop[key]);
  247. }
  248. }
  249. return ret;
  250. }
  251. /**
  252. * Check if an element is an array, and if not, make it into an array. Like
  253. * MooTools' $.splat.
  254. */
  255. function splat(obj) {
  256. return isArray(obj) ? obj : [obj];
  257. }
  258. /**
  259. * Return the first value that is defined. Like MooTools' $.pick.
  260. */
  261. var pick = Highcharts.pick = function () {
  262. var args = arguments,
  263. i,
  264. arg,
  265. length = args.length;
  266. for (i = 0; i < length; i++) {
  267. arg = args[i];
  268. if (arg !== UNDEFINED && arg !== null) {
  269. return arg;
  270. }
  271. }
  272. };
  273. /**
  274. * Set CSS on a given element
  275. * @param {Object} el
  276. * @param {Object} styles Style object with camel case property names
  277. */
  278. function css(el, styles) {
  279. if (isIE && !hasSVG) { // #2686
  280. if (styles && styles.opacity !== UNDEFINED) {
  281. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  282. }
  283. }
  284. extend(el.style, styles);
  285. }
  286. /**
  287. * Utility function to create element with attributes and styles
  288. * @param {Object} tag
  289. * @param {Object} attribs
  290. * @param {Object} styles
  291. * @param {Object} parent
  292. * @param {Object} nopad
  293. */
  294. function createElement(tag, attribs, styles, parent, nopad) {
  295. var el = doc.createElement(tag);
  296. if (attribs) {
  297. extend(el, attribs);
  298. }
  299. if (nopad) {
  300. css(el, {padding: 0, border: NONE, margin: 0});
  301. }
  302. if (styles) {
  303. css(el, styles);
  304. }
  305. if (parent) {
  306. parent.appendChild(el);
  307. }
  308. return el;
  309. }
  310. /**
  311. * Extend a prototyped class by new members
  312. * @param {Object} parent
  313. * @param {Object} members
  314. */
  315. function extendClass(parent, members) {
  316. var object = function () { return UNDEFINED; };
  317. object.prototype = new parent();
  318. extend(object.prototype, members);
  319. return object;
  320. }
  321. /**
  322. * Pad a string to a given length by adding 0 to the beginning
  323. * @param {Number} number
  324. * @param {Number} length
  325. */
  326. function pad(number, length) {
  327. // Create an array of the remaining length +1 and join it with 0's
  328. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  329. }
  330. /**
  331. * Return a length based on either the integer value, or a percentage of a base.
  332. */
  333. function relativeLength (value, base) {
  334. return (/%$/).test(value) ? base * parseFloat(value) / 100 : parseFloat(value);
  335. }
  336. /**
  337. * Wrap a method with extended functionality, preserving the original function
  338. * @param {Object} obj The context object that the method belongs to
  339. * @param {String} method The name of the method to extend
  340. * @param {Function} func A wrapper function callback. This function is called with the same arguments
  341. * as the original function, except that the original function is unshifted and passed as the first
  342. * argument.
  343. */
  344. var wrap = Highcharts.wrap = function (obj, method, func) {
  345. var proceed = obj[method];
  346. obj[method] = function () {
  347. var args = Array.prototype.slice.call(arguments);
  348. args.unshift(proceed);
  349. return func.apply(this, args);
  350. };
  351. };
  352. function getTZOffset(timestamp) {
  353. return ((getTimezoneOffset && getTimezoneOffset(timestamp)) || timezoneOffset || 0) * 60000;
  354. }
  355. /**
  356. * Based on http://www.php.net/manual/en/function.strftime.php
  357. * @param {String} format
  358. * @param {Number} timestamp
  359. * @param {Boolean} capitalize
  360. */
  361. dateFormat = function (format, timestamp, capitalize) {
  362. if (!defined(timestamp) || isNaN(timestamp)) {
  363. return defaultOptions.lang.invalidDate || '';
  364. }
  365. format = pick(format, '%Y-%m-%d %H:%M:%S');
  366. var date = new Date(timestamp - getTZOffset(timestamp)),
  367. key, // used in for constuct below
  368. // get the basic time values
  369. hours = date[getHours](),
  370. day = date[getDay](),
  371. dayOfMonth = date[getDate](),
  372. month = date[getMonth](),
  373. fullYear = date[getFullYear](),
  374. lang = defaultOptions.lang,
  375. langWeekdays = lang.weekdays,
  376. // List all format keys. Custom formats can be added from the outside.
  377. replacements = extend({
  378. // Day
  379. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  380. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  381. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  382. 'e': dayOfMonth, // Day of the month, 1 through 31
  383. 'w': day,
  384. // Week (none implemented)
  385. //'W': weekNumber(),
  386. // Month
  387. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  388. 'B': lang.months[month], // Long month, like 'January'
  389. 'm': pad(month + 1), // Two digit month number, 01 through 12
  390. // Year
  391. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  392. 'Y': fullYear, // Four digits year, like 2009
  393. // Time
  394. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  395. 'k': hours, // Hours in 24h format, 0 through 23
  396. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  397. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  398. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  399. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  400. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  401. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  402. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  403. }, Highcharts.dateFormats);
  404. // do the replaces
  405. for (key in replacements) {
  406. while (format.indexOf('%' + key) !== -1) { // regex would do it in one line, but this is faster
  407. format = format.replace('%' + key, typeof replacements[key] === 'function' ? replacements[key](timestamp) : replacements[key]);
  408. }
  409. }
  410. // Optionally capitalize the string and return
  411. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  412. };
  413. /**
  414. * Format a single variable. Similar to sprintf, without the % prefix.
  415. */
  416. function formatSingle(format, val) {
  417. var floatRegex = /f$/,
  418. decRegex = /\.([0-9])/,
  419. lang = defaultOptions.lang,
  420. decimals;
  421. if (floatRegex.test(format)) { // float
  422. decimals = format.match(decRegex);
  423. decimals = decimals ? decimals[1] : -1;
  424. if (val !== null) {
  425. val = Highcharts.numberFormat(
  426. val,
  427. decimals,
  428. lang.decimalPoint,
  429. format.indexOf(',') > -1 ? lang.thousandsSep : ''
  430. );
  431. }
  432. } else {
  433. val = dateFormat(format, val);
  434. }
  435. return val;
  436. }
  437. /**
  438. * Format a string according to a subset of the rules of Python's String.format method.
  439. */
  440. function format(str, ctx) {
  441. var splitter = '{',
  442. isInside = false,
  443. segment,
  444. valueAndFormat,
  445. path,
  446. i,
  447. len,
  448. ret = [],
  449. val,
  450. index;
  451. while ((index = str.indexOf(splitter)) !== -1) {
  452. segment = str.slice(0, index);
  453. if (isInside) { // we're on the closing bracket looking back
  454. valueAndFormat = segment.split(':');
  455. path = valueAndFormat.shift().split('.'); // get first and leave format
  456. len = path.length;
  457. val = ctx;
  458. // Assign deeper paths
  459. for (i = 0; i < len; i++) {
  460. val = val[path[i]];
  461. }
  462. // Format the replacement
  463. if (valueAndFormat.length) {
  464. val = formatSingle(valueAndFormat.join(':'), val);
  465. }
  466. // Push the result and advance the cursor
  467. ret.push(val);
  468. } else {
  469. ret.push(segment);
  470. }
  471. str = str.slice(index + 1); // the rest
  472. isInside = !isInside; // toggle
  473. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  474. }
  475. ret.push(str);
  476. return ret.join('');
  477. }
  478. /**
  479. * Get the magnitude of a number
  480. */
  481. function getMagnitude(num) {
  482. return math.pow(10, mathFloor(math.log(num) / math.LN10));
  483. }
  484. /**
  485. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  486. * @param {Number} interval
  487. * @param {Array} multiples
  488. * @param {Number} magnitude
  489. * @param {Object} options
  490. */
  491. function normalizeTickInterval(interval, multiples, magnitude, allowDecimals, preventExceed) {
  492. var normalized,
  493. i,
  494. retInterval = interval;
  495. // round to a tenfold of 1, 2, 2.5 or 5
  496. magnitude = pick(magnitude, 1);
  497. normalized = interval / magnitude;
  498. // multiples for a linear scale
  499. if (!multiples) {
  500. multiples = [1, 2, 2.5, 5, 10];
  501. // the allowDecimals option
  502. if (allowDecimals === false) {
  503. if (magnitude === 1) {
  504. multiples = [1, 2, 5, 10];
  505. } else if (magnitude <= 0.1) {
  506. multiples = [1 / magnitude];
  507. }
  508. }
  509. }
  510. // normalize the interval to the nearest multiple
  511. for (i = 0; i < multiples.length; i++) {
  512. retInterval = multiples[i];
  513. if ((preventExceed && retInterval * magnitude >= interval) || // only allow tick amounts smaller than natural
  514. (!preventExceed && (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2))) {
  515. break;
  516. }
  517. }
  518. // multiply back to the correct magnitude
  519. retInterval *= magnitude;
  520. return retInterval;
  521. }
  522. /**
  523. * Utility method that sorts an object array and keeping the order of equal items.
  524. * ECMA script standard does not specify the behaviour when items are equal.
  525. */
  526. function stableSort(arr, sortFunction) {
  527. var length = arr.length,
  528. sortValue,
  529. i;
  530. // Add index to each item
  531. for (i = 0; i < length; i++) {
  532. arr[i].ss_i = i; // stable sort index
  533. }
  534. arr.sort(function (a, b) {
  535. sortValue = sortFunction(a, b);
  536. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  537. });
  538. // Remove index from items
  539. for (i = 0; i < length; i++) {
  540. delete arr[i].ss_i; // stable sort index
  541. }
  542. }
  543. /**
  544. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  545. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  546. * method is slightly slower, but safe.
  547. */
  548. function arrayMin(data) {
  549. var i = data.length,
  550. min = data[0];
  551. while (i--) {
  552. if (data[i] < min) {
  553. min = data[i];
  554. }
  555. }
  556. return min;
  557. }
  558. /**
  559. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  560. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  561. * method is slightly slower, but safe.
  562. */
  563. function arrayMax(data) {
  564. var i = data.length,
  565. max = data[0];
  566. while (i--) {
  567. if (data[i] > max) {
  568. max = data[i];
  569. }
  570. }
  571. return max;
  572. }
  573. /**
  574. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  575. * It loops all properties and invokes destroy if there is a destroy method. The property is
  576. * then delete'ed.
  577. * @param {Object} The object to destroy properties on
  578. * @param {Object} Exception, do not destroy this property, only delete it.
  579. */
  580. function destroyObjectProperties(obj, except) {
  581. var n;
  582. for (n in obj) {
  583. // If the object is non-null and destroy is defined
  584. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  585. // Invoke the destroy
  586. obj[n].destroy();
  587. }
  588. // Delete the property from the object.
  589. delete obj[n];
  590. }
  591. }
  592. /**
  593. * Discard an element by moving it to the bin and delete
  594. * @param {Object} The HTML node to discard
  595. */
  596. function discardElement(element) {
  597. // create a garbage bin element, not part of the DOM
  598. if (!garbageBin) {
  599. garbageBin = createElement(DIV);
  600. }
  601. // move the node and empty bin
  602. if (element) {
  603. garbageBin.appendChild(element);
  604. }
  605. garbageBin.innerHTML = '';
  606. }
  607. /**
  608. * Provide error messages for debugging, with links to online explanation
  609. */
  610. function error (code, stop) {
  611. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  612. if (stop) {
  613. throw msg;
  614. }
  615. // else ...
  616. if (win.console) {
  617. console.log(msg);
  618. }
  619. }
  620. /**
  621. * Fix JS round off float errors
  622. * @param {Number} num
  623. */
  624. function correctFloat(num, prec) {
  625. return parseFloat(
  626. num.toPrecision(prec || 14)
  627. );
  628. }
  629. /**
  630. * Set the global animation to either a given value, or fall back to the
  631. * given chart's animation option
  632. * @param {Object} animation
  633. * @param {Object} chart
  634. */
  635. function setAnimation(animation, chart) {
  636. chart.renderer.globalAnimation = pick(animation, chart.animation);
  637. }
  638. /**
  639. * The time unit lookup
  640. */
  641. timeUnits = {
  642. millisecond: 1,
  643. second: 1000,
  644. minute: 60000,
  645. hour: 3600000,
  646. day: 24 * 3600000,
  647. week: 7 * 24 * 3600000,
  648. month: 28 * 24 * 3600000,
  649. year: 364 * 24 * 3600000
  650. };
  651. /**
  652. * Format a number and return a string based on input settings
  653. * @param {Number} number The input number to format
  654. * @param {Number} decimals The amount of decimals
  655. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  656. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  657. */
  658. Highcharts.numberFormat = function (number, decimals, decPoint, thousandsSep) {
  659. var lang = defaultOptions.lang,
  660. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  661. n = +number || 0,
  662. c = decimals === -1 ?
  663. mathMin((n.toString().split('.')[1] || '').length, 20) : // Preserve decimals. Not huge numbers (#3793).
  664. (isNaN(decimals = mathAbs(decimals)) ? 2 : decimals),
  665. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  666. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  667. s = n < 0 ? "-" : "",
  668. i = String(pInt(n = mathAbs(n).toFixed(c))),
  669. j = i.length > 3 ? i.length % 3 : 0;
  670. return (s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  671. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : ""));
  672. };
  673. /**
  674. * Path interpolation algorithm used across adapters
  675. */
  676. pathAnim = {
  677. /**
  678. * Prepare start and end values so that the path can be animated one to one
  679. */
  680. init: function (elem, fromD, toD) {
  681. fromD = fromD || '';
  682. var shift = elem.shift,
  683. bezier = fromD.indexOf('C') > -1,
  684. numParams = bezier ? 7 : 3,
  685. endLength,
  686. slice,
  687. i,
  688. start = fromD.split(' '),
  689. end = [].concat(toD), // copy
  690. startBaseLine,
  691. endBaseLine,
  692. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  693. i = arr.length;
  694. while (i--) {
  695. if (arr[i] === M) {
  696. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  697. }
  698. }
  699. };
  700. if (bezier) {
  701. sixify(start);
  702. sixify(end);
  703. }
  704. // pull out the base lines before padding
  705. if (elem.isArea) {
  706. startBaseLine = start.splice(start.length - 6, 6);
  707. endBaseLine = end.splice(end.length - 6, 6);
  708. }
  709. // if shifting points, prepend a dummy point to the end path
  710. if (shift <= end.length / numParams && start.length === end.length) {
  711. while (shift--) {
  712. end = [].concat(end).splice(0, numParams).concat(end);
  713. }
  714. }
  715. elem.shift = 0; // reset for following animations
  716. // copy and append last point until the length matches the end length
  717. if (start.length) {
  718. endLength = end.length;
  719. while (start.length < endLength) {
  720. //bezier && sixify(start);
  721. slice = [].concat(start).splice(start.length - numParams, numParams);
  722. if (bezier) { // disable first control point
  723. slice[numParams - 6] = slice[numParams - 2];
  724. slice[numParams - 5] = slice[numParams - 1];
  725. }
  726. start = start.concat(slice);
  727. }
  728. }
  729. if (startBaseLine) { // append the base lines for areas
  730. start = start.concat(startBaseLine);
  731. end = end.concat(endBaseLine);
  732. }
  733. return [start, end];
  734. },
  735. /**
  736. * Interpolate each value of the path and return the array
  737. */
  738. step: function (start, end, pos, complete) {
  739. var ret = [],
  740. i = start.length,
  741. startVal;
  742. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  743. ret = complete;
  744. } else if (i === end.length && pos < 1) {
  745. while (i--) {
  746. startVal = parseFloat(start[i]);
  747. ret[i] =
  748. isNaN(startVal) ? // a letter instruction like M or L
  749. start[i] :
  750. pos * (parseFloat(end[i] - startVal)) + startVal;
  751. }
  752. } else { // if animation is finished or length not matching, land on right value
  753. ret = end;
  754. }
  755. return ret;
  756. }
  757. };
  758. (function ($) {
  759. /**
  760. * The default HighchartsAdapter for jQuery
  761. */
  762. win.HighchartsAdapter = win.HighchartsAdapter || ($ && {
  763. /**
  764. * Initialize the adapter by applying some extensions to jQuery
  765. */
  766. init: function (pathAnim) {
  767. // extend the animate function to allow SVG animations
  768. var Fx = $.fx;
  769. /*jslint unparam: true*//* allow unused param x in this function */
  770. $.extend($.easing, {
  771. easeOutQuad: function (x, t, b, c, d) {
  772. return -c * (t /= d) * (t - 2) + b;
  773. }
  774. });
  775. /*jslint unparam: false*/
  776. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  777. $.each(['cur', '_default', 'width', 'height', 'opacity'], function (i, fn) {
  778. var obj = Fx.step,
  779. base;
  780. // Handle different parent objects
  781. if (fn === 'cur') {
  782. obj = Fx.prototype; // 'cur', the getter, relates to Fx.prototype
  783. } else if (fn === '_default' && $.Tween) { // jQuery 1.8 model
  784. obj = $.Tween.propHooks[fn];
  785. fn = 'set';
  786. }
  787. // Overwrite the method
  788. base = obj[fn];
  789. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  790. // create the extended function replacement
  791. obj[fn] = function (fx) {
  792. var elem;
  793. // Fx.prototype.cur does not use fx argument
  794. fx = i ? fx : this;
  795. // Don't run animations on textual properties like align (#1821)
  796. if (fx.prop === 'align') {
  797. return;
  798. }
  799. // shortcut
  800. elem = fx.elem;
  801. // Fx.prototype.cur returns the current value. The other ones are setters
  802. // and returning a value has no effect.
  803. return elem.attr ? // is SVG element wrapper
  804. elem.attr(fx.prop, fn === 'cur' ? UNDEFINED : fx.now) : // apply the SVG wrapper's method
  805. base.apply(this, arguments); // use jQuery's built-in method
  806. };
  807. }
  808. });
  809. // Extend the opacity getter, needed for fading opacity with IE9 and jQuery 1.10+
  810. wrap($.cssHooks.opacity, 'get', function (proceed, elem, computed) {
  811. return elem.attr ? (elem.opacity || 0) : proceed.call(this, elem, computed);
  812. });
  813. // Define the setter function for d (path definitions)
  814. this.addAnimSetter('d', function (fx) {
  815. var elem = fx.elem,
  816. ends;
  817. // Normally start and end should be set in state == 0, but sometimes,
  818. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  819. // in these cases
  820. if (!fx.started) {
  821. ends = pathAnim.init(elem, elem.d, elem.toD);
  822. fx.start = ends[0];
  823. fx.end = ends[1];
  824. fx.started = true;
  825. }
  826. // Interpolate each value of the path
  827. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  828. });
  829. /**
  830. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  831. * @param {Array} arr
  832. * @param {Function} fn
  833. */
  834. this.each = Array.prototype.forEach ?
  835. function (arr, fn) { // modern browsers
  836. return Array.prototype.forEach.call(arr, fn);
  837. } :
  838. function (arr, fn) { // legacy
  839. var i,
  840. len = arr.length;
  841. for (i = 0; i < len; i++) {
  842. if (fn.call(arr[i], arr[i], i, arr) === false) {
  843. return i;
  844. }
  845. }
  846. };
  847. /**
  848. * Register Highcharts as a plugin in the respective framework
  849. */
  850. $.fn.highcharts = function () {
  851. var constr = 'Chart', // default constructor
  852. args = arguments,
  853. options,
  854. ret,
  855. chart;
  856. if (this[0]) {
  857. if (isString(args[0])) {
  858. constr = args[0];
  859. args = Array.prototype.slice.call(args, 1);
  860. }
  861. options = args[0];
  862. // Create the chart
  863. if (options !== UNDEFINED) {
  864. /*jslint unused:false*/
  865. options.chart = options.chart || {};
  866. options.chart.renderTo = this[0];
  867. chart = new Highcharts[constr](options, args[1]);
  868. ret = this;
  869. /*jslint unused:true*/
  870. }
  871. // When called without parameters or with the return argument, get a predefined chart
  872. if (options === UNDEFINED) {
  873. ret = charts[attr(this[0], 'data-highcharts-chart')];
  874. }
  875. }
  876. return ret;
  877. };
  878. },
  879. /**
  880. * Add an animation setter for a specific property
  881. */
  882. addAnimSetter: function (prop, setter) {
  883. // jQuery 1.8 style
  884. if ($.Tween) {
  885. $.Tween.propHooks[prop] = {
  886. set: setter
  887. };
  888. // pre 1.8
  889. } else {
  890. $.fx.step[prop] = setter;
  891. }
  892. },
  893. /**
  894. * Downloads a script and executes a callback when done.
  895. * @param {String} scriptLocation
  896. * @param {Function} callback
  897. */
  898. getScript: $.getScript,
  899. /**
  900. * Return the index of an item in an array, or -1 if not found
  901. */
  902. inArray: $.inArray,
  903. /**
  904. * A direct link to jQuery methods. MooTools and Prototype adapters must be implemented for each case of method.
  905. * @param {Object} elem The HTML element
  906. * @param {String} method Which method to run on the wrapped element
  907. */
  908. adapterRun: function (elem, method) {
  909. return $(elem)[method]();
  910. },
  911. /**
  912. * Filter an array
  913. */
  914. grep: $.grep,
  915. /**
  916. * Map an array
  917. * @param {Array} arr
  918. * @param {Function} fn
  919. */
  920. map: function (arr, fn) {
  921. //return jQuery.map(arr, fn);
  922. var results = [],
  923. i = 0,
  924. len = arr.length;
  925. for (; i < len; i++) {
  926. results[i] = fn.call(arr[i], arr[i], i, arr);
  927. }
  928. return results;
  929. },
  930. /**
  931. * Get the position of an element relative to the top left of the page
  932. */
  933. offset: function (el) {
  934. return $(el).offset();
  935. },
  936. /**
  937. * Add an event listener
  938. * @param {Object} el A HTML element or custom object
  939. * @param {String} event The event type
  940. * @param {Function} fn The event handler
  941. */
  942. addEvent: function (el, event, fn) {
  943. $(el).bind(event, fn);
  944. },
  945. /**
  946. * Remove event added with addEvent
  947. * @param {Object} el The object
  948. * @param {String} eventType The event type. Leave blank to remove all events.
  949. * @param {Function} handler The function to remove
  950. */
  951. removeEvent: function (el, eventType, handler) {
  952. // workaround for jQuery issue with unbinding custom events:
  953. // http://forum.jQuery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jQuery-1-4-2
  954. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  955. if (doc[func] && el && !el[func]) {
  956. el[func] = function () {};
  957. }
  958. $(el).unbind(eventType, handler);
  959. },
  960. /**
  961. * Fire an event on a custom object
  962. * @param {Object} el
  963. * @param {String} type
  964. * @param {Object} eventArguments
  965. * @param {Function} defaultFunction
  966. */
  967. fireEvent: function (el, type, eventArguments, defaultFunction) {
  968. var event = $.Event(type),
  969. detachedType = 'detached' + type,
  970. defaultPrevented;
  971. // Remove warnings in Chrome when accessing returnValue (#2790), layerX and layerY. Although Highcharts
  972. // never uses these properties, Chrome includes them in the default click event and
  973. // raises the warning when they are copied over in the extend statement below.
  974. //
  975. // To avoid problems in IE (see #1010) where we cannot delete the properties and avoid
  976. // testing if they are there (warning in chrome) the only option is to test if running IE.
  977. if (!isIE && eventArguments) {
  978. delete eventArguments.layerX;
  979. delete eventArguments.layerY;
  980. delete eventArguments.returnValue;
  981. }
  982. extend(event, eventArguments);
  983. // Prevent jQuery from triggering the object method that is named the
  984. // same as the event. For example, if the event is 'select', jQuery
  985. // attempts calling el.select and it goes into a loop.
  986. if (el[type]) {
  987. el[detachedType] = el[type];
  988. el[type] = null;
  989. }
  990. // Wrap preventDefault and stopPropagation in try/catch blocks in
  991. // order to prevent JS errors when cancelling events on non-DOM
  992. // objects. #615.
  993. /*jslint unparam: true*/
  994. $.each(['preventDefault', 'stopPropagation'], function (i, fn) {
  995. var base = event[fn];
  996. event[fn] = function () {
  997. try {
  998. base.call(event);
  999. } catch (e) {
  1000. if (fn === 'preventDefault') {
  1001. defaultPrevented = true;
  1002. }
  1003. }
  1004. };
  1005. });
  1006. /*jslint unparam: false*/
  1007. // trigger it
  1008. $(el).trigger(event);
  1009. // attach the method
  1010. if (el[detachedType]) {
  1011. el[type] = el[detachedType];
  1012. el[detachedType] = null;
  1013. }
  1014. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1015. defaultFunction(event);
  1016. }
  1017. },
  1018. /**
  1019. * Extension method needed for MooTools
  1020. */
  1021. washMouseEvent: function (e) {
  1022. var ret = e.originalEvent || e;
  1023. // computed by jQuery, needed by IE8
  1024. if (ret.pageX === UNDEFINED) { // #1236
  1025. ret.pageX = e.pageX;
  1026. ret.pageY = e.pageY;
  1027. }
  1028. return ret;
  1029. },
  1030. /**
  1031. * Animate a HTML element or SVG element wrapper
  1032. * @param {Object} el
  1033. * @param {Object} params
  1034. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1035. */
  1036. animate: function (el, params, options) {
  1037. var $el = $(el);
  1038. if (!el.style) {
  1039. el.style = {}; // #1881
  1040. }
  1041. if (params.d) {
  1042. el.toD = params.d; // keep the array form for paths, used in $.fx.step.d
  1043. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1044. }
  1045. $el.stop();
  1046. if (params.opacity !== UNDEFINED && el.attr) {
  1047. params.opacity += 'px'; // force jQuery to use same logic as width and height (#2161)
  1048. }
  1049. el.hasAnim = 1; // #3342
  1050. $el.animate(params, options);
  1051. },
  1052. /**
  1053. * Stop running animation
  1054. */
  1055. stop: function (el) {
  1056. if (el.hasAnim) { // #3342, memory leak on calling $(el) from destroy
  1057. $(el).stop();
  1058. }
  1059. }
  1060. });
  1061. }(win.jQuery));
  1062. // check for a custom HighchartsAdapter defined prior to this file
  1063. var globalAdapter = win.HighchartsAdapter,
  1064. adapter = globalAdapter || {};
  1065. // Initialize the adapter
  1066. if (globalAdapter) {
  1067. globalAdapter.init.call(globalAdapter, pathAnim);
  1068. }
  1069. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  1070. // and all the utility functions will be null. In that case they are populated by the
  1071. // default adapters below.
  1072. var adapterRun = adapter.adapterRun,
  1073. getScript = adapter.getScript,
  1074. inArray = adapter.inArray,
  1075. each = Highcharts.each = adapter.each,
  1076. grep = adapter.grep,
  1077. offset = adapter.offset,
  1078. map = adapter.map,
  1079. addEvent = adapter.addEvent,
  1080. removeEvent = adapter.removeEvent,
  1081. fireEvent = adapter.fireEvent,
  1082. washMouseEvent = adapter.washMouseEvent,
  1083. animate = adapter.animate,
  1084. stop = adapter.stop;
  1085. /* ****************************************************************************
  1086. * Handle the options *
  1087. *****************************************************************************/
  1088. defaultOptions = {
  1089. colors: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c',
  1090. '#8085e9', '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'],
  1091. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1092. lang: {
  1093. loading: 'Loading...',
  1094. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1095. 'August', 'September', 'October', 'November', 'December'],
  1096. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1097. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1098. // invalidDate: '',
  1099. decimalPoint: '.',
  1100. numericSymbols: ['k', 'M', 'G', 'T', 'P', 'E'], // SI prefixes used in axis labels
  1101. resetZoom: 'Reset zoom',
  1102. resetZoomTitle: 'Reset zoom level 1:1',
  1103. thousandsSep: ' '
  1104. },
  1105. global: {
  1106. useUTC: true,
  1107. //timezoneOffset: 0,
  1108. canvasToolsURL: 'http://code.highcharts.com/4.1.8/modules/canvas-tools.js',
  1109. VMLRadialGradientURL: 'http://code.highcharts.com/4.1.8/gfx/vml-radial-gradient.png'
  1110. },
  1111. chart: {
  1112. //animation: true,
  1113. //alignTicks: false,
  1114. //reflow: true,
  1115. //className: null,
  1116. //events: { load, selection },
  1117. //margin: [null],
  1118. //marginTop: null,
  1119. //marginRight: null,
  1120. //marginBottom: null,
  1121. //marginLeft: null,
  1122. borderColor: '#4572A7',
  1123. //borderWidth: 0,
  1124. borderRadius: 0,
  1125. defaultSeriesType: 'line',
  1126. ignoreHiddenSeries: true,
  1127. //inverted: false,
  1128. //shadow: false,
  1129. spacing: [10, 10, 15, 10],
  1130. //spacingTop: 10,
  1131. //spacingRight: 10,
  1132. //spacingBottom: 15,
  1133. //spacingLeft: 10,
  1134. //style: {
  1135. // fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1136. // fontSize: '12px'
  1137. //},
  1138. backgroundColor: '#FFFFFF',
  1139. //plotBackgroundColor: null,
  1140. plotBorderColor: '#C0C0C0',
  1141. //plotBorderWidth: 0,
  1142. //plotShadow: false,
  1143. //zoomType: ''
  1144. resetZoomButton: {
  1145. theme: {
  1146. zIndex: 20
  1147. },
  1148. position: {
  1149. align: 'right',
  1150. x: -10,
  1151. //verticalAlign: 'top',
  1152. y: 10
  1153. }
  1154. // relativeTo: 'plot'
  1155. }
  1156. },
  1157. title: {
  1158. text: 'Chart title',
  1159. align: 'center',
  1160. // floating: false,
  1161. margin: 15,
  1162. // x: 0,
  1163. // verticalAlign: 'top',
  1164. // y: null,
  1165. style: {
  1166. color: '#333333',
  1167. fontSize: '18px'
  1168. }
  1169. },
  1170. subtitle: {
  1171. text: '',
  1172. align: 'center',
  1173. // floating: false
  1174. // x: 0,
  1175. // verticalAlign: 'top',
  1176. // y: null,
  1177. style: {
  1178. color: '#555555'
  1179. }
  1180. },
  1181. plotOptions: {
  1182. line: { // base series options
  1183. allowPointSelect: false,
  1184. showCheckbox: false,
  1185. animation: {
  1186. duration: 1000
  1187. },
  1188. //connectNulls: false,
  1189. //cursor: 'default',
  1190. //clip: true,
  1191. //dashStyle: null,
  1192. //enableMouseTracking: true,
  1193. events: {},
  1194. //legendIndex: 0,
  1195. //linecap: 'round',
  1196. lineWidth: 2,
  1197. //shadow: false,
  1198. // stacking: null,
  1199. marker: {
  1200. //enabled: true,
  1201. //symbol: null,
  1202. lineWidth: 0,
  1203. radius: 4,
  1204. lineColor: '#FFFFFF',
  1205. //fillColor: null,
  1206. states: { // states for a single point
  1207. hover: {
  1208. enabled: true,
  1209. lineWidthPlus: 1,
  1210. radiusPlus: 2
  1211. },
  1212. select: {
  1213. fillColor: '#FFFFFF',
  1214. lineColor: '#000000',
  1215. lineWidth: 2
  1216. }
  1217. }
  1218. },
  1219. point: {
  1220. events: {}
  1221. },
  1222. dataLabels: {
  1223. align: 'center',
  1224. // defer: true,
  1225. // enabled: false,
  1226. formatter: function () {
  1227. return this.y === null ? '' : Highcharts.numberFormat(this.y, -1);
  1228. },
  1229. style: {
  1230. color: 'contrast',
  1231. fontSize: '11px',
  1232. fontWeight: 'bold',
  1233. textShadow: '0 0 6px contrast, 0 0 3px contrast'
  1234. },
  1235. verticalAlign: 'bottom', // above singular point
  1236. x: 0,
  1237. y: 0,
  1238. // backgroundColor: undefined,
  1239. // borderColor: undefined,
  1240. // borderRadius: undefined,
  1241. // borderWidth: undefined,
  1242. padding: 5
  1243. // shadow: false
  1244. },
  1245. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1246. pointRange: 0,
  1247. //pointStart: 0,
  1248. //pointInterval: 1,
  1249. //showInLegend: null, // auto: true for standalone series, false for linked series
  1250. states: { // states for the entire series
  1251. hover: {
  1252. //enabled: false,
  1253. lineWidthPlus: 1,
  1254. marker: {
  1255. // lineWidth: base + 1,
  1256. // radius: base + 1
  1257. },
  1258. halo: {
  1259. size: 10,
  1260. opacity: 0.25
  1261. }
  1262. },
  1263. select: {
  1264. marker: {}
  1265. }
  1266. },
  1267. stickyTracking: true,
  1268. //tooltip: {
  1269. //pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.y}</b>'
  1270. //valueDecimals: null,
  1271. //xDateFormat: '%A, %b %e, %Y',
  1272. //valuePrefix: '',
  1273. //ySuffix: ''
  1274. //}
  1275. turboThreshold: 1000
  1276. // zIndex: null
  1277. }
  1278. },
  1279. labels: {
  1280. //items: [],
  1281. style: {
  1282. //font: defaultFont,
  1283. position: ABSOLUTE,
  1284. color: '#3E576F'
  1285. }
  1286. },
  1287. legend: {
  1288. enabled: true,
  1289. align: 'center',
  1290. //floating: false,
  1291. layout: 'horizontal',
  1292. labelFormatter: function () {
  1293. return this.name;
  1294. },
  1295. //borderWidth: 0,
  1296. borderColor: '#909090',
  1297. borderRadius: 0,
  1298. navigation: {
  1299. // animation: true,
  1300. activeColor: '#274b6d',
  1301. // arrowSize: 12
  1302. inactiveColor: '#CCC'
  1303. // style: {} // text styles
  1304. },
  1305. // margin: 20,
  1306. // reversed: false,
  1307. shadow: false,
  1308. // backgroundColor: null,
  1309. /*style: {
  1310. padding: '5px'
  1311. },*/
  1312. itemStyle: {
  1313. color: '#333333',
  1314. fontSize: '12px',
  1315. fontWeight: 'bold'
  1316. },
  1317. itemHoverStyle: {
  1318. //cursor: 'pointer', removed as of #601
  1319. color: '#000'
  1320. },
  1321. itemHiddenStyle: {
  1322. color: '#CCC'
  1323. },
  1324. itemCheckboxStyle: {
  1325. position: ABSOLUTE,
  1326. width: '13px', // for IE precision
  1327. height: '13px'
  1328. },
  1329. // itemWidth: undefined,
  1330. // symbolRadius: 0,
  1331. // symbolWidth: 16,
  1332. symbolPadding: 5,
  1333. verticalAlign: 'bottom',
  1334. // width: undefined,
  1335. x: 0,
  1336. y: 0,
  1337. title: {
  1338. //text: null,
  1339. style: {
  1340. fontWeight: 'bold'
  1341. }
  1342. }
  1343. },
  1344. loading: {
  1345. // hideDuration: 100,
  1346. labelStyle: {
  1347. fontWeight: 'bold',
  1348. position: RELATIVE,
  1349. top: '45%'
  1350. },
  1351. // showDuration: 0,
  1352. style: {
  1353. position: ABSOLUTE,
  1354. backgroundColor: 'white',
  1355. opacity: 0.5,
  1356. textAlign: 'center'
  1357. }
  1358. },
  1359. tooltip: {
  1360. enabled: true,
  1361. animation: hasSVG,
  1362. //crosshairs: null,
  1363. backgroundColor: 'rgba(249, 249, 249, .85)',
  1364. borderWidth: 1,
  1365. borderRadius: 3,
  1366. dateTimeLabelFormats: {
  1367. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1368. second: '%A, %b %e, %H:%M:%S',
  1369. minute: '%A, %b %e, %H:%M',
  1370. hour: '%A, %b %e, %H:%M',
  1371. day: '%A, %b %e, %Y',
  1372. week: 'Week from %A, %b %e, %Y',
  1373. month: '%B %Y',
  1374. year: '%Y'
  1375. },
  1376. footerFormat: '',
  1377. //formatter: defaultFormatter,
  1378. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1379. pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.y}</b><br/>',
  1380. shadow: true,
  1381. //shape: 'callout',
  1382. //shared: false,
  1383. snap: isTouchDevice ? 25 : 10,
  1384. style: {
  1385. color: '#333333',
  1386. cursor: 'default',
  1387. fontSize: '12px',
  1388. padding: '8px',
  1389. whiteSpace: 'nowrap'
  1390. }
  1391. //xDateFormat: '%A, %b %e, %Y',
  1392. //valueDecimals: null,
  1393. //valuePrefix: '',
  1394. //valueSuffix: ''
  1395. },
  1396. credits: {
  1397. enabled: true,
  1398. text: 'Highcharts.com',
  1399. href: 'http://www.highcharts.com',
  1400. position: {
  1401. align: 'right',
  1402. x: -10,
  1403. verticalAlign: 'bottom',
  1404. y: -5
  1405. },
  1406. style: {
  1407. cursor: 'pointer',
  1408. color: '#909090',
  1409. fontSize: '9px'
  1410. }
  1411. }
  1412. };
  1413. // Series defaults
  1414. var defaultPlotOptions = defaultOptions.plotOptions,
  1415. defaultSeriesOptions = defaultPlotOptions.line;
  1416. // set the default time methods
  1417. setTimeMethods();
  1418. /**
  1419. * Set the time methods globally based on the useUTC option. Time method can be either
  1420. * local time or UTC (default).
  1421. */
  1422. function setTimeMethods() {
  1423. var globalOptions = defaultOptions.global,
  1424. useUTC = globalOptions.useUTC,
  1425. GET = useUTC ? 'getUTC' : 'get',
  1426. SET = useUTC ? 'setUTC' : 'set';
  1427. Date = globalOptions.Date || window.Date;
  1428. timezoneOffset = useUTC && globalOptions.timezoneOffset;
  1429. getTimezoneOffset = useUTC && globalOptions.getTimezoneOffset;
  1430. makeTime = function (year, month, date, hours, minutes, seconds) {
  1431. var d;
  1432. if (useUTC) {
  1433. d = Date.UTC.apply(0, arguments);
  1434. d += getTZOffset(d);
  1435. } else {
  1436. d = new Date(
  1437. year,
  1438. month,
  1439. pick(date, 1),
  1440. pick(hours, 0),
  1441. pick(minutes, 0),
  1442. pick(seconds, 0)
  1443. ).getTime();
  1444. }
  1445. return d;
  1446. };
  1447. getMinutes = GET + 'Minutes';
  1448. getHours = GET + 'Hours';
  1449. getDay = GET + 'Day';
  1450. getDate = GET + 'Date';
  1451. getMonth = GET + 'Month';
  1452. getFullYear = GET + 'FullYear';
  1453. setMilliseconds = SET + 'Milliseconds';
  1454. setSeconds = SET + 'Seconds';
  1455. setMinutes = SET + 'Minutes';
  1456. setHours = SET + 'Hours';
  1457. setDate = SET + 'Date';
  1458. setMonth = SET + 'Month';
  1459. setFullYear = SET + 'FullYear';
  1460. }
  1461. /**
  1462. * Merge the default options with custom options and return the new options structure
  1463. * @param {Object} options The new custom options
  1464. */
  1465. function setOptions(options) {
  1466. // Copy in the default options
  1467. defaultOptions = merge(true, defaultOptions, options);
  1468. // Apply UTC
  1469. setTimeMethods();
  1470. return defaultOptions;
  1471. }
  1472. /**
  1473. * Get the updated default options. Until 3.0.7, merely exposing defaultOptions for outside modules
  1474. * wasn't enough because the setOptions method created a new object.
  1475. */
  1476. function getOptions() {
  1477. return defaultOptions;
  1478. }
  1479. /**
  1480. * Handle color operations. The object methods are chainable.
  1481. * @param {String} input The input color in either rbga or hex format
  1482. */
  1483. var rgbaRegEx = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]?(?:\.[0-9]+)?)\s*\)/,
  1484. hexRegEx = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
  1485. rgbRegEx = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/;
  1486. var Color = function (input) {
  1487. // declare variables
  1488. var rgba = [], result, stops;
  1489. /**
  1490. * Parse the input color to rgba array
  1491. * @param {String} input
  1492. */
  1493. function init(input) {
  1494. // Gradients
  1495. if (input && input.stops) {
  1496. stops = map(input.stops, function (stop) {
  1497. return Color(stop[1]);
  1498. });
  1499. // Solid colors
  1500. } else {
  1501. // rgba
  1502. result = rgbaRegEx.exec(input);
  1503. if (result) {
  1504. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1505. } else {
  1506. // hex
  1507. result = hexRegEx.exec(input);
  1508. if (result) {
  1509. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1510. } else {
  1511. // rgb
  1512. result = rgbRegEx.exec(input);
  1513. if (result) {
  1514. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. /**
  1521. * Return the color a specified format
  1522. * @param {String} format
  1523. */
  1524. function get(format) {
  1525. var ret;
  1526. if (stops) {
  1527. ret = merge(input);
  1528. ret.stops = [].concat(ret.stops);
  1529. each(stops, function (stop, i) {
  1530. ret.stops[i] = [ret.stops[i][0], stop.get(format)];
  1531. });
  1532. // it's NaN if gradient colors on a column chart
  1533. } else if (rgba && !isNaN(rgba[0])) {
  1534. if (format === 'rgb') {
  1535. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1536. } else if (format === 'a') {
  1537. ret = rgba[3];
  1538. } else {
  1539. ret = 'rgba(' + rgba.join(',') + ')';
  1540. }
  1541. } else {
  1542. ret = input;
  1543. }
  1544. return ret;
  1545. }
  1546. /**
  1547. * Brighten the color
  1548. * @param {Number} alpha
  1549. */
  1550. function brighten(alpha) {
  1551. if (stops) {
  1552. each(stops, function (stop) {
  1553. stop.brighten(alpha);
  1554. });
  1555. } else if (isNumber(alpha) && alpha !== 0) {
  1556. var i;
  1557. for (i = 0; i < 3; i++) {
  1558. rgba[i] += pInt(alpha * 255);
  1559. if (rgba[i] < 0) {
  1560. rgba[i] = 0;
  1561. }
  1562. if (rgba[i] > 255) {
  1563. rgba[i] = 255;
  1564. }
  1565. }
  1566. }
  1567. return this;
  1568. }
  1569. /**
  1570. * Set the color's opacity to a given alpha value
  1571. * @param {Number} alpha
  1572. */
  1573. function setOpacity(alpha) {
  1574. rgba[3] = alpha;
  1575. return this;
  1576. }
  1577. // initialize: parse the input
  1578. init(input);
  1579. // public methods
  1580. return {
  1581. get: get,
  1582. brighten: brighten,
  1583. rgba: rgba,
  1584. setOpacity: setOpacity,
  1585. raw: input
  1586. };
  1587. };
  1588. /**
  1589. * A wrapper object for SVG elements
  1590. */
  1591. function SVGElement() {}
  1592. SVGElement.prototype = {
  1593. // Default base for animation
  1594. opacity: 1,
  1595. // For labels, these CSS properties are applied to the <text> node directly
  1596. textProps: ['fontSize', 'fontWeight', 'fontFamily', 'fontStyle', 'color',
  1597. 'lineHeight', 'width', 'textDecoration', 'textOverflow', 'textShadow'],
  1598. /**
  1599. * Initialize the SVG renderer
  1600. * @param {Object} renderer
  1601. * @param {String} nodeName
  1602. */
  1603. init: function (renderer, nodeName) {
  1604. var wrapper = this;
  1605. wrapper.element = nodeName === 'span' ?
  1606. createElement(nodeName) :
  1607. doc.createElementNS(SVG_NS, nodeName);
  1608. wrapper.renderer = renderer;
  1609. },
  1610. /**
  1611. * Animate a given attribute
  1612. * @param {Object} params
  1613. * @param {Number} options The same options as in jQuery animation
  1614. * @param {Function} complete Function to perform at the end of animation
  1615. */
  1616. animate: function (params, options, complete) {
  1617. var animOptions = pick(options, this.renderer.globalAnimation, true);
  1618. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1619. if (animOptions) {
  1620. animOptions = merge(animOptions, {}); //#2625
  1621. if (complete) { // allows using a callback with the global animation without overwriting it
  1622. animOptions.complete = complete;
  1623. }
  1624. animate(this, params, animOptions);
  1625. } else {
  1626. this.attr(params, null, complete);
  1627. }
  1628. return this;
  1629. },
  1630. /**
  1631. * Build an SVG gradient out of a common JavaScript configuration object
  1632. */
  1633. colorGradient: function (color, prop, elem) {
  1634. var renderer = this.renderer,
  1635. colorObject,
  1636. gradName,
  1637. gradAttr,
  1638. gradients,
  1639. gradientObject,
  1640. stops,
  1641. stopColor,
  1642. stopOpacity,
  1643. radialReference,
  1644. n,
  1645. id,
  1646. key = [];
  1647. // Apply linear or radial gradients
  1648. if (color.linearGradient) {
  1649. gradName = 'linearGradient';
  1650. } else if (color.radialGradient) {
  1651. gradName = 'radialGradient';
  1652. }
  1653. if (gradName) {
  1654. gradAttr = color[gradName];
  1655. gradients = renderer.gradients;
  1656. stops = color.stops;
  1657. radialReference = elem.radialReference;
  1658. // Keep < 2.2 kompatibility
  1659. if (isArray(gradAttr)) {
  1660. color[gradName] = gradAttr = {
  1661. x1: gradAttr[0],
  1662. y1: gradAttr[1],
  1663. x2: gradAttr[2],
  1664. y2: gradAttr[3],
  1665. gradientUnits: 'userSpaceOnUse'
  1666. };
  1667. }
  1668. // Correct the radial gradient for the radial reference system
  1669. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  1670. gradAttr = merge(gradAttr, {
  1671. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  1672. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  1673. r: gradAttr.r * radialReference[2],
  1674. gradientUnits: 'userSpaceOnUse'
  1675. });
  1676. }
  1677. // Build the unique key to detect whether we need to create a new element (#1282)
  1678. for (n in gradAttr) {
  1679. if (n !== 'id') {
  1680. key.push(n, gradAttr[n]);
  1681. }
  1682. }
  1683. for (n in stops) {
  1684. key.push(stops[n]);
  1685. }
  1686. key = key.join(',');
  1687. // Check if a gradient object with the same config object is created within this renderer
  1688. if (gradients[key]) {
  1689. id = gradients[key].attr('id');
  1690. } else {
  1691. // Set the id and create the element
  1692. gradAttr.id = id = PREFIX + idCounter++;
  1693. gradients[key] = gradientObject = renderer.createElement(gradName)
  1694. .attr(gradAttr)
  1695. .add(renderer.defs);
  1696. // The gradient needs to keep a list of stops to be able to destroy them
  1697. gradientObject.stops = [];
  1698. each(stops, function (stop) {
  1699. var stopObject;
  1700. if (stop[1].indexOf('rgba') === 0) {
  1701. colorObject = Color(stop[1]);
  1702. stopColor = colorObject.get('rgb');
  1703. stopOpacity = colorObject.get('a');
  1704. } else {
  1705. stopColor = stop[1];
  1706. stopOpacity = 1;
  1707. }
  1708. stopObject = renderer.createElement('stop').attr({
  1709. offset: stop[0],
  1710. 'stop-color': stopColor,
  1711. 'stop-opacity': stopOpacity
  1712. }).add(gradientObject);
  1713. // Add the stop element to the gradient
  1714. gradientObject.stops.push(stopObject);
  1715. });
  1716. }
  1717. // Set the reference to the gradient object
  1718. elem.setAttribute(prop, 'url(' + renderer.url + '#' + id + ')');
  1719. }
  1720. },
  1721. /**
  1722. * Apply a polyfill to the text-stroke CSS property, by copying the text element
  1723. * and apply strokes to the copy.
  1724. *
  1725. * Contrast checks at http://jsfiddle.net/highcharts/43soe9m1/2/
  1726. *
  1727. * docs: update default, document the polyfill and the limitations on hex colors and pixel values, document contrast pseudo-color
  1728. */
  1729. applyTextShadow: function (textShadow) {
  1730. var elem = this.element,
  1731. tspans,
  1732. hasContrast = textShadow.indexOf('contrast') !== -1,
  1733. styles = {},
  1734. // IE10 and IE11 report textShadow in elem.style even though it doesn't work. Check
  1735. // this again with new IE release. In exports, the rendering is passed to PhantomJS.
  1736. supports = this.renderer.forExport || (elem.style.textShadow !== UNDEFINED && !isIE);
  1737. // When the text shadow is set to contrast, use dark stroke for light text and vice versa
  1738. if (hasContrast) {
  1739. styles.textShadow = textShadow = textShadow.replace(/contrast/g, this.renderer.getContrast(elem.style.fill));
  1740. }
  1741. // Safari with retina displays as well as PhantomJS bug (#3974). Firefox does not tolerate this,
  1742. // it removes the text shadows.
  1743. if (isWebKit) {
  1744. styles.textRendering = 'geometricPrecision';
  1745. }
  1746. /* Selective side-by-side testing in supported browser (http://jsfiddle.net/highcharts/73L1ptrh/)
  1747. if (elem.textContent.indexOf('2.') === 0) {
  1748. elem.style['text-shadow'] = 'none';
  1749. supports = false;
  1750. }
  1751. // */
  1752. // No reason to polyfill, we've got native support
  1753. if (supports) {
  1754. css(elem, styles); // Apply altered textShadow or textRendering workaround
  1755. } else {
  1756. this.fakeTS = true; // Fake text shadow
  1757. // In order to get the right y position of the clones,
  1758. // copy over the y setter
  1759. this.ySetter = this.xSetter;
  1760. tspans = [].slice.call(elem.getElementsByTagName('tspan'));
  1761. each(textShadow.split(/\s?,\s?/g), function (textShadow) {
  1762. var firstChild = elem.firstChild,
  1763. color,
  1764. strokeWidth;
  1765. textShadow = textShadow.split(' ');
  1766. color = textShadow[textShadow.length - 1];
  1767. // Approximately tune the settings to the text-shadow behaviour
  1768. strokeWidth = textShadow[textShadow.length - 2];
  1769. if (strokeWidth) {
  1770. each(tspans, function (tspan, y) {
  1771. var clone;
  1772. // Let the first line start at the correct X position
  1773. if (y === 0) {
  1774. tspan.setAttribute('x', elem.getAttribute('x'));
  1775. y = elem.getAttribute('y');
  1776. tspan.setAttribute('y', y || 0);
  1777. if (y === null) {
  1778. elem.setAttribute('y', 0);
  1779. }
  1780. }
  1781. // Create the clone and apply shadow properties
  1782. clone = tspan.cloneNode(1);
  1783. attr(clone, {
  1784. 'class': PREFIX + 'text-shadow',
  1785. 'fill': color,
  1786. 'stroke': color,
  1787. 'stroke-opacity': 1 / mathMax(pInt(strokeWidth), 3),
  1788. 'stroke-width': strokeWidth,
  1789. 'stroke-linejoin': 'round'
  1790. });
  1791. elem.insertBefore(clone, firstChild);
  1792. });
  1793. }
  1794. });
  1795. }
  1796. },
  1797. /**
  1798. * Set or get a given attribute
  1799. * @param {Object|String} hash
  1800. * @param {Mixed|Undefined} val
  1801. */
  1802. attr: function (hash, val, complete) {
  1803. var key,
  1804. value,
  1805. element = this.element,
  1806. hasSetSymbolSize,
  1807. ret = this,
  1808. skipAttr;
  1809. // single key-value pair
  1810. if (typeof hash === 'string' && val !== UNDEFINED) {
  1811. key = hash;
  1812. hash = {};
  1813. hash[key] = val;
  1814. }
  1815. // used as a getter: first argument is a string, second is undefined
  1816. if (typeof hash === 'string') {
  1817. ret = (this[hash + 'Getter'] || this._defaultGetter).call(this, hash, element);
  1818. // setter
  1819. } else {
  1820. for (key in hash) {
  1821. value = hash[key];
  1822. skipAttr = false;
  1823. if (this.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1824. if (!hasSetSymbolSize) {
  1825. this.symbolAttr(hash);
  1826. hasSetSymbolSize = true;
  1827. }
  1828. skipAttr = true;
  1829. }
  1830. if (this.rotation && (key === 'x' || key === 'y')) {
  1831. this.doTransform = true;
  1832. }
  1833. if (!skipAttr) {
  1834. (this[key + 'Setter'] || this._defaultSetter).call(this, value, key, element);
  1835. }
  1836. // Let the shadow follow the main element
  1837. if (this.shadows && /^(width|height|visibility|x|y|d|transform|cx|cy|r)$/.test(key)) {
  1838. this.updateShadows(key, value);
  1839. }
  1840. }
  1841. // Update transform. Do this outside the loop to prevent redundant updating for batch setting
  1842. // of attributes.
  1843. if (this.doTransform) {
  1844. this.updateTransform();
  1845. this.doTransform = false;
  1846. }
  1847. }
  1848. // In accordance with animate, run a complete callback
  1849. if (complete) {
  1850. complete();
  1851. }
  1852. return ret;
  1853. },
  1854. updateShadows: function (key, value) {
  1855. var shadows = this.shadows,
  1856. i = shadows.length;
  1857. while (i--) {
  1858. shadows[i].setAttribute(
  1859. key,
  1860. key === 'height' ?
  1861. mathMax(value - (shadows[i].cutHeight || 0), 0) :
  1862. key === 'd' ? this.d : value
  1863. );
  1864. }
  1865. },
  1866. /**
  1867. * Add a class name to an element
  1868. */
  1869. addClass: function (className) {
  1870. var element = this.element,
  1871. currentClassName = attr(element, 'class') || '';
  1872. if (currentClassName.indexOf(className) === -1) {
  1873. attr(element, 'class', currentClassName + ' ' + className);
  1874. }
  1875. return this;
  1876. },
  1877. /* hasClass and removeClass are not (yet) needed
  1878. hasClass: function (className) {
  1879. return attr(this.element, 'class').indexOf(className) !== -1;
  1880. },
  1881. removeClass: function (className) {
  1882. attr(this.element, 'class', attr(this.element, 'class').replace(className, ''));
  1883. return this;
  1884. },
  1885. */
  1886. /**
  1887. * If one of the symbol size affecting parameters are changed,
  1888. * check all the others only once for each call to an element's
  1889. * .attr() method
  1890. * @param {Object} hash
  1891. */
  1892. symbolAttr: function (hash) {
  1893. var wrapper = this;
  1894. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  1895. wrapper[key] = pick(hash[key], wrapper[key]);
  1896. });
  1897. wrapper.attr({
  1898. d: wrapper.renderer.symbols[wrapper.symbolName](
  1899. wrapper.x,
  1900. wrapper.y,
  1901. wrapper.width,
  1902. wrapper.height,
  1903. wrapper
  1904. )
  1905. });
  1906. },
  1907. /**
  1908. * Apply a clipping path to this object
  1909. * @param {String} id
  1910. */
  1911. clip: function (clipRect) {
  1912. return this.attr('clip-path', clipRect ? 'url(' + this.renderer.url + '#' + clipRect.id + ')' : NONE);
  1913. },
  1914. /**
  1915. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  1916. * calculated attributes
  1917. * @param {Number} strokeWidth
  1918. * @param {Number} x
  1919. * @param {Number} y
  1920. * @param {Number} width
  1921. * @param {Number} height
  1922. */
  1923. crisp: function (rect) {
  1924. var wrapper = this,
  1925. key,
  1926. attribs = {},
  1927. normalizer,
  1928. strokeWidth = rect.strokeWidth || wrapper.strokeWidth || 0;
  1929. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  1930. // normalize for crisp edges
  1931. rect.x = mathFloor(rect.x || wrapper.x || 0) + normalizer;
  1932. rect.y = mathFloor(rect.y || wrapper.y || 0) + normalizer;
  1933. rect.width = mathFloor((rect.width || wrapper.width || 0) - 2 * normalizer);
  1934. rect.height = mathFloor((rect.height || wrapper.height || 0) - 2 * normalizer);
  1935. rect.strokeWidth = strokeWidth;
  1936. for (key in rect) {
  1937. if (wrapper[key] !== rect[key]) { // only set attribute if changed
  1938. wrapper[key] = attribs[key] = rect[key];
  1939. }
  1940. }
  1941. return attribs;
  1942. },
  1943. /**
  1944. * Set styles for the element
  1945. * @param {Object} styles
  1946. */
  1947. css: function (styles) {
  1948. var elemWrapper = this,
  1949. oldStyles = elemWrapper.styles,
  1950. newStyles = {},
  1951. elem = elemWrapper.element,
  1952. textWidth,
  1953. n,
  1954. serializedCss = '',
  1955. hyphenate,
  1956. hasNew = !oldStyles;
  1957. // convert legacy
  1958. if (styles && styles.color) {
  1959. styles.fill = styles.color;
  1960. }
  1961. // Filter out existing styles to increase performance (#2640)
  1962. if (oldStyles) {
  1963. for (n in styles) {
  1964. if (styles[n] !== oldStyles[n]) {
  1965. newStyles[n] = styles[n];
  1966. hasNew = true;
  1967. }
  1968. }
  1969. }
  1970. if (hasNew) {
  1971. textWidth = elemWrapper.textWidth =
  1972. (styles && styles.width && elem.nodeName.toLowerCase() === 'text' && pInt(styles.width)) ||
  1973. elemWrapper.textWidth; // #3501
  1974. // Merge the new styles with the old ones
  1975. if (oldStyles) {
  1976. styles = extend(
  1977. oldStyles,
  1978. newStyles
  1979. );
  1980. }
  1981. // store object
  1982. elemWrapper.styles = styles;
  1983. if (textWidth && (useCanVG || (!hasSVG && elemWrapper.renderer.forExport))) {
  1984. delete styles.width;
  1985. }
  1986. // serialize and set style attribute
  1987. if (isIE && !hasSVG) {
  1988. css(elemWrapper.element, styles);
  1989. } else {
  1990. /*jslint unparam: true*/
  1991. hyphenate = function (a, b) { return '-' + b.toLowerCase(); };
  1992. /*jslint unparam: false*/
  1993. for (n in styles) {
  1994. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  1995. }
  1996. attr(elem, 'style', serializedCss); // #1881
  1997. }
  1998. // re-build text
  1999. if (textWidth && elemWrapper.added) {
  2000. elemWrapper.renderer.buildText(elemWrapper);
  2001. }
  2002. }
  2003. return elemWrapper;
  2004. },
  2005. /**
  2006. * Add an event listener
  2007. * @param {String} eventType
  2008. * @param {Function} handler
  2009. */
  2010. on: function (eventType, handler) {
  2011. var svgElement = this,
  2012. element = svgElement.element;
  2013. // touch
  2014. if (hasTouch && eventType === 'click') {
  2015. element.ontouchstart = function (e) {
  2016. svgElement.touchEventFired = Date.now();
  2017. e.preventDefault();
  2018. handler.call(element, e);
  2019. };
  2020. element.onclick = function (e) {
  2021. if (userAgent.indexOf('Android') === -1 || Date.now() - (svgElement.touchEventFired || 0) > 1100) { // #2269
  2022. handler.call(element, e);
  2023. }
  2024. };
  2025. } else {
  2026. // simplest possible event model for internal use
  2027. element['on' + eventType] = handler;
  2028. }
  2029. return this;
  2030. },
  2031. /**
  2032. * Set the coordinates needed to draw a consistent radial gradient across
  2033. * pie slices regardless of positioning inside the chart. The format is
  2034. * [centerX, centerY, diameter] in pixels.
  2035. */
  2036. setRadialReference: function (coordinates) {
  2037. this.element.radialReference = coordinates;
  2038. return this;
  2039. },
  2040. /**
  2041. * Move an object and its children by x and y values
  2042. * @param {Number} x
  2043. * @param {Number} y
  2044. */
  2045. translate: function (x, y) {
  2046. return this.attr({
  2047. translateX: x,
  2048. translateY: y
  2049. });
  2050. },
  2051. /**
  2052. * Invert a group, rotate and flip
  2053. */
  2054. invert: function () {
  2055. var wrapper = this;
  2056. wrapper.inverted = true;
  2057. wrapper.updateTransform();
  2058. return wrapper;
  2059. },
  2060. /**
  2061. * Private method to update the transform attribute based on internal
  2062. * properties
  2063. */
  2064. updateTransform: function () {
  2065. var wrapper = this,
  2066. translateX = wrapper.translateX || 0,
  2067. translateY = wrapper.translateY || 0,
  2068. scaleX = wrapper.scaleX,
  2069. scaleY = wrapper.scaleY,
  2070. inverted = wrapper.inverted,
  2071. rotation = wrapper.rotation,
  2072. element = wrapper.element,
  2073. transform;
  2074. // flipping affects translate as adjustment for flipping around the group's axis
  2075. if (inverted) {
  2076. translateX += wrapper.attr('width');
  2077. translateY += wrapper.attr('height');
  2078. }
  2079. // Apply translate. Nearly all transformed elements have translation, so instead
  2080. // of checking for translate = 0, do it always (#1767, #1846).
  2081. transform = ['translate(' + translateX + ',' + translateY + ')'];
  2082. // apply rotation
  2083. if (inverted) {
  2084. transform.push('rotate(90) scale(-1,1)');
  2085. } else if (rotation) { // text rotation
  2086. transform.push('rotate(' + rotation + ' ' + (element.getAttribute('x') || 0) + ' ' + (element.getAttribute('y') || 0) + ')');
  2087. // Delete bBox memo when the rotation changes
  2088. //delete wrapper.bBox;
  2089. }
  2090. // apply scale
  2091. if (defined(scaleX) || defined(scaleY)) {
  2092. transform.push('scale(' + pick(scaleX, 1) + ' ' + pick(scaleY, 1) + ')');
  2093. }
  2094. if (transform.length) {
  2095. element.setAttribute('transform', transform.join(' '));
  2096. }
  2097. },
  2098. /**
  2099. * Bring the element to the front
  2100. */
  2101. toFront: function () {
  2102. var element = this.element;
  2103. element.parentNode.appendChild(element);
  2104. return this;
  2105. },
  2106. /**
  2107. * Break down alignment options like align, verticalAlign, x and y
  2108. * to x and y relative to the chart.
  2109. *
  2110. * @param {Object} alignOptions
  2111. * @param {Boolean} alignByTranslate
  2112. * @param {String[Object} box The box to align to, needs a width and height. When the
  2113. * box is a string, it refers to an object in the Renderer. For example, when
  2114. * box is 'spacingBox', it refers to Renderer.spacingBox which holds width, height
  2115. * x and y properties.
  2116. *
  2117. */
  2118. align: function (alignOptions, alignByTranslate, box) {
  2119. var align,
  2120. vAlign,
  2121. x,
  2122. y,
  2123. attribs = {},
  2124. alignTo,
  2125. renderer = this.renderer,
  2126. alignedObjects = renderer.alignedObjects;
  2127. // First call on instanciate
  2128. if (alignOptions) {
  2129. this.alignOptions = alignOptions;
  2130. this.alignByTranslate = alignByTranslate;
  2131. if (!box || isString(box)) { // boxes other than renderer handle this internally
  2132. this.alignTo = alignTo = box || 'renderer';
  2133. erase(alignedObjects, this); // prevent duplicates, like legendGroup after resize
  2134. alignedObjects.push(this);
  2135. box = null; // reassign it below
  2136. }
  2137. // When called on resize, no arguments are supplied
  2138. } else {
  2139. alignOptions = this.alignOptions;
  2140. alignByTranslate = this.alignByTranslate;
  2141. alignTo = this.alignTo;
  2142. }
  2143. box = pick(box, renderer[alignTo], renderer);
  2144. // Assign variables
  2145. align = alignOptions.align;
  2146. vAlign = alignOptions.verticalAlign;
  2147. x = (box.x || 0) + (alignOptions.x || 0); // default: left align
  2148. y = (box.y || 0) + (alignOptions.y || 0); // default: top align
  2149. // Align
  2150. if (align === 'right' || align === 'center') {
  2151. x += (box.width - (alignOptions.width || 0)) /
  2152. { right: 1, center: 2 }[align];
  2153. }
  2154. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2155. // Vertical align
  2156. if (vAlign === 'bottom' || vAlign === 'middle') {
  2157. y += (box.height - (alignOptions.height || 0)) /
  2158. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2159. }
  2160. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2161. // Animate only if already placed
  2162. this[this.placed ? 'animate' : 'attr'](attribs);
  2163. this.placed = true;
  2164. this.alignAttr = attribs;
  2165. return this;
  2166. },
  2167. /**
  2168. * Get the bounding box (width, height, x and y) for the element
  2169. */
  2170. getBBox: function (reload) {
  2171. var wrapper = this,
  2172. bBox,// = wrapper.bBox,
  2173. renderer = wrapper.renderer,
  2174. width,
  2175. height,
  2176. rotation = wrapper.rotation,
  2177. element = wrapper.element,
  2178. styles = wrapper.styles,
  2179. rad = rotation * deg2rad,
  2180. textStr = wrapper.textStr,
  2181. textShadow,
  2182. elemStyle = element.style,
  2183. toggleTextShadowShim,
  2184. cacheKey;
  2185. if (textStr !== UNDEFINED) {
  2186. // Properties that affect bounding box
  2187. cacheKey = ['', rotation || 0, styles && styles.fontSize, element.style.width].join(',');
  2188. // Since numbers are monospaced, and numerical labels appear a lot in a chart,
  2189. // we assume that a label of n characters has the same bounding box as others
  2190. // of the same length.
  2191. if (textStr === '' || numRegex.test(textStr)) {
  2192. cacheKey = 'num:' + textStr.toString().length + cacheKey;
  2193. // Caching all strings reduces rendering time by 4-5%.
  2194. } else {
  2195. cacheKey = textStr + cacheKey;
  2196. }
  2197. }
  2198. if (cacheKey && !reload) {
  2199. bBox = renderer.cache[cacheKey];
  2200. }
  2201. // No cache found
  2202. if (!bBox) {
  2203. // SVG elements
  2204. if (element.namespaceURI === SVG_NS || renderer.forExport) {
  2205. try { // Fails in Firefox if the container has display: none.
  2206. // When the text shadow shim is used, we need to hide the fake shadows
  2207. // to get the correct bounding box (#3872)
  2208. toggleTextShadowShim = this.fakeTS && function (display) {
  2209. each(element.querySelectorAll('.' + PREFIX + 'text-shadow'), function (tspan) {
  2210. tspan.style.display = display;
  2211. });
  2212. };
  2213. // Workaround for #3842, Firefox reporting wrong bounding box for shadows
  2214. if (isFirefox && elemStyle.textShadow) {
  2215. textShadow = elemStyle.textShadow;
  2216. elemStyle.textShadow = '';
  2217. } else if (toggleTextShadowShim) {
  2218. toggleTextShadowShim(NONE);
  2219. }
  2220. bBox = element.getBBox ?
  2221. // SVG: use extend because IE9 is not allowed to change width and height in case
  2222. // of rotation (below)
  2223. extend({}, element.getBBox()) :
  2224. // Canvas renderer and legacy IE in export mode
  2225. {
  2226. width: element.offsetWidth,
  2227. height: element.offsetHeight
  2228. };
  2229. // #3842
  2230. if (textShadow) {
  2231. elemStyle.textShadow = textShadow;
  2232. } else if (toggleTextShadowShim) {
  2233. toggleTextShadowShim('');
  2234. }
  2235. } catch (e) {}
  2236. // If the bBox is not set, the try-catch block above failed. The other condition
  2237. // is for Opera that returns a width of -Infinity on hidden elements.
  2238. if (!bBox || bBox.width < 0) {
  2239. bBox = { width: 0, height: 0 };
  2240. }
  2241. // VML Renderer or useHTML within SVG
  2242. } else {
  2243. bBox = wrapper.htmlGetBBox();
  2244. }
  2245. // True SVG elements as well as HTML elements in modern browsers using the .useHTML option
  2246. // need to compensated for rotation
  2247. if (renderer.isSVG) {
  2248. width = bBox.width;
  2249. height = bBox.height;
  2250. // Workaround for wrong bounding box in IE9 and IE10 (#1101, #1505, #1669, #2568)
  2251. if (isIE && styles && styles.fontSize === '11px' && height.toPrecision(3) === '16.9') {
  2252. bBox.height = height = 14;
  2253. }
  2254. // Adjust for rotated text
  2255. if (rotation) {
  2256. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2257. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2258. }
  2259. }
  2260. // Cache it
  2261. if (cacheKey) {
  2262. renderer.cache[cacheKey] = bBox;
  2263. }
  2264. }
  2265. return bBox;
  2266. },
  2267. /**
  2268. * Show the element
  2269. */
  2270. show: function (inherit) {
  2271. return this.attr({ visibility: inherit ? 'inherit' : VISIBLE });
  2272. },
  2273. /**
  2274. * Hide the element
  2275. */
  2276. hide: function () {
  2277. return this.attr({ visibility: HIDDEN });
  2278. },
  2279. fadeOut: function (duration) {
  2280. var elemWrapper = this;
  2281. elemWrapper.animate({
  2282. opacity: 0
  2283. }, {
  2284. duration: duration || 150,
  2285. complete: function () {
  2286. elemWrapper.attr({ y: -9999 }); // #3088, assuming we're only using this for tooltips
  2287. }
  2288. });
  2289. },
  2290. /**
  2291. * Add the element
  2292. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2293. * to append the element to the renderer.box.
  2294. */
  2295. add: function (parent) {
  2296. var renderer = this.renderer,
  2297. element = this.element,
  2298. inserted;
  2299. if (parent) {
  2300. this.parentGroup = parent;
  2301. }
  2302. // mark as inverted
  2303. this.parentInverted = parent && parent.inverted;
  2304. // build formatted text
  2305. if (this.textStr !== undefined) {
  2306. renderer.buildText(this);
  2307. }
  2308. // Mark as added
  2309. this.added = true;
  2310. // If we're adding to renderer root, or other elements in the group
  2311. // have a z index, we need to handle it
  2312. if (!parent || parent.handleZ || this.zIndex) {
  2313. inserted = this.zIndexSetter();
  2314. }
  2315. // If zIndex is not handled, append at the end
  2316. if (!inserted) {
  2317. (parent ? parent.element : renderer.box).appendChild(element);
  2318. }
  2319. // fire an event for internal hooks
  2320. if (this.onAdd) {
  2321. this.onAdd();
  2322. }
  2323. return this;
  2324. },
  2325. /**
  2326. * Removes a child either by removeChild or move to garbageBin.
  2327. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2328. */
  2329. safeRemoveChild: function (element) {
  2330. var parentNode = element.parentNode;
  2331. if (parentNode) {
  2332. parentNode.removeChild(element);
  2333. }
  2334. },
  2335. /**
  2336. * Destroy the element and element wrapper
  2337. */
  2338. destroy: function () {
  2339. var wrapper = this,
  2340. element = wrapper.element || {},
  2341. shadows = wrapper.shadows,
  2342. parentToClean = wrapper.renderer.isSVG && element.nodeName === 'SPAN' && wrapper.parentGroup,
  2343. grandParent,
  2344. key,
  2345. i;
  2346. // remove events
  2347. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = element.point = null;
  2348. stop(wrapper); // stop running animations
  2349. if (wrapper.clipPath) {
  2350. wrapper.clipPath = wrapper.clipPath.destroy();
  2351. }
  2352. // Destroy stops in case this is a gradient object
  2353. if (wrapper.stops) {
  2354. for (i = 0; i < wrapper.stops.length; i++) {
  2355. wrapper.stops[i] = wrapper.stops[i].destroy();
  2356. }
  2357. wrapper.stops = null;
  2358. }
  2359. // remove element
  2360. wrapper.safeRemoveChild(element);
  2361. // destroy shadows
  2362. if (shadows) {
  2363. each(shadows, function (shadow) {
  2364. wrapper.safeRemoveChild(shadow);
  2365. });
  2366. }
  2367. // In case of useHTML, clean up empty containers emulating SVG groups (#1960, #2393, #2697).
  2368. while (parentToClean && parentToClean.div && parentToClean.div.childNodes.length === 0) {
  2369. grandParent = parentToClean.parentGroup;
  2370. wrapper.safeRemoveChild(parentToClean.div);
  2371. delete parentToClean.div;
  2372. parentToClean = grandParent;
  2373. }
  2374. // remove from alignObjects
  2375. if (wrapper.alignTo) {
  2376. erase(wrapper.renderer.alignedObjects, wrapper);
  2377. }
  2378. for (key in wrapper) {
  2379. delete wrapper[key];
  2380. }
  2381. return null;
  2382. },
  2383. /**
  2384. * Add a shadow to the element. Must be done after the element is added to the DOM
  2385. * @param {Boolean|Object} shadowOptions
  2386. */
  2387. shadow: function (shadowOptions, group, cutOff) {
  2388. var shadows = [],
  2389. i,
  2390. shadow,
  2391. element = this.element,
  2392. strokeWidth,
  2393. shadowWidth,
  2394. shadowElementOpacity,
  2395. // compensate for inverted plot area
  2396. transform;
  2397. if (shadowOptions) {
  2398. shadowWidth = pick(shadowOptions.width, 3);
  2399. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  2400. transform = this.parentInverted ?
  2401. '(-1,-1)' :
  2402. '(' + pick(shadowOptions.offsetX, 1) + ', ' + pick(shadowOptions.offsetY, 1) + ')';
  2403. for (i = 1; i <= shadowWidth; i++) {
  2404. shadow = element.cloneNode(0);
  2405. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  2406. attr(shadow, {
  2407. 'isShadow': 'true',
  2408. 'stroke': shadowOptions.color || 'black',
  2409. 'stroke-opacity': shadowElementOpacity * i,
  2410. 'stroke-width': strokeWidth,
  2411. 'transform': 'translate' + transform,
  2412. 'fill': NONE
  2413. });
  2414. if (cutOff) {
  2415. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2416. shadow.cutHeight = strokeWidth;
  2417. }
  2418. if (group) {
  2419. group.element.appendChild(shadow);
  2420. } else {
  2421. element.parentNode.insertBefore(shadow, element);
  2422. }
  2423. shadows.push(shadow);
  2424. }
  2425. this.shadows = shadows;
  2426. }
  2427. return this;
  2428. },
  2429. xGetter: function (key) {
  2430. if (this.element.nodeName === 'circle') {
  2431. key = { x: 'cx', y: 'cy' }[key] || key;
  2432. }
  2433. return this._defaultGetter(key);
  2434. },
  2435. /**
  2436. * Get the current value of an attribute or pseudo attribute, used mainly
  2437. * for animation.
  2438. */
  2439. _defaultGetter: function (key) {
  2440. var ret = pick(this[key], this.element ? this.element.getAttribute(key) : null, 0);
  2441. if (/^[\-0-9\.]+$/.test(ret)) { // is numerical
  2442. ret = parseFloat(ret);
  2443. }
  2444. return ret;
  2445. },
  2446. dSetter: function (value, key, element) {
  2447. if (value && value.join) { // join path
  2448. value = value.join(' ');
  2449. }
  2450. if (/(NaN| {2}|^$)/.test(value)) {
  2451. value = 'M 0 0';
  2452. }
  2453. element.setAttribute(key, value);
  2454. this[key] = value;
  2455. },
  2456. dashstyleSetter: function (value) {
  2457. var i;
  2458. value = value && value.toLowerCase();
  2459. if (value) {
  2460. value = value
  2461. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  2462. .replace('shortdashdot', '3,1,1,1')
  2463. .replace('shortdot', '1,1,')
  2464. .replace('shortdash', '3,1,')
  2465. .replace('longdash', '8,3,')
  2466. .replace(/dot/g, '1,3,')
  2467. .replace('dash', '4,3,')
  2468. .replace(/,$/, '')
  2469. .split(','); // ending comma
  2470. i = value.length;
  2471. while (i--) {
  2472. value[i] = pInt(value[i]) * this['stroke-width'];
  2473. }
  2474. value = value.join(',')
  2475. .replace('NaN', 'none'); // #3226
  2476. this.element.setAttribute('stroke-dasharray', value);
  2477. }
  2478. },
  2479. alignSetter: function (value) {
  2480. this.element.setAttribute('text-anchor', { left: 'start', center: 'middle', right: 'end' }[value]);
  2481. },
  2482. opacitySetter: function (value, key, element) {
  2483. this[key] = value;
  2484. element.setAttribute(key, value);
  2485. },
  2486. titleSetter: function (value) {
  2487. var titleNode = this.element.getElementsByTagName('title')[0];
  2488. if (!titleNode) {
  2489. titleNode = doc.createElementNS(SVG_NS, 'title');
  2490. this.element.appendChild(titleNode);
  2491. }
  2492. titleNode.appendChild(
  2493. doc.createTextNode(
  2494. (String(pick(value), '')).replace(/<[^>]*>/g, '') // #3276, #3895
  2495. )
  2496. );
  2497. },
  2498. textSetter: function (value) {
  2499. if (value !== this.textStr) {
  2500. // Delete bBox memo when the text changes
  2501. delete this.bBox;
  2502. this.textStr = value;
  2503. if (this.added) {
  2504. this.renderer.buildText(this);
  2505. }
  2506. }
  2507. },
  2508. fillSetter: function (value, key, element) {
  2509. if (typeof value === 'string') {
  2510. element.setAttribute(key, value);
  2511. } else if (value) {
  2512. this.colorGradient(value, key, element);
  2513. }
  2514. },
  2515. visibilitySetter: function (value, key, element) {
  2516. // IE9-11 doesn't handle visibilty:inherit well, so we remove the attribute instead (#2881, #3909)
  2517. if (value === 'inherit') {
  2518. element.removeAttribute(key);
  2519. } else {
  2520. element.setAttribute(key, value);
  2521. }
  2522. },
  2523. zIndexSetter: function (value, key) {
  2524. var renderer = this.renderer,
  2525. parentGroup = this.parentGroup,
  2526. parentWrapper = parentGroup || renderer,
  2527. parentNode = parentWrapper.element || renderer.box,
  2528. childNodes,
  2529. otherElement,
  2530. otherZIndex,
  2531. element = this.element,
  2532. inserted,
  2533. run = this.added,
  2534. i;
  2535. if (defined(value)) {
  2536. element.setAttribute(key, value); // So we can read it for other elements in the group
  2537. value = +value;
  2538. if (this[key] === value) { // Only update when needed (#3865)
  2539. run = false;
  2540. }
  2541. this[key] = value;
  2542. }
  2543. // Insert according to this and other elements' zIndex. Before .add() is called,
  2544. // nothing is done. Then on add, or by later calls to zIndexSetter, the node
  2545. // is placed on the right place in the DOM.
  2546. if (run) {
  2547. value = this.zIndex;
  2548. if (value && parentGroup) {
  2549. parentGroup.handleZ = true;
  2550. }
  2551. childNodes = parentNode.childNodes;
  2552. for (i = 0; i < childNodes.length && !inserted; i++) {
  2553. otherElement = childNodes[i];
  2554. otherZIndex = attr(otherElement, 'zIndex');
  2555. if (otherElement !== element && (
  2556. // Insert before the first element with a higher zIndex
  2557. pInt(otherZIndex) > value ||
  2558. // If no zIndex given, insert before the first element with a zIndex
  2559. (!defined(value) && defined(otherZIndex))
  2560. )) {
  2561. parentNode.insertBefore(element, otherElement);
  2562. inserted = true;
  2563. }
  2564. }
  2565. if (!inserted) {
  2566. parentNode.appendChild(element);
  2567. }
  2568. }
  2569. return inserted;
  2570. },
  2571. _defaultSetter: function (value, key, element) {
  2572. element.setAttribute(key, value);
  2573. }
  2574. };
  2575. // Some shared setters and getters
  2576. SVGElement.prototype.yGetter = SVGElement.prototype.xGetter;
  2577. SVGElement.prototype.translateXSetter = SVGElement.prototype.translateYSetter =
  2578. SVGElement.prototype.rotationSetter = SVGElement.prototype.verticalAlignSetter =
  2579. SVGElement.prototype.scaleXSetter = SVGElement.prototype.scaleYSetter = function (value, key) {
  2580. this[key] = value;
  2581. this.doTransform = true;
  2582. };
  2583. // WebKit and Batik have problems with a stroke-width of zero, so in this case we remove the
  2584. // stroke attribute altogether. #1270, #1369, #3065, #3072.
  2585. SVGElement.prototype['stroke-widthSetter'] = SVGElement.prototype.strokeSetter = function (value, key, element) {
  2586. this[key] = value;
  2587. // Only apply the stroke attribute if the stroke width is defined and larger than 0
  2588. if (this.stroke && this['stroke-width']) {
  2589. this.strokeWidth = this['stroke-width'];
  2590. SVGElement.prototype.fillSetter.call(this, this.stroke, 'stroke', element); // use prototype as instance may be overridden
  2591. element.setAttribute('stroke-width', this['stroke-width']);
  2592. this.hasStroke = true;
  2593. } else if (key === 'stroke-width' && value === 0 && this.hasStroke) {
  2594. element.removeAttribute('stroke');
  2595. this.hasStroke = false;
  2596. }
  2597. };
  2598. /**
  2599. * The default SVG renderer
  2600. */
  2601. var SVGRenderer = function () {
  2602. this.init.apply(this, arguments);
  2603. };
  2604. SVGRenderer.prototype = {
  2605. Element: SVGElement,
  2606. /**
  2607. * Initialize the SVGRenderer
  2608. * @param {Object} container
  2609. * @param {Number} width
  2610. * @param {Number} height
  2611. * @param {Boolean} forExport
  2612. */
  2613. init: function (container, width, height, style, forExport) {
  2614. var renderer = this,
  2615. loc = location,
  2616. boxWrapper,
  2617. element,
  2618. desc;
  2619. boxWrapper = renderer.createElement('svg')
  2620. .attr({
  2621. version: '1.1'
  2622. })
  2623. .css(this.getStyle(style));
  2624. element = boxWrapper.element;
  2625. container.appendChild(element);
  2626. // For browsers other than IE, add the namespace attribute (#1978)
  2627. if (container.innerHTML.indexOf('xmlns') === -1) {
  2628. attr(element, 'xmlns', SVG_NS);
  2629. }
  2630. // object properties
  2631. renderer.isSVG = true;
  2632. renderer.box = element;
  2633. renderer.boxWrapper = boxWrapper;
  2634. renderer.alignedObjects = [];
  2635. // Page url used for internal references. #24, #672, #1070
  2636. renderer.url = (isFirefox || isWebKit) && doc.getElementsByTagName('base').length ?
  2637. loc.href
  2638. .replace(/#.*?$/, '') // remove the hash
  2639. .replace(/([\('\)])/g, '\\$1') // escape parantheses and quotes
  2640. .replace(/ /g, '%20') : // replace spaces (needed for Safari only)
  2641. '';
  2642. // Add description
  2643. desc = this.createElement('desc').add();
  2644. desc.element.appendChild(doc.createTextNode('Created with ' + PRODUCT + ' ' + VERSION));
  2645. renderer.defs = this.createElement('defs').add();
  2646. renderer.forExport = forExport;
  2647. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2648. renderer.cache = {}; // Cache for numerical bounding boxes
  2649. renderer.setSize(width, height, false);
  2650. // Issue 110 workaround:
  2651. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2652. // between pixels. The container itself doesn't display this, but an SVG element
  2653. // inside this container will be drawn at subpixel precision. In order to draw
  2654. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2655. // iframes though (like in jsFiddle).
  2656. var subPixelFix, rect;
  2657. if (isFirefox && container.getBoundingClientRect) {
  2658. renderer.subPixelFix = subPixelFix = function () {
  2659. css(container, { left: 0, top: 0 });
  2660. rect = container.getBoundingClientRect();
  2661. css(container, {
  2662. left: (mathCeil(rect.left) - rect.left) + PX,
  2663. top: (mathCeil(rect.top) - rect.top) + PX
  2664. });
  2665. };
  2666. // run the fix now
  2667. subPixelFix();
  2668. // run it on resize
  2669. addEvent(win, 'resize', subPixelFix);
  2670. }
  2671. },
  2672. getStyle: function (style) {
  2673. return (this.style = extend({
  2674. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Arial, Helvetica, sans-serif', // default font
  2675. fontSize: '12px'
  2676. }, style));
  2677. },
  2678. /**
  2679. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2680. * has display: none. #608.
  2681. */
  2682. isHidden: function () {
  2683. return !this.boxWrapper.getBBox().width;
  2684. },
  2685. /**
  2686. * Destroys the renderer and its allocated members.
  2687. */
  2688. destroy: function () {
  2689. var renderer = this,
  2690. rendererDefs = renderer.defs;
  2691. renderer.box = null;
  2692. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2693. // Call destroy on all gradient elements
  2694. destroyObjectProperties(renderer.gradients || {});
  2695. renderer.gradients = null;
  2696. // Defs are null in VMLRenderer
  2697. // Otherwise, destroy them here.
  2698. if (rendererDefs) {
  2699. renderer.defs = rendererDefs.destroy();
  2700. }
  2701. // Remove sub pixel fix handler
  2702. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2703. // See issue #982
  2704. if (renderer.subPixelFix) {
  2705. removeEvent(win, 'resize', renderer.subPixelFix);
  2706. }
  2707. renderer.alignedObjects = null;
  2708. return null;
  2709. },
  2710. /**
  2711. * Create a wrapper for an SVG element
  2712. * @param {Object} nodeName
  2713. */
  2714. createElement: function (nodeName) {
  2715. var wrapper = new this.Element();
  2716. wrapper.init(this, nodeName);
  2717. return wrapper;
  2718. },
  2719. /**
  2720. * Dummy function for use in canvas renderer
  2721. */
  2722. draw: function () {},
  2723. /**
  2724. * Parse a simple HTML string into SVG tspans
  2725. *
  2726. * @param {Object} textNode The parent text SVG node
  2727. */
  2728. buildText: function (wrapper) {
  2729. var textNode = wrapper.element,
  2730. renderer = this,
  2731. forExport = renderer.forExport,
  2732. textStr = pick(wrapper.textStr, '').toString(),
  2733. hasMarkup = textStr.indexOf('<') !== -1,
  2734. lines,
  2735. childNodes = textNode.childNodes,
  2736. styleRegex,
  2737. hrefRegex,
  2738. parentX = attr(textNode, 'x'),
  2739. textStyles = wrapper.styles,
  2740. width = wrapper.textWidth,
  2741. textLineHeight = textStyles && textStyles.lineHeight,
  2742. textShadow = textStyles && textStyles.textShadow,
  2743. ellipsis = textStyles && textStyles.textOverflow === 'ellipsis',
  2744. i = childNodes.length,
  2745. tempParent = width && !wrapper.added && this.box,
  2746. getLineHeight = function (tspan) {
  2747. return textLineHeight ?
  2748. pInt(textLineHeight) :
  2749. renderer.fontMetrics(
  2750. /(px|em)$/.test(tspan && tspan.style.fontSize) ?
  2751. tspan.style.fontSize :
  2752. ((textStyles && textStyles.fontSize) || renderer.style.fontSize || 12),
  2753. tspan
  2754. ).h;
  2755. },
  2756. unescapeAngleBrackets = function (inputStr) {
  2757. return inputStr.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
  2758. };
  2759. /// remove old text
  2760. while (i--) {
  2761. textNode.removeChild(childNodes[i]);
  2762. }
  2763. // Skip tspans, add text directly to text node. The forceTSpan is a hook
  2764. // used in text outline hack.
  2765. if (!hasMarkup && !textShadow && !ellipsis && textStr.indexOf(' ') === -1) {
  2766. textNode.appendChild(doc.createTextNode(unescapeAngleBrackets(textStr)));
  2767. return;
  2768. // Complex strings, add more logic
  2769. } else {
  2770. styleRegex = /<.*style="([^"]+)".*>/;
  2771. hrefRegex = /<.*href="(http[^"]+)".*>/;
  2772. if (tempParent) {
  2773. tempParent.appendChild(textNode); // attach it to the DOM to read offset width
  2774. }
  2775. if (hasMarkup) {
  2776. lines = textStr
  2777. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2778. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2779. .replace(/<a/g, '<span')
  2780. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2781. .split(/<br.*?>/g);
  2782. } else {
  2783. lines = [textStr];
  2784. }
  2785. // remove empty line at end
  2786. if (lines[lines.length - 1] === '') {
  2787. lines.pop();
  2788. }
  2789. // build the lines
  2790. each(lines, function (line, lineNo) {
  2791. var spans, spanNo = 0;
  2792. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2793. spans = line.split('|||');
  2794. each(spans, function (span) {
  2795. if (span !== '' || spans.length === 1) {
  2796. var attributes = {},
  2797. tspan = doc.createElementNS(SVG_NS, 'tspan'),
  2798. spanStyle; // #390
  2799. if (styleRegex.test(span)) {
  2800. spanStyle = span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2');
  2801. attr(tspan, 'style', spanStyle);
  2802. }
  2803. if (hrefRegex.test(span) && !forExport) { // Not for export - #1529
  2804. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2805. css(tspan, { cursor: 'pointer' });
  2806. }
  2807. span = unescapeAngleBrackets(span.replace(/<(.|\n)*?>/g, '') || ' ');
  2808. // Nested tags aren't supported, and cause crash in Safari (#1596)
  2809. if (span !== ' ') {
  2810. // add the text node
  2811. tspan.appendChild(doc.createTextNode(span));
  2812. if (!spanNo) { // first span in a line, align it to the left
  2813. if (lineNo && parentX !== null) {
  2814. attributes.x = parentX;
  2815. }
  2816. } else {
  2817. attributes.dx = 0; // #16
  2818. }
  2819. // add attributes
  2820. attr(tspan, attributes);
  2821. // Append it
  2822. textNode.appendChild(tspan);
  2823. // first span on subsequent line, add the line height
  2824. if (!spanNo && lineNo) {
  2825. // allow getting the right offset height in exporting in IE
  2826. if (!hasSVG && forExport) {
  2827. css(tspan, { display: 'block' });
  2828. }
  2829. // Set the line height based on the font size of either
  2830. // the text element or the tspan element
  2831. attr(
  2832. tspan,
  2833. 'dy',
  2834. getLineHeight(tspan)
  2835. );
  2836. }
  2837. /*if (width) {
  2838. renderer.breakText(wrapper, width);
  2839. }*/
  2840. // Check width and apply soft breaks or ellipsis
  2841. if (width) {
  2842. var words = span.replace(/([^\^])-/g, '$1- ').split(' '), // #1273
  2843. hasWhiteSpace = spans.length > 1 || lineNo || (words.length > 1 && textStyles.whiteSpace !== 'nowrap'),
  2844. tooLong,
  2845. wasTooLong,
  2846. actualWidth,
  2847. rest = [],
  2848. dy = getLineHeight(tspan),
  2849. softLineNo = 1,
  2850. rotation = wrapper.rotation,
  2851. wordStr = span, // for ellipsis
  2852. cursor = wordStr.length, // binary search cursor
  2853. bBox;
  2854. while ((hasWhiteSpace || ellipsis) && (words.length || rest.length)) {
  2855. wrapper.rotation = 0; // discard rotation when computing box
  2856. bBox = wrapper.getBBox(true);
  2857. actualWidth = bBox.width;
  2858. // Old IE cannot measure the actualWidth for SVG elements (#2314)
  2859. if (!hasSVG && renderer.forExport) {
  2860. actualWidth = renderer.measureSpanWidth(tspan.firstChild.data, wrapper.styles);
  2861. }
  2862. tooLong = actualWidth > width;
  2863. // For ellipsis, do a binary search for the correct string length
  2864. if (wasTooLong === undefined) {
  2865. wasTooLong = tooLong; // First time
  2866. }
  2867. if (ellipsis && wasTooLong) {
  2868. cursor /= 2;
  2869. if (wordStr === '' || (!tooLong && cursor < 0.5)) {
  2870. words = []; // All ok, break out
  2871. } else {
  2872. if (tooLong) {
  2873. wasTooLong = true;
  2874. }
  2875. wordStr = span.substring(0, wordStr.length + (tooLong ? -1 : 1) * mathCeil(cursor));
  2876. words = [wordStr + (width > 3 ? '\u2026' : '')];
  2877. tspan.removeChild(tspan.firstChild);
  2878. }
  2879. // Looping down, this is the first word sequence that is not too long,
  2880. // so we can move on to build the next line.
  2881. } else if (!tooLong || words.length === 1) {
  2882. words = rest;
  2883. rest = [];
  2884. if (words.length) {
  2885. softLineNo++;
  2886. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2887. attr(tspan, {
  2888. dy: dy,
  2889. x: parentX
  2890. });
  2891. if (spanStyle) { // #390
  2892. attr(tspan, 'style', spanStyle);
  2893. }
  2894. textNode.appendChild(tspan);
  2895. }
  2896. if (actualWidth > width) { // a single word is pressing it out
  2897. width = actualWidth;
  2898. }
  2899. } else { // append to existing line tspan
  2900. tspan.removeChild(tspan.firstChild);
  2901. rest.unshift(words.pop());
  2902. }
  2903. if (words.length) {
  2904. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  2905. }
  2906. }
  2907. if (wasTooLong) {
  2908. wrapper.attr('title', wrapper.textStr);
  2909. }
  2910. wrapper.rotation = rotation;
  2911. }
  2912. spanNo++;
  2913. }
  2914. }
  2915. });
  2916. });
  2917. if (tempParent) {
  2918. tempParent.removeChild(textNode); // attach it to the DOM to read offset width
  2919. }
  2920. // Apply the text shadow
  2921. if (textShadow && wrapper.applyTextShadow) {
  2922. wrapper.applyTextShadow(textShadow);
  2923. }
  2924. }
  2925. },
  2926. /*
  2927. breakText: function (wrapper, width) {
  2928. var bBox = wrapper.getBBox(),
  2929. node = wrapper.element,
  2930. textLength = node.textContent.length,
  2931. pos = mathRound(width * textLength / bBox.width), // try this position first, based on average character width
  2932. increment = 0,
  2933. finalPos;
  2934. if (bBox.width > width) {
  2935. while (finalPos === undefined) {
  2936. textLength = node.getSubStringLength(0, pos);
  2937. if (textLength <= width) {
  2938. if (increment === -1) {
  2939. finalPos = pos;
  2940. } else {
  2941. increment = 1;
  2942. }
  2943. } else {
  2944. if (increment === 1) {
  2945. finalPos = pos - 1;
  2946. } else {
  2947. increment = -1;
  2948. }
  2949. }
  2950. pos += increment;
  2951. }
  2952. }
  2953. console.log(finalPos, node.getSubStringLength(0, finalPos))
  2954. },
  2955. */
  2956. /**
  2957. * Returns white for dark colors and black for bright colors
  2958. */
  2959. getContrast: function (color) {
  2960. color = Color(color).rgba;
  2961. return color[0] + color[1] + color[2] > 384 ? '#000000' : '#FFFFFF';
  2962. },
  2963. /**
  2964. * Create a button with preset states
  2965. * @param {String} text
  2966. * @param {Number} x
  2967. * @param {Number} y
  2968. * @param {Function} callback
  2969. * @param {Object} normalState
  2970. * @param {Object} hoverState
  2971. * @param {Object} pressedState
  2972. */
  2973. button: function (text, x, y, callback, normalState, hoverState, pressedState, disabledState, shape) {
  2974. var label = this.label(text, x, y, shape, null, null, null, null, 'button'),
  2975. curState = 0,
  2976. stateOptions,
  2977. stateStyle,
  2978. normalStyle,
  2979. hoverStyle,
  2980. pressedStyle,
  2981. disabledStyle,
  2982. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  2983. // Normal state - prepare the attributes
  2984. normalState = merge({
  2985. 'stroke-width': 1,
  2986. stroke: '#CCCCCC',
  2987. fill: {
  2988. linearGradient: verticalGradient,
  2989. stops: [
  2990. [0, '#FEFEFE'],
  2991. [1, '#F6F6F6']
  2992. ]
  2993. },
  2994. r: 2,
  2995. padding: 5,
  2996. style: {
  2997. color: 'black'
  2998. }
  2999. }, normalState);
  3000. normalStyle = normalState.style;
  3001. delete normalState.style;
  3002. // Hover state
  3003. hoverState = merge(normalState, {
  3004. stroke: '#68A',
  3005. fill: {
  3006. linearGradient: verticalGradient,
  3007. stops: [
  3008. [0, '#FFF'],
  3009. [1, '#ACF']
  3010. ]
  3011. }
  3012. }, hoverState);
  3013. hoverStyle = hoverState.style;
  3014. delete hoverState.style;
  3015. // Pressed state
  3016. pressedState = merge(normalState, {
  3017. stroke: '#68A',
  3018. fill: {
  3019. linearGradient: verticalGradient,
  3020. stops: [
  3021. [0, '#9BD'],
  3022. [1, '#CDF']
  3023. ]
  3024. }
  3025. }, pressedState);
  3026. pressedStyle = pressedState.style;
  3027. delete pressedState.style;
  3028. // Disabled state
  3029. disabledState = merge(normalState, {
  3030. style: {
  3031. color: '#CCC'
  3032. }
  3033. }, disabledState);
  3034. disabledStyle = disabledState.style;
  3035. delete disabledState.style;
  3036. // Add the events. IE9 and IE10 need mouseover and mouseout to funciton (#667).
  3037. addEvent(label.element, isIE ? 'mouseover' : 'mouseenter', function () {
  3038. if (curState !== 3) {
  3039. label.attr(hoverState)
  3040. .css(hoverStyle);
  3041. }
  3042. });
  3043. addEvent(label.element, isIE ? 'mouseout' : 'mouseleave', function () {
  3044. if (curState !== 3) {
  3045. stateOptions = [normalState, hoverState, pressedState][curState];
  3046. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  3047. label.attr(stateOptions)
  3048. .css(stateStyle);
  3049. }
  3050. });
  3051. label.setState = function (state) {
  3052. label.state = curState = state;
  3053. if (!state) {
  3054. label.attr(normalState)
  3055. .css(normalStyle);
  3056. } else if (state === 2) {
  3057. label.attr(pressedState)
  3058. .css(pressedStyle);
  3059. } else if (state === 3) {
  3060. label.attr(disabledState)
  3061. .css(disabledStyle);
  3062. }
  3063. };
  3064. return label
  3065. .on('click', function (e) {
  3066. if (curState !== 3) {
  3067. callback.call(label, e);
  3068. }
  3069. })
  3070. .attr(normalState)
  3071. .css(extend({ cursor: 'default' }, normalStyle));
  3072. },
  3073. /**
  3074. * Make a straight line crisper by not spilling out to neighbour pixels
  3075. * @param {Array} points
  3076. * @param {Number} width
  3077. */
  3078. crispLine: function (points, width) {
  3079. // points format: [M, 0, 0, L, 100, 0]
  3080. // normalize to a crisp line
  3081. if (points[1] === points[4]) {
  3082. // Substract due to #1129. Now bottom and left axis gridlines behave the same.
  3083. points[1] = points[4] = mathRound(points[1]) - (width % 2 / 2);
  3084. }
  3085. if (points[2] === points[5]) {
  3086. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  3087. }
  3088. return points;
  3089. },
  3090. /**
  3091. * Draw a path
  3092. * @param {Array} path An SVG path in array form
  3093. */
  3094. path: function (path) {
  3095. var attr = {
  3096. fill: NONE
  3097. };
  3098. if (isArray(path)) {
  3099. attr.d = path;
  3100. } else if (isObject(path)) { // attributes
  3101. extend(attr, path);
  3102. }
  3103. return this.createElement('path').attr(attr);
  3104. },
  3105. /**
  3106. * Draw and return an SVG circle
  3107. * @param {Number} x The x position
  3108. * @param {Number} y The y position
  3109. * @param {Number} r The radius
  3110. */
  3111. circle: function (x, y, r) {
  3112. var attr = isObject(x) ?
  3113. x :
  3114. {
  3115. x: x,
  3116. y: y,
  3117. r: r
  3118. },
  3119. wrapper = this.createElement('circle');
  3120. wrapper.xSetter = function (value) {
  3121. this.element.setAttribute('cx', value);
  3122. };
  3123. wrapper.ySetter = function (value) {
  3124. this.element.setAttribute('cy', value);
  3125. };
  3126. return wrapper.attr(attr);
  3127. },
  3128. /**
  3129. * Draw and return an arc
  3130. * @param {Number} x X position
  3131. * @param {Number} y Y position
  3132. * @param {Number} r Radius
  3133. * @param {Number} innerR Inner radius like used in donut charts
  3134. * @param {Number} start Starting angle
  3135. * @param {Number} end Ending angle
  3136. */
  3137. arc: function (x, y, r, innerR, start, end) {
  3138. var arc;
  3139. if (isObject(x)) {
  3140. y = x.y;
  3141. r = x.r;
  3142. innerR = x.innerR;
  3143. start = x.start;
  3144. end = x.end;
  3145. x = x.x;
  3146. }
  3147. // Arcs are defined as symbols for the ability to set
  3148. // attributes in attr and animate
  3149. arc = this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  3150. innerR: innerR || 0,
  3151. start: start || 0,
  3152. end: end || 0
  3153. });
  3154. arc.r = r; // #959
  3155. return arc;
  3156. },
  3157. /**
  3158. * Draw and return a rectangle
  3159. * @param {Number} x Left position
  3160. * @param {Number} y Top position
  3161. * @param {Number} width
  3162. * @param {Number} height
  3163. * @param {Number} r Border corner radius
  3164. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  3165. */
  3166. rect: function (x, y, width, height, r, strokeWidth) {
  3167. r = isObject(x) ? x.r : r;
  3168. var wrapper = this.createElement('rect'),
  3169. attribs = isObject(x) ? x : x === UNDEFINED ? {} : {
  3170. x: x,
  3171. y: y,
  3172. width: mathMax(width, 0),
  3173. height: mathMax(height, 0)
  3174. };
  3175. if (strokeWidth !== UNDEFINED) {
  3176. attribs.strokeWidth = strokeWidth;
  3177. attribs = wrapper.crisp(attribs);
  3178. }
  3179. if (r) {
  3180. attribs.r = r;
  3181. }
  3182. wrapper.rSetter = function (value) {
  3183. attr(this.element, {
  3184. rx: value,
  3185. ry: value
  3186. });
  3187. };
  3188. return wrapper.attr(attribs);
  3189. },
  3190. /**
  3191. * Resize the box and re-align all aligned elements
  3192. * @param {Object} width
  3193. * @param {Object} height
  3194. * @param {Boolean} animate
  3195. *
  3196. */
  3197. setSize: function (width, height, animate) {
  3198. var renderer = this,
  3199. alignedObjects = renderer.alignedObjects,
  3200. i = alignedObjects.length;
  3201. renderer.width = width;
  3202. renderer.height = height;
  3203. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  3204. width: width,
  3205. height: height
  3206. });
  3207. while (i--) {
  3208. alignedObjects[i].align();
  3209. }
  3210. },
  3211. /**
  3212. * Create a group
  3213. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  3214. * This can be used for styling and scripting.
  3215. */
  3216. g: function (name) {
  3217. var elem = this.createElement('g');
  3218. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  3219. },
  3220. /**
  3221. * Display an image
  3222. * @param {String} src
  3223. * @param {Number} x
  3224. * @param {Number} y
  3225. * @param {Number} width
  3226. * @param {Number} height
  3227. */
  3228. image: function (src, x, y, width, height) {
  3229. var attribs = {
  3230. preserveAspectRatio: NONE
  3231. },
  3232. elemWrapper;
  3233. // optional properties
  3234. if (arguments.length > 1) {
  3235. extend(attribs, {
  3236. x: x,
  3237. y: y,
  3238. width: width,
  3239. height: height
  3240. });
  3241. }
  3242. elemWrapper = this.createElement('image').attr(attribs);
  3243. // set the href in the xlink namespace
  3244. if (elemWrapper.element.setAttributeNS) {
  3245. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3246. 'href', src);
  3247. } else {
  3248. // could be exporting in IE
  3249. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3250. elemWrapper.element.setAttribute('hc-svg-href', src);
  3251. }
  3252. return elemWrapper;
  3253. },
  3254. /**
  3255. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3256. *
  3257. * @param {Object} symbol
  3258. * @param {Object} x
  3259. * @param {Object} y
  3260. * @param {Object} radius
  3261. * @param {Object} options
  3262. */
  3263. symbol: function (symbol, x, y, width, height, options) {
  3264. var obj,
  3265. // get the symbol definition function
  3266. symbolFn = this.symbols[symbol],
  3267. // check if there's a path defined for this symbol
  3268. path = symbolFn && symbolFn(
  3269. mathRound(x),
  3270. mathRound(y),
  3271. width,
  3272. height,
  3273. options
  3274. ),
  3275. imageElement,
  3276. imageRegex = /^url\((.*?)\)$/,
  3277. imageSrc,
  3278. imageSize,
  3279. centerImage;
  3280. if (path) {
  3281. obj = this.path(path);
  3282. // expando properties for use in animate and attr
  3283. extend(obj, {
  3284. symbolName: symbol,
  3285. x: x,
  3286. y: y,
  3287. width: width,
  3288. height: height
  3289. });
  3290. if (options) {
  3291. extend(obj, options);
  3292. }
  3293. // image symbols
  3294. } else if (imageRegex.test(symbol)) {
  3295. // On image load, set the size and position
  3296. centerImage = function (img, size) {
  3297. if (img.element) { // it may be destroyed in the meantime (#1390)
  3298. img.attr({
  3299. width: size[0],
  3300. height: size[1]
  3301. });
  3302. if (!img.alignByTranslate) { // #185
  3303. img.translate(
  3304. mathRound((width - size[0]) / 2), // #1378
  3305. mathRound((height - size[1]) / 2)
  3306. );
  3307. }
  3308. }
  3309. };
  3310. imageSrc = symbol.match(imageRegex)[1];
  3311. imageSize = symbolSizes[imageSrc] || (options && options.width && options.height && [options.width, options.height]);
  3312. // Ireate the image synchronously, add attribs async
  3313. obj = this.image(imageSrc)
  3314. .attr({
  3315. x: x,
  3316. y: y
  3317. });
  3318. obj.isImg = true;
  3319. if (imageSize) {
  3320. centerImage(obj, imageSize);
  3321. } else {
  3322. // Initialize image to be 0 size so export will still function if there's no cached sizes.
  3323. obj.attr({ width: 0, height: 0 });
  3324. // Create a dummy JavaScript image to get the width and height. Due to a bug in IE < 8,
  3325. // the created element must be assigned to a variable in order to load (#292).
  3326. imageElement = createElement('img', {
  3327. onload: function () {
  3328. centerImage(obj, symbolSizes[imageSrc] = [this.width, this.height]);
  3329. },
  3330. src: imageSrc
  3331. });
  3332. }
  3333. }
  3334. return obj;
  3335. },
  3336. /**
  3337. * An extendable collection of functions for defining symbol paths.
  3338. */
  3339. symbols: {
  3340. 'circle': function (x, y, w, h) {
  3341. var cpw = 0.166 * w;
  3342. return [
  3343. M, x + w / 2, y,
  3344. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3345. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3346. 'Z'
  3347. ];
  3348. },
  3349. 'square': function (x, y, w, h) {
  3350. return [
  3351. M, x, y,
  3352. L, x + w, y,
  3353. x + w, y + h,
  3354. x, y + h,
  3355. 'Z'
  3356. ];
  3357. },
  3358. 'triangle': function (x, y, w, h) {
  3359. return [
  3360. M, x + w / 2, y,
  3361. L, x + w, y + h,
  3362. x, y + h,
  3363. 'Z'
  3364. ];
  3365. },
  3366. 'triangle-down': function (x, y, w, h) {
  3367. return [
  3368. M, x, y,
  3369. L, x + w, y,
  3370. x + w / 2, y + h,
  3371. 'Z'
  3372. ];
  3373. },
  3374. 'diamond': function (x, y, w, h) {
  3375. return [
  3376. M, x + w / 2, y,
  3377. L, x + w, y + h / 2,
  3378. x + w / 2, y + h,
  3379. x, y + h / 2,
  3380. 'Z'
  3381. ];
  3382. },
  3383. 'arc': function (x, y, w, h, options) {
  3384. var start = options.start,
  3385. radius = options.r || w || h,
  3386. end = options.end - 0.001, // to prevent cos and sin of start and end from becoming equal on 360 arcs (related: #1561)
  3387. innerRadius = options.innerR,
  3388. open = options.open,
  3389. cosStart = mathCos(start),
  3390. sinStart = mathSin(start),
  3391. cosEnd = mathCos(end),
  3392. sinEnd = mathSin(end),
  3393. longArc = options.end - start < mathPI ? 0 : 1;
  3394. return [
  3395. M,
  3396. x + radius * cosStart,
  3397. y + radius * sinStart,
  3398. 'A', // arcTo
  3399. radius, // x radius
  3400. radius, // y radius
  3401. 0, // slanting
  3402. longArc, // long or short arc
  3403. 1, // clockwise
  3404. x + radius * cosEnd,
  3405. y + radius * sinEnd,
  3406. open ? M : L,
  3407. x + innerRadius * cosEnd,
  3408. y + innerRadius * sinEnd,
  3409. 'A', // arcTo
  3410. innerRadius, // x radius
  3411. innerRadius, // y radius
  3412. 0, // slanting
  3413. longArc, // long or short arc
  3414. 0, // clockwise
  3415. x + innerRadius * cosStart,
  3416. y + innerRadius * sinStart,
  3417. open ? '' : 'Z' // close
  3418. ];
  3419. },
  3420. /**
  3421. * Callout shape used for default tooltips, also used for rounded rectangles in VML
  3422. */
  3423. callout: function (x, y, w, h, options) {
  3424. var arrowLength = 6,
  3425. halfDistance = 6,
  3426. r = mathMin((options && options.r) || 0, w, h),
  3427. safeDistance = r + halfDistance,
  3428. anchorX = options && options.anchorX,
  3429. anchorY = options && options.anchorY,
  3430. path;
  3431. path = [
  3432. 'M', x + r, y,
  3433. 'L', x + w - r, y, // top side
  3434. 'C', x + w, y, x + w, y, x + w, y + r, // top-right corner
  3435. 'L', x + w, y + h - r, // right side
  3436. 'C', x + w, y + h, x + w, y + h, x + w - r, y + h, // bottom-right corner
  3437. 'L', x + r, y + h, // bottom side
  3438. 'C', x, y + h, x, y + h, x, y + h - r, // bottom-left corner
  3439. 'L', x, y + r, // left side
  3440. 'C', x, y, x, y, x + r, y // top-right corner
  3441. ];
  3442. if (anchorX && anchorX > w && anchorY > y + safeDistance && anchorY < y + h - safeDistance) { // replace right side
  3443. path.splice(13, 3,
  3444. 'L', x + w, anchorY - halfDistance,
  3445. x + w + arrowLength, anchorY,
  3446. x + w, anchorY + halfDistance,
  3447. x + w, y + h - r
  3448. );
  3449. } else if (anchorX && anchorX < 0 && anchorY > y + safeDistance && anchorY < y + h - safeDistance) { // replace left side
  3450. path.splice(33, 3,
  3451. 'L', x, anchorY + halfDistance,
  3452. x - arrowLength, anchorY,
  3453. x, anchorY - halfDistance,
  3454. x, y + r
  3455. );
  3456. } else if (anchorY && anchorY > h && anchorX > x + safeDistance && anchorX < x + w - safeDistance) { // replace bottom
  3457. path.splice(23, 3,
  3458. 'L', anchorX + halfDistance, y + h,
  3459. anchorX, y + h + arrowLength,
  3460. anchorX - halfDistance, y + h,
  3461. x + r, y + h
  3462. );
  3463. } else if (anchorY && anchorY < 0 && anchorX > x + safeDistance && anchorX < x + w - safeDistance) { // replace top
  3464. path.splice(3, 3,
  3465. 'L', anchorX - halfDistance, y,
  3466. anchorX, y - arrowLength,
  3467. anchorX + halfDistance, y,
  3468. w - r, y
  3469. );
  3470. }
  3471. return path;
  3472. }
  3473. },
  3474. /**
  3475. * Define a clipping rectangle
  3476. * @param {String} id
  3477. * @param {Number} x
  3478. * @param {Number} y
  3479. * @param {Number} width
  3480. * @param {Number} height
  3481. */
  3482. clipRect: function (x, y, width, height) {
  3483. var wrapper,
  3484. id = PREFIX + idCounter++,
  3485. clipPath = this.createElement('clipPath').attr({
  3486. id: id
  3487. }).add(this.defs);
  3488. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3489. wrapper.id = id;
  3490. wrapper.clipPath = clipPath;
  3491. wrapper.count = 0;
  3492. return wrapper;
  3493. },
  3494. /**
  3495. * Add text to the SVG object
  3496. * @param {String} str
  3497. * @param {Number} x Left position
  3498. * @param {Number} y Top position
  3499. * @param {Boolean} useHTML Use HTML to render the text
  3500. */
  3501. text: function (str, x, y, useHTML) {
  3502. // declare variables
  3503. var renderer = this,
  3504. fakeSVG = useCanVG || (!hasSVG && renderer.forExport),
  3505. wrapper,
  3506. attr = {};
  3507. if (useHTML && !renderer.forExport) {
  3508. return renderer.html(str, x, y);
  3509. }
  3510. attr.x = Math.round(x || 0); // X is always needed for line-wrap logic
  3511. if (y) {
  3512. attr.y = Math.round(y);
  3513. }
  3514. if (str || str === 0) {
  3515. attr.text = str;
  3516. }
  3517. wrapper = renderer.createElement('text')
  3518. .attr(attr);
  3519. // Prevent wrapping from creating false offsetWidths in export in legacy IE (#1079, #1063)
  3520. if (fakeSVG) {
  3521. wrapper.css({
  3522. position: ABSOLUTE
  3523. });
  3524. }
  3525. if (!useHTML) {
  3526. wrapper.xSetter = function (value, key, element) {
  3527. var tspans = element.getElementsByTagName('tspan'),
  3528. tspan,
  3529. parentVal = element.getAttribute(key),
  3530. i;
  3531. for (i = 0; i < tspans.length; i++) {
  3532. tspan = tspans[i];
  3533. // If the x values are equal, the tspan represents a linebreak
  3534. if (tspan.getAttribute(key) === parentVal) {
  3535. tspan.setAttribute(key, value);
  3536. }
  3537. }
  3538. element.setAttribute(key, value);
  3539. };
  3540. }
  3541. return wrapper;
  3542. },
  3543. /**
  3544. * Utility to return the baseline offset and total line height from the font size
  3545. */
  3546. fontMetrics: function (fontSize, elem) {
  3547. var lineHeight,
  3548. baseline,
  3549. style;
  3550. fontSize = fontSize || this.style.fontSize;
  3551. if (elem && win.getComputedStyle) {
  3552. elem = elem.element || elem; // SVGElement
  3553. style = win.getComputedStyle(elem, "");
  3554. fontSize = style && style.fontSize; // #4309, the style doesn't exist inside a hidden iframe in Firefox
  3555. }
  3556. fontSize = /px/.test(fontSize) ? pInt(fontSize) : /em/.test(fontSize) ? parseFloat(fontSize) * 12 : 12;
  3557. // Empirical values found by comparing font size and bounding box height.
  3558. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3559. lineHeight = fontSize < 24 ? fontSize + 3 : mathRound(fontSize * 1.2);
  3560. baseline = mathRound(lineHeight * 0.8);
  3561. return {
  3562. h: lineHeight,
  3563. b: baseline,
  3564. f: fontSize
  3565. };
  3566. },
  3567. /**
  3568. * Correct X and Y positioning of a label for rotation (#1764)
  3569. */
  3570. rotCorr: function (baseline, rotation, alterY) {
  3571. var y = baseline;
  3572. if (rotation && alterY) {
  3573. y = mathMax(y * mathCos(rotation * deg2rad), 4);
  3574. }
  3575. return {
  3576. x: (-baseline / 3) * mathSin(rotation * deg2rad),
  3577. y: y
  3578. };
  3579. },
  3580. /**
  3581. * Add a label, a text item that can hold a colored or gradient background
  3582. * as well as a border and shadow.
  3583. * @param {string} str
  3584. * @param {Number} x
  3585. * @param {Number} y
  3586. * @param {String} shape
  3587. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3588. * coordinates it should be pinned to
  3589. * @param {Number} anchorY
  3590. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3591. * like renderer.text, or to the upper border of the rectangle.
  3592. * @param {String} className Class name for the group
  3593. */
  3594. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3595. var renderer = this,
  3596. wrapper = renderer.g(className),
  3597. text = renderer.text('', 0, 0, useHTML)
  3598. .attr({
  3599. zIndex: 1
  3600. }),
  3601. //.add(wrapper),
  3602. box,
  3603. bBox,
  3604. alignFactor = 0,
  3605. padding = 3,
  3606. paddingLeft = 0,
  3607. width,
  3608. height,
  3609. wrapperX,
  3610. wrapperY,
  3611. crispAdjust = 0,
  3612. deferredAttr = {},
  3613. baselineOffset,
  3614. needsBox;
  3615. /**
  3616. * This function runs after the label is added to the DOM (when the bounding box is
  3617. * available), and after the text of the label is updated to detect the new bounding
  3618. * box and reflect it in the border box.
  3619. */
  3620. function updateBoxSize() {
  3621. var boxX,
  3622. boxY,
  3623. style = text.element.style;
  3624. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) && defined(text.textStr) &&
  3625. text.getBBox(); //#3295 && 3514 box failure when string equals 0
  3626. wrapper.width = (width || bBox.width || 0) + 2 * padding + paddingLeft;
  3627. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3628. // update the label-scoped y offset
  3629. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize, text).b;
  3630. if (needsBox) {
  3631. // create the border box if it is not already present
  3632. if (!box) {
  3633. boxX = mathRound(-alignFactor * padding) + crispAdjust;
  3634. boxY = (baseline ? -baselineOffset : 0) + crispAdjust;
  3635. wrapper.box = box = shape ?
  3636. renderer.symbol(shape, boxX, boxY, wrapper.width, wrapper.height, deferredAttr) :
  3637. renderer.rect(boxX, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3638. if (!box.isImg) { // #4324, fill "none" causes it to be ignored by mouse events in IE
  3639. box.attr('fill', NONE);
  3640. }
  3641. box.add(wrapper);
  3642. }
  3643. // apply the box attributes
  3644. if (!box.isImg) { // #1630
  3645. box.attr(extend({
  3646. width: mathRound(wrapper.width),
  3647. height: mathRound(wrapper.height)
  3648. }, deferredAttr));
  3649. }
  3650. deferredAttr = null;
  3651. }
  3652. }
  3653. /**
  3654. * This function runs after setting text or padding, but only if padding is changed
  3655. */
  3656. function updateTextPadding() {
  3657. var styles = wrapper.styles,
  3658. textAlign = styles && styles.textAlign,
  3659. x = paddingLeft + padding * (1 - alignFactor),
  3660. y;
  3661. // determin y based on the baseline
  3662. y = baseline ? 0 : baselineOffset;
  3663. // compensate for alignment
  3664. if (defined(width) && bBox && (textAlign === 'center' || textAlign === 'right')) {
  3665. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3666. }
  3667. // update if anything changed
  3668. if (x !== text.x || y !== text.y) {
  3669. text.attr('x', x);
  3670. if (y !== UNDEFINED) {
  3671. text.attr('y', y);
  3672. }
  3673. }
  3674. // record current values
  3675. text.x = x;
  3676. text.y = y;
  3677. }
  3678. /**
  3679. * Set a box attribute, or defer it if the box is not yet created
  3680. * @param {Object} key
  3681. * @param {Object} value
  3682. */
  3683. function boxAttr(key, value) {
  3684. if (box) {
  3685. box.attr(key, value);
  3686. } else {
  3687. deferredAttr[key] = value;
  3688. }
  3689. }
  3690. /**
  3691. * After the text element is added, get the desired size of the border box
  3692. * and add it before the text in the DOM.
  3693. */
  3694. wrapper.onAdd = function () {
  3695. text.add(wrapper);
  3696. wrapper.attr({
  3697. text: (str || str === 0) ? str : '', // alignment is available now // #3295: 0 not rendered if given as a value
  3698. x: x,
  3699. y: y
  3700. });
  3701. if (box && defined(anchorX)) {
  3702. wrapper.attr({
  3703. anchorX: anchorX,
  3704. anchorY: anchorY
  3705. });
  3706. }
  3707. };
  3708. /*
  3709. * Add specific attribute setters.
  3710. */
  3711. // only change local variables
  3712. wrapper.widthSetter = function (value) {
  3713. width = value;
  3714. };
  3715. wrapper.heightSetter = function (value) {
  3716. height = value;
  3717. };
  3718. wrapper.paddingSetter = function (value) {
  3719. if (defined(value) && value !== padding) {
  3720. padding = wrapper.padding = value;
  3721. updateTextPadding();
  3722. }
  3723. };
  3724. wrapper.paddingLeftSetter = function (value) {
  3725. if (defined(value) && value !== paddingLeft) {
  3726. paddingLeft = value;
  3727. updateTextPadding();
  3728. }
  3729. };
  3730. // change local variable and prevent setting attribute on the group
  3731. wrapper.alignSetter = function (value) {
  3732. alignFactor = { left: 0, center: 0.5, right: 1 }[value];
  3733. };
  3734. // apply these to the box and the text alike
  3735. wrapper.textSetter = function (value) {
  3736. if (value !== UNDEFINED) {
  3737. text.textSetter(value);
  3738. }
  3739. updateBoxSize();
  3740. updateTextPadding();
  3741. };
  3742. // apply these to the box but not to the text
  3743. wrapper['stroke-widthSetter'] = function (value, key) {
  3744. if (value) {
  3745. needsBox = true;
  3746. }
  3747. crispAdjust = value % 2 / 2;
  3748. boxAttr(key, value);
  3749. };
  3750. wrapper.strokeSetter = wrapper.fillSetter = wrapper.rSetter = function (value, key) {
  3751. if (key === 'fill' && value) {
  3752. needsBox = true;
  3753. }
  3754. boxAttr(key, value);
  3755. };
  3756. wrapper.anchorXSetter = function (value, key) {
  3757. anchorX = value;
  3758. boxAttr(key, mathRound(value) - crispAdjust - wrapperX);
  3759. };
  3760. wrapper.anchorYSetter = function (value, key) {
  3761. anchorY = value;
  3762. boxAttr(key, value - wrapperY);
  3763. };
  3764. // rename attributes
  3765. wrapper.xSetter = function (value) {
  3766. wrapper.x = value; // for animation getter
  3767. if (alignFactor) {
  3768. value -= alignFactor * ((width || bBox.width) + padding);
  3769. }
  3770. wrapperX = mathRound(value);
  3771. wrapper.attr('translateX', wrapperX);
  3772. };
  3773. wrapper.ySetter = function (value) {
  3774. wrapperY = wrapper.y = mathRound(value);
  3775. wrapper.attr('translateY', wrapperY);
  3776. };
  3777. // Redirect certain methods to either the box or the text
  3778. var baseCss = wrapper.css;
  3779. return extend(wrapper, {
  3780. /**
  3781. * Pick up some properties and apply them to the text instead of the wrapper
  3782. */
  3783. css: function (styles) {
  3784. if (styles) {
  3785. var textStyles = {};
  3786. styles = merge(styles); // create a copy to avoid altering the original object (#537)
  3787. each(wrapper.textProps, function (prop) {
  3788. if (styles[prop] !== UNDEFINED) {
  3789. textStyles[prop] = styles[prop];
  3790. delete styles[prop];
  3791. }
  3792. });
  3793. text.css(textStyles);
  3794. }
  3795. return baseCss.call(wrapper, styles);
  3796. },
  3797. /**
  3798. * Return the bounding box of the box, not the group
  3799. */
  3800. getBBox: function () {
  3801. return {
  3802. width: bBox.width + 2 * padding,
  3803. height: bBox.height + 2 * padding,
  3804. x: bBox.x - padding,
  3805. y: bBox.y - padding
  3806. };
  3807. },
  3808. /**
  3809. * Apply the shadow to the box
  3810. */
  3811. shadow: function (b) {
  3812. if (box) {
  3813. box.shadow(b);
  3814. }
  3815. return wrapper;
  3816. },
  3817. /**
  3818. * Destroy and release memory.
  3819. */
  3820. destroy: function () {
  3821. // Added by button implementation
  3822. removeEvent(wrapper.element, 'mouseenter');
  3823. removeEvent(wrapper.element, 'mouseleave');
  3824. if (text) {
  3825. text = text.destroy();
  3826. }
  3827. if (box) {
  3828. box = box.destroy();
  3829. }
  3830. // Call base implementation to destroy the rest
  3831. SVGElement.prototype.destroy.call(wrapper);
  3832. // Release local pointers (#1298)
  3833. wrapper = renderer = updateBoxSize = updateTextPadding = boxAttr = null;
  3834. }
  3835. });
  3836. }
  3837. }; // end SVGRenderer
  3838. // general renderer
  3839. Renderer = SVGRenderer;
  3840. // extend SvgElement for useHTML option
  3841. extend(SVGElement.prototype, {
  3842. /**
  3843. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  3844. * by the VML renderer
  3845. */
  3846. htmlCss: function (styles) {
  3847. var wrapper = this,
  3848. element = wrapper.element,
  3849. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  3850. if (textWidth) {
  3851. delete styles.width;
  3852. wrapper.textWidth = textWidth;
  3853. wrapper.updateTransform();
  3854. }
  3855. if (styles && styles.textOverflow === 'ellipsis') {
  3856. styles.whiteSpace = 'nowrap';
  3857. styles.overflow = 'hidden';
  3858. }
  3859. wrapper.styles = extend(wrapper.styles, styles);
  3860. css(wrapper.element, styles);
  3861. return wrapper;
  3862. },
  3863. /**
  3864. * VML and useHTML method for calculating the bounding box based on offsets
  3865. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  3866. * use the cached value
  3867. *
  3868. * @return {Object} A hash containing values for x, y, width and height
  3869. */
  3870. htmlGetBBox: function () {
  3871. var wrapper = this,
  3872. element = wrapper.element;
  3873. // faking getBBox in exported SVG in legacy IE
  3874. // faking getBBox in exported SVG in legacy IE (is this a duplicate of the fix for #1079?)
  3875. if (element.nodeName === 'text') {
  3876. element.style.position = ABSOLUTE;
  3877. }
  3878. return {
  3879. x: element.offsetLeft,
  3880. y: element.offsetTop,
  3881. width: element.offsetWidth,
  3882. height: element.offsetHeight
  3883. };
  3884. },
  3885. /**
  3886. * VML override private method to update elements based on internal
  3887. * properties based on SVG transform
  3888. */
  3889. htmlUpdateTransform: function () {
  3890. // aligning non added elements is expensive
  3891. if (!this.added) {
  3892. this.alignOnAdd = true;
  3893. return;
  3894. }
  3895. var wrapper = this,
  3896. renderer = wrapper.renderer,
  3897. elem = wrapper.element,
  3898. translateX = wrapper.translateX || 0,
  3899. translateY = wrapper.translateY || 0,
  3900. x = wrapper.x || 0,
  3901. y = wrapper.y || 0,
  3902. align = wrapper.textAlign || 'left',
  3903. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  3904. shadows = wrapper.shadows,
  3905. styles = wrapper.styles;
  3906. // apply translate
  3907. css(elem, {
  3908. marginLeft: translateX,
  3909. marginTop: translateY
  3910. });
  3911. if (shadows) { // used in labels/tooltip
  3912. each(shadows, function (shadow) {
  3913. css(shadow, {
  3914. marginLeft: translateX + 1,
  3915. marginTop: translateY + 1
  3916. });
  3917. });
  3918. }
  3919. // apply inversion
  3920. if (wrapper.inverted) { // wrapper is a group
  3921. each(elem.childNodes, function (child) {
  3922. renderer.invertChild(child, elem);
  3923. });
  3924. }
  3925. if (elem.tagName === 'SPAN') {
  3926. var width,
  3927. rotation = wrapper.rotation,
  3928. baseline,
  3929. textWidth = pInt(wrapper.textWidth),
  3930. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth, wrapper.textAlign].join(',');
  3931. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  3932. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  3933. // Renderer specific handling of span rotation
  3934. if (defined(rotation)) {
  3935. wrapper.setSpanRotation(rotation, alignCorrection, baseline);
  3936. }
  3937. width = pick(wrapper.elemWidth, elem.offsetWidth);
  3938. // Update textWidth
  3939. if (width > textWidth && /[ \-]/.test(elem.textContent || elem.innerText)) { // #983, #1254
  3940. css(elem, {
  3941. width: textWidth + PX,
  3942. display: 'block',
  3943. whiteSpace: (styles && styles.whiteSpace) || 'normal' // #3331
  3944. });
  3945. width = textWidth;
  3946. }
  3947. wrapper.getSpanCorrection(width, baseline, alignCorrection, rotation, align);
  3948. }
  3949. // apply position with correction
  3950. css(elem, {
  3951. left: (x + (wrapper.xCorr || 0)) + PX,
  3952. top: (y + (wrapper.yCorr || 0)) + PX
  3953. });
  3954. // force reflow in webkit to apply the left and top on useHTML element (#1249)
  3955. if (isWebKit) {
  3956. baseline = elem.offsetHeight; // assigned to baseline for JSLint purpose
  3957. }
  3958. // record current text transform
  3959. wrapper.cTT = currentTextTransform;
  3960. }
  3961. },
  3962. /**
  3963. * Set the rotation of an individual HTML span
  3964. */
  3965. setSpanRotation: function (rotation, alignCorrection, baseline) {
  3966. var rotationStyle = {},
  3967. cssTransformKey = isIE ? '-ms-transform' : isWebKit ? '-webkit-transform' : isFirefox ? 'MozTransform' : isOpera ? '-o-transform' : '';
  3968. rotationStyle[cssTransformKey] = rotationStyle.transform = 'rotate(' + rotation + 'deg)';
  3969. rotationStyle[cssTransformKey + (isFirefox ? 'Origin' : '-origin')] = rotationStyle.transformOrigin = (alignCorrection * 100) + '% ' + baseline + 'px';
  3970. css(this.element, rotationStyle);
  3971. },
  3972. /**
  3973. * Get the correction in X and Y positioning as the element is rotated.
  3974. */
  3975. getSpanCorrection: function (width, baseline, alignCorrection) {
  3976. this.xCorr = -width * alignCorrection;
  3977. this.yCorr = -baseline;
  3978. }
  3979. });
  3980. // Extend SvgRenderer for useHTML option.
  3981. extend(SVGRenderer.prototype, {
  3982. /**
  3983. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3984. * renderer through the useHTML option.
  3985. *
  3986. * @param {String} str
  3987. * @param {Number} x
  3988. * @param {Number} y
  3989. */
  3990. html: function (str, x, y) {
  3991. var wrapper = this.createElement('span'),
  3992. element = wrapper.element,
  3993. renderer = wrapper.renderer;
  3994. // Text setter
  3995. wrapper.textSetter = function (value) {
  3996. if (value !== element.innerHTML) {
  3997. delete this.bBox;
  3998. }
  3999. element.innerHTML = this.textStr = value;
  4000. wrapper.htmlUpdateTransform();
  4001. };
  4002. // Various setters which rely on update transform
  4003. wrapper.xSetter = wrapper.ySetter = wrapper.alignSetter = wrapper.rotationSetter = function (value, key) {
  4004. if (key === 'align') {
  4005. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  4006. }
  4007. wrapper[key] = value;
  4008. wrapper.htmlUpdateTransform();
  4009. };
  4010. // Set the default attributes
  4011. wrapper.attr({
  4012. text: str,
  4013. x: mathRound(x),
  4014. y: mathRound(y)
  4015. })
  4016. .css({
  4017. position: ABSOLUTE,
  4018. fontFamily: this.style.fontFamily,
  4019. fontSize: this.style.fontSize
  4020. });
  4021. // Keep the whiteSpace style outside the wrapper.styles collection
  4022. element.style.whiteSpace = 'nowrap';
  4023. // Use the HTML specific .css method
  4024. wrapper.css = wrapper.htmlCss;
  4025. // This is specific for HTML within SVG
  4026. if (renderer.isSVG) {
  4027. wrapper.add = function (svgGroupWrapper) {
  4028. var htmlGroup,
  4029. container = renderer.box.parentNode,
  4030. parentGroup,
  4031. parents = [];
  4032. this.parentGroup = svgGroupWrapper;
  4033. // Create a mock group to hold the HTML elements
  4034. if (svgGroupWrapper) {
  4035. htmlGroup = svgGroupWrapper.div;
  4036. if (!htmlGroup) {
  4037. // Read the parent chain into an array and read from top down
  4038. parentGroup = svgGroupWrapper;
  4039. while (parentGroup) {
  4040. parents.push(parentGroup);
  4041. // Move up to the next parent group
  4042. parentGroup = parentGroup.parentGroup;
  4043. }
  4044. // Ensure dynamically updating position when any parent is translated
  4045. each(parents.reverse(), function (parentGroup) {
  4046. var htmlGroupStyle,
  4047. cls = attr(parentGroup.element, 'class');
  4048. if (cls) {
  4049. cls = { className: cls };
  4050. } // else null
  4051. // Create a HTML div and append it to the parent div to emulate
  4052. // the SVG group structure
  4053. htmlGroup = parentGroup.div = parentGroup.div || createElement(DIV, cls, {
  4054. position: ABSOLUTE,
  4055. left: (parentGroup.translateX || 0) + PX,
  4056. top: (parentGroup.translateY || 0) + PX
  4057. }, htmlGroup || container); // the top group is appended to container
  4058. // Shortcut
  4059. htmlGroupStyle = htmlGroup.style;
  4060. // Set listeners to update the HTML div's position whenever the SVG group
  4061. // position is changed
  4062. extend(parentGroup, {
  4063. translateXSetter: function (value, key) {
  4064. htmlGroupStyle.left = value + PX;
  4065. parentGroup[key] = value;
  4066. parentGroup.doTransform = true;
  4067. },
  4068. translateYSetter: function (value, key) {
  4069. htmlGroupStyle.top = value + PX;
  4070. parentGroup[key] = value;
  4071. parentGroup.doTransform = true;
  4072. }
  4073. });
  4074. wrap(parentGroup, 'visibilitySetter', function (proceed, value, key, elem) {
  4075. proceed.call(this, value, key, elem);
  4076. htmlGroupStyle[key] = value;
  4077. });
  4078. });
  4079. }
  4080. } else {
  4081. htmlGroup = container;
  4082. }
  4083. htmlGroup.appendChild(element);
  4084. // Shared with VML:
  4085. wrapper.added = true;
  4086. if (wrapper.alignOnAdd) {
  4087. wrapper.htmlUpdateTransform();
  4088. }
  4089. return wrapper;
  4090. };
  4091. }
  4092. return wrapper;
  4093. }
  4094. });
  4095. /* ****************************************************************************
  4096. * *
  4097. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4098. * *
  4099. * For applications and websites that don't need IE support, like platform *
  4100. * targeted mobile apps and web apps, this code can be removed. *
  4101. * *
  4102. *****************************************************************************/
  4103. /**
  4104. * @constructor
  4105. */
  4106. var VMLRenderer, VMLElement;
  4107. if (!hasSVG && !useCanVG) {
  4108. /**
  4109. * The VML element wrapper.
  4110. */
  4111. VMLElement = {
  4112. /**
  4113. * Initialize a new VML element wrapper. It builds the markup as a string
  4114. * to minimize DOM traffic.
  4115. * @param {Object} renderer
  4116. * @param {Object} nodeName
  4117. */
  4118. init: function (renderer, nodeName) {
  4119. var wrapper = this,
  4120. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  4121. style = ['position: ', ABSOLUTE, ';'],
  4122. isDiv = nodeName === DIV;
  4123. // divs and shapes need size
  4124. if (nodeName === 'shape' || isDiv) {
  4125. style.push('left:0;top:0;width:1px;height:1px;');
  4126. }
  4127. style.push('visibility: ', isDiv ? HIDDEN : VISIBLE);
  4128. markup.push(' style="', style.join(''), '"/>');
  4129. // create element with default attributes and style
  4130. if (nodeName) {
  4131. markup = isDiv || nodeName === 'span' || nodeName === 'img' ?
  4132. markup.join('')
  4133. : renderer.prepVML(markup);
  4134. wrapper.element = createElement(markup);
  4135. }
  4136. wrapper.renderer = renderer;
  4137. },
  4138. /**
  4139. * Add the node to the given parent
  4140. * @param {Object} parent
  4141. */
  4142. add: function (parent) {
  4143. var wrapper = this,
  4144. renderer = wrapper.renderer,
  4145. element = wrapper.element,
  4146. box = renderer.box,
  4147. inverted = parent && parent.inverted,
  4148. // get the parent node
  4149. parentNode = parent ?
  4150. parent.element || parent :
  4151. box;
  4152. // if the parent group is inverted, apply inversion on all children
  4153. if (inverted) { // only on groups
  4154. renderer.invertChild(element, parentNode);
  4155. }
  4156. // append it
  4157. parentNode.appendChild(element);
  4158. // align text after adding to be able to read offset
  4159. wrapper.added = true;
  4160. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  4161. wrapper.updateTransform();
  4162. }
  4163. // fire an event for internal hooks
  4164. if (wrapper.onAdd) {
  4165. wrapper.onAdd();
  4166. }
  4167. return wrapper;
  4168. },
  4169. /**
  4170. * VML always uses htmlUpdateTransform
  4171. */
  4172. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  4173. /**
  4174. * Set the rotation of a span with oldIE's filter
  4175. */
  4176. setSpanRotation: function () {
  4177. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  4178. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  4179. // has support for CSS3 transform. The getBBox method also needs to be updated
  4180. // to compensate for the rotation, like it currently does for SVG.
  4181. // Test case: http://jsfiddle.net/highcharts/Ybt44/
  4182. var rotation = this.rotation,
  4183. costheta = mathCos(rotation * deg2rad),
  4184. sintheta = mathSin(rotation * deg2rad);
  4185. css(this.element, {
  4186. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  4187. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  4188. ', sizingMethod=\'auto expand\')'].join('') : NONE
  4189. });
  4190. },
  4191. /**
  4192. * Get the positioning correction for the span after rotating.
  4193. */
  4194. getSpanCorrection: function (width, baseline, alignCorrection, rotation, align) {
  4195. var costheta = rotation ? mathCos(rotation * deg2rad) : 1,
  4196. sintheta = rotation ? mathSin(rotation * deg2rad) : 0,
  4197. height = pick(this.elemHeight, this.element.offsetHeight),
  4198. quad,
  4199. nonLeft = align && align !== 'left';
  4200. // correct x and y
  4201. this.xCorr = costheta < 0 && -width;
  4202. this.yCorr = sintheta < 0 && -height;
  4203. // correct for baseline and corners spilling out after rotation
  4204. quad = costheta * sintheta < 0;
  4205. this.xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  4206. this.yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  4207. // correct for the length/height of the text
  4208. if (nonLeft) {
  4209. this.xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  4210. if (rotation) {
  4211. this.yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  4212. }
  4213. css(this.element, {
  4214. textAlign: align
  4215. });
  4216. }
  4217. },
  4218. /**
  4219. * Converts a subset of an SVG path definition to its VML counterpart. Takes an array
  4220. * as the parameter and returns a string.
  4221. */
  4222. pathToVML: function (value) {
  4223. // convert paths
  4224. var i = value.length,
  4225. path = [];
  4226. while (i--) {
  4227. // Multiply by 10 to allow subpixel precision.
  4228. // Substracting half a pixel seems to make the coordinates
  4229. // align with SVG, but this hasn't been tested thoroughly
  4230. if (isNumber(value[i])) {
  4231. path[i] = mathRound(value[i] * 10) - 5;
  4232. } else if (value[i] === 'Z') { // close the path
  4233. path[i] = 'x';
  4234. } else {
  4235. path[i] = value[i];
  4236. // When the start X and end X coordinates of an arc are too close,
  4237. // they are rounded to the same value above. In this case, substract or
  4238. // add 1 from the end X and Y positions. #186, #760, #1371, #1410.
  4239. if (value.isArc && (value[i] === 'wa' || value[i] === 'at')) {
  4240. // Start and end X
  4241. if (path[i + 5] === path[i + 7]) {
  4242. path[i + 7] += value[i + 7] > value[i + 5] ? 1 : -1;
  4243. }
  4244. // Start and end Y
  4245. if (path[i + 6] === path[i + 8]) {
  4246. path[i + 8] += value[i + 8] > value[i + 6] ? 1 : -1;
  4247. }
  4248. }
  4249. }
  4250. }
  4251. // Loop up again to handle path shortcuts (#2132)
  4252. /*while (i++ < path.length) {
  4253. if (path[i] === 'H') { // horizontal line to
  4254. path[i] = 'L';
  4255. path.splice(i + 2, 0, path[i - 1]);
  4256. } else if (path[i] === 'V') { // vertical line to
  4257. path[i] = 'L';
  4258. path.splice(i + 1, 0, path[i - 2]);
  4259. }
  4260. }*/
  4261. return path.join(' ') || 'x';
  4262. },
  4263. /**
  4264. * Set the element's clipping to a predefined rectangle
  4265. *
  4266. * @param {String} id The id of the clip rectangle
  4267. */
  4268. clip: function (clipRect) {
  4269. var wrapper = this,
  4270. clipMembers,
  4271. cssRet;
  4272. if (clipRect) {
  4273. clipMembers = clipRect.members;
  4274. erase(clipMembers, wrapper); // Ensure unique list of elements (#1258)
  4275. clipMembers.push(wrapper);
  4276. wrapper.destroyClip = function () {
  4277. erase(clipMembers, wrapper);
  4278. };
  4279. cssRet = clipRect.getCSS(wrapper);
  4280. } else {
  4281. if (wrapper.destroyClip) {
  4282. wrapper.destroyClip();
  4283. }
  4284. cssRet = { clip: docMode8 ? 'inherit' : 'rect(auto)' }; // #1214
  4285. }
  4286. return wrapper.css(cssRet);
  4287. },
  4288. /**
  4289. * Set styles for the element
  4290. * @param {Object} styles
  4291. */
  4292. css: SVGElement.prototype.htmlCss,
  4293. /**
  4294. * Removes a child either by removeChild or move to garbageBin.
  4295. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4296. */
  4297. safeRemoveChild: function (element) {
  4298. // discardElement will detach the node from its parent before attaching it
  4299. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4300. if (element.parentNode) {
  4301. discardElement(element);
  4302. }
  4303. },
  4304. /**
  4305. * Extend element.destroy by removing it from the clip members array
  4306. */
  4307. destroy: function () {
  4308. if (this.destroyClip) {
  4309. this.destroyClip();
  4310. }
  4311. return SVGElement.prototype.destroy.apply(this);
  4312. },
  4313. /**
  4314. * Add an event listener. VML override for normalizing event parameters.
  4315. * @param {String} eventType
  4316. * @param {Function} handler
  4317. */
  4318. on: function (eventType, handler) {
  4319. // simplest possible event model for internal use
  4320. this.element['on' + eventType] = function () {
  4321. var evt = win.event;
  4322. evt.target = evt.srcElement;
  4323. handler(evt);
  4324. };
  4325. return this;
  4326. },
  4327. /**
  4328. * In stacked columns, cut off the shadows so that they don't overlap
  4329. */
  4330. cutOffPath: function (path, length) {
  4331. var len;
  4332. path = path.split(/[ ,]/);
  4333. len = path.length;
  4334. if (len === 9 || len === 11) {
  4335. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4336. }
  4337. return path.join(' ');
  4338. },
  4339. /**
  4340. * Apply a drop shadow by copying elements and giving them different strokes
  4341. * @param {Boolean|Object} shadowOptions
  4342. */
  4343. shadow: function (shadowOptions, group, cutOff) {
  4344. var shadows = [],
  4345. i,
  4346. element = this.element,
  4347. renderer = this.renderer,
  4348. shadow,
  4349. elemStyle = element.style,
  4350. markup,
  4351. path = element.path,
  4352. strokeWidth,
  4353. modifiedPath,
  4354. shadowWidth,
  4355. shadowElementOpacity;
  4356. // some times empty paths are not strings
  4357. if (path && typeof path.value !== 'string') {
  4358. path = 'x';
  4359. }
  4360. modifiedPath = path;
  4361. if (shadowOptions) {
  4362. shadowWidth = pick(shadowOptions.width, 3);
  4363. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  4364. for (i = 1; i <= 3; i++) {
  4365. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  4366. // Cut off shadows for stacked column items
  4367. if (cutOff) {
  4368. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4369. }
  4370. markup = ['<shape isShadow="true" strokeweight="', strokeWidth,
  4371. '" filled="false" path="', modifiedPath,
  4372. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4373. shadow = createElement(renderer.prepVML(markup),
  4374. null, {
  4375. left: pInt(elemStyle.left) + pick(shadowOptions.offsetX, 1),
  4376. top: pInt(elemStyle.top) + pick(shadowOptions.offsetY, 1)
  4377. }
  4378. );
  4379. if (cutOff) {
  4380. shadow.cutOff = strokeWidth + 1;
  4381. }
  4382. // apply the opacity
  4383. markup = ['<stroke color="', shadowOptions.color || 'black', '" opacity="', shadowElementOpacity * i, '"/>'];
  4384. createElement(renderer.prepVML(markup), null, null, shadow);
  4385. // insert it
  4386. if (group) {
  4387. group.element.appendChild(shadow);
  4388. } else {
  4389. element.parentNode.insertBefore(shadow, element);
  4390. }
  4391. // record it
  4392. shadows.push(shadow);
  4393. }
  4394. this.shadows = shadows;
  4395. }
  4396. return this;
  4397. },
  4398. updateShadows: noop, // Used in SVG only
  4399. setAttr: function (key, value) {
  4400. if (docMode8) { // IE8 setAttribute bug
  4401. this.element[key] = value;
  4402. } else {
  4403. this.element.setAttribute(key, value);
  4404. }
  4405. },
  4406. classSetter: function (value) {
  4407. // IE8 Standards mode has problems retrieving the className unless set like this
  4408. this.element.className = value;
  4409. },
  4410. dashstyleSetter: function (value, key, element) {
  4411. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4412. createElement(this.renderer.prepVML(['<stroke/>']), null, null, element);
  4413. strokeElem[key] = value || 'solid';
  4414. this[key] = value; /* because changing stroke-width will change the dash length
  4415. and cause an epileptic effect */
  4416. },
  4417. dSetter: function (value, key, element) {
  4418. var i,
  4419. shadows = this.shadows;
  4420. value = value || [];
  4421. this.d = value.join && value.join(' '); // used in getter for animation
  4422. element.path = value = this.pathToVML(value);
  4423. // update shadows
  4424. if (shadows) {
  4425. i = shadows.length;
  4426. while (i--) {
  4427. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4428. }
  4429. }
  4430. this.setAttr(key, value);
  4431. },
  4432. fillSetter: function (value, key, element) {
  4433. var nodeName = element.nodeName;
  4434. if (nodeName === 'SPAN') { // text color
  4435. element.style.color = value;
  4436. } else if (nodeName !== 'IMG') { // #1336
  4437. element.filled = value !== NONE;
  4438. this.setAttr('fillcolor', this.renderer.color(value, element, key, this));
  4439. }
  4440. },
  4441. opacitySetter: noop, // Don't bother - animation is too slow and filters introduce artifacts
  4442. rotationSetter: function (value, key, element) {
  4443. var style = element.style;
  4444. this[key] = style[key] = value; // style is for #1873
  4445. // Correction for the 1x1 size of the shape container. Used in gauge needles.
  4446. style.left = -mathRound(mathSin(value * deg2rad) + 1) + PX;
  4447. style.top = mathRound(mathCos(value * deg2rad)) + PX;
  4448. },
  4449. strokeSetter: function (value, key, element) {
  4450. this.setAttr('strokecolor', this.renderer.color(value, element, key));
  4451. },
  4452. 'stroke-widthSetter': function (value, key, element) {
  4453. element.stroked = !!value; // VML "stroked" attribute
  4454. this[key] = value; // used in getter, issue #113
  4455. if (isNumber(value)) {
  4456. value += PX;
  4457. }
  4458. this.setAttr('strokeweight', value);
  4459. },
  4460. titleSetter: function (value, key) {
  4461. this.setAttr(key, value);
  4462. },
  4463. visibilitySetter: function (value, key, element) {
  4464. // Handle inherited visibility
  4465. if (value === 'inherit') {
  4466. value = VISIBLE;
  4467. }
  4468. // Let the shadow follow the main element
  4469. if (this.shadows) {
  4470. each(this.shadows, function (shadow) {
  4471. shadow.style[key] = value;
  4472. });
  4473. }
  4474. // Instead of toggling the visibility CSS property, move the div out of the viewport.
  4475. // This works around #61 and #586
  4476. if (element.nodeName === 'DIV') {
  4477. value = value === HIDDEN ? '-999em' : 0;
  4478. // In order to redraw, IE7 needs the div to be visible when tucked away
  4479. // outside the viewport. So the visibility is actually opposite of
  4480. // the expected value. This applies to the tooltip only.
  4481. if (!docMode8) {
  4482. element.style[key] = value ? VISIBLE : HIDDEN;
  4483. }
  4484. key = 'top';
  4485. }
  4486. element.style[key] = value;
  4487. },
  4488. xSetter: function (value, key, element) {
  4489. this[key] = value; // used in getter
  4490. if (key === 'x') {
  4491. key = 'left';
  4492. } else if (key === 'y') {
  4493. key = 'top';
  4494. }/* else {
  4495. value = mathMax(0, value); // don't set width or height below zero (#311)
  4496. }*/
  4497. // clipping rectangle special
  4498. if (this.updateClipping) {
  4499. this[key] = value; // the key is now 'left' or 'top' for 'x' and 'y'
  4500. this.updateClipping();
  4501. } else {
  4502. // normal
  4503. element.style[key] = value;
  4504. }
  4505. },
  4506. zIndexSetter: function (value, key, element) {
  4507. element.style[key] = value;
  4508. }
  4509. };
  4510. Highcharts.VMLElement = VMLElement = extendClass(SVGElement, VMLElement);
  4511. // Some shared setters
  4512. VMLElement.prototype.ySetter =
  4513. VMLElement.prototype.widthSetter =
  4514. VMLElement.prototype.heightSetter =
  4515. VMLElement.prototype.xSetter;
  4516. /**
  4517. * The VML renderer
  4518. */
  4519. var VMLRendererExtension = { // inherit SVGRenderer
  4520. Element: VMLElement,
  4521. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4522. /**
  4523. * Initialize the VMLRenderer
  4524. * @param {Object} container
  4525. * @param {Number} width
  4526. * @param {Number} height
  4527. */
  4528. init: function (container, width, height, style) {
  4529. var renderer = this,
  4530. boxWrapper,
  4531. box,
  4532. css;
  4533. renderer.alignedObjects = [];
  4534. boxWrapper = renderer.createElement(DIV)
  4535. .css(extend(this.getStyle(style), { position: RELATIVE}));
  4536. box = boxWrapper.element;
  4537. container.appendChild(boxWrapper.element);
  4538. // generate the containing box
  4539. renderer.isVML = true;
  4540. renderer.box = box;
  4541. renderer.boxWrapper = boxWrapper;
  4542. renderer.cache = {};
  4543. renderer.setSize(width, height, false);
  4544. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4545. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4546. // seems to be to add the xmlns attribute and the behaviour style inline.
  4547. if (!doc.namespaces.hcv) {
  4548. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4549. // Setup default CSS (#2153, #2368, #2384)
  4550. css = 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4551. '{ behavior:url(#default#VML); display: inline-block; } ';
  4552. try {
  4553. doc.createStyleSheet().cssText = css;
  4554. } catch (e) {
  4555. doc.styleSheets[0].cssText += css;
  4556. }
  4557. }
  4558. },
  4559. /**
  4560. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4561. * has display: none
  4562. */
  4563. isHidden: function () {
  4564. return !this.box.offsetWidth;
  4565. },
  4566. /**
  4567. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4568. * for setting the CSS style to all associated members.
  4569. *
  4570. * @param {Number} x
  4571. * @param {Number} y
  4572. * @param {Number} width
  4573. * @param {Number} height
  4574. */
  4575. clipRect: function (x, y, width, height) {
  4576. // create a dummy element
  4577. var clipRect = this.createElement(),
  4578. isObj = isObject(x);
  4579. // mimic a rectangle with its style object for automatic updating in attr
  4580. return extend(clipRect, {
  4581. members: [],
  4582. count: 0,
  4583. left: (isObj ? x.x : x) + 1,
  4584. top: (isObj ? x.y : y) + 1,
  4585. width: (isObj ? x.width : width) - 1,
  4586. height: (isObj ? x.height : height) - 1,
  4587. getCSS: function (wrapper) {
  4588. var element = wrapper.element,
  4589. nodeName = element.nodeName,
  4590. isShape = nodeName === 'shape',
  4591. inverted = wrapper.inverted,
  4592. rect = this,
  4593. top = rect.top - (isShape ? element.offsetTop : 0),
  4594. left = rect.left,
  4595. right = left + rect.width,
  4596. bottom = top + rect.height,
  4597. ret = {
  4598. clip: 'rect(' +
  4599. mathRound(inverted ? left : top) + 'px,' +
  4600. mathRound(inverted ? bottom : right) + 'px,' +
  4601. mathRound(inverted ? right : bottom) + 'px,' +
  4602. mathRound(inverted ? top : left) + 'px)'
  4603. };
  4604. // issue 74 workaround
  4605. if (!inverted && docMode8 && nodeName === 'DIV') {
  4606. extend(ret, {
  4607. width: right + PX,
  4608. height: bottom + PX
  4609. });
  4610. }
  4611. return ret;
  4612. },
  4613. // used in attr and animation to update the clipping of all members
  4614. updateClipping: function () {
  4615. each(clipRect.members, function (member) {
  4616. if (member.element) { // Deleted series, like in stock/members/series-remove demo. Should be removed from members, but this will do.
  4617. member.css(clipRect.getCSS(member));
  4618. }
  4619. });
  4620. }
  4621. });
  4622. },
  4623. /**
  4624. * Take a color and return it if it's a string, make it a gradient if it's a
  4625. * gradient configuration object, and apply opacity.
  4626. *
  4627. * @param {Object} color The color or config object
  4628. */
  4629. color: function (color, elem, prop, wrapper) {
  4630. var renderer = this,
  4631. colorObject,
  4632. regexRgba = /^rgba/,
  4633. markup,
  4634. fillType,
  4635. ret = NONE;
  4636. // Check for linear or radial gradient
  4637. if (color && color.linearGradient) {
  4638. fillType = 'gradient';
  4639. } else if (color && color.radialGradient) {
  4640. fillType = 'pattern';
  4641. }
  4642. if (fillType) {
  4643. var stopColor,
  4644. stopOpacity,
  4645. gradient = color.linearGradient || color.radialGradient,
  4646. x1,
  4647. y1,
  4648. x2,
  4649. y2,
  4650. opacity1,
  4651. opacity2,
  4652. color1,
  4653. color2,
  4654. fillAttr = '',
  4655. stops = color.stops,
  4656. firstStop,
  4657. lastStop,
  4658. colors = [],
  4659. addFillNode = function () {
  4660. // Add the fill subnode. When colors attribute is used, the meanings of opacity and o:opacity2
  4661. // are reversed.
  4662. markup = ['<fill colors="' + colors.join(',') + '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4663. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  4664. createElement(renderer.prepVML(markup), null, null, elem);
  4665. };
  4666. // Extend from 0 to 1
  4667. firstStop = stops[0];
  4668. lastStop = stops[stops.length - 1];
  4669. if (firstStop[0] > 0) {
  4670. stops.unshift([
  4671. 0,
  4672. firstStop[1]
  4673. ]);
  4674. }
  4675. if (lastStop[0] < 1) {
  4676. stops.push([
  4677. 1,
  4678. lastStop[1]
  4679. ]);
  4680. }
  4681. // Compute the stops
  4682. each(stops, function (stop, i) {
  4683. if (regexRgba.test(stop[1])) {
  4684. colorObject = Color(stop[1]);
  4685. stopColor = colorObject.get('rgb');
  4686. stopOpacity = colorObject.get('a');
  4687. } else {
  4688. stopColor = stop[1];
  4689. stopOpacity = 1;
  4690. }
  4691. // Build the color attribute
  4692. colors.push((stop[0] * 100) + '% ' + stopColor);
  4693. // Only start and end opacities are allowed, so we use the first and the last
  4694. if (!i) {
  4695. opacity1 = stopOpacity;
  4696. color2 = stopColor;
  4697. } else {
  4698. opacity2 = stopOpacity;
  4699. color1 = stopColor;
  4700. }
  4701. });
  4702. // Apply the gradient to fills only.
  4703. if (prop === 'fill') {
  4704. // Handle linear gradient angle
  4705. if (fillType === 'gradient') {
  4706. x1 = gradient.x1 || gradient[0] || 0;
  4707. y1 = gradient.y1 || gradient[1] || 0;
  4708. x2 = gradient.x2 || gradient[2] || 0;
  4709. y2 = gradient.y2 || gradient[3] || 0;
  4710. fillAttr = 'angle="' + (90 - math.atan(
  4711. (y2 - y1) / // y vector
  4712. (x2 - x1) // x vector
  4713. ) * 180 / mathPI) + '"';
  4714. addFillNode();
  4715. // Radial (circular) gradient
  4716. } else {
  4717. var r = gradient.r,
  4718. sizex = r * 2,
  4719. sizey = r * 2,
  4720. cx = gradient.cx,
  4721. cy = gradient.cy,
  4722. radialReference = elem.radialReference,
  4723. bBox,
  4724. applyRadialGradient = function () {
  4725. if (radialReference) {
  4726. bBox = wrapper.getBBox();
  4727. cx += (radialReference[0] - bBox.x) / bBox.width - 0.5;
  4728. cy += (radialReference[1] - bBox.y) / bBox.height - 0.5;
  4729. sizex *= radialReference[2] / bBox.width;
  4730. sizey *= radialReference[2] / bBox.height;
  4731. }
  4732. fillAttr = 'src="' + defaultOptions.global.VMLRadialGradientURL + '" ' +
  4733. 'size="' + sizex + ',' + sizey + '" ' +
  4734. 'origin="0.5,0.5" ' +
  4735. 'position="' + cx + ',' + cy + '" ' +
  4736. 'color2="' + color2 + '" ';
  4737. addFillNode();
  4738. };
  4739. // Apply radial gradient
  4740. if (wrapper.added) {
  4741. applyRadialGradient();
  4742. } else {
  4743. // We need to know the bounding box to get the size and position right
  4744. wrapper.onAdd = applyRadialGradient;
  4745. }
  4746. // The fill element's color attribute is broken in IE8 standards mode, so we
  4747. // need to set the parent shape's fillcolor attribute instead.
  4748. ret = color1;
  4749. }
  4750. // Gradients are not supported for VML stroke, return the first color. #722.
  4751. } else {
  4752. ret = stopColor;
  4753. }
  4754. // if the color is an rgba color, split it and add a fill node
  4755. // to hold the opacity component
  4756. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4757. colorObject = Color(color);
  4758. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4759. createElement(this.prepVML(markup), null, null, elem);
  4760. ret = colorObject.get('rgb');
  4761. } else {
  4762. var propNodes = elem.getElementsByTagName(prop); // 'stroke' or 'fill' node
  4763. if (propNodes.length) {
  4764. propNodes[0].opacity = 1;
  4765. propNodes[0].type = 'solid';
  4766. }
  4767. ret = color;
  4768. }
  4769. return ret;
  4770. },
  4771. /**
  4772. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4773. * @param {Array} markup A string array of the VML markup to prepare
  4774. */
  4775. prepVML: function (markup) {
  4776. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4777. isIE8 = this.isIE8;
  4778. markup = markup.join('');
  4779. if (isIE8) { // add xmlns and style inline
  4780. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4781. if (markup.indexOf('style="') === -1) {
  4782. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4783. } else {
  4784. markup = markup.replace('style="', 'style="' + vmlStyle);
  4785. }
  4786. } else { // add namespace
  4787. markup = markup.replace('<', '<hcv:');
  4788. }
  4789. return markup;
  4790. },
  4791. /**
  4792. * Create rotated and aligned text
  4793. * @param {String} str
  4794. * @param {Number} x
  4795. * @param {Number} y
  4796. */
  4797. text: SVGRenderer.prototype.html,
  4798. /**
  4799. * Create and return a path element
  4800. * @param {Array} path
  4801. */
  4802. path: function (path) {
  4803. var attr = {
  4804. // subpixel precision down to 0.1 (width and height = 1px)
  4805. coordsize: '10 10'
  4806. };
  4807. if (isArray(path)) {
  4808. attr.d = path;
  4809. } else if (isObject(path)) { // attributes
  4810. extend(attr, path);
  4811. }
  4812. // create the shape
  4813. return this.createElement('shape').attr(attr);
  4814. },
  4815. /**
  4816. * Create and return a circle element. In VML circles are implemented as
  4817. * shapes, which is faster than v:oval
  4818. * @param {Number} x
  4819. * @param {Number} y
  4820. * @param {Number} r
  4821. */
  4822. circle: function (x, y, r) {
  4823. var circle = this.symbol('circle');
  4824. if (isObject(x)) {
  4825. r = x.r;
  4826. y = x.y;
  4827. x = x.x;
  4828. }
  4829. circle.isCircle = true; // Causes x and y to mean center (#1682)
  4830. circle.r = r;
  4831. return circle.attr({ x: x, y: y });
  4832. },
  4833. /**
  4834. * Create a group using an outer div and an inner v:group to allow rotating
  4835. * and flipping. A simple v:group would have problems with positioning
  4836. * child HTML elements and CSS clip.
  4837. *
  4838. * @param {String} name The name of the group
  4839. */
  4840. g: function (name) {
  4841. var wrapper,
  4842. attribs;
  4843. // set the class name
  4844. if (name) {
  4845. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  4846. }
  4847. // the div to hold HTML and clipping
  4848. wrapper = this.createElement(DIV).attr(attribs);
  4849. return wrapper;
  4850. },
  4851. /**
  4852. * VML override to create a regular HTML image
  4853. * @param {String} src
  4854. * @param {Number} x
  4855. * @param {Number} y
  4856. * @param {Number} width
  4857. * @param {Number} height
  4858. */
  4859. image: function (src, x, y, width, height) {
  4860. var obj = this.createElement('img')
  4861. .attr({ src: src });
  4862. if (arguments.length > 1) {
  4863. obj.attr({
  4864. x: x,
  4865. y: y,
  4866. width: width,
  4867. height: height
  4868. });
  4869. }
  4870. return obj;
  4871. },
  4872. /**
  4873. * For rectangles, VML uses a shape for rect to overcome bugs and rotation problems
  4874. */
  4875. createElement: function (nodeName) {
  4876. return nodeName === 'rect' ? this.symbol(nodeName) : SVGRenderer.prototype.createElement.call(this, nodeName);
  4877. },
  4878. /**
  4879. * In the VML renderer, each child of an inverted div (group) is inverted
  4880. * @param {Object} element
  4881. * @param {Object} parentNode
  4882. */
  4883. invertChild: function (element, parentNode) {
  4884. var ren = this,
  4885. parentStyle = parentNode.style,
  4886. imgStyle = element.tagName === 'IMG' && element.style; // #1111
  4887. css(element, {
  4888. flip: 'x',
  4889. left: pInt(parentStyle.width) - (imgStyle ? pInt(imgStyle.top) : 1),
  4890. top: pInt(parentStyle.height) - (imgStyle ? pInt(imgStyle.left) : 1),
  4891. rotation: -90
  4892. });
  4893. // Recursively invert child elements, needed for nested composite shapes like box plots and error bars. #1680, #1806.
  4894. each(element.childNodes, function (child) {
  4895. ren.invertChild(child, element);
  4896. });
  4897. },
  4898. /**
  4899. * Symbol definitions that override the parent SVG renderer's symbols
  4900. *
  4901. */
  4902. symbols: {
  4903. // VML specific arc function
  4904. arc: function (x, y, w, h, options) {
  4905. var start = options.start,
  4906. end = options.end,
  4907. radius = options.r || w || h,
  4908. innerRadius = options.innerR,
  4909. cosStart = mathCos(start),
  4910. sinStart = mathSin(start),
  4911. cosEnd = mathCos(end),
  4912. sinEnd = mathSin(end),
  4913. ret;
  4914. if (end - start === 0) { // no angle, don't show it.
  4915. return ['x'];
  4916. }
  4917. ret = [
  4918. 'wa', // clockwise arc to
  4919. x - radius, // left
  4920. y - radius, // top
  4921. x + radius, // right
  4922. y + radius, // bottom
  4923. x + radius * cosStart, // start x
  4924. y + radius * sinStart, // start y
  4925. x + radius * cosEnd, // end x
  4926. y + radius * sinEnd // end y
  4927. ];
  4928. if (options.open && !innerRadius) {
  4929. ret.push(
  4930. 'e',
  4931. M,
  4932. x,// - innerRadius,
  4933. y// - innerRadius
  4934. );
  4935. }
  4936. ret.push(
  4937. 'at', // anti clockwise arc to
  4938. x - innerRadius, // left
  4939. y - innerRadius, // top
  4940. x + innerRadius, // right
  4941. y + innerRadius, // bottom
  4942. x + innerRadius * cosEnd, // start x
  4943. y + innerRadius * sinEnd, // start y
  4944. x + innerRadius * cosStart, // end x
  4945. y + innerRadius * sinStart, // end y
  4946. 'x', // finish path
  4947. 'e' // close
  4948. );
  4949. ret.isArc = true;
  4950. return ret;
  4951. },
  4952. // Add circle symbol path. This performs significantly faster than v:oval.
  4953. circle: function (x, y, w, h, wrapper) {
  4954. if (wrapper) {
  4955. w = h = 2 * wrapper.r;
  4956. }
  4957. // Center correction, #1682
  4958. if (wrapper && wrapper.isCircle) {
  4959. x -= w / 2;
  4960. y -= h / 2;
  4961. }
  4962. // Return the path
  4963. return [
  4964. 'wa', // clockwisearcto
  4965. x, // left
  4966. y, // top
  4967. x + w, // right
  4968. y + h, // bottom
  4969. x + w, // start x
  4970. y + h / 2, // start y
  4971. x + w, // end x
  4972. y + h / 2, // end y
  4973. //'x', // finish path
  4974. 'e' // close
  4975. ];
  4976. },
  4977. /**
  4978. * Add rectangle symbol path which eases rotation and omits arcsize problems
  4979. * compared to the built-in VML roundrect shape. When borders are not rounded,
  4980. * use the simpler square path, else use the callout path without the arrow.
  4981. */
  4982. rect: function (x, y, w, h, options) {
  4983. return SVGRenderer.prototype.symbols[
  4984. !defined(options) || !options.r ? 'square' : 'callout'
  4985. ].call(0, x, y, w, h, options);
  4986. }
  4987. }
  4988. };
  4989. Highcharts.VMLRenderer = VMLRenderer = function () {
  4990. this.init.apply(this, arguments);
  4991. };
  4992. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  4993. // general renderer
  4994. Renderer = VMLRenderer;
  4995. }
  4996. // This method is used with exporting in old IE, when emulating SVG (see #2314)
  4997. SVGRenderer.prototype.measureSpanWidth = function (text, styles) {
  4998. var measuringSpan = doc.createElement('span'),
  4999. offsetWidth,
  5000. textNode = doc.createTextNode(text);
  5001. measuringSpan.appendChild(textNode);
  5002. css(measuringSpan, styles);
  5003. this.box.appendChild(measuringSpan);
  5004. offsetWidth = measuringSpan.offsetWidth;
  5005. discardElement(measuringSpan); // #2463
  5006. return offsetWidth;
  5007. };
  5008. /* ****************************************************************************
  5009. * *
  5010. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  5011. * *
  5012. *****************************************************************************/
  5013. /* ****************************************************************************
  5014. * *
  5015. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  5016. * TARGETING THAT SYSTEM. *
  5017. * *
  5018. *****************************************************************************/
  5019. var CanVGRenderer,
  5020. CanVGController;
  5021. if (useCanVG) {
  5022. /**
  5023. * The CanVGRenderer is empty from start to keep the source footprint small.
  5024. * When requested, the CanVGController downloads the rest of the source packaged
  5025. * together with the canvg library.
  5026. */
  5027. Highcharts.CanVGRenderer = CanVGRenderer = function () {
  5028. // Override the global SVG namespace to fake SVG/HTML that accepts CSS
  5029. SVG_NS = 'http://www.w3.org/1999/xhtml';
  5030. };
  5031. /**
  5032. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  5033. * the implementation from SvgRenderer will not be merged in until first render.
  5034. */
  5035. CanVGRenderer.prototype.symbols = {};
  5036. /**
  5037. * Handles on demand download of canvg rendering support.
  5038. */
  5039. CanVGController = (function () {
  5040. // List of renderering calls
  5041. var deferredRenderCalls = [];
  5042. /**
  5043. * When downloaded, we are ready to draw deferred charts.
  5044. */
  5045. function drawDeferred() {
  5046. var callLength = deferredRenderCalls.length,
  5047. callIndex;
  5048. // Draw all pending render calls
  5049. for (callIndex = 0; callIndex < callLength; callIndex++) {
  5050. deferredRenderCalls[callIndex]();
  5051. }
  5052. // Clear the list
  5053. deferredRenderCalls = [];
  5054. }
  5055. return {
  5056. push: function (func, scriptLocation) {
  5057. // Only get the script once
  5058. if (deferredRenderCalls.length === 0) {
  5059. getScript(scriptLocation, drawDeferred);
  5060. }
  5061. // Register render call
  5062. deferredRenderCalls.push(func);
  5063. }
  5064. };
  5065. }());
  5066. Renderer = CanVGRenderer;
  5067. } // end CanVGRenderer
  5068. /* ****************************************************************************
  5069. * *
  5070. * END OF ANDROID < 3 SPECIFIC CODE *
  5071. * *
  5072. *****************************************************************************/
  5073. /**
  5074. * The Tick class
  5075. */
  5076. function Tick(axis, pos, type, noLabel) {
  5077. this.axis = axis;
  5078. this.pos = pos;
  5079. this.type = type || '';
  5080. this.isNew = true;
  5081. if (!type && !noLabel) {
  5082. this.addLabel();
  5083. }
  5084. }
  5085. Tick.prototype = {
  5086. /**
  5087. * Write the tick label
  5088. */
  5089. addLabel: function () {
  5090. var tick = this,
  5091. axis = tick.axis,
  5092. options = axis.options,
  5093. chart = axis.chart,
  5094. categories = axis.categories,
  5095. names = axis.names,
  5096. pos = tick.pos,
  5097. labelOptions = options.labels,
  5098. str,
  5099. tickPositions = axis.tickPositions,
  5100. isFirst = pos === tickPositions[0],
  5101. isLast = pos === tickPositions[tickPositions.length - 1],
  5102. value = categories ?
  5103. pick(categories[pos], names[pos], pos) :
  5104. pos,
  5105. label = tick.label,
  5106. tickPositionInfo = tickPositions.info,
  5107. dateTimeLabelFormat;
  5108. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  5109. // use the general format.
  5110. if (axis.isDatetimeAxis && tickPositionInfo) {
  5111. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  5112. }
  5113. // set properties for access in render method
  5114. tick.isFirst = isFirst;
  5115. tick.isLast = isLast;
  5116. // get the string
  5117. str = axis.labelFormatter.call({
  5118. axis: axis,
  5119. chart: chart,
  5120. isFirst: isFirst,
  5121. isLast: isLast,
  5122. dateTimeLabelFormat: dateTimeLabelFormat,
  5123. value: axis.isLog ? correctFloat(lin2log(value)) : value
  5124. });
  5125. // prepare CSS
  5126. //css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  5127. // first call
  5128. if (!defined(label)) {
  5129. tick.label = label =
  5130. defined(str) && labelOptions.enabled ?
  5131. chart.renderer.text(
  5132. str,
  5133. 0,
  5134. 0,
  5135. labelOptions.useHTML
  5136. )
  5137. //.attr(attr)
  5138. // without position absolute, IE export sometimes is wrong
  5139. .css(merge(labelOptions.style))
  5140. .add(axis.labelGroup) :
  5141. null;
  5142. tick.labelLength = label && label.getBBox().width; // Un-rotated length
  5143. tick.rotation = 0; // Base value to detect change for new calls to getBBox
  5144. // update
  5145. } else if (label) {
  5146. label.attr({ text: str });
  5147. }
  5148. },
  5149. /**
  5150. * Get the offset height or width of the label
  5151. */
  5152. getLabelSize: function () {
  5153. return this.label ?
  5154. this.label.getBBox()[this.axis.horiz ? 'height' : 'width'] :
  5155. 0;
  5156. },
  5157. /**
  5158. * Handle the label overflow by adjusting the labels to the left and right edge, or
  5159. * hide them if they collide into the neighbour label.
  5160. */
  5161. handleOverflow: function (xy) {
  5162. var axis = this.axis,
  5163. pxPos = xy.x,
  5164. chartWidth = axis.chart.chartWidth,
  5165. spacing = axis.chart.spacing,
  5166. leftBound = pick(axis.labelLeft, mathMin(axis.pos, spacing[3])),
  5167. rightBound = pick(axis.labelRight, mathMax(axis.pos + axis.len, chartWidth - spacing[1])),
  5168. label = this.label,
  5169. rotation = this.rotation,
  5170. factor = { left: 0, center: 0.5, right: 1 }[axis.labelAlign],
  5171. labelWidth = label.getBBox().width,
  5172. slotWidth = axis.slotWidth,
  5173. xCorrection = factor,
  5174. goRight = 1,
  5175. leftPos,
  5176. rightPos,
  5177. textWidth,
  5178. css = {};
  5179. // Check if the label overshoots the chart spacing box. If it does, move it.
  5180. // If it now overshoots the slotWidth, add ellipsis.
  5181. if (!rotation) {
  5182. leftPos = pxPos - factor * labelWidth;
  5183. rightPos = pxPos + (1 - factor) * labelWidth;
  5184. if (leftPos < leftBound) {
  5185. slotWidth = xy.x + slotWidth * (1 - factor) - leftBound;
  5186. } else if (rightPos > rightBound) {
  5187. slotWidth = rightBound - xy.x + slotWidth * factor;
  5188. goRight = -1;
  5189. }
  5190. slotWidth = mathMin(axis.slotWidth, slotWidth); // #4177
  5191. if (slotWidth < axis.slotWidth && axis.labelAlign === 'center') {
  5192. xy.x += goRight * (axis.slotWidth - slotWidth - xCorrection * (axis.slotWidth - mathMin(labelWidth, slotWidth)));
  5193. }
  5194. // If the label width exceeds the available space, set a text width to be
  5195. // picked up below. Also, if a width has been set before, we need to set a new
  5196. // one because the reported labelWidth will be limited by the box (#3938).
  5197. if (labelWidth > slotWidth || (axis.autoRotation && label.styles.width)) {
  5198. textWidth = slotWidth;
  5199. }
  5200. // Add ellipsis to prevent rotated labels to be clipped against the edge of the chart
  5201. } else if (rotation < 0 && pxPos - factor * labelWidth < leftBound) {
  5202. textWidth = mathRound(pxPos / mathCos(rotation * deg2rad) - leftBound);
  5203. } else if (rotation > 0 && pxPos + factor * labelWidth > rightBound) {
  5204. textWidth = mathRound((chartWidth - pxPos) / mathCos(rotation * deg2rad));
  5205. }
  5206. if (textWidth) {
  5207. css.width = textWidth;
  5208. if (!axis.options.labels.style.textOverflow) {
  5209. css.textOverflow = 'ellipsis';
  5210. }
  5211. label.css(css);
  5212. }
  5213. },
  5214. /**
  5215. * Get the x and y position for ticks and labels
  5216. */
  5217. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5218. var axis = this.axis,
  5219. chart = axis.chart,
  5220. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5221. return {
  5222. x: horiz ?
  5223. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5224. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5225. y: horiz ?
  5226. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5227. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5228. };
  5229. },
  5230. /**
  5231. * Get the x, y position of the tick label
  5232. */
  5233. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5234. var axis = this.axis,
  5235. transA = axis.transA,
  5236. reversed = axis.reversed,
  5237. staggerLines = axis.staggerLines,
  5238. rotCorr = axis.tickRotCorr || { x: 0, y: 0 },
  5239. yOffset = pick(labelOptions.y, rotCorr.y + (axis.side === 2 ? 8 : -(label.getBBox().height / 2))),
  5240. line;
  5241. x = x + labelOptions.x + rotCorr.x - (tickmarkOffset && horiz ?
  5242. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5243. y = y + yOffset - (tickmarkOffset && !horiz ?
  5244. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5245. // Correct for staggered labels
  5246. if (staggerLines) {
  5247. line = (index / (step || 1) % staggerLines);
  5248. y += line * (axis.labelOffset / staggerLines);
  5249. }
  5250. return {
  5251. x: x,
  5252. y: mathRound(y)
  5253. };
  5254. },
  5255. /**
  5256. * Extendible method to return the path of the marker
  5257. */
  5258. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5259. return renderer.crispLine([
  5260. M,
  5261. x,
  5262. y,
  5263. L,
  5264. x + (horiz ? 0 : -tickLength),
  5265. y + (horiz ? tickLength : 0)
  5266. ], tickWidth);
  5267. },
  5268. /**
  5269. * Put everything in place
  5270. *
  5271. * @param index {Number}
  5272. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5273. */
  5274. render: function (index, old, opacity) {
  5275. var tick = this,
  5276. axis = tick.axis,
  5277. options = axis.options,
  5278. chart = axis.chart,
  5279. renderer = chart.renderer,
  5280. horiz = axis.horiz,
  5281. type = tick.type,
  5282. label = tick.label,
  5283. pos = tick.pos,
  5284. labelOptions = options.labels,
  5285. gridLine = tick.gridLine,
  5286. gridPrefix = type ? type + 'Grid' : 'grid',
  5287. tickPrefix = type ? type + 'Tick' : 'tick',
  5288. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5289. gridLineColor = options[gridPrefix + 'LineColor'],
  5290. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5291. tickLength = options[tickPrefix + 'Length'],
  5292. tickWidth = pick(options[tickPrefix + 'Width'], !type && axis.isXAxis ? 1 : 0), // X axis defaults to 1
  5293. tickColor = options[tickPrefix + 'Color'],
  5294. tickPosition = options[tickPrefix + 'Position'],
  5295. gridLinePath,
  5296. mark = tick.mark,
  5297. markPath,
  5298. step = /*axis.labelStep || */labelOptions.step,
  5299. attribs,
  5300. show = true,
  5301. tickmarkOffset = axis.tickmarkOffset,
  5302. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5303. x = xy.x,
  5304. y = xy.y,
  5305. reverseCrisp = ((horiz && x === axis.pos + axis.len) || (!horiz && y === axis.pos)) ? -1 : 1; // #1480, #1687
  5306. opacity = pick(opacity, 1);
  5307. this.isActive = true;
  5308. // create the grid line
  5309. if (gridLineWidth) {
  5310. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth * reverseCrisp, old, true);
  5311. if (gridLine === UNDEFINED) {
  5312. attribs = {
  5313. stroke: gridLineColor,
  5314. 'stroke-width': gridLineWidth
  5315. };
  5316. if (dashStyle) {
  5317. attribs.dashstyle = dashStyle;
  5318. }
  5319. if (!type) {
  5320. attribs.zIndex = 1;
  5321. }
  5322. if (old) {
  5323. attribs.opacity = 0;
  5324. }
  5325. tick.gridLine = gridLine =
  5326. gridLineWidth ?
  5327. renderer.path(gridLinePath)
  5328. .attr(attribs).add(axis.gridGroup) :
  5329. null;
  5330. }
  5331. // If the parameter 'old' is set, the current call will be followed
  5332. // by another call, therefore do not do any animations this time
  5333. if (!old && gridLine && gridLinePath) {
  5334. gridLine[tick.isNew ? 'attr' : 'animate']({
  5335. d: gridLinePath,
  5336. opacity: opacity
  5337. });
  5338. }
  5339. }
  5340. // create the tick mark
  5341. if (tickWidth && tickLength) {
  5342. // negate the length
  5343. if (tickPosition === 'inside') {
  5344. tickLength = -tickLength;
  5345. }
  5346. if (axis.opposite) {
  5347. tickLength = -tickLength;
  5348. }
  5349. markPath = tick.getMarkPath(x, y, tickLength, tickWidth * reverseCrisp, horiz, renderer);
  5350. if (mark) { // updating
  5351. mark.animate({
  5352. d: markPath,
  5353. opacity: opacity
  5354. });
  5355. } else { // first time
  5356. tick.mark = renderer.path(
  5357. markPath
  5358. ).attr({
  5359. stroke: tickColor,
  5360. 'stroke-width': tickWidth,
  5361. opacity: opacity
  5362. }).add(axis.axisGroup);
  5363. }
  5364. }
  5365. // the label is created on init - now move it into place
  5366. if (label && !isNaN(x)) {
  5367. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5368. // Apply show first and show last. If the tick is both first and last, it is
  5369. // a single centered tick, in which case we show the label anyway (#2100).
  5370. if ((tick.isFirst && !tick.isLast && !pick(options.showFirstLabel, 1)) ||
  5371. (tick.isLast && !tick.isFirst && !pick(options.showLastLabel, 1))) {
  5372. show = false;
  5373. // Handle label overflow and show or hide accordingly
  5374. } else if (horiz && !axis.isRadial && !labelOptions.step && !labelOptions.rotation && !old && opacity !== 0) {
  5375. tick.handleOverflow(xy);
  5376. }
  5377. // apply step
  5378. if (step && index % step) {
  5379. // show those indices dividable by step
  5380. show = false;
  5381. }
  5382. // Set the new position, and show or hide
  5383. if (show && !isNaN(xy.y)) {
  5384. xy.opacity = opacity;
  5385. label[tick.isNew ? 'attr' : 'animate'](xy);
  5386. tick.isNew = false;
  5387. } else {
  5388. label.attr('y', -9999); // #1338
  5389. }
  5390. }
  5391. },
  5392. /**
  5393. * Destructor for the tick prototype
  5394. */
  5395. destroy: function () {
  5396. destroyObjectProperties(this, this.axis);
  5397. }
  5398. };
  5399. /**
  5400. * The object wrapper for plot lines and plot bands
  5401. * @param {Object} options
  5402. */
  5403. Highcharts.PlotLineOrBand = function (axis, options) {
  5404. this.axis = axis;
  5405. if (options) {
  5406. this.options = options;
  5407. this.id = options.id;
  5408. }
  5409. };
  5410. Highcharts.PlotLineOrBand.prototype = {
  5411. /**
  5412. * Render the plot line or plot band. If it is already existing,
  5413. * move it.
  5414. */
  5415. render: function () {
  5416. var plotLine = this,
  5417. axis = plotLine.axis,
  5418. horiz = axis.horiz,
  5419. options = plotLine.options,
  5420. optionsLabel = options.label,
  5421. label = plotLine.label,
  5422. width = options.width,
  5423. to = options.to,
  5424. from = options.from,
  5425. isBand = defined(from) && defined(to),
  5426. value = options.value,
  5427. dashStyle = options.dashStyle,
  5428. svgElem = plotLine.svgElem,
  5429. path = [],
  5430. addEvent,
  5431. eventType,
  5432. xs,
  5433. ys,
  5434. x,
  5435. y,
  5436. color = options.color,
  5437. zIndex = options.zIndex,
  5438. events = options.events,
  5439. attribs = {},
  5440. renderer = axis.chart.renderer;
  5441. // logarithmic conversion
  5442. if (axis.isLog) {
  5443. from = log2lin(from);
  5444. to = log2lin(to);
  5445. value = log2lin(value);
  5446. }
  5447. // plot line
  5448. if (width) {
  5449. path = axis.getPlotLinePath(value, width);
  5450. attribs = {
  5451. stroke: color,
  5452. 'stroke-width': width
  5453. };
  5454. if (dashStyle) {
  5455. attribs.dashstyle = dashStyle;
  5456. }
  5457. } else if (isBand) { // plot band
  5458. path = axis.getPlotBandPath(from, to, options);
  5459. if (color) {
  5460. attribs.fill = color;
  5461. }
  5462. if (options.borderWidth) {
  5463. attribs.stroke = options.borderColor;
  5464. attribs['stroke-width'] = options.borderWidth;
  5465. }
  5466. } else {
  5467. return;
  5468. }
  5469. // zIndex
  5470. if (defined(zIndex)) {
  5471. attribs.zIndex = zIndex;
  5472. }
  5473. // common for lines and bands
  5474. if (svgElem) {
  5475. if (path) {
  5476. svgElem.animate({
  5477. d: path
  5478. }, null, svgElem.onGetPath);
  5479. } else {
  5480. svgElem.hide();
  5481. svgElem.onGetPath = function () {
  5482. svgElem.show();
  5483. };
  5484. if (label) {
  5485. plotLine.label = label = label.destroy();
  5486. }
  5487. }
  5488. } else if (path && path.length) {
  5489. plotLine.svgElem = svgElem = renderer.path(path)
  5490. .attr(attribs).add();
  5491. // events
  5492. if (events) {
  5493. addEvent = function (eventType) {
  5494. svgElem.on(eventType, function (e) {
  5495. events[eventType].apply(plotLine, [e]);
  5496. });
  5497. };
  5498. for (eventType in events) {
  5499. addEvent(eventType);
  5500. }
  5501. }
  5502. }
  5503. // the plot band/line label
  5504. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axis.width > 0 && axis.height > 0) {
  5505. // apply defaults
  5506. optionsLabel = merge({
  5507. align: horiz && isBand && 'center',
  5508. x: horiz ? !isBand && 4 : 10,
  5509. verticalAlign : !horiz && isBand && 'middle',
  5510. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5511. rotation: horiz && !isBand && 90
  5512. }, optionsLabel);
  5513. // add the SVG element
  5514. if (!label) {
  5515. attribs = {
  5516. align: optionsLabel.textAlign || optionsLabel.align,
  5517. rotation: optionsLabel.rotation
  5518. };
  5519. if (defined(zIndex)) {
  5520. attribs.zIndex = zIndex;
  5521. }
  5522. plotLine.label = label = renderer.text(
  5523. optionsLabel.text,
  5524. 0,
  5525. 0,
  5526. optionsLabel.useHTML
  5527. )
  5528. .attr(attribs)
  5529. .css(optionsLabel.style)
  5530. .add();
  5531. }
  5532. // get the bounding box and align the label
  5533. // #3000 changed to better handle choice between plotband or plotline
  5534. xs = [path[1], path[4], (isBand ? path[6] : path[1])];
  5535. ys = [path[2], path[5], (isBand ? path[7] : path[2])];
  5536. x = arrayMin(xs);
  5537. y = arrayMin(ys);
  5538. label.align(optionsLabel, false, {
  5539. x: x,
  5540. y: y,
  5541. width: arrayMax(xs) - x,
  5542. height: arrayMax(ys) - y
  5543. });
  5544. label.show();
  5545. } else if (label) { // move out of sight
  5546. label.hide();
  5547. }
  5548. // chainable
  5549. return plotLine;
  5550. },
  5551. /**
  5552. * Remove the plot line or band
  5553. */
  5554. destroy: function () {
  5555. // remove it from the lookup
  5556. erase(this.axis.plotLinesAndBands, this);
  5557. delete this.axis;
  5558. destroyObjectProperties(this);
  5559. }
  5560. };
  5561. /**
  5562. * Object with members for extending the Axis prototype
  5563. */
  5564. AxisPlotLineOrBandExtension = {
  5565. /**
  5566. * Create the path for a plot band
  5567. */
  5568. getPlotBandPath: function (from, to) {
  5569. var toPath = this.getPlotLinePath(to, null, null, true),
  5570. path = this.getPlotLinePath(from, null, null, true);
  5571. if (path && toPath && path.toString() !== toPath.toString()) { // #3836
  5572. path.push(
  5573. toPath[4],
  5574. toPath[5],
  5575. toPath[1],
  5576. toPath[2]
  5577. );
  5578. } else { // outside the axis area
  5579. path = null;
  5580. }
  5581. return path;
  5582. },
  5583. addPlotBand: function (options) {
  5584. return this.addPlotBandOrLine(options, 'plotBands');
  5585. },
  5586. addPlotLine: function (options) {
  5587. return this.addPlotBandOrLine(options, 'plotLines');
  5588. },
  5589. /**
  5590. * Add a plot band or plot line after render time
  5591. *
  5592. * @param options {Object} The plotBand or plotLine configuration object
  5593. */
  5594. addPlotBandOrLine: function (options, coll) {
  5595. var obj = new Highcharts.PlotLineOrBand(this, options).render(),
  5596. userOptions = this.userOptions;
  5597. if (obj) { // #2189
  5598. // Add it to the user options for exporting and Axis.update
  5599. if (coll) {
  5600. userOptions[coll] = userOptions[coll] || [];
  5601. userOptions[coll].push(options);
  5602. }
  5603. this.plotLinesAndBands.push(obj);
  5604. }
  5605. return obj;
  5606. },
  5607. /**
  5608. * Remove a plot band or plot line from the chart by id
  5609. * @param {Object} id
  5610. */
  5611. removePlotBandOrLine: function (id) {
  5612. var plotLinesAndBands = this.plotLinesAndBands,
  5613. options = this.options,
  5614. userOptions = this.userOptions,
  5615. i = plotLinesAndBands.length;
  5616. while (i--) {
  5617. if (plotLinesAndBands[i].id === id) {
  5618. plotLinesAndBands[i].destroy();
  5619. }
  5620. }
  5621. each([options.plotLines || [], userOptions.plotLines || [], options.plotBands || [], userOptions.plotBands || []], function (arr) {
  5622. i = arr.length;
  5623. while (i--) {
  5624. if (arr[i].id === id) {
  5625. erase(arr, arr[i]);
  5626. }
  5627. }
  5628. });
  5629. }
  5630. };
  5631. /**
  5632. * Create a new axis object
  5633. * @param {Object} chart
  5634. * @param {Object} options
  5635. */
  5636. var Axis = Highcharts.Axis = function () {
  5637. this.init.apply(this, arguments);
  5638. };
  5639. Axis.prototype = {
  5640. /**
  5641. * Default options for the X axis - the Y axis has extended defaults
  5642. */
  5643. defaultOptions: {
  5644. // allowDecimals: null,
  5645. // alternateGridColor: null,
  5646. // categories: [],
  5647. dateTimeLabelFormats: {
  5648. millisecond: '%H:%M:%S.%L',
  5649. second: '%H:%M:%S',
  5650. minute: '%H:%M',
  5651. hour: '%H:%M',
  5652. day: '%e. %b',
  5653. week: '%e. %b',
  5654. month: '%b \'%y',
  5655. year: '%Y'
  5656. },
  5657. endOnTick: false,
  5658. gridLineColor: '#D8D8D8',
  5659. // gridLineDashStyle: 'solid',
  5660. // gridLineWidth: 0,
  5661. // reversed: false,
  5662. labels: {
  5663. enabled: true,
  5664. // rotation: 0,
  5665. // align: 'center',
  5666. // step: null,
  5667. style: {
  5668. color: '#606060',
  5669. cursor: 'default',
  5670. fontSize: '11px'
  5671. },
  5672. x: 0,
  5673. y: 15
  5674. /*formatter: function () {
  5675. return this.value;
  5676. },*/
  5677. },
  5678. lineColor: '#C0D0E0',
  5679. lineWidth: 1,
  5680. //linkedTo: null,
  5681. //max: undefined,
  5682. //min: undefined,
  5683. minPadding: 0.01,
  5684. maxPadding: 0.01,
  5685. //minRange: null,
  5686. minorGridLineColor: '#E0E0E0',
  5687. // minorGridLineDashStyle: null,
  5688. minorGridLineWidth: 1,
  5689. minorTickColor: '#A0A0A0',
  5690. //minorTickInterval: null,
  5691. minorTickLength: 2,
  5692. minorTickPosition: 'outside', // inside or outside
  5693. //minorTickWidth: 0,
  5694. //opposite: false,
  5695. //offset: 0,
  5696. //plotBands: [{
  5697. // events: {},
  5698. // zIndex: 1,
  5699. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5700. //}],
  5701. //plotLines: [{
  5702. // events: {}
  5703. // dashStyle: {}
  5704. // zIndex:
  5705. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5706. //}],
  5707. //reversed: false,
  5708. // showFirstLabel: true,
  5709. // showLastLabel: true,
  5710. startOfWeek: 1,
  5711. startOnTick: false,
  5712. tickColor: '#C0D0E0',
  5713. //tickInterval: null,
  5714. tickLength: 10,
  5715. tickmarkPlacement: 'between', // on or between
  5716. tickPixelInterval: 100,
  5717. tickPosition: 'outside',
  5718. //tickWidth: 1,
  5719. title: {
  5720. //text: null,
  5721. align: 'middle', // low, middle or high
  5722. //margin: 0 for horizontal, 10 for vertical axes,
  5723. //rotation: 0,
  5724. //side: 'outside',
  5725. style: {
  5726. color: '#707070'
  5727. }
  5728. //x: 0,
  5729. //y: 0
  5730. },
  5731. type: 'linear' // linear, logarithmic or datetime
  5732. },
  5733. /**
  5734. * This options set extends the defaultOptions for Y axes
  5735. */
  5736. defaultYAxisOptions: {
  5737. endOnTick: true,
  5738. gridLineWidth: 1,
  5739. tickPixelInterval: 72,
  5740. showLastLabel: true,
  5741. labels: {
  5742. x: -8,
  5743. y: 3
  5744. },
  5745. lineWidth: 0,
  5746. maxPadding: 0.05,
  5747. minPadding: 0.05,
  5748. startOnTick: true,
  5749. //tickWidth: 0,
  5750. title: {
  5751. rotation: 270,
  5752. text: 'Values'
  5753. },
  5754. stackLabels: {
  5755. enabled: false,
  5756. //align: dynamic,
  5757. //y: dynamic,
  5758. //x: dynamic,
  5759. //verticalAlign: dynamic,
  5760. //textAlign: dynamic,
  5761. //rotation: 0,
  5762. formatter: function () {
  5763. return Highcharts.numberFormat(this.total, -1);
  5764. },
  5765. style: merge(defaultPlotOptions.line.dataLabels.style, { color: '#000000' })
  5766. }
  5767. },
  5768. /**
  5769. * These options extend the defaultOptions for left axes
  5770. */
  5771. defaultLeftAxisOptions: {
  5772. labels: {
  5773. x: -15,
  5774. y: null
  5775. },
  5776. title: {
  5777. rotation: 270
  5778. }
  5779. },
  5780. /**
  5781. * These options extend the defaultOptions for right axes
  5782. */
  5783. defaultRightAxisOptions: {
  5784. labels: {
  5785. x: 15,
  5786. y: null
  5787. },
  5788. title: {
  5789. rotation: 90
  5790. }
  5791. },
  5792. /**
  5793. * These options extend the defaultOptions for bottom axes
  5794. */
  5795. defaultBottomAxisOptions: {
  5796. labels: {
  5797. autoRotation: [-45],
  5798. x: 0,
  5799. y: null // based on font size
  5800. // overflow: undefined,
  5801. // staggerLines: null
  5802. },
  5803. title: {
  5804. rotation: 0
  5805. }
  5806. },
  5807. /**
  5808. * These options extend the defaultOptions for top axes
  5809. */
  5810. defaultTopAxisOptions: {
  5811. labels: {
  5812. autoRotation: [-45],
  5813. x: 0,
  5814. y: -15
  5815. // overflow: undefined
  5816. // staggerLines: null
  5817. },
  5818. title: {
  5819. rotation: 0
  5820. }
  5821. },
  5822. /**
  5823. * Initialize the axis
  5824. */
  5825. init: function (chart, userOptions) {
  5826. var isXAxis = userOptions.isX,
  5827. axis = this;
  5828. axis.chart = chart;
  5829. // Flag, is the axis horizontal
  5830. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  5831. // Flag, isXAxis
  5832. axis.isXAxis = isXAxis;
  5833. axis.coll = isXAxis ? 'xAxis' : 'yAxis';
  5834. axis.opposite = userOptions.opposite; // needed in setOptions
  5835. axis.side = userOptions.side || (axis.horiz ?
  5836. (axis.opposite ? 0 : 2) : // top : bottom
  5837. (axis.opposite ? 1 : 3)); // right : left
  5838. axis.setOptions(userOptions);
  5839. var options = this.options,
  5840. type = options.type,
  5841. isDatetimeAxis = type === 'datetime';
  5842. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  5843. // Flag, stagger lines or not
  5844. axis.userOptions = userOptions;
  5845. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  5846. axis.minPixelPadding = 0;
  5847. //axis.ignoreMinPadding = UNDEFINED; // can be set to true by a column or bar series
  5848. //axis.ignoreMaxPadding = UNDEFINED;
  5849. axis.reversed = options.reversed;
  5850. axis.zoomEnabled = options.zoomEnabled !== false;
  5851. // Initial categories
  5852. axis.categories = options.categories || type === 'category';
  5853. axis.names = axis.names || []; // Preserve on update (#3830)
  5854. // Elements
  5855. //axis.axisGroup = UNDEFINED;
  5856. //axis.gridGroup = UNDEFINED;
  5857. //axis.axisTitle = UNDEFINED;
  5858. //axis.axisLine = UNDEFINED;
  5859. // Shorthand types
  5860. axis.isLog = type === 'logarithmic';
  5861. axis.isDatetimeAxis = isDatetimeAxis;
  5862. // Flag, if axis is linked to another axis
  5863. axis.isLinked = defined(options.linkedTo);
  5864. // Linked axis.
  5865. //axis.linkedParent = UNDEFINED;
  5866. // Tick positions
  5867. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  5868. // Tick intervals
  5869. //axis.tickInterval = UNDEFINED;
  5870. //axis.minorTickInterval = UNDEFINED;
  5871. // Major ticks
  5872. axis.ticks = {};
  5873. axis.labelEdge = [];
  5874. // Minor ticks
  5875. axis.minorTicks = {};
  5876. // List of plotLines/Bands
  5877. axis.plotLinesAndBands = [];
  5878. // Alternate bands
  5879. axis.alternateBands = {};
  5880. // Axis metrics
  5881. //axis.left = UNDEFINED;
  5882. //axis.top = UNDEFINED;
  5883. //axis.width = UNDEFINED;
  5884. //axis.height = UNDEFINED;
  5885. //axis.bottom = UNDEFINED;
  5886. //axis.right = UNDEFINED;
  5887. //axis.transA = UNDEFINED;
  5888. //axis.transB = UNDEFINED;
  5889. //axis.oldTransA = UNDEFINED;
  5890. axis.len = 0;
  5891. //axis.oldMin = UNDEFINED;
  5892. //axis.oldMax = UNDEFINED;
  5893. //axis.oldUserMin = UNDEFINED;
  5894. //axis.oldUserMax = UNDEFINED;
  5895. //axis.oldAxisLength = UNDEFINED;
  5896. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  5897. axis.range = options.range;
  5898. axis.offset = options.offset || 0;
  5899. // Dictionary for stacks
  5900. axis.stacks = {};
  5901. axis.oldStacks = {};
  5902. axis.stacksTouched = 0;
  5903. // Min and max in the data
  5904. //axis.dataMin = UNDEFINED,
  5905. //axis.dataMax = UNDEFINED,
  5906. // The axis range
  5907. axis.max = null;
  5908. axis.min = null;
  5909. // User set min and max
  5910. //axis.userMin = UNDEFINED,
  5911. //axis.userMax = UNDEFINED,
  5912. // Crosshair options
  5913. axis.crosshair = pick(options.crosshair, splat(chart.options.tooltip.crosshairs)[isXAxis ? 0 : 1], false);
  5914. // Run Axis
  5915. var eventType,
  5916. events = axis.options.events;
  5917. // Register
  5918. if (inArray(axis, chart.axes) === -1) { // don't add it again on Axis.update()
  5919. if (isXAxis && !this.isColorAxis) { // #2713
  5920. chart.axes.splice(chart.xAxis.length, 0, axis);
  5921. } else {
  5922. chart.axes.push(axis);
  5923. }
  5924. chart[axis.coll].push(axis);
  5925. }
  5926. axis.series = axis.series || []; // populated by Series
  5927. // inverted charts have reversed xAxes as default
  5928. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  5929. axis.reversed = true;
  5930. }
  5931. axis.removePlotBand = axis.removePlotBandOrLine;
  5932. axis.removePlotLine = axis.removePlotBandOrLine;
  5933. // register event listeners
  5934. for (eventType in events) {
  5935. addEvent(axis, eventType, events[eventType]);
  5936. }
  5937. // extend logarithmic axis
  5938. if (axis.isLog) {
  5939. axis.val2lin = log2lin;
  5940. axis.lin2val = lin2log;
  5941. }
  5942. },
  5943. /**
  5944. * Merge and set options
  5945. */
  5946. setOptions: function (userOptions) {
  5947. this.options = merge(
  5948. this.defaultOptions,
  5949. this.isXAxis ? {} : this.defaultYAxisOptions,
  5950. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  5951. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  5952. merge(
  5953. defaultOptions[this.coll], // if set in setOptions (#1053)
  5954. userOptions
  5955. )
  5956. );
  5957. },
  5958. /**
  5959. * The default label formatter. The context is a special config object for the label.
  5960. */
  5961. defaultLabelFormatter: function () {
  5962. var axis = this.axis,
  5963. value = this.value,
  5964. categories = axis.categories,
  5965. dateTimeLabelFormat = this.dateTimeLabelFormat,
  5966. numericSymbols = defaultOptions.lang.numericSymbols,
  5967. i = numericSymbols && numericSymbols.length,
  5968. multi,
  5969. ret,
  5970. formatOption = axis.options.labels.format,
  5971. // make sure the same symbol is added for all labels on a linear axis
  5972. numericSymbolDetector = axis.isLog ? value : axis.tickInterval;
  5973. if (formatOption) {
  5974. ret = format(formatOption, this);
  5975. } else if (categories) {
  5976. ret = value;
  5977. } else if (dateTimeLabelFormat) { // datetime axis
  5978. ret = dateFormat(dateTimeLabelFormat, value);
  5979. } else if (i && numericSymbolDetector >= 1000) {
  5980. // Decide whether we should add a numeric symbol like k (thousands) or M (millions).
  5981. // If we are to enable this in tooltip or other places as well, we can move this
  5982. // logic to the numberFormatter and enable it by a parameter.
  5983. while (i-- && ret === UNDEFINED) {
  5984. multi = Math.pow(1000, i + 1);
  5985. if (numericSymbolDetector >= multi && (value * 10) % multi === 0 && numericSymbols[i] !== null) {
  5986. ret = Highcharts.numberFormat(value / multi, -1) + numericSymbols[i];
  5987. }
  5988. }
  5989. }
  5990. if (ret === UNDEFINED) {
  5991. if (mathAbs(value) >= 10000) { // add thousands separators
  5992. ret = Highcharts.numberFormat(value, -1);
  5993. } else { // small numbers
  5994. ret = Highcharts.numberFormat(value, -1, UNDEFINED, ''); // #2466
  5995. }
  5996. }
  5997. return ret;
  5998. },
  5999. /**
  6000. * Get the minimum and maximum for the series of each axis
  6001. */
  6002. getSeriesExtremes: function () {
  6003. var axis = this,
  6004. chart = axis.chart;
  6005. axis.hasVisibleSeries = false;
  6006. // Reset properties in case we're redrawing (#3353)
  6007. axis.dataMin = axis.dataMax = axis.ignoreMinPadding = axis.ignoreMaxPadding = null;
  6008. if (axis.buildStacks) {
  6009. axis.buildStacks();
  6010. }
  6011. // loop through this axis' series
  6012. each(axis.series, function (series) {
  6013. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  6014. var seriesOptions = series.options,
  6015. xData,
  6016. threshold = seriesOptions.threshold,
  6017. seriesDataMin,
  6018. seriesDataMax;
  6019. axis.hasVisibleSeries = true;
  6020. // Validate threshold in logarithmic axes
  6021. if (axis.isLog && threshold <= 0) {
  6022. threshold = null;
  6023. }
  6024. // Get dataMin and dataMax for X axes
  6025. if (axis.isXAxis) {
  6026. xData = series.xData;
  6027. if (xData.length) {
  6028. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), arrayMin(xData));
  6029. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  6030. }
  6031. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  6032. } else {
  6033. // Get this particular series extremes
  6034. series.getExtremes();
  6035. seriesDataMax = series.dataMax;
  6036. seriesDataMin = series.dataMin;
  6037. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  6038. // always 0 and 100. If seriesDataMin and seriesDataMax is null, then series
  6039. // doesn't have active y data, we continue with nulls
  6040. if (defined(seriesDataMin) && defined(seriesDataMax)) {
  6041. axis.dataMin = mathMin(pick(axis.dataMin, seriesDataMin), seriesDataMin);
  6042. axis.dataMax = mathMax(pick(axis.dataMax, seriesDataMax), seriesDataMax);
  6043. }
  6044. // Adjust to threshold
  6045. if (defined(threshold)) {
  6046. if (axis.dataMin >= threshold) {
  6047. axis.dataMin = threshold;
  6048. axis.ignoreMinPadding = true;
  6049. } else if (axis.dataMax < threshold) {
  6050. axis.dataMax = threshold;
  6051. axis.ignoreMaxPadding = true;
  6052. }
  6053. }
  6054. }
  6055. }
  6056. });
  6057. },
  6058. /**
  6059. * Translate from axis value to pixel position on the chart, or back
  6060. *
  6061. */
  6062. translate: function (val, backwards, cvsCoord, old, handleLog, pointPlacement) {
  6063. var axis = this.linkedParent || this, // #1417
  6064. sign = 1,
  6065. cvsOffset = 0,
  6066. localA = old ? axis.oldTransA : axis.transA,
  6067. localMin = old ? axis.oldMin : axis.min,
  6068. returnValue,
  6069. minPixelPadding = axis.minPixelPadding,
  6070. doPostTranslate = (axis.doPostTranslate || (axis.isLog && handleLog)) && axis.lin2val;
  6071. if (!localA) {
  6072. localA = axis.transA;
  6073. }
  6074. // In vertical axes, the canvas coordinates start from 0 at the top like in
  6075. // SVG.
  6076. if (cvsCoord) {
  6077. sign *= -1; // canvas coordinates inverts the value
  6078. cvsOffset = axis.len;
  6079. }
  6080. // Handle reversed axis
  6081. if (axis.reversed) {
  6082. sign *= -1;
  6083. cvsOffset -= sign * (axis.sector || axis.len);
  6084. }
  6085. // From pixels to value
  6086. if (backwards) { // reverse translation
  6087. val = val * sign + cvsOffset;
  6088. val -= minPixelPadding;
  6089. returnValue = val / localA + localMin; // from chart pixel to value
  6090. if (doPostTranslate) { // log and ordinal axes
  6091. returnValue = axis.lin2val(returnValue);
  6092. }
  6093. // From value to pixels
  6094. } else {
  6095. if (doPostTranslate) { // log and ordinal axes
  6096. val = axis.val2lin(val);
  6097. }
  6098. if (pointPlacement === 'between') {
  6099. pointPlacement = 0.5;
  6100. }
  6101. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding) +
  6102. (isNumber(pointPlacement) ? localA * pointPlacement * axis.pointRange : 0);
  6103. }
  6104. return returnValue;
  6105. },
  6106. /**
  6107. * Utility method to translate an axis value to pixel position.
  6108. * @param {Number} value A value in terms of axis units
  6109. * @param {Boolean} paneCoordinates Whether to return the pixel coordinate relative to the chart
  6110. * or just the axis/pane itself.
  6111. */
  6112. toPixels: function (value, paneCoordinates) {
  6113. return this.translate(value, false, !this.horiz, null, true) + (paneCoordinates ? 0 : this.pos);
  6114. },
  6115. /*
  6116. * Utility method to translate a pixel position in to an axis value
  6117. * @param {Number} pixel The pixel value coordinate
  6118. * @param {Boolean} paneCoordiantes Whether the input pixel is relative to the chart or just the
  6119. * axis/pane itself.
  6120. */
  6121. toValue: function (pixel, paneCoordinates) {
  6122. return this.translate(pixel - (paneCoordinates ? 0 : this.pos), true, !this.horiz, null, true);
  6123. },
  6124. /**
  6125. * Create the path for a plot line that goes from the given value on
  6126. * this axis, across the plot to the opposite side
  6127. * @param {Number} value
  6128. * @param {Number} lineWidth Used for calculation crisp line
  6129. * @param {Number] old Use old coordinates (for resizing and rescaling)
  6130. */
  6131. getPlotLinePath: function (value, lineWidth, old, force, translatedValue) {
  6132. var axis = this,
  6133. chart = axis.chart,
  6134. axisLeft = axis.left,
  6135. axisTop = axis.top,
  6136. x1,
  6137. y1,
  6138. x2,
  6139. y2,
  6140. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  6141. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  6142. skip,
  6143. transB = axis.transB,
  6144. /**
  6145. * Check if x is between a and b. If not, either move to a/b or skip,
  6146. * depending on the force parameter.
  6147. */
  6148. between = function (x, a, b) {
  6149. if (x < a || x > b) {
  6150. if (force) {
  6151. x = mathMin(mathMax(a, x), b);
  6152. } else {
  6153. skip = true;
  6154. }
  6155. }
  6156. return x;
  6157. };
  6158. translatedValue = pick(translatedValue, axis.translate(value, null, null, old));
  6159. x1 = x2 = mathRound(translatedValue + transB);
  6160. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  6161. if (isNaN(translatedValue)) { // no min or max
  6162. skip = true;
  6163. } else if (axis.horiz) {
  6164. y1 = axisTop;
  6165. y2 = cHeight - axis.bottom;
  6166. x1 = x2 = between(x1, axisLeft, axisLeft + axis.width);
  6167. } else {
  6168. x1 = axisLeft;
  6169. x2 = cWidth - axis.right;
  6170. y1 = y2 = between(y1, axisTop, axisTop + axis.height);
  6171. }
  6172. return skip && !force ?
  6173. null :
  6174. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 1);
  6175. },
  6176. /**
  6177. * Set the tick positions of a linear axis to round values like whole tens or every five.
  6178. */
  6179. getLinearTickPositions: function (tickInterval, min, max) {
  6180. var pos,
  6181. lastPos,
  6182. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  6183. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  6184. tickPositions = [];
  6185. // For single points, add a tick regardless of the relative position (#2662)
  6186. if (min === max && isNumber(min)) {
  6187. return [min];
  6188. }
  6189. // Populate the intermediate values
  6190. pos = roundedMin;
  6191. while (pos <= roundedMax) {
  6192. // Place the tick on the rounded value
  6193. tickPositions.push(pos);
  6194. // Always add the raw tickInterval, not the corrected one.
  6195. pos = correctFloat(pos + tickInterval);
  6196. // If the interval is not big enough in the current min - max range to actually increase
  6197. // the loop variable, we need to break out to prevent endless loop. Issue #619
  6198. if (pos === lastPos) {
  6199. break;
  6200. }
  6201. // Record the last value
  6202. lastPos = pos;
  6203. }
  6204. return tickPositions;
  6205. },
  6206. /**
  6207. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  6208. * as for major ticks.
  6209. */
  6210. getMinorTickPositions: function () {
  6211. var axis = this,
  6212. options = axis.options,
  6213. tickPositions = axis.tickPositions,
  6214. minorTickInterval = axis.minorTickInterval,
  6215. minorTickPositions = [],
  6216. pos,
  6217. i,
  6218. pointRangePadding = axis.pointRangePadding || 0,
  6219. min = axis.min - pointRangePadding, // #1498
  6220. max = axis.max + pointRangePadding, // #1498
  6221. range = max - min,
  6222. len;
  6223. // If minor ticks get too dense, they are hard to read, and may cause long running script. So we don't draw them.
  6224. if (range && range / minorTickInterval < axis.len / 3) { // #3875
  6225. if (axis.isLog) {
  6226. len = tickPositions.length;
  6227. for (i = 1; i < len; i++) {
  6228. minorTickPositions = minorTickPositions.concat(
  6229. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6230. );
  6231. }
  6232. } else if (axis.isDatetimeAxis && options.minorTickInterval === 'auto') { // #1314
  6233. minorTickPositions = minorTickPositions.concat(
  6234. axis.getTimeTicks(
  6235. axis.normalizeTimeTickInterval(minorTickInterval),
  6236. min,
  6237. max,
  6238. options.startOfWeek
  6239. )
  6240. );
  6241. } else {
  6242. for (pos = min + (tickPositions[0] - min) % minorTickInterval; pos <= max; pos += minorTickInterval) {
  6243. minorTickPositions.push(pos);
  6244. }
  6245. }
  6246. }
  6247. if(minorTickPositions.length !== 0) { // don't change the extremes, when there is no minor ticks
  6248. axis.trimTicks(minorTickPositions, options.startOnTick, options.endOnTick); // #3652 #3743 #1498
  6249. }
  6250. return minorTickPositions;
  6251. },
  6252. /**
  6253. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6254. * not yet processed, so we don't have information on data cropping and grouping, or
  6255. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6256. * we have finally established min and max.
  6257. */
  6258. adjustForMinRange: function () {
  6259. var axis = this,
  6260. options = axis.options,
  6261. min = axis.min,
  6262. max = axis.max,
  6263. zoomOffset,
  6264. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6265. closestDataRange,
  6266. i,
  6267. distance,
  6268. xData,
  6269. loopLength,
  6270. minArgs,
  6271. maxArgs;
  6272. // Set the automatic minimum range based on the closest point distance
  6273. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6274. if (defined(options.min) || defined(options.max)) {
  6275. axis.minRange = null; // don't do this again
  6276. } else {
  6277. // Find the closest distance between raw data points, as opposed to
  6278. // closestPointRange that applies to processed points (cropped and grouped)
  6279. each(axis.series, function (series) {
  6280. xData = series.xData;
  6281. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6282. for (i = loopLength; i > 0; i--) {
  6283. distance = xData[i] - xData[i - 1];
  6284. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6285. closestDataRange = distance;
  6286. }
  6287. }
  6288. });
  6289. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6290. }
  6291. }
  6292. // if minRange is exceeded, adjust
  6293. if (max - min < axis.minRange) {
  6294. var minRange = axis.minRange;
  6295. zoomOffset = (minRange - max + min) / 2;
  6296. // if min and max options have been set, don't go beyond it
  6297. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6298. if (spaceAvailable) { // if space is available, stay within the data range
  6299. minArgs[2] = axis.dataMin;
  6300. }
  6301. min = arrayMax(minArgs);
  6302. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6303. if (spaceAvailable) { // if space is availabe, stay within the data range
  6304. maxArgs[2] = axis.dataMax;
  6305. }
  6306. max = arrayMin(maxArgs);
  6307. // now if the max is adjusted, adjust the min back
  6308. if (max - min < minRange) {
  6309. minArgs[0] = max - minRange;
  6310. minArgs[1] = pick(options.min, max - minRange);
  6311. min = arrayMax(minArgs);
  6312. }
  6313. }
  6314. // Record modified extremes
  6315. axis.min = min;
  6316. axis.max = max;
  6317. },
  6318. /**
  6319. * Update translation information
  6320. */
  6321. setAxisTranslation: function (saveOld) {
  6322. var axis = this,
  6323. range = axis.max - axis.min,
  6324. pointRange = axis.axisPointRange || 0,
  6325. closestPointRange,
  6326. minPointOffset = 0,
  6327. pointRangePadding = 0,
  6328. linkedParent = axis.linkedParent,
  6329. ordinalCorrection,
  6330. hasCategories = !!axis.categories,
  6331. transA = axis.transA,
  6332. isXAxis = axis.isXAxis;
  6333. // Adjust translation for padding. Y axis with categories need to go through the same (#1784).
  6334. if (isXAxis || hasCategories || pointRange) {
  6335. if (linkedParent) {
  6336. minPointOffset = linkedParent.minPointOffset;
  6337. pointRangePadding = linkedParent.pointRangePadding;
  6338. } else {
  6339. each(axis.series, function (series) {
  6340. var seriesPointRange = hasCategories ? 1 : (isXAxis ? series.pointRange : (axis.axisPointRange || 0)), // #2806
  6341. pointPlacement = series.options.pointPlacement,
  6342. seriesClosestPointRange = series.closestPointRange;
  6343. if (seriesPointRange > range) { // #1446
  6344. seriesPointRange = 0;
  6345. }
  6346. pointRange = mathMax(pointRange, seriesPointRange);
  6347. if (!axis.single) {
  6348. // minPointOffset is the value padding to the left of the axis in order to make
  6349. // room for points with a pointRange, typically columns. When the pointPlacement option
  6350. // is 'between' or 'on', this padding does not apply.
  6351. minPointOffset = mathMax(
  6352. minPointOffset,
  6353. isString(pointPlacement) ? 0 : seriesPointRange / 2
  6354. );
  6355. // Determine the total padding needed to the length of the axis to make room for the
  6356. // pointRange. If the series' pointPlacement is 'on', no padding is added.
  6357. pointRangePadding = mathMax(
  6358. pointRangePadding,
  6359. pointPlacement === 'on' ? 0 : seriesPointRange
  6360. );
  6361. }
  6362. // Set the closestPointRange
  6363. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  6364. closestPointRange = defined(closestPointRange) ?
  6365. mathMin(closestPointRange, seriesClosestPointRange) :
  6366. seriesClosestPointRange;
  6367. }
  6368. });
  6369. }
  6370. // Record minPointOffset and pointRangePadding
  6371. ordinalCorrection = axis.ordinalSlope && closestPointRange ? axis.ordinalSlope / closestPointRange : 1; // #988, #1853
  6372. axis.minPointOffset = minPointOffset = minPointOffset * ordinalCorrection;
  6373. axis.pointRangePadding = pointRangePadding = pointRangePadding * ordinalCorrection;
  6374. // pointRange means the width reserved for each point, like in a column chart
  6375. axis.pointRange = mathMin(pointRange, range);
  6376. // closestPointRange means the closest distance between points. In columns
  6377. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6378. // is some other value
  6379. if (isXAxis) {
  6380. axis.closestPointRange = closestPointRange;
  6381. }
  6382. }
  6383. // Secondary values
  6384. if (saveOld) {
  6385. axis.oldTransA = transA;
  6386. }
  6387. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRangePadding) || 1);
  6388. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6389. axis.minPixelPadding = transA * minPointOffset;
  6390. },
  6391. minFromRange: function () {
  6392. return this.max - this.range;
  6393. },
  6394. /**
  6395. * Set the tick positions to round values and optionally extend the extremes
  6396. * to the nearest tick
  6397. */
  6398. setTickInterval: function (secondPass) {
  6399. var axis = this,
  6400. chart = axis.chart,
  6401. options = axis.options,
  6402. isLog = axis.isLog,
  6403. isDatetimeAxis = axis.isDatetimeAxis,
  6404. isXAxis = axis.isXAxis,
  6405. isLinked = axis.isLinked,
  6406. maxPadding = options.maxPadding,
  6407. minPadding = options.minPadding,
  6408. length,
  6409. linkedParentExtremes,
  6410. tickIntervalOption = options.tickInterval,
  6411. minTickInterval,
  6412. tickPixelIntervalOption = options.tickPixelInterval,
  6413. categories = axis.categories;
  6414. if (!isDatetimeAxis && !categories && !isLinked) {
  6415. this.getTickAmount();
  6416. }
  6417. // linked axis gets the extremes from the parent axis
  6418. if (isLinked) {
  6419. axis.linkedParent = chart[axis.coll][options.linkedTo];
  6420. linkedParentExtremes = axis.linkedParent.getExtremes();
  6421. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6422. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6423. if (options.type !== axis.linkedParent.options.type) {
  6424. error(11, 1); // Can't link axes of different type
  6425. }
  6426. } else { // initial min and max from the extreme data values
  6427. axis.min = pick(axis.userMin, options.min, axis.dataMin);
  6428. axis.max = pick(axis.userMax, options.max, axis.dataMax);
  6429. }
  6430. if (isLog) {
  6431. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6432. error(10, 1); // Can't plot negative values on log axis
  6433. }
  6434. // The correctFloat cures #934, float errors on full tens. But it
  6435. // was too aggressive for #4360 because of conversion back to lin,
  6436. // therefore use precision 15.
  6437. axis.min = correctFloat(log2lin(axis.min), 15);
  6438. axis.max = correctFloat(log2lin(axis.max), 15);
  6439. }
  6440. // handle zoomed range
  6441. if (axis.range && defined(axis.max)) {
  6442. axis.userMin = axis.min = mathMax(axis.min, axis.minFromRange()); // #618
  6443. axis.userMax = axis.max;
  6444. axis.range = null; // don't use it when running setExtremes
  6445. }
  6446. // Hook for adjusting this.min and this.max. Used by bubble series.
  6447. if (axis.beforePadding) {
  6448. axis.beforePadding();
  6449. }
  6450. // adjust min and max for the minimum range
  6451. axis.adjustForMinRange();
  6452. // Pad the values to get clear of the chart's edges. To avoid tickInterval taking the padding
  6453. // into account, we do this after computing tick interval (#1337).
  6454. if (!categories && !axis.axisPointRange && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6455. length = axis.max - axis.min;
  6456. if (length) {
  6457. if (!defined(options.min) && !defined(axis.userMin) && minPadding && (axis.dataMin < 0 || !axis.ignoreMinPadding)) {
  6458. axis.min -= length * minPadding;
  6459. }
  6460. if (!defined(options.max) && !defined(axis.userMax) && maxPadding && (axis.dataMax > 0 || !axis.ignoreMaxPadding)) {
  6461. axis.max += length * maxPadding;
  6462. }
  6463. }
  6464. }
  6465. // Stay within floor and ceiling
  6466. if (isNumber(options.floor)) {
  6467. axis.min = mathMax(axis.min, options.floor);
  6468. }
  6469. if (isNumber(options.ceiling)) {
  6470. axis.max = mathMin(axis.max, options.ceiling);
  6471. }
  6472. // get tickInterval
  6473. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6474. axis.tickInterval = 1;
  6475. } else if (isLinked && !tickIntervalOption &&
  6476. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6477. axis.tickInterval = tickIntervalOption = axis.linkedParent.tickInterval;
  6478. } else {
  6479. axis.tickInterval = pick(
  6480. tickIntervalOption,
  6481. this.tickAmount ? ((axis.max - axis.min) / mathMax(this.tickAmount - 1, 1)) : undefined,
  6482. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6483. 1 :
  6484. // don't let it be more than the data range
  6485. (axis.max - axis.min) * tickPixelIntervalOption / mathMax(axis.len, tickPixelIntervalOption)
  6486. );
  6487. }
  6488. // Now we're finished detecting min and max, crop and group series data. This
  6489. // is in turn needed in order to find tick positions in ordinal axes.
  6490. if (isXAxis && !secondPass) {
  6491. each(axis.series, function (series) {
  6492. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6493. });
  6494. }
  6495. // set the translation factor used in translate function
  6496. axis.setAxisTranslation(true);
  6497. // hook for ordinal axes and radial axes
  6498. if (axis.beforeSetTickPositions) {
  6499. axis.beforeSetTickPositions();
  6500. }
  6501. // hook for extensions, used in Highstock ordinal axes
  6502. if (axis.postProcessTickInterval) {
  6503. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6504. }
  6505. // In column-like charts, don't cramp in more ticks than there are points (#1943)
  6506. if (axis.pointRange) {
  6507. axis.tickInterval = mathMax(axis.pointRange, axis.tickInterval);
  6508. }
  6509. // Before normalizing the tick interval, handle minimum tick interval. This applies only if tickInterval is not defined.
  6510. minTickInterval = pick(options.minTickInterval, axis.isDatetimeAxis && axis.closestPointRange);
  6511. if (!tickIntervalOption && axis.tickInterval < minTickInterval) {
  6512. axis.tickInterval = minTickInterval;
  6513. }
  6514. // for linear axes, get magnitude and normalize the interval
  6515. if (!isDatetimeAxis && !isLog && !tickIntervalOption) {
  6516. axis.tickInterval = normalizeTickInterval(
  6517. axis.tickInterval,
  6518. null,
  6519. getMagnitude(axis.tickInterval),
  6520. // If the tick interval is between 0.5 and 5 and the axis max is in the order of
  6521. // thousands, chances are we are dealing with years. Don't allow decimals. #3363.
  6522. pick(options.allowDecimals, !(axis.tickInterval > 0.5 && axis.tickInterval < 5 && axis.max > 1000 && axis.max < 9999)),
  6523. !!this.tickAmount
  6524. );
  6525. }
  6526. // Prevent ticks from getting so close that we can't draw the labels
  6527. if (!this.tickAmount && this.len) { // Color axis with disabled legend has no length
  6528. axis.tickInterval = axis.unsquish();
  6529. }
  6530. this.setTickPositions();
  6531. },
  6532. /**
  6533. * Now we have computed the normalized tickInterval, get the tick positions
  6534. */
  6535. setTickPositions: function () {
  6536. var options = this.options,
  6537. tickPositions,
  6538. tickPositionsOption = options.tickPositions,
  6539. tickPositioner = options.tickPositioner,
  6540. startOnTick = options.startOnTick,
  6541. endOnTick = options.endOnTick,
  6542. single;
  6543. // Set the tickmarkOffset
  6544. this.tickmarkOffset = (this.categories && options.tickmarkPlacement === 'between' &&
  6545. this.tickInterval === 1) ? 0.5 : 0; // #3202
  6546. // get minorTickInterval
  6547. this.minorTickInterval = options.minorTickInterval === 'auto' && this.tickInterval ?
  6548. this.tickInterval / 5 : options.minorTickInterval;
  6549. // Find the tick positions
  6550. this.tickPositions = tickPositions = tickPositionsOption && tickPositionsOption.slice(); // Work on a copy (#1565)
  6551. if (!tickPositions) {
  6552. if (this.isDatetimeAxis) {
  6553. tickPositions = this.getTimeTicks(
  6554. this.normalizeTimeTickInterval(this.tickInterval, options.units),
  6555. this.min,
  6556. this.max,
  6557. options.startOfWeek,
  6558. this.ordinalPositions,
  6559. this.closestPointRange,
  6560. true
  6561. );
  6562. } else if (this.isLog) {
  6563. tickPositions = this.getLogTickPositions(this.tickInterval, this.min, this.max);
  6564. } else {
  6565. tickPositions = this.getLinearTickPositions(this.tickInterval, this.min, this.max);
  6566. }
  6567. // Too dense ticks, keep only the first and last (#4477)
  6568. if (tickPositions.length > this.len) {
  6569. tickPositions = [tickPositions[0], tickPositions.pop()];
  6570. }
  6571. this.tickPositions = tickPositions;
  6572. // Run the tick positioner callback, that allows modifying auto tick positions.
  6573. if (tickPositioner) {
  6574. tickPositioner = tickPositioner.apply(this, [this.min, this.max]);
  6575. if (tickPositioner) {
  6576. this.tickPositions = tickPositions = tickPositioner;
  6577. }
  6578. }
  6579. }
  6580. if (!this.isLinked) {
  6581. // reset min/max or remove extremes based on start/end on tick
  6582. this.trimTicks(tickPositions, startOnTick, endOnTick);
  6583. // When there is only one point, or all points have the same value on this axis, then min
  6584. // and max are equal and tickPositions.length is 0 or 1. In this case, add some padding
  6585. // in order to center the point, but leave it with one tick. #1337.
  6586. if (this.min === this.max && defined(this.min) && !this.tickAmount) {
  6587. // Substract half a unit (#2619, #2846, #2515, #3390)
  6588. single = true;
  6589. this.min -= 0.5;
  6590. this.max += 0.5;
  6591. }
  6592. this.single = single;
  6593. if (!tickPositionsOption && !tickPositioner) {
  6594. this.adjustTickAmount();
  6595. }
  6596. }
  6597. },
  6598. /**
  6599. * Handle startOnTick and endOnTick by either adapting to padding min/max or rounded min/max
  6600. */
  6601. trimTicks: function (tickPositions, startOnTick, endOnTick) {
  6602. var roundedMin = tickPositions[0],
  6603. roundedMax = tickPositions[tickPositions.length - 1],
  6604. minPointOffset = this.minPointOffset || 0;
  6605. if (startOnTick) {
  6606. this.min = roundedMin;
  6607. } else if (this.min - minPointOffset > roundedMin) {
  6608. tickPositions.shift();
  6609. }
  6610. if (endOnTick) {
  6611. this.max = roundedMax;
  6612. } else if (this.max + minPointOffset < roundedMax) {
  6613. tickPositions.pop();
  6614. }
  6615. // If no tick are left, set one tick in the middle (#3195)
  6616. if (tickPositions.length === 0 && defined(roundedMin)) {
  6617. tickPositions.push((roundedMax + roundedMin) / 2);
  6618. }
  6619. },
  6620. /**
  6621. * Set the max ticks of either the x and y axis collection
  6622. */
  6623. getTickAmount: function () {
  6624. var others = {}, // Whether there is another axis to pair with this one
  6625. hasOther,
  6626. options = this.options,
  6627. tickAmount = options.tickAmount,
  6628. tickPixelInterval = options.tickPixelInterval;
  6629. if (!defined(options.tickInterval) && this.len < tickPixelInterval && !this.isRadial &&
  6630. !this.isLog && options.startOnTick && options.endOnTick) {
  6631. tickAmount = 2;
  6632. }
  6633. if (!tickAmount && this.chart.options.chart.alignTicks !== false && options.alignTicks !== false) {
  6634. // Check if there are multiple axes in the same pane
  6635. each(this.chart[this.coll], function (axis) {
  6636. var options = axis.options,
  6637. horiz = axis.horiz,
  6638. key = [horiz ? options.left : options.top, horiz ? options.width : options.height, options.pane].join(',');
  6639. if (others[key]) {
  6640. if (axis.series.length) {
  6641. hasOther = true; // #4201
  6642. }
  6643. } else {
  6644. others[key] = 1;
  6645. }
  6646. });
  6647. if (hasOther) {
  6648. // Add 1 because 4 tick intervals require 5 ticks (including first and last)
  6649. tickAmount = mathCeil(this.len / tickPixelInterval) + 1;
  6650. }
  6651. }
  6652. // For tick amounts of 2 and 3, compute five ticks and remove the intermediate ones. This
  6653. // prevents the axis from adding ticks that are too far away from the data extremes.
  6654. if (tickAmount < 4) {
  6655. this.finalTickAmt = tickAmount;
  6656. tickAmount = 5;
  6657. }
  6658. this.tickAmount = tickAmount;
  6659. },
  6660. /**
  6661. * When using multiple axes, adjust the number of ticks to match the highest
  6662. * number of ticks in that group
  6663. */
  6664. adjustTickAmount: function () {
  6665. var tickInterval = this.tickInterval,
  6666. tickPositions = this.tickPositions,
  6667. tickAmount = this.tickAmount,
  6668. finalTickAmt = this.finalTickAmt,
  6669. currentTickAmount = tickPositions && tickPositions.length,
  6670. i,
  6671. len;
  6672. if (currentTickAmount < tickAmount) { // TODO: Check #3411
  6673. while (tickPositions.length < tickAmount) {
  6674. tickPositions.push(correctFloat(
  6675. tickPositions[tickPositions.length - 1] + tickInterval
  6676. ));
  6677. }
  6678. this.transA *= (currentTickAmount - 1) / (tickAmount - 1);
  6679. this.max = tickPositions[tickPositions.length - 1];
  6680. // We have too many ticks, run second pass to try to reduce ticks
  6681. } else if (currentTickAmount > tickAmount) {
  6682. this.tickInterval *= 2;
  6683. this.setTickPositions();
  6684. }
  6685. // The finalTickAmt property is set in getTickAmount
  6686. if (defined(finalTickAmt)) {
  6687. i = len = tickPositions.length;
  6688. while (i--) {
  6689. if (
  6690. (finalTickAmt === 3 && i % 2 === 1) || // Remove every other tick
  6691. (finalTickAmt <= 2 && i > 0 && i < len - 1) // Remove all but first and last
  6692. ) {
  6693. tickPositions.splice(i, 1);
  6694. }
  6695. }
  6696. this.finalTickAmt = UNDEFINED;
  6697. }
  6698. },
  6699. /**
  6700. * Set the scale based on data min and max, user set min and max or options
  6701. *
  6702. */
  6703. setScale: function () {
  6704. var axis = this,
  6705. isDirtyData,
  6706. isDirtyAxisLength;
  6707. axis.oldMin = axis.min;
  6708. axis.oldMax = axis.max;
  6709. axis.oldAxisLength = axis.len;
  6710. // set the new axisLength
  6711. axis.setAxisSize();
  6712. //axisLength = horiz ? axisWidth : axisHeight;
  6713. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  6714. // is there new data?
  6715. each(axis.series, function (series) {
  6716. if (series.isDirtyData || series.isDirty ||
  6717. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  6718. isDirtyData = true;
  6719. }
  6720. });
  6721. // do we really need to go through all this?
  6722. if (isDirtyAxisLength || isDirtyData || axis.isLinked || axis.forceRedraw ||
  6723. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax) {
  6724. if (axis.resetStacks) {
  6725. axis.resetStacks();
  6726. }
  6727. axis.forceRedraw = false;
  6728. // get data extremes if needed
  6729. axis.getSeriesExtremes();
  6730. // get fixed positions based on tickInterval
  6731. axis.setTickInterval();
  6732. // record old values to decide whether a rescale is necessary later on (#540)
  6733. axis.oldUserMin = axis.userMin;
  6734. axis.oldUserMax = axis.userMax;
  6735. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  6736. if (!axis.isDirty) {
  6737. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  6738. }
  6739. } else if (axis.cleanStacks) {
  6740. axis.cleanStacks();
  6741. }
  6742. },
  6743. /**
  6744. * Set the extremes and optionally redraw
  6745. * @param {Number} newMin
  6746. * @param {Number} newMax
  6747. * @param {Boolean} redraw
  6748. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  6749. * configuration
  6750. * @param {Object} eventArguments
  6751. *
  6752. */
  6753. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  6754. var axis = this,
  6755. chart = axis.chart;
  6756. redraw = pick(redraw, true); // defaults to true
  6757. each(axis.series, function (serie) {
  6758. delete serie.kdTree;
  6759. });
  6760. // Extend the arguments with min and max
  6761. eventArguments = extend(eventArguments, {
  6762. min: newMin,
  6763. max: newMax
  6764. });
  6765. // Fire the event
  6766. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  6767. axis.userMin = newMin;
  6768. axis.userMax = newMax;
  6769. axis.eventArgs = eventArguments;
  6770. if (redraw) {
  6771. chart.redraw(animation);
  6772. }
  6773. });
  6774. },
  6775. /**
  6776. * Overridable method for zooming chart. Pulled out in a separate method to allow overriding
  6777. * in stock charts.
  6778. */
  6779. zoom: function (newMin, newMax) {
  6780. var dataMin = this.dataMin,
  6781. dataMax = this.dataMax,
  6782. options = this.options,
  6783. min = mathMin(dataMin, pick(options.min, dataMin)),
  6784. max = mathMax(dataMax, pick(options.max, dataMax));
  6785. // Prevent pinch zooming out of range. Check for defined is for #1946. #1734.
  6786. if (!this.allowZoomOutside) {
  6787. if (defined(dataMin) && newMin <= min) {
  6788. newMin = min;
  6789. }
  6790. if (defined(dataMax) && newMax >= max) {
  6791. newMax = max;
  6792. }
  6793. }
  6794. // In full view, displaying the reset zoom button is not required
  6795. this.displayBtn = newMin !== UNDEFINED || newMax !== UNDEFINED;
  6796. // Do it
  6797. this.setExtremes(
  6798. newMin,
  6799. newMax,
  6800. false,
  6801. UNDEFINED,
  6802. { trigger: 'zoom' }
  6803. );
  6804. return true;
  6805. },
  6806. /**
  6807. * Update the axis metrics
  6808. */
  6809. setAxisSize: function () {
  6810. var chart = this.chart,
  6811. options = this.options,
  6812. offsetLeft = options.offsetLeft || 0,
  6813. offsetRight = options.offsetRight || 0,
  6814. horiz = this.horiz,
  6815. width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight),
  6816. height = pick(options.height, chart.plotHeight),
  6817. top = pick(options.top, chart.plotTop),
  6818. left = pick(options.left, chart.plotLeft + offsetLeft),
  6819. percentRegex = /%$/;
  6820. // Check for percentage based input values
  6821. if (percentRegex.test(height)) {
  6822. height = parseFloat(height) / 100 * chart.plotHeight;
  6823. }
  6824. if (percentRegex.test(top)) {
  6825. top = parseFloat(top) / 100 * chart.plotHeight + chart.plotTop;
  6826. }
  6827. // Expose basic values to use in Series object and navigator
  6828. this.left = left;
  6829. this.top = top;
  6830. this.width = width;
  6831. this.height = height;
  6832. this.bottom = chart.chartHeight - height - top;
  6833. this.right = chart.chartWidth - width - left;
  6834. // Direction agnostic properties
  6835. this.len = mathMax(horiz ? width : height, 0); // mathMax fixes #905
  6836. this.pos = horiz ? left : top; // distance from SVG origin
  6837. },
  6838. /**
  6839. * Get the actual axis extremes
  6840. */
  6841. getExtremes: function () {
  6842. var axis = this,
  6843. isLog = axis.isLog;
  6844. return {
  6845. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  6846. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  6847. dataMin: axis.dataMin,
  6848. dataMax: axis.dataMax,
  6849. userMin: axis.userMin,
  6850. userMax: axis.userMax
  6851. };
  6852. },
  6853. /**
  6854. * Get the zero plane either based on zero or on the min or max value.
  6855. * Used in bar and area plots
  6856. */
  6857. getThreshold: function (threshold) {
  6858. var axis = this,
  6859. isLog = axis.isLog,
  6860. realMin = isLog ? lin2log(axis.min) : axis.min,
  6861. realMax = isLog ? lin2log(axis.max) : axis.max;
  6862. // With a threshold of null, make the columns/areas rise from the top or bottom
  6863. // depending on the value, assuming an actual threshold of 0 (#4233).
  6864. if (threshold === null) {
  6865. threshold = realMax < 0 ? realMax : realMin;
  6866. } else if (realMin > threshold) {
  6867. threshold = realMin;
  6868. } else if (realMax < threshold) {
  6869. threshold = realMax;
  6870. }
  6871. return axis.translate(threshold, 0, 1, 0, 1);
  6872. },
  6873. /**
  6874. * Compute auto alignment for the axis label based on which side the axis is on
  6875. * and the given rotation for the label
  6876. */
  6877. autoLabelAlign: function (rotation) {
  6878. var ret,
  6879. angle = (pick(rotation, 0) - (this.side * 90) + 720) % 360;
  6880. if (angle > 15 && angle < 165) {
  6881. ret = 'right';
  6882. } else if (angle > 195 && angle < 345) {
  6883. ret = 'left';
  6884. } else {
  6885. ret = 'center';
  6886. }
  6887. return ret;
  6888. },
  6889. /**
  6890. * Prevent the ticks from getting so close we can't draw the labels. On a horizontal
  6891. * axis, this is handled by rotating the labels, removing ticks and adding ellipsis.
  6892. * On a vertical axis remove ticks and add ellipsis.
  6893. */
  6894. unsquish: function () {
  6895. var chart = this.chart,
  6896. ticks = this.ticks,
  6897. labelOptions = this.options.labels,
  6898. horiz = this.horiz,
  6899. tickInterval = this.tickInterval,
  6900. newTickInterval = tickInterval,
  6901. slotSize = this.len / (((this.categories ? 1 : 0) + this.max - this.min) / tickInterval),
  6902. rotation,
  6903. rotationOption = labelOptions.rotation,
  6904. labelMetrics = chart.renderer.fontMetrics(labelOptions.style.fontSize, ticks[0] && ticks[0].label),
  6905. step,
  6906. bestScore = Number.MAX_VALUE,
  6907. autoRotation,
  6908. // Return the multiple of tickInterval that is needed to avoid collision
  6909. getStep = function (spaceNeeded) {
  6910. var step = spaceNeeded / (slotSize || 1);
  6911. step = step > 1 ? mathCeil(step) : 1;
  6912. return step * tickInterval;
  6913. };
  6914. if (horiz) {
  6915. autoRotation = defined(rotationOption) ?
  6916. [rotationOption] :
  6917. slotSize < pick(labelOptions.autoRotationLimit, 80) && !labelOptions.staggerLines && !labelOptions.step && labelOptions.autoRotation;
  6918. if (autoRotation) {
  6919. // Loop over the given autoRotation options, and determine which gives the best score. The
  6920. // best score is that with the lowest number of steps and a rotation closest to horizontal.
  6921. each(autoRotation, function (rot) {
  6922. var score;
  6923. if (rot === rotationOption || (rot && rot >= -90 && rot <= 90)) { // #3891
  6924. step = getStep(mathAbs(labelMetrics.h / mathSin(deg2rad * rot)));
  6925. score = step + mathAbs(rot / 360);
  6926. if (score < bestScore) {
  6927. bestScore = score;
  6928. rotation = rot;
  6929. newTickInterval = step;
  6930. }
  6931. }
  6932. });
  6933. }
  6934. } else if (!labelOptions.step) { // #4411
  6935. newTickInterval = getStep(labelMetrics.h);
  6936. }
  6937. this.autoRotation = autoRotation;
  6938. this.labelRotation = rotation;
  6939. return newTickInterval;
  6940. },
  6941. renderUnsquish: function () {
  6942. var chart = this.chart,
  6943. renderer = chart.renderer,
  6944. tickPositions = this.tickPositions,
  6945. ticks = this.ticks,
  6946. labelOptions = this.options.labels,
  6947. horiz = this.horiz,
  6948. margin = chart.margin,
  6949. slotCount = this.categories ? tickPositions.length : tickPositions.length - 1,
  6950. slotWidth = this.slotWidth = (horiz && !labelOptions.step && !labelOptions.rotation &&
  6951. ((this.staggerLines || 1) * chart.plotWidth) / slotCount) ||
  6952. (!horiz && ((margin[3] && (margin[3] - chart.spacing[3])) || chart.chartWidth * 0.33)), // #1580, #1931,
  6953. innerWidth = mathMax(1, mathRound(slotWidth - 2 * (labelOptions.padding || 5))),
  6954. attr = {},
  6955. labelMetrics = renderer.fontMetrics(labelOptions.style.fontSize, ticks[0] && ticks[0].label),
  6956. textOverflowOption = labelOptions.style.textOverflow,
  6957. css,
  6958. labelLength = 0,
  6959. label,
  6960. i,
  6961. pos;
  6962. // Set rotation option unless it is "auto", like in gauges
  6963. if (!isString(labelOptions.rotation)) {
  6964. attr.rotation = labelOptions.rotation || 0; // #4443
  6965. }
  6966. // Handle auto rotation on horizontal axis
  6967. if (this.autoRotation) {
  6968. // Get the longest label length
  6969. each(tickPositions, function (tick) {
  6970. tick = ticks[tick];
  6971. if (tick && tick.labelLength > labelLength) {
  6972. labelLength = tick.labelLength;
  6973. }
  6974. });
  6975. // Apply rotation only if the label is too wide for the slot, and
  6976. // the label is wider than its height.
  6977. if (labelLength > innerWidth && labelLength > labelMetrics.h) {
  6978. attr.rotation = this.labelRotation;
  6979. } else {
  6980. this.labelRotation = 0;
  6981. }
  6982. // Handle word-wrap or ellipsis on vertical axis
  6983. } else if (slotWidth) {
  6984. // For word-wrap or ellipsis
  6985. css = { width: innerWidth + PX };
  6986. if (!textOverflowOption) {
  6987. css.textOverflow = 'clip';
  6988. // On vertical axis, only allow word wrap if there is room for more lines.
  6989. i = tickPositions.length;
  6990. while (!horiz && i--) {
  6991. pos = tickPositions[i];
  6992. label = ticks[pos].label;
  6993. if (label) {
  6994. // Reset ellipsis in order to get the correct bounding box (#4070)
  6995. if (label.styles.textOverflow === 'ellipsis') {
  6996. label.css({ textOverflow: 'clip' });
  6997. }
  6998. if (label.getBBox().height > this.len / tickPositions.length - (labelMetrics.h - labelMetrics.f)) {
  6999. label.specCss = { textOverflow: 'ellipsis' };
  7000. }
  7001. }
  7002. }
  7003. }
  7004. }
  7005. // Add ellipsis if the label length is significantly longer than ideal
  7006. if (attr.rotation) {
  7007. css = {
  7008. width: (labelLength > chart.chartHeight * 0.5 ? chart.chartHeight * 0.33 : chart.chartHeight) + PX
  7009. };
  7010. if (!textOverflowOption) {
  7011. css.textOverflow = 'ellipsis';
  7012. }
  7013. }
  7014. // Set the explicit or automatic label alignment
  7015. this.labelAlign = attr.align = labelOptions.align || this.autoLabelAlign(this.labelRotation);
  7016. // Apply general and specific CSS
  7017. each(tickPositions, function (pos) {
  7018. var tick = ticks[pos],
  7019. label = tick && tick.label;
  7020. if (label) {
  7021. if (css) {
  7022. label.css(merge(css, label.specCss));
  7023. }
  7024. delete label.specCss;
  7025. label.attr(attr);
  7026. tick.rotation = attr.rotation;
  7027. }
  7028. });
  7029. // TODO: Why not part of getLabelPosition?
  7030. this.tickRotCorr = renderer.rotCorr(labelMetrics.b, this.labelRotation || 0, this.side === 2);
  7031. },
  7032. /**
  7033. * Return true if the axis has associated data
  7034. */
  7035. hasData: function () {
  7036. return this.hasVisibleSeries || (defined(this.min) && defined(this.max) && !!this.tickPositions);
  7037. },
  7038. /**
  7039. * Render the tick labels to a preliminary position to get their sizes
  7040. */
  7041. getOffset: function () {
  7042. var axis = this,
  7043. chart = axis.chart,
  7044. renderer = chart.renderer,
  7045. options = axis.options,
  7046. tickPositions = axis.tickPositions,
  7047. ticks = axis.ticks,
  7048. horiz = axis.horiz,
  7049. side = axis.side,
  7050. invertedSide = chart.inverted ? [1, 0, 3, 2][side] : side,
  7051. hasData,
  7052. showAxis,
  7053. titleOffset = 0,
  7054. titleOffsetOption,
  7055. titleMargin = 0,
  7056. axisTitleOptions = options.title,
  7057. labelOptions = options.labels,
  7058. labelOffset = 0, // reset
  7059. labelOffsetPadded,
  7060. axisOffset = chart.axisOffset,
  7061. clipOffset = chart.clipOffset,
  7062. clip,
  7063. directionFactor = [-1, 1, 1, -1][side],
  7064. n,
  7065. lineHeightCorrection;
  7066. // For reuse in Axis.render
  7067. hasData = axis.hasData();
  7068. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  7069. // Set/reset staggerLines
  7070. axis.staggerLines = axis.horiz && labelOptions.staggerLines;
  7071. // Create the axisGroup and gridGroup elements on first iteration
  7072. if (!axis.axisGroup) {
  7073. axis.gridGroup = renderer.g('grid')
  7074. .attr({ zIndex: options.gridZIndex || 1 })
  7075. .add();
  7076. axis.axisGroup = renderer.g('axis')
  7077. .attr({ zIndex: options.zIndex || 2 })
  7078. .add();
  7079. axis.labelGroup = renderer.g('axis-labels')
  7080. .attr({ zIndex: labelOptions.zIndex || 7 })
  7081. .addClass(PREFIX + axis.coll.toLowerCase() + '-labels')
  7082. .add();
  7083. }
  7084. if (hasData || axis.isLinked) {
  7085. // Generate ticks
  7086. each(tickPositions, function (pos) {
  7087. if (!ticks[pos]) {
  7088. ticks[pos] = new Tick(axis, pos);
  7089. } else {
  7090. ticks[pos].addLabel(); // update labels depending on tick interval
  7091. }
  7092. });
  7093. axis.renderUnsquish();
  7094. each(tickPositions, function (pos) {
  7095. // left side must be align: right and right side must have align: left for labels
  7096. if (side === 0 || side === 2 || { 1: 'left', 3: 'right' }[side] === axis.labelAlign) {
  7097. // get the highest offset
  7098. labelOffset = mathMax(
  7099. ticks[pos].getLabelSize(),
  7100. labelOffset
  7101. );
  7102. }
  7103. });
  7104. if (axis.staggerLines) {
  7105. labelOffset *= axis.staggerLines;
  7106. axis.labelOffset = labelOffset;
  7107. }
  7108. } else { // doesn't have data
  7109. for (n in ticks) {
  7110. ticks[n].destroy();
  7111. delete ticks[n];
  7112. }
  7113. }
  7114. if (axisTitleOptions && axisTitleOptions.text && axisTitleOptions.enabled !== false) {
  7115. if (!axis.axisTitle) {
  7116. axis.axisTitle = renderer.text(
  7117. axisTitleOptions.text,
  7118. 0,
  7119. 0,
  7120. axisTitleOptions.useHTML
  7121. )
  7122. .attr({
  7123. zIndex: 7,
  7124. rotation: axisTitleOptions.rotation || 0,
  7125. align:
  7126. axisTitleOptions.textAlign ||
  7127. { low: 'left', middle: 'center', high: 'right' }[axisTitleOptions.align]
  7128. })
  7129. .addClass(PREFIX + this.coll.toLowerCase() + '-title')
  7130. .css(axisTitleOptions.style)
  7131. .add(axis.axisGroup);
  7132. axis.axisTitle.isNew = true;
  7133. }
  7134. if (showAxis) {
  7135. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  7136. titleOffsetOption = axisTitleOptions.offset;
  7137. titleMargin = defined(titleOffsetOption) ? 0 : pick(axisTitleOptions.margin, horiz ? 5 : 10);
  7138. }
  7139. // hide or show the title depending on whether showEmpty is set
  7140. axis.axisTitle[showAxis ? 'show' : 'hide']();
  7141. }
  7142. // handle automatic or user set offset
  7143. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  7144. axis.tickRotCorr = axis.tickRotCorr || { x: 0, y: 0 }; // polar
  7145. lineHeightCorrection = side === 2 ? axis.tickRotCorr.y : 0;
  7146. labelOffsetPadded = labelOffset + titleMargin +
  7147. (labelOffset && (directionFactor * (horiz ? pick(labelOptions.y, axis.tickRotCorr.y + 8) : labelOptions.x) - lineHeightCorrection));
  7148. axis.axisTitleMargin = pick(titleOffsetOption, labelOffsetPadded);
  7149. axisOffset[side] = mathMax(
  7150. axisOffset[side],
  7151. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset,
  7152. labelOffsetPadded // #3027
  7153. );
  7154. // Decide the clipping needed to keep the graph inside the plot area and axis lines
  7155. clip = options.offset ? 0 : mathFloor(options.lineWidth / 2) * 2; // #4308, #4371
  7156. clipOffset[invertedSide] = mathMax(clipOffset[invertedSide], clip);
  7157. },
  7158. /**
  7159. * Get the path for the axis line
  7160. */
  7161. getLinePath: function (lineWidth) {
  7162. var chart = this.chart,
  7163. opposite = this.opposite,
  7164. offset = this.offset,
  7165. horiz = this.horiz,
  7166. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  7167. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  7168. if (opposite) {
  7169. lineWidth *= -1; // crispify the other way - #1480, #1687
  7170. }
  7171. return chart.renderer.crispLine([
  7172. M,
  7173. horiz ?
  7174. this.left :
  7175. lineLeft,
  7176. horiz ?
  7177. lineTop :
  7178. this.top,
  7179. L,
  7180. horiz ?
  7181. chart.chartWidth - this.right :
  7182. lineLeft,
  7183. horiz ?
  7184. lineTop :
  7185. chart.chartHeight - this.bottom
  7186. ], lineWidth);
  7187. },
  7188. /**
  7189. * Position the title
  7190. */
  7191. getTitlePosition: function () {
  7192. // compute anchor points for each of the title align options
  7193. var horiz = this.horiz,
  7194. axisLeft = this.left,
  7195. axisTop = this.top,
  7196. axisLength = this.len,
  7197. axisTitleOptions = this.options.title,
  7198. margin = horiz ? axisLeft : axisTop,
  7199. opposite = this.opposite,
  7200. offset = this.offset,
  7201. xOption = axisTitleOptions.x || 0,
  7202. yOption = axisTitleOptions.y || 0,
  7203. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  7204. // the position in the length direction of the axis
  7205. alongAxis = {
  7206. low: margin + (horiz ? 0 : axisLength),
  7207. middle: margin + axisLength / 2,
  7208. high: margin + (horiz ? axisLength : 0)
  7209. }[axisTitleOptions.align],
  7210. // the position in the perpendicular direction of the axis
  7211. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  7212. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  7213. (opposite ? -1 : 1) * // so does opposite axes
  7214. this.axisTitleMargin +
  7215. (this.side === 2 ? fontSize : 0);
  7216. return {
  7217. x: horiz ?
  7218. alongAxis + xOption :
  7219. offAxis + (opposite ? this.width : 0) + offset + xOption,
  7220. y: horiz ?
  7221. offAxis + yOption - (opposite ? this.height : 0) + offset :
  7222. alongAxis + yOption
  7223. };
  7224. },
  7225. /**
  7226. * Render the axis
  7227. */
  7228. render: function () {
  7229. var axis = this,
  7230. chart = axis.chart,
  7231. renderer = chart.renderer,
  7232. options = axis.options,
  7233. isLog = axis.isLog,
  7234. isLinked = axis.isLinked,
  7235. tickPositions = axis.tickPositions,
  7236. axisTitle = axis.axisTitle,
  7237. ticks = axis.ticks,
  7238. minorTicks = axis.minorTicks,
  7239. alternateBands = axis.alternateBands,
  7240. stackLabelOptions = options.stackLabels,
  7241. alternateGridColor = options.alternateGridColor,
  7242. tickmarkOffset = axis.tickmarkOffset,
  7243. lineWidth = options.lineWidth,
  7244. linePath,
  7245. hasRendered = chart.hasRendered,
  7246. slideInTicks = hasRendered && defined(axis.oldMin) && !isNaN(axis.oldMin),
  7247. showAxis = axis.showAxis,
  7248. globalAnimation = renderer.globalAnimation,
  7249. from,
  7250. to;
  7251. // Reset
  7252. axis.labelEdge.length = 0;
  7253. //axis.justifyToPlot = overflow === 'justify';
  7254. axis.overlap = false;
  7255. // Mark all elements inActive before we go over and mark the active ones
  7256. each([ticks, minorTicks, alternateBands], function (coll) {
  7257. var pos;
  7258. for (pos in coll) {
  7259. coll[pos].isActive = false;
  7260. }
  7261. });
  7262. // If the series has data draw the ticks. Else only the line and title
  7263. if (axis.hasData() || isLinked) {
  7264. // minor ticks
  7265. if (axis.minorTickInterval && !axis.categories) {
  7266. each(axis.getMinorTickPositions(), function (pos) {
  7267. if (!minorTicks[pos]) {
  7268. minorTicks[pos] = new Tick(axis, pos, 'minor');
  7269. }
  7270. // render new ticks in old position
  7271. if (slideInTicks && minorTicks[pos].isNew) {
  7272. minorTicks[pos].render(null, true);
  7273. }
  7274. minorTicks[pos].render(null, false, 1);
  7275. });
  7276. }
  7277. // Major ticks. Pull out the first item and render it last so that
  7278. // we can get the position of the neighbour label. #808.
  7279. if (tickPositions.length) { // #1300
  7280. each(tickPositions, function (pos, i) {
  7281. // linked axes need an extra check to find out if
  7282. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  7283. if (!ticks[pos]) {
  7284. ticks[pos] = new Tick(axis, pos);
  7285. }
  7286. // render new ticks in old position
  7287. if (slideInTicks && ticks[pos].isNew) {
  7288. ticks[pos].render(i, true, 0.1);
  7289. }
  7290. ticks[pos].render(i);
  7291. }
  7292. });
  7293. // In a categorized axis, the tick marks are displayed between labels. So
  7294. // we need to add a tick mark and grid line at the left edge of the X axis.
  7295. if (tickmarkOffset && (axis.min === 0 || axis.single)) {
  7296. if (!ticks[-1]) {
  7297. ticks[-1] = new Tick(axis, -1, null, true);
  7298. }
  7299. ticks[-1].render(-1);
  7300. }
  7301. }
  7302. // alternate grid color
  7303. if (alternateGridColor) {
  7304. each(tickPositions, function (pos, i) {
  7305. if (i % 2 === 0 && pos < axis.max) {
  7306. if (!alternateBands[pos]) {
  7307. alternateBands[pos] = new Highcharts.PlotLineOrBand(axis);
  7308. }
  7309. from = pos + tickmarkOffset; // #949
  7310. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] + tickmarkOffset : axis.max;
  7311. alternateBands[pos].options = {
  7312. from: isLog ? lin2log(from) : from,
  7313. to: isLog ? lin2log(to) : to,
  7314. color: alternateGridColor
  7315. };
  7316. alternateBands[pos].render();
  7317. alternateBands[pos].isActive = true;
  7318. }
  7319. });
  7320. }
  7321. // custom plot lines and bands
  7322. if (!axis._addedPlotLB) { // only first time
  7323. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  7324. axis.addPlotBandOrLine(plotLineOptions);
  7325. });
  7326. axis._addedPlotLB = true;
  7327. }
  7328. } // end if hasData
  7329. // Remove inactive ticks
  7330. each([ticks, minorTicks, alternateBands], function (coll) {
  7331. var pos,
  7332. i,
  7333. forDestruction = [],
  7334. delay = globalAnimation ? globalAnimation.duration || 500 : 0,
  7335. destroyInactiveItems = function () {
  7336. i = forDestruction.length;
  7337. while (i--) {
  7338. // When resizing rapidly, the same items may be destroyed in different timeouts,
  7339. // or the may be reactivated
  7340. if (coll[forDestruction[i]] && !coll[forDestruction[i]].isActive) {
  7341. coll[forDestruction[i]].destroy();
  7342. delete coll[forDestruction[i]];
  7343. }
  7344. }
  7345. };
  7346. for (pos in coll) {
  7347. if (!coll[pos].isActive) {
  7348. // Render to zero opacity
  7349. coll[pos].render(pos, false, 0);
  7350. coll[pos].isActive = false;
  7351. forDestruction.push(pos);
  7352. }
  7353. }
  7354. // When the objects are finished fading out, destroy them
  7355. if (coll === alternateBands || !chart.hasRendered || !delay) {
  7356. destroyInactiveItems();
  7357. } else if (delay) {
  7358. setTimeout(destroyInactiveItems, delay);
  7359. }
  7360. });
  7361. // Static items. As the axis group is cleared on subsequent calls
  7362. // to render, these items are added outside the group.
  7363. // axis line
  7364. if (lineWidth) {
  7365. linePath = axis.getLinePath(lineWidth);
  7366. if (!axis.axisLine) {
  7367. axis.axisLine = renderer.path(linePath)
  7368. .attr({
  7369. stroke: options.lineColor,
  7370. 'stroke-width': lineWidth,
  7371. zIndex: 7
  7372. })
  7373. .add(axis.axisGroup);
  7374. } else {
  7375. axis.axisLine.animate({ d: linePath });
  7376. }
  7377. // show or hide the line depending on options.showEmpty
  7378. axis.axisLine[showAxis ? 'show' : 'hide']();
  7379. }
  7380. if (axisTitle && showAxis) {
  7381. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  7382. axis.getTitlePosition()
  7383. );
  7384. axisTitle.isNew = false;
  7385. }
  7386. // Stacked totals:
  7387. if (stackLabelOptions && stackLabelOptions.enabled) {
  7388. axis.renderStackTotals();
  7389. }
  7390. // End stacked totals
  7391. axis.isDirty = false;
  7392. },
  7393. /**
  7394. * Redraw the axis to reflect changes in the data or axis extremes
  7395. */
  7396. redraw: function () {
  7397. // render the axis
  7398. this.render();
  7399. // move plot lines and bands
  7400. each(this.plotLinesAndBands, function (plotLine) {
  7401. plotLine.render();
  7402. });
  7403. // mark associated series as dirty and ready for redraw
  7404. each(this.series, function (series) {
  7405. series.isDirty = true;
  7406. });
  7407. },
  7408. /**
  7409. * Destroys an Axis instance.
  7410. */
  7411. destroy: function (keepEvents) {
  7412. var axis = this,
  7413. stacks = axis.stacks,
  7414. stackKey,
  7415. plotLinesAndBands = axis.plotLinesAndBands,
  7416. i;
  7417. // Remove the events
  7418. if (!keepEvents) {
  7419. removeEvent(axis);
  7420. }
  7421. // Destroy each stack total
  7422. for (stackKey in stacks) {
  7423. destroyObjectProperties(stacks[stackKey]);
  7424. stacks[stackKey] = null;
  7425. }
  7426. // Destroy collections
  7427. each([axis.ticks, axis.minorTicks, axis.alternateBands], function (coll) {
  7428. destroyObjectProperties(coll);
  7429. });
  7430. i = plotLinesAndBands.length;
  7431. while (i--) { // #1975
  7432. plotLinesAndBands[i].destroy();
  7433. }
  7434. // Destroy local variables
  7435. each(['stackTotalGroup', 'axisLine', 'axisTitle', 'axisGroup', 'cross', 'gridGroup', 'labelGroup'], function (prop) {
  7436. if (axis[prop]) {
  7437. axis[prop] = axis[prop].destroy();
  7438. }
  7439. });
  7440. // Destroy crosshair
  7441. if (this.cross) {
  7442. this.cross.destroy();
  7443. }
  7444. },
  7445. /**
  7446. * Draw the crosshair
  7447. */
  7448. drawCrosshair: function (e, point) { // docs: Missing docs for Axis.crosshair. Also for properties.
  7449. var path,
  7450. options = this.crosshair,
  7451. animation = options.animation,
  7452. pos,
  7453. attribs,
  7454. categorized;
  7455. if (
  7456. // Disabled in options
  7457. !this.crosshair ||
  7458. // Snap
  7459. ((defined(point) || !pick(this.crosshair.snap, true)) === false) ||
  7460. // Not on this axis (#4095, #2888)
  7461. (point && point.series && point.series[this.coll] !== this)
  7462. ) {
  7463. this.hideCrosshair();
  7464. } else {
  7465. // Get the path
  7466. if (!pick(options.snap, true)) {
  7467. pos = (this.horiz ? e.chartX - this.pos : this.len - e.chartY + this.pos);
  7468. } else if (defined(point)) {
  7469. /*jslint eqeq: true*/
  7470. pos = this.isXAxis ? point.plotX : this.len - point.plotY; // #3834
  7471. /*jslint eqeq: false*/
  7472. }
  7473. if (this.isRadial) {
  7474. path = this.getPlotLinePath(this.isXAxis ? point.x : pick(point.stackY, point.y)) || null; // #3189
  7475. } else {
  7476. path = this.getPlotLinePath(null, null, null, null, pos) || null; // #3189
  7477. }
  7478. if (path === null) {
  7479. this.hideCrosshair();
  7480. return;
  7481. }
  7482. // Draw the cross
  7483. if (this.cross) {
  7484. this.cross
  7485. .attr({ visibility: VISIBLE })[animation ? 'animate' : 'attr']({ d: path }, animation);
  7486. } else {
  7487. categorized = this.categories && !this.isRadial;
  7488. attribs = {
  7489. 'stroke-width': options.width || (categorized ? this.transA : 1),
  7490. stroke: options.color || (categorized ? 'rgba(155,200,255,0.2)' : '#C0C0C0'),
  7491. zIndex: options.zIndex || 2
  7492. };
  7493. if (options.dashStyle) {
  7494. attribs.dashstyle = options.dashStyle;
  7495. }
  7496. this.cross = this.chart.renderer.path(path).attr(attribs).add();
  7497. }
  7498. }
  7499. },
  7500. /**
  7501. * Hide the crosshair.
  7502. */
  7503. hideCrosshair: function () {
  7504. if (this.cross) {
  7505. this.cross.hide();
  7506. }
  7507. }
  7508. }; // end Axis
  7509. extend(Axis.prototype, AxisPlotLineOrBandExtension);
  7510. /**
  7511. * Set the tick positions to a time unit that makes sense, for example
  7512. * on the first of each month or on every Monday. Return an array
  7513. * with the time positions. Used in datetime axes as well as for grouping
  7514. * data on a datetime axis.
  7515. *
  7516. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  7517. * @param {Number} min The minimum in axis values
  7518. * @param {Number} max The maximum in axis values
  7519. * @param {Number} startOfWeek
  7520. */
  7521. Axis.prototype.getTimeTicks = function (normalizedInterval, min, max, startOfWeek) {
  7522. var tickPositions = [],
  7523. i,
  7524. higherRanks = {},
  7525. useUTC = defaultOptions.global.useUTC,
  7526. minYear, // used in months and years as a basis for Date.UTC()
  7527. minDate = new Date(min - getTZOffset(min)),
  7528. interval = normalizedInterval.unitRange,
  7529. count = normalizedInterval.count;
  7530. if (defined(min)) { // #1300
  7531. minDate[setMilliseconds](interval >= timeUnits.second ? 0 : // #3935
  7532. count * mathFloor(minDate.getMilliseconds() / count)); // #3652, #3654
  7533. if (interval >= timeUnits.second) { // second
  7534. minDate[setSeconds](interval >= timeUnits.minute ? 0 : // #3935
  7535. count * mathFloor(minDate.getSeconds() / count));
  7536. }
  7537. if (interval >= timeUnits.minute) { // minute
  7538. minDate[setMinutes](interval >= timeUnits.hour ? 0 :
  7539. count * mathFloor(minDate[getMinutes]() / count));
  7540. }
  7541. if (interval >= timeUnits.hour) { // hour
  7542. minDate[setHours](interval >= timeUnits.day ? 0 :
  7543. count * mathFloor(minDate[getHours]() / count));
  7544. }
  7545. if (interval >= timeUnits.day) { // day
  7546. minDate[setDate](interval >= timeUnits.month ? 1 :
  7547. count * mathFloor(minDate[getDate]() / count));
  7548. }
  7549. if (interval >= timeUnits.month) { // month
  7550. minDate[setMonth](interval >= timeUnits.year ? 0 :
  7551. count * mathFloor(minDate[getMonth]() / count));
  7552. minYear = minDate[getFullYear]();
  7553. }
  7554. if (interval >= timeUnits.year) { // year
  7555. minYear -= minYear % count;
  7556. minDate[setFullYear](minYear);
  7557. }
  7558. // week is a special case that runs outside the hierarchy
  7559. if (interval === timeUnits.week) {
  7560. // get start of current week, independent of count
  7561. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  7562. pick(startOfWeek, 1));
  7563. }
  7564. // get tick positions
  7565. i = 1;
  7566. if (timezoneOffset || getTimezoneOffset) {
  7567. minDate = minDate.getTime();
  7568. minDate = new Date(minDate + getTZOffset(minDate));
  7569. }
  7570. minYear = minDate[getFullYear]();
  7571. var time = minDate.getTime(),
  7572. minMonth = minDate[getMonth](),
  7573. minDateDate = minDate[getDate](),
  7574. localTimezoneOffset = (timeUnits.day +
  7575. (useUTC ? getTZOffset(minDate) : minDate.getTimezoneOffset() * 60 * 1000)
  7576. ) % timeUnits.day; // #950, #3359
  7577. // iterate and add tick positions at appropriate values
  7578. while (time < max) {
  7579. tickPositions.push(time);
  7580. // if the interval is years, use Date.UTC to increase years
  7581. if (interval === timeUnits.year) {
  7582. time = makeTime(minYear + i * count, 0);
  7583. // if the interval is months, use Date.UTC to increase months
  7584. } else if (interval === timeUnits.month) {
  7585. time = makeTime(minYear, minMonth + i * count);
  7586. // if we're using global time, the interval is not fixed as it jumps
  7587. // one hour at the DST crossover
  7588. } else if (!useUTC && (interval === timeUnits.day || interval === timeUnits.week)) {
  7589. time = makeTime(minYear, minMonth, minDateDate +
  7590. i * count * (interval === timeUnits.day ? 1 : 7));
  7591. // else, the interval is fixed and we use simple addition
  7592. } else {
  7593. time += interval * count;
  7594. }
  7595. i++;
  7596. }
  7597. // push the last time
  7598. tickPositions.push(time);
  7599. // mark new days if the time is dividible by day (#1649, #1760)
  7600. each(grep(tickPositions, function (time) {
  7601. return interval <= timeUnits.hour && time % timeUnits.day === localTimezoneOffset;
  7602. }), function (time) {
  7603. higherRanks[time] = 'day';
  7604. });
  7605. }
  7606. // record information on the chosen unit - for dynamic label formatter
  7607. tickPositions.info = extend(normalizedInterval, {
  7608. higherRanks: higherRanks,
  7609. totalRange: interval * count
  7610. });
  7611. return tickPositions;
  7612. };
  7613. /**
  7614. * Get a normalized tick interval for dates. Returns a configuration object with
  7615. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  7616. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  7617. * of segments in stock charts, the normalizing logic was extracted in order to
  7618. * prevent it for running over again for each segment having the same interval.
  7619. * #662, #697.
  7620. */
  7621. Axis.prototype.normalizeTimeTickInterval = function (tickInterval, unitsOption) {
  7622. var units = unitsOption || [[
  7623. 'millisecond', // unit name
  7624. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  7625. ], [
  7626. 'second',
  7627. [1, 2, 5, 10, 15, 30]
  7628. ], [
  7629. 'minute',
  7630. [1, 2, 5, 10, 15, 30]
  7631. ], [
  7632. 'hour',
  7633. [1, 2, 3, 4, 6, 8, 12]
  7634. ], [
  7635. 'day',
  7636. [1, 2]
  7637. ], [
  7638. 'week',
  7639. [1, 2]
  7640. ], [
  7641. 'month',
  7642. [1, 2, 3, 4, 6]
  7643. ], [
  7644. 'year',
  7645. null
  7646. ]],
  7647. unit = units[units.length - 1], // default unit is years
  7648. interval = timeUnits[unit[0]],
  7649. multiples = unit[1],
  7650. count,
  7651. i;
  7652. // loop through the units to find the one that best fits the tickInterval
  7653. for (i = 0; i < units.length; i++) {
  7654. unit = units[i];
  7655. interval = timeUnits[unit[0]];
  7656. multiples = unit[1];
  7657. if (units[i + 1]) {
  7658. // lessThan is in the middle between the highest multiple and the next unit.
  7659. var lessThan = (interval * multiples[multiples.length - 1] +
  7660. timeUnits[units[i + 1][0]]) / 2;
  7661. // break and keep the current unit
  7662. if (tickInterval <= lessThan) {
  7663. break;
  7664. }
  7665. }
  7666. }
  7667. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  7668. if (interval === timeUnits.year && tickInterval < 5 * interval) {
  7669. multiples = [1, 2, 5];
  7670. }
  7671. // get the count
  7672. count = normalizeTickInterval(
  7673. tickInterval / interval,
  7674. multiples,
  7675. unit[0] === 'year' ? mathMax(getMagnitude(tickInterval / interval), 1) : 1 // #1913, #2360
  7676. );
  7677. return {
  7678. unitRange: interval,
  7679. count: count,
  7680. unitName: unit[0]
  7681. };
  7682. };/**
  7683. * Methods defined on the Axis prototype
  7684. */
  7685. /**
  7686. * Set the tick positions of a logarithmic axis
  7687. */
  7688. Axis.prototype.getLogTickPositions = function (interval, min, max, minor) {
  7689. var axis = this,
  7690. options = axis.options,
  7691. axisLength = axis.len,
  7692. // Since we use this method for both major and minor ticks,
  7693. // use a local variable and return the result
  7694. positions = [];
  7695. // Reset
  7696. if (!minor) {
  7697. axis._minorAutoInterval = null;
  7698. }
  7699. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  7700. if (interval >= 0.5) {
  7701. interval = mathRound(interval);
  7702. positions = axis.getLinearTickPositions(interval, min, max);
  7703. // Second case: We need intermediary ticks. For example
  7704. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  7705. } else if (interval >= 0.08) {
  7706. var roundedMin = mathFloor(min),
  7707. intermediate,
  7708. i,
  7709. j,
  7710. len,
  7711. pos,
  7712. lastPos,
  7713. break2;
  7714. if (interval > 0.3) {
  7715. intermediate = [1, 2, 4];
  7716. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  7717. intermediate = [1, 2, 4, 6, 8];
  7718. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  7719. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  7720. }
  7721. for (i = roundedMin; i < max + 1 && !break2; i++) {
  7722. len = intermediate.length;
  7723. for (j = 0; j < len && !break2; j++) {
  7724. pos = log2lin(lin2log(i) * intermediate[j]);
  7725. if (pos > min && (!minor || lastPos <= max) && lastPos !== UNDEFINED) { // #1670, lastPos is #3113
  7726. positions.push(lastPos);
  7727. }
  7728. if (lastPos > max) {
  7729. break2 = true;
  7730. }
  7731. lastPos = pos;
  7732. }
  7733. }
  7734. // Third case: We are so deep in between whole logarithmic values that
  7735. // we might as well handle the tick positions like a linear axis. For
  7736. // example 1.01, 1.02, 1.03, 1.04.
  7737. } else {
  7738. var realMin = lin2log(min),
  7739. realMax = lin2log(max),
  7740. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  7741. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  7742. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  7743. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  7744. interval = pick(
  7745. filteredTickIntervalOption,
  7746. axis._minorAutoInterval,
  7747. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  7748. );
  7749. interval = normalizeTickInterval(
  7750. interval,
  7751. null,
  7752. getMagnitude(interval)
  7753. );
  7754. positions = map(axis.getLinearTickPositions(
  7755. interval,
  7756. realMin,
  7757. realMax
  7758. ), log2lin);
  7759. if (!minor) {
  7760. axis._minorAutoInterval = interval / 5;
  7761. }
  7762. }
  7763. // Set the axis-level tickInterval variable
  7764. if (!minor) {
  7765. axis.tickInterval = interval;
  7766. }
  7767. return positions;
  7768. };/**
  7769. * The tooltip object
  7770. * @param {Object} chart The chart instance
  7771. * @param {Object} options Tooltip options
  7772. */
  7773. var Tooltip = Highcharts.Tooltip = function () {
  7774. this.init.apply(this, arguments);
  7775. };
  7776. Tooltip.prototype = {
  7777. init: function (chart, options) {
  7778. var borderWidth = options.borderWidth,
  7779. style = options.style,
  7780. padding = pInt(style.padding);
  7781. // Save the chart and options
  7782. this.chart = chart;
  7783. this.options = options;
  7784. // Keep track of the current series
  7785. //this.currentSeries = UNDEFINED;
  7786. // List of crosshairs
  7787. this.crosshairs = [];
  7788. // Current values of x and y when animating
  7789. this.now = { x: 0, y: 0 };
  7790. // The tooltip is initially hidden
  7791. this.isHidden = true;
  7792. // create the label
  7793. this.label = chart.renderer.label('', 0, 0, options.shape || 'callout', null, null, options.useHTML, null, 'tooltip')
  7794. .attr({
  7795. padding: padding,
  7796. fill: options.backgroundColor,
  7797. 'stroke-width': borderWidth,
  7798. r: options.borderRadius,
  7799. zIndex: 8
  7800. })
  7801. .css(style)
  7802. .css({ padding: 0 }) // Remove it from VML, the padding is applied as an attribute instead (#1117)
  7803. .add()
  7804. .attr({ y: -9999 }); // #2301, #2657
  7805. // When using canVG the shadow shows up as a gray circle
  7806. // even if the tooltip is hidden.
  7807. if (!useCanVG) {
  7808. this.label.shadow(options.shadow);
  7809. }
  7810. // Public property for getting the shared state.
  7811. this.shared = options.shared;
  7812. },
  7813. /**
  7814. * Destroy the tooltip and its elements.
  7815. */
  7816. destroy: function () {
  7817. // Destroy and clear local variables
  7818. if (this.label) {
  7819. this.label = this.label.destroy();
  7820. }
  7821. clearTimeout(this.hideTimer);
  7822. clearTimeout(this.tooltipTimeout);
  7823. },
  7824. /**
  7825. * Provide a soft movement for the tooltip
  7826. *
  7827. * @param {Number} x
  7828. * @param {Number} y
  7829. * @private
  7830. */
  7831. move: function (x, y, anchorX, anchorY) {
  7832. var tooltip = this,
  7833. now = tooltip.now,
  7834. animate = tooltip.options.animation !== false && !tooltip.isHidden &&
  7835. // When we get close to the target position, abort animation and land on the right place (#3056)
  7836. (mathAbs(x - now.x) > 1 || mathAbs(y - now.y) > 1),
  7837. skipAnchor = tooltip.followPointer || tooltip.len > 1;
  7838. // Get intermediate values for animation
  7839. extend(now, {
  7840. x: animate ? (2 * now.x + x) / 3 : x,
  7841. y: animate ? (now.y + y) / 2 : y,
  7842. anchorX: skipAnchor ? UNDEFINED : animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
  7843. anchorY: skipAnchor ? UNDEFINED : animate ? (now.anchorY + anchorY) / 2 : anchorY
  7844. });
  7845. // Move to the intermediate value
  7846. tooltip.label.attr(now);
  7847. // Run on next tick of the mouse tracker
  7848. if (animate) {
  7849. // Never allow two timeouts
  7850. clearTimeout(this.tooltipTimeout);
  7851. // Set the fixed interval ticking for the smooth tooltip
  7852. this.tooltipTimeout = setTimeout(function () {
  7853. // The interval function may still be running during destroy, so check that the chart is really there before calling.
  7854. if (tooltip) {
  7855. tooltip.move(x, y, anchorX, anchorY);
  7856. }
  7857. }, 32);
  7858. }
  7859. },
  7860. /**
  7861. * Hide the tooltip
  7862. */
  7863. hide: function (delay) {
  7864. var tooltip = this,
  7865. hoverPoints;
  7866. clearTimeout(this.hideTimer); // disallow duplicate timers (#1728, #1766)
  7867. if (!this.isHidden) {
  7868. hoverPoints = this.chart.hoverPoints;
  7869. this.hideTimer = setTimeout(function () {
  7870. tooltip.label.fadeOut();
  7871. tooltip.isHidden = true;
  7872. }, pick(delay, this.options.hideDelay, 500));
  7873. }
  7874. },
  7875. /**
  7876. * Extendable method to get the anchor position of the tooltip
  7877. * from a point or set of points
  7878. */
  7879. getAnchor: function (points, mouseEvent) {
  7880. var ret,
  7881. chart = this.chart,
  7882. inverted = chart.inverted,
  7883. plotTop = chart.plotTop,
  7884. plotLeft = chart.plotLeft,
  7885. plotX = 0,
  7886. plotY = 0,
  7887. yAxis,
  7888. xAxis;
  7889. points = splat(points);
  7890. // Pie uses a special tooltipPos
  7891. ret = points[0].tooltipPos;
  7892. // When tooltip follows mouse, relate the position to the mouse
  7893. if (this.followPointer && mouseEvent) {
  7894. if (mouseEvent.chartX === UNDEFINED) {
  7895. mouseEvent = chart.pointer.normalize(mouseEvent);
  7896. }
  7897. ret = [
  7898. mouseEvent.chartX - chart.plotLeft,
  7899. mouseEvent.chartY - plotTop
  7900. ];
  7901. }
  7902. // When shared, use the average position
  7903. if (!ret) {
  7904. each(points, function (point) {
  7905. yAxis = point.series.yAxis;
  7906. xAxis = point.series.xAxis;
  7907. plotX += point.plotX + (!inverted && xAxis ? xAxis.left - plotLeft : 0);
  7908. plotY += (point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY) +
  7909. (!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
  7910. });
  7911. plotX /= points.length;
  7912. plotY /= points.length;
  7913. ret = [
  7914. inverted ? chart.plotWidth - plotY : plotX,
  7915. this.shared && !inverted && points.length > 1 && mouseEvent ?
  7916. mouseEvent.chartY - plotTop : // place shared tooltip next to the mouse (#424)
  7917. inverted ? chart.plotHeight - plotX : plotY
  7918. ];
  7919. }
  7920. return map(ret, mathRound);
  7921. },
  7922. /**
  7923. * Place the tooltip in a chart without spilling over
  7924. * and not covering the point it self.
  7925. */
  7926. getPosition: function (boxWidth, boxHeight, point) {
  7927. var chart = this.chart,
  7928. distance = this.distance,
  7929. ret = {},
  7930. h = point.h || 0, // #4117
  7931. swapped,
  7932. first = ['y', chart.chartHeight, boxHeight, point.plotY + chart.plotTop, chart.plotTop, chart.plotTop + chart.plotHeight],
  7933. second = ['x', chart.chartWidth, boxWidth, point.plotX + chart.plotLeft, chart.plotLeft, chart.plotLeft + chart.plotWidth],
  7934. // The far side is right or bottom
  7935. preferFarSide = pick(point.ttBelow, (chart.inverted && !point.negative) || (!chart.inverted && point.negative)),
  7936. /**
  7937. * Handle the preferred dimension. When the preferred dimension is tooltip
  7938. * on top or bottom of the point, it will look for space there.
  7939. */
  7940. firstDimension = function (dim, outerSize, innerSize, point, min, max) {
  7941. var roomLeft = innerSize < point - distance,
  7942. roomRight = point + distance + innerSize < outerSize,
  7943. alignedLeft = point - distance - innerSize,
  7944. alignedRight = point + distance;
  7945. if (preferFarSide && roomRight) {
  7946. ret[dim] = alignedRight;
  7947. } else if (!preferFarSide && roomLeft) {
  7948. ret[dim] = alignedLeft;
  7949. } else if (roomLeft) {
  7950. ret[dim] = mathMin(max - innerSize, alignedLeft - h < 0 ? alignedLeft : alignedLeft - h);
  7951. } else if (roomRight) {
  7952. ret[dim] = mathMax(min, alignedRight + h + innerSize > outerSize ? alignedRight : alignedRight + h);
  7953. } else {
  7954. return false;
  7955. }
  7956. },
  7957. /**
  7958. * Handle the secondary dimension. If the preferred dimension is tooltip
  7959. * on top or bottom of the point, the second dimension is to align the tooltip
  7960. * above the point, trying to align center but allowing left or right
  7961. * align within the chart box.
  7962. */
  7963. secondDimension = function (dim, outerSize, innerSize, point) {
  7964. // Too close to the edge, return false and swap dimensions
  7965. if (point < distance || point > outerSize - distance) {
  7966. return false;
  7967. // Align left/top
  7968. } else if (point < innerSize / 2) {
  7969. ret[dim] = 1;
  7970. // Align right/bottom
  7971. } else if (point > outerSize - innerSize / 2) {
  7972. ret[dim] = outerSize - innerSize - 2;
  7973. // Align center
  7974. } else {
  7975. ret[dim] = point - innerSize / 2;
  7976. }
  7977. },
  7978. /**
  7979. * Swap the dimensions
  7980. */
  7981. swap = function (count) {
  7982. var temp = first;
  7983. first = second;
  7984. second = temp;
  7985. swapped = count;
  7986. },
  7987. run = function () {
  7988. if (firstDimension.apply(0, first) !== false) {
  7989. if (secondDimension.apply(0, second) === false && !swapped) {
  7990. swap(true);
  7991. run();
  7992. }
  7993. } else if (!swapped) {
  7994. swap(true);
  7995. run();
  7996. } else {
  7997. ret.x = ret.y = 0;
  7998. }
  7999. };
  8000. // Under these conditions, prefer the tooltip on the side of the point
  8001. if (chart.inverted || this.len > 1) {
  8002. swap();
  8003. }
  8004. run();
  8005. return ret;
  8006. },
  8007. /**
  8008. * In case no user defined formatter is given, this will be used. Note that the context
  8009. * here is an object holding point, series, x, y etc.
  8010. */
  8011. defaultFormatter: function (tooltip) {
  8012. var items = this.points || splat(this),
  8013. s;
  8014. // build the header
  8015. s = [tooltip.tooltipFooterHeaderFormatter(items[0])]; //#3397: abstraction to enable formatting of footer and header
  8016. // build the values
  8017. s = s.concat(tooltip.bodyFormatter(items));
  8018. // footer
  8019. s.push(tooltip.tooltipFooterHeaderFormatter(items[0], true)); //#3397: abstraction to enable formatting of footer and header
  8020. return s.join('');
  8021. },
  8022. /**
  8023. * Refresh the tooltip's text and position.
  8024. * @param {Object} point
  8025. */
  8026. refresh: function (point, mouseEvent) {
  8027. var tooltip = this,
  8028. chart = tooltip.chart,
  8029. label = tooltip.label,
  8030. options = tooltip.options,
  8031. x,
  8032. y,
  8033. anchor,
  8034. textConfig = {},
  8035. text,
  8036. pointConfig = [],
  8037. formatter = options.formatter || tooltip.defaultFormatter,
  8038. hoverPoints = chart.hoverPoints,
  8039. borderColor,
  8040. shared = tooltip.shared,
  8041. currentSeries;
  8042. clearTimeout(this.hideTimer);
  8043. // get the reference point coordinates (pie charts use tooltipPos)
  8044. tooltip.followPointer = splat(point)[0].series.tooltipOptions.followPointer;
  8045. anchor = tooltip.getAnchor(point, mouseEvent);
  8046. x = anchor[0];
  8047. y = anchor[1];
  8048. // shared tooltip, array is sent over
  8049. if (shared && !(point.series && point.series.noSharedTooltip)) {
  8050. // hide previous hoverPoints and set new
  8051. chart.hoverPoints = point;
  8052. if (hoverPoints) {
  8053. each(hoverPoints, function (point) {
  8054. point.setState();
  8055. });
  8056. }
  8057. each(point, function (item) {
  8058. item.setState(HOVER_STATE);
  8059. pointConfig.push(item.getLabelConfig());
  8060. });
  8061. textConfig = {
  8062. x: point[0].category,
  8063. y: point[0].y
  8064. };
  8065. textConfig.points = pointConfig;
  8066. this.len = pointConfig.length;
  8067. point = point[0];
  8068. // single point tooltip
  8069. } else {
  8070. textConfig = point.getLabelConfig();
  8071. }
  8072. text = formatter.call(textConfig, tooltip);
  8073. // register the current series
  8074. currentSeries = point.series;
  8075. this.distance = pick(currentSeries.tooltipOptions.distance, 16);
  8076. // update the inner HTML
  8077. if (text === false) {
  8078. this.hide();
  8079. } else {
  8080. // show it
  8081. if (tooltip.isHidden) {
  8082. stop(label);
  8083. label.attr('opacity', 1).show();
  8084. }
  8085. // update text
  8086. label.attr({
  8087. text: text
  8088. });
  8089. // set the stroke color of the box
  8090. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  8091. label.attr({
  8092. stroke: borderColor
  8093. });
  8094. tooltip.updatePosition({
  8095. plotX: x,
  8096. plotY: y,
  8097. negative: point.negative,
  8098. ttBelow: point.ttBelow,
  8099. h: anchor[2] || 0
  8100. });
  8101. this.isHidden = false;
  8102. }
  8103. fireEvent(chart, 'tooltipRefresh', {
  8104. text: text,
  8105. x: x + chart.plotLeft,
  8106. y: y + chart.plotTop,
  8107. borderColor: borderColor
  8108. });
  8109. },
  8110. /**
  8111. * Find the new position and perform the move
  8112. */
  8113. updatePosition: function (point) {
  8114. var chart = this.chart,
  8115. label = this.label,
  8116. pos = (this.options.positioner || this.getPosition).call(
  8117. this,
  8118. label.width,
  8119. label.height,
  8120. point
  8121. );
  8122. // do the move
  8123. this.move(
  8124. mathRound(pos.x),
  8125. mathRound(pos.y || 0), // can be undefined (#3977)
  8126. point.plotX + chart.plotLeft,
  8127. point.plotY + chart.plotTop
  8128. );
  8129. },
  8130. /**
  8131. * Get the best X date format based on the closest point range on the axis.
  8132. */
  8133. getXDateFormat: function (point, options, xAxis) {
  8134. var xDateFormat,
  8135. dateTimeLabelFormats = options.dateTimeLabelFormats,
  8136. closestPointRange = xAxis && xAxis.closestPointRange,
  8137. n,
  8138. blank = '01-01 00:00:00.000',
  8139. strpos = {
  8140. millisecond: 15,
  8141. second: 12,
  8142. minute: 9,
  8143. hour: 6,
  8144. day: 3
  8145. },
  8146. date,
  8147. lastN = 'millisecond'; // for sub-millisecond data, #4223
  8148. if (closestPointRange) {
  8149. date = dateFormat('%m-%d %H:%M:%S.%L', point.x);
  8150. for (n in timeUnits) {
  8151. // If the range is exactly one week and we're looking at a Sunday/Monday, go for the week format
  8152. if (closestPointRange === timeUnits.week && +dateFormat('%w', point.x) === xAxis.options.startOfWeek &&
  8153. date.substr(6) === blank.substr(6)) {
  8154. n = 'week';
  8155. break;
  8156. // The first format that is too great for the range
  8157. } else if (timeUnits[n] > closestPointRange) {
  8158. n = lastN;
  8159. break;
  8160. // If the point is placed every day at 23:59, we need to show
  8161. // the minutes as well. #2637.
  8162. } else if (strpos[n] && date.substr(strpos[n]) !== blank.substr(strpos[n])) {
  8163. break;
  8164. }
  8165. // Weeks are outside the hierarchy, only apply them on Mondays/Sundays like in the first condition
  8166. if (n !== 'week') {
  8167. lastN = n;
  8168. }
  8169. }
  8170. if (n) {
  8171. xDateFormat = dateTimeLabelFormats[n];
  8172. }
  8173. } else {
  8174. xDateFormat = dateTimeLabelFormats.day;
  8175. }
  8176. return xDateFormat || dateTimeLabelFormats.year; // #2546, 2581
  8177. },
  8178. /**
  8179. * Format the footer/header of the tooltip
  8180. * #3397: abstraction to enable formatting of footer and header
  8181. */
  8182. tooltipFooterHeaderFormatter: function (point, isFooter) {
  8183. var footOrHead = isFooter ? 'footer' : 'header',
  8184. series = point.series,
  8185. tooltipOptions = series.tooltipOptions,
  8186. xDateFormat = tooltipOptions.xDateFormat,
  8187. xAxis = series.xAxis,
  8188. isDateTime = xAxis && xAxis.options.type === 'datetime' && isNumber(point.key),
  8189. formatString = tooltipOptions[footOrHead+'Format'];
  8190. // Guess the best date format based on the closest point distance (#568, #3418)
  8191. if (isDateTime && !xDateFormat) {
  8192. xDateFormat = this.getXDateFormat(point, tooltipOptions, xAxis);
  8193. }
  8194. // Insert the footer date format if any
  8195. if (isDateTime && xDateFormat) {
  8196. formatString = formatString.replace('{point.key}', '{point.key:' + xDateFormat + '}');
  8197. }
  8198. return format(formatString, {
  8199. point: point,
  8200. series: series
  8201. });
  8202. },
  8203. /**
  8204. * Build the body (lines) of the tooltip by iterating over the items and returning one entry for each item,
  8205. * abstracting this functionality allows to easily overwrite and extend it.
  8206. */
  8207. bodyFormatter: function (items) {
  8208. return map(items, function (item) {
  8209. var tooltipOptions = item.series.tooltipOptions;
  8210. return (tooltipOptions.pointFormatter || item.point.tooltipFormatter).call(item.point, tooltipOptions.pointFormat);
  8211. });
  8212. }
  8213. };
  8214. var hoverChartIndex;
  8215. // Global flag for touch support
  8216. hasTouch = doc.documentElement.ontouchstart !== UNDEFINED;
  8217. /**
  8218. * The mouse tracker object. All methods starting with "on" are primary DOM event handlers.
  8219. * Subsequent methods should be named differently from what they are doing.
  8220. * @param {Object} chart The Chart instance
  8221. * @param {Object} options The root options object
  8222. */
  8223. var Pointer = Highcharts.Pointer = function (chart, options) {
  8224. this.init(chart, options);
  8225. };
  8226. Pointer.prototype = {
  8227. /**
  8228. * Initialize Pointer
  8229. */
  8230. init: function (chart, options) {
  8231. var chartOptions = options.chart,
  8232. chartEvents = chartOptions.events,
  8233. zoomType = useCanVG ? '' : chartOptions.zoomType,
  8234. inverted = chart.inverted,
  8235. zoomX,
  8236. zoomY;
  8237. // Store references
  8238. this.options = options;
  8239. this.chart = chart;
  8240. // Zoom status
  8241. this.zoomX = zoomX = /x/.test(zoomType);
  8242. this.zoomY = zoomY = /y/.test(zoomType);
  8243. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  8244. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  8245. this.hasZoom = zoomX || zoomY;
  8246. // Do we need to handle click on a touch device?
  8247. this.runChartClick = chartEvents && !!chartEvents.click;
  8248. this.pinchDown = [];
  8249. this.lastValidTouch = {};
  8250. if (Highcharts.Tooltip && options.tooltip.enabled) {
  8251. chart.tooltip = new Tooltip(chart, options.tooltip);
  8252. this.followTouchMove = pick(options.tooltip.followTouchMove, true);
  8253. }
  8254. this.setDOMEvents();
  8255. },
  8256. /**
  8257. * Add crossbrowser support for chartX and chartY
  8258. * @param {Object} e The event object in standard browsers
  8259. */
  8260. normalize: function (e, chartPosition) {
  8261. var chartX,
  8262. chartY,
  8263. ePos;
  8264. // common IE normalizing
  8265. e = e || window.event;
  8266. // Framework specific normalizing (#1165)
  8267. e = washMouseEvent(e);
  8268. // More IE normalizing, needs to go after washMouseEvent
  8269. if (!e.target) {
  8270. e.target = e.srcElement;
  8271. }
  8272. // iOS (#2757)
  8273. ePos = e.touches ? (e.touches.length ? e.touches.item(0) : e.changedTouches[0]) : e;
  8274. // Get mouse position
  8275. if (!chartPosition) {
  8276. this.chartPosition = chartPosition = offset(this.chart.container);
  8277. }
  8278. // chartX and chartY
  8279. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  8280. chartX = mathMax(e.x, e.clientX - chartPosition.left); // #2005, #2129: the second case is
  8281. // for IE10 quirks mode within framesets
  8282. chartY = e.y;
  8283. } else {
  8284. chartX = ePos.pageX - chartPosition.left;
  8285. chartY = ePos.pageY - chartPosition.top;
  8286. }
  8287. return extend(e, {
  8288. chartX: mathRound(chartX),
  8289. chartY: mathRound(chartY)
  8290. });
  8291. },
  8292. /**
  8293. * Get the click position in terms of axis values.
  8294. *
  8295. * @param {Object} e A pointer event
  8296. */
  8297. getCoordinates: function (e) {
  8298. var coordinates = {
  8299. xAxis: [],
  8300. yAxis: []
  8301. };
  8302. each(this.chart.axes, function (axis) {
  8303. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  8304. axis: axis,
  8305. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  8306. });
  8307. });
  8308. return coordinates;
  8309. },
  8310. /**
  8311. * With line type charts with a single tracker, get the point closest to the mouse.
  8312. * Run Point.onMouseOver and display tooltip for the point or points.
  8313. */
  8314. runPointActions: function (e) {
  8315. var pointer = this,
  8316. chart = pointer.chart,
  8317. series = chart.series,
  8318. tooltip = chart.tooltip,
  8319. shared = tooltip ? tooltip.shared : false,
  8320. followPointer,
  8321. hoverPoint = chart.hoverPoint,
  8322. hoverSeries = chart.hoverSeries,
  8323. i,
  8324. distance = chart.chartWidth,
  8325. anchor,
  8326. noSharedTooltip,
  8327. directTouch,
  8328. kdpoints = [],
  8329. kdpoint,
  8330. kdpointT;
  8331. // For hovering over the empty parts of the plot area (hoverSeries is undefined).
  8332. // If there is one series with point tracking (combo chart), don't go to nearest neighbour.
  8333. if (!shared && !hoverSeries) {
  8334. for (i = 0; i < series.length; i++) {
  8335. if (series[i].directTouch || !series[i].options.stickyTracking) {
  8336. series = [];
  8337. }
  8338. }
  8339. }
  8340. // If it has a hoverPoint and that series requires direct touch (like columns),
  8341. // use the hoverPoint (#3899). Otherwise, search the k-d tree.
  8342. if (!shared && hoverSeries && hoverSeries.directTouch && hoverPoint) {
  8343. kdpoint = hoverPoint;
  8344. // Handle shared tooltip or cases where a series is not yet hovered
  8345. } else {
  8346. // Find nearest points on all series
  8347. each(series, function (s) {
  8348. // Skip hidden series
  8349. noSharedTooltip = s.noSharedTooltip && shared;
  8350. directTouch = !shared && s.directTouch;
  8351. if (s.visible && !noSharedTooltip && !directTouch && pick(s.options.enableMouseTracking, true)) { // #3821
  8352. kdpointT = s.searchPoint(e, !noSharedTooltip && s.kdDimensions === 1); // #3828
  8353. if (kdpointT) {
  8354. kdpoints.push(kdpointT);
  8355. }
  8356. }
  8357. });
  8358. // Find absolute nearest point
  8359. each(kdpoints, function (p) {
  8360. if (p && typeof p.dist === 'number' && p.dist < distance) {
  8361. distance = p.dist;
  8362. kdpoint = p;
  8363. }
  8364. });
  8365. }
  8366. // Refresh tooltip for kdpoint if new hover point or tooltip was hidden // #3926, #4200
  8367. if (kdpoint && (kdpoint !== this.prevKDPoint || (tooltip && tooltip.isHidden))) {
  8368. // Draw tooltip if necessary
  8369. if (shared && !kdpoint.series.noSharedTooltip) {
  8370. i = kdpoints.length;
  8371. while (i--) {
  8372. if (kdpoints[i].clientX !== kdpoint.clientX || kdpoints[i].series.noSharedTooltip) {
  8373. kdpoints.splice(i, 1);
  8374. }
  8375. }
  8376. if (kdpoints.length && tooltip) {
  8377. tooltip.refresh(kdpoints, e);
  8378. }
  8379. // Do mouseover on all points (#3919, #3985, #4410)
  8380. each(kdpoints, function (point) {
  8381. point.onMouseOver(e, point !== ((hoverSeries && hoverSeries.directTouch && hoverPoint) || kdpoint));
  8382. });
  8383. } else {
  8384. if (tooltip) {
  8385. tooltip.refresh(kdpoint, e);
  8386. }
  8387. if(!hoverSeries || !hoverSeries.directTouch) { // #4448
  8388. kdpoint.onMouseOver(e);
  8389. }
  8390. }
  8391. this.prevKDPoint = kdpoint;
  8392. // Update positions (regardless of kdpoint or hoverPoint)
  8393. } else {
  8394. followPointer = hoverSeries && hoverSeries.tooltipOptions.followPointer;
  8395. if (tooltip && followPointer && !tooltip.isHidden) {
  8396. anchor = tooltip.getAnchor([{}], e);
  8397. tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
  8398. }
  8399. }
  8400. // Start the event listener to pick up the tooltip
  8401. if (tooltip && !pointer._onDocumentMouseMove) {
  8402. pointer._onDocumentMouseMove = function (e) {
  8403. if (charts[hoverChartIndex]) {
  8404. charts[hoverChartIndex].pointer.onDocumentMouseMove(e);
  8405. }
  8406. };
  8407. addEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  8408. }
  8409. // Crosshair
  8410. each(chart.axes, function (axis) {
  8411. axis.drawCrosshair(e, pick(kdpoint, hoverPoint));
  8412. });
  8413. },
  8414. /**
  8415. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  8416. *
  8417. * @param allowMove {Boolean} Instead of destroying the tooltip altogether, allow moving it if possible
  8418. */
  8419. reset: function (allowMove, delay) {
  8420. var pointer = this,
  8421. chart = pointer.chart,
  8422. hoverSeries = chart.hoverSeries,
  8423. hoverPoint = chart.hoverPoint,
  8424. hoverPoints = chart.hoverPoints,
  8425. tooltip = chart.tooltip,
  8426. tooltipPoints = tooltip && tooltip.shared ? hoverPoints : hoverPoint;
  8427. // Narrow in allowMove
  8428. allowMove = allowMove && tooltip && tooltipPoints;
  8429. // Check if the points have moved outside the plot area, #1003
  8430. if (allowMove && splat(tooltipPoints)[0].plotX === UNDEFINED) {
  8431. allowMove = false;
  8432. }
  8433. // Just move the tooltip, #349
  8434. if (allowMove) {
  8435. tooltip.refresh(tooltipPoints);
  8436. if (hoverPoint) { // #2500
  8437. hoverPoint.setState(hoverPoint.state, true);
  8438. each(chart.axes, function (axis) {
  8439. if (pick(axis.options.crosshair && axis.options.crosshair.snap, true)) {
  8440. axis.drawCrosshair(null, hoverPoint);
  8441. } else {
  8442. axis.hideCrosshair();
  8443. }
  8444. });
  8445. }
  8446. // Full reset
  8447. } else {
  8448. if (hoverPoint) {
  8449. hoverPoint.onMouseOut();
  8450. }
  8451. if (hoverPoints) {
  8452. each(hoverPoints, function (point) {
  8453. point.setState();
  8454. });
  8455. }
  8456. if (hoverSeries) {
  8457. hoverSeries.onMouseOut();
  8458. }
  8459. if (tooltip) {
  8460. tooltip.hide(delay);
  8461. }
  8462. if (pointer._onDocumentMouseMove) {
  8463. removeEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  8464. pointer._onDocumentMouseMove = null;
  8465. }
  8466. // Remove crosshairs
  8467. each(chart.axes, function (axis) {
  8468. axis.hideCrosshair();
  8469. });
  8470. pointer.hoverX = chart.hoverPoints = chart.hoverPoint = null;
  8471. }
  8472. },
  8473. /**
  8474. * Scale series groups to a certain scale and translation
  8475. */
  8476. scaleGroups: function (attribs, clip) {
  8477. var chart = this.chart,
  8478. seriesAttribs;
  8479. // Scale each series
  8480. each(chart.series, function (series) {
  8481. seriesAttribs = attribs || series.getPlotBox(); // #1701
  8482. if (series.xAxis && series.xAxis.zoomEnabled) {
  8483. series.group.attr(seriesAttribs);
  8484. if (series.markerGroup) {
  8485. series.markerGroup.attr(seriesAttribs);
  8486. series.markerGroup.clip(clip ? chart.clipRect : null);
  8487. }
  8488. if (series.dataLabelsGroup) {
  8489. series.dataLabelsGroup.attr(seriesAttribs);
  8490. }
  8491. }
  8492. });
  8493. // Clip
  8494. chart.clipRect.attr(clip || chart.clipBox);
  8495. },
  8496. /**
  8497. * Start a drag operation
  8498. */
  8499. dragStart: function (e) {
  8500. var chart = this.chart;
  8501. // Record the start position
  8502. chart.mouseIsDown = e.type;
  8503. chart.cancelClick = false;
  8504. chart.mouseDownX = this.mouseDownX = e.chartX;
  8505. chart.mouseDownY = this.mouseDownY = e.chartY;
  8506. },
  8507. /**
  8508. * Perform a drag operation in response to a mousemove event while the mouse is down
  8509. */
  8510. drag: function (e) {
  8511. var chart = this.chart,
  8512. chartOptions = chart.options.chart,
  8513. chartX = e.chartX,
  8514. chartY = e.chartY,
  8515. zoomHor = this.zoomHor,
  8516. zoomVert = this.zoomVert,
  8517. plotLeft = chart.plotLeft,
  8518. plotTop = chart.plotTop,
  8519. plotWidth = chart.plotWidth,
  8520. plotHeight = chart.plotHeight,
  8521. clickedInside,
  8522. size,
  8523. selectionMarker = this.selectionMarker,
  8524. mouseDownX = this.mouseDownX,
  8525. mouseDownY = this.mouseDownY,
  8526. panKey = chartOptions.panKey && e[chartOptions.panKey + 'Key'];
  8527. // If the device supports both touch and mouse (like IE11), and we are touch-dragging
  8528. // inside the plot area, don't handle the mouse event. #4339.
  8529. if (selectionMarker && selectionMarker.touch) {
  8530. return;
  8531. }
  8532. // If the mouse is outside the plot area, adjust to cooordinates
  8533. // inside to prevent the selection marker from going outside
  8534. if (chartX < plotLeft) {
  8535. chartX = plotLeft;
  8536. } else if (chartX > plotLeft + plotWidth) {
  8537. chartX = plotLeft + plotWidth;
  8538. }
  8539. if (chartY < plotTop) {
  8540. chartY = plotTop;
  8541. } else if (chartY > plotTop + plotHeight) {
  8542. chartY = plotTop + plotHeight;
  8543. }
  8544. // determine if the mouse has moved more than 10px
  8545. this.hasDragged = Math.sqrt(
  8546. Math.pow(mouseDownX - chartX, 2) +
  8547. Math.pow(mouseDownY - chartY, 2)
  8548. );
  8549. if (this.hasDragged > 10) {
  8550. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  8551. // make a selection
  8552. if (chart.hasCartesianSeries && (this.zoomX || this.zoomY) && clickedInside && !panKey) {
  8553. if (!selectionMarker) {
  8554. this.selectionMarker = selectionMarker = chart.renderer.rect(
  8555. plotLeft,
  8556. plotTop,
  8557. zoomHor ? 1 : plotWidth,
  8558. zoomVert ? 1 : plotHeight,
  8559. 0
  8560. )
  8561. .attr({
  8562. fill: chartOptions.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  8563. zIndex: 7
  8564. })
  8565. .add();
  8566. }
  8567. }
  8568. // adjust the width of the selection marker
  8569. if (selectionMarker && zoomHor) {
  8570. size = chartX - mouseDownX;
  8571. selectionMarker.attr({
  8572. width: mathAbs(size),
  8573. x: (size > 0 ? 0 : size) + mouseDownX
  8574. });
  8575. }
  8576. // adjust the height of the selection marker
  8577. if (selectionMarker && zoomVert) {
  8578. size = chartY - mouseDownY;
  8579. selectionMarker.attr({
  8580. height: mathAbs(size),
  8581. y: (size > 0 ? 0 : size) + mouseDownY
  8582. });
  8583. }
  8584. // panning
  8585. if (clickedInside && !selectionMarker && chartOptions.panning) {
  8586. chart.pan(e, chartOptions.panning);
  8587. }
  8588. }
  8589. },
  8590. /**
  8591. * On mouse up or touch end across the entire document, drop the selection.
  8592. */
  8593. drop: function (e) {
  8594. var pointer = this,
  8595. chart = this.chart,
  8596. hasPinched = this.hasPinched;
  8597. if (this.selectionMarker) {
  8598. var selectionData = {
  8599. xAxis: [],
  8600. yAxis: [],
  8601. originalEvent: e.originalEvent || e
  8602. },
  8603. selectionBox = this.selectionMarker,
  8604. selectionLeft = selectionBox.attr ? selectionBox.attr('x') : selectionBox.x,
  8605. selectionTop = selectionBox.attr ? selectionBox.attr('y') : selectionBox.y,
  8606. selectionWidth = selectionBox.attr ? selectionBox.attr('width') : selectionBox.width,
  8607. selectionHeight = selectionBox.attr ? selectionBox.attr('height') : selectionBox.height,
  8608. runZoom;
  8609. // a selection has been made
  8610. if (this.hasDragged || hasPinched) {
  8611. // record each axis' min and max
  8612. each(chart.axes, function (axis) {
  8613. if (axis.zoomEnabled && defined(axis.min) && (hasPinched || pointer[{ xAxis: 'zoomX', yAxis: 'zoomY' }[axis.coll]])) { // #859, #3569
  8614. var horiz = axis.horiz,
  8615. minPixelPadding = e.type === 'touchend' ? axis.minPixelPadding: 0, // #1207, #3075
  8616. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop) + minPixelPadding),
  8617. selectionMax = axis.toValue((horiz ? selectionLeft + selectionWidth : selectionTop + selectionHeight) - minPixelPadding);
  8618. selectionData[axis.coll].push({
  8619. axis: axis,
  8620. min: mathMin(selectionMin, selectionMax), // for reversed axes
  8621. max: mathMax(selectionMin, selectionMax)
  8622. });
  8623. runZoom = true;
  8624. }
  8625. });
  8626. if (runZoom) {
  8627. fireEvent(chart, 'selection', selectionData, function (args) {
  8628. chart.zoom(extend(args, hasPinched ? { animation: false } : null));
  8629. });
  8630. }
  8631. }
  8632. this.selectionMarker = this.selectionMarker.destroy();
  8633. // Reset scaling preview
  8634. if (hasPinched) {
  8635. this.scaleGroups();
  8636. }
  8637. }
  8638. // Reset all
  8639. if (chart) { // it may be destroyed on mouse up - #877
  8640. css(chart.container, { cursor: chart._cursor });
  8641. chart.cancelClick = this.hasDragged > 10; // #370
  8642. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  8643. this.pinchDown = [];
  8644. }
  8645. },
  8646. onContainerMouseDown: function (e) {
  8647. e = this.normalize(e);
  8648. // issue #295, dragging not always working in Firefox
  8649. if (e.preventDefault) {
  8650. e.preventDefault();
  8651. }
  8652. this.dragStart(e);
  8653. },
  8654. onDocumentMouseUp: function (e) {
  8655. if (charts[hoverChartIndex]) {
  8656. charts[hoverChartIndex].pointer.drop(e);
  8657. }
  8658. },
  8659. /**
  8660. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  8661. * Issue #149 workaround. The mouseleave event does not always fire.
  8662. */
  8663. onDocumentMouseMove: function (e) {
  8664. var chart = this.chart,
  8665. chartPosition = this.chartPosition;
  8666. e = this.normalize(e, chartPosition);
  8667. // If we're outside, hide the tooltip
  8668. if (chartPosition && !this.inClass(e.target, 'highcharts-tracker') &&
  8669. !chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8670. this.reset();
  8671. }
  8672. },
  8673. /**
  8674. * When mouse leaves the container, hide the tooltip.
  8675. */
  8676. onContainerMouseLeave: function () {
  8677. var chart = charts[hoverChartIndex];
  8678. if (chart) {
  8679. chart.pointer.reset();
  8680. chart.pointer.chartPosition = null; // also reset the chart position, used in #149 fix
  8681. }
  8682. },
  8683. // The mousemove, touchmove and touchstart event handler
  8684. onContainerMouseMove: function (e) {
  8685. var chart = this.chart;
  8686. hoverChartIndex = chart.index;
  8687. e = this.normalize(e);
  8688. e.returnValue = false; // #2251, #3224
  8689. if (chart.mouseIsDown === 'mousedown') {
  8690. this.drag(e);
  8691. }
  8692. // Show the tooltip and run mouse over events (#977)
  8693. if ((this.inClass(e.target, 'highcharts-tracker') ||
  8694. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) && !chart.openMenu) {
  8695. this.runPointActions(e);
  8696. }
  8697. },
  8698. /**
  8699. * Utility to detect whether an element has, or has a parent with, a specific
  8700. * class name. Used on detection of tracker objects and on deciding whether
  8701. * hovering the tooltip should cause the active series to mouse out.
  8702. */
  8703. inClass: function (element, className) {
  8704. var elemClassName;
  8705. while (element) {
  8706. elemClassName = attr(element, 'class');
  8707. if (elemClassName) {
  8708. if (elemClassName.indexOf(className) !== -1) {
  8709. return true;
  8710. } else if (elemClassName.indexOf(PREFIX + 'container') !== -1) {
  8711. return false;
  8712. }
  8713. }
  8714. element = element.parentNode;
  8715. }
  8716. },
  8717. onTrackerMouseOut: function (e) {
  8718. var series = this.chart.hoverSeries,
  8719. relatedTarget = e.relatedTarget || e.toElement,
  8720. relatedSeries = relatedTarget && relatedTarget.point && relatedTarget.point.series; // #2499
  8721. if (series && !series.options.stickyTracking && !this.inClass(relatedTarget, PREFIX + 'tooltip') &&
  8722. relatedSeries !== series) {
  8723. series.onMouseOut();
  8724. }
  8725. },
  8726. onContainerClick: function (e) {
  8727. var chart = this.chart,
  8728. hoverPoint = chart.hoverPoint,
  8729. plotLeft = chart.plotLeft,
  8730. plotTop = chart.plotTop;
  8731. e = this.normalize(e);
  8732. e.originalEvent = e; // #3913
  8733. if (!chart.cancelClick) {
  8734. // On tracker click, fire the series and point events. #783, #1583
  8735. if (hoverPoint && this.inClass(e.target, PREFIX + 'tracker')) {
  8736. // the series click event
  8737. fireEvent(hoverPoint.series, 'click', extend(e, {
  8738. point: hoverPoint
  8739. }));
  8740. // the point click event
  8741. if (chart.hoverPoint) { // it may be destroyed (#1844)
  8742. hoverPoint.firePointEvent('click', e);
  8743. }
  8744. // When clicking outside a tracker, fire a chart event
  8745. } else {
  8746. extend(e, this.getCoordinates(e));
  8747. // fire a click event in the chart
  8748. if (chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  8749. fireEvent(chart, 'click', e);
  8750. }
  8751. }
  8752. }
  8753. },
  8754. /**
  8755. * Set the JS DOM events on the container and document. This method should contain
  8756. * a one-to-one assignment between methods and their handlers. Any advanced logic should
  8757. * be moved to the handler reflecting the event's name.
  8758. */
  8759. setDOMEvents: function () {
  8760. var pointer = this,
  8761. container = pointer.chart.container;
  8762. container.onmousedown = function (e) {
  8763. pointer.onContainerMouseDown(e);
  8764. };
  8765. container.onmousemove = function (e) {
  8766. pointer.onContainerMouseMove(e);
  8767. };
  8768. container.onclick = function (e) {
  8769. pointer.onContainerClick(e);
  8770. };
  8771. addEvent(container, 'mouseleave', pointer.onContainerMouseLeave);
  8772. if (chartCount === 1) {
  8773. addEvent(doc, 'mouseup', pointer.onDocumentMouseUp);
  8774. }
  8775. if (hasTouch) {
  8776. container.ontouchstart = function (e) {
  8777. pointer.onContainerTouchStart(e);
  8778. };
  8779. container.ontouchmove = function (e) {
  8780. pointer.onContainerTouchMove(e);
  8781. };
  8782. if (chartCount === 1) {
  8783. addEvent(doc, 'touchend', pointer.onDocumentTouchEnd);
  8784. }
  8785. }
  8786. },
  8787. /**
  8788. * Destroys the Pointer object and disconnects DOM events.
  8789. */
  8790. destroy: function () {
  8791. var prop;
  8792. removeEvent(this.chart.container, 'mouseleave', this.onContainerMouseLeave);
  8793. if (!chartCount) {
  8794. removeEvent(doc, 'mouseup', this.onDocumentMouseUp);
  8795. removeEvent(doc, 'touchend', this.onDocumentTouchEnd);
  8796. }
  8797. // memory and CPU leak
  8798. clearInterval(this.tooltipTimeout);
  8799. for (prop in this) {
  8800. this[prop] = null;
  8801. }
  8802. }
  8803. };
  8804. /* Support for touch devices */
  8805. extend(Highcharts.Pointer.prototype, {
  8806. /**
  8807. * Run translation operations
  8808. */
  8809. pinchTranslate: function (pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  8810. if (this.zoomHor || this.pinchHor) {
  8811. this.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8812. }
  8813. if (this.zoomVert || this.pinchVert) {
  8814. this.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8815. }
  8816. },
  8817. /**
  8818. * Run translation operations for each direction (horizontal and vertical) independently
  8819. */
  8820. pinchTranslateDirection: function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch, forcedScale) {
  8821. var chart = this.chart,
  8822. xy = horiz ? 'x' : 'y',
  8823. XY = horiz ? 'X' : 'Y',
  8824. sChartXY = 'chart' + XY,
  8825. wh = horiz ? 'width' : 'height',
  8826. plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')],
  8827. selectionWH,
  8828. selectionXY,
  8829. clipXY,
  8830. scale = forcedScale || 1,
  8831. inverted = chart.inverted,
  8832. bounds = chart.bounds[horiz ? 'h' : 'v'],
  8833. singleTouch = pinchDown.length === 1,
  8834. touch0Start = pinchDown[0][sChartXY],
  8835. touch0Now = touches[0][sChartXY],
  8836. touch1Start = !singleTouch && pinchDown[1][sChartXY],
  8837. touch1Now = !singleTouch && touches[1][sChartXY],
  8838. outOfBounds,
  8839. transformScale,
  8840. scaleKey,
  8841. setScale = function () {
  8842. if (!singleTouch && mathAbs(touch0Start - touch1Start) > 20) { // Don't zoom if fingers are too close on this axis
  8843. scale = forcedScale || mathAbs(touch0Now - touch1Now) / mathAbs(touch0Start - touch1Start);
  8844. }
  8845. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  8846. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  8847. };
  8848. // Set the scale, first pass
  8849. setScale();
  8850. selectionXY = clipXY; // the clip position (x or y) is altered if out of bounds, the selection position is not
  8851. // Out of bounds
  8852. if (selectionXY < bounds.min) {
  8853. selectionXY = bounds.min;
  8854. outOfBounds = true;
  8855. } else if (selectionXY + selectionWH > bounds.max) {
  8856. selectionXY = bounds.max - selectionWH;
  8857. outOfBounds = true;
  8858. }
  8859. // Is the chart dragged off its bounds, determined by dataMin and dataMax?
  8860. if (outOfBounds) {
  8861. // Modify the touchNow position in order to create an elastic drag movement. This indicates
  8862. // to the user that the chart is responsive but can't be dragged further.
  8863. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  8864. if (!singleTouch) {
  8865. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  8866. }
  8867. // Set the scale, second pass to adapt to the modified touchNow positions
  8868. setScale();
  8869. } else {
  8870. lastValidTouch[xy] = [touch0Now, touch1Now];
  8871. }
  8872. // Set geometry for clipping, selection and transformation
  8873. if (!inverted) { // TODO: implement clipping for inverted charts
  8874. clip[xy] = clipXY - plotLeftTop;
  8875. clip[wh] = selectionWH;
  8876. }
  8877. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  8878. transformScale = inverted ? 1 / scale : scale;
  8879. selectionMarker[wh] = selectionWH;
  8880. selectionMarker[xy] = selectionXY;
  8881. transform[scaleKey] = scale;
  8882. transform['translate' + XY] = (transformScale * plotLeftTop) + (touch0Now - (transformScale * touch0Start));
  8883. },
  8884. /**
  8885. * Handle touch events with two touches
  8886. */
  8887. pinch: function (e) {
  8888. var self = this,
  8889. chart = self.chart,
  8890. pinchDown = self.pinchDown,
  8891. touches = e.touches,
  8892. touchesLength = touches.length,
  8893. lastValidTouch = self.lastValidTouch,
  8894. hasZoom = self.hasZoom,
  8895. selectionMarker = self.selectionMarker,
  8896. transform = {},
  8897. fireClickEvent = touchesLength === 1 && ((self.inClass(e.target, PREFIX + 'tracker') &&
  8898. chart.runTrackerClick) || self.runChartClick),
  8899. clip = {};
  8900. // Don't initiate panning until the user has pinched. This prevents us from
  8901. // blocking page scrolling as users scroll down a long page (#4210).
  8902. if (touchesLength > 1) {
  8903. self.initiated = true;
  8904. }
  8905. // On touch devices, only proceed to trigger click if a handler is defined
  8906. if (hasZoom && self.initiated && !fireClickEvent) {
  8907. e.preventDefault();
  8908. }
  8909. // Normalize each touch
  8910. map(touches, function (e) {
  8911. return self.normalize(e);
  8912. });
  8913. // Register the touch start position
  8914. if (e.type === 'touchstart') {
  8915. each(touches, function (e, i) {
  8916. pinchDown[i] = { chartX: e.chartX, chartY: e.chartY };
  8917. });
  8918. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] && pinchDown[1].chartX];
  8919. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] && pinchDown[1].chartY];
  8920. // Identify the data bounds in pixels
  8921. each(chart.axes, function (axis) {
  8922. if (axis.zoomEnabled) {
  8923. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'],
  8924. minPixelPadding = axis.minPixelPadding,
  8925. min = axis.toPixels(pick(axis.options.min, axis.dataMin)),
  8926. max = axis.toPixels(pick(axis.options.max, axis.dataMax)),
  8927. absMin = mathMin(min, max),
  8928. absMax = mathMax(min, max);
  8929. // Store the bounds for use in the touchmove handler
  8930. bounds.min = mathMin(axis.pos, absMin - minPixelPadding);
  8931. bounds.max = mathMax(axis.pos + axis.len, absMax + minPixelPadding);
  8932. }
  8933. });
  8934. self.res = true; // reset on next move
  8935. // Event type is touchmove, handle panning and pinching
  8936. } else if (pinchDown.length) { // can be 0 when releasing, if touchend fires first
  8937. // Set the marker
  8938. if (!selectionMarker) {
  8939. self.selectionMarker = selectionMarker = extend({
  8940. destroy: noop,
  8941. touch: true
  8942. }, chart.plotBox);
  8943. }
  8944. self.pinchTranslate(pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8945. self.hasPinched = hasZoom;
  8946. // Scale and translate the groups to provide visual feedback during pinching
  8947. self.scaleGroups(transform, clip);
  8948. // Optionally move the tooltip on touchmove
  8949. if (!hasZoom && self.followTouchMove && touchesLength === 1) {
  8950. this.runPointActions(self.normalize(e));
  8951. } else if (self.res) {
  8952. self.res = false;
  8953. this.reset(false, 0);
  8954. }
  8955. }
  8956. },
  8957. /**
  8958. * General touch handler shared by touchstart and touchmove.
  8959. */
  8960. touch: function (e, start) {
  8961. var chart = this.chart;
  8962. hoverChartIndex = chart.index;
  8963. if (e.touches.length === 1) {
  8964. e = this.normalize(e);
  8965. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop) && !chart.openMenu) {
  8966. // Run mouse events and display tooltip etc
  8967. if (start) {
  8968. this.runPointActions(e);
  8969. }
  8970. this.pinch(e);
  8971. } else if (start) {
  8972. // Hide the tooltip on touching outside the plot area (#1203)
  8973. this.reset();
  8974. }
  8975. } else if (e.touches.length === 2) {
  8976. this.pinch(e);
  8977. }
  8978. },
  8979. onContainerTouchStart: function (e) {
  8980. this.touch(e, true);
  8981. },
  8982. onContainerTouchMove: function (e) {
  8983. this.touch(e);
  8984. },
  8985. onDocumentTouchEnd: function (e) {
  8986. if (charts[hoverChartIndex]) {
  8987. charts[hoverChartIndex].pointer.drop(e);
  8988. }
  8989. }
  8990. });
  8991. if (win.PointerEvent || win.MSPointerEvent) {
  8992. // The touches object keeps track of the points being touched at all times
  8993. var touches = {},
  8994. hasPointerEvent = !!win.PointerEvent,
  8995. getWebkitTouches = function () {
  8996. var key, fake = [];
  8997. fake.item = function (i) { return this[i]; };
  8998. for (key in touches) {
  8999. if (touches.hasOwnProperty(key)) {
  9000. fake.push({
  9001. pageX: touches[key].pageX,
  9002. pageY: touches[key].pageY,
  9003. target: touches[key].target
  9004. });
  9005. }
  9006. }
  9007. return fake;
  9008. },
  9009. translateMSPointer = function (e, method, wktype, callback) {
  9010. var p;
  9011. e = e.originalEvent || e;
  9012. if ((e.pointerType === 'touch' || e.pointerType === e.MSPOINTER_TYPE_TOUCH) && charts[hoverChartIndex]) {
  9013. callback(e);
  9014. p = charts[hoverChartIndex].pointer;
  9015. p[method]({
  9016. type: wktype,
  9017. target: e.currentTarget,
  9018. preventDefault: noop,
  9019. touches: getWebkitTouches()
  9020. });
  9021. }
  9022. };
  9023. /**
  9024. * Extend the Pointer prototype with methods for each event handler and more
  9025. */
  9026. extend(Pointer.prototype, {
  9027. onContainerPointerDown: function (e) {
  9028. translateMSPointer(e, 'onContainerTouchStart', 'touchstart', function (e) {
  9029. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY, target: e.currentTarget };
  9030. });
  9031. },
  9032. onContainerPointerMove: function (e) {
  9033. translateMSPointer(e, 'onContainerTouchMove', 'touchmove', function (e) {
  9034. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY };
  9035. if (!touches[e.pointerId].target) {
  9036. touches[e.pointerId].target = e.currentTarget;
  9037. }
  9038. });
  9039. },
  9040. onDocumentPointerUp: function (e) {
  9041. translateMSPointer(e, 'onDocumentTouchEnd', 'touchend', function (e) {
  9042. delete touches[e.pointerId];
  9043. });
  9044. },
  9045. /**
  9046. * Add or remove the MS Pointer specific events
  9047. */
  9048. batchMSEvents: function (fn) {
  9049. fn(this.chart.container, hasPointerEvent ? 'pointerdown' : 'MSPointerDown', this.onContainerPointerDown);
  9050. fn(this.chart.container, hasPointerEvent ? 'pointermove' : 'MSPointerMove', this.onContainerPointerMove);
  9051. fn(doc, hasPointerEvent ? 'pointerup' : 'MSPointerUp', this.onDocumentPointerUp);
  9052. }
  9053. });
  9054. // Disable default IE actions for pinch and such on chart element
  9055. wrap(Pointer.prototype, 'init', function (proceed, chart, options) {
  9056. proceed.call(this, chart, options);
  9057. if (this.hasZoom) { // #4014
  9058. css(chart.container, {
  9059. '-ms-touch-action': NONE,
  9060. 'touch-action': NONE
  9061. });
  9062. }
  9063. });
  9064. // Add IE specific touch events to chart
  9065. wrap(Pointer.prototype, 'setDOMEvents', function (proceed) {
  9066. proceed.apply(this);
  9067. if (this.hasZoom || this.followTouchMove) {
  9068. this.batchMSEvents(addEvent);
  9069. }
  9070. });
  9071. // Destroy MS events also
  9072. wrap(Pointer.prototype, 'destroy', function (proceed) {
  9073. this.batchMSEvents(removeEvent);
  9074. proceed.call(this);
  9075. });
  9076. }
  9077. /**
  9078. * The overview of the chart's series
  9079. */
  9080. var Legend = Highcharts.Legend = function (chart, options) {
  9081. this.init(chart, options);
  9082. };
  9083. Legend.prototype = {
  9084. /**
  9085. * Initialize the legend
  9086. */
  9087. init: function (chart, options) {
  9088. var legend = this,
  9089. itemStyle = options.itemStyle,
  9090. padding,
  9091. itemMarginTop = options.itemMarginTop || 0;
  9092. this.options = options;
  9093. if (!options.enabled) {
  9094. return;
  9095. }
  9096. legend.itemStyle = itemStyle;
  9097. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  9098. legend.itemMarginTop = itemMarginTop;
  9099. legend.padding = padding = pick(options.padding, 8);
  9100. legend.initialItemX = padding;
  9101. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  9102. legend.maxItemWidth = 0;
  9103. legend.chart = chart;
  9104. legend.itemHeight = 0;
  9105. legend.symbolWidth = pick(options.symbolWidth, 16);
  9106. legend.pages = [];
  9107. // Render it
  9108. legend.render();
  9109. // move checkboxes
  9110. addEvent(legend.chart, 'endResize', function () {
  9111. legend.positionCheckboxes();
  9112. });
  9113. },
  9114. /**
  9115. * Set the colors for the legend item
  9116. * @param {Object} item A Series or Point instance
  9117. * @param {Object} visible Dimmed or colored
  9118. */
  9119. colorizeItem: function (item, visible) {
  9120. var legend = this,
  9121. options = legend.options,
  9122. legendItem = item.legendItem,
  9123. legendLine = item.legendLine,
  9124. legendSymbol = item.legendSymbol,
  9125. hiddenColor = legend.itemHiddenStyle.color,
  9126. textColor = visible ? options.itemStyle.color : hiddenColor,
  9127. symbolColor = visible ? (item.legendColor || item.color || '#CCC') : hiddenColor,
  9128. markerOptions = item.options && item.options.marker,
  9129. symbolAttr = { fill: symbolColor },
  9130. key,
  9131. val;
  9132. if (legendItem) {
  9133. legendItem.css({ fill: textColor, color: textColor }); // color for #1553, oldIE
  9134. }
  9135. if (legendLine) {
  9136. legendLine.attr({ stroke: symbolColor });
  9137. }
  9138. if (legendSymbol) {
  9139. // Apply marker options
  9140. if (markerOptions && legendSymbol.isMarker) { // #585
  9141. symbolAttr.stroke = symbolColor;
  9142. markerOptions = item.convertAttribs(markerOptions);
  9143. for (key in markerOptions) {
  9144. val = markerOptions[key];
  9145. if (val !== UNDEFINED) {
  9146. symbolAttr[key] = val;
  9147. }
  9148. }
  9149. }
  9150. legendSymbol.attr(symbolAttr);
  9151. }
  9152. },
  9153. /**
  9154. * Position the legend item
  9155. * @param {Object} item A Series or Point instance
  9156. */
  9157. positionItem: function (item) {
  9158. var legend = this,
  9159. options = legend.options,
  9160. symbolPadding = options.symbolPadding,
  9161. ltr = !options.rtl,
  9162. legendItemPos = item._legendItemPos,
  9163. itemX = legendItemPos[0],
  9164. itemY = legendItemPos[1],
  9165. checkbox = item.checkbox,
  9166. legendGroup = item.legendGroup;
  9167. if (legendGroup && legendGroup.element) {
  9168. legendGroup.translate(
  9169. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  9170. itemY
  9171. );
  9172. }
  9173. if (checkbox) {
  9174. checkbox.x = itemX;
  9175. checkbox.y = itemY;
  9176. }
  9177. },
  9178. /**
  9179. * Destroy a single legend item
  9180. * @param {Object} item The series or point
  9181. */
  9182. destroyItem: function (item) {
  9183. var checkbox = item.checkbox;
  9184. // destroy SVG elements
  9185. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  9186. if (item[key]) {
  9187. item[key] = item[key].destroy();
  9188. }
  9189. });
  9190. if (checkbox) {
  9191. discardElement(item.checkbox);
  9192. }
  9193. },
  9194. /**
  9195. * Destroys the legend.
  9196. */
  9197. destroy: function () {
  9198. var legend = this,
  9199. legendGroup = legend.group,
  9200. box = legend.box;
  9201. if (box) {
  9202. legend.box = box.destroy();
  9203. }
  9204. if (legendGroup) {
  9205. legend.group = legendGroup.destroy();
  9206. }
  9207. },
  9208. /**
  9209. * Position the checkboxes after the width is determined
  9210. */
  9211. positionCheckboxes: function (scrollOffset) {
  9212. var alignAttr = this.group.alignAttr,
  9213. translateY,
  9214. clipHeight = this.clipHeight || this.legendHeight;
  9215. if (alignAttr) {
  9216. translateY = alignAttr.translateY;
  9217. each(this.allItems, function (item) {
  9218. var checkbox = item.checkbox,
  9219. top;
  9220. if (checkbox) {
  9221. top = (translateY + checkbox.y + (scrollOffset || 0) + 3);
  9222. css(checkbox, {
  9223. left: (alignAttr.translateX + item.checkboxOffset + checkbox.x - 20) + PX,
  9224. top: top + PX,
  9225. display: top > translateY - 6 && top < translateY + clipHeight - 6 ? '' : NONE
  9226. });
  9227. }
  9228. });
  9229. }
  9230. },
  9231. /**
  9232. * Render the legend title on top of the legend
  9233. */
  9234. renderTitle: function () {
  9235. var options = this.options,
  9236. padding = this.padding,
  9237. titleOptions = options.title,
  9238. titleHeight = 0,
  9239. bBox;
  9240. if (titleOptions.text) {
  9241. if (!this.title) {
  9242. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, null, null, 'legend-title')
  9243. .attr({ zIndex: 1 })
  9244. .css(titleOptions.style)
  9245. .add(this.group);
  9246. }
  9247. bBox = this.title.getBBox();
  9248. titleHeight = bBox.height;
  9249. this.offsetWidth = bBox.width; // #1717
  9250. this.contentGroup.attr({ translateY: titleHeight });
  9251. }
  9252. this.titleHeight = titleHeight;
  9253. },
  9254. /**
  9255. * Set the legend item text
  9256. */
  9257. setText: function (item) {
  9258. var options = this.options;
  9259. item.legendItem.attr({
  9260. text: options.labelFormat ? format(options.labelFormat, item) : options.labelFormatter.call(item)
  9261. });
  9262. },
  9263. /**
  9264. * Render a single specific legend item
  9265. * @param {Object} item A series or point
  9266. */
  9267. renderItem: function (item) {
  9268. var legend = this,
  9269. chart = legend.chart,
  9270. renderer = chart.renderer,
  9271. options = legend.options,
  9272. horizontal = options.layout === 'horizontal',
  9273. symbolWidth = legend.symbolWidth,
  9274. symbolPadding = options.symbolPadding,
  9275. itemStyle = legend.itemStyle,
  9276. itemHiddenStyle = legend.itemHiddenStyle,
  9277. padding = legend.padding,
  9278. itemDistance = horizontal ? pick(options.itemDistance, 20) : 0,
  9279. ltr = !options.rtl,
  9280. itemHeight,
  9281. widthOption = options.width,
  9282. itemMarginBottom = options.itemMarginBottom || 0,
  9283. itemMarginTop = legend.itemMarginTop,
  9284. initialItemX = legend.initialItemX,
  9285. bBox,
  9286. itemWidth,
  9287. li = item.legendItem,
  9288. series = item.series && item.series.drawLegendSymbol ? item.series : item,
  9289. seriesOptions = series.options,
  9290. showCheckbox = legend.createCheckboxForItem && seriesOptions && seriesOptions.showCheckbox,
  9291. useHTML = options.useHTML;
  9292. if (!li) { // generate it once, later move it
  9293. // Generate the group box
  9294. // A group to hold the symbol and text. Text is to be appended in Legend class.
  9295. item.legendGroup = renderer.g('legend-item')
  9296. .attr({ zIndex: 1 })
  9297. .add(legend.scrollGroup);
  9298. // Generate the list item text and add it to the group
  9299. item.legendItem = li = renderer.text(
  9300. '',
  9301. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  9302. legend.baseline || 0,
  9303. useHTML
  9304. )
  9305. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  9306. .attr({
  9307. align: ltr ? 'left' : 'right',
  9308. zIndex: 2
  9309. })
  9310. .add(item.legendGroup);
  9311. // Get the baseline for the first item - the font size is equal for all
  9312. if (!legend.baseline) {
  9313. legend.fontMetrics = renderer.fontMetrics(itemStyle.fontSize, li);
  9314. legend.baseline = legend.fontMetrics.f + 3 + itemMarginTop;
  9315. li.attr('y', legend.baseline);
  9316. }
  9317. // Draw the legend symbol inside the group box
  9318. series.drawLegendSymbol(legend, item);
  9319. if (legend.setItemEvents) {
  9320. legend.setItemEvents(item, li, useHTML, itemStyle, itemHiddenStyle);
  9321. }
  9322. // Colorize the items
  9323. legend.colorizeItem(item, item.visible);
  9324. // add the HTML checkbox on top
  9325. if (showCheckbox) {
  9326. legend.createCheckboxForItem(item);
  9327. }
  9328. }
  9329. // Always update the text
  9330. legend.setText(item);
  9331. // calculate the positions for the next line
  9332. bBox = li.getBBox();
  9333. itemWidth = item.checkboxOffset =
  9334. options.itemWidth ||
  9335. item.legendItemWidth ||
  9336. symbolWidth + symbolPadding + bBox.width + itemDistance + (showCheckbox ? 20 : 0);
  9337. legend.itemHeight = itemHeight = mathRound(item.legendItemHeight || bBox.height);
  9338. // if the item exceeds the width, start a new line
  9339. if (horizontal && legend.itemX - initialItemX + itemWidth >
  9340. (widthOption || (chart.chartWidth - 2 * padding - initialItemX - options.x))) {
  9341. legend.itemX = initialItemX;
  9342. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  9343. legend.lastLineHeight = 0; // reset for next line (#915, #3976)
  9344. }
  9345. // If the item exceeds the height, start a new column
  9346. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  9347. legend.itemY = legend.initialItemY;
  9348. legend.itemX += legend.maxItemWidth;
  9349. legend.maxItemWidth = 0;
  9350. }*/
  9351. // Set the edge positions
  9352. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  9353. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  9354. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  9355. // cache the position of the newly generated or reordered items
  9356. item._legendItemPos = [legend.itemX, legend.itemY];
  9357. // advance
  9358. if (horizontal) {
  9359. legend.itemX += itemWidth;
  9360. } else {
  9361. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  9362. legend.lastLineHeight = itemHeight;
  9363. }
  9364. // the width of the widest item
  9365. legend.offsetWidth = widthOption || mathMax(
  9366. (horizontal ? legend.itemX - initialItemX - itemDistance : itemWidth) + padding,
  9367. legend.offsetWidth
  9368. );
  9369. },
  9370. /**
  9371. * Get all items, which is one item per series for normal series and one item per point
  9372. * for pie series.
  9373. */
  9374. getAllItems: function () {
  9375. var allItems = [];
  9376. each(this.chart.series, function (series) {
  9377. var seriesOptions = series.options;
  9378. // Handle showInLegend. If the series is linked to another series, defaults to false.
  9379. if (!pick(seriesOptions.showInLegend, !defined(seriesOptions.linkedTo) ? UNDEFINED : false, true)) {
  9380. return;
  9381. }
  9382. // use points or series for the legend item depending on legendType
  9383. allItems = allItems.concat(
  9384. series.legendItems ||
  9385. (seriesOptions.legendType === 'point' ?
  9386. series.data :
  9387. series)
  9388. );
  9389. });
  9390. return allItems;
  9391. },
  9392. /**
  9393. * Adjust the chart margins by reserving space for the legend on only one side
  9394. * of the chart. If the position is set to a corner, top or bottom is reserved
  9395. * for horizontal legends and left or right for vertical ones.
  9396. */
  9397. adjustMargins: function (margin, spacing) {
  9398. var chart = this.chart,
  9399. options = this.options,
  9400. // Use the first letter of each alignment option in order to detect the side
  9401. alignment = options.align.charAt(0) + options.verticalAlign.charAt(0) + options.layout.charAt(0); // #4189 - use charAt(x) notation instead of [x] for IE7
  9402. if (this.display && !options.floating) {
  9403. each([
  9404. /(lth|ct|rth)/,
  9405. /(rtv|rm|rbv)/,
  9406. /(rbh|cb|lbh)/,
  9407. /(lbv|lm|ltv)/
  9408. ], function (alignments, side) {
  9409. if (alignments.test(alignment) && !defined(margin[side])) {
  9410. // Now we have detected on which side of the chart we should reserve space for the legend
  9411. chart[marginNames[side]] = mathMax(
  9412. chart[marginNames[side]],
  9413. chart.legend[(side + 1) % 2 ? 'legendHeight' : 'legendWidth'] +
  9414. [1, -1, -1, 1][side] * options[(side % 2) ? 'x' : 'y'] +
  9415. pick(options.margin, 12) +
  9416. spacing[side]
  9417. );
  9418. }
  9419. });
  9420. }
  9421. },
  9422. /**
  9423. * Render the legend. This method can be called both before and after
  9424. * chart.render. If called after, it will only rearrange items instead
  9425. * of creating new ones.
  9426. */
  9427. render: function () {
  9428. var legend = this,
  9429. chart = legend.chart,
  9430. renderer = chart.renderer,
  9431. legendGroup = legend.group,
  9432. allItems,
  9433. display,
  9434. legendWidth,
  9435. legendHeight,
  9436. box = legend.box,
  9437. options = legend.options,
  9438. padding = legend.padding,
  9439. legendBorderWidth = options.borderWidth,
  9440. legendBackgroundColor = options.backgroundColor;
  9441. legend.itemX = legend.initialItemX;
  9442. legend.itemY = legend.initialItemY;
  9443. legend.offsetWidth = 0;
  9444. legend.lastItemY = 0;
  9445. if (!legendGroup) {
  9446. legend.group = legendGroup = renderer.g('legend')
  9447. .attr({ zIndex: 7 })
  9448. .add();
  9449. legend.contentGroup = renderer.g()
  9450. .attr({ zIndex: 1 }) // above background
  9451. .add(legendGroup);
  9452. legend.scrollGroup = renderer.g()
  9453. .add(legend.contentGroup);
  9454. }
  9455. legend.renderTitle();
  9456. // add each series or point
  9457. allItems = legend.getAllItems();
  9458. // sort by legendIndex
  9459. stableSort(allItems, function (a, b) {
  9460. return ((a.options && a.options.legendIndex) || 0) - ((b.options && b.options.legendIndex) || 0);
  9461. });
  9462. // reversed legend
  9463. if (options.reversed) {
  9464. allItems.reverse();
  9465. }
  9466. legend.allItems = allItems;
  9467. legend.display = display = !!allItems.length;
  9468. // render the items
  9469. legend.lastLineHeight = 0;
  9470. each(allItems, function (item) {
  9471. legend.renderItem(item);
  9472. });
  9473. // Get the box
  9474. legendWidth = (options.width || legend.offsetWidth) + padding;
  9475. legendHeight = legend.lastItemY + legend.lastLineHeight + legend.titleHeight;
  9476. legendHeight = legend.handleOverflow(legendHeight);
  9477. legendHeight += padding;
  9478. // Draw the border and/or background
  9479. if (legendBorderWidth || legendBackgroundColor) {
  9480. if (!box) {
  9481. legend.box = box = renderer.rect(
  9482. 0,
  9483. 0,
  9484. legendWidth,
  9485. legendHeight,
  9486. options.borderRadius,
  9487. legendBorderWidth || 0
  9488. ).attr({
  9489. stroke: options.borderColor,
  9490. 'stroke-width': legendBorderWidth || 0,
  9491. fill: legendBackgroundColor || NONE
  9492. })
  9493. .add(legendGroup)
  9494. .shadow(options.shadow);
  9495. box.isNew = true;
  9496. } else if (legendWidth > 0 && legendHeight > 0) {
  9497. box[box.isNew ? 'attr' : 'animate'](
  9498. box.crisp({ width: legendWidth, height: legendHeight })
  9499. );
  9500. box.isNew = false;
  9501. }
  9502. // hide the border if no items
  9503. box[display ? 'show' : 'hide']();
  9504. }
  9505. legend.legendWidth = legendWidth;
  9506. legend.legendHeight = legendHeight;
  9507. // Now that the legend width and height are established, put the items in the
  9508. // final position
  9509. each(allItems, function (item) {
  9510. legend.positionItem(item);
  9511. });
  9512. // 1.x compatibility: positioning based on style
  9513. /*var props = ['left', 'right', 'top', 'bottom'],
  9514. prop,
  9515. i = 4;
  9516. while (i--) {
  9517. prop = props[i];
  9518. if (options.style[prop] && options.style[prop] !== 'auto') {
  9519. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  9520. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  9521. }
  9522. }*/
  9523. if (display) {
  9524. legendGroup.align(extend({
  9525. width: legendWidth,
  9526. height: legendHeight
  9527. }, options), true, 'spacingBox');
  9528. }
  9529. if (!chart.isResizing) {
  9530. this.positionCheckboxes();
  9531. }
  9532. },
  9533. /**
  9534. * Set up the overflow handling by adding navigation with up and down arrows below the
  9535. * legend.
  9536. */
  9537. handleOverflow: function (legendHeight) {
  9538. var legend = this,
  9539. chart = this.chart,
  9540. renderer = chart.renderer,
  9541. options = this.options,
  9542. optionsY = options.y,
  9543. alignTop = options.verticalAlign === 'top',
  9544. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  9545. maxHeight = options.maxHeight,
  9546. clipHeight,
  9547. clipRect = this.clipRect,
  9548. navOptions = options.navigation,
  9549. animation = pick(navOptions.animation, true),
  9550. arrowSize = navOptions.arrowSize || 12,
  9551. nav = this.nav,
  9552. pages = this.pages,
  9553. padding = this.padding,
  9554. lastY,
  9555. allItems = this.allItems,
  9556. clipToHeight = function (height) {
  9557. clipRect.attr({
  9558. height: height
  9559. });
  9560. // useHTML
  9561. if (legend.contentGroup.div) {
  9562. legend.contentGroup.div.style.clip = 'rect(' + padding + 'px,9999px,' + (padding + height) + 'px,0)';
  9563. }
  9564. };
  9565. // Adjust the height
  9566. if (options.layout === 'horizontal') {
  9567. spaceHeight /= 2;
  9568. }
  9569. if (maxHeight) {
  9570. spaceHeight = mathMin(spaceHeight, maxHeight);
  9571. }
  9572. // Reset the legend height and adjust the clipping rectangle
  9573. pages.length = 0;
  9574. if (legendHeight > spaceHeight) {
  9575. this.clipHeight = clipHeight = mathMax(spaceHeight - 20 - this.titleHeight - padding, 0);
  9576. this.currentPage = pick(this.currentPage, 1);
  9577. this.fullHeight = legendHeight;
  9578. // Fill pages with Y positions so that the top of each a legend item defines
  9579. // the scroll top for each page (#2098)
  9580. each(allItems, function (item, i) {
  9581. var y = item._legendItemPos[1],
  9582. h = mathRound(item.legendItem.getBBox().height),
  9583. len = pages.length;
  9584. if (!len || (y - pages[len - 1] > clipHeight && (lastY || y) !== pages[len - 1])) {
  9585. pages.push(lastY || y);
  9586. len++;
  9587. }
  9588. if (i === allItems.length - 1 && y + h - pages[len - 1] > clipHeight) {
  9589. pages.push(y);
  9590. }
  9591. if (y !== lastY) {
  9592. lastY = y;
  9593. }
  9594. });
  9595. // Only apply clipping if needed. Clipping causes blurred legend in PDF export (#1787)
  9596. if (!clipRect) {
  9597. clipRect = legend.clipRect = renderer.clipRect(0, padding, 9999, 0);
  9598. legend.contentGroup.clip(clipRect);
  9599. }
  9600. clipToHeight(clipHeight);
  9601. // Add navigation elements
  9602. if (!nav) {
  9603. this.nav = nav = renderer.g().attr({ zIndex: 1 }).add(this.group);
  9604. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  9605. .on('click', function () {
  9606. legend.scroll(-1, animation);
  9607. })
  9608. .add(nav);
  9609. this.pager = renderer.text('', 15, 10)
  9610. .css(navOptions.style)
  9611. .add(nav);
  9612. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  9613. .on('click', function () {
  9614. legend.scroll(1, animation);
  9615. })
  9616. .add(nav);
  9617. }
  9618. // Set initial position
  9619. legend.scroll(0);
  9620. legendHeight = spaceHeight;
  9621. } else if (nav) {
  9622. clipToHeight(chart.chartHeight);
  9623. nav.hide();
  9624. this.scrollGroup.attr({
  9625. translateY: 1
  9626. });
  9627. this.clipHeight = 0; // #1379
  9628. }
  9629. return legendHeight;
  9630. },
  9631. /**
  9632. * Scroll the legend by a number of pages
  9633. * @param {Object} scrollBy
  9634. * @param {Object} animation
  9635. */
  9636. scroll: function (scrollBy, animation) {
  9637. var pages = this.pages,
  9638. pageCount = pages.length,
  9639. currentPage = this.currentPage + scrollBy,
  9640. clipHeight = this.clipHeight,
  9641. navOptions = this.options.navigation,
  9642. activeColor = navOptions.activeColor,
  9643. inactiveColor = navOptions.inactiveColor,
  9644. pager = this.pager,
  9645. padding = this.padding,
  9646. scrollOffset;
  9647. // When resizing while looking at the last page
  9648. if (currentPage > pageCount) {
  9649. currentPage = pageCount;
  9650. }
  9651. if (currentPage > 0) {
  9652. if (animation !== UNDEFINED) {
  9653. setAnimation(animation, this.chart);
  9654. }
  9655. this.nav.attr({
  9656. translateX: padding,
  9657. translateY: clipHeight + this.padding + 7 + this.titleHeight,
  9658. visibility: VISIBLE
  9659. });
  9660. this.up.attr({
  9661. fill: currentPage === 1 ? inactiveColor : activeColor
  9662. })
  9663. .css({
  9664. cursor: currentPage === 1 ? 'default' : 'pointer'
  9665. });
  9666. pager.attr({
  9667. text: currentPage + '/' + pageCount
  9668. });
  9669. this.down.attr({
  9670. x: 18 + this.pager.getBBox().width, // adjust to text width
  9671. fill: currentPage === pageCount ? inactiveColor : activeColor
  9672. })
  9673. .css({
  9674. cursor: currentPage === pageCount ? 'default' : 'pointer'
  9675. });
  9676. scrollOffset = -pages[currentPage - 1] + this.initialItemY;
  9677. this.scrollGroup.animate({
  9678. translateY: scrollOffset
  9679. });
  9680. this.currentPage = currentPage;
  9681. this.positionCheckboxes(scrollOffset);
  9682. }
  9683. }
  9684. };
  9685. /*
  9686. * LegendSymbolMixin
  9687. */
  9688. var LegendSymbolMixin = Highcharts.LegendSymbolMixin = {
  9689. /**
  9690. * Get the series' symbol in the legend
  9691. *
  9692. * @param {Object} legend The legend object
  9693. * @param {Object} item The series (this) or point
  9694. */
  9695. drawRectangle: function (legend, item) {
  9696. var symbolHeight = legend.options.symbolHeight || legend.fontMetrics.f;
  9697. item.legendSymbol = this.chart.renderer.rect(
  9698. 0,
  9699. legend.baseline - symbolHeight + 1, // #3988
  9700. legend.symbolWidth,
  9701. symbolHeight,
  9702. legend.options.symbolRadius || 0
  9703. ).attr({
  9704. zIndex: 3
  9705. }).add(item.legendGroup);
  9706. },
  9707. /**
  9708. * Get the series' symbol in the legend. This method should be overridable to create custom
  9709. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  9710. *
  9711. * @param {Object} legend The legend object
  9712. */
  9713. drawLineMarker: function (legend) {
  9714. var options = this.options,
  9715. markerOptions = options.marker,
  9716. radius,
  9717. legendSymbol,
  9718. symbolWidth = legend.symbolWidth,
  9719. renderer = this.chart.renderer,
  9720. legendItemGroup = this.legendGroup,
  9721. verticalCenter = legend.baseline - mathRound(legend.fontMetrics.b * 0.3),
  9722. attr;
  9723. // Draw the line
  9724. if (options.lineWidth) {
  9725. attr = {
  9726. 'stroke-width': options.lineWidth
  9727. };
  9728. if (options.dashStyle) {
  9729. attr.dashstyle = options.dashStyle;
  9730. }
  9731. this.legendLine = renderer.path([
  9732. M,
  9733. 0,
  9734. verticalCenter,
  9735. L,
  9736. symbolWidth,
  9737. verticalCenter
  9738. ])
  9739. .attr(attr)
  9740. .add(legendItemGroup);
  9741. }
  9742. // Draw the marker
  9743. if (markerOptions && markerOptions.enabled !== false) {
  9744. radius = markerOptions.radius;
  9745. this.legendSymbol = legendSymbol = renderer.symbol(
  9746. this.symbol,
  9747. (symbolWidth / 2) - radius,
  9748. verticalCenter - radius,
  9749. 2 * radius,
  9750. 2 * radius
  9751. )
  9752. .add(legendItemGroup);
  9753. legendSymbol.isMarker = true;
  9754. }
  9755. }
  9756. };
  9757. // Workaround for #2030, horizontal legend items not displaying in IE11 Preview,
  9758. // and for #2580, a similar drawing flaw in Firefox 26.
  9759. // TODO: Explore if there's a general cause for this. The problem may be related
  9760. // to nested group elements, as the legend item texts are within 4 group elements.
  9761. if (/Trident\/7\.0/.test(userAgent) || isFirefox) {
  9762. wrap(Legend.prototype, 'positionItem', function (proceed, item) {
  9763. var legend = this,
  9764. runPositionItem = function () { // If chart destroyed in sync, this is undefined (#2030)
  9765. if (item._legendItemPos) {
  9766. proceed.call(legend, item);
  9767. }
  9768. };
  9769. // Do it now, for export and to get checkbox placement
  9770. runPositionItem();
  9771. // Do it after to work around the core issue
  9772. setTimeout(runPositionItem);
  9773. });
  9774. }
  9775. /**
  9776. * The chart class
  9777. * @param {Object} options
  9778. * @param {Function} callback Function to run when the chart has loaded
  9779. */
  9780. var Chart = Highcharts.Chart = function () {
  9781. this.init.apply(this, arguments);
  9782. };
  9783. Chart.prototype = {
  9784. /**
  9785. * Hook for modules
  9786. */
  9787. callbacks: [],
  9788. /**
  9789. * Initialize the chart
  9790. */
  9791. init: function (userOptions, callback) {
  9792. // Handle regular options
  9793. var options,
  9794. seriesOptions = userOptions.series; // skip merging data points to increase performance
  9795. userOptions.series = null;
  9796. options = merge(defaultOptions, userOptions); // do the merge
  9797. options.series = userOptions.series = seriesOptions; // set back the series data
  9798. this.userOptions = userOptions;
  9799. var optionsChart = options.chart;
  9800. // Create margin & spacing array
  9801. this.margin = this.splashArray('margin', optionsChart);
  9802. this.spacing = this.splashArray('spacing', optionsChart);
  9803. var chartEvents = optionsChart.events;
  9804. //this.runChartClick = chartEvents && !!chartEvents.click;
  9805. this.bounds = { h: {}, v: {} }; // Pixel data bounds for touch zoom
  9806. this.callback = callback;
  9807. this.isResizing = 0;
  9808. this.options = options;
  9809. //chartTitleOptions = UNDEFINED;
  9810. //chartSubtitleOptions = UNDEFINED;
  9811. this.axes = [];
  9812. this.series = [];
  9813. this.hasCartesianSeries = optionsChart.showAxes;
  9814. //this.axisOffset = UNDEFINED;
  9815. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  9816. //this.inverted = UNDEFINED;
  9817. //this.loadingShown = UNDEFINED;
  9818. //this.container = UNDEFINED;
  9819. //this.chartWidth = UNDEFINED;
  9820. //this.chartHeight = UNDEFINED;
  9821. //this.marginRight = UNDEFINED;
  9822. //this.marginBottom = UNDEFINED;
  9823. //this.containerWidth = UNDEFINED;
  9824. //this.containerHeight = UNDEFINED;
  9825. //this.oldChartWidth = UNDEFINED;
  9826. //this.oldChartHeight = UNDEFINED;
  9827. //this.renderTo = UNDEFINED;
  9828. //this.renderToClone = UNDEFINED;
  9829. //this.spacingBox = UNDEFINED
  9830. //this.legend = UNDEFINED;
  9831. // Elements
  9832. //this.chartBackground = UNDEFINED;
  9833. //this.plotBackground = UNDEFINED;
  9834. //this.plotBGImage = UNDEFINED;
  9835. //this.plotBorder = UNDEFINED;
  9836. //this.loadingDiv = UNDEFINED;
  9837. //this.loadingSpan = UNDEFINED;
  9838. var chart = this,
  9839. eventType;
  9840. // Add the chart to the global lookup
  9841. chart.index = charts.length;
  9842. charts.push(chart);
  9843. chartCount++;
  9844. // Set up auto resize
  9845. if (optionsChart.reflow !== false) {
  9846. addEvent(chart, 'load', function () {
  9847. chart.initReflow();
  9848. });
  9849. }
  9850. // Chart event handlers
  9851. if (chartEvents) {
  9852. for (eventType in chartEvents) {
  9853. addEvent(chart, eventType, chartEvents[eventType]);
  9854. }
  9855. }
  9856. chart.xAxis = [];
  9857. chart.yAxis = [];
  9858. // Expose methods and variables
  9859. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  9860. chart.pointCount = chart.colorCounter = chart.symbolCounter = 0;
  9861. chart.firstRender();
  9862. },
  9863. /**
  9864. * Initialize an individual series, called internally before render time
  9865. */
  9866. initSeries: function (options) {
  9867. var chart = this,
  9868. optionsChart = chart.options.chart,
  9869. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  9870. series,
  9871. constr = seriesTypes[type];
  9872. // No such series type
  9873. if (!constr) {
  9874. error(17, true);
  9875. }
  9876. series = new constr();
  9877. series.init(this, options);
  9878. return series;
  9879. },
  9880. /**
  9881. * Check whether a given point is within the plot area
  9882. *
  9883. * @param {Number} plotX Pixel x relative to the plot area
  9884. * @param {Number} plotY Pixel y relative to the plot area
  9885. * @param {Boolean} inverted Whether the chart is inverted
  9886. */
  9887. isInsidePlot: function (plotX, plotY, inverted) {
  9888. var x = inverted ? plotY : plotX,
  9889. y = inverted ? plotX : plotY;
  9890. return x >= 0 &&
  9891. x <= this.plotWidth &&
  9892. y >= 0 &&
  9893. y <= this.plotHeight;
  9894. },
  9895. /**
  9896. * Redraw legend, axes or series based on updated data
  9897. *
  9898. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9899. * configuration
  9900. */
  9901. redraw: function (animation) {
  9902. var chart = this,
  9903. axes = chart.axes,
  9904. series = chart.series,
  9905. pointer = chart.pointer,
  9906. legend = chart.legend,
  9907. redrawLegend = chart.isDirtyLegend,
  9908. hasStackedSeries,
  9909. hasDirtyStacks,
  9910. hasCartesianSeries = chart.hasCartesianSeries,
  9911. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  9912. seriesLength = series.length,
  9913. i = seriesLength,
  9914. serie,
  9915. renderer = chart.renderer,
  9916. isHiddenChart = renderer.isHidden(),
  9917. afterRedraw = [];
  9918. setAnimation(animation, chart);
  9919. if (isHiddenChart) {
  9920. chart.cloneRenderTo();
  9921. }
  9922. // Adjust title layout (reflow multiline text)
  9923. chart.layOutTitles();
  9924. // link stacked series
  9925. while (i--) {
  9926. serie = series[i];
  9927. if (serie.options.stacking) {
  9928. hasStackedSeries = true;
  9929. if (serie.isDirty) {
  9930. hasDirtyStacks = true;
  9931. break;
  9932. }
  9933. }
  9934. }
  9935. if (hasDirtyStacks) { // mark others as dirty
  9936. i = seriesLength;
  9937. while (i--) {
  9938. serie = series[i];
  9939. if (serie.options.stacking) {
  9940. serie.isDirty = true;
  9941. }
  9942. }
  9943. }
  9944. // Handle updated data in the series
  9945. each(series, function (serie) {
  9946. if (serie.isDirty) {
  9947. if (serie.options.legendType === 'point') {
  9948. if (serie.updateTotals) {
  9949. serie.updateTotals();
  9950. }
  9951. redrawLegend = true;
  9952. }
  9953. }
  9954. });
  9955. // handle added or removed series
  9956. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  9957. // draw legend graphics
  9958. legend.render();
  9959. chart.isDirtyLegend = false;
  9960. }
  9961. // reset stacks
  9962. if (hasStackedSeries) {
  9963. chart.getStacks();
  9964. }
  9965. if (hasCartesianSeries) {
  9966. if (!chart.isResizing) {
  9967. // reset maxTicks
  9968. chart.maxTicks = null;
  9969. // set axes scales
  9970. each(axes, function (axis) {
  9971. axis.setScale();
  9972. });
  9973. }
  9974. }
  9975. chart.getMargins(); // #3098
  9976. if (hasCartesianSeries) {
  9977. // If one axis is dirty, all axes must be redrawn (#792, #2169)
  9978. each(axes, function (axis) {
  9979. if (axis.isDirty) {
  9980. isDirtyBox = true;
  9981. }
  9982. });
  9983. // redraw axes
  9984. each(axes, function (axis) {
  9985. // Fire 'afterSetExtremes' only if extremes are set
  9986. var key = axis.min + ',' + axis.max;
  9987. if (axis.extKey !== key) { // #821, #4452
  9988. axis.extKey = key;
  9989. afterRedraw.push(function () { // prevent a recursive call to chart.redraw() (#1119)
  9990. fireEvent(axis, 'afterSetExtremes', extend(axis.eventArgs, axis.getExtremes())); // #747, #751
  9991. delete axis.eventArgs;
  9992. });
  9993. }
  9994. if (isDirtyBox || hasStackedSeries) {
  9995. axis.redraw();
  9996. }
  9997. });
  9998. }
  9999. // the plot areas size has changed
  10000. if (isDirtyBox) {
  10001. chart.drawChartBox();
  10002. }
  10003. // redraw affected series
  10004. each(series, function (serie) {
  10005. if (serie.isDirty && serie.visible &&
  10006. (!serie.isCartesian || serie.xAxis)) { // issue #153
  10007. serie.redraw();
  10008. }
  10009. });
  10010. // move tooltip or reset
  10011. if (pointer) {
  10012. pointer.reset(true);
  10013. }
  10014. // redraw if canvas
  10015. renderer.draw();
  10016. // fire the event
  10017. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  10018. if (isHiddenChart) {
  10019. chart.cloneRenderTo(true);
  10020. }
  10021. // Fire callbacks that are put on hold until after the redraw
  10022. each(afterRedraw, function (callback) {
  10023. callback.call();
  10024. });
  10025. },
  10026. /**
  10027. * Get an axis, series or point object by id.
  10028. * @param id {String} The id as given in the configuration options
  10029. */
  10030. get: function (id) {
  10031. var chart = this,
  10032. axes = chart.axes,
  10033. series = chart.series;
  10034. var i,
  10035. j,
  10036. points;
  10037. // search axes
  10038. for (i = 0; i < axes.length; i++) {
  10039. if (axes[i].options.id === id) {
  10040. return axes[i];
  10041. }
  10042. }
  10043. // search series
  10044. for (i = 0; i < series.length; i++) {
  10045. if (series[i].options.id === id) {
  10046. return series[i];
  10047. }
  10048. }
  10049. // search points
  10050. for (i = 0; i < series.length; i++) {
  10051. points = series[i].points || [];
  10052. for (j = 0; j < points.length; j++) {
  10053. if (points[j].id === id) {
  10054. return points[j];
  10055. }
  10056. }
  10057. }
  10058. return null;
  10059. },
  10060. /**
  10061. * Create the Axis instances based on the config options
  10062. */
  10063. getAxes: function () {
  10064. var chart = this,
  10065. options = this.options,
  10066. xAxisOptions = options.xAxis = splat(options.xAxis || {}),
  10067. yAxisOptions = options.yAxis = splat(options.yAxis || {}),
  10068. optionsArray,
  10069. axis;
  10070. // make sure the options are arrays and add some members
  10071. each(xAxisOptions, function (axis, i) {
  10072. axis.index = i;
  10073. axis.isX = true;
  10074. });
  10075. each(yAxisOptions, function (axis, i) {
  10076. axis.index = i;
  10077. });
  10078. // concatenate all axis options into one array
  10079. optionsArray = xAxisOptions.concat(yAxisOptions);
  10080. each(optionsArray, function (axisOptions) {
  10081. axis = new Axis(chart, axisOptions);
  10082. });
  10083. },
  10084. /**
  10085. * Get the currently selected points from all series
  10086. */
  10087. getSelectedPoints: function () {
  10088. var points = [];
  10089. each(this.series, function (serie) {
  10090. points = points.concat(grep(serie.points || [], function (point) {
  10091. return point.selected;
  10092. }));
  10093. });
  10094. return points;
  10095. },
  10096. /**
  10097. * Get the currently selected series
  10098. */
  10099. getSelectedSeries: function () {
  10100. return grep(this.series, function (serie) {
  10101. return serie.selected;
  10102. });
  10103. },
  10104. /**
  10105. * Show the title and subtitle of the chart
  10106. *
  10107. * @param titleOptions {Object} New title options
  10108. * @param subtitleOptions {Object} New subtitle options
  10109. *
  10110. */
  10111. setTitle: function (titleOptions, subtitleOptions, redraw) {
  10112. var chart = this,
  10113. options = chart.options,
  10114. chartTitleOptions,
  10115. chartSubtitleOptions;
  10116. chartTitleOptions = options.title = merge(options.title, titleOptions);
  10117. chartSubtitleOptions = options.subtitle = merge(options.subtitle, subtitleOptions);
  10118. // add title and subtitle
  10119. each([
  10120. ['title', titleOptions, chartTitleOptions],
  10121. ['subtitle', subtitleOptions, chartSubtitleOptions]
  10122. ], function (arr) {
  10123. var name = arr[0],
  10124. title = chart[name],
  10125. titleOptions = arr[1],
  10126. chartTitleOptions = arr[2];
  10127. if (title && titleOptions) {
  10128. chart[name] = title = title.destroy(); // remove old
  10129. }
  10130. if (chartTitleOptions && chartTitleOptions.text && !title) {
  10131. chart[name] = chart.renderer.text(
  10132. chartTitleOptions.text,
  10133. 0,
  10134. 0,
  10135. chartTitleOptions.useHTML
  10136. )
  10137. .attr({
  10138. align: chartTitleOptions.align,
  10139. 'class': PREFIX + name,
  10140. zIndex: chartTitleOptions.zIndex || 4
  10141. })
  10142. .css(chartTitleOptions.style)
  10143. .add();
  10144. }
  10145. });
  10146. chart.layOutTitles(redraw);
  10147. },
  10148. /**
  10149. * Lay out the chart titles and cache the full offset height for use in getMargins
  10150. */
  10151. layOutTitles: function (redraw) {
  10152. var titleOffset = 0,
  10153. title = this.title,
  10154. subtitle = this.subtitle,
  10155. options = this.options,
  10156. titleOptions = options.title,
  10157. subtitleOptions = options.subtitle,
  10158. requiresDirtyBox,
  10159. renderer = this.renderer,
  10160. autoWidth = this.spacingBox.width - 44; // 44 makes room for default context button
  10161. if (title) {
  10162. title
  10163. .css({ width: (titleOptions.width || autoWidth) + PX })
  10164. .align(extend({
  10165. y: renderer.fontMetrics(titleOptions.style.fontSize, title).b - 3
  10166. }, titleOptions), false, 'spacingBox');
  10167. if (!titleOptions.floating && !titleOptions.verticalAlign) {
  10168. titleOffset = title.getBBox().height;
  10169. }
  10170. }
  10171. if (subtitle) {
  10172. subtitle
  10173. .css({ width: (subtitleOptions.width || autoWidth) + PX })
  10174. .align(extend({
  10175. y: titleOffset + (titleOptions.margin - 13) + renderer.fontMetrics(titleOptions.style.fontSize, subtitle).b
  10176. }, subtitleOptions), false, 'spacingBox');
  10177. if (!subtitleOptions.floating && !subtitleOptions.verticalAlign) {
  10178. titleOffset = mathCeil(titleOffset + subtitle.getBBox().height);
  10179. }
  10180. }
  10181. requiresDirtyBox = this.titleOffset !== titleOffset;
  10182. this.titleOffset = titleOffset; // used in getMargins
  10183. if (!this.isDirtyBox && requiresDirtyBox) {
  10184. this.isDirtyBox = requiresDirtyBox;
  10185. // Redraw if necessary (#2719, #2744)
  10186. if (this.hasRendered && pick(redraw, true) && this.isDirtyBox) {
  10187. this.redraw();
  10188. }
  10189. }
  10190. },
  10191. /**
  10192. * Get chart width and height according to options and container size
  10193. */
  10194. getChartSize: function () {
  10195. var chart = this,
  10196. optionsChart = chart.options.chart,
  10197. widthOption = optionsChart.width,
  10198. heightOption = optionsChart.height,
  10199. renderTo = chart.renderToClone || chart.renderTo;
  10200. // get inner width and height from jQuery (#824)
  10201. if (!defined(widthOption)) {
  10202. chart.containerWidth = adapterRun(renderTo, 'width');
  10203. }
  10204. if (!defined(heightOption)) {
  10205. chart.containerHeight = adapterRun(renderTo, 'height');
  10206. }
  10207. chart.chartWidth = mathMax(0, widthOption || chart.containerWidth || 600); // #1393, 1460
  10208. chart.chartHeight = mathMax(0, pick(heightOption,
  10209. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  10210. chart.containerHeight > 19 ? chart.containerHeight : 400));
  10211. },
  10212. /**
  10213. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  10214. * size computation on chart.render and chart.redraw
  10215. */
  10216. cloneRenderTo: function (revert) {
  10217. var clone = this.renderToClone,
  10218. container = this.container;
  10219. // Destroy the clone and bring the container back to the real renderTo div
  10220. if (revert) {
  10221. if (clone) {
  10222. this.renderTo.appendChild(container);
  10223. discardElement(clone);
  10224. delete this.renderToClone;
  10225. }
  10226. // Set up the clone
  10227. } else {
  10228. if (container && container.parentNode === this.renderTo) {
  10229. this.renderTo.removeChild(container); // do not clone this
  10230. }
  10231. this.renderToClone = clone = this.renderTo.cloneNode(0);
  10232. css(clone, {
  10233. position: ABSOLUTE,
  10234. top: '-9999px',
  10235. display: 'block' // #833
  10236. });
  10237. if (clone.style.setProperty) { // #2631
  10238. clone.style.setProperty('display', 'block', 'important');
  10239. }
  10240. doc.body.appendChild(clone);
  10241. if (container) {
  10242. clone.appendChild(container);
  10243. }
  10244. }
  10245. },
  10246. /**
  10247. * Get the containing element, determine the size and create the inner container
  10248. * div to hold the chart
  10249. */
  10250. getContainer: function () {
  10251. var chart = this,
  10252. container,
  10253. optionsChart = chart.options.chart,
  10254. chartWidth,
  10255. chartHeight,
  10256. renderTo,
  10257. indexAttrName = 'data-highcharts-chart',
  10258. oldChartIndex,
  10259. containerId;
  10260. chart.renderTo = renderTo = optionsChart.renderTo;
  10261. containerId = PREFIX + idCounter++;
  10262. if (isString(renderTo)) {
  10263. chart.renderTo = renderTo = doc.getElementById(renderTo);
  10264. }
  10265. // Display an error if the renderTo is wrong
  10266. if (!renderTo) {
  10267. error(13, true);
  10268. }
  10269. // If the container already holds a chart, destroy it. The check for hasRendered is there
  10270. // because web pages that are saved to disk from the browser, will preserve the data-highcharts-chart
  10271. // attribute and the SVG contents, but not an interactive chart. So in this case,
  10272. // charts[oldChartIndex] will point to the wrong chart if any (#2609).
  10273. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  10274. if (!isNaN(oldChartIndex) && charts[oldChartIndex] && charts[oldChartIndex].hasRendered) {
  10275. charts[oldChartIndex].destroy();
  10276. }
  10277. // Make a reference to the chart from the div
  10278. attr(renderTo, indexAttrName, chart.index);
  10279. // remove previous chart
  10280. renderTo.innerHTML = '';
  10281. // If the container doesn't have an offsetWidth, it has or is a child of a node
  10282. // that has display:none. We need to temporarily move it out to a visible
  10283. // state to determine the size, else the legend and tooltips won't render
  10284. // properly. The allowClone option is used in sparklines as a micro optimization,
  10285. // saving about 1-2 ms each chart.
  10286. if (!optionsChart.skipClone && !renderTo.offsetWidth) {
  10287. chart.cloneRenderTo();
  10288. }
  10289. // get the width and height
  10290. chart.getChartSize();
  10291. chartWidth = chart.chartWidth;
  10292. chartHeight = chart.chartHeight;
  10293. // create the inner container
  10294. chart.container = container = createElement(DIV, {
  10295. className: PREFIX + 'container' +
  10296. (optionsChart.className ? ' ' + optionsChart.className : ''),
  10297. id: containerId
  10298. }, extend({
  10299. position: RELATIVE,
  10300. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  10301. // content overflow in IE
  10302. width: chartWidth + PX,
  10303. height: chartHeight + PX,
  10304. textAlign: 'left',
  10305. lineHeight: 'normal', // #427
  10306. zIndex: 0, // #1072
  10307. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  10308. }, optionsChart.style),
  10309. chart.renderToClone || renderTo
  10310. );
  10311. // cache the cursor (#1650)
  10312. chart._cursor = container.style.cursor;
  10313. // Initialize the renderer
  10314. chart.renderer =
  10315. optionsChart.forExport ? // force SVG, used for SVG export
  10316. new SVGRenderer(container, chartWidth, chartHeight, optionsChart.style, true) :
  10317. new Renderer(container, chartWidth, chartHeight, optionsChart.style);
  10318. if (useCanVG) {
  10319. // If we need canvg library, extend and configure the renderer
  10320. // to get the tracker for translating mouse events
  10321. chart.renderer.create(chart, container, chartWidth, chartHeight);
  10322. }
  10323. // Add a reference to the charts index
  10324. chart.renderer.chartIndex = chart.index;
  10325. },
  10326. /**
  10327. * Calculate margins by rendering axis labels in a preliminary position. Title,
  10328. * subtitle and legend have already been rendered at this stage, but will be
  10329. * moved into their final positions
  10330. */
  10331. getMargins: function (skipAxes) {
  10332. var chart = this,
  10333. spacing = chart.spacing,
  10334. margin = chart.margin,
  10335. titleOffset = chart.titleOffset;
  10336. chart.resetMargins();
  10337. // Adjust for title and subtitle
  10338. if (titleOffset && !defined(margin[0])) {
  10339. chart.plotTop = mathMax(chart.plotTop, titleOffset + chart.options.title.margin + spacing[0]);
  10340. }
  10341. // Adjust for legend
  10342. chart.legend.adjustMargins(margin, spacing);
  10343. // adjust for scroller
  10344. if (chart.extraBottomMargin) {
  10345. chart.marginBottom += chart.extraBottomMargin;
  10346. }
  10347. if (chart.extraTopMargin) {
  10348. chart.plotTop += chart.extraTopMargin;
  10349. }
  10350. if (!skipAxes) {
  10351. this.getAxisMargins();
  10352. }
  10353. },
  10354. getAxisMargins: function () {
  10355. var chart = this,
  10356. axisOffset = chart.axisOffset = [0, 0, 0, 0], // top, right, bottom, left
  10357. margin = chart.margin;
  10358. // pre-render axes to get labels offset width
  10359. if (chart.hasCartesianSeries) {
  10360. each(chart.axes, function (axis) {
  10361. axis.getOffset();
  10362. });
  10363. }
  10364. // Add the axis offsets
  10365. each(marginNames, function (m, side) {
  10366. if (!defined(margin[side])) {
  10367. chart[m] += axisOffset[side];
  10368. }
  10369. });
  10370. chart.setChartSize();
  10371. },
  10372. /**
  10373. * Resize the chart to its container if size is not explicitly set
  10374. */
  10375. reflow: function (e) {
  10376. var chart = this,
  10377. optionsChart = chart.options.chart,
  10378. renderTo = chart.renderTo,
  10379. width = optionsChart.width || adapterRun(renderTo, 'width'),
  10380. height = optionsChart.height || adapterRun(renderTo, 'height'),
  10381. target = e ? e.target : win, // #805 - MooTools doesn't supply e
  10382. doReflow = function () {
  10383. if (chart.container) { // It may have been destroyed in the meantime (#1257)
  10384. chart.setSize(width, height, false);
  10385. chart.hasUserSize = null;
  10386. }
  10387. };
  10388. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  10389. // win in Firefox, Chrome and IE9.
  10390. if (!chart.hasUserSize && !chart.isPrinting && width && height && (target === win || target === doc)) { // #1093
  10391. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  10392. clearTimeout(chart.reflowTimeout);
  10393. if (e) { // Called from window.resize
  10394. chart.reflowTimeout = setTimeout(doReflow, 100);
  10395. } else { // Called directly (#2224)
  10396. doReflow();
  10397. }
  10398. }
  10399. chart.containerWidth = width;
  10400. chart.containerHeight = height;
  10401. }
  10402. },
  10403. /**
  10404. * Add the event handlers necessary for auto resizing
  10405. */
  10406. initReflow: function () {
  10407. var chart = this,
  10408. reflow = function (e) {
  10409. chart.reflow(e);
  10410. };
  10411. addEvent(win, 'resize', reflow);
  10412. addEvent(chart, 'destroy', function () {
  10413. removeEvent(win, 'resize', reflow);
  10414. });
  10415. },
  10416. /**
  10417. * Resize the chart to a given width and height
  10418. * @param {Number} width
  10419. * @param {Number} height
  10420. * @param {Object|Boolean} animation
  10421. */
  10422. setSize: function (width, height, animation) {
  10423. var chart = this,
  10424. chartWidth,
  10425. chartHeight,
  10426. fireEndResize,
  10427. renderer = chart.renderer,
  10428. globalAnimation = renderer.globalAnimation;
  10429. // Handle the isResizing counter
  10430. chart.isResizing += 1;
  10431. fireEndResize = function () {
  10432. if (chart) {
  10433. fireEvent(chart, 'endResize', null, function () {
  10434. chart.isResizing -= 1;
  10435. });
  10436. }
  10437. };
  10438. // set the animation for the current process
  10439. setAnimation(animation, chart);
  10440. chart.oldChartHeight = chart.chartHeight;
  10441. chart.oldChartWidth = chart.chartWidth;
  10442. if (defined(width)) {
  10443. chart.chartWidth = chartWidth = mathMax(0, mathRound(width));
  10444. chart.hasUserSize = !!chartWidth;
  10445. }
  10446. if (defined(height)) {
  10447. chart.chartHeight = chartHeight = mathMax(0, mathRound(height));
  10448. }
  10449. // Resize the container with the global animation applied if enabled (#2503)
  10450. (globalAnimation ? animate : css)(chart.container, {
  10451. width: chartWidth + PX,
  10452. height: chartHeight + PX
  10453. }, globalAnimation);
  10454. chart.setChartSize(true);
  10455. renderer.setSize(chartWidth, chartHeight, animation);
  10456. // handle axes
  10457. chart.maxTicks = null;
  10458. each(chart.axes, function (axis) {
  10459. axis.isDirty = true;
  10460. axis.setScale();
  10461. });
  10462. // make sure non-cartesian series are also handled
  10463. each(chart.series, function (serie) {
  10464. serie.isDirty = true;
  10465. });
  10466. chart.isDirtyLegend = true; // force legend redraw
  10467. chart.isDirtyBox = true; // force redraw of plot and chart border
  10468. chart.layOutTitles(); // #2857
  10469. chart.getMargins();
  10470. chart.redraw(animation);
  10471. chart.oldChartHeight = null;
  10472. fireEvent(chart, 'resize');
  10473. // fire endResize and set isResizing back
  10474. // If animation is disabled, fire without delay
  10475. if (globalAnimation === false) {
  10476. fireEndResize();
  10477. } else { // else set a timeout with the animation duration
  10478. setTimeout(fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  10479. }
  10480. },
  10481. /**
  10482. * Set the public chart properties. This is done before and after the pre-render
  10483. * to determine margin sizes
  10484. */
  10485. setChartSize: function (skipAxes) {
  10486. var chart = this,
  10487. inverted = chart.inverted,
  10488. renderer = chart.renderer,
  10489. chartWidth = chart.chartWidth,
  10490. chartHeight = chart.chartHeight,
  10491. optionsChart = chart.options.chart,
  10492. spacing = chart.spacing,
  10493. clipOffset = chart.clipOffset,
  10494. clipX,
  10495. clipY,
  10496. plotLeft,
  10497. plotTop,
  10498. plotWidth,
  10499. plotHeight,
  10500. plotBorderWidth;
  10501. chart.plotLeft = plotLeft = mathRound(chart.plotLeft);
  10502. chart.plotTop = plotTop = mathRound(chart.plotTop);
  10503. chart.plotWidth = plotWidth = mathMax(0, mathRound(chartWidth - plotLeft - chart.marginRight));
  10504. chart.plotHeight = plotHeight = mathMax(0, mathRound(chartHeight - plotTop - chart.marginBottom));
  10505. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  10506. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  10507. chart.plotBorderWidth = optionsChart.plotBorderWidth || 0;
  10508. // Set boxes used for alignment
  10509. chart.spacingBox = renderer.spacingBox = {
  10510. x: spacing[3],
  10511. y: spacing[0],
  10512. width: chartWidth - spacing[3] - spacing[1],
  10513. height: chartHeight - spacing[0] - spacing[2]
  10514. };
  10515. chart.plotBox = renderer.plotBox = {
  10516. x: plotLeft,
  10517. y: plotTop,
  10518. width: plotWidth,
  10519. height: plotHeight
  10520. };
  10521. plotBorderWidth = 2 * mathFloor(chart.plotBorderWidth / 2);
  10522. clipX = mathCeil(mathMax(plotBorderWidth, clipOffset[3]) / 2);
  10523. clipY = mathCeil(mathMax(plotBorderWidth, clipOffset[0]) / 2);
  10524. chart.clipBox = {
  10525. x: clipX,
  10526. y: clipY,
  10527. width: mathFloor(chart.plotSizeX - mathMax(plotBorderWidth, clipOffset[1]) / 2 - clipX),
  10528. height: mathMax(0, mathFloor(chart.plotSizeY - mathMax(plotBorderWidth, clipOffset[2]) / 2 - clipY))
  10529. };
  10530. if (!skipAxes) {
  10531. each(chart.axes, function (axis) {
  10532. axis.setAxisSize();
  10533. axis.setAxisTranslation();
  10534. });
  10535. }
  10536. },
  10537. /**
  10538. * Initial margins before auto size margins are applied
  10539. */
  10540. resetMargins: function () {
  10541. var chart = this;
  10542. each(marginNames, function (m, side) {
  10543. chart[m] = pick(chart.margin[side], chart.spacing[side]);
  10544. });
  10545. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  10546. chart.clipOffset = [0, 0, 0, 0];
  10547. },
  10548. /**
  10549. * Draw the borders and backgrounds for chart and plot area
  10550. */
  10551. drawChartBox: function () {
  10552. var chart = this,
  10553. optionsChart = chart.options.chart,
  10554. renderer = chart.renderer,
  10555. chartWidth = chart.chartWidth,
  10556. chartHeight = chart.chartHeight,
  10557. chartBackground = chart.chartBackground,
  10558. plotBackground = chart.plotBackground,
  10559. plotBorder = chart.plotBorder,
  10560. plotBGImage = chart.plotBGImage,
  10561. chartBorderWidth = optionsChart.borderWidth || 0,
  10562. chartBackgroundColor = optionsChart.backgroundColor,
  10563. plotBackgroundColor = optionsChart.plotBackgroundColor,
  10564. plotBackgroundImage = optionsChart.plotBackgroundImage,
  10565. plotBorderWidth = optionsChart.plotBorderWidth || 0,
  10566. mgn,
  10567. bgAttr,
  10568. plotLeft = chart.plotLeft,
  10569. plotTop = chart.plotTop,
  10570. plotWidth = chart.plotWidth,
  10571. plotHeight = chart.plotHeight,
  10572. plotBox = chart.plotBox,
  10573. clipRect = chart.clipRect,
  10574. clipBox = chart.clipBox;
  10575. // Chart area
  10576. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  10577. if (chartBorderWidth || chartBackgroundColor) {
  10578. if (!chartBackground) {
  10579. bgAttr = {
  10580. fill: chartBackgroundColor || NONE
  10581. };
  10582. if (chartBorderWidth) { // #980
  10583. bgAttr.stroke = optionsChart.borderColor;
  10584. bgAttr['stroke-width'] = chartBorderWidth;
  10585. }
  10586. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  10587. optionsChart.borderRadius, chartBorderWidth)
  10588. .attr(bgAttr)
  10589. .addClass(PREFIX + 'background')
  10590. .add()
  10591. .shadow(optionsChart.shadow);
  10592. } else { // resize
  10593. chartBackground.animate(
  10594. chartBackground.crisp({ width: chartWidth - mgn, height: chartHeight - mgn })
  10595. );
  10596. }
  10597. }
  10598. // Plot background
  10599. if (plotBackgroundColor) {
  10600. if (!plotBackground) {
  10601. chart.plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  10602. .attr({
  10603. fill: plotBackgroundColor
  10604. })
  10605. .add()
  10606. .shadow(optionsChart.plotShadow);
  10607. } else {
  10608. plotBackground.animate(plotBox);
  10609. }
  10610. }
  10611. if (plotBackgroundImage) {
  10612. if (!plotBGImage) {
  10613. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  10614. .add();
  10615. } else {
  10616. plotBGImage.animate(plotBox);
  10617. }
  10618. }
  10619. // Plot clip
  10620. if (!clipRect) {
  10621. chart.clipRect = renderer.clipRect(clipBox);
  10622. } else {
  10623. clipRect.animate({
  10624. width: clipBox.width,
  10625. height: clipBox.height
  10626. });
  10627. }
  10628. // Plot area border
  10629. if (plotBorderWidth) {
  10630. if (!plotBorder) {
  10631. chart.plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, -plotBorderWidth)
  10632. .attr({
  10633. stroke: optionsChart.plotBorderColor,
  10634. 'stroke-width': plotBorderWidth,
  10635. fill: NONE,
  10636. zIndex: 1
  10637. })
  10638. .add();
  10639. } else {
  10640. plotBorder.animate(
  10641. plotBorder.crisp({ x: plotLeft, y: plotTop, width: plotWidth, height: plotHeight, strokeWidth: -plotBorderWidth }) //#3282 plotBorder should be negative
  10642. );
  10643. }
  10644. }
  10645. // reset
  10646. chart.isDirtyBox = false;
  10647. },
  10648. /**
  10649. * Detect whether a certain chart property is needed based on inspecting its options
  10650. * and series. This mainly applies to the chart.invert property, and in extensions to
  10651. * the chart.angular and chart.polar properties.
  10652. */
  10653. propFromSeries: function () {
  10654. var chart = this,
  10655. optionsChart = chart.options.chart,
  10656. klass,
  10657. seriesOptions = chart.options.series,
  10658. i,
  10659. value;
  10660. each(['inverted', 'angular', 'polar'], function (key) {
  10661. // The default series type's class
  10662. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  10663. // Get the value from available chart-wide properties
  10664. value = (
  10665. chart[key] || // 1. it is set before
  10666. optionsChart[key] || // 2. it is set in the options
  10667. (klass && klass.prototype[key]) // 3. it's default series class requires it
  10668. );
  10669. // 4. Check if any the chart's series require it
  10670. i = seriesOptions && seriesOptions.length;
  10671. while (!value && i--) {
  10672. klass = seriesTypes[seriesOptions[i].type];
  10673. if (klass && klass.prototype[key]) {
  10674. value = true;
  10675. }
  10676. }
  10677. // Set the chart property
  10678. chart[key] = value;
  10679. });
  10680. },
  10681. /**
  10682. * Link two or more series together. This is done initially from Chart.render,
  10683. * and after Chart.addSeries and Series.remove.
  10684. */
  10685. linkSeries: function () {
  10686. var chart = this,
  10687. chartSeries = chart.series;
  10688. // Reset links
  10689. each(chartSeries, function (series) {
  10690. series.linkedSeries.length = 0;
  10691. });
  10692. // Apply new links
  10693. each(chartSeries, function (series) {
  10694. var linkedTo = series.options.linkedTo;
  10695. if (isString(linkedTo)) {
  10696. if (linkedTo === ':previous') {
  10697. linkedTo = chart.series[series.index - 1];
  10698. } else {
  10699. linkedTo = chart.get(linkedTo);
  10700. }
  10701. if (linkedTo) {
  10702. linkedTo.linkedSeries.push(series);
  10703. series.linkedParent = linkedTo;
  10704. series.visible = pick(series.options.visible, linkedTo.options.visible, series.visible); // #3879
  10705. }
  10706. }
  10707. });
  10708. },
  10709. /**
  10710. * Render series for the chart
  10711. */
  10712. renderSeries: function () {
  10713. each(this.series, function (serie) {
  10714. serie.translate();
  10715. serie.render();
  10716. });
  10717. },
  10718. /**
  10719. * Render labels for the chart
  10720. */
  10721. renderLabels: function () {
  10722. var chart = this,
  10723. labels = chart.options.labels;
  10724. if (labels.items) {
  10725. each(labels.items, function (label) {
  10726. var style = extend(labels.style, label.style),
  10727. x = pInt(style.left) + chart.plotLeft,
  10728. y = pInt(style.top) + chart.plotTop + 12;
  10729. // delete to prevent rewriting in IE
  10730. delete style.left;
  10731. delete style.top;
  10732. chart.renderer.text(
  10733. label.html,
  10734. x,
  10735. y
  10736. )
  10737. .attr({ zIndex: 2 })
  10738. .css(style)
  10739. .add();
  10740. });
  10741. }
  10742. },
  10743. /**
  10744. * Render all graphics for the chart
  10745. */
  10746. render: function () {
  10747. var chart = this,
  10748. axes = chart.axes,
  10749. renderer = chart.renderer,
  10750. options = chart.options,
  10751. tempWidth,
  10752. tempHeight,
  10753. redoHorizontal,
  10754. redoVertical;
  10755. // Title
  10756. chart.setTitle();
  10757. // Legend
  10758. chart.legend = new Legend(chart, options.legend);
  10759. // Get stacks
  10760. if (chart.getStacks) {
  10761. chart.getStacks();
  10762. }
  10763. // Get chart margins
  10764. chart.getMargins(true);
  10765. chart.setChartSize();
  10766. // Record preliminary dimensions for later comparison
  10767. tempWidth = chart.plotWidth;
  10768. tempHeight = chart.plotHeight = chart.plotHeight - 13; // 13 is the most common height of X axis labels
  10769. // Get margins by pre-rendering axes
  10770. each(axes, function (axis) {
  10771. axis.setScale();
  10772. });
  10773. chart.getAxisMargins();
  10774. // If the plot area size has changed significantly, calculate tick positions again
  10775. redoHorizontal = tempWidth / chart.plotWidth > 1.1;
  10776. redoVertical = tempHeight / chart.plotHeight > 1.1;
  10777. if (redoHorizontal || redoVertical) {
  10778. chart.maxTicks = null; // reset for second pass
  10779. each(axes, function (axis) {
  10780. if ((axis.horiz && redoHorizontal) || (!axis.horiz && redoVertical)) {
  10781. axis.setTickInterval(true); // update to reflect the new margins
  10782. }
  10783. });
  10784. chart.getMargins(); // second pass to check for new labels
  10785. }
  10786. // Draw the borders and backgrounds
  10787. chart.drawChartBox();
  10788. // Axes
  10789. if (chart.hasCartesianSeries) {
  10790. each(axes, function (axis) {
  10791. axis.render();
  10792. });
  10793. }
  10794. // The series
  10795. if (!chart.seriesGroup) {
  10796. chart.seriesGroup = renderer.g('series-group')
  10797. .attr({ zIndex: 3 })
  10798. .add();
  10799. }
  10800. chart.renderSeries();
  10801. // Labels
  10802. chart.renderLabels();
  10803. // Credits
  10804. chart.showCredits(options.credits);
  10805. // Set flag
  10806. chart.hasRendered = true;
  10807. },
  10808. /**
  10809. * Show chart credits based on config options
  10810. */
  10811. showCredits: function (credits) {
  10812. if (credits.enabled && !this.credits) {
  10813. this.credits = this.renderer.text(
  10814. credits.text,
  10815. 0,
  10816. 0
  10817. )
  10818. .on('click', function () {
  10819. if (credits.href) {
  10820. location.href = credits.href;
  10821. }
  10822. })
  10823. .attr({
  10824. align: credits.position.align,
  10825. zIndex: 8
  10826. })
  10827. .css(credits.style)
  10828. .add()
  10829. .align(credits.position);
  10830. }
  10831. },
  10832. /**
  10833. * Clean up memory usage
  10834. */
  10835. destroy: function () {
  10836. var chart = this,
  10837. axes = chart.axes,
  10838. series = chart.series,
  10839. container = chart.container,
  10840. i,
  10841. parentNode = container && container.parentNode;
  10842. // fire the chart.destoy event
  10843. fireEvent(chart, 'destroy');
  10844. // Delete the chart from charts lookup array
  10845. charts[chart.index] = UNDEFINED;
  10846. chartCount--;
  10847. chart.renderTo.removeAttribute('data-highcharts-chart');
  10848. // remove events
  10849. removeEvent(chart);
  10850. // ==== Destroy collections:
  10851. // Destroy axes
  10852. i = axes.length;
  10853. while (i--) {
  10854. axes[i] = axes[i].destroy();
  10855. }
  10856. // Destroy each series
  10857. i = series.length;
  10858. while (i--) {
  10859. series[i] = series[i].destroy();
  10860. }
  10861. // ==== Destroy chart properties:
  10862. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage',
  10863. 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'pointer', 'scroller',
  10864. 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  10865. var prop = chart[name];
  10866. if (prop && prop.destroy) {
  10867. chart[name] = prop.destroy();
  10868. }
  10869. });
  10870. // remove container and all SVG
  10871. if (container) { // can break in IE when destroyed before finished loading
  10872. container.innerHTML = '';
  10873. removeEvent(container);
  10874. if (parentNode) {
  10875. discardElement(container);
  10876. }
  10877. }
  10878. // clean it all up
  10879. for (i in chart) {
  10880. delete chart[i];
  10881. }
  10882. },
  10883. /**
  10884. * VML namespaces can't be added until after complete. Listening
  10885. * for Perini's doScroll hack is not enough.
  10886. */
  10887. isReadyToRender: function () {
  10888. var chart = this;
  10889. // Note: in spite of JSLint's complaints, win == win.top is required
  10890. /*jslint eqeq: true*/
  10891. if ((!hasSVG && (win == win.top && doc.readyState !== 'complete')) || (useCanVG && !win.canvg)) {
  10892. /*jslint eqeq: false*/
  10893. if (useCanVG) {
  10894. // Delay rendering until canvg library is downloaded and ready
  10895. CanVGController.push(function () { chart.firstRender(); }, chart.options.global.canvasToolsURL);
  10896. } else {
  10897. doc.attachEvent('onreadystatechange', function () {
  10898. doc.detachEvent('onreadystatechange', chart.firstRender);
  10899. if (doc.readyState === 'complete') {
  10900. chart.firstRender();
  10901. }
  10902. });
  10903. }
  10904. return false;
  10905. }
  10906. return true;
  10907. },
  10908. /**
  10909. * Prepare for first rendering after all data are loaded
  10910. */
  10911. firstRender: function () {
  10912. var chart = this,
  10913. options = chart.options,
  10914. callback = chart.callback;
  10915. // Check whether the chart is ready to render
  10916. if (!chart.isReadyToRender()) {
  10917. return;
  10918. }
  10919. // Create the container
  10920. chart.getContainer();
  10921. // Run an early event after the container and renderer are established
  10922. fireEvent(chart, 'init');
  10923. chart.resetMargins();
  10924. chart.setChartSize();
  10925. // Set the common chart properties (mainly invert) from the given series
  10926. chart.propFromSeries();
  10927. // get axes
  10928. chart.getAxes();
  10929. // Initialize the series
  10930. each(options.series || [], function (serieOptions) {
  10931. chart.initSeries(serieOptions);
  10932. });
  10933. chart.linkSeries();
  10934. // Run an event after axes and series are initialized, but before render. At this stage,
  10935. // the series data is indexed and cached in the xData and yData arrays, so we can access
  10936. // those before rendering. Used in Highstock.
  10937. fireEvent(chart, 'beforeRender');
  10938. // depends on inverted and on margins being set
  10939. if (Highcharts.Pointer) {
  10940. chart.pointer = new Pointer(chart, options);
  10941. }
  10942. chart.render();
  10943. // add canvas
  10944. chart.renderer.draw();
  10945. // run callbacks
  10946. if (callback) {
  10947. callback.apply(chart, [chart]);
  10948. }
  10949. each(chart.callbacks, function (fn) {
  10950. if (chart.index !== UNDEFINED) { // Chart destroyed in its own callback (#3600)
  10951. fn.apply(chart, [chart]);
  10952. }
  10953. });
  10954. // Fire the load event
  10955. fireEvent(chart, 'load');
  10956. // If the chart was rendered outside the top container, put it back in (#3679)
  10957. chart.cloneRenderTo(true);
  10958. },
  10959. /**
  10960. * Creates arrays for spacing and margin from given options.
  10961. */
  10962. splashArray: function (target, options) {
  10963. var oVar = options[target],
  10964. tArray = isObject(oVar) ? oVar : [oVar, oVar, oVar, oVar];
  10965. return [pick(options[target + 'Top'], tArray[0]),
  10966. pick(options[target + 'Right'], tArray[1]),
  10967. pick(options[target + 'Bottom'], tArray[2]),
  10968. pick(options[target + 'Left'], tArray[3])];
  10969. }
  10970. }; // end Chart
  10971. var CenteredSeriesMixin = Highcharts.CenteredSeriesMixin = {
  10972. /**
  10973. * Get the center of the pie based on the size and center options relative to the
  10974. * plot area. Borrowed by the polar and gauge series types.
  10975. */
  10976. getCenter: function () {
  10977. var options = this.options,
  10978. chart = this.chart,
  10979. slicingRoom = 2 * (options.slicedOffset || 0),
  10980. handleSlicingRoom,
  10981. plotWidth = chart.plotWidth - 2 * slicingRoom,
  10982. plotHeight = chart.plotHeight - 2 * slicingRoom,
  10983. centerOption = options.center,
  10984. positions = [pick(centerOption[0], '50%'), pick(centerOption[1], '50%'), options.size || '100%', options.innerSize || 0],
  10985. smallestSize = mathMin(plotWidth, plotHeight),
  10986. i,
  10987. value;
  10988. for (i = 0; i < 4; ++i) {
  10989. value = positions[i];
  10990. handleSlicingRoom = i < 2 || (i === 2 && /%$/.test(value));
  10991. // i == 0: centerX, relative to width
  10992. // i == 1: centerY, relative to height
  10993. // i == 2: size, relative to smallestSize
  10994. // i == 3: innerSize, relative to size
  10995. positions[i] = relativeLength(value, [plotWidth, plotHeight, smallestSize, positions[2]][i]) +
  10996. (handleSlicingRoom ? slicingRoom : 0);
  10997. }
  10998. return positions;
  10999. }
  11000. };
  11001. /**
  11002. * The Point object and prototype. Inheritable and used as base for PiePoint
  11003. */
  11004. var Point = function () {};
  11005. Point.prototype = {
  11006. /**
  11007. * Initialize the point
  11008. * @param {Object} series The series object containing this point
  11009. * @param {Object} options The data in either number, array or object format
  11010. */
  11011. init: function (series, options, x) {
  11012. var point = this,
  11013. colors;
  11014. point.series = series;
  11015. point.color = series.color; // #3445
  11016. point.applyOptions(options, x);
  11017. point.pointAttr = {};
  11018. if (series.options.colorByPoint) {
  11019. colors = series.options.colors || series.chart.options.colors;
  11020. point.color = point.color || colors[series.colorCounter++];
  11021. // loop back to zero
  11022. if (series.colorCounter === colors.length) {
  11023. series.colorCounter = 0;
  11024. }
  11025. }
  11026. series.chart.pointCount++;
  11027. return point;
  11028. },
  11029. /**
  11030. * Apply the options containing the x and y data and possible some extra properties.
  11031. * This is called on point init or from point.update.
  11032. *
  11033. * @param {Object} options
  11034. */
  11035. applyOptions: function (options, x) {
  11036. var point = this,
  11037. series = point.series,
  11038. pointValKey = series.options.pointValKey || series.pointValKey;
  11039. options = Point.prototype.optionsToObject.call(this, options);
  11040. // copy options directly to point
  11041. extend(point, options);
  11042. point.options = point.options ? extend(point.options, options) : options;
  11043. // For higher dimension series types. For instance, for ranges, point.y is mapped to point.low.
  11044. if (pointValKey) {
  11045. point.y = point[pointValKey];
  11046. }
  11047. // If no x is set by now, get auto incremented value. All points must have an
  11048. // x value, however the y value can be null to create a gap in the series
  11049. if (point.x === UNDEFINED && series) {
  11050. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  11051. }
  11052. return point;
  11053. },
  11054. /**
  11055. * Transform number or array configs into objects
  11056. */
  11057. optionsToObject: function (options) {
  11058. var ret = {},
  11059. series = this.series,
  11060. keys = series.options.keys,
  11061. pointArrayMap = keys || series.pointArrayMap || ['y'],
  11062. valueCount = pointArrayMap.length,
  11063. firstItemType,
  11064. i = 0,
  11065. j = 0;
  11066. if (typeof options === 'number' || options === null) {
  11067. ret[pointArrayMap[0]] = options;
  11068. } else if (isArray(options)) {
  11069. // with leading x value
  11070. if (!keys && options.length > valueCount) {
  11071. firstItemType = typeof options[0];
  11072. if (firstItemType === 'string') {
  11073. ret.name = options[0];
  11074. } else if (firstItemType === 'number') {
  11075. ret.x = options[0];
  11076. }
  11077. i++;
  11078. }
  11079. while (j < valueCount) {
  11080. ret[pointArrayMap[j++]] = options[i++];
  11081. }
  11082. } else if (typeof options === 'object') {
  11083. ret = options;
  11084. // This is the fastest way to detect if there are individual point dataLabels that need
  11085. // to be considered in drawDataLabels. These can only occur in object configs.
  11086. if (options.dataLabels) {
  11087. series._hasPointLabels = true;
  11088. }
  11089. // Same approach as above for markers
  11090. if (options.marker) {
  11091. series._hasPointMarkers = true;
  11092. }
  11093. }
  11094. return ret;
  11095. },
  11096. /**
  11097. * Destroy a point to clear memory. Its reference still stays in series.data.
  11098. */
  11099. destroy: function () {
  11100. var point = this,
  11101. series = point.series,
  11102. chart = series.chart,
  11103. hoverPoints = chart.hoverPoints,
  11104. prop;
  11105. chart.pointCount--;
  11106. if (hoverPoints) {
  11107. point.setState();
  11108. erase(hoverPoints, point);
  11109. if (!hoverPoints.length) {
  11110. chart.hoverPoints = null;
  11111. }
  11112. }
  11113. if (point === chart.hoverPoint) {
  11114. point.onMouseOut();
  11115. }
  11116. // remove all events
  11117. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  11118. removeEvent(point);
  11119. point.destroyElements();
  11120. }
  11121. if (point.legendItem) { // pies have legend items
  11122. chart.legend.destroyItem(point);
  11123. }
  11124. for (prop in point) {
  11125. point[prop] = null;
  11126. }
  11127. },
  11128. /**
  11129. * Destroy SVG elements associated with the point
  11130. */
  11131. destroyElements: function () {
  11132. var point = this,
  11133. props = ['graphic', 'dataLabel', 'dataLabelUpper', 'group', 'connector', 'shadowGroup'],
  11134. prop,
  11135. i = 6;
  11136. while (i--) {
  11137. prop = props[i];
  11138. if (point[prop]) {
  11139. point[prop] = point[prop].destroy();
  11140. }
  11141. }
  11142. },
  11143. /**
  11144. * Return the configuration hash needed for the data label and tooltip formatters
  11145. */
  11146. getLabelConfig: function () {
  11147. return {
  11148. x: this.category,
  11149. y: this.y,
  11150. color: this.color,
  11151. key: this.name || this.category,
  11152. series: this.series,
  11153. point: this,
  11154. percentage: this.percentage,
  11155. total: this.total || this.stackTotal
  11156. };
  11157. },
  11158. /**
  11159. * Extendable method for formatting each point's tooltip line
  11160. *
  11161. * @return {String} A string to be concatenated in to the common tooltip text
  11162. */
  11163. tooltipFormatter: function (pointFormat) {
  11164. // Insert options for valueDecimals, valuePrefix, and valueSuffix
  11165. var series = this.series,
  11166. seriesTooltipOptions = series.tooltipOptions,
  11167. valueDecimals = pick(seriesTooltipOptions.valueDecimals, ''),
  11168. valuePrefix = seriesTooltipOptions.valuePrefix || '',
  11169. valueSuffix = seriesTooltipOptions.valueSuffix || '';
  11170. // Loop over the point array map and replace unformatted values with sprintf formatting markup
  11171. each(series.pointArrayMap || ['y'], function (key) {
  11172. key = '{point.' + key; // without the closing bracket
  11173. if (valuePrefix || valueSuffix) {
  11174. pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);
  11175. }
  11176. pointFormat = pointFormat.replace(key + '}', key + ':,.' + valueDecimals + 'f}');
  11177. });
  11178. return format(pointFormat, {
  11179. point: this,
  11180. series: this.series
  11181. });
  11182. },
  11183. /**
  11184. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  11185. * causes a name clash in MooTools
  11186. * @param {String} eventType
  11187. * @param {Object} eventArgs Additional event arguments
  11188. * @param {Function} defaultFunction Default event handler
  11189. */
  11190. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  11191. var point = this,
  11192. series = this.series,
  11193. seriesOptions = series.options;
  11194. // load event handlers on demand to save time on mouseover/out
  11195. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  11196. this.importEvents();
  11197. }
  11198. // add default handler if in selection mode
  11199. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  11200. defaultFunction = function (event) {
  11201. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  11202. if (point.select) { // Could be destroyed by prior event handlers (#2911)
  11203. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  11204. }
  11205. };
  11206. }
  11207. fireEvent(this, eventType, eventArgs, defaultFunction);
  11208. }
  11209. };/**
  11210. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  11211. * in various arrays.
  11212. *
  11213. * - First, series.options.data contains all the original config options for
  11214. * each point whether added by options or methods like series.addPoint.
  11215. * - Next, series.data contains those values converted to points, but in case the series data length
  11216. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  11217. * only contains the points that have been created on demand.
  11218. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  11219. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  11220. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  11221. * be correlated one to one.
  11222. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  11223. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  11224. *
  11225. * @param {Object} chart
  11226. * @param {Object} options
  11227. */
  11228. var Series = Highcharts.Series = function () {};
  11229. Series.prototype = {
  11230. isCartesian: true,
  11231. type: 'line',
  11232. pointClass: Point,
  11233. sorted: true, // requires the data to be sorted
  11234. requireSorting: true,
  11235. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  11236. stroke: 'lineColor',
  11237. 'stroke-width': 'lineWidth',
  11238. fill: 'fillColor',
  11239. r: 'radius'
  11240. },
  11241. axisTypes: ['xAxis', 'yAxis'],
  11242. colorCounter: 0,
  11243. parallelArrays: ['x', 'y'], // each point's x and y values are stored in this.xData and this.yData
  11244. init: function (chart, options) {
  11245. var series = this,
  11246. eventType,
  11247. events,
  11248. chartSeries = chart.series,
  11249. sortByIndex = function (a, b) {
  11250. return pick(a.options.index, a._i) - pick(b.options.index, b._i);
  11251. };
  11252. series.chart = chart;
  11253. series.options = options = series.setOptions(options); // merge with plotOptions
  11254. series.linkedSeries = [];
  11255. // bind the axes
  11256. series.bindAxes();
  11257. // set some variables
  11258. extend(series, {
  11259. name: options.name,
  11260. state: NORMAL_STATE,
  11261. pointAttr: {},
  11262. visible: options.visible !== false, // true by default
  11263. selected: options.selected === true // false by default
  11264. });
  11265. // special
  11266. if (useCanVG) {
  11267. options.animation = false;
  11268. }
  11269. // register event listeners
  11270. events = options.events;
  11271. for (eventType in events) {
  11272. addEvent(series, eventType, events[eventType]);
  11273. }
  11274. if (
  11275. (events && events.click) ||
  11276. (options.point && options.point.events && options.point.events.click) ||
  11277. options.allowPointSelect
  11278. ) {
  11279. chart.runTrackerClick = true;
  11280. }
  11281. series.getColor();
  11282. series.getSymbol();
  11283. // Set the data
  11284. each(series.parallelArrays, function (key) {
  11285. series[key + 'Data'] = [];
  11286. });
  11287. series.setData(options.data, false);
  11288. // Mark cartesian
  11289. if (series.isCartesian) {
  11290. chart.hasCartesianSeries = true;
  11291. }
  11292. // Register it in the chart
  11293. chartSeries.push(series);
  11294. series._i = chartSeries.length - 1;
  11295. // Sort series according to index option (#248, #1123, #2456)
  11296. stableSort(chartSeries, sortByIndex);
  11297. if (this.yAxis) {
  11298. stableSort(this.yAxis.series, sortByIndex);
  11299. }
  11300. each(chartSeries, function (series, i) {
  11301. series.index = i;
  11302. series.name = series.name || 'Series ' + (i + 1);
  11303. });
  11304. },
  11305. /**
  11306. * Set the xAxis and yAxis properties of cartesian series, and register the series
  11307. * in the axis.series array
  11308. */
  11309. bindAxes: function () {
  11310. var series = this,
  11311. seriesOptions = series.options,
  11312. chart = series.chart,
  11313. axisOptions;
  11314. each(series.axisTypes || [], function (AXIS) { // repeat for xAxis and yAxis
  11315. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  11316. axisOptions = axis.options;
  11317. // apply if the series xAxis or yAxis option mathches the number of the
  11318. // axis, or if undefined, use the first axis
  11319. if ((seriesOptions[AXIS] === axisOptions.index) ||
  11320. (seriesOptions[AXIS] !== UNDEFINED && seriesOptions[AXIS] === axisOptions.id) ||
  11321. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  11322. // register this series in the axis.series lookup
  11323. axis.series.push(series);
  11324. // set this series.xAxis or series.yAxis reference
  11325. series[AXIS] = axis;
  11326. // mark dirty for redraw
  11327. axis.isDirty = true;
  11328. }
  11329. });
  11330. // The series needs an X and an Y axis
  11331. if (!series[AXIS] && series.optionalAxis !== AXIS) {
  11332. error(18, true);
  11333. }
  11334. });
  11335. },
  11336. /**
  11337. * For simple series types like line and column, the data values are held in arrays like
  11338. * xData and yData for quick lookup to find extremes and more. For multidimensional series
  11339. * like bubble and map, this can be extended with arrays like zData and valueData by
  11340. * adding to the series.parallelArrays array.
  11341. */
  11342. updateParallelArrays: function (point, i) {
  11343. var series = point.series,
  11344. args = arguments,
  11345. fn = typeof i === 'number' ?
  11346. // Insert the value in the given position
  11347. function (key) {
  11348. var val = key === 'y' && series.toYData ? series.toYData(point) : point[key];
  11349. series[key + 'Data'][i] = val;
  11350. } :
  11351. // Apply the method specified in i with the following arguments as arguments
  11352. function (key) {
  11353. Array.prototype[i].apply(series[key + 'Data'], Array.prototype.slice.call(args, 2));
  11354. };
  11355. each(series.parallelArrays, fn);
  11356. },
  11357. /**
  11358. * Return an auto incremented x value based on the pointStart and pointInterval options.
  11359. * This is only used if an x value is not given for the point that calls autoIncrement.
  11360. */
  11361. autoIncrement: function () {
  11362. var options = this.options,
  11363. xIncrement = this.xIncrement,
  11364. date,
  11365. pointInterval,
  11366. pointIntervalUnit = options.pointIntervalUnit;
  11367. xIncrement = pick(xIncrement, options.pointStart, 0);
  11368. this.pointInterval = pointInterval = pick(this.pointInterval, options.pointInterval, 1);
  11369. // Added code for pointInterval strings
  11370. if (pointIntervalUnit === 'month' || pointIntervalUnit === 'year') {
  11371. date = new Date(xIncrement);
  11372. date = (pointIntervalUnit === 'month') ?
  11373. +date[setMonth](date[getMonth]() + pointInterval) :
  11374. +date[setFullYear](date[getFullYear]() + pointInterval);
  11375. pointInterval = date - xIncrement;
  11376. }
  11377. this.xIncrement = xIncrement + pointInterval;
  11378. return xIncrement;
  11379. },
  11380. /**
  11381. * Divide the series data into segments divided by null values.
  11382. */
  11383. getSegments: function () {
  11384. var series = this,
  11385. lastNull = -1,
  11386. segments = [],
  11387. i,
  11388. points = series.points,
  11389. pointsLength = points.length;
  11390. if (pointsLength) { // no action required for []
  11391. // if connect nulls, just remove null points
  11392. if (series.options.connectNulls) {
  11393. i = pointsLength;
  11394. while (i--) {
  11395. if (points[i].y === null) {
  11396. points.splice(i, 1);
  11397. }
  11398. }
  11399. if (points.length) {
  11400. segments = [points];
  11401. }
  11402. // else, split on null points
  11403. } else {
  11404. each(points, function (point, i) {
  11405. if (point.y === null) {
  11406. if (i > lastNull + 1) {
  11407. segments.push(points.slice(lastNull + 1, i));
  11408. }
  11409. lastNull = i;
  11410. } else if (i === pointsLength - 1) { // last value
  11411. segments.push(points.slice(lastNull + 1, i + 1));
  11412. }
  11413. });
  11414. }
  11415. }
  11416. // register it
  11417. series.segments = segments;
  11418. },
  11419. /**
  11420. * Set the series options by merging from the options tree
  11421. * @param {Object} itemOptions
  11422. */
  11423. setOptions: function (itemOptions) {
  11424. var chart = this.chart,
  11425. chartOptions = chart.options,
  11426. plotOptions = chartOptions.plotOptions,
  11427. userOptions = chart.userOptions || {},
  11428. userPlotOptions = userOptions.plotOptions || {},
  11429. typeOptions = plotOptions[this.type],
  11430. options,
  11431. zones;
  11432. this.userOptions = itemOptions;
  11433. // General series options take precedence over type options because otherwise, default
  11434. // type options like column.animation would be overwritten by the general option.
  11435. // But issues have been raised here (#3881), and the solution may be to distinguish
  11436. // between default option and userOptions like in the tooltip below.
  11437. options = merge(
  11438. typeOptions,
  11439. plotOptions.series,
  11440. itemOptions
  11441. );
  11442. // The tooltip options are merged between global and series specific options
  11443. this.tooltipOptions = merge(
  11444. defaultOptions.tooltip,
  11445. defaultOptions.plotOptions[this.type].tooltip,
  11446. userOptions.tooltip,
  11447. userPlotOptions.series && userPlotOptions.series.tooltip,
  11448. userPlotOptions[this.type] && userPlotOptions[this.type].tooltip,
  11449. itemOptions.tooltip
  11450. );
  11451. // Delete marker object if not allowed (#1125)
  11452. if (typeOptions.marker === null) {
  11453. delete options.marker;
  11454. }
  11455. // Handle color zones
  11456. this.zoneAxis = options.zoneAxis;
  11457. zones = this.zones = (options.zones || []).slice();
  11458. if ((options.negativeColor || options.negativeFillColor) && !options.zones) {
  11459. zones.push({
  11460. value: options[this.zoneAxis + 'Threshold'] || options.threshold || 0,
  11461. color: options.negativeColor,
  11462. fillColor: options.negativeFillColor
  11463. });
  11464. }
  11465. if (zones.length) { // Push one extra zone for the rest
  11466. if (defined(zones[zones.length - 1].value)) {
  11467. zones.push({
  11468. color: this.color,
  11469. fillColor: this.fillColor
  11470. });
  11471. }
  11472. }
  11473. return options;
  11474. },
  11475. getCyclic: function (prop, value, defaults) {
  11476. var i,
  11477. userOptions = this.userOptions,
  11478. indexName = '_' + prop + 'Index',
  11479. counterName = prop + 'Counter';
  11480. if (!value) {
  11481. if (defined(userOptions[indexName])) { // after Series.update()
  11482. i = userOptions[indexName];
  11483. } else {
  11484. userOptions[indexName] = i = this.chart[counterName] % defaults.length;
  11485. this.chart[counterName] += 1;
  11486. }
  11487. value = defaults[i];
  11488. }
  11489. this[prop] = value;
  11490. },
  11491. /**
  11492. * Get the series' color
  11493. */
  11494. getColor: function () {
  11495. if (this.options.colorByPoint) {
  11496. this.options.color = null; // #4359, selected slice got series.color even when colorByPoint was set.
  11497. } else {
  11498. this.getCyclic('color', this.options.color || defaultPlotOptions[this.type].color, this.chart.options.colors);
  11499. }
  11500. },
  11501. /**
  11502. * Get the series' symbol
  11503. */
  11504. getSymbol: function () {
  11505. var seriesMarkerOption = this.options.marker;
  11506. this.getCyclic('symbol', seriesMarkerOption.symbol, this.chart.options.symbols);
  11507. // don't substract radius in image symbols (#604)
  11508. if (/^url/.test(this.symbol)) {
  11509. seriesMarkerOption.radius = 0;
  11510. }
  11511. },
  11512. drawLegendSymbol: LegendSymbolMixin.drawLineMarker,
  11513. /**
  11514. * Replace the series data with a new set of data
  11515. * @param {Object} data
  11516. * @param {Object} redraw
  11517. */
  11518. setData: function (data, redraw, animation, updatePoints) {
  11519. var series = this,
  11520. oldData = series.points,
  11521. oldDataLength = (oldData && oldData.length) || 0,
  11522. dataLength,
  11523. options = series.options,
  11524. chart = series.chart,
  11525. firstPoint = null,
  11526. xAxis = series.xAxis,
  11527. hasCategories = xAxis && !!xAxis.categories,
  11528. i,
  11529. turboThreshold = options.turboThreshold,
  11530. pt,
  11531. xData = this.xData,
  11532. yData = this.yData,
  11533. pointArrayMap = series.pointArrayMap,
  11534. valueCount = pointArrayMap && pointArrayMap.length;
  11535. data = data || [];
  11536. dataLength = data.length;
  11537. redraw = pick(redraw, true);
  11538. // If the point count is the same as is was, just run Point.update which is
  11539. // cheaper, allows animation, and keeps references to points.
  11540. if (updatePoints !== false && dataLength && oldDataLength === dataLength && !series.cropped && !series.hasGroupedData && series.visible) {
  11541. each(data, function (point, i) {
  11542. if (oldData[i].update) { // Linked, previously hidden series (#3709)
  11543. oldData[i].update(point, false, null, false);
  11544. }
  11545. });
  11546. } else {
  11547. // Reset properties
  11548. series.xIncrement = null;
  11549. series.pointRange = hasCategories ? 1 : options.pointRange;
  11550. series.colorCounter = 0; // for series with colorByPoint (#1547)
  11551. // Update parallel arrays
  11552. each(this.parallelArrays, function (key) {
  11553. series[key + 'Data'].length = 0;
  11554. });
  11555. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  11556. // first value is tested, and we assume that all the rest are defined the same
  11557. // way. Although the 'for' loops are similar, they are repeated inside each
  11558. // if-else conditional for max performance.
  11559. if (turboThreshold && dataLength > turboThreshold) {
  11560. // find the first non-null point
  11561. i = 0;
  11562. while (firstPoint === null && i < dataLength) {
  11563. firstPoint = data[i];
  11564. i++;
  11565. }
  11566. if (isNumber(firstPoint)) { // assume all points are numbers
  11567. var x = pick(options.pointStart, 0),
  11568. pointInterval = pick(options.pointInterval, 1);
  11569. for (i = 0; i < dataLength; i++) {
  11570. xData[i] = x;
  11571. yData[i] = data[i];
  11572. x += pointInterval;
  11573. }
  11574. series.xIncrement = x;
  11575. } else if (isArray(firstPoint)) { // assume all points are arrays
  11576. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  11577. for (i = 0; i < dataLength; i++) {
  11578. pt = data[i];
  11579. xData[i] = pt[0];
  11580. yData[i] = pt.slice(1, valueCount + 1);
  11581. }
  11582. } else { // [x, y]
  11583. for (i = 0; i < dataLength; i++) {
  11584. pt = data[i];
  11585. xData[i] = pt[0];
  11586. yData[i] = pt[1];
  11587. }
  11588. }
  11589. } else {
  11590. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  11591. }
  11592. } else {
  11593. for (i = 0; i < dataLength; i++) {
  11594. if (data[i] !== UNDEFINED) { // stray commas in oldIE
  11595. pt = { series: series };
  11596. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  11597. series.updateParallelArrays(pt, i);
  11598. if (hasCategories && defined(pt.name)) { // #4401
  11599. xAxis.names[pt.x] = pt.name; // #2046
  11600. }
  11601. }
  11602. }
  11603. }
  11604. // Forgetting to cast strings to numbers is a common caveat when handling CSV or JSON
  11605. if (isString(yData[0])) {
  11606. error(14, true);
  11607. }
  11608. series.data = [];
  11609. series.options.data = data;
  11610. //series.zData = zData;
  11611. // destroy old points
  11612. i = oldDataLength;
  11613. while (i--) {
  11614. if (oldData[i] && oldData[i].destroy) {
  11615. oldData[i].destroy();
  11616. }
  11617. }
  11618. // reset minRange (#878)
  11619. if (xAxis) {
  11620. xAxis.minRange = xAxis.userMinRange;
  11621. }
  11622. // redraw
  11623. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  11624. animation = false;
  11625. }
  11626. if (redraw) {
  11627. chart.redraw(animation);
  11628. }
  11629. },
  11630. /**
  11631. * Process the data by cropping away unused data points if the series is longer
  11632. * than the crop threshold. This saves computing time for lage series.
  11633. */
  11634. processData: function (force) {
  11635. var series = this,
  11636. processedXData = series.xData, // copied during slice operation below
  11637. processedYData = series.yData,
  11638. dataLength = processedXData.length,
  11639. croppedData,
  11640. cropStart = 0,
  11641. cropped,
  11642. distance,
  11643. closestPointRange,
  11644. xAxis = series.xAxis,
  11645. i, // loop variable
  11646. options = series.options,
  11647. cropThreshold = options.cropThreshold,
  11648. isCartesian = series.isCartesian,
  11649. xExtremes,
  11650. min,
  11651. max;
  11652. // If the series data or axes haven't changed, don't go through this. Return false to pass
  11653. // the message on to override methods like in data grouping.
  11654. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  11655. return false;
  11656. }
  11657. if (xAxis) {
  11658. xExtremes = xAxis.getExtremes(); // corrected for log axis (#3053)
  11659. min = xExtremes.min;
  11660. max = xExtremes.max;
  11661. }
  11662. // optionally filter out points outside the plot area
  11663. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  11664. // it's outside current extremes
  11665. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  11666. processedXData = [];
  11667. processedYData = [];
  11668. // only crop if it's actually spilling out
  11669. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  11670. croppedData = this.cropData(series.xData, series.yData, min, max);
  11671. processedXData = croppedData.xData;
  11672. processedYData = croppedData.yData;
  11673. cropStart = croppedData.start;
  11674. cropped = true;
  11675. }
  11676. }
  11677. // Find the closest distance between processed points
  11678. for (i = processedXData.length - 1; i >= 0; i--) {
  11679. distance = processedXData[i] - processedXData[i - 1];
  11680. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  11681. closestPointRange = distance;
  11682. // Unsorted data is not supported by the line tooltip, as well as data grouping and
  11683. // navigation in Stock charts (#725) and width calculation of columns (#1900)
  11684. } else if (distance < 0 && series.requireSorting) {
  11685. error(15);
  11686. }
  11687. }
  11688. // Record the properties
  11689. series.cropped = cropped; // undefined or true
  11690. series.cropStart = cropStart;
  11691. series.processedXData = processedXData;
  11692. series.processedYData = processedYData;
  11693. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  11694. series.pointRange = closestPointRange || 1;
  11695. }
  11696. series.closestPointRange = closestPointRange;
  11697. },
  11698. /**
  11699. * Iterate over xData and crop values between min and max. Returns object containing crop start/end
  11700. * cropped xData with corresponding part of yData, dataMin and dataMax within the cropped range
  11701. */
  11702. cropData: function (xData, yData, min, max) {
  11703. var dataLength = xData.length,
  11704. cropStart = 0,
  11705. cropEnd = dataLength,
  11706. cropShoulder = pick(this.cropShoulder, 1), // line-type series need one point outside
  11707. i;
  11708. // iterate up to find slice start
  11709. for (i = 0; i < dataLength; i++) {
  11710. if (xData[i] >= min) {
  11711. cropStart = mathMax(0, i - cropShoulder);
  11712. break;
  11713. }
  11714. }
  11715. // proceed to find slice end
  11716. for (; i < dataLength; i++) {
  11717. if (xData[i] > max) {
  11718. cropEnd = i + cropShoulder;
  11719. break;
  11720. }
  11721. }
  11722. return {
  11723. xData: xData.slice(cropStart, cropEnd),
  11724. yData: yData.slice(cropStart, cropEnd),
  11725. start: cropStart,
  11726. end: cropEnd
  11727. };
  11728. },
  11729. /**
  11730. * Generate the data point after the data has been processed by cropping away
  11731. * unused points and optionally grouped in Highcharts Stock.
  11732. */
  11733. generatePoints: function () {
  11734. var series = this,
  11735. options = series.options,
  11736. dataOptions = options.data,
  11737. data = series.data,
  11738. dataLength,
  11739. processedXData = series.processedXData,
  11740. processedYData = series.processedYData,
  11741. pointClass = series.pointClass,
  11742. processedDataLength = processedXData.length,
  11743. cropStart = series.cropStart || 0,
  11744. cursor,
  11745. hasGroupedData = series.hasGroupedData,
  11746. point,
  11747. points = [],
  11748. i;
  11749. if (!data && !hasGroupedData) {
  11750. var arr = [];
  11751. arr.length = dataOptions.length;
  11752. data = series.data = arr;
  11753. }
  11754. for (i = 0; i < processedDataLength; i++) {
  11755. cursor = cropStart + i;
  11756. if (!hasGroupedData) {
  11757. if (data[cursor]) {
  11758. point = data[cursor];
  11759. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  11760. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  11761. }
  11762. points[i] = point;
  11763. } else {
  11764. // splat the y data in case of ohlc data array
  11765. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  11766. }
  11767. points[i].index = cursor; // For faster access in Point.update
  11768. }
  11769. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  11770. // swithching view from non-grouped data to grouped data (#637)
  11771. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  11772. for (i = 0; i < dataLength; i++) {
  11773. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  11774. i += processedDataLength;
  11775. }
  11776. if (data[i]) {
  11777. data[i].destroyElements();
  11778. data[i].plotX = UNDEFINED; // #1003
  11779. }
  11780. }
  11781. }
  11782. series.data = data;
  11783. series.points = points;
  11784. },
  11785. /**
  11786. * Calculate Y extremes for visible data
  11787. */
  11788. getExtremes: function (yData) {
  11789. var xAxis = this.xAxis,
  11790. yAxis = this.yAxis,
  11791. xData = this.processedXData,
  11792. yDataLength,
  11793. activeYData = [],
  11794. activeCounter = 0,
  11795. xExtremes = xAxis.getExtremes(), // #2117, need to compensate for log X axis
  11796. xMin = xExtremes.min,
  11797. xMax = xExtremes.max,
  11798. validValue,
  11799. withinRange,
  11800. x,
  11801. y,
  11802. i,
  11803. j;
  11804. yData = yData || this.stackedYData || this.processedYData;
  11805. yDataLength = yData.length;
  11806. for (i = 0; i < yDataLength; i++) {
  11807. x = xData[i];
  11808. y = yData[i];
  11809. // For points within the visible range, including the first point outside the
  11810. // visible range, consider y extremes
  11811. validValue = y !== null && y !== UNDEFINED && (!yAxis.isLog || (y.length || y > 0));
  11812. withinRange = this.getExtremesFromAll || this.options.getExtremesFromAll || this.cropped ||
  11813. ((xData[i + 1] || x) >= xMin && (xData[i - 1] || x) <= xMax);
  11814. if (validValue && withinRange) {
  11815. j = y.length;
  11816. if (j) { // array, like ohlc or range data
  11817. while (j--) {
  11818. if (y[j] !== null) {
  11819. activeYData[activeCounter++] = y[j];
  11820. }
  11821. }
  11822. } else {
  11823. activeYData[activeCounter++] = y;
  11824. }
  11825. }
  11826. }
  11827. this.dataMin = arrayMin(activeYData);
  11828. this.dataMax = arrayMax(activeYData);
  11829. },
  11830. /**
  11831. * Translate data points from raw data values to chart specific positioning data
  11832. * needed later in drawPoints, drawGraph and drawTracker.
  11833. */
  11834. translate: function () {
  11835. if (!this.processedXData) { // hidden series
  11836. this.processData();
  11837. }
  11838. this.generatePoints();
  11839. var series = this,
  11840. options = series.options,
  11841. stacking = options.stacking,
  11842. xAxis = series.xAxis,
  11843. categories = xAxis.categories,
  11844. yAxis = series.yAxis,
  11845. points = series.points,
  11846. dataLength = points.length,
  11847. hasModifyValue = !!series.modifyValue,
  11848. i,
  11849. pointPlacement = options.pointPlacement,
  11850. dynamicallyPlaced = pointPlacement === 'between' || isNumber(pointPlacement),
  11851. threshold = options.threshold,
  11852. stackThreshold = options.startFromThreshold ? threshold : 0,
  11853. plotX,
  11854. plotY,
  11855. lastPlotX,
  11856. closestPointRangePx = Number.MAX_VALUE;
  11857. // Translate each point
  11858. for (i = 0; i < dataLength; i++) {
  11859. var point = points[i],
  11860. xValue = point.x,
  11861. yValue = point.y,
  11862. yBottom = point.low,
  11863. stack = stacking && yAxis.stacks[(series.negStacks && yValue < (stackThreshold ? 0 : threshold) ? '-' : '') + series.stackKey],
  11864. pointStack,
  11865. stackValues;
  11866. // Discard disallowed y values for log axes (#3434)
  11867. if (yAxis.isLog && yValue !== null && yValue <= 0) {
  11868. point.y = yValue = null;
  11869. error(10);
  11870. }
  11871. // Get the plotX translation
  11872. point.plotX = plotX = mathMin(mathMax(-1e5, xAxis.translate(xValue, 0, 0, 0, 1, pointPlacement, this.type === 'flags')), 1e5); // #3923
  11873. // Calculate the bottom y value for stacked series
  11874. if (stacking && series.visible && stack && stack[xValue]) {
  11875. pointStack = stack[xValue];
  11876. stackValues = pointStack.points[series.index + ',' + i];
  11877. yBottom = stackValues[0];
  11878. yValue = stackValues[1];
  11879. if (yBottom === stackThreshold) {
  11880. yBottom = pick(threshold, yAxis.min);
  11881. }
  11882. if (yAxis.isLog && yBottom <= 0) { // #1200, #1232
  11883. yBottom = null;
  11884. }
  11885. point.total = point.stackTotal = pointStack.total;
  11886. point.percentage = pointStack.total && (point.y / pointStack.total * 100);
  11887. point.stackY = yValue;
  11888. // Place the stack label
  11889. pointStack.setOffset(series.pointXOffset || 0, series.barW || 0);
  11890. }
  11891. // Set translated yBottom or remove it
  11892. point.yBottom = defined(yBottom) ?
  11893. yAxis.translate(yBottom, 0, 1, 0, 1) :
  11894. null;
  11895. // general hook, used for Highstock compare mode
  11896. if (hasModifyValue) {
  11897. yValue = series.modifyValue(yValue, point);
  11898. }
  11899. // Set the the plotY value, reset it for redraws
  11900. point.plotY = plotY = (typeof yValue === 'number' && yValue !== Infinity) ?
  11901. mathMin(mathMax(-1e5, yAxis.translate(yValue, 0, 1, 0, 1)), 1e5) : // #3201
  11902. UNDEFINED;
  11903. point.isInside = plotY !== UNDEFINED && plotY >= 0 && plotY <= yAxis.len && // #3519
  11904. plotX >= 0 && plotX <= xAxis.len;
  11905. // Set client related positions for mouse tracking
  11906. point.clientX = dynamicallyPlaced ? xAxis.translate(xValue, 0, 0, 0, 1) : plotX; // #1514
  11907. point.negative = point.y < (threshold || 0);
  11908. // some API data
  11909. point.category = categories && categories[point.x] !== UNDEFINED ?
  11910. categories[point.x] : point.x;
  11911. // Determine auto enabling of markers (#3635)
  11912. if (i) {
  11913. closestPointRangePx = mathMin(closestPointRangePx, mathAbs(plotX - lastPlotX));
  11914. }
  11915. lastPlotX = plotX;
  11916. }
  11917. series.closestPointRangePx = closestPointRangePx;
  11918. // now that we have the cropped data, build the segments
  11919. series.getSegments();
  11920. },
  11921. /**
  11922. * Set the clipping for the series. For animated series it is called twice, first to initiate
  11923. * animating the clip then the second time without the animation to set the final clip.
  11924. */
  11925. setClip: function (animation) {
  11926. var chart = this.chart,
  11927. renderer = chart.renderer,
  11928. inverted = chart.inverted,
  11929. seriesClipBox = this.clipBox,
  11930. clipBox = seriesClipBox || chart.clipBox,
  11931. sharedClipKey = this.sharedClipKey || ['_sharedClip', animation && animation.duration, animation && animation.easing, clipBox.height].join(','),
  11932. clipRect = chart[sharedClipKey],
  11933. markerClipRect = chart[sharedClipKey + 'm'];
  11934. // If a clipping rectangle with the same properties is currently present in the chart, use that.
  11935. if (!clipRect) {
  11936. // When animation is set, prepare the initial positions
  11937. if (animation) {
  11938. clipBox.width = 0;
  11939. chart[sharedClipKey + 'm'] = markerClipRect = renderer.clipRect(
  11940. -99, // include the width of the first marker
  11941. inverted ? -chart.plotLeft : -chart.plotTop,
  11942. 99,
  11943. inverted ? chart.chartWidth : chart.chartHeight
  11944. );
  11945. }
  11946. chart[sharedClipKey] = clipRect = renderer.clipRect(clipBox);
  11947. }
  11948. if (animation) {
  11949. clipRect.count += 1;
  11950. }
  11951. if (this.options.clip !== false) {
  11952. this.group.clip(animation || seriesClipBox ? clipRect : chart.clipRect);
  11953. this.markerGroup.clip(markerClipRect);
  11954. this.sharedClipKey = sharedClipKey;
  11955. }
  11956. // Remove the shared clipping rectangle when all series are shown
  11957. if (!animation) {
  11958. clipRect.count -= 1;
  11959. if (clipRect.count <= 0 && sharedClipKey && chart[sharedClipKey]) {
  11960. if (!seriesClipBox) {
  11961. chart[sharedClipKey] = chart[sharedClipKey].destroy();
  11962. }
  11963. if (chart[sharedClipKey + 'm']) {
  11964. chart[sharedClipKey + 'm'] = chart[sharedClipKey + 'm'].destroy();
  11965. }
  11966. }
  11967. }
  11968. },
  11969. /**
  11970. * Animate in the series
  11971. */
  11972. animate: function (init) {
  11973. var series = this,
  11974. chart = series.chart,
  11975. clipRect,
  11976. animation = series.options.animation,
  11977. sharedClipKey;
  11978. // Animation option is set to true
  11979. if (animation && !isObject(animation)) {
  11980. animation = defaultPlotOptions[series.type].animation;
  11981. }
  11982. // Initialize the animation. Set up the clipping rectangle.
  11983. if (init) {
  11984. series.setClip(animation);
  11985. // Run the animation
  11986. } else {
  11987. sharedClipKey = this.sharedClipKey;
  11988. clipRect = chart[sharedClipKey];
  11989. if (clipRect) {
  11990. clipRect.animate({
  11991. width: chart.plotSizeX
  11992. }, animation);
  11993. }
  11994. if (chart[sharedClipKey + 'm']) {
  11995. chart[sharedClipKey + 'm'].animate({
  11996. width: chart.plotSizeX + 99
  11997. }, animation);
  11998. }
  11999. // Delete this function to allow it only once
  12000. series.animate = null;
  12001. }
  12002. },
  12003. /**
  12004. * This runs after animation to land on the final plot clipping
  12005. */
  12006. afterAnimate: function () {
  12007. this.setClip();
  12008. fireEvent(this, 'afterAnimate');
  12009. },
  12010. /**
  12011. * Draw the markers
  12012. */
  12013. drawPoints: function () {
  12014. var series = this,
  12015. pointAttr,
  12016. points = series.points,
  12017. chart = series.chart,
  12018. plotX,
  12019. plotY,
  12020. i,
  12021. point,
  12022. radius,
  12023. symbol,
  12024. isImage,
  12025. graphic,
  12026. options = series.options,
  12027. seriesMarkerOptions = options.marker,
  12028. seriesPointAttr = series.pointAttr[''],
  12029. pointMarkerOptions,
  12030. hasPointMarker,
  12031. enabled,
  12032. isInside,
  12033. markerGroup = series.markerGroup,
  12034. xAxis = series.xAxis,
  12035. globallyEnabled = pick(
  12036. seriesMarkerOptions.enabled,
  12037. xAxis.isRadial,
  12038. series.closestPointRangePx > 2 * seriesMarkerOptions.radius
  12039. );
  12040. if (seriesMarkerOptions.enabled !== false || series._hasPointMarkers) {
  12041. i = points.length;
  12042. while (i--) {
  12043. point = points[i];
  12044. plotX = mathFloor(point.plotX); // #1843
  12045. plotY = point.plotY;
  12046. graphic = point.graphic;
  12047. pointMarkerOptions = point.marker || {};
  12048. hasPointMarker = !!point.marker;
  12049. enabled = (globallyEnabled && pointMarkerOptions.enabled === UNDEFINED) || pointMarkerOptions.enabled;
  12050. isInside = point.isInside;
  12051. // only draw the point if y is defined
  12052. if (enabled && plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  12053. // shortcuts
  12054. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || seriesPointAttr;
  12055. radius = pointAttr.r;
  12056. symbol = pick(pointMarkerOptions.symbol, series.symbol);
  12057. isImage = symbol.indexOf('url') === 0;
  12058. if (graphic) { // update
  12059. graphic[isInside ? 'show' : 'hide'](true) // Since the marker group isn't clipped, each individual marker must be toggled
  12060. .animate(extend({
  12061. x: plotX - radius,
  12062. y: plotY - radius
  12063. }, graphic.symbolName ? { // don't apply to image symbols #507
  12064. width: 2 * radius,
  12065. height: 2 * radius
  12066. } : {}));
  12067. } else if (isInside && (radius > 0 || isImage)) {
  12068. point.graphic = graphic = chart.renderer.symbol(
  12069. symbol,
  12070. plotX - radius,
  12071. plotY - radius,
  12072. 2 * radius,
  12073. 2 * radius,
  12074. hasPointMarker ? pointMarkerOptions : seriesMarkerOptions
  12075. )
  12076. .attr(pointAttr)
  12077. .add(markerGroup);
  12078. }
  12079. } else if (graphic) {
  12080. point.graphic = graphic.destroy(); // #1269
  12081. }
  12082. }
  12083. }
  12084. },
  12085. /**
  12086. * Convert state properties from API naming conventions to SVG attributes
  12087. *
  12088. * @param {Object} options API options object
  12089. * @param {Object} base1 SVG attribute object to inherit from
  12090. * @param {Object} base2 Second level SVG attribute object to inherit from
  12091. */
  12092. convertAttribs: function (options, base1, base2, base3) {
  12093. var conversion = this.pointAttrToOptions,
  12094. attr,
  12095. option,
  12096. obj = {};
  12097. options = options || {};
  12098. base1 = base1 || {};
  12099. base2 = base2 || {};
  12100. base3 = base3 || {};
  12101. for (attr in conversion) {
  12102. option = conversion[attr];
  12103. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  12104. }
  12105. return obj;
  12106. },
  12107. /**
  12108. * Get the state attributes. Each series type has its own set of attributes
  12109. * that are allowed to change on a point's state change. Series wide attributes are stored for
  12110. * all series, and additionally point specific attributes are stored for all
  12111. * points with individual marker options. If such options are not defined for the point,
  12112. * a reference to the series wide attributes is stored in point.pointAttr.
  12113. */
  12114. getAttribs: function () {
  12115. var series = this,
  12116. seriesOptions = series.options,
  12117. normalOptions = defaultPlotOptions[series.type].marker ? seriesOptions.marker : seriesOptions,
  12118. stateOptions = normalOptions.states,
  12119. stateOptionsHover = stateOptions[HOVER_STATE],
  12120. pointStateOptionsHover,
  12121. seriesColor = series.color,
  12122. seriesNegativeColor = series.options.negativeColor,
  12123. normalDefaults = {
  12124. stroke: seriesColor,
  12125. fill: seriesColor
  12126. },
  12127. points = series.points || [], // #927
  12128. i,
  12129. j,
  12130. threshold,
  12131. point,
  12132. seriesPointAttr = [],
  12133. pointAttr,
  12134. pointAttrToOptions = series.pointAttrToOptions,
  12135. hasPointSpecificOptions = series.hasPointSpecificOptions,
  12136. defaultLineColor = normalOptions.lineColor,
  12137. defaultFillColor = normalOptions.fillColor,
  12138. turboThreshold = seriesOptions.turboThreshold,
  12139. zones = series.zones,
  12140. zoneAxis = series.zoneAxis || 'y',
  12141. attr,
  12142. key;
  12143. // series type specific modifications
  12144. if (seriesOptions.marker) { // line, spline, area, areaspline, scatter
  12145. // if no hover radius is given, default to normal radius + 2
  12146. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + stateOptionsHover.radiusPlus;
  12147. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + stateOptionsHover.lineWidthPlus;
  12148. } else { // column, bar, pie
  12149. // if no hover color is given, brighten the normal color
  12150. stateOptionsHover.color = stateOptionsHover.color ||
  12151. Color(stateOptionsHover.color || seriesColor)
  12152. .brighten(stateOptionsHover.brightness).get();
  12153. // if no hover negativeColor is given, brighten the normal negativeColor
  12154. stateOptionsHover.negativeColor = stateOptionsHover.negativeColor ||
  12155. Color(stateOptionsHover.negativeColor || seriesNegativeColor)
  12156. .brighten(stateOptionsHover.brightness).get();
  12157. }
  12158. // general point attributes for the series normal state
  12159. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  12160. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  12161. each([HOVER_STATE, SELECT_STATE], function (state) {
  12162. seriesPointAttr[state] =
  12163. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  12164. });
  12165. // set it
  12166. series.pointAttr = seriesPointAttr;
  12167. // Generate the point-specific attribute collections if specific point
  12168. // options are given. If not, create a referance to the series wide point
  12169. // attributes
  12170. i = points.length;
  12171. if (!turboThreshold || i < turboThreshold || hasPointSpecificOptions) {
  12172. while (i--) {
  12173. point = points[i];
  12174. normalOptions = (point.options && point.options.marker) || point.options;
  12175. if (normalOptions && normalOptions.enabled === false) {
  12176. normalOptions.radius = 0;
  12177. }
  12178. if (zones.length) {
  12179. j = 0;
  12180. threshold = zones[j];
  12181. while (point[zoneAxis] >= threshold.value) {
  12182. threshold = zones[++j];
  12183. }
  12184. if (threshold.color) {
  12185. point.color = point.fillColor = threshold.color;
  12186. }
  12187. }
  12188. hasPointSpecificOptions = seriesOptions.colorByPoint || point.color; // #868
  12189. // check if the point has specific visual options
  12190. if (point.options) {
  12191. for (key in pointAttrToOptions) {
  12192. if (defined(normalOptions[pointAttrToOptions[key]])) {
  12193. hasPointSpecificOptions = true;
  12194. }
  12195. }
  12196. }
  12197. // a specific marker config object is defined for the individual point:
  12198. // create it's own attribute collection
  12199. if (hasPointSpecificOptions) {
  12200. normalOptions = normalOptions || {};
  12201. pointAttr = [];
  12202. stateOptions = normalOptions.states || {}; // reassign for individual point
  12203. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  12204. // Handle colors for column and pies
  12205. if (!seriesOptions.marker) { // column, bar, point
  12206. // If no hover color is given, brighten the normal color. #1619, #2579
  12207. pointStateOptionsHover.color = pointStateOptionsHover.color || (!point.options.color && stateOptionsHover[(point.negative && seriesNegativeColor ? 'negativeColor' : 'color')]) ||
  12208. Color(point.color)
  12209. .brighten(pointStateOptionsHover.brightness || stateOptionsHover.brightness)
  12210. .get();
  12211. }
  12212. // normal point state inherits series wide normal state
  12213. attr = { color: point.color }; // #868
  12214. if (!defaultFillColor) { // Individual point color or negative color markers (#2219)
  12215. attr.fillColor = point.color;
  12216. }
  12217. if (!defaultLineColor) {
  12218. attr.lineColor = point.color; // Bubbles take point color, line markers use white
  12219. }
  12220. // Color is explicitly set to null or undefined (#1288, #4068)
  12221. if (normalOptions.hasOwnProperty('color') && !normalOptions.color) {
  12222. delete normalOptions.color;
  12223. }
  12224. pointAttr[NORMAL_STATE] = series.convertAttribs(extend(attr, normalOptions), seriesPointAttr[NORMAL_STATE]);
  12225. // inherit from point normal and series hover
  12226. pointAttr[HOVER_STATE] = series.convertAttribs(
  12227. stateOptions[HOVER_STATE],
  12228. seriesPointAttr[HOVER_STATE],
  12229. pointAttr[NORMAL_STATE]
  12230. );
  12231. // inherit from point normal and series hover
  12232. pointAttr[SELECT_STATE] = series.convertAttribs(
  12233. stateOptions[SELECT_STATE],
  12234. seriesPointAttr[SELECT_STATE],
  12235. pointAttr[NORMAL_STATE]
  12236. );
  12237. // no marker config object is created: copy a reference to the series-wide
  12238. // attribute collection
  12239. } else {
  12240. pointAttr = seriesPointAttr;
  12241. }
  12242. point.pointAttr = pointAttr;
  12243. }
  12244. }
  12245. },
  12246. /**
  12247. * Clear DOM objects and free up memory
  12248. */
  12249. destroy: function () {
  12250. var series = this,
  12251. chart = series.chart,
  12252. issue134 = /AppleWebKit\/533/.test(userAgent),
  12253. destroy,
  12254. i,
  12255. data = series.data || [],
  12256. point,
  12257. prop,
  12258. axis;
  12259. // add event hook
  12260. fireEvent(series, 'destroy');
  12261. // remove all events
  12262. removeEvent(series);
  12263. // erase from axes
  12264. each(series.axisTypes || [], function (AXIS) {
  12265. axis = series[AXIS];
  12266. if (axis) {
  12267. erase(axis.series, series);
  12268. axis.isDirty = axis.forceRedraw = true;
  12269. }
  12270. });
  12271. // remove legend items
  12272. if (series.legendItem) {
  12273. series.chart.legend.destroyItem(series);
  12274. }
  12275. // destroy all points with their elements
  12276. i = data.length;
  12277. while (i--) {
  12278. point = data[i];
  12279. if (point && point.destroy) {
  12280. point.destroy();
  12281. }
  12282. }
  12283. series.points = null;
  12284. // Clear the animation timeout if we are destroying the series during initial animation
  12285. clearTimeout(series.animationTimeout);
  12286. // Destroy all SVGElements associated to the series
  12287. for (prop in series) {
  12288. if (series[prop] instanceof SVGElement && !series[prop].survive) { // Survive provides a hook for not destroying
  12289. // issue 134 workaround
  12290. destroy = issue134 && prop === 'group' ?
  12291. 'hide' :
  12292. 'destroy';
  12293. series[prop][destroy]();
  12294. }
  12295. }
  12296. // remove from hoverSeries
  12297. if (chart.hoverSeries === series) {
  12298. chart.hoverSeries = null;
  12299. }
  12300. erase(chart.series, series);
  12301. // clear all members
  12302. for (prop in series) {
  12303. delete series[prop];
  12304. }
  12305. },
  12306. /**
  12307. * Return the graph path of a segment
  12308. */
  12309. getSegmentPath: function (segment) {
  12310. var series = this,
  12311. segmentPath = [],
  12312. step = series.options.step;
  12313. // build the segment line
  12314. each(segment, function (point, i) {
  12315. var plotX = point.plotX,
  12316. plotY = point.plotY,
  12317. lastPoint;
  12318. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  12319. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  12320. } else {
  12321. // moveTo or lineTo
  12322. segmentPath.push(i ? L : M);
  12323. // step line?
  12324. if (step && i) {
  12325. lastPoint = segment[i - 1];
  12326. if (step === 'right') {
  12327. segmentPath.push(
  12328. lastPoint.plotX,
  12329. plotY
  12330. );
  12331. } else if (step === 'center') {
  12332. segmentPath.push(
  12333. (lastPoint.plotX + plotX) / 2,
  12334. lastPoint.plotY,
  12335. (lastPoint.plotX + plotX) / 2,
  12336. plotY
  12337. );
  12338. } else {
  12339. segmentPath.push(
  12340. plotX,
  12341. lastPoint.plotY
  12342. );
  12343. }
  12344. }
  12345. // normal line to next point
  12346. segmentPath.push(
  12347. point.plotX,
  12348. point.plotY
  12349. );
  12350. }
  12351. });
  12352. return segmentPath;
  12353. },
  12354. /**
  12355. * Get the graph path
  12356. */
  12357. getGraphPath: function () {
  12358. var series = this,
  12359. graphPath = [],
  12360. segmentPath,
  12361. singlePoints = []; // used in drawTracker
  12362. // Divide into segments and build graph and area paths
  12363. each(series.segments, function (segment) {
  12364. segmentPath = series.getSegmentPath(segment);
  12365. // add the segment to the graph, or a single point for tracking
  12366. if (segment.length > 1) {
  12367. graphPath = graphPath.concat(segmentPath);
  12368. } else {
  12369. singlePoints.push(segment[0]);
  12370. }
  12371. });
  12372. // Record it for use in drawGraph and drawTracker, and return graphPath
  12373. series.singlePoints = singlePoints;
  12374. series.graphPath = graphPath;
  12375. return graphPath;
  12376. },
  12377. /**
  12378. * Draw the actual graph
  12379. */
  12380. drawGraph: function () {
  12381. var series = this,
  12382. options = this.options,
  12383. props = [['graph', options.lineColor || this.color, options.dashStyle]],
  12384. lineWidth = options.lineWidth,
  12385. roundCap = options.linecap !== 'square',
  12386. graphPath = this.getGraphPath(),
  12387. fillColor = (this.fillGraph && this.color) || NONE, // polygon series use filled graph
  12388. zones = this.zones;
  12389. each(zones, function (threshold, i) {
  12390. props.push(['zoneGraph' + i, threshold.color || series.color, threshold.dashStyle || options.dashStyle]);
  12391. });
  12392. // Draw the graph
  12393. each(props, function (prop, i) {
  12394. var graphKey = prop[0],
  12395. graph = series[graphKey],
  12396. attribs;
  12397. if (graph) {
  12398. graph.animate({ d: graphPath });
  12399. } else if ((lineWidth || fillColor) && graphPath.length) { // #1487
  12400. attribs = {
  12401. stroke: prop[1],
  12402. 'stroke-width': lineWidth,
  12403. fill: fillColor,
  12404. zIndex: 1 // #1069
  12405. };
  12406. if (prop[2]) {
  12407. attribs.dashstyle = prop[2];
  12408. } else if (roundCap) {
  12409. attribs['stroke-linecap'] = attribs['stroke-linejoin'] = 'round';
  12410. }
  12411. series[graphKey] = series.chart.renderer.path(graphPath)
  12412. .attr(attribs)
  12413. .add(series.group)
  12414. .shadow((i < 2) && options.shadow); // add shadow to normal series (0) or to first zone (1) #3932
  12415. }
  12416. });
  12417. },
  12418. /**
  12419. * Clip the graphs into the positive and negative coloured graphs
  12420. */
  12421. applyZones: function () {
  12422. var series = this,
  12423. chart = this.chart,
  12424. renderer = chart.renderer,
  12425. zones = this.zones,
  12426. translatedFrom,
  12427. translatedTo,
  12428. clips = this.clips || [],
  12429. clipAttr,
  12430. graph = this.graph,
  12431. area = this.area,
  12432. chartSizeMax = mathMax(chart.chartWidth, chart.chartHeight),
  12433. axis = this[(this.zoneAxis || 'y') + 'Axis'],
  12434. extremes,
  12435. reversed = axis.reversed,
  12436. inverted = chart.inverted,
  12437. horiz = axis.horiz,
  12438. pxRange,
  12439. pxPosMin,
  12440. pxPosMax,
  12441. ignoreZones = false;
  12442. if (zones.length && (graph || area) && axis.min !== UNDEFINED) {
  12443. // The use of the Color Threshold assumes there are no gaps
  12444. // so it is safe to hide the original graph and area
  12445. if (graph) {
  12446. graph.hide();
  12447. }
  12448. if (area) {
  12449. area.hide();
  12450. }
  12451. // Create the clips
  12452. extremes = axis.getExtremes();
  12453. each(zones, function (threshold, i) {
  12454. translatedFrom = reversed ?
  12455. (horiz ? chart.plotWidth : 0) :
  12456. (horiz ? 0 : axis.toPixels(extremes.min));
  12457. translatedFrom = mathMin(mathMax(pick(translatedTo, translatedFrom), 0), chartSizeMax);
  12458. translatedTo = mathMin(mathMax(mathRound(axis.toPixels(pick(threshold.value, extremes.max), true)), 0), chartSizeMax);
  12459. if (ignoreZones) {
  12460. translatedFrom = translatedTo = axis.toPixels(extremes.max);
  12461. }
  12462. pxRange = Math.abs(translatedFrom - translatedTo);
  12463. pxPosMin = mathMin(translatedFrom, translatedTo);
  12464. pxPosMax = mathMax(translatedFrom, translatedTo);
  12465. if (axis.isXAxis) {
  12466. clipAttr = {
  12467. x: inverted ? pxPosMax : pxPosMin,
  12468. y: 0,
  12469. width: pxRange,
  12470. height: chartSizeMax
  12471. };
  12472. if (!horiz) {
  12473. clipAttr.x = chart.plotHeight - clipAttr.x;
  12474. }
  12475. } else {
  12476. clipAttr = {
  12477. x: 0,
  12478. y: inverted ? pxPosMax : pxPosMin,
  12479. width: chartSizeMax,
  12480. height: pxRange
  12481. };
  12482. if (horiz) {
  12483. clipAttr.y = chart.plotWidth - clipAttr.y;
  12484. }
  12485. }
  12486. /// VML SUPPPORT
  12487. if (chart.inverted && renderer.isVML) {
  12488. if (axis.isXAxis) {
  12489. clipAttr = {
  12490. x: 0,
  12491. y: reversed ? pxPosMin : pxPosMax,
  12492. height: clipAttr.width,
  12493. width: chart.chartWidth
  12494. };
  12495. } else {
  12496. clipAttr = {
  12497. x: clipAttr.y - chart.plotLeft - chart.spacingBox.x,
  12498. y: 0,
  12499. width: clipAttr.height,
  12500. height: chart.chartHeight
  12501. };
  12502. }
  12503. }
  12504. /// END OF VML SUPPORT
  12505. if (clips[i]) {
  12506. clips[i].animate(clipAttr);
  12507. } else {
  12508. clips[i] = renderer.clipRect(clipAttr);
  12509. if (graph) {
  12510. series['zoneGraph' + i].clip(clips[i]);
  12511. }
  12512. if (area) {
  12513. series['zoneArea' + i].clip(clips[i]);
  12514. }
  12515. }
  12516. // if this zone extends out of the axis, ignore the others
  12517. ignoreZones = threshold.value > extremes.max;
  12518. });
  12519. this.clips = clips;
  12520. }
  12521. },
  12522. /**
  12523. * Initialize and perform group inversion on series.group and series.markerGroup
  12524. */
  12525. invertGroups: function () {
  12526. var series = this,
  12527. chart = series.chart;
  12528. // Pie, go away (#1736)
  12529. if (!series.xAxis) {
  12530. return;
  12531. }
  12532. // A fixed size is needed for inversion to work
  12533. function setInvert() {
  12534. var size = {
  12535. width: series.yAxis.len,
  12536. height: series.xAxis.len
  12537. };
  12538. each(['group', 'markerGroup'], function (groupName) {
  12539. if (series[groupName]) {
  12540. series[groupName].attr(size).invert();
  12541. }
  12542. });
  12543. }
  12544. addEvent(chart, 'resize', setInvert); // do it on resize
  12545. addEvent(series, 'destroy', function () {
  12546. removeEvent(chart, 'resize', setInvert);
  12547. });
  12548. // Do it now
  12549. setInvert(); // do it now
  12550. // On subsequent render and redraw, just do setInvert without setting up events again
  12551. series.invertGroups = setInvert;
  12552. },
  12553. /**
  12554. * General abstraction for creating plot groups like series.group, series.dataLabelsGroup and
  12555. * series.markerGroup. On subsequent calls, the group will only be adjusted to the updated plot size.
  12556. */
  12557. plotGroup: function (prop, name, visibility, zIndex, parent) {
  12558. var group = this[prop],
  12559. isNew = !group;
  12560. // Generate it on first call
  12561. if (isNew) {
  12562. this[prop] = group = this.chart.renderer.g(name)
  12563. .attr({
  12564. visibility: visibility,
  12565. zIndex: zIndex || 0.1 // IE8 needs this
  12566. })
  12567. .add(parent);
  12568. }
  12569. // Place it on first and subsequent (redraw) calls
  12570. group[isNew ? 'attr' : 'animate'](this.getPlotBox());
  12571. return group;
  12572. },
  12573. /**
  12574. * Get the translation and scale for the plot area of this series
  12575. */
  12576. getPlotBox: function () {
  12577. var chart = this.chart,
  12578. xAxis = this.xAxis,
  12579. yAxis = this.yAxis;
  12580. // Swap axes for inverted (#2339)
  12581. if (chart.inverted) {
  12582. xAxis = yAxis;
  12583. yAxis = this.xAxis;
  12584. }
  12585. return {
  12586. translateX: xAxis ? xAxis.left : chart.plotLeft,
  12587. translateY: yAxis ? yAxis.top : chart.plotTop,
  12588. scaleX: 1, // #1623
  12589. scaleY: 1
  12590. };
  12591. },
  12592. /**
  12593. * Render the graph and markers
  12594. */
  12595. render: function () {
  12596. var series = this,
  12597. chart = series.chart,
  12598. group,
  12599. options = series.options,
  12600. animation = options.animation,
  12601. // Animation doesn't work in IE8 quirks when the group div is hidden,
  12602. // and looks bad in other oldIE
  12603. animDuration = (animation && !!series.animate && chart.renderer.isSVG && pick(animation.duration, 500)) || 0,
  12604. visibility = series.visible ? VISIBLE : HIDDEN,
  12605. zIndex = options.zIndex,
  12606. hasRendered = series.hasRendered,
  12607. chartSeriesGroup = chart.seriesGroup;
  12608. // the group
  12609. group = series.plotGroup(
  12610. 'group',
  12611. 'series',
  12612. visibility,
  12613. zIndex,
  12614. chartSeriesGroup
  12615. );
  12616. series.markerGroup = series.plotGroup(
  12617. 'markerGroup',
  12618. 'markers',
  12619. visibility,
  12620. zIndex,
  12621. chartSeriesGroup
  12622. );
  12623. // initiate the animation
  12624. if (animDuration) {
  12625. series.animate(true);
  12626. }
  12627. // cache attributes for shapes
  12628. series.getAttribs();
  12629. // SVGRenderer needs to know this before drawing elements (#1089, #1795)
  12630. group.inverted = series.isCartesian ? chart.inverted : false;
  12631. // draw the graph if any
  12632. if (series.drawGraph) {
  12633. series.drawGraph();
  12634. series.applyZones();
  12635. }
  12636. each(series.points, function (point) {
  12637. if (point.redraw) {
  12638. point.redraw();
  12639. }
  12640. });
  12641. // draw the data labels (inn pies they go before the points)
  12642. if (series.drawDataLabels) {
  12643. series.drawDataLabels();
  12644. }
  12645. // draw the points
  12646. if (series.visible) {
  12647. series.drawPoints();
  12648. }
  12649. // draw the mouse tracking area
  12650. if (series.drawTracker && series.options.enableMouseTracking !== false) {
  12651. series.drawTracker();
  12652. }
  12653. // Handle inverted series and tracker groups
  12654. if (chart.inverted) {
  12655. series.invertGroups();
  12656. }
  12657. // Initial clipping, must be defined after inverting groups for VML. Applies to columns etc. (#3839).
  12658. if (options.clip !== false && !series.sharedClipKey && !hasRendered) {
  12659. group.clip(chart.clipRect);
  12660. }
  12661. // Run the animation
  12662. if (animDuration) {
  12663. series.animate();
  12664. }
  12665. // Call the afterAnimate function on animation complete (but don't overwrite the animation.complete option
  12666. // which should be available to the user).
  12667. if (!hasRendered) {
  12668. if (animDuration) {
  12669. series.animationTimeout = setTimeout(function () {
  12670. series.afterAnimate();
  12671. }, animDuration);
  12672. } else {
  12673. series.afterAnimate();
  12674. }
  12675. }
  12676. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12677. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12678. series.hasRendered = true;
  12679. },
  12680. /**
  12681. * Redraw the series after an update in the axes.
  12682. */
  12683. redraw: function () {
  12684. var series = this,
  12685. chart = series.chart,
  12686. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  12687. wasDirty = series.isDirty,
  12688. group = series.group,
  12689. xAxis = series.xAxis,
  12690. yAxis = series.yAxis;
  12691. // reposition on resize
  12692. if (group) {
  12693. if (chart.inverted) {
  12694. group.attr({
  12695. width: chart.plotWidth,
  12696. height: chart.plotHeight
  12697. });
  12698. }
  12699. group.animate({
  12700. translateX: pick(xAxis && xAxis.left, chart.plotLeft),
  12701. translateY: pick(yAxis && yAxis.top, chart.plotTop)
  12702. });
  12703. }
  12704. series.translate();
  12705. series.render();
  12706. if (wasDirtyData) {
  12707. fireEvent(series, 'updatedData');
  12708. }
  12709. if (wasDirty || wasDirtyData) { // #3945 recalculate the kdtree when dirty
  12710. delete this.kdTree; // #3868 recalculate the kdtree with dirty data
  12711. }
  12712. },
  12713. /**
  12714. * KD Tree && PointSearching Implementation
  12715. */
  12716. kdDimensions: 1,
  12717. kdAxisArray: ['clientX', 'plotY'],
  12718. searchPoint: function (e, compareX) {
  12719. var series = this,
  12720. xAxis = series.xAxis,
  12721. yAxis = series.yAxis,
  12722. inverted = series.chart.inverted;
  12723. return this.searchKDTree({
  12724. clientX: inverted ? xAxis.len - e.chartY + xAxis.pos : e.chartX - xAxis.pos,
  12725. plotY: inverted ? yAxis.len - e.chartX + yAxis.pos : e.chartY - yAxis.pos
  12726. }, compareX);
  12727. },
  12728. buildKDTree: function () {
  12729. var series = this,
  12730. dimensions = series.kdDimensions;
  12731. // Internal function
  12732. function _kdtree(points, depth, dimensions) {
  12733. var axis, median, length = points && points.length;
  12734. if (length) {
  12735. // alternate between the axis
  12736. axis = series.kdAxisArray[depth % dimensions];
  12737. // sort point array
  12738. points.sort(function(a, b) {
  12739. return a[axis] - b[axis];
  12740. });
  12741. median = Math.floor(length / 2);
  12742. // build and return nod
  12743. return {
  12744. point: points[median],
  12745. left: _kdtree(points.slice(0, median), depth + 1, dimensions),
  12746. right: _kdtree(points.slice(median + 1), depth + 1, dimensions)
  12747. };
  12748. }
  12749. }
  12750. // Start the recursive build process with a clone of the points array and null points filtered out (#3873)
  12751. function startRecursive() {
  12752. var points = grep(series.points || [], function (point) { // #4390
  12753. return point.y !== null;
  12754. });
  12755. series.kdTree = _kdtree(points, dimensions, dimensions);
  12756. }
  12757. delete series.kdTree;
  12758. if (series.options.kdSync) { // For testing tooltips, don't build async
  12759. startRecursive();
  12760. } else {
  12761. setTimeout(startRecursive);
  12762. }
  12763. },
  12764. searchKDTree: function (point, compareX) {
  12765. var series = this,
  12766. kdX = this.kdAxisArray[0],
  12767. kdY = this.kdAxisArray[1],
  12768. kdComparer = compareX ? 'distX' : 'dist';
  12769. // Set the one and two dimensional distance on the point object
  12770. function setDistance(p1, p2) {
  12771. var x = (defined(p1[kdX]) && defined(p2[kdX])) ? Math.pow(p1[kdX] - p2[kdX], 2) : null,
  12772. y = (defined(p1[kdY]) && defined(p2[kdY])) ? Math.pow(p1[kdY] - p2[kdY], 2) : null,
  12773. r = (x || 0) + (y || 0);
  12774. p2.dist = defined(r) ? Math.sqrt(r) : Number.MAX_VALUE;
  12775. p2.distX = defined(x) ? Math.sqrt(x) : Number.MAX_VALUE;
  12776. }
  12777. function _search(search, tree, depth, dimensions) {
  12778. var point = tree.point,
  12779. axis = series.kdAxisArray[depth % dimensions],
  12780. tdist,
  12781. sideA,
  12782. sideB,
  12783. ret = point,
  12784. nPoint1,
  12785. nPoint2;
  12786. setDistance(search, point);
  12787. // Pick side based on distance to splitting point
  12788. tdist = search[axis] - point[axis];
  12789. sideA = tdist < 0 ? 'left' : 'right';
  12790. sideB = tdist < 0 ? 'right' : 'left';
  12791. // End of tree
  12792. if (tree[sideA]) {
  12793. nPoint1 =_search(search, tree[sideA], depth + 1, dimensions);
  12794. ret = (nPoint1[kdComparer] < ret[kdComparer] ? nPoint1 : point);
  12795. }
  12796. if (tree[sideB]) {
  12797. // compare distance to current best to splitting point to decide wether to check side B or not
  12798. if (Math.sqrt(tdist * tdist) < ret[kdComparer]) {
  12799. nPoint2 = _search(search, tree[sideB], depth + 1, dimensions);
  12800. ret = (nPoint2[kdComparer] < ret[kdComparer] ? nPoint2 : ret);
  12801. }
  12802. }
  12803. return ret;
  12804. }
  12805. if (!this.kdTree) {
  12806. this.buildKDTree();
  12807. }
  12808. if (this.kdTree) {
  12809. return _search(point,
  12810. this.kdTree, this.kdDimensions, this.kdDimensions);
  12811. }
  12812. }
  12813. }; // end Series prototype
  12814. /**
  12815. * The class for stack items
  12816. */
  12817. function StackItem(axis, options, isNegative, x, stackOption) {
  12818. var inverted = axis.chart.inverted;
  12819. this.axis = axis;
  12820. // Tells if the stack is negative
  12821. this.isNegative = isNegative;
  12822. // Save the options to be able to style the label
  12823. this.options = options;
  12824. // Save the x value to be able to position the label later
  12825. this.x = x;
  12826. // Initialize total value
  12827. this.total = null;
  12828. // This will keep each points' extremes stored by series.index and point index
  12829. this.points = {};
  12830. // Save the stack option on the series configuration object, and whether to treat it as percent
  12831. this.stack = stackOption;
  12832. // The align options and text align varies on whether the stack is negative and
  12833. // if the chart is inverted or not.
  12834. // First test the user supplied value, then use the dynamic.
  12835. this.alignOptions = {
  12836. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  12837. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  12838. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  12839. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  12840. };
  12841. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  12842. }
  12843. StackItem.prototype = {
  12844. destroy: function () {
  12845. destroyObjectProperties(this, this.axis);
  12846. },
  12847. /**
  12848. * Renders the stack total label and adds it to the stack label group.
  12849. */
  12850. render: function (group) {
  12851. var options = this.options,
  12852. formatOption = options.format,
  12853. str = formatOption ?
  12854. format(formatOption, this) :
  12855. options.formatter.call(this); // format the text in the label
  12856. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  12857. if (this.label) {
  12858. this.label.attr({text: str, visibility: HIDDEN});
  12859. // Create new label
  12860. } else {
  12861. this.label =
  12862. this.axis.chart.renderer.text(str, null, null, options.useHTML) // dummy positions, actual position updated with setOffset method in columnseries
  12863. .css(options.style) // apply style
  12864. .attr({
  12865. align: this.textAlign, // fix the text-anchor
  12866. rotation: options.rotation, // rotation
  12867. visibility: HIDDEN // hidden until setOffset is called
  12868. })
  12869. .add(group); // add to the labels-group
  12870. }
  12871. },
  12872. /**
  12873. * Sets the offset that the stack has from the x value and repositions the label.
  12874. */
  12875. setOffset: function (xOffset, xWidth) {
  12876. var stackItem = this,
  12877. axis = stackItem.axis,
  12878. chart = axis.chart,
  12879. inverted = chart.inverted,
  12880. reversed = axis.reversed,
  12881. neg = (this.isNegative && !reversed) || (!this.isNegative && reversed), // #4056
  12882. y = axis.translate(axis.usePercentage ? 100 : this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  12883. yZero = axis.translate(0), // stack origin
  12884. h = mathAbs(y - yZero), // stack height
  12885. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  12886. plotHeight = chart.plotHeight,
  12887. stackBox = { // this is the box for the complete stack
  12888. x: inverted ? (neg ? y : y - h) : x,
  12889. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  12890. width: inverted ? h : xWidth,
  12891. height: inverted ? xWidth : h
  12892. },
  12893. label = this.label,
  12894. alignAttr;
  12895. if (label) {
  12896. label.align(this.alignOptions, null, stackBox); // align the label to the box
  12897. // Set visibility (#678)
  12898. alignAttr = label.alignAttr;
  12899. label[this.options.crop === false || chart.isInsidePlot(alignAttr.x, alignAttr.y) ? 'show' : 'hide'](true);
  12900. }
  12901. }
  12902. };
  12903. /**
  12904. * Generate stacks for each series and calculate stacks total values
  12905. */
  12906. Chart.prototype.getStacks = function () {
  12907. var chart = this;
  12908. // reset stacks for each yAxis
  12909. each(chart.yAxis, function (axis) {
  12910. if (axis.stacks && axis.hasVisibleSeries) {
  12911. axis.oldStacks = axis.stacks;
  12912. }
  12913. });
  12914. each(chart.series, function (series) {
  12915. if (series.options.stacking && (series.visible === true || chart.options.chart.ignoreHiddenSeries === false)) {
  12916. series.stackKey = series.type + pick(series.options.stack, '');
  12917. }
  12918. });
  12919. };
  12920. // Stacking methods defined on the Axis prototype
  12921. /**
  12922. * Build the stacks from top down
  12923. */
  12924. Axis.prototype.buildStacks = function () {
  12925. var series = this.series,
  12926. reversedStacks = pick(this.options.reversedStacks, true),
  12927. i = series.length;
  12928. if (!this.isXAxis) {
  12929. this.usePercentage = false;
  12930. while (i--) {
  12931. series[reversedStacks ? i : series.length - i - 1].setStackedPoints();
  12932. }
  12933. // Loop up again to compute percent stack
  12934. if (this.usePercentage) {
  12935. for (i = 0; i < series.length; i++) {
  12936. series[i].setPercentStacks();
  12937. }
  12938. }
  12939. }
  12940. };
  12941. Axis.prototype.renderStackTotals = function () {
  12942. var axis = this,
  12943. chart = axis.chart,
  12944. renderer = chart.renderer,
  12945. stacks = axis.stacks,
  12946. stackKey,
  12947. oneStack,
  12948. stackCategory,
  12949. stackTotalGroup = axis.stackTotalGroup;
  12950. // Create a separate group for the stack total labels
  12951. if (!stackTotalGroup) {
  12952. axis.stackTotalGroup = stackTotalGroup =
  12953. renderer.g('stack-labels')
  12954. .attr({
  12955. visibility: VISIBLE,
  12956. zIndex: 6
  12957. })
  12958. .add();
  12959. }
  12960. // plotLeft/Top will change when y axis gets wider so we need to translate the
  12961. // stackTotalGroup at every render call. See bug #506 and #516
  12962. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  12963. // Render each stack total
  12964. for (stackKey in stacks) {
  12965. oneStack = stacks[stackKey];
  12966. for (stackCategory in oneStack) {
  12967. oneStack[stackCategory].render(stackTotalGroup);
  12968. }
  12969. }
  12970. };
  12971. /**
  12972. * Set all the stacks to initial states and destroy unused ones.
  12973. */
  12974. Axis.prototype.resetStacks = function () {
  12975. var stacks = this.stacks,
  12976. type,
  12977. i;
  12978. if (!this.isXAxis) {
  12979. for (type in stacks) {
  12980. for (i in stacks[type]) {
  12981. // Clean up memory after point deletion (#1044, #4320)
  12982. if (stacks[type][i].touched < this.stacksTouched) {
  12983. stacks[type][i].destroy();
  12984. delete stacks[type][i];
  12985. // Reset stacks
  12986. } else {
  12987. stacks[type][i].total = null;
  12988. stacks[type][i].cum = 0;
  12989. }
  12990. }
  12991. }
  12992. }
  12993. };
  12994. Axis.prototype.cleanStacks = function () {
  12995. var stacks, type, i;
  12996. if (!this.isXAxis) {
  12997. if (this.oldStacks) {
  12998. stacks = this.stacks = this.oldStacks;
  12999. }
  13000. // reset stacks
  13001. for (type in stacks) {
  13002. for (i in stacks[type]) {
  13003. stacks[type][i].cum = stacks[type][i].total;
  13004. }
  13005. }
  13006. }
  13007. };
  13008. // Stacking methods defnied for Series prototype
  13009. /**
  13010. * Adds series' points value to corresponding stack
  13011. */
  13012. Series.prototype.setStackedPoints = function () {
  13013. if (!this.options.stacking || (this.visible !== true && this.chart.options.chart.ignoreHiddenSeries !== false)) {
  13014. return;
  13015. }
  13016. var series = this,
  13017. xData = series.processedXData,
  13018. yData = series.processedYData,
  13019. stackedYData = [],
  13020. yDataLength = yData.length,
  13021. seriesOptions = series.options,
  13022. threshold = seriesOptions.threshold,
  13023. stackThreshold = seriesOptions.startFromThreshold ? threshold : 0,
  13024. stackOption = seriesOptions.stack,
  13025. stacking = seriesOptions.stacking,
  13026. stackKey = series.stackKey,
  13027. negKey = '-' + stackKey,
  13028. negStacks = series.negStacks,
  13029. yAxis = series.yAxis,
  13030. stacks = yAxis.stacks,
  13031. oldStacks = yAxis.oldStacks,
  13032. isNegative,
  13033. stack,
  13034. other,
  13035. key,
  13036. pointKey,
  13037. i,
  13038. x,
  13039. y;
  13040. yAxis.stacksTouched += 1;
  13041. // loop over the non-null y values and read them into a local array
  13042. for (i = 0; i < yDataLength; i++) {
  13043. x = xData[i];
  13044. y = yData[i];
  13045. pointKey = series.index + ',' + i;
  13046. // Read stacked values into a stack based on the x value,
  13047. // the sign of y and the stack key. Stacking is also handled for null values (#739)
  13048. isNegative = negStacks && y < (stackThreshold ? 0 : threshold);
  13049. key = isNegative ? negKey : stackKey;
  13050. // Create empty object for this stack if it doesn't exist yet
  13051. if (!stacks[key]) {
  13052. stacks[key] = {};
  13053. }
  13054. // Initialize StackItem for this x
  13055. if (!stacks[key][x]) {
  13056. if (oldStacks[key] && oldStacks[key][x]) {
  13057. stacks[key][x] = oldStacks[key][x];
  13058. stacks[key][x].total = null;
  13059. } else {
  13060. stacks[key][x] = new StackItem(yAxis, yAxis.options.stackLabels, isNegative, x, stackOption);
  13061. }
  13062. }
  13063. // If the StackItem doesn't exist, create it first
  13064. stack = stacks[key][x];
  13065. //stack.points[pointKey] = [stack.cum || stackThreshold];
  13066. stack.points[pointKey] = [pick(stack.cum, stackThreshold)];
  13067. stack.touched = yAxis.stacksTouched;
  13068. // Add value to the stack total
  13069. if (stacking === 'percent') {
  13070. // Percent stacked column, totals are the same for the positive and negative stacks
  13071. other = isNegative ? stackKey : negKey;
  13072. if (negStacks && stacks[other] && stacks[other][x]) {
  13073. other = stacks[other][x];
  13074. stack.total = other.total = mathMax(other.total, stack.total) + mathAbs(y) || 0;
  13075. // Percent stacked areas
  13076. } else {
  13077. stack.total = correctFloat(stack.total + (mathAbs(y) || 0));
  13078. }
  13079. } else {
  13080. stack.total = correctFloat(stack.total + (y || 0));
  13081. }
  13082. stack.cum = pick(stack.cum, stackThreshold) + (y || 0);
  13083. stack.points[pointKey].push(stack.cum);
  13084. stackedYData[i] = stack.cum;
  13085. }
  13086. if (stacking === 'percent') {
  13087. yAxis.usePercentage = true;
  13088. }
  13089. this.stackedYData = stackedYData; // To be used in getExtremes
  13090. // Reset old stacks
  13091. yAxis.oldStacks = {};
  13092. };
  13093. /**
  13094. * Iterate over all stacks and compute the absolute values to percent
  13095. */
  13096. Series.prototype.setPercentStacks = function () {
  13097. var series = this,
  13098. stackKey = series.stackKey,
  13099. stacks = series.yAxis.stacks,
  13100. processedXData = series.processedXData;
  13101. each([stackKey, '-' + stackKey], function (key) {
  13102. var i = processedXData.length,
  13103. x,
  13104. stack,
  13105. pointExtremes,
  13106. totalFactor;
  13107. while (i--) {
  13108. x = processedXData[i];
  13109. stack = stacks[key] && stacks[key][x];
  13110. pointExtremes = stack && stack.points[series.index + ',' + i];
  13111. if (pointExtremes) {
  13112. totalFactor = stack.total ? 100 / stack.total : 0;
  13113. pointExtremes[0] = correctFloat(pointExtremes[0] * totalFactor); // Y bottom value
  13114. pointExtremes[1] = correctFloat(pointExtremes[1] * totalFactor); // Y value
  13115. series.stackedYData[i] = pointExtremes[1];
  13116. }
  13117. }
  13118. });
  13119. };
  13120. // Extend the Chart prototype for dynamic methods
  13121. extend(Chart.prototype, {
  13122. /**
  13123. * Add a series dynamically after time
  13124. *
  13125. * @param {Object} options The config options
  13126. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  13127. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13128. * configuration
  13129. *
  13130. * @return {Object} series The newly created series object
  13131. */
  13132. addSeries: function (options, redraw, animation) {
  13133. var series,
  13134. chart = this;
  13135. if (options) {
  13136. redraw = pick(redraw, true); // defaults to true
  13137. fireEvent(chart, 'addSeries', { options: options }, function () {
  13138. series = chart.initSeries(options);
  13139. chart.isDirtyLegend = true; // the series array is out of sync with the display
  13140. chart.linkSeries();
  13141. if (redraw) {
  13142. chart.redraw(animation);
  13143. }
  13144. });
  13145. }
  13146. return series;
  13147. },
  13148. /**
  13149. * Add an axis to the chart
  13150. * @param {Object} options The axis option
  13151. * @param {Boolean} isX Whether it is an X axis or a value axis
  13152. */
  13153. addAxis: function (options, isX, redraw, animation) {
  13154. var key = isX ? 'xAxis' : 'yAxis',
  13155. chartOptions = this.options,
  13156. axis;
  13157. /*jslint unused: false*/
  13158. axis = new Axis(this, merge(options, {
  13159. index: this[key].length,
  13160. isX: isX
  13161. }));
  13162. /*jslint unused: true*/
  13163. // Push the new axis options to the chart options
  13164. chartOptions[key] = splat(chartOptions[key] || {});
  13165. chartOptions[key].push(options);
  13166. if (pick(redraw, true)) {
  13167. this.redraw(animation);
  13168. }
  13169. },
  13170. /**
  13171. * Dim the chart and show a loading text or symbol
  13172. * @param {String} str An optional text to show in the loading label instead of the default one
  13173. */
  13174. showLoading: function (str) {
  13175. var chart = this,
  13176. options = chart.options,
  13177. loadingDiv = chart.loadingDiv,
  13178. loadingOptions = options.loading,
  13179. setLoadingSize = function () {
  13180. if (loadingDiv) {
  13181. css(loadingDiv, {
  13182. left: chart.plotLeft + PX,
  13183. top: chart.plotTop + PX,
  13184. width: chart.plotWidth + PX,
  13185. height: chart.plotHeight + PX
  13186. });
  13187. }
  13188. };
  13189. // create the layer at the first call
  13190. if (!loadingDiv) {
  13191. chart.loadingDiv = loadingDiv = createElement(DIV, {
  13192. className: PREFIX + 'loading'
  13193. }, extend(loadingOptions.style, {
  13194. zIndex: 10,
  13195. display: NONE
  13196. }), chart.container);
  13197. chart.loadingSpan = createElement(
  13198. 'span',
  13199. null,
  13200. loadingOptions.labelStyle,
  13201. loadingDiv
  13202. );
  13203. addEvent(chart, 'redraw', setLoadingSize); // #1080
  13204. }
  13205. // update text
  13206. chart.loadingSpan.innerHTML = str || options.lang.loading;
  13207. // show it
  13208. if (!chart.loadingShown) {
  13209. css(loadingDiv, {
  13210. opacity: 0,
  13211. display: ''
  13212. });
  13213. animate(loadingDiv, {
  13214. opacity: loadingOptions.style.opacity
  13215. }, {
  13216. duration: loadingOptions.showDuration || 0
  13217. });
  13218. chart.loadingShown = true;
  13219. }
  13220. setLoadingSize();
  13221. },
  13222. /**
  13223. * Hide the loading layer
  13224. */
  13225. hideLoading: function () {
  13226. var options = this.options,
  13227. loadingDiv = this.loadingDiv;
  13228. if (loadingDiv) {
  13229. animate(loadingDiv, {
  13230. opacity: 0
  13231. }, {
  13232. duration: options.loading.hideDuration || 100,
  13233. complete: function () {
  13234. css(loadingDiv, { display: NONE });
  13235. }
  13236. });
  13237. }
  13238. this.loadingShown = false;
  13239. }
  13240. });
  13241. // extend the Point prototype for dynamic methods
  13242. extend(Point.prototype, {
  13243. /**
  13244. * Update the point with new options (typically x/y data) and optionally redraw the series.
  13245. *
  13246. * @param {Object} options Point options as defined in the series.data array
  13247. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13248. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13249. * configuration
  13250. *
  13251. */
  13252. update: function (options, redraw, animation, runEvent) {
  13253. var point = this,
  13254. series = point.series,
  13255. graphic = point.graphic,
  13256. i,
  13257. chart = series.chart,
  13258. seriesOptions = series.options,
  13259. names = series.xAxis && series.xAxis.names;
  13260. redraw = pick(redraw, true);
  13261. function update() {
  13262. point.applyOptions(options);
  13263. // Update visuals
  13264. if (point.y === null && graphic) { // #4146
  13265. point.graphic = graphic.destroy();
  13266. }
  13267. if (isObject(options) && !isArray(options)) {
  13268. // Defer the actual redraw until getAttribs has been called (#3260)
  13269. point.redraw = function () {
  13270. if (graphic && graphic.element) {
  13271. if (options && options.marker && options.marker.symbol) {
  13272. point.graphic = graphic.destroy();
  13273. }
  13274. }
  13275. if (options && options.dataLabels && point.dataLabel) { // #2468
  13276. point.dataLabel = point.dataLabel.destroy();
  13277. }
  13278. point.redraw = null;
  13279. };
  13280. }
  13281. // record changes in the parallel arrays
  13282. i = point.index;
  13283. series.updateParallelArrays(point, i);
  13284. if (names && point.name) {
  13285. names[point.x] = point.name;
  13286. }
  13287. seriesOptions.data[i] = point.options;
  13288. // redraw
  13289. series.isDirty = series.isDirtyData = true;
  13290. if (!series.fixedBox && series.hasCartesianSeries) { // #1906, #2320
  13291. chart.isDirtyBox = true;
  13292. }
  13293. if (seriesOptions.legendType === 'point') { // #1831, #1885
  13294. chart.isDirtyLegend = true;
  13295. }
  13296. if (redraw) {
  13297. chart.redraw(animation);
  13298. }
  13299. }
  13300. // Fire the event with a default handler of doing the update
  13301. if (runEvent === false) { // When called from setData
  13302. update();
  13303. } else {
  13304. point.firePointEvent('update', { options: options }, update);
  13305. }
  13306. },
  13307. /**
  13308. * Remove a point and optionally redraw the series and if necessary the axes
  13309. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13310. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13311. * configuration
  13312. */
  13313. remove: function (redraw, animation) {
  13314. this.series.removePoint(inArray(this, this.series.data), redraw, animation);
  13315. }
  13316. });
  13317. // Extend the series prototype for dynamic methods
  13318. extend(Series.prototype, {
  13319. /**
  13320. * Add a point dynamically after chart load time
  13321. * @param {Object} options Point options as given in series.data
  13322. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13323. * @param {Boolean} shift If shift is true, a point is shifted off the start
  13324. * of the series as one is appended to the end.
  13325. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13326. * configuration
  13327. */
  13328. addPoint: function (options, redraw, shift, animation) {
  13329. var series = this,
  13330. seriesOptions = series.options,
  13331. data = series.data,
  13332. graph = series.graph,
  13333. area = series.area,
  13334. chart = series.chart,
  13335. names = series.xAxis && series.xAxis.names,
  13336. currentShift = (graph && graph.shift) || 0,
  13337. shiftShapes = ['graph', 'area'],
  13338. dataOptions = seriesOptions.data,
  13339. point,
  13340. isInTheMiddle,
  13341. xData = series.xData,
  13342. i,
  13343. x;
  13344. setAnimation(animation, chart);
  13345. // Make graph animate sideways
  13346. if (shift) {
  13347. i = series.zones.length;
  13348. while (i--) {
  13349. shiftShapes.push('zoneGraph' + i, 'zoneArea' + i);
  13350. }
  13351. each(shiftShapes, function (shape) {
  13352. if (series[shape]) {
  13353. series[shape].shift = currentShift + 1;
  13354. }
  13355. });
  13356. }
  13357. if (area) {
  13358. area.isArea = true; // needed in animation, both with and without shift
  13359. }
  13360. // Optional redraw, defaults to true
  13361. redraw = pick(redraw, true);
  13362. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  13363. // the Point instance will be created on demand and pushed to the series.data array.
  13364. point = { series: series };
  13365. series.pointClass.prototype.applyOptions.apply(point, [options]);
  13366. x = point.x;
  13367. // Get the insertion point
  13368. i = xData.length;
  13369. if (series.requireSorting && x < xData[i - 1]) {
  13370. isInTheMiddle = true;
  13371. while (i && xData[i - 1] > x) {
  13372. i--;
  13373. }
  13374. }
  13375. series.updateParallelArrays(point, 'splice', i, 0, 0); // insert undefined item
  13376. series.updateParallelArrays(point, i); // update it
  13377. if (names && point.name) {
  13378. names[x] = point.name;
  13379. }
  13380. dataOptions.splice(i, 0, options);
  13381. if (isInTheMiddle) {
  13382. series.data.splice(i, 0, null);
  13383. series.processData();
  13384. }
  13385. // Generate points to be added to the legend (#1329)
  13386. if (seriesOptions.legendType === 'point') {
  13387. series.generatePoints();
  13388. }
  13389. // Shift the first point off the parallel arrays
  13390. // todo: consider series.removePoint(i) method
  13391. if (shift) {
  13392. if (data[0] && data[0].remove) {
  13393. data[0].remove(false);
  13394. } else {
  13395. data.shift();
  13396. series.updateParallelArrays(point, 'shift');
  13397. dataOptions.shift();
  13398. }
  13399. }
  13400. // redraw
  13401. series.isDirty = true;
  13402. series.isDirtyData = true;
  13403. if (redraw) {
  13404. series.getAttribs(); // #1937
  13405. chart.redraw();
  13406. }
  13407. },
  13408. /**
  13409. * Remove a point (rendered or not), by index
  13410. */
  13411. removePoint: function (i, redraw, animation) {
  13412. var series = this,
  13413. data = series.data,
  13414. point = data[i],
  13415. points = series.points,
  13416. chart = series.chart,
  13417. remove = function () {
  13418. if (data.length === points.length) {
  13419. points.splice(i, 1);
  13420. }
  13421. data.splice(i, 1);
  13422. series.options.data.splice(i, 1);
  13423. series.updateParallelArrays(point || { series: series }, 'splice', i, 1);
  13424. if (point) {
  13425. point.destroy();
  13426. }
  13427. // redraw
  13428. series.isDirty = true;
  13429. series.isDirtyData = true;
  13430. if (redraw) {
  13431. chart.redraw();
  13432. }
  13433. };
  13434. setAnimation(animation, chart);
  13435. redraw = pick(redraw, true);
  13436. // Fire the event with a default handler of removing the point
  13437. if (point) {
  13438. point.firePointEvent('remove', null, remove);
  13439. } else {
  13440. remove();
  13441. }
  13442. },
  13443. /**
  13444. * Remove a series and optionally redraw the chart
  13445. *
  13446. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  13447. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  13448. * configuration
  13449. */
  13450. remove: function (redraw, animation) {
  13451. var series = this,
  13452. chart = series.chart;
  13453. redraw = pick(redraw, true);
  13454. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  13455. (calling the remove function from the remove event) */
  13456. series.isRemoving = true;
  13457. // fire the event with a default handler of removing the point
  13458. fireEvent(series, 'remove', null, function () {
  13459. // destroy elements
  13460. series.destroy();
  13461. // redraw
  13462. chart.isDirtyLegend = chart.isDirtyBox = true;
  13463. chart.linkSeries();
  13464. if (redraw) {
  13465. chart.redraw(animation);
  13466. }
  13467. });
  13468. }
  13469. series.isRemoving = false;
  13470. },
  13471. /**
  13472. * Update the series with a new set of options
  13473. */
  13474. update: function (newOptions, redraw) {
  13475. var series = this,
  13476. chart = this.chart,
  13477. // must use user options when changing type because this.options is merged
  13478. // in with type specific plotOptions
  13479. oldOptions = this.userOptions,
  13480. oldType = this.type,
  13481. proto = seriesTypes[oldType].prototype,
  13482. preserve = ['group', 'markerGroup', 'dataLabelsGroup'],
  13483. n;
  13484. // If we're changing type or zIndex, create new groups (#3380, #3404)
  13485. if ((newOptions.type && newOptions.type !== oldType) || newOptions.zIndex !== undefined) {
  13486. preserve.length = 0;
  13487. }
  13488. // Make sure groups are not destroyed (#3094)
  13489. each(preserve, function (prop) {
  13490. preserve[prop] = series[prop];
  13491. delete series[prop];
  13492. });
  13493. // Do the merge, with some forced options
  13494. newOptions = merge(oldOptions, {
  13495. animation: false,
  13496. index: this.index,
  13497. pointStart: this.xData[0] // when updating after addPoint
  13498. }, { data: this.options.data }, newOptions);
  13499. // Destroy the series and delete all properties. Reinsert all methods
  13500. // and properties from the new type prototype (#2270, #3719)
  13501. this.remove(false);
  13502. for (n in proto) {
  13503. this[n] = UNDEFINED;
  13504. }
  13505. extend(this, seriesTypes[newOptions.type || oldType].prototype);
  13506. // Re-register groups (#3094)
  13507. each(preserve, function (prop) {
  13508. series[prop] = preserve[prop];
  13509. });
  13510. this.init(chart, newOptions);
  13511. chart.linkSeries(); // Links are lost in this.remove (#3028)
  13512. if (pick(redraw, true)) {
  13513. chart.redraw(false);
  13514. }
  13515. }
  13516. });
  13517. // Extend the Axis.prototype for dynamic methods
  13518. extend(Axis.prototype, {
  13519. /**
  13520. * Update the axis with a new options structure
  13521. */
  13522. update: function (newOptions, redraw) {
  13523. var chart = this.chart;
  13524. newOptions = chart.options[this.coll][this.options.index] = merge(this.userOptions, newOptions);
  13525. this.destroy(true);
  13526. this._addedPlotLB = this.chart._labelPanes = UNDEFINED; // #1611, #2887, #4314
  13527. this.init(chart, extend(newOptions, { events: UNDEFINED }));
  13528. chart.isDirtyBox = true;
  13529. if (pick(redraw, true)) {
  13530. chart.redraw();
  13531. }
  13532. },
  13533. /**
  13534. * Remove the axis from the chart
  13535. */
  13536. remove: function (redraw) {
  13537. var chart = this.chart,
  13538. key = this.coll, // xAxis or yAxis
  13539. axisSeries = this.series,
  13540. i = axisSeries.length;
  13541. // Remove associated series (#2687)
  13542. while (i--) {
  13543. if (axisSeries[i]) {
  13544. axisSeries[i].remove(false);
  13545. }
  13546. }
  13547. // Remove the axis
  13548. erase(chart.axes, this);
  13549. erase(chart[key], this);
  13550. chart.options[key].splice(this.options.index, 1);
  13551. each(chart[key], function (axis, i) { // Re-index, #1706
  13552. axis.options.index = i;
  13553. });
  13554. this.destroy();
  13555. chart.isDirtyBox = true;
  13556. if (pick(redraw, true)) {
  13557. chart.redraw();
  13558. }
  13559. },
  13560. /**
  13561. * Update the axis title by options
  13562. */
  13563. setTitle: function (newTitleOptions, redraw) {
  13564. this.update({ title: newTitleOptions }, redraw);
  13565. },
  13566. /**
  13567. * Set new axis categories and optionally redraw
  13568. * @param {Array} categories
  13569. * @param {Boolean} redraw
  13570. */
  13571. setCategories: function (categories, redraw) {
  13572. this.update({ categories: categories }, redraw);
  13573. }
  13574. });
  13575. /**
  13576. * LineSeries object
  13577. */
  13578. var LineSeries = extendClass(Series);
  13579. seriesTypes.line = LineSeries;
  13580. /**
  13581. * Set the default options for area
  13582. */
  13583. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  13584. threshold: 0
  13585. // trackByArea: false,
  13586. // lineColor: null, // overrides color, but lets fillColor be unaltered
  13587. // fillOpacity: 0.75,
  13588. // fillColor: null
  13589. });
  13590. /**
  13591. * AreaSeries object
  13592. */
  13593. var AreaSeries = extendClass(Series, {
  13594. type: 'area',
  13595. /**
  13596. * For stacks, don't split segments on null values. Instead, draw null values with
  13597. * no marker. Also insert dummy points for any X position that exists in other series
  13598. * in the stack.
  13599. */
  13600. getSegments: function () {
  13601. var series = this,
  13602. segments = [],
  13603. segment = [],
  13604. keys = [],
  13605. xAxis = this.xAxis,
  13606. yAxis = this.yAxis,
  13607. stack = yAxis.stacks[this.stackKey],
  13608. pointMap = {},
  13609. plotX,
  13610. plotY,
  13611. points = this.points,
  13612. connectNulls = this.options.connectNulls,
  13613. i,
  13614. x;
  13615. if (this.options.stacking && !this.cropped) { // cropped causes artefacts in Stock, and perf issue
  13616. // Create a map where we can quickly look up the points by their X value.
  13617. for (i = 0; i < points.length; i++) {
  13618. pointMap[points[i].x] = points[i];
  13619. }
  13620. // Sort the keys (#1651)
  13621. for (x in stack) {
  13622. if (stack[x].total !== null) { // nulled after switching between grouping and not (#1651, #2336)
  13623. keys.push(+x);
  13624. }
  13625. }
  13626. keys.sort(function (a, b) {
  13627. return a - b;
  13628. });
  13629. each(keys, function (x) {
  13630. var y = 0,
  13631. stackPoint;
  13632. if (connectNulls && (!pointMap[x] || pointMap[x].y === null)) { // #1836
  13633. return;
  13634. // The point exists, push it to the segment
  13635. } else if (pointMap[x]) {
  13636. segment.push(pointMap[x]);
  13637. // There is no point for this X value in this series, so we
  13638. // insert a dummy point in order for the areas to be drawn
  13639. // correctly.
  13640. } else {
  13641. // Loop down the stack to find the series below this one that has
  13642. // a value (#1991)
  13643. for (i = series.index; i <= yAxis.series.length; i++) {
  13644. stackPoint = stack[x].points[i + ',' + x];
  13645. if (stackPoint) {
  13646. y = stackPoint[1];
  13647. break;
  13648. }
  13649. }
  13650. plotX = xAxis.translate(x);
  13651. plotY = yAxis.toPixels(y, true);
  13652. segment.push({
  13653. y: null,
  13654. plotX: plotX,
  13655. clientX: plotX,
  13656. plotY: plotY,
  13657. yBottom: plotY,
  13658. onMouseOver: noop
  13659. });
  13660. }
  13661. });
  13662. if (segment.length) {
  13663. segments.push(segment);
  13664. }
  13665. } else {
  13666. Series.prototype.getSegments.call(this);
  13667. segments = this.segments;
  13668. }
  13669. this.segments = segments;
  13670. },
  13671. /**
  13672. * Extend the base Series getSegmentPath method by adding the path for the area.
  13673. * This path is pushed to the series.areaPath property.
  13674. */
  13675. getSegmentPath: function (segment) {
  13676. var segmentPath = Series.prototype.getSegmentPath.call(this, segment), // call base method
  13677. areaSegmentPath = [].concat(segmentPath), // work on a copy for the area path
  13678. i,
  13679. options = this.options,
  13680. segLength = segmentPath.length,
  13681. translatedThreshold = this.yAxis.getThreshold(options.threshold), // #2181
  13682. yBottom;
  13683. if (segLength === 3) { // for animation from 1 to two points
  13684. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  13685. }
  13686. if (options.stacking && !this.closedStacks) {
  13687. // Follow stack back. Todo: implement areaspline. A general solution could be to
  13688. // reverse the entire graphPath of the previous series, though may be hard with
  13689. // splines and with series with different extremes
  13690. for (i = segment.length - 1; i >= 0; i--) {
  13691. yBottom = pick(segment[i].yBottom, translatedThreshold);
  13692. // step line?
  13693. if (i < segment.length - 1 && options.step) {
  13694. areaSegmentPath.push(segment[i + 1].plotX, yBottom);
  13695. }
  13696. areaSegmentPath.push(segment[i].plotX, yBottom);
  13697. }
  13698. } else { // follow zero line back
  13699. this.closeSegment(areaSegmentPath, segment, translatedThreshold);
  13700. }
  13701. this.areaPath = this.areaPath.concat(areaSegmentPath);
  13702. return segmentPath;
  13703. },
  13704. /**
  13705. * Extendable method to close the segment path of an area. This is overridden in polar
  13706. * charts.
  13707. */
  13708. closeSegment: function (path, segment, translatedThreshold) {
  13709. path.push(
  13710. L,
  13711. segment[segment.length - 1].plotX,
  13712. translatedThreshold,
  13713. L,
  13714. segment[0].plotX,
  13715. translatedThreshold
  13716. );
  13717. },
  13718. /**
  13719. * Draw the graph and the underlying area. This method calls the Series base
  13720. * function and adds the area. The areaPath is calculated in the getSegmentPath
  13721. * method called from Series.prototype.drawGraph.
  13722. */
  13723. drawGraph: function () {
  13724. // Define or reset areaPath
  13725. this.areaPath = [];
  13726. // Call the base method
  13727. Series.prototype.drawGraph.apply(this);
  13728. // Define local variables
  13729. var series = this,
  13730. areaPath = this.areaPath,
  13731. options = this.options,
  13732. zones = this.zones,
  13733. props = [['area', this.color, options.fillColor]]; // area name, main color, fill color
  13734. each(zones, function (threshold, i) {
  13735. props.push(['zoneArea' + i, threshold.color || series.color, threshold.fillColor || options.fillColor]);
  13736. });
  13737. each(props, function (prop) {
  13738. var areaKey = prop[0],
  13739. area = series[areaKey];
  13740. // Create or update the area
  13741. if (area) { // update
  13742. area.animate({ d: areaPath });
  13743. } else { // create
  13744. series[areaKey] = series.chart.renderer.path(areaPath)
  13745. .attr({
  13746. fill: pick(
  13747. prop[2],
  13748. Color(prop[1]).setOpacity(pick(options.fillOpacity, 0.75)).get()
  13749. ),
  13750. zIndex: 0 // #1069
  13751. }).add(series.group);
  13752. }
  13753. });
  13754. },
  13755. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  13756. });
  13757. seriesTypes.area = AreaSeries;
  13758. /**
  13759. * Set the default options for spline
  13760. */
  13761. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  13762. /**
  13763. * SplineSeries object
  13764. */
  13765. var SplineSeries = extendClass(Series, {
  13766. type: 'spline',
  13767. /**
  13768. * Get the spline segment from a given point's previous neighbour to the given point
  13769. */
  13770. getPointSpline: function (segment, point, i) {
  13771. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  13772. denom = smoothing + 1,
  13773. plotX = point.plotX,
  13774. plotY = point.plotY,
  13775. lastPoint = segment[i - 1],
  13776. nextPoint = segment[i + 1],
  13777. leftContX,
  13778. leftContY,
  13779. rightContX,
  13780. rightContY,
  13781. ret;
  13782. // find control points
  13783. if (lastPoint && nextPoint) {
  13784. var lastX = lastPoint.plotX,
  13785. lastY = lastPoint.plotY,
  13786. nextX = nextPoint.plotX,
  13787. nextY = nextPoint.plotY,
  13788. correction;
  13789. leftContX = (smoothing * plotX + lastX) / denom;
  13790. leftContY = (smoothing * plotY + lastY) / denom;
  13791. rightContX = (smoothing * plotX + nextX) / denom;
  13792. rightContY = (smoothing * plotY + nextY) / denom;
  13793. // have the two control points make a straight line through main point
  13794. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  13795. (rightContX - leftContX) + plotY - rightContY;
  13796. leftContY += correction;
  13797. rightContY += correction;
  13798. // to prevent false extremes, check that control points are between
  13799. // neighbouring points' y values
  13800. if (leftContY > lastY && leftContY > plotY) {
  13801. leftContY = mathMax(lastY, plotY);
  13802. rightContY = 2 * plotY - leftContY; // mirror of left control point
  13803. } else if (leftContY < lastY && leftContY < plotY) {
  13804. leftContY = mathMin(lastY, plotY);
  13805. rightContY = 2 * plotY - leftContY;
  13806. }
  13807. if (rightContY > nextY && rightContY > plotY) {
  13808. rightContY = mathMax(nextY, plotY);
  13809. leftContY = 2 * plotY - rightContY;
  13810. } else if (rightContY < nextY && rightContY < plotY) {
  13811. rightContY = mathMin(nextY, plotY);
  13812. leftContY = 2 * plotY - rightContY;
  13813. }
  13814. // record for drawing in next point
  13815. point.rightContX = rightContX;
  13816. point.rightContY = rightContY;
  13817. }
  13818. // Visualize control points for debugging
  13819. /*
  13820. if (leftContX) {
  13821. this.chart.renderer.circle(leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop, 2)
  13822. .attr({
  13823. stroke: 'red',
  13824. 'stroke-width': 1,
  13825. fill: 'none'
  13826. })
  13827. .add();
  13828. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop,
  13829. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  13830. .attr({
  13831. stroke: 'red',
  13832. 'stroke-width': 1
  13833. })
  13834. .add();
  13835. this.chart.renderer.circle(rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop, 2)
  13836. .attr({
  13837. stroke: 'green',
  13838. 'stroke-width': 1,
  13839. fill: 'none'
  13840. })
  13841. .add();
  13842. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop,
  13843. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  13844. .attr({
  13845. stroke: 'green',
  13846. 'stroke-width': 1
  13847. })
  13848. .add();
  13849. }
  13850. */
  13851. // moveTo or lineTo
  13852. if (!i) {
  13853. ret = [M, plotX, plotY];
  13854. } else { // curve from last point to this
  13855. ret = [
  13856. 'C',
  13857. lastPoint.rightContX || lastPoint.plotX,
  13858. lastPoint.rightContY || lastPoint.plotY,
  13859. leftContX || plotX,
  13860. leftContY || plotY,
  13861. plotX,
  13862. plotY
  13863. ];
  13864. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  13865. }
  13866. return ret;
  13867. }
  13868. });
  13869. seriesTypes.spline = SplineSeries;
  13870. /**
  13871. * Set the default options for areaspline
  13872. */
  13873. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  13874. /**
  13875. * AreaSplineSeries object
  13876. */
  13877. var areaProto = AreaSeries.prototype,
  13878. AreaSplineSeries = extendClass(SplineSeries, {
  13879. type: 'areaspline',
  13880. closedStacks: true, // instead of following the previous graph back, follow the threshold back
  13881. // Mix in methods from the area series
  13882. getSegmentPath: areaProto.getSegmentPath,
  13883. closeSegment: areaProto.closeSegment,
  13884. drawGraph: areaProto.drawGraph,
  13885. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  13886. });
  13887. seriesTypes.areaspline = AreaSplineSeries;
  13888. /**
  13889. * Set the default options for column
  13890. */
  13891. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  13892. borderColor: '#FFFFFF',
  13893. //borderWidth: 1,
  13894. borderRadius: 0,
  13895. //colorByPoint: undefined,
  13896. groupPadding: 0.2,
  13897. //grouping: true,
  13898. marker: null, // point options are specified in the base options
  13899. pointPadding: 0.1,
  13900. //pointWidth: null,
  13901. minPointLength: 0,
  13902. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  13903. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  13904. states: {
  13905. hover: {
  13906. brightness: 0.1,
  13907. shadow: false,
  13908. halo: false
  13909. },
  13910. select: {
  13911. color: '#C0C0C0',
  13912. borderColor: '#000000',
  13913. shadow: false
  13914. }
  13915. },
  13916. dataLabels: {
  13917. align: null, // auto
  13918. verticalAlign: null, // auto
  13919. y: null
  13920. },
  13921. startFromThreshold: true, // docs: http://jsfiddle.net/highcharts/hz8fopan/14/
  13922. stickyTracking: false,
  13923. tooltip: {
  13924. distance: 6
  13925. },
  13926. threshold: 0
  13927. });
  13928. /**
  13929. * ColumnSeries object
  13930. */
  13931. var ColumnSeries = extendClass(Series, {
  13932. type: 'column',
  13933. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13934. stroke: 'borderColor',
  13935. fill: 'color',
  13936. r: 'borderRadius'
  13937. },
  13938. cropShoulder: 0,
  13939. directTouch: true, // When tooltip is not shared, this series (and derivatives) requires direct touch/hover. KD-tree does not apply.
  13940. trackerGroups: ['group', 'dataLabelsGroup'],
  13941. negStacks: true, // use separate negative stacks, unlike area stacks where a negative
  13942. // point is substracted from previous (#1910)
  13943. /**
  13944. * Initialize the series
  13945. */
  13946. init: function () {
  13947. Series.prototype.init.apply(this, arguments);
  13948. var series = this,
  13949. chart = series.chart;
  13950. // if the series is added dynamically, force redraw of other
  13951. // series affected by a new column
  13952. if (chart.hasRendered) {
  13953. each(chart.series, function (otherSeries) {
  13954. if (otherSeries.type === series.type) {
  13955. otherSeries.isDirty = true;
  13956. }
  13957. });
  13958. }
  13959. },
  13960. /**
  13961. * Return the width and x offset of the columns adjusted for grouping, groupPadding, pointPadding,
  13962. * pointWidth etc.
  13963. */
  13964. getColumnMetrics: function () {
  13965. var series = this,
  13966. options = series.options,
  13967. xAxis = series.xAxis,
  13968. yAxis = series.yAxis,
  13969. reversedXAxis = xAxis.reversed,
  13970. stackKey,
  13971. stackGroups = {},
  13972. columnIndex,
  13973. columnCount = 0;
  13974. // Get the total number of column type series.
  13975. // This is called on every series. Consider moving this logic to a
  13976. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  13977. if (options.grouping === false) {
  13978. columnCount = 1;
  13979. } else {
  13980. each(series.chart.series, function (otherSeries) {
  13981. var otherOptions = otherSeries.options,
  13982. otherYAxis = otherSeries.yAxis;
  13983. if (otherSeries.type === series.type && otherSeries.visible &&
  13984. yAxis.len === otherYAxis.len && yAxis.pos === otherYAxis.pos) { // #642, #2086
  13985. if (otherOptions.stacking) {
  13986. stackKey = otherSeries.stackKey;
  13987. if (stackGroups[stackKey] === UNDEFINED) {
  13988. stackGroups[stackKey] = columnCount++;
  13989. }
  13990. columnIndex = stackGroups[stackKey];
  13991. } else if (otherOptions.grouping !== false) { // #1162
  13992. columnIndex = columnCount++;
  13993. }
  13994. otherSeries.columnIndex = columnIndex;
  13995. }
  13996. });
  13997. }
  13998. var categoryWidth = mathMin(
  13999. mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || xAxis.tickInterval || 1), // #2610
  14000. xAxis.len // #1535
  14001. ),
  14002. groupPadding = categoryWidth * options.groupPadding,
  14003. groupWidth = categoryWidth - 2 * groupPadding,
  14004. pointOffsetWidth = groupWidth / columnCount,
  14005. pointWidth = mathMin(
  14006. options.maxPointWidth || xAxis.len,
  14007. pick(options.pointWidth, pointOffsetWidth * (1 - 2 * options.pointPadding))
  14008. ),
  14009. pointPadding = (pointOffsetWidth - pointWidth) / 2,
  14010. colIndex = (reversedXAxis ?
  14011. columnCount - (series.columnIndex || 0) : // #1251
  14012. series.columnIndex) || 0,
  14013. pointXOffset = pointPadding + (groupPadding + colIndex *
  14014. pointOffsetWidth - (categoryWidth / 2)) *
  14015. (reversedXAxis ? -1 : 1);
  14016. // Save it for reading in linked series (Error bars particularly)
  14017. return (series.columnMetrics = {
  14018. width: pointWidth,
  14019. offset: pointXOffset
  14020. });
  14021. },
  14022. /**
  14023. * Translate each point to the plot area coordinate system and find shape positions
  14024. */
  14025. translate: function () {
  14026. var series = this,
  14027. chart = series.chart,
  14028. options = series.options,
  14029. borderWidth = series.borderWidth = pick(
  14030. options.borderWidth,
  14031. series.closestPointRange * series.xAxis.transA < 2 ? 0 : 1 // #3635
  14032. ),
  14033. yAxis = series.yAxis,
  14034. threshold = options.threshold,
  14035. translatedThreshold = series.translatedThreshold = yAxis.getThreshold(threshold),
  14036. minPointLength = pick(options.minPointLength, 5),
  14037. metrics = series.getColumnMetrics(),
  14038. pointWidth = metrics.width,
  14039. seriesBarW = series.barW = mathMax(pointWidth, 1 + 2 * borderWidth), // postprocessed for border width
  14040. pointXOffset = series.pointXOffset = metrics.offset,
  14041. xCrisp = -(borderWidth % 2 ? 0.5 : 0),
  14042. yCrisp = borderWidth % 2 ? 0.5 : 1;
  14043. if (chart.inverted) {
  14044. translatedThreshold -= 0.5; // #3355
  14045. if (chart.renderer.isVML) {
  14046. yCrisp += 1;
  14047. }
  14048. }
  14049. // When the pointPadding is 0, we want the columns to be packed tightly, so we allow individual
  14050. // columns to have individual sizes. When pointPadding is greater, we strive for equal-width
  14051. // columns (#2694).
  14052. if (options.pointPadding) {
  14053. seriesBarW = mathCeil(seriesBarW);
  14054. }
  14055. Series.prototype.translate.apply(series);
  14056. // Record the new values
  14057. each(series.points, function (point) {
  14058. var yBottom = pick(point.yBottom, translatedThreshold),
  14059. safeDistance = 999 + mathAbs(yBottom),
  14060. plotY = mathMin(mathMax(-safeDistance, point.plotY), yAxis.len + safeDistance), // Don't draw too far outside plot area (#1303, #2241, #4264)
  14061. barX = point.plotX + pointXOffset,
  14062. barW = seriesBarW,
  14063. barY = mathMin(plotY, yBottom),
  14064. right,
  14065. bottom,
  14066. fromTop,
  14067. up,
  14068. barH = mathMax(plotY, yBottom) - barY;
  14069. // Handle options.minPointLength
  14070. if (mathAbs(barH) < minPointLength) {
  14071. if (minPointLength) {
  14072. barH = minPointLength;
  14073. up = (!yAxis.reversed && !point.negative) || (yAxis.reversed && point.negative);
  14074. barY =
  14075. mathRound(mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  14076. yBottom - minPointLength : // keep position
  14077. translatedThreshold - (up ? minPointLength : 0)); // #1485, #4051
  14078. }
  14079. }
  14080. // Cache for access in polar
  14081. point.barX = barX;
  14082. point.pointWidth = pointWidth;
  14083. // Round off to obtain crisp edges and avoid overlapping with neighbours (#2694)
  14084. right = mathRound(barX + barW) + xCrisp;
  14085. barX = mathRound(barX) + xCrisp;
  14086. barW = right - barX;
  14087. fromTop = mathAbs(barY) < 0.5;
  14088. bottom = mathMin(mathRound(barY + barH) + yCrisp, 9e4); // #3575
  14089. barY = mathRound(barY) + yCrisp;
  14090. barH = bottom - barY;
  14091. // Top edges are exceptions
  14092. if (fromTop) {
  14093. barY -= 1;
  14094. barH += 1;
  14095. }
  14096. // Fix the tooltip on center of grouped columns (#1216, #424, #3648)
  14097. point.tooltipPos = chart.inverted ?
  14098. [yAxis.len + yAxis.pos - chart.plotLeft - plotY, series.xAxis.len - barX - barW / 2, barH] :
  14099. [barX + barW / 2, plotY + yAxis.pos - chart.plotTop, barH];
  14100. // Register shape type and arguments to be used in drawPoints
  14101. point.shapeType = 'rect';
  14102. point.shapeArgs = {
  14103. x: barX,
  14104. y: barY,
  14105. width: barW,
  14106. height: barH
  14107. };
  14108. });
  14109. },
  14110. getSymbol: noop,
  14111. /**
  14112. * Use a solid rectangle like the area series types
  14113. */
  14114. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  14115. /**
  14116. * Columns have no graph
  14117. */
  14118. drawGraph: noop,
  14119. /**
  14120. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  14121. * apply for columns and bars. This method is inherited by scatter series.
  14122. *
  14123. */
  14124. drawPoints: function () {
  14125. var series = this,
  14126. chart = this.chart,
  14127. options = series.options,
  14128. renderer = chart.renderer,
  14129. animationLimit = options.animationLimit || 250,
  14130. shapeArgs,
  14131. pointAttr;
  14132. // draw the columns
  14133. each(series.points, function (point) {
  14134. var plotY = point.plotY,
  14135. graphic = point.graphic,
  14136. borderAttr;
  14137. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  14138. shapeArgs = point.shapeArgs;
  14139. borderAttr = defined(series.borderWidth) ? {
  14140. 'stroke-width': series.borderWidth
  14141. } : {};
  14142. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || series.pointAttr[NORMAL_STATE];
  14143. if (graphic) { // update
  14144. stop(graphic);
  14145. graphic.attr(borderAttr)[chart.pointCount < animationLimit ? 'animate' : 'attr'](merge(shapeArgs));
  14146. } else {
  14147. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  14148. .attr(borderAttr)
  14149. .attr(pointAttr)
  14150. .add(series.group)
  14151. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  14152. }
  14153. } else if (graphic) {
  14154. point.graphic = graphic.destroy(); // #1269
  14155. }
  14156. });
  14157. },
  14158. /**
  14159. * Animate the column heights one by one from zero
  14160. * @param {Boolean} init Whether to initialize the animation or run it
  14161. */
  14162. animate: function (init) {
  14163. var series = this,
  14164. yAxis = this.yAxis,
  14165. options = series.options,
  14166. inverted = this.chart.inverted,
  14167. attr = {},
  14168. translatedThreshold;
  14169. if (hasSVG) { // VML is too slow anyway
  14170. if (init) {
  14171. attr.scaleY = 0.001;
  14172. translatedThreshold = mathMin(yAxis.pos + yAxis.len, mathMax(yAxis.pos, yAxis.toPixels(options.threshold)));
  14173. if (inverted) {
  14174. attr.translateX = translatedThreshold - yAxis.len;
  14175. } else {
  14176. attr.translateY = translatedThreshold;
  14177. }
  14178. series.group.attr(attr);
  14179. } else { // run the animation
  14180. attr.scaleY = 1;
  14181. attr[inverted ? 'translateX' : 'translateY'] = yAxis.pos;
  14182. series.group.animate(attr, series.options.animation);
  14183. // delete this function to allow it only once
  14184. series.animate = null;
  14185. }
  14186. }
  14187. },
  14188. /**
  14189. * Remove this series from the chart
  14190. */
  14191. remove: function () {
  14192. var series = this,
  14193. chart = series.chart;
  14194. // column and bar series affects other series of the same type
  14195. // as they are either stacked or grouped
  14196. if (chart.hasRendered) {
  14197. each(chart.series, function (otherSeries) {
  14198. if (otherSeries.type === series.type) {
  14199. otherSeries.isDirty = true;
  14200. }
  14201. });
  14202. }
  14203. Series.prototype.remove.apply(series, arguments);
  14204. }
  14205. });
  14206. seriesTypes.column = ColumnSeries;
  14207. /**
  14208. * Set the default options for bar
  14209. */
  14210. defaultPlotOptions.bar = merge(defaultPlotOptions.column);
  14211. /**
  14212. * The Bar series class
  14213. */
  14214. var BarSeries = extendClass(ColumnSeries, {
  14215. type: 'bar',
  14216. inverted: true
  14217. });
  14218. seriesTypes.bar = BarSeries;
  14219. /**
  14220. * Set the default options for scatter
  14221. */
  14222. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  14223. lineWidth: 0,
  14224. marker: {
  14225. enabled: true // Overrides auto-enabling in line series (#3647)
  14226. },
  14227. tooltip: {
  14228. headerFormat: '<span style="color:{point.color}">\u25CF</span> <span style="font-size: 10px;"> {series.name}</span><br/>',
  14229. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>'
  14230. }
  14231. });
  14232. /**
  14233. * The scatter series class
  14234. */
  14235. var ScatterSeries = extendClass(Series, {
  14236. type: 'scatter',
  14237. sorted: false,
  14238. requireSorting: false,
  14239. noSharedTooltip: true,
  14240. trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
  14241. takeOrdinalPosition: false, // #2342
  14242. kdDimensions: 2,
  14243. drawGraph: function () {
  14244. if (this.options.lineWidth) {
  14245. Series.prototype.drawGraph.call(this);
  14246. }
  14247. }
  14248. });
  14249. seriesTypes.scatter = ScatterSeries;
  14250. /**
  14251. * Set the default options for pie
  14252. */
  14253. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  14254. borderColor: '#FFFFFF',
  14255. borderWidth: 1,
  14256. center: [null, null],
  14257. clip: false,
  14258. colorByPoint: true, // always true for pies
  14259. dataLabels: {
  14260. // align: null,
  14261. // connectorWidth: 1,
  14262. // connectorColor: point.color,
  14263. // connectorPadding: 5,
  14264. distance: 30,
  14265. enabled: true,
  14266. formatter: function () { // #2945
  14267. return this.y === null ? undefined : this.point.name;
  14268. },
  14269. // softConnector: true,
  14270. x: 0
  14271. // y: 0
  14272. },
  14273. ignoreHiddenPoint: true,
  14274. //innerSize: 0,
  14275. legendType: 'point',
  14276. marker: null, // point options are specified in the base options
  14277. size: null,
  14278. showInLegend: false,
  14279. slicedOffset: 10,
  14280. states: {
  14281. hover: {
  14282. brightness: 0.1,
  14283. shadow: false
  14284. }
  14285. },
  14286. stickyTracking: false,
  14287. tooltip: {
  14288. followPointer: true
  14289. }
  14290. });
  14291. /**
  14292. * Extended point object for pies
  14293. */
  14294. var PiePoint = extendClass(Point, {
  14295. /**
  14296. * Initiate the pie slice
  14297. */
  14298. init: function () {
  14299. Point.prototype.init.apply(this, arguments);
  14300. var point = this,
  14301. toggleSlice;
  14302. extend(point, {
  14303. visible: point.visible !== false,
  14304. name: pick(point.name, 'Slice')
  14305. });
  14306. // add event listener for select
  14307. toggleSlice = function (e) {
  14308. point.slice(e.type === 'select');
  14309. };
  14310. addEvent(point, 'select', toggleSlice);
  14311. addEvent(point, 'unselect', toggleSlice);
  14312. return point;
  14313. },
  14314. /**
  14315. * Toggle the visibility of the pie slice
  14316. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  14317. * visibility is toggled
  14318. */
  14319. setVisible: function (vis, redraw) {
  14320. var point = this,
  14321. series = point.series,
  14322. chart = series.chart,
  14323. ignoreHiddenPoint = series.options.ignoreHiddenPoint;
  14324. redraw = pick(redraw, ignoreHiddenPoint);
  14325. if (vis !== point.visible) {
  14326. // If called without an argument, toggle visibility
  14327. point.visible = point.options.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  14328. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  14329. // Show and hide associated elements. This is performed regardless of redraw or not,
  14330. // because chart.redraw only handles full series.
  14331. each(['graphic', 'dataLabel', 'connector', 'shadowGroup'], function (key) {
  14332. if (point[key]) {
  14333. point[key][vis ? 'show' : 'hide'](true);
  14334. }
  14335. });
  14336. if (point.legendItem) {
  14337. chart.legend.colorizeItem(point, vis);
  14338. }
  14339. // #4170, hide halo after hiding point
  14340. if (!vis && point.state === 'hover') {
  14341. point.setState('');
  14342. }
  14343. // Handle ignore hidden slices
  14344. if (ignoreHiddenPoint) {
  14345. series.isDirty = true;
  14346. }
  14347. if (redraw) {
  14348. chart.redraw();
  14349. }
  14350. }
  14351. },
  14352. /**
  14353. * Set or toggle whether the slice is cut out from the pie
  14354. * @param {Boolean} sliced When undefined, the slice state is toggled
  14355. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  14356. */
  14357. slice: function (sliced, redraw, animation) {
  14358. var point = this,
  14359. series = point.series,
  14360. chart = series.chart,
  14361. translation;
  14362. setAnimation(animation, chart);
  14363. // redraw is true by default
  14364. redraw = pick(redraw, true);
  14365. // if called without an argument, toggle
  14366. point.sliced = point.options.sliced = sliced = defined(sliced) ? sliced : !point.sliced;
  14367. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  14368. translation = sliced ? point.slicedTranslation : {
  14369. translateX: 0,
  14370. translateY: 0
  14371. };
  14372. point.graphic.animate(translation);
  14373. if (point.shadowGroup) {
  14374. point.shadowGroup.animate(translation);
  14375. }
  14376. },
  14377. haloPath: function (size) {
  14378. var shapeArgs = this.shapeArgs,
  14379. chart = this.series.chart;
  14380. return this.sliced || !this.visible ? [] : this.series.chart.renderer.symbols.arc(chart.plotLeft + shapeArgs.x, chart.plotTop + shapeArgs.y, shapeArgs.r + size, shapeArgs.r + size, {
  14381. innerR: this.shapeArgs.r,
  14382. start: shapeArgs.start,
  14383. end: shapeArgs.end
  14384. });
  14385. }
  14386. });
  14387. /**
  14388. * The Pie series class
  14389. */
  14390. var PieSeries = {
  14391. type: 'pie',
  14392. isCartesian: false,
  14393. pointClass: PiePoint,
  14394. requireSorting: false,
  14395. directTouch: true,
  14396. noSharedTooltip: true,
  14397. trackerGroups: ['group', 'dataLabelsGroup'],
  14398. axisTypes: [],
  14399. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  14400. stroke: 'borderColor',
  14401. 'stroke-width': 'borderWidth',
  14402. fill: 'color'
  14403. },
  14404. /**
  14405. * Animate the pies in
  14406. */
  14407. animate: function (init) {
  14408. var series = this,
  14409. points = series.points,
  14410. startAngleRad = series.startAngleRad;
  14411. if (!init) {
  14412. each(points, function (point) {
  14413. var graphic = point.graphic,
  14414. args = point.shapeArgs;
  14415. if (graphic) {
  14416. // start values
  14417. graphic.attr({
  14418. r: point.startR || (series.center[3] / 2), // animate from inner radius (#779)
  14419. start: startAngleRad,
  14420. end: startAngleRad
  14421. });
  14422. // animate
  14423. graphic.animate({
  14424. r: args.r,
  14425. start: args.start,
  14426. end: args.end
  14427. }, series.options.animation);
  14428. }
  14429. });
  14430. // delete this function to allow it only once
  14431. series.animate = null;
  14432. }
  14433. },
  14434. /**
  14435. * Extend the basic setData method by running processData and generatePoints immediately,
  14436. * in order to access the points from the legend.
  14437. */
  14438. setData: function (data, redraw, animation, updatePoints) {
  14439. Series.prototype.setData.call(this, data, false, animation, updatePoints);
  14440. this.processData();
  14441. this.generatePoints();
  14442. if (pick(redraw, true)) {
  14443. this.chart.redraw(animation);
  14444. }
  14445. },
  14446. /**
  14447. * Recompute total chart sum and update percentages of points.
  14448. */
  14449. updateTotals: function () {
  14450. var i,
  14451. total = 0,
  14452. points = this.points,
  14453. len = points.length,
  14454. point,
  14455. ignoreHiddenPoint = this.options.ignoreHiddenPoint;
  14456. // Get the total sum
  14457. for (i = 0; i < len; i++) {
  14458. point = points[i];
  14459. total += (ignoreHiddenPoint && !point.visible) ? 0 : point.y;
  14460. }
  14461. this.total = total;
  14462. // Set each point's properties
  14463. for (i = 0; i < len; i++) {
  14464. point = points[i];
  14465. point.percentage = (total > 0 && (point.visible || !ignoreHiddenPoint)) ? point.y / total * 100 : 0;
  14466. point.total = total;
  14467. }
  14468. },
  14469. /**
  14470. * Extend the generatePoints method by adding total and percentage properties to each point
  14471. */
  14472. generatePoints: function () {
  14473. Series.prototype.generatePoints.call(this);
  14474. this.updateTotals();
  14475. },
  14476. /**
  14477. * Do translation for pie slices
  14478. */
  14479. translate: function (positions) {
  14480. this.generatePoints();
  14481. var series = this,
  14482. cumulative = 0,
  14483. precision = 1000, // issue #172
  14484. options = series.options,
  14485. slicedOffset = options.slicedOffset,
  14486. connectorOffset = slicedOffset + options.borderWidth,
  14487. start,
  14488. end,
  14489. angle,
  14490. startAngle = options.startAngle || 0,
  14491. startAngleRad = series.startAngleRad = mathPI / 180 * (startAngle - 90),
  14492. endAngleRad = series.endAngleRad = mathPI / 180 * ((pick(options.endAngle, startAngle + 360)) - 90),
  14493. circ = endAngleRad - startAngleRad, //2 * mathPI,
  14494. points = series.points,
  14495. radiusX, // the x component of the radius vector for a given point
  14496. radiusY,
  14497. labelDistance = options.dataLabels.distance,
  14498. ignoreHiddenPoint = options.ignoreHiddenPoint,
  14499. i,
  14500. len = points.length,
  14501. point;
  14502. // Get positions - either an integer or a percentage string must be given.
  14503. // If positions are passed as a parameter, we're in a recursive loop for adjusting
  14504. // space for data labels.
  14505. if (!positions) {
  14506. series.center = positions = series.getCenter();
  14507. }
  14508. // utility for getting the x value from a given y, used for anticollision logic in data labels
  14509. series.getX = function (y, left) {
  14510. angle = math.asin(mathMin((y - positions[1]) / (positions[2] / 2 + labelDistance), 1));
  14511. return positions[0] +
  14512. (left ? -1 : 1) *
  14513. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  14514. };
  14515. // Calculate the geometry for each point
  14516. for (i = 0; i < len; i++) {
  14517. point = points[i];
  14518. // set start and end angle
  14519. start = startAngleRad + (cumulative * circ);
  14520. if (!ignoreHiddenPoint || point.visible) {
  14521. cumulative += point.percentage / 100;
  14522. }
  14523. end = startAngleRad + (cumulative * circ);
  14524. // set the shape
  14525. point.shapeType = 'arc';
  14526. point.shapeArgs = {
  14527. x: positions[0],
  14528. y: positions[1],
  14529. r: positions[2] / 2,
  14530. innerR: positions[3] / 2,
  14531. start: mathRound(start * precision) / precision,
  14532. end: mathRound(end * precision) / precision
  14533. };
  14534. // The angle must stay within -90 and 270 (#2645)
  14535. angle = (end + start) / 2;
  14536. if (angle > 1.5 * mathPI) {
  14537. angle -= 2 * mathPI;
  14538. } else if (angle < -mathPI / 2) {
  14539. angle += 2 * mathPI;
  14540. }
  14541. // Center for the sliced out slice
  14542. point.slicedTranslation = {
  14543. translateX: mathRound(mathCos(angle) * slicedOffset),
  14544. translateY: mathRound(mathSin(angle) * slicedOffset)
  14545. };
  14546. // set the anchor point for tooltips
  14547. radiusX = mathCos(angle) * positions[2] / 2;
  14548. radiusY = mathSin(angle) * positions[2] / 2;
  14549. point.tooltipPos = [
  14550. positions[0] + radiusX * 0.7,
  14551. positions[1] + radiusY * 0.7
  14552. ];
  14553. point.half = angle < -mathPI / 2 || angle > mathPI / 2 ? 1 : 0;
  14554. point.angle = angle;
  14555. // set the anchor point for data labels
  14556. connectorOffset = mathMin(connectorOffset, labelDistance / 2); // #1678
  14557. point.labelPos = [
  14558. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  14559. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  14560. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  14561. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  14562. positions[0] + radiusX, // landing point for connector
  14563. positions[1] + radiusY, // a/a
  14564. labelDistance < 0 ? // alignment
  14565. 'center' :
  14566. point.half ? 'right' : 'left', // alignment
  14567. angle // center angle
  14568. ];
  14569. }
  14570. },
  14571. drawGraph: null,
  14572. /**
  14573. * Draw the data points
  14574. */
  14575. drawPoints: function () {
  14576. var series = this,
  14577. chart = series.chart,
  14578. renderer = chart.renderer,
  14579. groupTranslation,
  14580. //center,
  14581. graphic,
  14582. //group,
  14583. shadow = series.options.shadow,
  14584. shadowGroup,
  14585. shapeArgs,
  14586. attr;
  14587. if (shadow && !series.shadowGroup) {
  14588. series.shadowGroup = renderer.g('shadow')
  14589. .add(series.group);
  14590. }
  14591. // draw the slices
  14592. each(series.points, function (point) {
  14593. if (point.y !== null) {
  14594. graphic = point.graphic;
  14595. shapeArgs = point.shapeArgs;
  14596. shadowGroup = point.shadowGroup;
  14597. // put the shadow behind all points
  14598. if (shadow && !shadowGroup) {
  14599. shadowGroup = point.shadowGroup = renderer.g('shadow')
  14600. .add(series.shadowGroup);
  14601. }
  14602. // if the point is sliced, use special translation, else use plot area traslation
  14603. groupTranslation = point.sliced ? point.slicedTranslation : {
  14604. translateX: 0,
  14605. translateY: 0
  14606. };
  14607. //group.translate(groupTranslation[0], groupTranslation[1]);
  14608. if (shadowGroup) {
  14609. shadowGroup.attr(groupTranslation);
  14610. }
  14611. // draw the slice
  14612. if (graphic) {
  14613. graphic.animate(extend(shapeArgs, groupTranslation));
  14614. } else {
  14615. attr = { 'stroke-linejoin': 'round' };
  14616. if (!point.visible) {
  14617. attr.visibility = 'hidden';
  14618. }
  14619. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  14620. .setRadialReference(series.center)
  14621. .attr(
  14622. point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE]
  14623. )
  14624. .attr(attr)
  14625. .attr(groupTranslation)
  14626. .add(series.group)
  14627. .shadow(shadow, shadowGroup);
  14628. }
  14629. }
  14630. });
  14631. },
  14632. searchPoint: noop,
  14633. /**
  14634. * Utility for sorting data labels
  14635. */
  14636. sortByAngle: function (points, sign) {
  14637. points.sort(function (a, b) {
  14638. return a.angle !== undefined && (b.angle - a.angle) * sign;
  14639. });
  14640. },
  14641. /**
  14642. * Use a simple symbol from LegendSymbolMixin
  14643. */
  14644. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  14645. /**
  14646. * Use the getCenter method from drawLegendSymbol
  14647. */
  14648. getCenter: CenteredSeriesMixin.getCenter,
  14649. /**
  14650. * Pies don't have point marker symbols
  14651. */
  14652. getSymbol: noop
  14653. };
  14654. PieSeries = extendClass(Series, PieSeries);
  14655. seriesTypes.pie = PieSeries;
  14656. /**
  14657. * Draw the data labels
  14658. */
  14659. Series.prototype.drawDataLabels = function () {
  14660. var series = this,
  14661. seriesOptions = series.options,
  14662. cursor = seriesOptions.cursor,
  14663. options = seriesOptions.dataLabels,
  14664. points = series.points,
  14665. pointOptions,
  14666. generalOptions,
  14667. hasRendered = series.hasRendered || 0,
  14668. str,
  14669. dataLabelsGroup,
  14670. renderer = series.chart.renderer;
  14671. if (options.enabled || series._hasPointLabels) {
  14672. // Process default alignment of data labels for columns
  14673. if (series.dlProcessOptions) {
  14674. series.dlProcessOptions(options);
  14675. }
  14676. // Create a separate group for the data labels to avoid rotation
  14677. dataLabelsGroup = series.plotGroup(
  14678. 'dataLabelsGroup',
  14679. 'data-labels',
  14680. options.defer ? HIDDEN : VISIBLE,
  14681. options.zIndex || 6
  14682. );
  14683. if (pick(options.defer, true)) {
  14684. dataLabelsGroup.attr({ opacity: +hasRendered }); // #3300
  14685. if (!hasRendered) {
  14686. addEvent(series, 'afterAnimate', function () {
  14687. if (series.visible) { // #3023, #3024
  14688. dataLabelsGroup.show();
  14689. }
  14690. dataLabelsGroup[seriesOptions.animation ? 'animate' : 'attr']({ opacity: 1 }, { duration: 200 });
  14691. });
  14692. }
  14693. }
  14694. // Make the labels for each point
  14695. generalOptions = options;
  14696. each(points, function (point) {
  14697. var enabled,
  14698. dataLabel = point.dataLabel,
  14699. labelConfig,
  14700. attr,
  14701. name,
  14702. rotation,
  14703. connector = point.connector,
  14704. isNew = true,
  14705. style,
  14706. moreStyle = {};
  14707. // Determine if each data label is enabled
  14708. pointOptions = point.dlOptions || (point.options && point.options.dataLabels); // dlOptions is used in treemaps
  14709. enabled = pick(pointOptions && pointOptions.enabled, generalOptions.enabled); // #2282
  14710. // If the point is outside the plot area, destroy it. #678, #820
  14711. if (dataLabel && !enabled) {
  14712. point.dataLabel = dataLabel.destroy();
  14713. // Individual labels are disabled if the are explicitly disabled
  14714. // in the point options, or if they fall outside the plot area.
  14715. } else if (enabled) {
  14716. // Create individual options structure that can be extended without
  14717. // affecting others
  14718. options = merge(generalOptions, pointOptions);
  14719. style = options.style;
  14720. rotation = options.rotation;
  14721. // Get the string
  14722. labelConfig = point.getLabelConfig();
  14723. str = options.format ?
  14724. format(options.format, labelConfig) :
  14725. options.formatter.call(labelConfig, options);
  14726. // Determine the color
  14727. style.color = pick(options.color, style.color, series.color, 'black');
  14728. // update existing label
  14729. if (dataLabel) {
  14730. if (defined(str)) {
  14731. dataLabel
  14732. .attr({
  14733. text: str
  14734. });
  14735. isNew = false;
  14736. } else { // #1437 - the label is shown conditionally
  14737. point.dataLabel = dataLabel = dataLabel.destroy();
  14738. if (connector) {
  14739. point.connector = connector.destroy();
  14740. }
  14741. }
  14742. // create new label
  14743. } else if (defined(str)) {
  14744. attr = {
  14745. //align: align,
  14746. fill: options.backgroundColor,
  14747. stroke: options.borderColor,
  14748. 'stroke-width': options.borderWidth,
  14749. r: options.borderRadius || 0,
  14750. rotation: rotation,
  14751. padding: options.padding,
  14752. zIndex: 1
  14753. };
  14754. // Get automated contrast color
  14755. if (style.color === 'contrast') {
  14756. moreStyle.color = options.inside || options.distance < 0 || !!seriesOptions.stacking ?
  14757. renderer.getContrast(point.color || series.color) :
  14758. '#000000';
  14759. }
  14760. if (cursor) {
  14761. moreStyle.cursor = cursor;
  14762. }
  14763. // Remove unused attributes (#947)
  14764. for (name in attr) {
  14765. if (attr[name] === UNDEFINED) {
  14766. delete attr[name];
  14767. }
  14768. }
  14769. dataLabel = point.dataLabel = renderer[rotation ? 'text' : 'label']( // labels don't support rotation
  14770. str,
  14771. 0,
  14772. -999,
  14773. options.shape,
  14774. null,
  14775. null,
  14776. options.useHTML
  14777. )
  14778. .attr(attr)
  14779. .css(extend(style, moreStyle))
  14780. .add(dataLabelsGroup)
  14781. .shadow(options.shadow);
  14782. }
  14783. if (dataLabel) {
  14784. // Now the data label is created and placed at 0,0, so we need to align it
  14785. series.alignDataLabel(point, dataLabel, options, null, isNew);
  14786. }
  14787. }
  14788. });
  14789. }
  14790. };
  14791. /**
  14792. * Align each individual data label
  14793. */
  14794. Series.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  14795. var chart = this.chart,
  14796. inverted = chart.inverted,
  14797. plotX = pick(point.plotX, -999),
  14798. plotY = pick(point.plotY, -999),
  14799. bBox = dataLabel.getBBox(),
  14800. baseline = chart.renderer.fontMetrics(options.style.fontSize).b,
  14801. rotCorr, // rotation correction
  14802. // Math.round for rounding errors (#2683), alignTo to allow column labels (#2700)
  14803. visible = this.visible && (point.series.forceDL || chart.isInsidePlot(plotX, mathRound(plotY), inverted) ||
  14804. (alignTo && chart.isInsidePlot(plotX, inverted ? alignTo.x + 1 : alignTo.y + alignTo.height - 1, inverted))),
  14805. alignAttr; // the final position;
  14806. if (visible) {
  14807. // The alignment box is a singular point
  14808. alignTo = extend({
  14809. x: inverted ? chart.plotWidth - plotY : plotX,
  14810. y: mathRound(inverted ? chart.plotHeight - plotX : plotY),
  14811. width: 0,
  14812. height: 0
  14813. }, alignTo);
  14814. // Add the text size for alignment calculation
  14815. extend(options, {
  14816. width: bBox.width,
  14817. height: bBox.height
  14818. });
  14819. // Allow a hook for changing alignment in the last moment, then do the alignment
  14820. if (options.rotation) { // Fancy box alignment isn't supported for rotated text
  14821. rotCorr = chart.renderer.rotCorr(baseline, options.rotation); // #3723
  14822. dataLabel[isNew ? 'attr' : 'animate']({
  14823. x: alignTo.x + options.x + alignTo.width / 2 + rotCorr.x,
  14824. y: alignTo.y + options.y + alignTo.height / 2
  14825. })
  14826. .attr({ // #3003
  14827. align: options.align
  14828. });
  14829. } else {
  14830. dataLabel.align(options, null, alignTo);
  14831. alignAttr = dataLabel.alignAttr;
  14832. // Handle justify or crop
  14833. if (pick(options.overflow, 'justify') === 'justify') {
  14834. this.justifyDataLabel(dataLabel, options, alignAttr, bBox, alignTo, isNew);
  14835. } else if (pick(options.crop, true)) {
  14836. // Now check that the data label is within the plot area
  14837. visible = chart.isInsidePlot(alignAttr.x, alignAttr.y) && chart.isInsidePlot(alignAttr.x + bBox.width, alignAttr.y + bBox.height);
  14838. }
  14839. // When we're using a shape, make it possible with a connector or an arrow pointing to thie point
  14840. if (options.shape) {
  14841. dataLabel.attr({
  14842. anchorX: point.plotX,
  14843. anchorY: point.plotY
  14844. });
  14845. }
  14846. }
  14847. }
  14848. // Show or hide based on the final aligned position
  14849. if (!visible) {
  14850. dataLabel.attr({ y: -999 });
  14851. dataLabel.placed = false; // don't animate back in
  14852. }
  14853. };
  14854. /**
  14855. * If data labels fall partly outside the plot area, align them back in, in a way that
  14856. * doesn't hide the point.
  14857. */
  14858. Series.prototype.justifyDataLabel = function (dataLabel, options, alignAttr, bBox, alignTo, isNew) {
  14859. var chart = this.chart,
  14860. align = options.align,
  14861. verticalAlign = options.verticalAlign,
  14862. off,
  14863. justified,
  14864. padding = dataLabel.box ? 0 : (dataLabel.padding || 0);
  14865. // Off left
  14866. off = alignAttr.x + padding;
  14867. if (off < 0) {
  14868. if (align === 'right') {
  14869. options.align = 'left';
  14870. } else {
  14871. options.x = -off;
  14872. }
  14873. justified = true;
  14874. }
  14875. // Off right
  14876. off = alignAttr.x + bBox.width - padding;
  14877. if (off > chart.plotWidth) {
  14878. if (align === 'left') {
  14879. options.align = 'right';
  14880. } else {
  14881. options.x = chart.plotWidth - off;
  14882. }
  14883. justified = true;
  14884. }
  14885. // Off top
  14886. off = alignAttr.y + padding;
  14887. if (off < 0) {
  14888. if (verticalAlign === 'bottom') {
  14889. options.verticalAlign = 'top';
  14890. } else {
  14891. options.y = -off;
  14892. }
  14893. justified = true;
  14894. }
  14895. // Off bottom
  14896. off = alignAttr.y + bBox.height - padding;
  14897. if (off > chart.plotHeight) {
  14898. if (verticalAlign === 'top') {
  14899. options.verticalAlign = 'bottom';
  14900. } else {
  14901. options.y = chart.plotHeight - off;
  14902. }
  14903. justified = true;
  14904. }
  14905. if (justified) {
  14906. dataLabel.placed = !isNew;
  14907. dataLabel.align(options, null, alignTo);
  14908. }
  14909. };
  14910. /**
  14911. * Override the base drawDataLabels method by pie specific functionality
  14912. */
  14913. if (seriesTypes.pie) {
  14914. seriesTypes.pie.prototype.drawDataLabels = function () {
  14915. var series = this,
  14916. data = series.data,
  14917. point,
  14918. chart = series.chart,
  14919. options = series.options.dataLabels,
  14920. connectorPadding = pick(options.connectorPadding, 10),
  14921. connectorWidth = pick(options.connectorWidth, 1),
  14922. plotWidth = chart.plotWidth,
  14923. plotHeight = chart.plotHeight,
  14924. connector,
  14925. connectorPath,
  14926. softConnector = pick(options.softConnector, true),
  14927. distanceOption = options.distance,
  14928. seriesCenter = series.center,
  14929. radius = seriesCenter[2] / 2,
  14930. centerY = seriesCenter[1],
  14931. outside = distanceOption > 0,
  14932. dataLabel,
  14933. dataLabelWidth,
  14934. labelPos,
  14935. labelHeight,
  14936. halves = [// divide the points into right and left halves for anti collision
  14937. [], // right
  14938. [] // left
  14939. ],
  14940. x,
  14941. y,
  14942. visibility,
  14943. rankArr,
  14944. i,
  14945. j,
  14946. overflow = [0, 0, 0, 0], // top, right, bottom, left
  14947. sort = function (a, b) {
  14948. return b.y - a.y;
  14949. };
  14950. // get out if not enabled
  14951. if (!series.visible || (!options.enabled && !series._hasPointLabels)) {
  14952. return;
  14953. }
  14954. // run parent method
  14955. Series.prototype.drawDataLabels.apply(series);
  14956. // arrange points for detection collision
  14957. each(data, function (point) {
  14958. if (point.dataLabel && point.visible) { // #407, #2510
  14959. halves[point.half].push(point);
  14960. }
  14961. });
  14962. /* Loop over the points in each half, starting from the top and bottom
  14963. * of the pie to detect overlapping labels.
  14964. */
  14965. i = 2;
  14966. while (i--) {
  14967. var slots = [],
  14968. slotsLength,
  14969. usedSlots = [],
  14970. points = halves[i],
  14971. pos,
  14972. bottom,
  14973. length = points.length,
  14974. slotIndex;
  14975. if (!length) {
  14976. continue;
  14977. }
  14978. // Sort by angle
  14979. series.sortByAngle(points, i - 0.5);
  14980. // Assume equal label heights on either hemisphere (#2630)
  14981. j = labelHeight = 0;
  14982. while (!labelHeight && points[j]) { // #1569
  14983. labelHeight = points[j] && points[j].dataLabel && (points[j].dataLabel.getBBox().height || 21); // 21 is for #968
  14984. j++;
  14985. }
  14986. // Only do anti-collision when we are outside the pie and have connectors (#856)
  14987. if (distanceOption > 0) {
  14988. // Build the slots
  14989. bottom = mathMin(centerY + radius + distanceOption, chart.plotHeight);
  14990. for (pos = mathMax(0, centerY - radius - distanceOption); pos <= bottom; pos += labelHeight) {
  14991. slots.push(pos);
  14992. }
  14993. slotsLength = slots.length;
  14994. /* Visualize the slots
  14995. if (!series.slotElements) {
  14996. series.slotElements = [];
  14997. }
  14998. if (i === 1) {
  14999. series.slotElements.forEach(function (elem) {
  15000. elem.destroy();
  15001. });
  15002. series.slotElements.length = 0;
  15003. }
  15004. slots.forEach(function (pos, no) {
  15005. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  15006. slotY = pos + chart.plotTop;
  15007. if (!isNaN(slotX)) {
  15008. series.slotElements.push(chart.renderer.rect(slotX, slotY - 7, 100, labelHeight, 1)
  15009. .attr({
  15010. 'stroke-width': 1,
  15011. stroke: 'silver',
  15012. fill: 'rgba(0,0,255,0.1)'
  15013. })
  15014. .add());
  15015. series.slotElements.push(chart.renderer.text('Slot '+ no, slotX, slotY + 4)
  15016. .attr({
  15017. fill: 'silver'
  15018. }).add());
  15019. }
  15020. });
  15021. // */
  15022. // if there are more values than available slots, remove lowest values
  15023. if (length > slotsLength) {
  15024. // create an array for sorting and ranking the points within each quarter
  15025. rankArr = [].concat(points);
  15026. rankArr.sort(sort);
  15027. j = length;
  15028. while (j--) {
  15029. rankArr[j].rank = j;
  15030. }
  15031. j = length;
  15032. while (j--) {
  15033. if (points[j].rank >= slotsLength) {
  15034. points.splice(j, 1);
  15035. }
  15036. }
  15037. length = points.length;
  15038. }
  15039. // The label goes to the nearest open slot, but not closer to the edge than
  15040. // the label's index.
  15041. for (j = 0; j < length; j++) {
  15042. point = points[j];
  15043. labelPos = point.labelPos;
  15044. var closest = 9999,
  15045. distance,
  15046. slotI;
  15047. // find the closest slot index
  15048. for (slotI = 0; slotI < slotsLength; slotI++) {
  15049. distance = mathAbs(slots[slotI] - labelPos[1]);
  15050. if (distance < closest) {
  15051. closest = distance;
  15052. slotIndex = slotI;
  15053. }
  15054. }
  15055. // if that slot index is closer to the edges of the slots, move it
  15056. // to the closest appropriate slot
  15057. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  15058. slotIndex = j;
  15059. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  15060. slotIndex = slotsLength - length + j;
  15061. while (slots[slotIndex] === null) { // make sure it is not taken
  15062. slotIndex++;
  15063. }
  15064. } else {
  15065. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  15066. // slot above these, because it is the closest one
  15067. while (slots[slotIndex] === null) { // make sure it is not taken
  15068. slotIndex++;
  15069. }
  15070. }
  15071. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  15072. slots[slotIndex] = null; // mark as taken
  15073. }
  15074. // sort them in order to fill in from the top
  15075. usedSlots.sort(sort);
  15076. }
  15077. // now the used slots are sorted, fill them up sequentially
  15078. for (j = 0; j < length; j++) {
  15079. var slot, naturalY;
  15080. point = points[j];
  15081. labelPos = point.labelPos;
  15082. dataLabel = point.dataLabel;
  15083. visibility = point.visible === false ? HIDDEN : 'inherit';
  15084. naturalY = labelPos[1];
  15085. if (distanceOption > 0) {
  15086. slot = usedSlots.pop();
  15087. slotIndex = slot.i;
  15088. // if the slot next to currrent slot is free, the y value is allowed
  15089. // to fall back to the natural position
  15090. y = slot.y;
  15091. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  15092. (naturalY < y && slots[slotIndex - 1] !== null)) {
  15093. y = mathMin(mathMax(0, naturalY), chart.plotHeight);
  15094. }
  15095. } else {
  15096. y = naturalY;
  15097. }
  15098. // get the x - use the natural x position for first and last slot, to prevent the top
  15099. // and botton slice connectors from touching each other on either side
  15100. x = options.justify ?
  15101. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  15102. series.getX(y === centerY - radius - distanceOption || y === centerY + radius + distanceOption ? naturalY : y, i);
  15103. // Record the placement and visibility
  15104. dataLabel._attr = {
  15105. visibility: visibility,
  15106. align: labelPos[6]
  15107. };
  15108. dataLabel._pos = {
  15109. x: x + options.x +
  15110. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  15111. y: y + options.y - 10 // 10 is for the baseline (label vs text)
  15112. };
  15113. dataLabel.connX = x;
  15114. dataLabel.connY = y;
  15115. // Detect overflowing data labels
  15116. if (this.options.size === null) {
  15117. dataLabelWidth = dataLabel.width;
  15118. // Overflow left
  15119. if (x - dataLabelWidth < connectorPadding) {
  15120. overflow[3] = mathMax(mathRound(dataLabelWidth - x + connectorPadding), overflow[3]);
  15121. // Overflow right
  15122. } else if (x + dataLabelWidth > plotWidth - connectorPadding) {
  15123. overflow[1] = mathMax(mathRound(x + dataLabelWidth - plotWidth + connectorPadding), overflow[1]);
  15124. }
  15125. // Overflow top
  15126. if (y - labelHeight / 2 < 0) {
  15127. overflow[0] = mathMax(mathRound(-y + labelHeight / 2), overflow[0]);
  15128. // Overflow left
  15129. } else if (y + labelHeight / 2 > plotHeight) {
  15130. overflow[2] = mathMax(mathRound(y + labelHeight / 2 - plotHeight), overflow[2]);
  15131. }
  15132. }
  15133. } // for each point
  15134. } // for each half
  15135. // Do not apply the final placement and draw the connectors until we have verified
  15136. // that labels are not spilling over.
  15137. if (arrayMax(overflow) === 0 || this.verifyDataLabelOverflow(overflow)) {
  15138. // Place the labels in the final position
  15139. this.placeDataLabels();
  15140. // Draw the connectors
  15141. if (outside && connectorWidth) {
  15142. each(this.points, function (point) {
  15143. connector = point.connector;
  15144. labelPos = point.labelPos;
  15145. dataLabel = point.dataLabel;
  15146. if (dataLabel && dataLabel._pos && point.visible) {
  15147. visibility = dataLabel._attr.visibility;
  15148. x = dataLabel.connX;
  15149. y = dataLabel.connY;
  15150. connectorPath = softConnector ? [
  15151. M,
  15152. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  15153. 'C',
  15154. x, y, // first break, next to the label
  15155. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  15156. labelPos[2], labelPos[3], // second break
  15157. L,
  15158. labelPos[4], labelPos[5] // base
  15159. ] : [
  15160. M,
  15161. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  15162. L,
  15163. labelPos[2], labelPos[3], // second break
  15164. L,
  15165. labelPos[4], labelPos[5] // base
  15166. ];
  15167. if (connector) {
  15168. connector.animate({ d: connectorPath });
  15169. connector.attr('visibility', visibility);
  15170. } else {
  15171. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  15172. 'stroke-width': connectorWidth,
  15173. stroke: options.connectorColor || point.color || '#606060',
  15174. visibility: visibility
  15175. //zIndex: 0 // #2722 (reversed)
  15176. })
  15177. .add(series.dataLabelsGroup);
  15178. }
  15179. } else if (connector) {
  15180. point.connector = connector.destroy();
  15181. }
  15182. });
  15183. }
  15184. }
  15185. };
  15186. /**
  15187. * Perform the final placement of the data labels after we have verified that they
  15188. * fall within the plot area.
  15189. */
  15190. seriesTypes.pie.prototype.placeDataLabels = function () {
  15191. each(this.points, function (point) {
  15192. var dataLabel = point.dataLabel,
  15193. _pos;
  15194. if (dataLabel && point.visible) {
  15195. _pos = dataLabel._pos;
  15196. if (_pos) {
  15197. dataLabel.attr(dataLabel._attr);
  15198. dataLabel[dataLabel.moved ? 'animate' : 'attr'](_pos);
  15199. dataLabel.moved = true;
  15200. } else if (dataLabel) {
  15201. dataLabel.attr({ y: -999 });
  15202. }
  15203. }
  15204. });
  15205. };
  15206. seriesTypes.pie.prototype.alignDataLabel = noop;
  15207. /**
  15208. * Verify whether the data labels are allowed to draw, or we should run more translation and data
  15209. * label positioning to keep them inside the plot area. Returns true when data labels are ready
  15210. * to draw.
  15211. */
  15212. seriesTypes.pie.prototype.verifyDataLabelOverflow = function (overflow) {
  15213. var center = this.center,
  15214. options = this.options,
  15215. centerOption = options.center,
  15216. minSize = options.minSize || 80,
  15217. newSize = minSize,
  15218. ret;
  15219. // Handle horizontal size and center
  15220. if (centerOption[0] !== null) { // Fixed center
  15221. newSize = mathMax(center[2] - mathMax(overflow[1], overflow[3]), minSize);
  15222. } else { // Auto center
  15223. newSize = mathMax(
  15224. center[2] - overflow[1] - overflow[3], // horizontal overflow
  15225. minSize
  15226. );
  15227. center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
  15228. }
  15229. // Handle vertical size and center
  15230. if (centerOption[1] !== null) { // Fixed center
  15231. newSize = mathMax(mathMin(newSize, center[2] - mathMax(overflow[0], overflow[2])), minSize);
  15232. } else { // Auto center
  15233. newSize = mathMax(
  15234. mathMin(
  15235. newSize,
  15236. center[2] - overflow[0] - overflow[2] // vertical overflow
  15237. ),
  15238. minSize
  15239. );
  15240. center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
  15241. }
  15242. // If the size must be decreased, we need to run translate and drawDataLabels again
  15243. if (newSize < center[2]) {
  15244. center[2] = newSize;
  15245. center[3] = relativeLength(options.innerSize || 0, newSize);
  15246. this.translate(center);
  15247. each(this.points, function (point) {
  15248. if (point.dataLabel) {
  15249. point.dataLabel._pos = null; // reset
  15250. }
  15251. });
  15252. if (this.drawDataLabels) {
  15253. this.drawDataLabels();
  15254. }
  15255. // Else, return true to indicate that the pie and its labels is within the plot area
  15256. } else {
  15257. ret = true;
  15258. }
  15259. return ret;
  15260. };
  15261. }
  15262. if (seriesTypes.column) {
  15263. /**
  15264. * Override the basic data label alignment by adjusting for the position of the column
  15265. */
  15266. seriesTypes.column.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  15267. var inverted = this.chart.inverted,
  15268. series = point.series,
  15269. dlBox = point.dlBox || point.shapeArgs, // data label box for alignment
  15270. below = pick(point.below, point.plotY > pick(this.translatedThreshold, series.yAxis.len)), // point.below is used in range series
  15271. inside = pick(options.inside, !!this.options.stacking); // draw it inside the box?
  15272. // Align to the column itself, or the top of it
  15273. if (dlBox) { // Area range uses this method but not alignTo
  15274. alignTo = merge(dlBox);
  15275. if (inverted) {
  15276. alignTo = {
  15277. x: series.yAxis.len - alignTo.y - alignTo.height,
  15278. y: series.xAxis.len - alignTo.x - alignTo.width,
  15279. width: alignTo.height,
  15280. height: alignTo.width
  15281. };
  15282. }
  15283. // Compute the alignment box
  15284. if (!inside) {
  15285. if (inverted) {
  15286. alignTo.x += below ? 0 : alignTo.width;
  15287. alignTo.width = 0;
  15288. } else {
  15289. alignTo.y += below ? alignTo.height : 0;
  15290. alignTo.height = 0;
  15291. }
  15292. }
  15293. }
  15294. // When alignment is undefined (typically columns and bars), display the individual
  15295. // point below or above the point depending on the threshold
  15296. options.align = pick(
  15297. options.align,
  15298. !inverted || inside ? 'center' : below ? 'right' : 'left'
  15299. );
  15300. options.verticalAlign = pick(
  15301. options.verticalAlign,
  15302. inverted || inside ? 'middle' : below ? 'top' : 'bottom'
  15303. );
  15304. // Call the parent method
  15305. Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  15306. };
  15307. }
  15308. /**
  15309. * Highcharts JS v4.1.8 (2015-08-20)
  15310. * Highcharts module to hide overlapping data labels. This module is included by default in Highmaps.
  15311. *
  15312. * (c) 2010-2014 Torstein Honsi
  15313. *
  15314. * License: www.highcharts.com/license
  15315. */
  15316. /*global Highcharts, HighchartsAdapter */
  15317. (function (H) {
  15318. var Chart = H.Chart,
  15319. each = H.each,
  15320. pick = H.pick,
  15321. addEvent = HighchartsAdapter.addEvent;
  15322. // Collect potensial overlapping data labels. Stack labels probably don't need to be
  15323. // considered because they are usually accompanied by data labels that lie inside the columns.
  15324. Chart.prototype.callbacks.push(function (chart) {
  15325. function collectAndHide() {
  15326. var labels = [];
  15327. each(chart.series, function (series) {
  15328. var dlOptions = series.options.dataLabels,
  15329. collections = series.dataLabelCollections || ['dataLabel']; // Range series have two collections
  15330. if ((dlOptions.enabled || series._hasPointLabels) && !dlOptions.allowOverlap && series.visible) { // #3866
  15331. each(collections, function (coll) {
  15332. each(series.points, function (point) {
  15333. if (point[coll]) {
  15334. point[coll].labelrank = pick(point.labelrank, point.shapeArgs && point.shapeArgs.height); // #4118
  15335. labels.push(point[coll]);
  15336. }
  15337. });
  15338. });
  15339. }
  15340. });
  15341. chart.hideOverlappingLabels(labels);
  15342. }
  15343. // Do it now ...
  15344. collectAndHide();
  15345. // ... and after each chart redraw
  15346. addEvent(chart, 'redraw', collectAndHide);
  15347. });
  15348. /**
  15349. * Hide overlapping labels. Labels are moved and faded in and out on zoom to provide a smooth
  15350. * visual imression.
  15351. */
  15352. Chart.prototype.hideOverlappingLabels = function (labels) {
  15353. var len = labels.length,
  15354. label,
  15355. i,
  15356. j,
  15357. label1,
  15358. label2,
  15359. isIntersecting,
  15360. pos1,
  15361. pos2,
  15362. padding,
  15363. intersectRect = function (x1, y1, w1, h1, x2, y2, w2, h2) {
  15364. return !(
  15365. x2 > x1 + w1 ||
  15366. x2 + w2 < x1 ||
  15367. y2 > y1 + h1 ||
  15368. y2 + h2 < y1
  15369. );
  15370. };
  15371. // Mark with initial opacity
  15372. for (i = 0; i < len; i++) {
  15373. label = labels[i];
  15374. if (label) {
  15375. label.oldOpacity = label.opacity;
  15376. label.newOpacity = 1;
  15377. }
  15378. }
  15379. // Prevent a situation in a gradually rising slope, that each label
  15380. // will hide the previous one because the previous one always has
  15381. // lower rank.
  15382. labels.sort(function (a, b) {
  15383. return (b.labelrank || 0) - (a.labelrank || 0);
  15384. });
  15385. // Detect overlapping labels
  15386. for (i = 0; i < len; i++) {
  15387. label1 = labels[i];
  15388. for (j = i + 1; j < len; ++j) {
  15389. label2 = labels[j];
  15390. if (label1 && label2 && label1.placed && label2.placed && label1.newOpacity !== 0 && label2.newOpacity !== 0) {
  15391. pos1 = label1.alignAttr;
  15392. pos2 = label2.alignAttr;
  15393. padding = 2 * (label1.box ? 0 : label1.padding); // Substract the padding if no background or border (#4333)
  15394. isIntersecting = intersectRect(
  15395. pos1.x,
  15396. pos1.y,
  15397. label1.width - padding,
  15398. label1.height - padding,
  15399. pos2.x,
  15400. pos2.y,
  15401. label2.width - padding,
  15402. label2.height - padding
  15403. );
  15404. if (isIntersecting) {
  15405. (label1.labelrank < label2.labelrank ? label1 : label2).newOpacity = 0;
  15406. }
  15407. }
  15408. }
  15409. }
  15410. // Hide or show
  15411. each(labels, function (label) {
  15412. var complete,
  15413. newOpacity;
  15414. if (label) {
  15415. newOpacity = label.newOpacity;
  15416. if (label.oldOpacity !== newOpacity && label.placed) {
  15417. // Make sure the label is completely hidden to avoid catching clicks (#4362)
  15418. if (newOpacity) {
  15419. label.show(true);
  15420. } else {
  15421. complete = function () {
  15422. label.hide();
  15423. };
  15424. }
  15425. // Animate or set the opacity
  15426. label.alignAttr.opacity = newOpacity;
  15427. label[label.isOld ? 'animate' : 'attr'](label.alignAttr, null, complete);
  15428. }
  15429. label.isOld = true;
  15430. }
  15431. });
  15432. };
  15433. }(Highcharts));/**
  15434. * TrackerMixin for points and graphs
  15435. */
  15436. var TrackerMixin = Highcharts.TrackerMixin = {
  15437. drawTrackerPoint: function () {
  15438. var series = this,
  15439. chart = series.chart,
  15440. pointer = chart.pointer,
  15441. cursor = series.options.cursor,
  15442. css = cursor && { cursor: cursor },
  15443. onMouseOver = function (e) {
  15444. var target = e.target,
  15445. point;
  15446. while (target && !point) {
  15447. point = target.point;
  15448. target = target.parentNode;
  15449. }
  15450. if (point !== UNDEFINED && point !== chart.hoverPoint) { // undefined on graph in scatterchart
  15451. point.onMouseOver(e);
  15452. }
  15453. };
  15454. // Add reference to the point
  15455. each(series.points, function (point) {
  15456. if (point.graphic) {
  15457. point.graphic.element.point = point;
  15458. }
  15459. if (point.dataLabel) {
  15460. point.dataLabel.element.point = point;
  15461. }
  15462. });
  15463. // Add the event listeners, we need to do this only once
  15464. if (!series._hasTracking) {
  15465. each(series.trackerGroups, function (key) {
  15466. if (series[key]) { // we don't always have dataLabelsGroup
  15467. series[key]
  15468. .addClass(PREFIX + 'tracker')
  15469. .on('mouseover', onMouseOver)
  15470. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  15471. .css(css);
  15472. if (hasTouch) {
  15473. series[key].on('touchstart', onMouseOver);
  15474. }
  15475. }
  15476. });
  15477. series._hasTracking = true;
  15478. }
  15479. },
  15480. /**
  15481. * Draw the tracker object that sits above all data labels and markers to
  15482. * track mouse events on the graph or points. For the line type charts
  15483. * the tracker uses the same graphPath, but with a greater stroke width
  15484. * for better control.
  15485. */
  15486. drawTrackerGraph: function () {
  15487. var series = this,
  15488. options = series.options,
  15489. trackByArea = options.trackByArea,
  15490. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  15491. trackerPathLength = trackerPath.length,
  15492. chart = series.chart,
  15493. pointer = chart.pointer,
  15494. renderer = chart.renderer,
  15495. snap = chart.options.tooltip.snap,
  15496. tracker = series.tracker,
  15497. cursor = options.cursor,
  15498. css = cursor && { cursor: cursor },
  15499. singlePoints = series.singlePoints,
  15500. singlePoint,
  15501. i,
  15502. onMouseOver = function () {
  15503. if (chart.hoverSeries !== series) {
  15504. series.onMouseOver();
  15505. }
  15506. },
  15507. /*
  15508. * Empirical lowest possible opacities for TRACKER_FILL for an element to stay invisible but clickable
  15509. * IE6: 0.002
  15510. * IE7: 0.002
  15511. * IE8: 0.002
  15512. * IE9: 0.00000000001 (unlimited)
  15513. * IE10: 0.0001 (exporting only)
  15514. * FF: 0.00000000001 (unlimited)
  15515. * Chrome: 0.000001
  15516. * Safari: 0.000001
  15517. * Opera: 0.00000000001 (unlimited)
  15518. */
  15519. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.0001 : 0.002) + ')';
  15520. // Extend end points. A better way would be to use round linecaps,
  15521. // but those are not clickable in VML.
  15522. if (trackerPathLength && !trackByArea) {
  15523. i = trackerPathLength + 1;
  15524. while (i--) {
  15525. if (trackerPath[i] === M) { // extend left side
  15526. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  15527. }
  15528. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  15529. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  15530. }
  15531. }
  15532. }
  15533. // handle single points
  15534. for (i = 0; i < singlePoints.length; i++) {
  15535. singlePoint = singlePoints[i];
  15536. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  15537. L, singlePoint.plotX + snap, singlePoint.plotY);
  15538. }
  15539. // draw the tracker
  15540. if (tracker) {
  15541. tracker.attr({ d: trackerPath });
  15542. } else { // create
  15543. series.tracker = renderer.path(trackerPath)
  15544. .attr({
  15545. 'stroke-linejoin': 'round', // #1225
  15546. visibility: series.visible ? VISIBLE : HIDDEN,
  15547. stroke: TRACKER_FILL,
  15548. fill: trackByArea ? TRACKER_FILL : NONE,
  15549. 'stroke-width' : options.lineWidth + (trackByArea ? 0 : 2 * snap),
  15550. zIndex: 2
  15551. })
  15552. .add(series.group);
  15553. // The tracker is added to the series group, which is clipped, but is covered
  15554. // by the marker group. So the marker group also needs to capture events.
  15555. each([series.tracker, series.markerGroup], function (tracker) {
  15556. tracker.addClass(PREFIX + 'tracker')
  15557. .on('mouseover', onMouseOver)
  15558. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  15559. .css(css);
  15560. if (hasTouch) {
  15561. tracker.on('touchstart', onMouseOver);
  15562. }
  15563. });
  15564. }
  15565. }
  15566. };
  15567. /* End TrackerMixin */
  15568. /**
  15569. * Add tracking event listener to the series group, so the point graphics
  15570. * themselves act as trackers
  15571. */
  15572. if (seriesTypes.column) {
  15573. ColumnSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  15574. }
  15575. if (seriesTypes.pie) {
  15576. seriesTypes.pie.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  15577. }
  15578. if (seriesTypes.scatter) {
  15579. ScatterSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  15580. }
  15581. /*
  15582. * Extend Legend for item events
  15583. */
  15584. extend(Legend.prototype, {
  15585. setItemEvents: function (item, legendItem, useHTML, itemStyle, itemHiddenStyle) {
  15586. var legend = this;
  15587. // Set the events on the item group, or in case of useHTML, the item itself (#1249)
  15588. (useHTML ? legendItem : item.legendGroup).on('mouseover', function () {
  15589. item.setState(HOVER_STATE);
  15590. legendItem.css(legend.options.itemHoverStyle);
  15591. })
  15592. .on('mouseout', function () {
  15593. legendItem.css(item.visible ? itemStyle : itemHiddenStyle);
  15594. item.setState();
  15595. })
  15596. .on('click', function (event) {
  15597. var strLegendItemClick = 'legendItemClick',
  15598. fnLegendItemClick = function () {
  15599. item.setVisible();
  15600. };
  15601. // Pass over the click/touch event. #4.
  15602. event = {
  15603. browserEvent: event
  15604. };
  15605. // click the name or symbol
  15606. if (item.firePointEvent) { // point
  15607. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  15608. } else {
  15609. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  15610. }
  15611. });
  15612. },
  15613. createCheckboxForItem: function (item) {
  15614. var legend = this;
  15615. item.checkbox = createElement('input', {
  15616. type: 'checkbox',
  15617. checked: item.selected,
  15618. defaultChecked: item.selected // required by IE7
  15619. }, legend.options.itemCheckboxStyle, legend.chart.container);
  15620. addEvent(item.checkbox, 'click', function (event) {
  15621. var target = event.target;
  15622. fireEvent(item.series || item, 'checkboxClick', { // #3712
  15623. checked: target.checked,
  15624. item: item
  15625. },
  15626. function () {
  15627. item.select();
  15628. }
  15629. );
  15630. });
  15631. }
  15632. });
  15633. /*
  15634. * Add pointer cursor to legend itemstyle in defaultOptions
  15635. */
  15636. defaultOptions.legend.itemStyle.cursor = 'pointer';
  15637. /*
  15638. * Extend the Chart object with interaction
  15639. */
  15640. extend(Chart.prototype, {
  15641. /**
  15642. * Display the zoom button
  15643. */
  15644. showResetZoom: function () {
  15645. var chart = this,
  15646. lang = defaultOptions.lang,
  15647. btnOptions = chart.options.chart.resetZoomButton,
  15648. theme = btnOptions.theme,
  15649. states = theme.states,
  15650. alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
  15651. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, function () { chart.zoomOut(); }, theme, states && states.hover)
  15652. .attr({
  15653. align: btnOptions.position.align,
  15654. title: lang.resetZoomTitle
  15655. })
  15656. .add()
  15657. .align(btnOptions.position, false, alignTo);
  15658. },
  15659. /**
  15660. * Zoom out to 1:1
  15661. */
  15662. zoomOut: function () {
  15663. var chart = this;
  15664. fireEvent(chart, 'selection', { resetSelection: true }, function () {
  15665. chart.zoom();
  15666. });
  15667. },
  15668. /**
  15669. * Zoom into a given portion of the chart given by axis coordinates
  15670. * @param {Object} event
  15671. */
  15672. zoom: function (event) {
  15673. var chart = this,
  15674. hasZoomed,
  15675. pointer = chart.pointer,
  15676. displayButton = false,
  15677. resetZoomButton;
  15678. // If zoom is called with no arguments, reset the axes
  15679. if (!event || event.resetSelection) {
  15680. each(chart.axes, function (axis) {
  15681. hasZoomed = axis.zoom();
  15682. });
  15683. } else { // else, zoom in on all axes
  15684. each(event.xAxis.concat(event.yAxis), function (axisData) {
  15685. var axis = axisData.axis,
  15686. isXAxis = axis.isXAxis;
  15687. // don't zoom more than minRange
  15688. if (pointer[isXAxis ? 'zoomX' : 'zoomY'] || pointer[isXAxis ? 'pinchX' : 'pinchY']) {
  15689. hasZoomed = axis.zoom(axisData.min, axisData.max);
  15690. if (axis.displayBtn) {
  15691. displayButton = true;
  15692. }
  15693. }
  15694. });
  15695. }
  15696. // Show or hide the Reset zoom button
  15697. resetZoomButton = chart.resetZoomButton;
  15698. if (displayButton && !resetZoomButton) {
  15699. chart.showResetZoom();
  15700. } else if (!displayButton && isObject(resetZoomButton)) {
  15701. chart.resetZoomButton = resetZoomButton.destroy();
  15702. }
  15703. // Redraw
  15704. if (hasZoomed) {
  15705. chart.redraw(
  15706. pick(chart.options.chart.animation, event && event.animation, chart.pointCount < 100) // animation
  15707. );
  15708. }
  15709. },
  15710. /**
  15711. * Pan the chart by dragging the mouse across the pane. This function is called
  15712. * on mouse move, and the distance to pan is computed from chartX compared to
  15713. * the first chartX position in the dragging operation.
  15714. */
  15715. pan: function (e, panning) {
  15716. var chart = this,
  15717. hoverPoints = chart.hoverPoints,
  15718. doRedraw;
  15719. // remove active points for shared tooltip
  15720. if (hoverPoints) {
  15721. each(hoverPoints, function (point) {
  15722. point.setState();
  15723. });
  15724. }
  15725. each(panning === 'xy' ? [1, 0] : [1], function (isX) { // xy is used in maps
  15726. var mousePos = e[isX ? 'chartX' : 'chartY'],
  15727. axis = chart[isX ? 'xAxis' : 'yAxis'][0],
  15728. startPos = chart[isX ? 'mouseDownX' : 'mouseDownY'],
  15729. halfPointRange = (axis.pointRange || 0) / 2,
  15730. extremes = axis.getExtremes(),
  15731. newMin = axis.toValue(startPos - mousePos, true) + halfPointRange,
  15732. newMax = axis.toValue(startPos + chart[isX ? 'plotWidth' : 'plotHeight'] - mousePos, true) - halfPointRange,
  15733. goingLeft = startPos > mousePos; // #3613
  15734. if (axis.series.length &&
  15735. (goingLeft || newMin > mathMin(extremes.dataMin, extremes.min)) &&
  15736. (!goingLeft || newMax < mathMax(extremes.dataMax, extremes.max))) {
  15737. axis.setExtremes(newMin, newMax, false, false, { trigger: 'pan' });
  15738. doRedraw = true;
  15739. }
  15740. chart[isX ? 'mouseDownX' : 'mouseDownY'] = mousePos; // set new reference for next run
  15741. });
  15742. if (doRedraw) {
  15743. chart.redraw(false);
  15744. }
  15745. css(chart.container, { cursor: 'move' });
  15746. }
  15747. });
  15748. /*
  15749. * Extend the Point object with interaction
  15750. */
  15751. extend(Point.prototype, {
  15752. /**
  15753. * Toggle the selection status of a point
  15754. * @param {Boolean} selected Whether to select or unselect the point.
  15755. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  15756. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  15757. */
  15758. select: function (selected, accumulate) {
  15759. var point = this,
  15760. series = point.series,
  15761. chart = series.chart;
  15762. selected = pick(selected, !point.selected);
  15763. // fire the event with the defalut handler
  15764. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  15765. point.selected = point.options.selected = selected;
  15766. series.options.data[inArray(point, series.data)] = point.options;
  15767. point.setState(selected && SELECT_STATE);
  15768. // unselect all other points unless Ctrl or Cmd + click
  15769. if (!accumulate) {
  15770. each(chart.getSelectedPoints(), function (loopPoint) {
  15771. if (loopPoint.selected && loopPoint !== point) {
  15772. loopPoint.selected = loopPoint.options.selected = false;
  15773. series.options.data[inArray(loopPoint, series.data)] = loopPoint.options;
  15774. loopPoint.setState(NORMAL_STATE);
  15775. loopPoint.firePointEvent('unselect');
  15776. }
  15777. });
  15778. }
  15779. });
  15780. },
  15781. /**
  15782. * Runs on mouse over the point
  15783. *
  15784. * @param {Object} e The event arguments
  15785. * @param {Boolean} byProximity Falsy for kd points that are closest to the mouse, or to
  15786. * actually hovered points. True for other points in shared tooltip.
  15787. */
  15788. onMouseOver: function (e, byProximity) {
  15789. var point = this,
  15790. series = point.series,
  15791. chart = series.chart,
  15792. tooltip = chart.tooltip,
  15793. hoverPoint = chart.hoverPoint;
  15794. if (chart.hoverSeries !== series) {
  15795. series.onMouseOver();
  15796. }
  15797. // set normal state to previous series
  15798. if (hoverPoint && hoverPoint !== point) {
  15799. hoverPoint.onMouseOut();
  15800. }
  15801. if (point.series) { // It may have been destroyed, #4130
  15802. // trigger the event
  15803. point.firePointEvent('mouseOver');
  15804. // update the tooltip
  15805. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  15806. tooltip.refresh(point, e);
  15807. }
  15808. // hover this
  15809. point.setState(HOVER_STATE);
  15810. if (!byProximity) {
  15811. chart.hoverPoint = point;
  15812. }
  15813. }
  15814. },
  15815. /**
  15816. * Runs on mouse out from the point
  15817. */
  15818. onMouseOut: function () {
  15819. var chart = this.series.chart,
  15820. hoverPoints = chart.hoverPoints;
  15821. this.firePointEvent('mouseOut');
  15822. if (!hoverPoints || inArray(this, hoverPoints) === -1) { // #887, #2240
  15823. this.setState();
  15824. chart.hoverPoint = null;
  15825. }
  15826. },
  15827. /**
  15828. * Import events from the series' and point's options. Only do it on
  15829. * demand, to save processing time on hovering.
  15830. */
  15831. importEvents: function () {
  15832. if (!this.hasImportedEvents) {
  15833. var point = this,
  15834. options = merge(point.series.options.point, point.options),
  15835. events = options.events,
  15836. eventType;
  15837. point.events = events;
  15838. for (eventType in events) {
  15839. addEvent(point, eventType, events[eventType]);
  15840. }
  15841. this.hasImportedEvents = true;
  15842. }
  15843. },
  15844. /**
  15845. * Set the point's state
  15846. * @param {String} state
  15847. */
  15848. setState: function (state, move) {
  15849. var point = this,
  15850. plotX = point.plotX,
  15851. plotY = point.plotY,
  15852. series = point.series,
  15853. stateOptions = series.options.states,
  15854. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  15855. normalDisabled = markerOptions && !markerOptions.enabled,
  15856. markerStateOptions = markerOptions && markerOptions.states[state],
  15857. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  15858. stateMarkerGraphic = series.stateMarkerGraphic,
  15859. pointMarker = point.marker || {},
  15860. chart = series.chart,
  15861. radius,
  15862. halo = series.halo,
  15863. haloOptions,
  15864. newSymbol,
  15865. pointAttr;
  15866. state = state || NORMAL_STATE; // empty string
  15867. pointAttr = point.pointAttr[state] || series.pointAttr[state];
  15868. if (
  15869. // already has this state
  15870. (state === point.state && !move) ||
  15871. // selected points don't respond to hover
  15872. (point.selected && state !== SELECT_STATE) ||
  15873. // series' state options is disabled
  15874. (stateOptions[state] && stateOptions[state].enabled === false) ||
  15875. // general point marker's state options is disabled
  15876. (state && (stateDisabled || (normalDisabled && markerStateOptions.enabled === false))) ||
  15877. // individual point marker's state options is disabled
  15878. (state && pointMarker.states && pointMarker.states[state] && pointMarker.states[state].enabled === false) // #1610
  15879. ) {
  15880. return;
  15881. }
  15882. // apply hover styles to the existing point
  15883. if (point.graphic) {
  15884. radius = markerOptions && point.graphic.symbolName && pointAttr.r;
  15885. point.graphic.attr(merge(
  15886. pointAttr,
  15887. radius ? { // new symbol attributes (#507, #612)
  15888. x: plotX - radius,
  15889. y: plotY - radius,
  15890. width: 2 * radius,
  15891. height: 2 * radius
  15892. } : {}
  15893. ));
  15894. // Zooming in from a range with no markers to a range with markers
  15895. if (stateMarkerGraphic) {
  15896. stateMarkerGraphic.hide();
  15897. }
  15898. } else {
  15899. // if a graphic is not applied to each point in the normal state, create a shared
  15900. // graphic for the hover state
  15901. if (state && markerStateOptions) {
  15902. radius = markerStateOptions.radius;
  15903. newSymbol = pointMarker.symbol || series.symbol;
  15904. // If the point has another symbol than the previous one, throw away the
  15905. // state marker graphic and force a new one (#1459)
  15906. if (stateMarkerGraphic && stateMarkerGraphic.currentSymbol !== newSymbol) {
  15907. stateMarkerGraphic = stateMarkerGraphic.destroy();
  15908. }
  15909. // Add a new state marker graphic
  15910. if (!stateMarkerGraphic) {
  15911. if (newSymbol) {
  15912. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  15913. newSymbol,
  15914. plotX - radius,
  15915. plotY - radius,
  15916. 2 * radius,
  15917. 2 * radius
  15918. )
  15919. .attr(pointAttr)
  15920. .add(series.markerGroup);
  15921. stateMarkerGraphic.currentSymbol = newSymbol;
  15922. }
  15923. // Move the existing graphic
  15924. } else {
  15925. stateMarkerGraphic[move ? 'animate' : 'attr']({ // #1054
  15926. x: plotX - radius,
  15927. y: plotY - radius
  15928. });
  15929. }
  15930. }
  15931. if (stateMarkerGraphic) {
  15932. stateMarkerGraphic[state && chart.isInsidePlot(plotX, plotY, chart.inverted) ? 'show' : 'hide'](); // #2450
  15933. stateMarkerGraphic.element.point = point; // #4310
  15934. }
  15935. }
  15936. // Show me your halo
  15937. haloOptions = stateOptions[state] && stateOptions[state].halo;
  15938. if (haloOptions && haloOptions.size) {
  15939. if (!halo) {
  15940. series.halo = halo = chart.renderer.path()
  15941. .add(chart.seriesGroup);
  15942. }
  15943. halo.attr(extend({
  15944. fill: Color(point.color || series.color).setOpacity(haloOptions.opacity).get()
  15945. }, haloOptions.attributes))[move ? 'animate' : 'attr']({
  15946. d: point.haloPath(haloOptions.size)
  15947. });
  15948. } else if (halo) {
  15949. halo.attr({ d: [] });
  15950. }
  15951. point.state = state;
  15952. },
  15953. haloPath: function (size) {
  15954. var series = this.series,
  15955. chart = series.chart,
  15956. plotBox = series.getPlotBox(),
  15957. inverted = chart.inverted;
  15958. return chart.renderer.symbols.circle(
  15959. plotBox.translateX + (inverted ? series.yAxis.len - this.plotY : this.plotX) - size,
  15960. plotBox.translateY + (inverted ? series.xAxis.len - this.plotX : this.plotY) - size,
  15961. size * 2,
  15962. size * 2
  15963. );
  15964. }
  15965. });
  15966. /*
  15967. * Extend the Series object with interaction
  15968. */
  15969. extend(Series.prototype, {
  15970. /**
  15971. * Series mouse over handler
  15972. */
  15973. onMouseOver: function () {
  15974. var series = this,
  15975. chart = series.chart,
  15976. hoverSeries = chart.hoverSeries;
  15977. // set normal state to previous series
  15978. if (hoverSeries && hoverSeries !== series) {
  15979. hoverSeries.onMouseOut();
  15980. }
  15981. // trigger the event, but to save processing time,
  15982. // only if defined
  15983. if (series.options.events.mouseOver) {
  15984. fireEvent(series, 'mouseOver');
  15985. }
  15986. // hover this
  15987. series.setState(HOVER_STATE);
  15988. chart.hoverSeries = series;
  15989. },
  15990. /**
  15991. * Series mouse out handler
  15992. */
  15993. onMouseOut: function () {
  15994. // trigger the event only if listeners exist
  15995. var series = this,
  15996. options = series.options,
  15997. chart = series.chart,
  15998. tooltip = chart.tooltip,
  15999. hoverPoint = chart.hoverPoint;
  16000. chart.hoverSeries = null; // #182, set to null before the mouseOut event fires
  16001. // trigger mouse out on the point, which must be in this series
  16002. if (hoverPoint) {
  16003. hoverPoint.onMouseOut();
  16004. }
  16005. // fire the mouse out event
  16006. if (series && options.events.mouseOut) {
  16007. fireEvent(series, 'mouseOut');
  16008. }
  16009. // hide the tooltip
  16010. if (tooltip && !options.stickyTracking && (!tooltip.shared || series.noSharedTooltip)) {
  16011. tooltip.hide();
  16012. }
  16013. // set normal state
  16014. series.setState();
  16015. },
  16016. /**
  16017. * Set the state of the graph
  16018. */
  16019. setState: function (state) {
  16020. var series = this,
  16021. options = series.options,
  16022. graph = series.graph,
  16023. stateOptions = options.states,
  16024. lineWidth = options.lineWidth,
  16025. attribs,
  16026. i = 0;
  16027. state = state || NORMAL_STATE;
  16028. if (series.state !== state) {
  16029. series.state = state;
  16030. if (stateOptions[state] && stateOptions[state].enabled === false) {
  16031. return;
  16032. }
  16033. if (state) {
  16034. lineWidth = stateOptions[state].lineWidth || lineWidth + (stateOptions[state].lineWidthPlus || 0); // #4035
  16035. }
  16036. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  16037. attribs = {
  16038. 'stroke-width': lineWidth
  16039. };
  16040. // use attr because animate will cause any other animation on the graph to stop
  16041. graph.attr(attribs);
  16042. while (series['zoneGraph' + i]) {
  16043. series['zoneGraph' + i].attr(attribs);
  16044. i = i + 1;
  16045. }
  16046. }
  16047. }
  16048. },
  16049. /**
  16050. * Set the visibility of the graph
  16051. *
  16052. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  16053. * the visibility is toggled.
  16054. */
  16055. setVisible: function (vis, redraw) {
  16056. var series = this,
  16057. chart = series.chart,
  16058. legendItem = series.legendItem,
  16059. showOrHide,
  16060. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  16061. oldVisibility = series.visible;
  16062. // if called without an argument, toggle visibility
  16063. series.visible = vis = series.userOptions.visible = vis === UNDEFINED ? !oldVisibility : vis;
  16064. showOrHide = vis ? 'show' : 'hide';
  16065. // show or hide elements
  16066. each(['group', 'dataLabelsGroup', 'markerGroup', 'tracker'], function (key) {
  16067. if (series[key]) {
  16068. series[key][showOrHide]();
  16069. }
  16070. });
  16071. // hide tooltip (#1361)
  16072. if (chart.hoverSeries === series || (chart.hoverPoint && chart.hoverPoint.series) === series) {
  16073. series.onMouseOut();
  16074. }
  16075. if (legendItem) {
  16076. chart.legend.colorizeItem(series, vis);
  16077. }
  16078. // rescale or adapt to resized chart
  16079. series.isDirty = true;
  16080. // in a stack, all other series are affected
  16081. if (series.options.stacking) {
  16082. each(chart.series, function (otherSeries) {
  16083. if (otherSeries.options.stacking && otherSeries.visible) {
  16084. otherSeries.isDirty = true;
  16085. }
  16086. });
  16087. }
  16088. // show or hide linked series
  16089. each(series.linkedSeries, function (otherSeries) {
  16090. otherSeries.setVisible(vis, false);
  16091. });
  16092. if (ignoreHiddenSeries) {
  16093. chart.isDirtyBox = true;
  16094. }
  16095. if (redraw !== false) {
  16096. chart.redraw();
  16097. }
  16098. fireEvent(series, showOrHide);
  16099. },
  16100. /**
  16101. * Show the graph
  16102. */
  16103. show: function () {
  16104. this.setVisible(true);
  16105. },
  16106. /**
  16107. * Hide the graph
  16108. */
  16109. hide: function () {
  16110. this.setVisible(false);
  16111. },
  16112. /**
  16113. * Set the selected state of the graph
  16114. *
  16115. * @param selected {Boolean} True to select the series, false to unselect. If
  16116. * UNDEFINED, the selection state is toggled.
  16117. */
  16118. select: function (selected) {
  16119. var series = this;
  16120. // if called without an argument, toggle
  16121. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  16122. if (series.checkbox) {
  16123. series.checkbox.checked = selected;
  16124. }
  16125. fireEvent(series, selected ? 'select' : 'unselect');
  16126. },
  16127. drawTracker: TrackerMixin.drawTrackerGraph
  16128. });
  16129. // global variables
  16130. extend(Highcharts, {
  16131. // Constructors
  16132. Color: Color,
  16133. Point: Point,
  16134. Tick: Tick,
  16135. Renderer: Renderer,
  16136. SVGElement: SVGElement,
  16137. SVGRenderer: SVGRenderer,
  16138. // Various
  16139. arrayMin: arrayMin,
  16140. arrayMax: arrayMax,
  16141. charts: charts,
  16142. dateFormat: dateFormat,
  16143. error: error,
  16144. format: format,
  16145. pathAnim: pathAnim,
  16146. getOptions: getOptions,
  16147. hasBidiBug: hasBidiBug,
  16148. isTouchDevice: isTouchDevice,
  16149. setOptions: setOptions,
  16150. addEvent: addEvent,
  16151. removeEvent: removeEvent,
  16152. createElement: createElement,
  16153. discardElement: discardElement,
  16154. css: css,
  16155. each: each,
  16156. map: map,
  16157. merge: merge,
  16158. splat: splat,
  16159. extendClass: extendClass,
  16160. pInt: pInt,
  16161. svg: hasSVG,
  16162. canvas: useCanVG,
  16163. vml: !hasSVG && !useCanVG,
  16164. product: PRODUCT,
  16165. version: VERSION
  16166. });
  16167. }());