纽威
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.

17588 lines
453 KiB

2 years ago
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3. /**
  4. * @license Highcharts JS v3.0.10 (2014-03-10)
  5. *
  6. * (c) 2009-2014 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. // JSLint options:
  11. /*global Highcharts, document, window, navigator, setInterval, clearInterval, clearTimeout, setTimeout, location, jQuery, $, console, each, grep */
  12. (function () {
  13. // encapsulated variables
  14. var UNDEFINED,
  15. doc = document,
  16. win = window,
  17. math = Math,
  18. mathRound = math.round,
  19. mathFloor = math.floor,
  20. mathCeil = math.ceil,
  21. mathMax = math.max,
  22. mathMin = math.min,
  23. mathAbs = math.abs,
  24. mathCos = math.cos,
  25. mathSin = math.sin,
  26. mathPI = math.PI,
  27. deg2rad = mathPI * 2 / 360,
  28. // some variables
  29. userAgent = navigator.userAgent,
  30. isOpera = win.opera,
  31. isIE = /msie/i.test(userAgent) && !isOpera,
  32. docMode8 = doc.documentMode === 8,
  33. isWebKit = /AppleWebKit/.test(userAgent),
  34. isFirefox = /Firefox/.test(userAgent),
  35. isTouchDevice = /(Mobile|Android|Windows Phone)/.test(userAgent),
  36. SVG_NS = 'http://www.w3.org/2000/svg',
  37. hasSVG = !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  38. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  39. useCanVG = !hasSVG && !isIE && !!doc.createElement('canvas').getContext,
  40. Renderer,
  41. hasTouch,
  42. symbolSizes = {},
  43. idCounter = 0,
  44. garbageBin,
  45. defaultOptions,
  46. dateFormat, // function
  47. globalAnimation,
  48. pathAnim,
  49. timeUnits,
  50. noop = function () {},
  51. charts = [],
  52. PRODUCT = 'Highcharts',
  53. VERSION = '3.0.10',
  54. // some constants for frequently used strings
  55. DIV = 'div',
  56. ABSOLUTE = 'absolute',
  57. RELATIVE = 'relative',
  58. HIDDEN = 'hidden',
  59. PREFIX = 'highcharts-',
  60. VISIBLE = 'visible',
  61. PX = 'px',
  62. NONE = 'none',
  63. M = 'M',
  64. L = 'L',
  65. numRegex = /^[0-9]+$/,
  66. NORMAL_STATE = '',
  67. HOVER_STATE = 'hover',
  68. SELECT_STATE = 'select',
  69. MILLISECOND = 'millisecond',
  70. SECOND = 'second',
  71. MINUTE = 'minute',
  72. HOUR = 'hour',
  73. DAY = 'day',
  74. WEEK = 'week',
  75. MONTH = 'month',
  76. YEAR = 'year',
  77. // Object for extending Axis
  78. AxisPlotLineOrBandExtension,
  79. // constants for attributes
  80. STROKE_WIDTH = 'stroke-width',
  81. // time methods, changed based on whether or not UTC is used
  82. makeTime,
  83. timezoneOffset,
  84. getMinutes,
  85. getHours,
  86. getDay,
  87. getDate,
  88. getMonth,
  89. getFullYear,
  90. setMinutes,
  91. setHours,
  92. setDate,
  93. setMonth,
  94. setFullYear,
  95. // lookup over the types and the associated classes
  96. seriesTypes = {};
  97. // The Highcharts namespace
  98. var Highcharts = win.Highcharts = win.Highcharts ? error(16, true) : {};
  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. function extend(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. * Take an array and turn into a hash with even number arguments as keys and odd numbers as
  161. * values. Allows creating constants for commonly used style properties, attributes etc.
  162. * Avoid it in performance critical situations like looping
  163. */
  164. function hash() {
  165. var i = 0,
  166. args = arguments,
  167. length = args.length,
  168. obj = {};
  169. for (; i < length; i++) {
  170. obj[args[i++]] = args[i];
  171. }
  172. return obj;
  173. }
  174. /**
  175. * Shortcut for parseInt
  176. * @param {Object} s
  177. * @param {Number} mag Magnitude
  178. */
  179. function pInt(s, mag) {
  180. return parseInt(s, mag || 10);
  181. }
  182. /**
  183. * Check for string
  184. * @param {Object} s
  185. */
  186. function isString(s) {
  187. return typeof s === 'string';
  188. }
  189. /**
  190. * Check for object
  191. * @param {Object} obj
  192. */
  193. function isObject(obj) {
  194. return typeof obj === 'object';
  195. }
  196. /**
  197. * Check for array
  198. * @param {Object} obj
  199. */
  200. function isArray(obj) {
  201. return Object.prototype.toString.call(obj) === '[object Array]';
  202. }
  203. /**
  204. * Check for number
  205. * @param {Object} n
  206. */
  207. function isNumber(n) {
  208. return typeof n === 'number';
  209. }
  210. function log2lin(num) {
  211. return math.log(num) / math.LN10;
  212. }
  213. function lin2log(num) {
  214. return math.pow(10, num);
  215. }
  216. /**
  217. * Remove last occurence of an item from an array
  218. * @param {Array} arr
  219. * @param {Mixed} item
  220. */
  221. function erase(arr, item) {
  222. var i = arr.length;
  223. while (i--) {
  224. if (arr[i] === item) {
  225. arr.splice(i, 1);
  226. break;
  227. }
  228. }
  229. //return arr;
  230. }
  231. /**
  232. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  233. * @param {Object} obj
  234. */
  235. function defined(obj) {
  236. return obj !== UNDEFINED && obj !== null;
  237. }
  238. /**
  239. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  240. * it attempts to set expando properties on the SVG element, which is not allowed.
  241. *
  242. * @param {Object} elem The DOM element to receive the attribute(s)
  243. * @param {String|Object} prop The property or an abject of key-value pairs
  244. * @param {String} value The value if a single property is set
  245. */
  246. function attr(elem, prop, value) {
  247. var key,
  248. setAttribute = 'setAttribute',
  249. ret;
  250. // if the prop is a string
  251. if (isString(prop)) {
  252. // set the value
  253. if (defined(value)) {
  254. elem[setAttribute](prop, value);
  255. // get the value
  256. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  257. ret = elem.getAttribute(prop);
  258. }
  259. // else if prop is defined, it is a hash of key/value pairs
  260. } else if (defined(prop) && isObject(prop)) {
  261. for (key in prop) {
  262. elem[setAttribute](key, prop[key]);
  263. }
  264. }
  265. return ret;
  266. }
  267. /**
  268. * Check if an element is an array, and if not, make it into an array. Like
  269. * MooTools' $.splat.
  270. */
  271. function splat(obj) {
  272. return isArray(obj) ? obj : [obj];
  273. }
  274. /**
  275. * Return the first value that is defined. Like MooTools' $.pick.
  276. */
  277. function pick() {
  278. var args = arguments,
  279. i,
  280. arg,
  281. length = args.length;
  282. for (i = 0; i < length; i++) {
  283. arg = args[i];
  284. if (typeof arg !== 'undefined' && arg !== null) {
  285. return arg;
  286. }
  287. }
  288. }
  289. /**
  290. * Set CSS on a given element
  291. * @param {Object} el
  292. * @param {Object} styles Style object with camel case property names
  293. */
  294. function css(el, styles) {
  295. if (isIE && !hasSVG) { // #2686
  296. if (styles && styles.opacity !== UNDEFINED) {
  297. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  298. }
  299. }
  300. extend(el.style, styles);
  301. }
  302. /**
  303. * Utility function to create element with attributes and styles
  304. * @param {Object} tag
  305. * @param {Object} attribs
  306. * @param {Object} styles
  307. * @param {Object} parent
  308. * @param {Object} nopad
  309. */
  310. function createElement(tag, attribs, styles, parent, nopad) {
  311. var el = doc.createElement(tag);
  312. if (attribs) {
  313. extend(el, attribs);
  314. }
  315. if (nopad) {
  316. css(el, {padding: 0, border: NONE, margin: 0});
  317. }
  318. if (styles) {
  319. css(el, styles);
  320. }
  321. if (parent) {
  322. parent.appendChild(el);
  323. }
  324. return el;
  325. }
  326. /**
  327. * Extend a prototyped class by new members
  328. * @param {Object} parent
  329. * @param {Object} members
  330. */
  331. function extendClass(parent, members) {
  332. var object = function () {};
  333. object.prototype = new parent();
  334. extend(object.prototype, members);
  335. return object;
  336. }
  337. /**
  338. * Format a number and return a string based on input settings
  339. * @param {Number} number The input number to format
  340. * @param {Number} decimals The amount of decimals
  341. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  342. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  343. */
  344. function numberFormat(number, decimals, decPoint, thousandsSep) {
  345. var lang = defaultOptions.lang,
  346. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  347. n = +number || 0,
  348. c = decimals === -1 ?
  349. (n.toString().split('.')[1] || '').length : // preserve decimals
  350. (isNaN(decimals = mathAbs(decimals)) ? 2 : decimals),
  351. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  352. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  353. s = n < 0 ? "-" : "",
  354. i = String(pInt(n = mathAbs(n).toFixed(c))),
  355. j = i.length > 3 ? i.length % 3 : 0;
  356. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  357. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : "");
  358. }
  359. /**
  360. * Pad a string to a given length by adding 0 to the beginning
  361. * @param {Number} number
  362. * @param {Number} length
  363. */
  364. function pad(number, length) {
  365. // Create an array of the remaining length +1 and join it with 0's
  366. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  367. }
  368. /**
  369. * Wrap a method with extended functionality, preserving the original function
  370. * @param {Object} obj The context object that the method belongs to
  371. * @param {String} method The name of the method to extend
  372. * @param {Function} func A wrapper function callback. This function is called with the same arguments
  373. * as the original function, except that the original function is unshifted and passed as the first
  374. * argument.
  375. */
  376. function wrap(obj, method, func) {
  377. var proceed = obj[method];
  378. obj[method] = function () {
  379. var args = Array.prototype.slice.call(arguments);
  380. args.unshift(proceed);
  381. return func.apply(this, args);
  382. };
  383. }
  384. /**
  385. * Based on http://www.php.net/manual/en/function.strftime.php
  386. * @param {String} format
  387. * @param {Number} timestamp
  388. * @param {Boolean} capitalize
  389. */
  390. dateFormat = function (format, timestamp, capitalize) {
  391. if (!defined(timestamp) || isNaN(timestamp)) {
  392. return 'Invalid date';
  393. }
  394. format = pick(format, '%Y-%m-%d %H:%M:%S');
  395. var date = new Date(timestamp - timezoneOffset),
  396. key, // used in for constuct below
  397. // get the basic time values
  398. hours = date[getHours](),
  399. day = date[getDay](),
  400. dayOfMonth = date[getDate](),
  401. month = date[getMonth](),
  402. fullYear = date[getFullYear](),
  403. lang = defaultOptions.lang,
  404. langWeekdays = lang.weekdays,
  405. // List all format keys. Custom formats can be added from the outside.
  406. replacements = extend({
  407. // Day
  408. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  409. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  410. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  411. 'e': dayOfMonth, // Day of the month, 1 through 31
  412. // Week (none implemented)
  413. //'W': weekNumber(),
  414. // Month
  415. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  416. 'B': lang.months[month], // Long month, like 'January'
  417. 'm': pad(month + 1), // Two digit month number, 01 through 12
  418. // Year
  419. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  420. 'Y': fullYear, // Four digits year, like 2009
  421. // Time
  422. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  423. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  424. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  425. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  426. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  427. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  428. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  429. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  430. }, Highcharts.dateFormats);
  431. // do the replaces
  432. for (key in replacements) {
  433. while (format.indexOf('%' + key) !== -1) { // regex would do it in one line, but this is faster
  434. format = format.replace('%' + key, typeof replacements[key] === 'function' ? replacements[key](timestamp) : replacements[key]);
  435. }
  436. }
  437. // Optionally capitalize the string and return
  438. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  439. };
  440. /**
  441. * Format a single variable. Similar to sprintf, without the % prefix.
  442. */
  443. function formatSingle(format, val) {
  444. var floatRegex = /f$/,
  445. decRegex = /\.([0-9])/,
  446. lang = defaultOptions.lang,
  447. decimals;
  448. if (floatRegex.test(format)) { // float
  449. decimals = format.match(decRegex);
  450. decimals = decimals ? decimals[1] : -1;
  451. val = numberFormat(
  452. val,
  453. decimals,
  454. lang.decimalPoint,
  455. format.indexOf(',') > -1 ? lang.thousandsSep : ''
  456. );
  457. } else {
  458. val = dateFormat(format, val);
  459. }
  460. return val;
  461. }
  462. /**
  463. * Format a string according to a subset of the rules of Python's String.format method.
  464. */
  465. function format(str, ctx) {
  466. var splitter = '{',
  467. isInside = false,
  468. segment,
  469. valueAndFormat,
  470. path,
  471. i,
  472. len,
  473. ret = [],
  474. val,
  475. index;
  476. while ((index = str.indexOf(splitter)) !== -1) {
  477. segment = str.slice(0, index);
  478. if (isInside) { // we're on the closing bracket looking back
  479. valueAndFormat = segment.split(':');
  480. path = valueAndFormat.shift().split('.'); // get first and leave format
  481. len = path.length;
  482. val = ctx;
  483. // Assign deeper paths
  484. for (i = 0; i < len; i++) {
  485. val = val[path[i]];
  486. }
  487. // Format the replacement
  488. if (valueAndFormat.length) {
  489. val = formatSingle(valueAndFormat.join(':'), val);
  490. }
  491. // Push the result and advance the cursor
  492. ret.push(val);
  493. } else {
  494. ret.push(segment);
  495. }
  496. str = str.slice(index + 1); // the rest
  497. isInside = !isInside; // toggle
  498. splitter = isInside ? '}' : '{'; // now look for next matching bracket
  499. }
  500. ret.push(str);
  501. return ret.join('');
  502. }
  503. /**
  504. * Get the magnitude of a number
  505. */
  506. function getMagnitude(num) {
  507. return math.pow(10, mathFloor(math.log(num) / math.LN10));
  508. }
  509. /**
  510. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  511. * @param {Number} interval
  512. * @param {Array} multiples
  513. * @param {Number} magnitude
  514. * @param {Object} options
  515. */
  516. function normalizeTickInterval(interval, multiples, magnitude, options) {
  517. var normalized, i;
  518. // round to a tenfold of 1, 2, 2.5 or 5
  519. magnitude = pick(magnitude, 1);
  520. normalized = interval / magnitude;
  521. // multiples for a linear scale
  522. if (!multiples) {
  523. multiples = [1, 2, 2.5, 5, 10];
  524. // the allowDecimals option
  525. if (options && options.allowDecimals === false) {
  526. if (magnitude === 1) {
  527. multiples = [1, 2, 5, 10];
  528. } else if (magnitude <= 0.1) {
  529. multiples = [1 / magnitude];
  530. }
  531. }
  532. }
  533. // normalize the interval to the nearest multiple
  534. for (i = 0; i < multiples.length; i++) {
  535. interval = multiples[i];
  536. if (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2) {
  537. break;
  538. }
  539. }
  540. // multiply back to the correct magnitude
  541. interval *= magnitude;
  542. return interval;
  543. }
  544. /**
  545. * Helper class that contains variuos counters that are local to the chart.
  546. */
  547. function ChartCounters() {
  548. this.color = 0;
  549. this.symbol = 0;
  550. }
  551. ChartCounters.prototype = {
  552. /**
  553. * Wraps the color counter if it reaches the specified length.
  554. */
  555. wrapColor: function (length) {
  556. if (this.color >= length) {
  557. this.color = 0;
  558. }
  559. },
  560. /**
  561. * Wraps the symbol counter if it reaches the specified length.
  562. */
  563. wrapSymbol: function (length) {
  564. if (this.symbol >= length) {
  565. this.symbol = 0;
  566. }
  567. }
  568. };
  569. /**
  570. * Utility method that sorts an object array and keeping the order of equal items.
  571. * ECMA script standard does not specify the behaviour when items are equal.
  572. */
  573. function stableSort(arr, sortFunction) {
  574. var length = arr.length,
  575. sortValue,
  576. i;
  577. // Add index to each item
  578. for (i = 0; i < length; i++) {
  579. arr[i].ss_i = i; // stable sort index
  580. }
  581. arr.sort(function (a, b) {
  582. sortValue = sortFunction(a, b);
  583. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  584. });
  585. // Remove index from items
  586. for (i = 0; i < length; i++) {
  587. delete arr[i].ss_i; // stable sort index
  588. }
  589. }
  590. /**
  591. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  592. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  593. * method is slightly slower, but safe.
  594. */
  595. function arrayMin(data) {
  596. var i = data.length,
  597. min = data[0];
  598. while (i--) {
  599. if (data[i] < min) {
  600. min = data[i];
  601. }
  602. }
  603. return min;
  604. }
  605. /**
  606. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  607. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  608. * method is slightly slower, but safe.
  609. */
  610. function arrayMax(data) {
  611. var i = data.length,
  612. max = data[0];
  613. while (i--) {
  614. if (data[i] > max) {
  615. max = data[i];
  616. }
  617. }
  618. return max;
  619. }
  620. /**
  621. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  622. * It loops all properties and invokes destroy if there is a destroy method. The property is
  623. * then delete'ed.
  624. * @param {Object} The object to destroy properties on
  625. * @param {Object} Exception, do not destroy this property, only delete it.
  626. */
  627. function destroyObjectProperties(obj, except) {
  628. var n;
  629. for (n in obj) {
  630. // If the object is non-null and destroy is defined
  631. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  632. // Invoke the destroy
  633. obj[n].destroy();
  634. }
  635. // Delete the property from the object.
  636. delete obj[n];
  637. }
  638. }
  639. /**
  640. * Discard an element by moving it to the bin and delete
  641. * @param {Object} The HTML node to discard
  642. */
  643. function discardElement(element) {
  644. // create a garbage bin element, not part of the DOM
  645. if (!garbageBin) {
  646. garbageBin = createElement(DIV);
  647. }
  648. // move the node and empty bin
  649. if (element) {
  650. garbageBin.appendChild(element);
  651. }
  652. garbageBin.innerHTML = '';
  653. }
  654. /**
  655. * Provide error messages for debugging, with links to online explanation
  656. */
  657. function error(code, stop) {
  658. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  659. if (stop) {
  660. throw msg;
  661. } else if (win.console) {
  662. console.log(msg);
  663. }
  664. }
  665. /**
  666. * Fix JS round off float errors
  667. * @param {Number} num
  668. */
  669. function correctFloat(num) {
  670. return parseFloat(
  671. num.toPrecision(14)
  672. );
  673. }
  674. /**
  675. * Set the global animation to either a given value, or fall back to the
  676. * given chart's animation option
  677. * @param {Object} animation
  678. * @param {Object} chart
  679. */
  680. function setAnimation(animation, chart) {
  681. globalAnimation = pick(animation, chart.animation);
  682. }
  683. /**
  684. * The time unit lookup
  685. */
  686. /*jslint white: true*/
  687. timeUnits = hash(
  688. MILLISECOND, 1,
  689. SECOND, 1000,
  690. MINUTE, 60000,
  691. HOUR, 3600000,
  692. DAY, 24 * 3600000,
  693. WEEK, 7 * 24 * 3600000,
  694. MONTH, 31 * 24 * 3600000,
  695. YEAR, 31556952000
  696. );
  697. /*jslint white: false*/
  698. /**
  699. * Path interpolation algorithm used across adapters
  700. */
  701. pathAnim = {
  702. /**
  703. * Prepare start and end values so that the path can be animated one to one
  704. */
  705. init: function (elem, fromD, toD) {
  706. fromD = fromD || '';
  707. var shift = elem.shift,
  708. bezier = fromD.indexOf('C') > -1,
  709. numParams = bezier ? 7 : 3,
  710. endLength,
  711. slice,
  712. i,
  713. start = fromD.split(' '),
  714. end = [].concat(toD), // copy
  715. startBaseLine,
  716. endBaseLine,
  717. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  718. i = arr.length;
  719. while (i--) {
  720. if (arr[i] === M) {
  721. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  722. }
  723. }
  724. };
  725. if (bezier) {
  726. sixify(start);
  727. sixify(end);
  728. }
  729. // pull out the base lines before padding
  730. if (elem.isArea) {
  731. startBaseLine = start.splice(start.length - 6, 6);
  732. endBaseLine = end.splice(end.length - 6, 6);
  733. }
  734. // if shifting points, prepend a dummy point to the end path
  735. if (shift <= end.length / numParams && start.length === end.length) {
  736. while (shift--) {
  737. end = [].concat(end).splice(0, numParams).concat(end);
  738. }
  739. }
  740. elem.shift = 0; // reset for following animations
  741. // copy and append last point until the length matches the end length
  742. if (start.length) {
  743. endLength = end.length;
  744. while (start.length < endLength) {
  745. //bezier && sixify(start);
  746. slice = [].concat(start).splice(start.length - numParams, numParams);
  747. if (bezier) { // disable first control point
  748. slice[numParams - 6] = slice[numParams - 2];
  749. slice[numParams - 5] = slice[numParams - 1];
  750. }
  751. start = start.concat(slice);
  752. }
  753. }
  754. if (startBaseLine) { // append the base lines for areas
  755. start = start.concat(startBaseLine);
  756. end = end.concat(endBaseLine);
  757. }
  758. return [start, end];
  759. },
  760. /**
  761. * Interpolate each value of the path and return the array
  762. */
  763. step: function (start, end, pos, complete) {
  764. var ret = [],
  765. i = start.length,
  766. startVal;
  767. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  768. ret = complete;
  769. } else if (i === end.length && pos < 1) {
  770. while (i--) {
  771. startVal = parseFloat(start[i]);
  772. ret[i] =
  773. isNaN(startVal) ? // a letter instruction like M or L
  774. start[i] :
  775. pos * (parseFloat(end[i] - startVal)) + startVal;
  776. }
  777. } else { // if animation is finished or length not matching, land on right value
  778. ret = end;
  779. }
  780. return ret;
  781. }
  782. };
  783. (function ($) {
  784. /**
  785. * The default HighchartsAdapter for jQuery
  786. */
  787. win.HighchartsAdapter = win.HighchartsAdapter || ($ && {
  788. /**
  789. * Initialize the adapter by applying some extensions to jQuery
  790. */
  791. init: function (pathAnim) {
  792. // extend the animate function to allow SVG animations
  793. var Fx = $.fx,
  794. Step = Fx.step,
  795. dSetter,
  796. Tween = $.Tween,
  797. propHooks = Tween && Tween.propHooks,
  798. opacityHook = $.cssHooks.opacity;
  799. /*jslint unparam: true*//* allow unused param x in this function */
  800. $.extend($.easing, {
  801. easeOutQuad: function (x, t, b, c, d) {
  802. return -c * (t /= d) * (t - 2) + b;
  803. }
  804. });
  805. /*jslint unparam: false*/
  806. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  807. $.each(['cur', '_default', 'width', 'height', 'opacity'], function (i, fn) {
  808. var obj = Step,
  809. base;
  810. // Handle different parent objects
  811. if (fn === 'cur') {
  812. obj = Fx.prototype; // 'cur', the getter, relates to Fx.prototype
  813. } else if (fn === '_default' && Tween) { // jQuery 1.8 model
  814. obj = propHooks[fn];
  815. fn = 'set';
  816. }
  817. // Overwrite the method
  818. base = obj[fn];
  819. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  820. // create the extended function replacement
  821. obj[fn] = function (fx) {
  822. var elem;
  823. // Fx.prototype.cur does not use fx argument
  824. fx = i ? fx : this;
  825. // Don't run animations on textual properties like align (#1821)
  826. if (fx.prop === 'align') {
  827. return;
  828. }
  829. // shortcut
  830. elem = fx.elem;
  831. // Fx.prototype.cur returns the current value. The other ones are setters
  832. // and returning a value has no effect.
  833. return elem.attr ? // is SVG element wrapper
  834. elem.attr(fx.prop, fn === 'cur' ? UNDEFINED : fx.now) : // apply the SVG wrapper's method
  835. base.apply(this, arguments); // use jQuery's built-in method
  836. };
  837. }
  838. });
  839. // Extend the opacity getter, needed for fading opacity with IE9 and jQuery 1.10+
  840. wrap(opacityHook, 'get', function (proceed, elem, computed) {
  841. return elem.attr ? (elem.opacity || 0) : proceed.call(this, elem, computed);
  842. });
  843. // Define the setter function for d (path definitions)
  844. dSetter = function (fx) {
  845. var elem = fx.elem,
  846. ends;
  847. // Normally start and end should be set in state == 0, but sometimes,
  848. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  849. // in these cases
  850. if (!fx.started) {
  851. ends = pathAnim.init(elem, elem.d, elem.toD);
  852. fx.start = ends[0];
  853. fx.end = ends[1];
  854. fx.started = true;
  855. }
  856. // interpolate each value of the path
  857. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  858. };
  859. // jQuery 1.8 style
  860. if (Tween) {
  861. propHooks.d = {
  862. set: dSetter
  863. };
  864. // pre 1.8
  865. } else {
  866. // animate paths
  867. Step.d = dSetter;
  868. }
  869. /**
  870. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  871. * @param {Array} arr
  872. * @param {Function} fn
  873. */
  874. this.each = Array.prototype.forEach ?
  875. function (arr, fn) { // modern browsers
  876. return Array.prototype.forEach.call(arr, fn);
  877. } :
  878. function (arr, fn) { // legacy
  879. var i = 0,
  880. len = arr.length;
  881. for (; i < len; i++) {
  882. if (fn.call(arr[i], arr[i], i, arr) === false) {
  883. return i;
  884. }
  885. }
  886. };
  887. /**
  888. * Register Highcharts as a plugin in the respective framework
  889. */
  890. $.fn.highcharts = function () {
  891. var constr = 'Chart', // default constructor
  892. args = arguments,
  893. options,
  894. ret,
  895. chart;
  896. if (isString(args[0])) {
  897. constr = args[0];
  898. args = Array.prototype.slice.call(args, 1);
  899. }
  900. options = args[0];
  901. // Create the chart
  902. if (options !== UNDEFINED) {
  903. /*jslint unused:false*/
  904. options.chart = options.chart || {};
  905. options.chart.renderTo = this[0];
  906. chart = new Highcharts[constr](options, args[1]);
  907. ret = this;
  908. /*jslint unused:true*/
  909. }
  910. // When called without parameters or with the return argument, get a predefined chart
  911. if (options === UNDEFINED) {
  912. ret = charts[attr(this[0], 'data-highcharts-chart')];
  913. }
  914. return ret;
  915. };
  916. },
  917. /**
  918. * Downloads a script and executes a callback when done.
  919. * @param {String} scriptLocation
  920. * @param {Function} callback
  921. */
  922. getScript: $.getScript,
  923. /**
  924. * Return the index of an item in an array, or -1 if not found
  925. */
  926. inArray: $.inArray,
  927. /**
  928. * A direct link to jQuery methods. MooTools and Prototype adapters must be implemented for each case of method.
  929. * @param {Object} elem The HTML element
  930. * @param {String} method Which method to run on the wrapped element
  931. */
  932. adapterRun: function (elem, method) {
  933. return $(elem)[method]();
  934. },
  935. /**
  936. * Filter an array
  937. */
  938. grep: $.grep,
  939. /**
  940. * Map an array
  941. * @param {Array} arr
  942. * @param {Function} fn
  943. */
  944. map: function (arr, fn) {
  945. //return jQuery.map(arr, fn);
  946. var results = [],
  947. i = 0,
  948. len = arr.length;
  949. for (; i < len; i++) {
  950. results[i] = fn.call(arr[i], arr[i], i, arr);
  951. }
  952. return results;
  953. },
  954. /**
  955. * Get the position of an element relative to the top left of the page
  956. */
  957. offset: function (el) {
  958. return $(el).offset();
  959. },
  960. /**
  961. * Add an event listener
  962. * @param {Object} el A HTML element or custom object
  963. * @param {String} event The event type
  964. * @param {Function} fn The event handler
  965. */
  966. addEvent: function (el, event, fn) {
  967. $(el).bind(event, fn);
  968. },
  969. /**
  970. * Remove event added with addEvent
  971. * @param {Object} el The object
  972. * @param {String} eventType The event type. Leave blank to remove all events.
  973. * @param {Function} handler The function to remove
  974. */
  975. removeEvent: function (el, eventType, handler) {
  976. // workaround for jQuery issue with unbinding custom events:
  977. // http://forum.jQuery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jQuery-1-4-2
  978. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  979. if (doc[func] && el && !el[func]) {
  980. el[func] = function () {};
  981. }
  982. $(el).unbind(eventType, handler);
  983. },
  984. /**
  985. * Fire an event on a custom object
  986. * @param {Object} el
  987. * @param {String} type
  988. * @param {Object} eventArguments
  989. * @param {Function} defaultFunction
  990. */
  991. fireEvent: function (el, type, eventArguments, defaultFunction) {
  992. var event = $.Event(type),
  993. detachedType = 'detached' + type,
  994. defaultPrevented;
  995. // Remove warnings in Chrome when accessing layerX and layerY. Although Highcharts
  996. // never uses these properties, Chrome includes them in the default click event and
  997. // raises the warning when they are copied over in the extend statement below.
  998. //
  999. // To avoid problems in IE (see #1010) where we cannot delete the properties and avoid
  1000. // testing if they are there (warning in chrome) the only option is to test if running IE.
  1001. if (!isIE && eventArguments) {
  1002. delete eventArguments.layerX;
  1003. delete eventArguments.layerY;
  1004. }
  1005. extend(event, eventArguments);
  1006. // Prevent jQuery from triggering the object method that is named the
  1007. // same as the event. For example, if the event is 'select', jQuery
  1008. // attempts calling el.select and it goes into a loop.
  1009. if (el[type]) {
  1010. el[detachedType] = el[type];
  1011. el[type] = null;
  1012. }
  1013. // Wrap preventDefault and stopPropagation in try/catch blocks in
  1014. // order to prevent JS errors when cancelling events on non-DOM
  1015. // objects. #615.
  1016. /*jslint unparam: true*/
  1017. $.each(['preventDefault', 'stopPropagation'], function (i, fn) {
  1018. var base = event[fn];
  1019. event[fn] = function () {
  1020. try {
  1021. base.call(event);
  1022. } catch (e) {
  1023. if (fn === 'preventDefault') {
  1024. defaultPrevented = true;
  1025. }
  1026. }
  1027. };
  1028. });
  1029. /*jslint unparam: false*/
  1030. // trigger it
  1031. $(el).trigger(event);
  1032. // attach the method
  1033. if (el[detachedType]) {
  1034. el[type] = el[detachedType];
  1035. el[detachedType] = null;
  1036. }
  1037. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1038. defaultFunction(event);
  1039. }
  1040. },
  1041. /**
  1042. * Extension method needed for MooTools
  1043. */
  1044. washMouseEvent: function (e) {
  1045. var ret = e.originalEvent || e;
  1046. // computed by jQuery, needed by IE8
  1047. if (ret.pageX === UNDEFINED) { // #1236
  1048. ret.pageX = e.pageX;
  1049. ret.pageY = e.pageY;
  1050. }
  1051. return ret;
  1052. },
  1053. /**
  1054. * Animate a HTML element or SVG element wrapper
  1055. * @param {Object} el
  1056. * @param {Object} params
  1057. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1058. */
  1059. animate: function (el, params, options) {
  1060. var $el = $(el);
  1061. if (!el.style) {
  1062. el.style = {}; // #1881
  1063. }
  1064. if (params.d) {
  1065. el.toD = params.d; // keep the array form for paths, used in $.fx.step.d
  1066. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1067. }
  1068. $el.stop();
  1069. if (params.opacity !== UNDEFINED && el.attr) {
  1070. params.opacity += 'px'; // force jQuery to use same logic as width and height (#2161)
  1071. }
  1072. $el.animate(params, options);
  1073. },
  1074. /**
  1075. * Stop running animation
  1076. */
  1077. stop: function (el) {
  1078. $(el).stop();
  1079. }
  1080. });
  1081. }(win.jQuery));
  1082. // check for a custom HighchartsAdapter defined prior to this file
  1083. var globalAdapter = win.HighchartsAdapter,
  1084. adapter = globalAdapter || {};
  1085. // Initialize the adapter
  1086. if (globalAdapter) {
  1087. globalAdapter.init.call(globalAdapter, pathAnim);
  1088. }
  1089. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  1090. // and all the utility functions will be null. In that case they are populated by the
  1091. // default adapters below.
  1092. var adapterRun = adapter.adapterRun,
  1093. getScript = adapter.getScript,
  1094. inArray = adapter.inArray,
  1095. each = adapter.each,
  1096. grep = adapter.grep,
  1097. offset = adapter.offset,
  1098. map = adapter.map,
  1099. addEvent = adapter.addEvent,
  1100. removeEvent = adapter.removeEvent,
  1101. fireEvent = adapter.fireEvent,
  1102. washMouseEvent = adapter.washMouseEvent,
  1103. animate = adapter.animate,
  1104. stop = adapter.stop;
  1105. /* ****************************************************************************
  1106. * Handle the options *
  1107. *****************************************************************************/
  1108. var
  1109. defaultLabelOptions = {
  1110. enabled: true,
  1111. // rotation: 0,
  1112. // align: 'center',
  1113. x: 0,
  1114. y: 15,
  1115. /*formatter: function () {
  1116. return this.value;
  1117. },*/
  1118. style: {
  1119. color: '#666',
  1120. cursor: 'default',
  1121. fontSize: '11px'
  1122. }
  1123. };
  1124. defaultOptions = {
  1125. colors: ['#2f7ed8', '#0d233a', '#8bbc21', '#910000', '#1aadce', '#492970',
  1126. '#f28f43', '#77a1e5', '#c42525', '#a6c96a'],
  1127. //colors: ['#8085e8', '#252530', '#90ee7e', '#8d4654', '#2b908f', '#76758e', '#f6a45c', '#7eb5ee', '#f45b5b', '#9ff0cf'],
  1128. symbols: ['circle', 'diamond', 'square', 'triangle', 'triangle-down'],
  1129. lang: {
  1130. loading: 'Loading...',
  1131. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1132. 'August', 'September', 'October', 'November', 'December'],
  1133. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1134. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1135. decimalPoint: '.',
  1136. numericSymbols: ['k', 'M', 'G', 'T', 'P', 'E'], // SI prefixes used in axis labels
  1137. resetZoom: 'Reset zoom',
  1138. resetZoomTitle: 'Reset zoom level 1:1',
  1139. thousandsSep: ','
  1140. },
  1141. global: {
  1142. useUTC: true,
  1143. //timezoneOffset: 0,
  1144. canvasToolsURL: 'http://code.highcharts.com/3.0.10/modules/canvas-tools.js',
  1145. VMLRadialGradientURL: 'http://code.highcharts.com/3.0.10/gfx/vml-radial-gradient.png'
  1146. },
  1147. chart: {
  1148. //animation: true,
  1149. //alignTicks: false,
  1150. //reflow: true,
  1151. //className: null,
  1152. //events: { load, selection },
  1153. //margin: [null],
  1154. //marginTop: null,
  1155. //marginRight: null,
  1156. //marginBottom: null,
  1157. //marginLeft: null,
  1158. borderColor: '#4572A7',
  1159. //borderWidth: 0,
  1160. borderRadius: 5,
  1161. defaultSeriesType: 'line',
  1162. ignoreHiddenSeries: true,
  1163. //inverted: false,
  1164. //shadow: false,
  1165. spacing: [10, 10, 15, 10],
  1166. //spacingTop: 10,
  1167. //spacingRight: 10,
  1168. //spacingBottom: 15,
  1169. //spacingLeft: 10,
  1170. //style: {
  1171. // fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1172. // fontSize: '12px'
  1173. //},
  1174. backgroundColor: '#FFFFFF',
  1175. //plotBackgroundColor: null,
  1176. plotBorderColor: '#C0C0C0',
  1177. //plotBorderWidth: 0,
  1178. //plotShadow: false,
  1179. //zoomType: ''
  1180. resetZoomButton: {
  1181. theme: {
  1182. zIndex: 20
  1183. },
  1184. position: {
  1185. align: 'right',
  1186. x: -10,
  1187. //verticalAlign: 'top',
  1188. y: 10
  1189. }
  1190. // relativeTo: 'plot'
  1191. }
  1192. },
  1193. title: {
  1194. text: 'Chart title',
  1195. align: 'center',
  1196. // floating: false,
  1197. margin: 15,
  1198. // x: 0,
  1199. // verticalAlign: 'top',
  1200. // y: null,
  1201. style: {
  1202. color: '#274b6d',//#3E576F',
  1203. fontSize: '16px'
  1204. }
  1205. },
  1206. subtitle: {
  1207. text: '',
  1208. align: 'center',
  1209. // floating: false
  1210. // x: 0,
  1211. // verticalAlign: 'top',
  1212. // y: null,
  1213. style: {
  1214. color: '#4d759e'
  1215. }
  1216. },
  1217. plotOptions: {
  1218. line: { // base series options
  1219. allowPointSelect: false,
  1220. showCheckbox: false,
  1221. animation: {
  1222. duration: 1000
  1223. },
  1224. //connectNulls: false,
  1225. //cursor: 'default',
  1226. //clip: true,
  1227. //dashStyle: null,
  1228. //enableMouseTracking: true,
  1229. events: {},
  1230. //legendIndex: 0,
  1231. //linecap: 'round',
  1232. lineWidth: 2,
  1233. //shadow: false,
  1234. // stacking: null,
  1235. marker: {
  1236. enabled: true,
  1237. //symbol: null,
  1238. lineWidth: 0,
  1239. radius: 4,
  1240. lineColor: '#FFFFFF',
  1241. //fillColor: null,
  1242. states: { // states for a single point
  1243. hover: {
  1244. enabled: true
  1245. //radius: base + 2
  1246. },
  1247. select: {
  1248. fillColor: '#FFFFFF',
  1249. lineColor: '#000000',
  1250. lineWidth: 2
  1251. }
  1252. }
  1253. },
  1254. point: {
  1255. events: {}
  1256. },
  1257. dataLabels: merge(defaultLabelOptions, {
  1258. align: 'center',
  1259. enabled: false,
  1260. formatter: function () {
  1261. return this.y === null ? '' : numberFormat(this.y, -1);
  1262. },
  1263. verticalAlign: 'bottom', // above singular point
  1264. y: 0
  1265. // backgroundColor: undefined,
  1266. // borderColor: undefined,
  1267. // borderRadius: undefined,
  1268. // borderWidth: undefined,
  1269. // padding: 3,
  1270. // shadow: false
  1271. }),
  1272. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1273. pointRange: 0,
  1274. //pointStart: 0,
  1275. //pointInterval: 1,
  1276. //showInLegend: null, // auto: true for standalone series, false for linked series
  1277. states: { // states for the entire series
  1278. hover: {
  1279. //enabled: false,
  1280. //lineWidth: base + 1,
  1281. marker: {
  1282. // lineWidth: base + 1,
  1283. // radius: base + 1
  1284. }
  1285. },
  1286. select: {
  1287. marker: {}
  1288. }
  1289. },
  1290. stickyTracking: true,
  1291. //tooltip: {
  1292. //pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b>'
  1293. //valueDecimals: null,
  1294. //xDateFormat: '%A, %b %e, %Y',
  1295. //valuePrefix: '',
  1296. //ySuffix: ''
  1297. //}
  1298. turboThreshold: 1000
  1299. // zIndex: null
  1300. }
  1301. },
  1302. labels: {
  1303. //items: [],
  1304. style: {
  1305. //font: defaultFont,
  1306. position: ABSOLUTE,
  1307. color: '#3E576F'
  1308. }
  1309. },
  1310. legend: {
  1311. enabled: true,
  1312. align: 'center',
  1313. //floating: false,
  1314. layout: 'horizontal',
  1315. labelFormatter: function () {
  1316. return this.name;
  1317. },
  1318. borderWidth: 1,
  1319. borderColor: '#909090',
  1320. borderRadius: 5,
  1321. navigation: {
  1322. // animation: true,
  1323. activeColor: '#274b6d',
  1324. // arrowSize: 12
  1325. inactiveColor: '#CCC'
  1326. // style: {} // text styles
  1327. },
  1328. // margin: 10,
  1329. // reversed: false,
  1330. shadow: false,
  1331. // backgroundColor: null,
  1332. /*style: {
  1333. padding: '5px'
  1334. },*/
  1335. itemStyle: {
  1336. color: '#274b6d',
  1337. fontSize: '12px'
  1338. },
  1339. itemHoverStyle: {
  1340. //cursor: 'pointer', removed as of #601
  1341. color: '#000'
  1342. },
  1343. itemHiddenStyle: {
  1344. color: '#CCC'
  1345. },
  1346. itemCheckboxStyle: {
  1347. position: ABSOLUTE,
  1348. width: '13px', // for IE precision
  1349. height: '13px'
  1350. },
  1351. // itemWidth: undefined,
  1352. // symbolWidth: 16,
  1353. symbolPadding: 5,
  1354. verticalAlign: 'bottom',
  1355. // width: undefined,
  1356. x: 0,
  1357. y: 0,
  1358. title: {
  1359. //text: null,
  1360. style: {
  1361. fontWeight: 'bold'
  1362. }
  1363. }
  1364. },
  1365. loading: {
  1366. // hideDuration: 100,
  1367. labelStyle: {
  1368. fontWeight: 'bold',
  1369. position: RELATIVE,
  1370. top: '1em'
  1371. },
  1372. // showDuration: 0,
  1373. style: {
  1374. position: ABSOLUTE,
  1375. backgroundColor: 'white',
  1376. opacity: 0.5,
  1377. textAlign: 'center'
  1378. }
  1379. },
  1380. tooltip: {
  1381. enabled: true,
  1382. animation: hasSVG,
  1383. //crosshairs: null,
  1384. backgroundColor: 'rgba(255, 255, 255, .85)',
  1385. borderWidth: 1,
  1386. borderRadius: 3,
  1387. dateTimeLabelFormats: {
  1388. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1389. second: '%A, %b %e, %H:%M:%S',
  1390. minute: '%A, %b %e, %H:%M',
  1391. hour: '%A, %b %e, %H:%M',
  1392. day: '%A, %b %e, %Y',
  1393. week: 'Week from %A, %b %e, %Y',
  1394. month: '%B %Y',
  1395. year: '%Y'
  1396. },
  1397. //formatter: defaultFormatter,
  1398. headerFormat: '<span style="font-size: 10px">{point.key}</span><br/>',
  1399. pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
  1400. shadow: true,
  1401. //shared: false,
  1402. snap: isTouchDevice ? 25 : 10,
  1403. style: {
  1404. color: '#333333',
  1405. cursor: 'default',
  1406. fontSize: '12px',
  1407. padding: '8px',
  1408. whiteSpace: 'nowrap'
  1409. }
  1410. //xDateFormat: '%A, %b %e, %Y',
  1411. //valueDecimals: null,
  1412. //valuePrefix: '',
  1413. //valueSuffix: ''
  1414. },
  1415. credits: {
  1416. enabled: true,
  1417. text: 'Highcharts.com',
  1418. href: 'http://www.highcharts.com',
  1419. position: {
  1420. align: 'right',
  1421. x: -10,
  1422. verticalAlign: 'bottom',
  1423. y: -5
  1424. },
  1425. style: {
  1426. cursor: 'pointer',
  1427. color: '#909090',
  1428. fontSize: '9px'
  1429. }
  1430. }
  1431. };
  1432. // Series defaults
  1433. var defaultPlotOptions = defaultOptions.plotOptions,
  1434. defaultSeriesOptions = defaultPlotOptions.line;
  1435. // set the default time methods
  1436. setTimeMethods();
  1437. /**
  1438. * Set the time methods globally based on the useUTC option. Time method can be either
  1439. * local time or UTC (default).
  1440. */
  1441. function setTimeMethods() {
  1442. var useUTC = defaultOptions.global.useUTC,
  1443. GET = useUTC ? 'getUTC' : 'get',
  1444. SET = useUTC ? 'setUTC' : 'set';
  1445. timezoneOffset = ((useUTC && defaultOptions.global.timezoneOffset) || 0) * 60000;
  1446. makeTime = useUTC ? Date.UTC : function (year, month, date, hours, minutes, seconds) {
  1447. return new Date(
  1448. year,
  1449. month,
  1450. pick(date, 1),
  1451. pick(hours, 0),
  1452. pick(minutes, 0),
  1453. pick(seconds, 0)
  1454. ).getTime();
  1455. };
  1456. getMinutes = GET + 'Minutes';
  1457. getHours = GET + 'Hours';
  1458. getDay = GET + 'Day';
  1459. getDate = GET + 'Date';
  1460. getMonth = GET + 'Month';
  1461. getFullYear = GET + 'FullYear';
  1462. setMinutes = SET + 'Minutes';
  1463. setHours = SET + 'Hours';
  1464. setDate = SET + 'Date';
  1465. setMonth = SET + 'Month';
  1466. setFullYear = SET + 'FullYear';
  1467. }
  1468. /**
  1469. * Merge the default options with custom options and return the new options structure
  1470. * @param {Object} options The new custom options
  1471. */
  1472. function setOptions(options) {
  1473. // Copy in the default options
  1474. defaultOptions = merge(true, defaultOptions, options);
  1475. // Apply UTC
  1476. setTimeMethods();
  1477. return defaultOptions;
  1478. }
  1479. /**
  1480. * Get the updated default options. Until 3.0.7, merely exposing defaultOptions for outside modules
  1481. * wasn't enough because the setOptions method created a new object.
  1482. */
  1483. function getOptions() {
  1484. return defaultOptions;
  1485. }
  1486. /**
  1487. * Handle color operations. The object methods are chainable.
  1488. * @param {String} input The input color in either rbga or hex format
  1489. */
  1490. 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*\)/,
  1491. hexRegEx = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,
  1492. rgbRegEx = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/;
  1493. var Color = function (input) {
  1494. // declare variables
  1495. var rgba = [], result, stops;
  1496. /**
  1497. * Parse the input color to rgba array
  1498. * @param {String} input
  1499. */
  1500. function init(input) {
  1501. // Gradients
  1502. if (input && input.stops) {
  1503. stops = map(input.stops, function (stop) {
  1504. return Color(stop[1]);
  1505. });
  1506. // Solid colors
  1507. } else {
  1508. // rgba
  1509. result = rgbaRegEx.exec(input);
  1510. if (result) {
  1511. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1512. } else {
  1513. // hex
  1514. result = hexRegEx.exec(input);
  1515. if (result) {
  1516. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1517. } else {
  1518. // rgb
  1519. result = rgbRegEx.exec(input);
  1520. if (result) {
  1521. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), 1];
  1522. }
  1523. }
  1524. }
  1525. }
  1526. }
  1527. /**
  1528. * Return the color a specified format
  1529. * @param {String} format
  1530. */
  1531. function get(format) {
  1532. var ret;
  1533. if (stops) {
  1534. ret = merge(input);
  1535. ret.stops = [].concat(ret.stops);
  1536. each(stops, function (stop, i) {
  1537. ret.stops[i] = [ret.stops[i][0], stop.get(format)];
  1538. });
  1539. // it's NaN if gradient colors on a column chart
  1540. } else if (rgba && !isNaN(rgba[0])) {
  1541. if (format === 'rgb') {
  1542. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1543. } else if (format === 'a') {
  1544. ret = rgba[3];
  1545. } else {
  1546. ret = 'rgba(' + rgba.join(',') + ')';
  1547. }
  1548. } else {
  1549. ret = input;
  1550. }
  1551. return ret;
  1552. }
  1553. /**
  1554. * Brighten the color
  1555. * @param {Number} alpha
  1556. */
  1557. function brighten(alpha) {
  1558. if (stops) {
  1559. each(stops, function (stop) {
  1560. stop.brighten(alpha);
  1561. });
  1562. } else if (isNumber(alpha) && alpha !== 0) {
  1563. var i;
  1564. for (i = 0; i < 3; i++) {
  1565. rgba[i] += pInt(alpha * 255);
  1566. if (rgba[i] < 0) {
  1567. rgba[i] = 0;
  1568. }
  1569. if (rgba[i] > 255) {
  1570. rgba[i] = 255;
  1571. }
  1572. }
  1573. }
  1574. return this;
  1575. }
  1576. /**
  1577. * Set the color's opacity to a given alpha value
  1578. * @param {Number} alpha
  1579. */
  1580. function setOpacity(alpha) {
  1581. rgba[3] = alpha;
  1582. return this;
  1583. }
  1584. // initialize: parse the input
  1585. init(input);
  1586. // public methods
  1587. return {
  1588. get: get,
  1589. brighten: brighten,
  1590. rgba: rgba,
  1591. setOpacity: setOpacity
  1592. };
  1593. };
  1594. /**
  1595. * A wrapper object for SVG elements
  1596. */
  1597. function SVGElement() {}
  1598. SVGElement.prototype = {
  1599. /**
  1600. * Initialize the SVG renderer
  1601. * @param {Object} renderer
  1602. * @param {String} nodeName
  1603. */
  1604. init: function (renderer, nodeName) {
  1605. var wrapper = this;
  1606. wrapper.element = nodeName === 'span' ?
  1607. createElement(nodeName) :
  1608. doc.createElementNS(SVG_NS, nodeName);
  1609. wrapper.renderer = renderer;
  1610. /**
  1611. * A collection of attribute setters. These methods, if defined, are called right before a certain
  1612. * attribute is set on an element wrapper. Returning false prevents the default attribute
  1613. * setter to run. Returning a value causes the default setter to set that value. Used in
  1614. * Renderer.label.
  1615. */
  1616. wrapper.attrSetters = {};
  1617. },
  1618. /**
  1619. * Default base for animation
  1620. */
  1621. opacity: 1,
  1622. /**
  1623. * Animate a given attribute
  1624. * @param {Object} params
  1625. * @param {Number} options The same options as in jQuery animation
  1626. * @param {Function} complete Function to perform at the end of animation
  1627. */
  1628. animate: function (params, options, complete) {
  1629. var animOptions = pick(options, globalAnimation, true);
  1630. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1631. if (animOptions) {
  1632. animOptions = merge(animOptions, {}); //#2625
  1633. if (complete) { // allows using a callback with the global animation without overwriting it
  1634. animOptions.complete = complete;
  1635. }
  1636. animate(this, params, animOptions);
  1637. } else {
  1638. this.attr(params);
  1639. if (complete) {
  1640. complete();
  1641. }
  1642. }
  1643. },
  1644. /**
  1645. * Set or get a given attribute
  1646. * @param {Object|String} hash
  1647. * @param {Mixed|Undefined} val
  1648. */
  1649. attr: function (hash, val) {
  1650. var wrapper = this,
  1651. key,
  1652. value,
  1653. result,
  1654. i,
  1655. child,
  1656. element = wrapper.element,
  1657. nodeName = element.nodeName.toLowerCase(), // Android2 requires lower for "text"
  1658. renderer = wrapper.renderer,
  1659. skipAttr,
  1660. titleNode,
  1661. attrSetters = wrapper.attrSetters,
  1662. shadows = wrapper.shadows,
  1663. hasSetSymbolSize,
  1664. doTransform,
  1665. ret = wrapper;
  1666. // single key-value pair
  1667. if (isString(hash) && defined(val)) {
  1668. key = hash;
  1669. hash = {};
  1670. hash[key] = val;
  1671. }
  1672. // used as a getter: first argument is a string, second is undefined
  1673. if (isString(hash)) {
  1674. key = hash;
  1675. if (nodeName === 'circle') {
  1676. key = { x: 'cx', y: 'cy' }[key] || key;
  1677. } else if (key === 'strokeWidth') {
  1678. key = 'stroke-width';
  1679. }
  1680. ret = attr(element, key) || wrapper[key] || 0;
  1681. if (key !== 'd' && key !== 'visibility' && key !== 'fill') { // 'd' is string in animation step
  1682. ret = parseFloat(ret);
  1683. }
  1684. // setter
  1685. } else {
  1686. for (key in hash) {
  1687. skipAttr = false; // reset
  1688. value = hash[key];
  1689. // check for a specific attribute setter
  1690. result = attrSetters[key] && attrSetters[key].call(wrapper, value, key);
  1691. if (result !== false) {
  1692. if (result !== UNDEFINED) {
  1693. value = result; // the attribute setter has returned a new value to set
  1694. }
  1695. // paths
  1696. if (key === 'd') {
  1697. if (value && value.join) { // join path
  1698. value = value.join(' ');
  1699. }
  1700. if (/(NaN| {2}|^$)/.test(value)) {
  1701. value = 'M 0 0';
  1702. }
  1703. //wrapper.d = value; // shortcut for animations
  1704. // update child tspans x values
  1705. } else if (key === 'x' && nodeName === 'text') {
  1706. for (i = 0; i < element.childNodes.length; i++) {
  1707. child = element.childNodes[i];
  1708. // if the x values are equal, the tspan represents a linebreak
  1709. if (attr(child, 'x') === attr(element, 'x')) {
  1710. //child.setAttribute('x', value);
  1711. attr(child, 'x', value);
  1712. }
  1713. }
  1714. } else if (wrapper.rotation && (key === 'x' || key === 'y')) {
  1715. doTransform = true;
  1716. // apply gradients
  1717. } else if (key === 'fill') {
  1718. value = renderer.color(value, element, key);
  1719. // circle x and y
  1720. } else if (nodeName === 'circle' && (key === 'x' || key === 'y')) {
  1721. key = { x: 'cx', y: 'cy' }[key] || key;
  1722. // rectangle border radius
  1723. } else if (nodeName === 'rect' && key === 'r') {
  1724. attr(element, {
  1725. rx: value,
  1726. ry: value
  1727. });
  1728. skipAttr = true;
  1729. // translation and text rotation
  1730. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation' ||
  1731. key === 'verticalAlign' || key === 'scaleX' || key === 'scaleY') {
  1732. doTransform = true;
  1733. skipAttr = true;
  1734. // apply opacity as subnode (required by legacy WebKit and Batik)
  1735. } else if (key === 'stroke') {
  1736. value = renderer.color(value, element, key);
  1737. // emulate VML's dashstyle implementation
  1738. } else if (key === 'dashstyle') {
  1739. key = 'stroke-dasharray';
  1740. value = value && value.toLowerCase();
  1741. if (value === 'solid') {
  1742. value = NONE;
  1743. } else if (value) {
  1744. value = value
  1745. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  1746. .replace('shortdashdot', '3,1,1,1')
  1747. .replace('shortdot', '1,1,')
  1748. .replace('shortdash', '3,1,')
  1749. .replace('longdash', '8,3,')
  1750. .replace(/dot/g, '1,3,')
  1751. .replace('dash', '4,3,')
  1752. .replace(/,$/, '')
  1753. .split(','); // ending comma
  1754. i = value.length;
  1755. while (i--) {
  1756. value[i] = pInt(value[i]) * pick(hash['stroke-width'], wrapper['stroke-width']);
  1757. }
  1758. value = value.join(',');
  1759. }
  1760. // IE9/MooTools combo: MooTools returns objects instead of numbers and IE9 Beta 2
  1761. // is unable to cast them. Test again with final IE9.
  1762. } else if (key === 'width') {
  1763. value = pInt(value);
  1764. // Text alignment
  1765. } else if (key === 'align') {
  1766. key = 'text-anchor';
  1767. value = { left: 'start', center: 'middle', right: 'end' }[value];
  1768. // Title requires a subnode, #431
  1769. } else if (key === 'title') {
  1770. titleNode = element.getElementsByTagName('title')[0];
  1771. if (!titleNode) {
  1772. titleNode = doc.createElementNS(SVG_NS, 'title');
  1773. element.appendChild(titleNode);
  1774. }
  1775. titleNode.textContent = value;
  1776. }
  1777. // jQuery animate changes case
  1778. if (key === 'strokeWidth') {
  1779. key = 'stroke-width';
  1780. }
  1781. // In Chrome/Win < 6 as well as Batik, the stroke attribute can't be set when the stroke-
  1782. // width is 0. #1369
  1783. if (key === 'stroke-width' || key === 'stroke') {
  1784. wrapper[key] = value;
  1785. // Only apply the stroke attribute if the stroke width is defined and larger than 0
  1786. if (wrapper.stroke && wrapper['stroke-width']) {
  1787. attr(element, 'stroke', wrapper.stroke);
  1788. attr(element, 'stroke-width', wrapper['stroke-width']);
  1789. wrapper.hasStroke = true;
  1790. } else if (key === 'stroke-width' && value === 0 && wrapper.hasStroke) {
  1791. element.removeAttribute('stroke');
  1792. wrapper.hasStroke = false;
  1793. }
  1794. skipAttr = true;
  1795. }
  1796. // symbols
  1797. if (wrapper.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1798. if (!hasSetSymbolSize) {
  1799. wrapper.symbolAttr(hash);
  1800. hasSetSymbolSize = true;
  1801. }
  1802. skipAttr = true;
  1803. }
  1804. // let the shadow follow the main element
  1805. if (shadows && /^(width|height|visibility|x|y|d|transform|cx|cy|r)$/.test(key)) {
  1806. i = shadows.length;
  1807. while (i--) {
  1808. attr(
  1809. shadows[i],
  1810. key,
  1811. key === 'height' ?
  1812. mathMax(value - (shadows[i].cutHeight || 0), 0) :
  1813. value
  1814. );
  1815. }
  1816. }
  1817. // validate heights
  1818. if ((key === 'width' || key === 'height') && nodeName === 'rect' && value < 0) {
  1819. value = 0;
  1820. }
  1821. // Record for animation and quick access without polling the DOM
  1822. wrapper[key] = value;
  1823. if (key === 'text') {
  1824. if (value !== wrapper.textStr) {
  1825. // Delete bBox memo when the text changes
  1826. delete wrapper.bBox;
  1827. wrapper.textStr = value;
  1828. if (wrapper.added) {
  1829. renderer.buildText(wrapper);
  1830. }
  1831. }
  1832. } else if (!skipAttr) {
  1833. //attr(element, key, value);
  1834. if (value !== undefined) {
  1835. element.setAttribute(key, value);
  1836. }
  1837. }
  1838. }
  1839. }
  1840. // Update transform. Do this outside the loop to prevent redundant updating for batch setting
  1841. // of attributes.
  1842. if (doTransform) {
  1843. wrapper.updateTransform();
  1844. }
  1845. }
  1846. return ret;
  1847. },
  1848. /**
  1849. * Add a class name to an element
  1850. */
  1851. addClass: function (className) {
  1852. var element = this.element,
  1853. currentClassName = attr(element, 'class') || '';
  1854. if (currentClassName.indexOf(className) === -1) {
  1855. attr(element, 'class', currentClassName + ' ' + className);
  1856. }
  1857. return this;
  1858. },
  1859. /* hasClass and removeClass are not (yet) needed
  1860. hasClass: function (className) {
  1861. return attr(this.element, 'class').indexOf(className) !== -1;
  1862. },
  1863. removeClass: function (className) {
  1864. attr(this.element, 'class', attr(this.element, 'class').replace(className, ''));
  1865. return this;
  1866. },
  1867. */
  1868. /**
  1869. * If one of the symbol size affecting parameters are changed,
  1870. * check all the others only once for each call to an element's
  1871. * .attr() method
  1872. * @param {Object} hash
  1873. */
  1874. symbolAttr: function (hash) {
  1875. var wrapper = this;
  1876. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  1877. wrapper[key] = pick(hash[key], wrapper[key]);
  1878. });
  1879. wrapper.attr({
  1880. d: wrapper.renderer.symbols[wrapper.symbolName](
  1881. wrapper.x,
  1882. wrapper.y,
  1883. wrapper.width,
  1884. wrapper.height,
  1885. wrapper
  1886. )
  1887. });
  1888. },
  1889. /**
  1890. * Apply a clipping path to this object
  1891. * @param {String} id
  1892. */
  1893. clip: function (clipRect) {
  1894. return this.attr('clip-path', clipRect ? 'url(' + this.renderer.url + '#' + clipRect.id + ')' : NONE);
  1895. },
  1896. /**
  1897. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  1898. * calculated attributes
  1899. * @param {Number} strokeWidth
  1900. * @param {Number} x
  1901. * @param {Number} y
  1902. * @param {Number} width
  1903. * @param {Number} height
  1904. */
  1905. crisp: function (rect) {
  1906. var wrapper = this,
  1907. key,
  1908. attribs = {},
  1909. normalizer,
  1910. strokeWidth = rect.strokeWidth || wrapper.strokeWidth || (wrapper.attr && wrapper.attr('stroke-width')) || 0;
  1911. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  1912. // normalize for crisp edges
  1913. rect.x = mathFloor(rect.x || wrapper.x || 0) + normalizer;
  1914. rect.y = mathFloor(rect.y || wrapper.y || 0) + normalizer;
  1915. rect.width = mathFloor((rect.width || wrapper.width || 0) - 2 * normalizer);
  1916. rect.height = mathFloor((rect.height || wrapper.height || 0) - 2 * normalizer);
  1917. rect.strokeWidth = strokeWidth;
  1918. for (key in rect) {
  1919. if (wrapper[key] !== rect[key]) { // only set attribute if changed
  1920. wrapper[key] = attribs[key] = rect[key];
  1921. }
  1922. }
  1923. return attribs;
  1924. },
  1925. /**
  1926. * Set styles for the element
  1927. * @param {Object} styles
  1928. */
  1929. css: function (styles) {
  1930. var elemWrapper = this,
  1931. oldStyles = elemWrapper.styles,
  1932. newStyles = {},
  1933. elem = elemWrapper.element,
  1934. textWidth,
  1935. n,
  1936. serializedCss = '',
  1937. hyphenate,
  1938. hasNew = !oldStyles;
  1939. // convert legacy
  1940. if (styles && styles.color) {
  1941. styles.fill = styles.color;
  1942. }
  1943. // Filter out existing styles to increase performance (#2640)
  1944. if (oldStyles) {
  1945. for (n in styles) {
  1946. if (styles[n] !== oldStyles[n]) {
  1947. newStyles[n] = styles[n];
  1948. hasNew = true;
  1949. }
  1950. }
  1951. }
  1952. if (hasNew) {
  1953. textWidth = elemWrapper.textWidth = styles && styles.width && elem.nodeName.toLowerCase() === 'text' && pInt(styles.width);
  1954. // Merge the new styles with the old ones
  1955. if (oldStyles) {
  1956. styles = extend(
  1957. oldStyles,
  1958. newStyles
  1959. );
  1960. }
  1961. // store object
  1962. elemWrapper.styles = styles;
  1963. if (textWidth && (useCanVG || (!hasSVG && elemWrapper.renderer.forExport))) {
  1964. delete styles.width;
  1965. }
  1966. // serialize and set style attribute
  1967. if (isIE && !hasSVG) {
  1968. css(elemWrapper.element, styles);
  1969. } else {
  1970. /*jslint unparam: true*/
  1971. hyphenate = function (a, b) { return '-' + b.toLowerCase(); };
  1972. /*jslint unparam: false*/
  1973. for (n in styles) {
  1974. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  1975. }
  1976. attr(elem, 'style', serializedCss); // #1881
  1977. }
  1978. // re-build text
  1979. if (textWidth && elemWrapper.added) {
  1980. elemWrapper.renderer.buildText(elemWrapper);
  1981. }
  1982. }
  1983. return elemWrapper;
  1984. },
  1985. /**
  1986. * Add an event listener
  1987. * @param {String} eventType
  1988. * @param {Function} handler
  1989. */
  1990. on: function (eventType, handler) {
  1991. var svgElement = this,
  1992. element = svgElement.element;
  1993. // touch
  1994. if (hasTouch && eventType === 'click') {
  1995. element.ontouchstart = function (e) {
  1996. svgElement.touchEventFired = Date.now();
  1997. e.preventDefault();
  1998. handler.call(element, e);
  1999. };
  2000. element.onclick = function (e) {
  2001. if (userAgent.indexOf('Android') === -1 || Date.now() - (svgElement.touchEventFired || 0) > 1100) { // #2269
  2002. handler.call(element, e);
  2003. }
  2004. };
  2005. } else {
  2006. // simplest possible event model for internal use
  2007. element['on' + eventType] = handler;
  2008. }
  2009. return this;
  2010. },
  2011. /**
  2012. * Set the coordinates needed to draw a consistent radial gradient across
  2013. * pie slices regardless of positioning inside the chart. The format is
  2014. * [centerX, centerY, diameter] in pixels.
  2015. */
  2016. setRadialReference: function (coordinates) {
  2017. this.element.radialReference = coordinates;
  2018. return this;
  2019. },
  2020. /**
  2021. * Move an object and its children by x and y values
  2022. * @param {Number} x
  2023. * @param {Number} y
  2024. */
  2025. translate: function (x, y) {
  2026. return this.attr({
  2027. translateX: x,
  2028. translateY: y
  2029. });
  2030. },
  2031. /**
  2032. * Invert a group, rotate and flip
  2033. */
  2034. invert: function () {
  2035. var wrapper = this;
  2036. wrapper.inverted = true;
  2037. wrapper.updateTransform();
  2038. return wrapper;
  2039. },
  2040. /**
  2041. * Private method to update the transform attribute based on internal
  2042. * properties
  2043. */
  2044. updateTransform: function () {
  2045. var wrapper = this,
  2046. translateX = wrapper.translateX || 0,
  2047. translateY = wrapper.translateY || 0,
  2048. scaleX = wrapper.scaleX,
  2049. scaleY = wrapper.scaleY,
  2050. inverted = wrapper.inverted,
  2051. rotation = wrapper.rotation,
  2052. transform;
  2053. // flipping affects translate as adjustment for flipping around the group's axis
  2054. if (inverted) {
  2055. translateX += wrapper.attr('width');
  2056. translateY += wrapper.attr('height');
  2057. }
  2058. // Apply translate. Nearly all transformed elements have translation, so instead
  2059. // of checking for translate = 0, do it always (#1767, #1846).
  2060. transform = ['translate(' + translateX + ',' + translateY + ')'];
  2061. // apply rotation
  2062. if (inverted) {
  2063. transform.push('rotate(90) scale(-1,1)');
  2064. } else if (rotation) { // text rotation
  2065. transform.push('rotate(' + rotation + ' ' + (wrapper.x || 0) + ' ' + (wrapper.y || 0) + ')');
  2066. }
  2067. // apply scale
  2068. if (defined(scaleX) || defined(scaleY)) {
  2069. transform.push('scale(' + pick(scaleX, 1) + ' ' + pick(scaleY, 1) + ')');
  2070. }
  2071. if (transform.length) {
  2072. attr(wrapper.element, 'transform', transform.join(' '));
  2073. }
  2074. },
  2075. /**
  2076. * Bring the element to the front
  2077. */
  2078. toFront: function () {
  2079. var element = this.element;
  2080. element.parentNode.appendChild(element);
  2081. return this;
  2082. },
  2083. /**
  2084. * Break down alignment options like align, verticalAlign, x and y
  2085. * to x and y relative to the chart.
  2086. *
  2087. * @param {Object} alignOptions
  2088. * @param {Boolean} alignByTranslate
  2089. * @param {String[Object} box The box to align to, needs a width and height. When the
  2090. * box is a string, it refers to an object in the Renderer. For example, when
  2091. * box is 'spacingBox', it refers to Renderer.spacingBox which holds width, height
  2092. * x and y properties.
  2093. *
  2094. */
  2095. align: function (alignOptions, alignByTranslate, box) {
  2096. var align,
  2097. vAlign,
  2098. x,
  2099. y,
  2100. attribs = {},
  2101. alignTo,
  2102. renderer = this.renderer,
  2103. alignedObjects = renderer.alignedObjects;
  2104. // First call on instanciate
  2105. if (alignOptions) {
  2106. this.alignOptions = alignOptions;
  2107. this.alignByTranslate = alignByTranslate;
  2108. if (!box || isString(box)) { // boxes other than renderer handle this internally
  2109. this.alignTo = alignTo = box || 'renderer';
  2110. erase(alignedObjects, this); // prevent duplicates, like legendGroup after resize
  2111. alignedObjects.push(this);
  2112. box = null; // reassign it below
  2113. }
  2114. // When called on resize, no arguments are supplied
  2115. } else {
  2116. alignOptions = this.alignOptions;
  2117. alignByTranslate = this.alignByTranslate;
  2118. alignTo = this.alignTo;
  2119. }
  2120. box = pick(box, renderer[alignTo], renderer);
  2121. // Assign variables
  2122. align = alignOptions.align;
  2123. vAlign = alignOptions.verticalAlign;
  2124. x = (box.x || 0) + (alignOptions.x || 0); // default: left align
  2125. y = (box.y || 0) + (alignOptions.y || 0); // default: top align
  2126. // Align
  2127. if (align === 'right' || align === 'center') {
  2128. x += (box.width - (alignOptions.width || 0)) /
  2129. { right: 1, center: 2 }[align];
  2130. }
  2131. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2132. // Vertical align
  2133. if (vAlign === 'bottom' || vAlign === 'middle') {
  2134. y += (box.height - (alignOptions.height || 0)) /
  2135. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2136. }
  2137. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2138. // Animate only if already placed
  2139. this[this.placed ? 'animate' : 'attr'](attribs);
  2140. this.placed = true;
  2141. this.alignAttr = attribs;
  2142. return this;
  2143. },
  2144. /**
  2145. * Get the bounding box (width, height, x and y) for the element
  2146. */
  2147. getBBox: function () {
  2148. var wrapper = this,
  2149. bBox = wrapper.bBox,
  2150. renderer = wrapper.renderer,
  2151. width,
  2152. height,
  2153. rotation = wrapper.rotation,
  2154. element = wrapper.element,
  2155. styles = wrapper.styles,
  2156. rad = rotation * deg2rad,
  2157. textStr = wrapper.textStr,
  2158. numKey;
  2159. // Since numbers are monospaced, and numerical labels appear a lot in a chart,
  2160. // we assume that a label of n characters has the same bounding box as others
  2161. // of the same length.
  2162. if (textStr === '' || numRegex.test(textStr)) {
  2163. numKey = textStr.toString().length + (styles ? ('|' + styles.fontSize + '|' + styles.fontFamily) : '');
  2164. bBox = renderer.cache[numKey];
  2165. }
  2166. // No cache found
  2167. if (!bBox) {
  2168. // SVG elements
  2169. if (element.namespaceURI === SVG_NS || renderer.forExport) {
  2170. try { // Fails in Firefox if the container has display: none.
  2171. bBox = element.getBBox ?
  2172. // SVG: use extend because IE9 is not allowed to change width and height in case
  2173. // of rotation (below)
  2174. extend({}, element.getBBox()) :
  2175. // Canvas renderer and legacy IE in export mode
  2176. {
  2177. width: element.offsetWidth,
  2178. height: element.offsetHeight
  2179. };
  2180. } catch (e) {}
  2181. // If the bBox is not set, the try-catch block above failed. The other condition
  2182. // is for Opera that returns a width of -Infinity on hidden elements.
  2183. if (!bBox || bBox.width < 0) {
  2184. bBox = { width: 0, height: 0 };
  2185. }
  2186. // VML Renderer or useHTML within SVG
  2187. } else {
  2188. bBox = wrapper.htmlGetBBox();
  2189. }
  2190. // True SVG elements as well as HTML elements in modern browsers using the .useHTML option
  2191. // need to compensated for rotation
  2192. if (renderer.isSVG) {
  2193. width = bBox.width;
  2194. height = bBox.height;
  2195. // Workaround for wrong bounding box in IE9 and IE10 (#1101, #1505, #1669, #2568)
  2196. if (isIE && styles && styles.fontSize === '11px' && height.toPrecision(3) === '16.9') {
  2197. bBox.height = height = 14;
  2198. }
  2199. // Adjust for rotated text
  2200. if (rotation) {
  2201. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2202. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2203. }
  2204. }
  2205. // Cache it
  2206. wrapper.bBox = bBox;
  2207. if (numKey) {
  2208. renderer.cache[numKey] = bBox;
  2209. }
  2210. }
  2211. return bBox;
  2212. },
  2213. /**
  2214. * Show the element
  2215. */
  2216. show: function (inherit) {
  2217. return this.attr({ visibility: inherit ? 'inherit' : VISIBLE });
  2218. },
  2219. /**
  2220. * Hide the element
  2221. */
  2222. hide: function () {
  2223. return this.attr({ visibility: HIDDEN });
  2224. },
  2225. fadeOut: function (duration) {
  2226. var elemWrapper = this;
  2227. elemWrapper.animate({
  2228. opacity: 0
  2229. }, {
  2230. duration: duration || 150,
  2231. complete: function () {
  2232. elemWrapper.hide();
  2233. }
  2234. });
  2235. },
  2236. /**
  2237. * Add the element
  2238. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2239. * to append the element to the renderer.box.
  2240. */
  2241. add: function (parent) {
  2242. var renderer = this.renderer,
  2243. parentWrapper = parent || renderer,
  2244. parentNode = parentWrapper.element || renderer.box,
  2245. childNodes,
  2246. element = this.element,
  2247. zIndex = this.zIndex,
  2248. otherElement,
  2249. otherZIndex,
  2250. i,
  2251. inserted;
  2252. if (parent) {
  2253. this.parentGroup = parent;
  2254. }
  2255. // mark as inverted
  2256. this.parentInverted = parent && parent.inverted;
  2257. // build formatted text
  2258. if (this.textStr !== undefined) {
  2259. renderer.buildText(this);
  2260. }
  2261. // mark the container as having z indexed children
  2262. if (zIndex) {
  2263. parentWrapper.handleZ = true;
  2264. zIndex = pInt(zIndex);
  2265. }
  2266. // insert according to this and other elements' zIndex
  2267. if (parentWrapper.handleZ) { // this element or any of its siblings has a z index
  2268. childNodes = parentNode.childNodes;
  2269. for (i = 0; i < childNodes.length; i++) {
  2270. otherElement = childNodes[i];
  2271. otherZIndex = attr(otherElement, 'zIndex');
  2272. if (otherElement !== element && (
  2273. // insert before the first element with a higher zIndex
  2274. pInt(otherZIndex) > zIndex ||
  2275. // if no zIndex given, insert before the first element with a zIndex
  2276. (!defined(zIndex) && defined(otherZIndex))
  2277. )) {
  2278. parentNode.insertBefore(element, otherElement);
  2279. inserted = true;
  2280. break;
  2281. }
  2282. }
  2283. }
  2284. // default: append at the end
  2285. if (!inserted) {
  2286. parentNode.appendChild(element);
  2287. }
  2288. // mark as added
  2289. this.added = true;
  2290. // fire an event for internal hooks
  2291. if (this.onAdd) {
  2292. this.onAdd();
  2293. }
  2294. return this;
  2295. },
  2296. /**
  2297. * Removes a child either by removeChild or move to garbageBin.
  2298. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2299. */
  2300. safeRemoveChild: function (element) {
  2301. var parentNode = element.parentNode;
  2302. if (parentNode) {
  2303. parentNode.removeChild(element);
  2304. }
  2305. },
  2306. /**
  2307. * Destroy the element and element wrapper
  2308. */
  2309. destroy: function () {
  2310. var wrapper = this,
  2311. element = wrapper.element || {},
  2312. shadows = wrapper.shadows,
  2313. parentToClean = wrapper.renderer.isSVG && element.nodeName === 'SPAN' && wrapper.parentGroup,
  2314. grandParent,
  2315. key,
  2316. i;
  2317. // remove events
  2318. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = element.point = null;
  2319. stop(wrapper); // stop running animations
  2320. if (wrapper.clipPath) {
  2321. wrapper.clipPath = wrapper.clipPath.destroy();
  2322. }
  2323. // Destroy stops in case this is a gradient object
  2324. if (wrapper.stops) {
  2325. for (i = 0; i < wrapper.stops.length; i++) {
  2326. wrapper.stops[i] = wrapper.stops[i].destroy();
  2327. }
  2328. wrapper.stops = null;
  2329. }
  2330. // remove element
  2331. wrapper.safeRemoveChild(element);
  2332. // destroy shadows
  2333. if (shadows) {
  2334. each(shadows, function (shadow) {
  2335. wrapper.safeRemoveChild(shadow);
  2336. });
  2337. }
  2338. // In case of useHTML, clean up empty containers emulating SVG groups (#1960, #2393).
  2339. while (parentToClean && parentToClean.div.childNodes.length === 0) {
  2340. grandParent = parentToClean.parentGroup;
  2341. wrapper.safeRemoveChild(parentToClean.div);
  2342. delete parentToClean.div;
  2343. parentToClean = grandParent;
  2344. }
  2345. // remove from alignObjects
  2346. if (wrapper.alignTo) {
  2347. erase(wrapper.renderer.alignedObjects, wrapper);
  2348. }
  2349. for (key in wrapper) {
  2350. delete wrapper[key];
  2351. }
  2352. return null;
  2353. },
  2354. /**
  2355. * Add a shadow to the element. Must be done after the element is added to the DOM
  2356. * @param {Boolean|Object} shadowOptions
  2357. */
  2358. shadow: function (shadowOptions, group, cutOff) {
  2359. var shadows = [],
  2360. i,
  2361. shadow,
  2362. element = this.element,
  2363. strokeWidth,
  2364. shadowWidth,
  2365. shadowElementOpacity,
  2366. // compensate for inverted plot area
  2367. transform;
  2368. if (shadowOptions) {
  2369. shadowWidth = pick(shadowOptions.width, 3);
  2370. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  2371. transform = this.parentInverted ?
  2372. '(-1,-1)' :
  2373. '(' + pick(shadowOptions.offsetX, 1) + ', ' + pick(shadowOptions.offsetY, 1) + ')';
  2374. for (i = 1; i <= shadowWidth; i++) {
  2375. shadow = element.cloneNode(0);
  2376. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  2377. attr(shadow, {
  2378. 'isShadow': 'true',
  2379. 'stroke': shadowOptions.color || 'black',
  2380. 'stroke-opacity': shadowElementOpacity * i,
  2381. 'stroke-width': strokeWidth,
  2382. 'transform': 'translate' + transform,
  2383. 'fill': NONE
  2384. });
  2385. if (cutOff) {
  2386. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2387. shadow.cutHeight = strokeWidth;
  2388. }
  2389. if (group) {
  2390. group.element.appendChild(shadow);
  2391. } else {
  2392. element.parentNode.insertBefore(shadow, element);
  2393. }
  2394. shadows.push(shadow);
  2395. }
  2396. this.shadows = shadows;
  2397. }
  2398. return this;
  2399. }
  2400. };
  2401. /**
  2402. * The default SVG renderer
  2403. */
  2404. var SVGRenderer = function () {
  2405. this.init.apply(this, arguments);
  2406. };
  2407. SVGRenderer.prototype = {
  2408. Element: SVGElement,
  2409. /**
  2410. * Initialize the SVGRenderer
  2411. * @param {Object} container
  2412. * @param {Number} width
  2413. * @param {Number} height
  2414. * @param {Boolean} forExport
  2415. */
  2416. init: function (container, width, height, style, forExport) {
  2417. var renderer = this,
  2418. loc = location,
  2419. boxWrapper,
  2420. element,
  2421. desc;
  2422. boxWrapper = renderer.createElement('svg')
  2423. .attr({
  2424. version: '1.1'
  2425. })
  2426. .css(this.getStyle(style));
  2427. element = boxWrapper.element;
  2428. container.appendChild(element);
  2429. // For browsers other than IE, add the namespace attribute (#1978)
  2430. if (container.innerHTML.indexOf('xmlns') === -1) {
  2431. attr(element, 'xmlns', SVG_NS);
  2432. }
  2433. // object properties
  2434. renderer.isSVG = true;
  2435. renderer.box = element;
  2436. renderer.boxWrapper = boxWrapper;
  2437. renderer.alignedObjects = [];
  2438. // Page url used for internal references. #24, #672, #1070
  2439. renderer.url = (isFirefox || isWebKit) && doc.getElementsByTagName('base').length ?
  2440. loc.href
  2441. .replace(/#.*?$/, '') // remove the hash
  2442. .replace(/([\('\)])/g, '\\$1') // escape parantheses and quotes
  2443. .replace(/ /g, '%20') : // replace spaces (needed for Safari only)
  2444. '';
  2445. // Add description
  2446. desc = this.createElement('desc').add();
  2447. desc.element.appendChild(doc.createTextNode('Created with ' + PRODUCT + ' ' + VERSION));
  2448. renderer.defs = this.createElement('defs').add();
  2449. renderer.forExport = forExport;
  2450. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2451. renderer.cache = {}; // Cache for numerical bounding boxes
  2452. renderer.setSize(width, height, false);
  2453. // Issue 110 workaround:
  2454. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2455. // between pixels. The container itself doesn't display this, but an SVG element
  2456. // inside this container will be drawn at subpixel precision. In order to draw
  2457. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2458. // iframes though (like in jsFiddle).
  2459. var subPixelFix, rect;
  2460. if (isFirefox && container.getBoundingClientRect) {
  2461. renderer.subPixelFix = subPixelFix = function () {
  2462. css(container, { left: 0, top: 0 });
  2463. rect = container.getBoundingClientRect();
  2464. css(container, {
  2465. left: (mathCeil(rect.left) - rect.left) + PX,
  2466. top: (mathCeil(rect.top) - rect.top) + PX
  2467. });
  2468. };
  2469. // run the fix now
  2470. subPixelFix();
  2471. // run it on resize
  2472. addEvent(win, 'resize', subPixelFix);
  2473. }
  2474. },
  2475. getStyle: function (style) {
  2476. return (this.style = extend({
  2477. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  2478. fontSize: '12px'
  2479. }, style));
  2480. },
  2481. /**
  2482. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2483. * has display: none. #608.
  2484. */
  2485. isHidden: function () {
  2486. return !this.boxWrapper.getBBox().width;
  2487. },
  2488. /**
  2489. * Destroys the renderer and its allocated members.
  2490. */
  2491. destroy: function () {
  2492. var renderer = this,
  2493. rendererDefs = renderer.defs;
  2494. renderer.box = null;
  2495. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2496. // Call destroy on all gradient elements
  2497. destroyObjectProperties(renderer.gradients || {});
  2498. renderer.gradients = null;
  2499. // Defs are null in VMLRenderer
  2500. // Otherwise, destroy them here.
  2501. if (rendererDefs) {
  2502. renderer.defs = rendererDefs.destroy();
  2503. }
  2504. // Remove sub pixel fix handler
  2505. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2506. // See issue #982
  2507. if (renderer.subPixelFix) {
  2508. removeEvent(win, 'resize', renderer.subPixelFix);
  2509. }
  2510. renderer.alignedObjects = null;
  2511. return null;
  2512. },
  2513. /**
  2514. * Create a wrapper for an SVG element
  2515. * @param {Object} nodeName
  2516. */
  2517. createElement: function (nodeName) {
  2518. var wrapper = new this.Element();
  2519. wrapper.init(this, nodeName);
  2520. return wrapper;
  2521. },
  2522. /**
  2523. * Dummy function for use in canvas renderer
  2524. */
  2525. draw: function () {},
  2526. /**
  2527. * Parse a simple HTML string into SVG tspans
  2528. *
  2529. * @param {Object} textNode The parent text SVG node
  2530. */
  2531. buildText: function (wrapper) {
  2532. var textNode = wrapper.element,
  2533. renderer = this,
  2534. forExport = renderer.forExport,
  2535. lines = pick(wrapper.textStr, '').toString()
  2536. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2537. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2538. .replace(/<a/g, '<span')
  2539. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2540. .split(/<br.*?>/g),
  2541. childNodes = textNode.childNodes,
  2542. styleRegex = /<.*style="([^"]+)".*>/,
  2543. hrefRegex = /<.*href="(http[^"]+)".*>/,
  2544. parentX = attr(textNode, 'x'),
  2545. textStyles = wrapper.styles,
  2546. width = wrapper.textWidth,
  2547. textLineHeight = textStyles && textStyles.lineHeight,
  2548. i = childNodes.length,
  2549. getLineHeight = function (tspan) {
  2550. return textLineHeight ?
  2551. pInt(textLineHeight) :
  2552. renderer.fontMetrics(
  2553. /(px|em)$/.test(tspan && tspan.style.fontSize) ?
  2554. tspan.style.fontSize :
  2555. (textStyles.fontSize || 11)
  2556. ).h;
  2557. };
  2558. /// remove old text
  2559. while (i--) {
  2560. textNode.removeChild(childNodes[i]);
  2561. }
  2562. if (width && !wrapper.added) {
  2563. this.box.appendChild(textNode); // attach it to the DOM to read offset width
  2564. }
  2565. // remove empty line at end
  2566. if (lines[lines.length - 1] === '') {
  2567. lines.pop();
  2568. }
  2569. // build the lines
  2570. each(lines, function (line, lineNo) {
  2571. var spans, spanNo = 0;
  2572. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2573. spans = line.split('|||');
  2574. each(spans, function (span) {
  2575. if (span !== '' || spans.length === 1) {
  2576. var attributes = {},
  2577. tspan = doc.createElementNS(SVG_NS, 'tspan'),
  2578. spanStyle; // #390
  2579. if (styleRegex.test(span)) {
  2580. spanStyle = span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2');
  2581. attr(tspan, 'style', spanStyle);
  2582. }
  2583. if (hrefRegex.test(span) && !forExport) { // Not for export - #1529
  2584. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2585. css(tspan, { cursor: 'pointer' });
  2586. }
  2587. span = (span.replace(/<(.|\n)*?>/g, '') || ' ')
  2588. .replace(/&lt;/g, '<')
  2589. .replace(/&gt;/g, '>');
  2590. // Nested tags aren't supported, and cause crash in Safari (#1596)
  2591. if (span !== ' ') {
  2592. // add the text node
  2593. tspan.appendChild(doc.createTextNode(span));
  2594. if (!spanNo) { // first span in a line, align it to the left
  2595. attributes.x = parentX;
  2596. } else {
  2597. attributes.dx = 0; // #16
  2598. }
  2599. // add attributes
  2600. attr(tspan, attributes);
  2601. // first span on subsequent line, add the line height
  2602. if (!spanNo && lineNo) {
  2603. // allow getting the right offset height in exporting in IE
  2604. if (!hasSVG && forExport) {
  2605. css(tspan, { display: 'block' });
  2606. }
  2607. // Set the line height based on the font size of either
  2608. // the text element or the tspan element
  2609. attr(
  2610. tspan,
  2611. 'dy',
  2612. getLineHeight(tspan),
  2613. // Safari 6.0.2 - too optimized for its own good (#1539)
  2614. // TODO: revisit this with future versions of Safari
  2615. isWebKit && tspan.offsetHeight
  2616. );
  2617. }
  2618. // Append it
  2619. textNode.appendChild(tspan);
  2620. spanNo++;
  2621. // check width and apply soft breaks
  2622. if (width) {
  2623. var words = span.replace(/([^\^])-/g, '$1- ').split(' '), // #1273
  2624. hasWhiteSpace = words.length > 1 && textStyles.whiteSpace !== 'nowrap',
  2625. tooLong,
  2626. actualWidth,
  2627. clipHeight = wrapper._clipHeight,
  2628. rest = [],
  2629. dy = getLineHeight(),
  2630. softLineNo = 1,
  2631. bBox;
  2632. while (hasWhiteSpace && (words.length || rest.length)) {
  2633. delete wrapper.bBox; // delete cache
  2634. bBox = wrapper.getBBox();
  2635. actualWidth = bBox.width;
  2636. // Old IE cannot measure the actualWidth for SVG elements (#2314)
  2637. if (!hasSVG && renderer.forExport) {
  2638. actualWidth = renderer.measureSpanWidth(tspan.firstChild.data, wrapper.styles);
  2639. }
  2640. tooLong = actualWidth > width;
  2641. if (!tooLong || words.length === 1) { // new line needed
  2642. words = rest;
  2643. rest = [];
  2644. if (words.length) {
  2645. softLineNo++;
  2646. if (clipHeight && softLineNo * dy > clipHeight) {
  2647. words = ['...'];
  2648. wrapper.attr('title', wrapper.textStr);
  2649. } else {
  2650. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2651. attr(tspan, {
  2652. dy: dy,
  2653. x: parentX
  2654. });
  2655. if (spanStyle) { // #390
  2656. attr(tspan, 'style', spanStyle);
  2657. }
  2658. textNode.appendChild(tspan);
  2659. if (actualWidth > width) { // a single word is pressing it out
  2660. width = actualWidth;
  2661. }
  2662. }
  2663. }
  2664. } else { // append to existing line tspan
  2665. tspan.removeChild(tspan.firstChild);
  2666. rest.unshift(words.pop());
  2667. }
  2668. if (words.length) {
  2669. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  2670. }
  2671. }
  2672. }
  2673. }
  2674. }
  2675. });
  2676. });
  2677. },
  2678. /**
  2679. * Create a button with preset states
  2680. * @param {String} text
  2681. * @param {Number} x
  2682. * @param {Number} y
  2683. * @param {Function} callback
  2684. * @param {Object} normalState
  2685. * @param {Object} hoverState
  2686. * @param {Object} pressedState
  2687. */
  2688. button: function (text, x, y, callback, normalState, hoverState, pressedState, disabledState, shape) {
  2689. var label = this.label(text, x, y, shape, null, null, null, null, 'button'),
  2690. curState = 0,
  2691. stateOptions,
  2692. stateStyle,
  2693. normalStyle,
  2694. hoverStyle,
  2695. pressedStyle,
  2696. disabledStyle,
  2697. STYLE = 'style',
  2698. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  2699. // Normal state - prepare the attributes
  2700. normalState = merge({
  2701. 'stroke-width': 1,
  2702. stroke: '#CCCCCC',
  2703. fill: {
  2704. linearGradient: verticalGradient,
  2705. stops: [
  2706. [0, '#FEFEFE'],
  2707. [1, '#F6F6F6']
  2708. ]
  2709. },
  2710. r: 2,
  2711. padding: 5,
  2712. style: {
  2713. color: 'black'
  2714. }
  2715. }, normalState);
  2716. normalStyle = normalState[STYLE];
  2717. delete normalState[STYLE];
  2718. // Hover state
  2719. hoverState = merge(normalState, {
  2720. stroke: '#68A',
  2721. fill: {
  2722. linearGradient: verticalGradient,
  2723. stops: [
  2724. [0, '#FFF'],
  2725. [1, '#ACF']
  2726. ]
  2727. }
  2728. }, hoverState);
  2729. hoverStyle = hoverState[STYLE];
  2730. delete hoverState[STYLE];
  2731. // Pressed state
  2732. pressedState = merge(normalState, {
  2733. stroke: '#68A',
  2734. fill: {
  2735. linearGradient: verticalGradient,
  2736. stops: [
  2737. [0, '#9BD'],
  2738. [1, '#CDF']
  2739. ]
  2740. }
  2741. }, pressedState);
  2742. pressedStyle = pressedState[STYLE];
  2743. delete pressedState[STYLE];
  2744. // Disabled state
  2745. disabledState = merge(normalState, {
  2746. style: {
  2747. color: '#CCC'
  2748. }
  2749. }, disabledState);
  2750. disabledStyle = disabledState[STYLE];
  2751. delete disabledState[STYLE];
  2752. // Add the events. IE9 and IE10 need mouseover and mouseout to funciton (#667).
  2753. addEvent(label.element, isIE ? 'mouseover' : 'mouseenter', function () {
  2754. if (curState !== 3) {
  2755. label.attr(hoverState)
  2756. .css(hoverStyle);
  2757. }
  2758. });
  2759. addEvent(label.element, isIE ? 'mouseout' : 'mouseleave', function () {
  2760. if (curState !== 3) {
  2761. stateOptions = [normalState, hoverState, pressedState][curState];
  2762. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  2763. label.attr(stateOptions)
  2764. .css(stateStyle);
  2765. }
  2766. });
  2767. label.setState = function (state) {
  2768. label.state = curState = state;
  2769. if (!state) {
  2770. label.attr(normalState)
  2771. .css(normalStyle);
  2772. } else if (state === 2) {
  2773. label.attr(pressedState)
  2774. .css(pressedStyle);
  2775. } else if (state === 3) {
  2776. label.attr(disabledState)
  2777. .css(disabledStyle);
  2778. }
  2779. };
  2780. return label
  2781. .on('click', function () {
  2782. if (curState !== 3) {
  2783. callback.call(label);
  2784. }
  2785. })
  2786. .attr(normalState)
  2787. .css(extend({ cursor: 'default' }, normalStyle));
  2788. },
  2789. /**
  2790. * Make a straight line crisper by not spilling out to neighbour pixels
  2791. * @param {Array} points
  2792. * @param {Number} width
  2793. */
  2794. crispLine: function (points, width) {
  2795. // points format: [M, 0, 0, L, 100, 0]
  2796. // normalize to a crisp line
  2797. if (points[1] === points[4]) {
  2798. // Substract due to #1129. Now bottom and left axis gridlines behave the same.
  2799. points[1] = points[4] = mathRound(points[1]) - (width % 2 / 2);
  2800. }
  2801. if (points[2] === points[5]) {
  2802. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  2803. }
  2804. return points;
  2805. },
  2806. /**
  2807. * Draw a path
  2808. * @param {Array} path An SVG path in array form
  2809. */
  2810. path: function (path) {
  2811. var attr = {
  2812. fill: NONE
  2813. };
  2814. if (isArray(path)) {
  2815. attr.d = path;
  2816. } else if (isObject(path)) { // attributes
  2817. extend(attr, path);
  2818. }
  2819. return this.createElement('path').attr(attr);
  2820. },
  2821. /**
  2822. * Draw and return an SVG circle
  2823. * @param {Number} x The x position
  2824. * @param {Number} y The y position
  2825. * @param {Number} r The radius
  2826. */
  2827. circle: function (x, y, r) {
  2828. var attr = isObject(x) ?
  2829. x :
  2830. {
  2831. x: x,
  2832. y: y,
  2833. r: r
  2834. };
  2835. return this.createElement('circle').attr(attr);
  2836. },
  2837. /**
  2838. * Draw and return an arc
  2839. * @param {Number} x X position
  2840. * @param {Number} y Y position
  2841. * @param {Number} r Radius
  2842. * @param {Number} innerR Inner radius like used in donut charts
  2843. * @param {Number} start Starting angle
  2844. * @param {Number} end Ending angle
  2845. */
  2846. arc: function (x, y, r, innerR, start, end) {
  2847. var arc;
  2848. if (isObject(x)) {
  2849. y = x.y;
  2850. r = x.r;
  2851. innerR = x.innerR;
  2852. start = x.start;
  2853. end = x.end;
  2854. x = x.x;
  2855. }
  2856. // Arcs are defined as symbols for the ability to set
  2857. // attributes in attr and animate
  2858. arc = this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  2859. innerR: innerR || 0,
  2860. start: start || 0,
  2861. end: end || 0
  2862. });
  2863. arc.r = r; // #959
  2864. return arc;
  2865. },
  2866. /**
  2867. * Draw and return a rectangle
  2868. * @param {Number} x Left position
  2869. * @param {Number} y Top position
  2870. * @param {Number} width
  2871. * @param {Number} height
  2872. * @param {Number} r Border corner radius
  2873. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  2874. */
  2875. rect: function (x, y, width, height, r, strokeWidth) {
  2876. r = isObject(x) ? x.r : r;
  2877. var wrapper = this.createElement('rect'),
  2878. attr = isObject(x) ? x : x === UNDEFINED ? {} : {
  2879. x: x,
  2880. y: y,
  2881. width: mathMax(width, 0),
  2882. height: mathMax(height, 0)
  2883. };
  2884. if (strokeWidth !== UNDEFINED) {
  2885. attr.strokeWidth = strokeWidth;
  2886. attr = wrapper.crisp(attr);
  2887. }
  2888. if (r) {
  2889. attr.r = r;
  2890. }
  2891. return wrapper.attr(attr);
  2892. },
  2893. /**
  2894. * Resize the box and re-align all aligned elements
  2895. * @param {Object} width
  2896. * @param {Object} height
  2897. * @param {Boolean} animate
  2898. *
  2899. */
  2900. setSize: function (width, height, animate) {
  2901. var renderer = this,
  2902. alignedObjects = renderer.alignedObjects,
  2903. i = alignedObjects.length;
  2904. renderer.width = width;
  2905. renderer.height = height;
  2906. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  2907. width: width,
  2908. height: height
  2909. });
  2910. while (i--) {
  2911. alignedObjects[i].align();
  2912. }
  2913. },
  2914. /**
  2915. * Create a group
  2916. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  2917. * This can be used for styling and scripting.
  2918. */
  2919. g: function (name) {
  2920. var elem = this.createElement('g');
  2921. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  2922. },
  2923. /**
  2924. * Display an image
  2925. * @param {String} src
  2926. * @param {Number} x
  2927. * @param {Number} y
  2928. * @param {Number} width
  2929. * @param {Number} height
  2930. */
  2931. image: function (src, x, y, width, height) {
  2932. var attribs = {
  2933. preserveAspectRatio: NONE
  2934. },
  2935. elemWrapper;
  2936. // optional properties
  2937. if (arguments.length > 1) {
  2938. extend(attribs, {
  2939. x: x,
  2940. y: y,
  2941. width: width,
  2942. height: height
  2943. });
  2944. }
  2945. elemWrapper = this.createElement('image').attr(attribs);
  2946. // set the href in the xlink namespace
  2947. if (elemWrapper.element.setAttributeNS) {
  2948. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  2949. 'href', src);
  2950. } else {
  2951. // could be exporting in IE
  2952. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  2953. elemWrapper.element.setAttribute('hc-svg-href', src);
  2954. }
  2955. return elemWrapper;
  2956. },
  2957. /**
  2958. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  2959. *
  2960. * @param {Object} symbol
  2961. * @param {Object} x
  2962. * @param {Object} y
  2963. * @param {Object} radius
  2964. * @param {Object} options
  2965. */
  2966. symbol: function (symbol, x, y, width, height, options) {
  2967. var obj,
  2968. // get the symbol definition function
  2969. symbolFn = this.symbols[symbol],
  2970. // check if there's a path defined for this symbol
  2971. path = symbolFn && symbolFn(
  2972. mathRound(x),
  2973. mathRound(y),
  2974. width,
  2975. height,
  2976. options
  2977. ),
  2978. imageElement,
  2979. imageRegex = /^url\((.*?)\)$/,
  2980. imageSrc,
  2981. imageSize,
  2982. centerImage;
  2983. if (path) {
  2984. obj = this.path(path);
  2985. // expando properties for use in animate and attr
  2986. extend(obj, {
  2987. symbolName: symbol,
  2988. x: x,
  2989. y: y,
  2990. width: width,
  2991. height: height
  2992. });
  2993. if (options) {
  2994. extend(obj, options);
  2995. }
  2996. // image symbols
  2997. } else if (imageRegex.test(symbol)) {
  2998. // On image load, set the size and position
  2999. centerImage = function (img, size) {
  3000. if (img.element) { // it may be destroyed in the meantime (#1390)
  3001. img.attr({
  3002. width: size[0],
  3003. height: size[1]
  3004. });
  3005. if (!img.alignByTranslate) { // #185
  3006. img.translate(
  3007. mathRound((width - size[0]) / 2), // #1378
  3008. mathRound((height - size[1]) / 2)
  3009. );
  3010. }
  3011. }
  3012. };
  3013. imageSrc = symbol.match(imageRegex)[1];
  3014. imageSize = symbolSizes[imageSrc];
  3015. // Ireate the image synchronously, add attribs async
  3016. obj = this.image(imageSrc)
  3017. .attr({
  3018. x: x,
  3019. y: y
  3020. });
  3021. obj.isImg = true;
  3022. if (imageSize) {
  3023. centerImage(obj, imageSize);
  3024. } else {
  3025. // Initialize image to be 0 size so export will still function if there's no cached sizes.
  3026. //
  3027. obj.attr({ width: 0, height: 0 });
  3028. // Create a dummy JavaScript image to get the width and height. Due to a bug in IE < 8,
  3029. // the created element must be assigned to a variable in order to load (#292).
  3030. imageElement = createElement('img', {
  3031. onload: function () {
  3032. centerImage(obj, symbolSizes[imageSrc] = [this.width, this.height]);
  3033. },
  3034. src: imageSrc
  3035. });
  3036. }
  3037. }
  3038. return obj;
  3039. },
  3040. /**
  3041. * An extendable collection of functions for defining symbol paths.
  3042. */
  3043. symbols: {
  3044. 'circle': function (x, y, w, h) {
  3045. var cpw = 0.166 * w;
  3046. return [
  3047. M, x + w / 2, y,
  3048. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3049. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3050. 'Z'
  3051. ];
  3052. },
  3053. 'square': function (x, y, w, h) {
  3054. return [
  3055. M, x, y,
  3056. L, x + w, y,
  3057. x + w, y + h,
  3058. x, y + h,
  3059. 'Z'
  3060. ];
  3061. },
  3062. 'triangle': function (x, y, w, h) {
  3063. return [
  3064. M, x + w / 2, y,
  3065. L, x + w, y + h,
  3066. x, y + h,
  3067. 'Z'
  3068. ];
  3069. },
  3070. 'triangle-down': function (x, y, w, h) {
  3071. return [
  3072. M, x, y,
  3073. L, x + w, y,
  3074. x + w / 2, y + h,
  3075. 'Z'
  3076. ];
  3077. },
  3078. 'diamond': function (x, y, w, h) {
  3079. return [
  3080. M, x + w / 2, y,
  3081. L, x + w, y + h / 2,
  3082. x + w / 2, y + h,
  3083. x, y + h / 2,
  3084. 'Z'
  3085. ];
  3086. },
  3087. 'arc': function (x, y, w, h, options) {
  3088. var start = options.start,
  3089. radius = options.r || w || h,
  3090. end = options.end - 0.001, // to prevent cos and sin of start and end from becoming equal on 360 arcs (related: #1561)
  3091. innerRadius = options.innerR,
  3092. open = options.open,
  3093. cosStart = mathCos(start),
  3094. sinStart = mathSin(start),
  3095. cosEnd = mathCos(end),
  3096. sinEnd = mathSin(end),
  3097. longArc = options.end - start < mathPI ? 0 : 1;
  3098. return [
  3099. M,
  3100. x + radius * cosStart,
  3101. y + radius * sinStart,
  3102. 'A', // arcTo
  3103. radius, // x radius
  3104. radius, // y radius
  3105. 0, // slanting
  3106. longArc, // long or short arc
  3107. 1, // clockwise
  3108. x + radius * cosEnd,
  3109. y + radius * sinEnd,
  3110. open ? M : L,
  3111. x + innerRadius * cosEnd,
  3112. y + innerRadius * sinEnd,
  3113. 'A', // arcTo
  3114. innerRadius, // x radius
  3115. innerRadius, // y radius
  3116. 0, // slanting
  3117. longArc, // long or short arc
  3118. 0, // clockwise
  3119. x + innerRadius * cosStart,
  3120. y + innerRadius * sinStart,
  3121. open ? '' : 'Z' // close
  3122. ];
  3123. }
  3124. },
  3125. /**
  3126. * Define a clipping rectangle
  3127. * @param {String} id
  3128. * @param {Number} x
  3129. * @param {Number} y
  3130. * @param {Number} width
  3131. * @param {Number} height
  3132. */
  3133. clipRect: function (x, y, width, height) {
  3134. var wrapper,
  3135. id = PREFIX + idCounter++,
  3136. clipPath = this.createElement('clipPath').attr({
  3137. id: id
  3138. }).add(this.defs);
  3139. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3140. wrapper.id = id;
  3141. wrapper.clipPath = clipPath;
  3142. return wrapper;
  3143. },
  3144. /**
  3145. * Take a color and return it if it's a string, make it a gradient if it's a
  3146. * gradient configuration object. Prior to Highstock, an array was used to define
  3147. * a linear gradient with pixel positions relative to the SVG. In newer versions
  3148. * we change the coordinates to apply relative to the shape, using coordinates
  3149. * 0-1 within the shape. To preserve backwards compatibility, linearGradient
  3150. * in this definition is an object of x1, y1, x2 and y2.
  3151. *
  3152. * @param {Object} color The color or config object
  3153. */
  3154. color: function (color, elem, prop) {
  3155. var renderer = this,
  3156. colorObject,
  3157. regexRgba = /^rgba/,
  3158. gradName,
  3159. gradAttr,
  3160. gradients,
  3161. gradientObject,
  3162. stops,
  3163. stopColor,
  3164. stopOpacity,
  3165. radialReference,
  3166. n,
  3167. id,
  3168. key = [];
  3169. // Apply linear or radial gradients
  3170. if (color && color.linearGradient) {
  3171. gradName = 'linearGradient';
  3172. } else if (color && color.radialGradient) {
  3173. gradName = 'radialGradient';
  3174. }
  3175. if (gradName) {
  3176. gradAttr = color[gradName];
  3177. gradients = renderer.gradients;
  3178. stops = color.stops;
  3179. radialReference = elem.radialReference;
  3180. // Keep < 2.2 kompatibility
  3181. if (isArray(gradAttr)) {
  3182. color[gradName] = gradAttr = {
  3183. x1: gradAttr[0],
  3184. y1: gradAttr[1],
  3185. x2: gradAttr[2],
  3186. y2: gradAttr[3],
  3187. gradientUnits: 'userSpaceOnUse'
  3188. };
  3189. }
  3190. // Correct the radial gradient for the radial reference system
  3191. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  3192. gradAttr = merge(gradAttr, {
  3193. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  3194. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  3195. r: gradAttr.r * radialReference[2],
  3196. gradientUnits: 'userSpaceOnUse'
  3197. });
  3198. }
  3199. // Build the unique key to detect whether we need to create a new element (#1282)
  3200. for (n in gradAttr) {
  3201. if (n !== 'id') {
  3202. key.push(n, gradAttr[n]);
  3203. }
  3204. }
  3205. for (n in stops) {
  3206. key.push(stops[n]);
  3207. }
  3208. key = key.join(',');
  3209. // Check if a gradient object with the same config object is created within this renderer
  3210. if (gradients[key]) {
  3211. id = gradients[key].id;
  3212. } else {
  3213. // Set the id and create the element
  3214. gradAttr.id = id = PREFIX + idCounter++;
  3215. gradients[key] = gradientObject = renderer.createElement(gradName)
  3216. .attr(gradAttr)
  3217. .add(renderer.defs);
  3218. // The gradient needs to keep a list of stops to be able to destroy them
  3219. gradientObject.stops = [];
  3220. each(stops, function (stop) {
  3221. var stopObject;
  3222. if (regexRgba.test(stop[1])) {
  3223. colorObject = Color(stop[1]);
  3224. stopColor = colorObject.get('rgb');
  3225. stopOpacity = colorObject.get('a');
  3226. } else {
  3227. stopColor = stop[1];
  3228. stopOpacity = 1;
  3229. }
  3230. stopObject = renderer.createElement('stop').attr({
  3231. offset: stop[0],
  3232. 'stop-color': stopColor,
  3233. 'stop-opacity': stopOpacity
  3234. }).add(gradientObject);
  3235. // Add the stop element to the gradient
  3236. gradientObject.stops.push(stopObject);
  3237. });
  3238. }
  3239. // Return the reference to the gradient object
  3240. return 'url(' + renderer.url + '#' + id + ')';
  3241. // Webkit and Batik can't show rgba.
  3242. } else if (regexRgba.test(color)) {
  3243. colorObject = Color(color);
  3244. attr(elem, prop + '-opacity', colorObject.get('a'));
  3245. return colorObject.get('rgb');
  3246. } else {
  3247. // Remove the opacity attribute added above. Does not throw if the attribute is not there.
  3248. elem.removeAttribute(prop + '-opacity');
  3249. return color;
  3250. }
  3251. },
  3252. /**
  3253. * Add text to the SVG object
  3254. * @param {String} str
  3255. * @param {Number} x Left position
  3256. * @param {Number} y Top position
  3257. * @param {Boolean} useHTML Use HTML to render the text
  3258. */
  3259. text: function (str, x, y, useHTML) {
  3260. // declare variables
  3261. var renderer = this,
  3262. fakeSVG = useCanVG || (!hasSVG && renderer.forExport),
  3263. wrapper;
  3264. if (useHTML && !renderer.forExport) {
  3265. return renderer.html(str, x, y);
  3266. }
  3267. x = mathRound(pick(x, 0));
  3268. y = mathRound(pick(y, 0));
  3269. wrapper = renderer.createElement('text')
  3270. .attr({
  3271. x: x,
  3272. y: y,
  3273. text: str
  3274. });
  3275. // Prevent wrapping from creating false offsetWidths in export in legacy IE (#1079, #1063)
  3276. if (fakeSVG) {
  3277. wrapper.css({
  3278. position: ABSOLUTE
  3279. });
  3280. }
  3281. wrapper.x = x;
  3282. wrapper.y = y;
  3283. return wrapper;
  3284. },
  3285. /**
  3286. * Utility to return the baseline offset and total line height from the font size
  3287. */
  3288. fontMetrics: function (fontSize) {
  3289. fontSize = fontSize || this.style.fontSize;
  3290. fontSize = /px/.test(fontSize) ? pInt(fontSize) : /em/.test(fontSize) ? parseFloat(fontSize) * 12 : 12;
  3291. // Empirical values found by comparing font size and bounding box height.
  3292. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3293. var lineHeight = fontSize < 24 ? fontSize + 4 : mathRound(fontSize * 1.2),
  3294. baseline = mathRound(lineHeight * 0.8);
  3295. return {
  3296. h: lineHeight,
  3297. b: baseline
  3298. };
  3299. },
  3300. /**
  3301. * Add a label, a text item that can hold a colored or gradient background
  3302. * as well as a border and shadow.
  3303. * @param {string} str
  3304. * @param {Number} x
  3305. * @param {Number} y
  3306. * @param {String} shape
  3307. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3308. * coordinates it should be pinned to
  3309. * @param {Number} anchorY
  3310. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3311. * like renderer.text, or to the upper border of the rectangle.
  3312. * @param {String} className Class name for the group
  3313. */
  3314. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3315. var renderer = this,
  3316. wrapper = renderer.g(className),
  3317. text = renderer.text('', 0, 0, useHTML)
  3318. .attr({
  3319. zIndex: 1
  3320. }),
  3321. //.add(wrapper),
  3322. box,
  3323. bBox,
  3324. alignFactor = 0,
  3325. padding = 3,
  3326. paddingLeft = 0,
  3327. width,
  3328. height,
  3329. wrapperX,
  3330. wrapperY,
  3331. crispAdjust = 0,
  3332. deferredAttr = {},
  3333. baselineOffset,
  3334. attrSetters = wrapper.attrSetters,
  3335. needsBox;
  3336. /**
  3337. * This function runs after the label is added to the DOM (when the bounding box is
  3338. * available), and after the text of the label is updated to detect the new bounding
  3339. * box and reflect it in the border box.
  3340. */
  3341. function updateBoxSize() {
  3342. var boxX,
  3343. boxY,
  3344. style = text.element.style;
  3345. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) && text.textStr &&
  3346. text.getBBox();
  3347. wrapper.width = (width || bBox.width || 0) + 2 * padding + paddingLeft;
  3348. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3349. // update the label-scoped y offset
  3350. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize).b;
  3351. if (needsBox) {
  3352. // create the border box if it is not already present
  3353. if (!box) {
  3354. boxX = mathRound(-alignFactor * padding);
  3355. boxY = baseline ? -baselineOffset : 0;
  3356. wrapper.box = box = shape ?
  3357. renderer.symbol(shape, boxX, boxY, wrapper.width, wrapper.height, deferredAttr) :
  3358. renderer.rect(boxX, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3359. box.attr('fill', NONE).add(wrapper);
  3360. }
  3361. // apply the box attributes
  3362. if (!box.isImg) { // #1630
  3363. box.attr(merge({
  3364. width: wrapper.width,
  3365. height: wrapper.height
  3366. }, deferredAttr));
  3367. }
  3368. deferredAttr = null;
  3369. }
  3370. }
  3371. /**
  3372. * This function runs after setting text or padding, but only if padding is changed
  3373. */
  3374. function updateTextPadding() {
  3375. var styles = wrapper.styles,
  3376. textAlign = styles && styles.textAlign,
  3377. x = paddingLeft + padding * (1 - alignFactor),
  3378. y;
  3379. // determin y based on the baseline
  3380. y = baseline ? 0 : baselineOffset;
  3381. // compensate for alignment
  3382. if (defined(width) && bBox && (textAlign === 'center' || textAlign === 'right')) {
  3383. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3384. }
  3385. // update if anything changed
  3386. if (x !== text.x || y !== text.y) {
  3387. text.attr({
  3388. x: x,
  3389. y: y
  3390. });
  3391. }
  3392. // record current values
  3393. text.x = x;
  3394. text.y = y;
  3395. }
  3396. /**
  3397. * Set a box attribute, or defer it if the box is not yet created
  3398. * @param {Object} key
  3399. * @param {Object} value
  3400. */
  3401. function boxAttr(key, value) {
  3402. if (box) {
  3403. box.attr(key, value);
  3404. } else {
  3405. deferredAttr[key] = value;
  3406. }
  3407. }
  3408. /**
  3409. * After the text element is added, get the desired size of the border box
  3410. * and add it before the text in the DOM.
  3411. */
  3412. wrapper.onAdd = function () {
  3413. text.add(wrapper);
  3414. wrapper.attr({
  3415. text: str, // alignment is available now
  3416. x: x,
  3417. y: y
  3418. });
  3419. if (box && defined(anchorX)) {
  3420. wrapper.attr({
  3421. anchorX: anchorX,
  3422. anchorY: anchorY
  3423. });
  3424. }
  3425. };
  3426. /*
  3427. * Add specific attribute setters.
  3428. */
  3429. // only change local variables
  3430. attrSetters.width = function (value) {
  3431. width = value;
  3432. return false;
  3433. };
  3434. attrSetters.height = function (value) {
  3435. height = value;
  3436. return false;
  3437. };
  3438. attrSetters.padding = function (value) {
  3439. if (defined(value) && value !== padding) {
  3440. padding = value;
  3441. updateTextPadding();
  3442. }
  3443. return false;
  3444. };
  3445. attrSetters.paddingLeft = function (value) {
  3446. if (defined(value) && value !== paddingLeft) {
  3447. paddingLeft = value;
  3448. updateTextPadding();
  3449. }
  3450. return false;
  3451. };
  3452. // change local variable and set attribue as well
  3453. attrSetters.align = function (value) {
  3454. alignFactor = { left: 0, center: 0.5, right: 1 }[value];
  3455. return false; // prevent setting text-anchor on the group
  3456. };
  3457. // apply these to the box and the text alike
  3458. attrSetters.text = function (value, key) {
  3459. text.attr(key, value);
  3460. updateBoxSize();
  3461. updateTextPadding();
  3462. return false;
  3463. };
  3464. // apply these to the box but not to the text
  3465. attrSetters[STROKE_WIDTH] = function (value, key) {
  3466. if (value) {
  3467. needsBox = true;
  3468. }
  3469. crispAdjust = value % 2 / 2;
  3470. boxAttr(key, value);
  3471. return false;
  3472. };
  3473. attrSetters.stroke = attrSetters.fill = attrSetters.r = function (value, key) {
  3474. if (key === 'fill' && value) {
  3475. needsBox = true;
  3476. }
  3477. boxAttr(key, value);
  3478. return false;
  3479. };
  3480. attrSetters.anchorX = function (value, key) {
  3481. anchorX = value;
  3482. boxAttr(key, value + crispAdjust - wrapperX);
  3483. return false;
  3484. };
  3485. attrSetters.anchorY = function (value, key) {
  3486. anchorY = value;
  3487. boxAttr(key, value - wrapperY);
  3488. return false;
  3489. };
  3490. // rename attributes
  3491. attrSetters.x = function (value) {
  3492. wrapper.x = value; // for animation getter
  3493. value -= alignFactor * ((width || bBox.width) + padding);
  3494. wrapperX = mathRound(value);
  3495. wrapper.attr('translateX', wrapperX);
  3496. return false;
  3497. };
  3498. attrSetters.y = function (value) {
  3499. wrapperY = wrapper.y = mathRound(value);
  3500. wrapper.attr('translateY', wrapperY);
  3501. return false;
  3502. };
  3503. // Redirect certain methods to either the box or the text
  3504. var baseCss = wrapper.css;
  3505. return extend(wrapper, {
  3506. /**
  3507. * Pick up some properties and apply them to the text instead of the wrapper
  3508. */
  3509. css: function (styles) {
  3510. if (styles) {
  3511. var textStyles = {};
  3512. styles = merge(styles); // create a copy to avoid altering the original object (#537)
  3513. each(['fontSize', 'fontWeight', 'fontFamily', 'color', 'lineHeight', 'width', 'textDecoration', 'textShadow'], function (prop) {
  3514. if (styles[prop] !== UNDEFINED) {
  3515. textStyles[prop] = styles[prop];
  3516. delete styles[prop];
  3517. }
  3518. });
  3519. text.css(textStyles);
  3520. }
  3521. return baseCss.call(wrapper, styles);
  3522. },
  3523. /**
  3524. * Return the bounding box of the box, not the group
  3525. */
  3526. getBBox: function () {
  3527. return {
  3528. width: bBox.width + 2 * padding,
  3529. height: bBox.height + 2 * padding,
  3530. x: bBox.x - padding,
  3531. y: bBox.y - padding
  3532. };
  3533. },
  3534. /**
  3535. * Apply the shadow to the box
  3536. */
  3537. shadow: function (b) {
  3538. if (box) {
  3539. box.shadow(b);
  3540. }
  3541. return wrapper;
  3542. },
  3543. /**
  3544. * Destroy and release memory.
  3545. */
  3546. destroy: function () {
  3547. // Added by button implementation
  3548. removeEvent(wrapper.element, 'mouseenter');
  3549. removeEvent(wrapper.element, 'mouseleave');
  3550. if (text) {
  3551. text = text.destroy();
  3552. }
  3553. if (box) {
  3554. box = box.destroy();
  3555. }
  3556. // Call base implementation to destroy the rest
  3557. SVGElement.prototype.destroy.call(wrapper);
  3558. // Release local pointers (#1298)
  3559. wrapper = renderer = updateBoxSize = updateTextPadding = boxAttr = null;
  3560. }
  3561. });
  3562. }
  3563. }; // end SVGRenderer
  3564. // general renderer
  3565. Renderer = SVGRenderer;
  3566. // extend SvgElement for useHTML option
  3567. extend(SVGElement.prototype, {
  3568. /**
  3569. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  3570. * by the VML renderer
  3571. */
  3572. htmlCss: function (styles) {
  3573. var wrapper = this,
  3574. element = wrapper.element,
  3575. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  3576. if (textWidth) {
  3577. delete styles.width;
  3578. wrapper.textWidth = textWidth;
  3579. wrapper.updateTransform();
  3580. }
  3581. wrapper.styles = extend(wrapper.styles, styles);
  3582. css(wrapper.element, styles);
  3583. return wrapper;
  3584. },
  3585. /**
  3586. * VML and useHTML method for calculating the bounding box based on offsets
  3587. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  3588. * use the cached value
  3589. *
  3590. * @return {Object} A hash containing values for x, y, width and height
  3591. */
  3592. htmlGetBBox: function () {
  3593. var wrapper = this,
  3594. element = wrapper.element,
  3595. bBox = wrapper.bBox;
  3596. // faking getBBox in exported SVG in legacy IE
  3597. if (!bBox) {
  3598. // faking getBBox in exported SVG in legacy IE (is this a duplicate of the fix for #1079?)
  3599. if (element.nodeName === 'text') {
  3600. element.style.position = ABSOLUTE;
  3601. }
  3602. bBox = wrapper.bBox = {
  3603. x: element.offsetLeft,
  3604. y: element.offsetTop,
  3605. width: element.offsetWidth,
  3606. height: element.offsetHeight
  3607. };
  3608. }
  3609. return bBox;
  3610. },
  3611. /**
  3612. * VML override private method to update elements based on internal
  3613. * properties based on SVG transform
  3614. */
  3615. htmlUpdateTransform: function () {
  3616. // aligning non added elements is expensive
  3617. if (!this.added) {
  3618. this.alignOnAdd = true;
  3619. return;
  3620. }
  3621. var wrapper = this,
  3622. renderer = wrapper.renderer,
  3623. elem = wrapper.element,
  3624. translateX = wrapper.translateX || 0,
  3625. translateY = wrapper.translateY || 0,
  3626. x = wrapper.x || 0,
  3627. y = wrapper.y || 0,
  3628. align = wrapper.textAlign || 'left',
  3629. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  3630. shadows = wrapper.shadows;
  3631. // apply translate
  3632. css(elem, {
  3633. marginLeft: translateX,
  3634. marginTop: translateY
  3635. });
  3636. if (shadows) { // used in labels/tooltip
  3637. each(shadows, function (shadow) {
  3638. css(shadow, {
  3639. marginLeft: translateX + 1,
  3640. marginTop: translateY + 1
  3641. });
  3642. });
  3643. }
  3644. // apply inversion
  3645. if (wrapper.inverted) { // wrapper is a group
  3646. each(elem.childNodes, function (child) {
  3647. renderer.invertChild(child, elem);
  3648. });
  3649. }
  3650. if (elem.tagName === 'SPAN') {
  3651. var width,
  3652. rotation = wrapper.rotation,
  3653. baseline,
  3654. textWidth = pInt(wrapper.textWidth),
  3655. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth].join(',');
  3656. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  3657. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  3658. // Renderer specific handling of span rotation
  3659. if (defined(rotation)) {
  3660. wrapper.setSpanRotation(rotation, alignCorrection, baseline);
  3661. }
  3662. width = pick(wrapper.elemWidth, elem.offsetWidth);
  3663. // Update textWidth
  3664. if (width > textWidth && /[ \-]/.test(elem.textContent || elem.innerText)) { // #983, #1254
  3665. css(elem, {
  3666. width: textWidth + PX,
  3667. display: 'block',
  3668. whiteSpace: 'normal'
  3669. });
  3670. width = textWidth;
  3671. }
  3672. wrapper.getSpanCorrection(width, baseline, alignCorrection, rotation, align);
  3673. }
  3674. // apply position with correction
  3675. css(elem, {
  3676. left: (x + (wrapper.xCorr || 0)) + PX,
  3677. top: (y + (wrapper.yCorr || 0)) + PX
  3678. });
  3679. // force reflow in webkit to apply the left and top on useHTML element (#1249)
  3680. if (isWebKit) {
  3681. baseline = elem.offsetHeight; // assigned to baseline for JSLint purpose
  3682. }
  3683. // record current text transform
  3684. wrapper.cTT = currentTextTransform;
  3685. }
  3686. },
  3687. /**
  3688. * Set the rotation of an individual HTML span
  3689. */
  3690. setSpanRotation: function (rotation, alignCorrection, baseline) {
  3691. var rotationStyle = {},
  3692. cssTransformKey = isIE ? '-ms-transform' : isWebKit ? '-webkit-transform' : isFirefox ? 'MozTransform' : isOpera ? '-o-transform' : '';
  3693. rotationStyle[cssTransformKey] = rotationStyle.transform = 'rotate(' + rotation + 'deg)';
  3694. rotationStyle[cssTransformKey + (isFirefox ? 'Origin' : '-origin')] = rotationStyle.transformOrigin = (alignCorrection * 100) + '% ' + baseline + 'px';
  3695. css(this.element, rotationStyle);
  3696. },
  3697. /**
  3698. * Get the correction in X and Y positioning as the element is rotated.
  3699. */
  3700. getSpanCorrection: function (width, baseline, alignCorrection) {
  3701. this.xCorr = -width * alignCorrection;
  3702. this.yCorr = -baseline;
  3703. }
  3704. });
  3705. // Extend SvgRenderer for useHTML option.
  3706. extend(SVGRenderer.prototype, {
  3707. /**
  3708. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3709. * renderer through the useHTML option.
  3710. *
  3711. * @param {String} str
  3712. * @param {Number} x
  3713. * @param {Number} y
  3714. */
  3715. html: function (str, x, y) {
  3716. var wrapper = this.createElement('span'),
  3717. attrSetters = wrapper.attrSetters,
  3718. element = wrapper.element,
  3719. renderer = wrapper.renderer;
  3720. // Text setter
  3721. attrSetters.text = function (value) {
  3722. if (value !== element.innerHTML) {
  3723. delete this.bBox;
  3724. }
  3725. element.innerHTML = this.textStr = value;
  3726. return false;
  3727. };
  3728. // Various setters which rely on update transform
  3729. attrSetters.x = attrSetters.y = attrSetters.align = attrSetters.rotation = function (value, key) {
  3730. if (key === 'align') {
  3731. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  3732. }
  3733. wrapper[key] = value;
  3734. wrapper.htmlUpdateTransform();
  3735. return false;
  3736. };
  3737. // Set the default attributes
  3738. wrapper.attr({
  3739. text: str,
  3740. x: mathRound(x),
  3741. y: mathRound(y)
  3742. })
  3743. .css({
  3744. position: ABSOLUTE,
  3745. whiteSpace: 'nowrap',
  3746. fontFamily: this.style.fontFamily,
  3747. fontSize: this.style.fontSize
  3748. });
  3749. // Use the HTML specific .css method
  3750. wrapper.css = wrapper.htmlCss;
  3751. // This is specific for HTML within SVG
  3752. if (renderer.isSVG) {
  3753. wrapper.add = function (svgGroupWrapper) {
  3754. var htmlGroup,
  3755. container = renderer.box.parentNode,
  3756. parentGroup,
  3757. parents = [];
  3758. this.parentGroup = svgGroupWrapper;
  3759. // Create a mock group to hold the HTML elements
  3760. if (svgGroupWrapper) {
  3761. htmlGroup = svgGroupWrapper.div;
  3762. if (!htmlGroup) {
  3763. // Read the parent chain into an array and read from top down
  3764. parentGroup = svgGroupWrapper;
  3765. while (parentGroup) {
  3766. parents.push(parentGroup);
  3767. // Move up to the next parent group
  3768. parentGroup = parentGroup.parentGroup;
  3769. }
  3770. // Ensure dynamically updating position when any parent is translated
  3771. each(parents.reverse(), function (parentGroup) {
  3772. var htmlGroupStyle;
  3773. // Create a HTML div and append it to the parent div to emulate
  3774. // the SVG group structure
  3775. htmlGroup = parentGroup.div = parentGroup.div || createElement(DIV, {
  3776. className: attr(parentGroup.element, 'class')
  3777. }, {
  3778. position: ABSOLUTE,
  3779. left: (parentGroup.translateX || 0) + PX,
  3780. top: (parentGroup.translateY || 0) + PX
  3781. }, htmlGroup || container); // the top group is appended to container
  3782. // Shortcut
  3783. htmlGroupStyle = htmlGroup.style;
  3784. // Set listeners to update the HTML div's position whenever the SVG group
  3785. // position is changed
  3786. extend(parentGroup.attrSetters, {
  3787. translateX: function (value) {
  3788. htmlGroupStyle.left = value + PX;
  3789. },
  3790. translateY: function (value) {
  3791. htmlGroupStyle.top = value + PX;
  3792. },
  3793. visibility: function (value, key) {
  3794. htmlGroupStyle[key] = value;
  3795. }
  3796. });
  3797. });
  3798. }
  3799. } else {
  3800. htmlGroup = container;
  3801. }
  3802. htmlGroup.appendChild(element);
  3803. // Shared with VML:
  3804. wrapper.added = true;
  3805. if (wrapper.alignOnAdd) {
  3806. wrapper.htmlUpdateTransform();
  3807. }
  3808. return wrapper;
  3809. };
  3810. }
  3811. return wrapper;
  3812. }
  3813. });
  3814. /* ****************************************************************************
  3815. * *
  3816. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  3817. * *
  3818. * For applications and websites that don't need IE support, like platform *
  3819. * targeted mobile apps and web apps, this code can be removed. *
  3820. * *
  3821. *****************************************************************************/
  3822. /**
  3823. * @constructor
  3824. */
  3825. var VMLRenderer, VMLElement;
  3826. if (!hasSVG && !useCanVG) {
  3827. /**
  3828. * The VML element wrapper.
  3829. */
  3830. Highcharts.VMLElement = VMLElement = {
  3831. /**
  3832. * Initialize a new VML element wrapper. It builds the markup as a string
  3833. * to minimize DOM traffic.
  3834. * @param {Object} renderer
  3835. * @param {Object} nodeName
  3836. */
  3837. init: function (renderer, nodeName) {
  3838. var wrapper = this,
  3839. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  3840. style = ['position: ', ABSOLUTE, ';'],
  3841. isDiv = nodeName === DIV;
  3842. // divs and shapes need size
  3843. if (nodeName === 'shape' || isDiv) {
  3844. style.push('left:0;top:0;width:1px;height:1px;');
  3845. }
  3846. style.push('visibility: ', isDiv ? HIDDEN : VISIBLE);
  3847. markup.push(' style="', style.join(''), '"/>');
  3848. // create element with default attributes and style
  3849. if (nodeName) {
  3850. markup = isDiv || nodeName === 'span' || nodeName === 'img' ?
  3851. markup.join('')
  3852. : renderer.prepVML(markup);
  3853. wrapper.element = createElement(markup);
  3854. }
  3855. wrapper.renderer = renderer;
  3856. wrapper.attrSetters = {};
  3857. },
  3858. /**
  3859. * Add the node to the given parent
  3860. * @param {Object} parent
  3861. */
  3862. add: function (parent) {
  3863. var wrapper = this,
  3864. renderer = wrapper.renderer,
  3865. element = wrapper.element,
  3866. box = renderer.box,
  3867. inverted = parent && parent.inverted,
  3868. // get the parent node
  3869. parentNode = parent ?
  3870. parent.element || parent :
  3871. box;
  3872. // if the parent group is inverted, apply inversion on all children
  3873. if (inverted) { // only on groups
  3874. renderer.invertChild(element, parentNode);
  3875. }
  3876. // append it
  3877. parentNode.appendChild(element);
  3878. // align text after adding to be able to read offset
  3879. wrapper.added = true;
  3880. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  3881. wrapper.updateTransform();
  3882. }
  3883. // fire an event for internal hooks
  3884. if (wrapper.onAdd) {
  3885. wrapper.onAdd();
  3886. }
  3887. return wrapper;
  3888. },
  3889. /**
  3890. * VML always uses htmlUpdateTransform
  3891. */
  3892. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  3893. /**
  3894. * Set the rotation of a span with oldIE's filter
  3895. */
  3896. setSpanRotation: function () {
  3897. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  3898. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  3899. // has support for CSS3 transform. The getBBox method also needs to be updated
  3900. // to compensate for the rotation, like it currently does for SVG.
  3901. // Test case: http://jsfiddle.net/highcharts/Ybt44/
  3902. var rotation = this.rotation,
  3903. costheta = mathCos(rotation * deg2rad),
  3904. sintheta = mathSin(rotation * deg2rad);
  3905. css(this.element, {
  3906. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  3907. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  3908. ', sizingMethod=\'auto expand\')'].join('') : NONE
  3909. });
  3910. },
  3911. /**
  3912. * Get the positioning correction for the span after rotating.
  3913. */
  3914. getSpanCorrection: function (width, baseline, alignCorrection, rotation, align) {
  3915. var costheta = rotation ? mathCos(rotation * deg2rad) : 1,
  3916. sintheta = rotation ? mathSin(rotation * deg2rad) : 0,
  3917. height = pick(this.elemHeight, this.element.offsetHeight),
  3918. quad,
  3919. nonLeft = align && align !== 'left';
  3920. // correct x and y
  3921. this.xCorr = costheta < 0 && -width;
  3922. this.yCorr = sintheta < 0 && -height;
  3923. // correct for baseline and corners spilling out after rotation
  3924. quad = costheta * sintheta < 0;
  3925. this.xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  3926. this.yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  3927. // correct for the length/height of the text
  3928. if (nonLeft) {
  3929. this.xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  3930. if (rotation) {
  3931. this.yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  3932. }
  3933. css(this.element, {
  3934. textAlign: align
  3935. });
  3936. }
  3937. },
  3938. /**
  3939. * Converts a subset of an SVG path definition to its VML counterpart. Takes an array
  3940. * as the parameter and returns a string.
  3941. */
  3942. pathToVML: function (value) {
  3943. // convert paths
  3944. var i = value.length,
  3945. path = [];
  3946. while (i--) {
  3947. // Multiply by 10 to allow subpixel precision.
  3948. // Substracting half a pixel seems to make the coordinates
  3949. // align with SVG, but this hasn't been tested thoroughly
  3950. if (isNumber(value[i])) {
  3951. path[i] = mathRound(value[i] * 10) - 5;
  3952. } else if (value[i] === 'Z') { // close the path
  3953. path[i] = 'x';
  3954. } else {
  3955. path[i] = value[i];
  3956. // When the start X and end X coordinates of an arc are too close,
  3957. // they are rounded to the same value above. In this case, substract or
  3958. // add 1 from the end X and Y positions. #186, #760, #1371, #1410.
  3959. if (value.isArc && (value[i] === 'wa' || value[i] === 'at')) {
  3960. // Start and end X
  3961. if (path[i + 5] === path[i + 7]) {
  3962. path[i + 7] += value[i + 7] > value[i + 5] ? 1 : -1;
  3963. }
  3964. // Start and end Y
  3965. if (path[i + 6] === path[i + 8]) {
  3966. path[i + 8] += value[i + 8] > value[i + 6] ? 1 : -1;
  3967. }
  3968. }
  3969. }
  3970. }
  3971. // Loop up again to handle path shortcuts (#2132)
  3972. /*while (i++ < path.length) {
  3973. if (path[i] === 'H') { // horizontal line to
  3974. path[i] = 'L';
  3975. path.splice(i + 2, 0, path[i - 1]);
  3976. } else if (path[i] === 'V') { // vertical line to
  3977. path[i] = 'L';
  3978. path.splice(i + 1, 0, path[i - 2]);
  3979. }
  3980. }*/
  3981. return path.join(' ') || 'x';
  3982. },
  3983. /**
  3984. * Get or set attributes
  3985. */
  3986. attr: function (hash, val) {
  3987. var wrapper = this,
  3988. key,
  3989. value,
  3990. i,
  3991. result,
  3992. element = wrapper.element || {},
  3993. elemStyle = element.style,
  3994. nodeName = element.nodeName,
  3995. renderer = wrapper.renderer,
  3996. symbolName = wrapper.symbolName,
  3997. hasSetSymbolSize,
  3998. shadows = wrapper.shadows,
  3999. skipAttr,
  4000. attrSetters = wrapper.attrSetters,
  4001. ret = wrapper;
  4002. // single key-value pair
  4003. if (isString(hash) && defined(val)) {
  4004. key = hash;
  4005. hash = {};
  4006. hash[key] = val;
  4007. }
  4008. // used as a getter, val is undefined
  4009. if (isString(hash)) {
  4010. key = hash;
  4011. if (key === 'strokeWidth' || key === 'stroke-width') {
  4012. ret = wrapper.strokeweight;
  4013. } else {
  4014. ret = wrapper[key];
  4015. }
  4016. // setter
  4017. } else {
  4018. for (key in hash) {
  4019. value = hash[key];
  4020. skipAttr = false;
  4021. // check for a specific attribute setter
  4022. result = attrSetters[key] && attrSetters[key].call(wrapper, value, key);
  4023. if (result !== false && value !== null) { // #620
  4024. if (result !== UNDEFINED) {
  4025. value = result; // the attribute setter has returned a new value to set
  4026. }
  4027. // prepare paths
  4028. // symbols
  4029. if (symbolName && /^(x|y|r|start|end|width|height|innerR|anchorX|anchorY)/.test(key)) {
  4030. // if one of the symbol size affecting parameters are changed,
  4031. // check all the others only once for each call to an element's
  4032. // .attr() method
  4033. if (!hasSetSymbolSize) {
  4034. wrapper.symbolAttr(hash);
  4035. hasSetSymbolSize = true;
  4036. }
  4037. skipAttr = true;
  4038. } else if (key === 'd') {
  4039. value = value || [];
  4040. wrapper.d = value.join(' '); // used in getter for animation
  4041. element.path = value = wrapper.pathToVML(value);
  4042. // update shadows
  4043. if (shadows) {
  4044. i = shadows.length;
  4045. while (i--) {
  4046. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4047. }
  4048. }
  4049. skipAttr = true;
  4050. // handle visibility
  4051. } else if (key === 'visibility') {
  4052. // Handle inherited visibility
  4053. if (value === 'inherit') {
  4054. value = VISIBLE;
  4055. }
  4056. // Let the shadow follow the main element
  4057. if (shadows) {
  4058. i = shadows.length;
  4059. while (i--) {
  4060. shadows[i].style[key] = value;
  4061. }
  4062. }
  4063. // Instead of toggling the visibility CSS property, move the div out of the viewport.
  4064. // This works around #61 and #586
  4065. if (nodeName === 'DIV') {
  4066. value = value === HIDDEN ? '-999em' : 0;
  4067. // In order to redraw, IE7 needs the div to be visible when tucked away
  4068. // outside the viewport. So the visibility is actually opposite of
  4069. // the expected value. This applies to the tooltip only.
  4070. if (!docMode8) {
  4071. elemStyle[key] = value ? VISIBLE : HIDDEN;
  4072. }
  4073. key = 'top';
  4074. }
  4075. elemStyle[key] = value;
  4076. skipAttr = true;
  4077. // directly mapped to css
  4078. } else if (key === 'zIndex') {
  4079. if (value) {
  4080. elemStyle[key] = value;
  4081. }
  4082. skipAttr = true;
  4083. // x, y, width, height
  4084. } else if (inArray(key, ['x', 'y', 'width', 'height']) !== -1) {
  4085. wrapper[key] = value; // used in getter
  4086. if (key === 'x' || key === 'y') {
  4087. key = { x: 'left', y: 'top' }[key];
  4088. } else {
  4089. value = mathMax(0, value); // don't set width or height below zero (#311)
  4090. }
  4091. // clipping rectangle special
  4092. if (wrapper.updateClipping) {
  4093. wrapper[key] = value; // the key is now 'left' or 'top' for 'x' and 'y'
  4094. wrapper.updateClipping();
  4095. } else {
  4096. // normal
  4097. elemStyle[key] = value;
  4098. }
  4099. skipAttr = true;
  4100. // class name
  4101. } else if (key === 'class' && nodeName === 'DIV') {
  4102. // IE8 Standards mode has problems retrieving the className
  4103. element.className = value;
  4104. // stroke
  4105. } else if (key === 'stroke') {
  4106. value = renderer.color(value, element, key);
  4107. key = 'strokecolor';
  4108. // stroke width
  4109. } else if (key === 'stroke-width' || key === 'strokeWidth') {
  4110. element.stroked = value ? true : false;
  4111. key = 'strokeweight';
  4112. wrapper[key] = value; // used in getter, issue #113
  4113. if (isNumber(value)) {
  4114. value += PX;
  4115. }
  4116. // dashStyle
  4117. } else if (key === 'dashstyle') {
  4118. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4119. createElement(renderer.prepVML(['<stroke/>']), null, null, element);
  4120. strokeElem[key] = value || 'solid';
  4121. wrapper.dashstyle = value; /* because changing stroke-width will change the dash length
  4122. and cause an epileptic effect */
  4123. skipAttr = true;
  4124. // fill
  4125. } else if (key === 'fill') {
  4126. if (nodeName === 'SPAN') { // text color
  4127. elemStyle.color = value;
  4128. } else if (nodeName !== 'IMG') { // #1336
  4129. element.filled = value !== NONE ? true : false;
  4130. value = renderer.color(value, element, key, wrapper);
  4131. key = 'fillcolor';
  4132. }
  4133. // opacity: don't bother - animation is too slow and filters introduce artifacts
  4134. } else if (key === 'opacity') {
  4135. /*css(element, {
  4136. opacity: value
  4137. });*/
  4138. skipAttr = true;
  4139. // rotation on VML elements
  4140. } else if (nodeName === 'shape' && key === 'rotation') {
  4141. wrapper[key] = element.style[key] = value; // style is for #1873
  4142. // Correction for the 1x1 size of the shape container. Used in gauge needles.
  4143. element.style.left = -mathRound(mathSin(value * deg2rad) + 1) + PX;
  4144. element.style.top = mathRound(mathCos(value * deg2rad)) + PX;
  4145. // translation for animation
  4146. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation') {
  4147. wrapper[key] = value;
  4148. wrapper.updateTransform();
  4149. skipAttr = true;
  4150. }
  4151. if (!skipAttr) {
  4152. if (docMode8) { // IE8 setAttribute bug
  4153. element[key] = value;
  4154. } else {
  4155. attr(element, key, value);
  4156. }
  4157. }
  4158. }
  4159. }
  4160. }
  4161. return ret;
  4162. },
  4163. /**
  4164. * Set the element's clipping to a predefined rectangle
  4165. *
  4166. * @param {String} id The id of the clip rectangle
  4167. */
  4168. clip: function (clipRect) {
  4169. var wrapper = this,
  4170. clipMembers,
  4171. cssRet;
  4172. if (clipRect) {
  4173. clipMembers = clipRect.members;
  4174. erase(clipMembers, wrapper); // Ensure unique list of elements (#1258)
  4175. clipMembers.push(wrapper);
  4176. wrapper.destroyClip = function () {
  4177. erase(clipMembers, wrapper);
  4178. };
  4179. cssRet = clipRect.getCSS(wrapper);
  4180. } else {
  4181. if (wrapper.destroyClip) {
  4182. wrapper.destroyClip();
  4183. }
  4184. cssRet = { clip: docMode8 ? 'inherit' : 'rect(auto)' }; // #1214
  4185. }
  4186. return wrapper.css(cssRet);
  4187. },
  4188. /**
  4189. * Set styles for the element
  4190. * @param {Object} styles
  4191. */
  4192. css: SVGElement.prototype.htmlCss,
  4193. /**
  4194. * Removes a child either by removeChild or move to garbageBin.
  4195. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4196. */
  4197. safeRemoveChild: function (element) {
  4198. // discardElement will detach the node from its parent before attaching it
  4199. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4200. if (element.parentNode) {
  4201. discardElement(element);
  4202. }
  4203. },
  4204. /**
  4205. * Extend element.destroy by removing it from the clip members array
  4206. */
  4207. destroy: function () {
  4208. if (this.destroyClip) {
  4209. this.destroyClip();
  4210. }
  4211. return SVGElement.prototype.destroy.apply(this);
  4212. },
  4213. /**
  4214. * Add an event listener. VML override for normalizing event parameters.
  4215. * @param {String} eventType
  4216. * @param {Function} handler
  4217. */
  4218. on: function (eventType, handler) {
  4219. // simplest possible event model for internal use
  4220. this.element['on' + eventType] = function () {
  4221. var evt = win.event;
  4222. evt.target = evt.srcElement;
  4223. handler(evt);
  4224. };
  4225. return this;
  4226. },
  4227. /**
  4228. * In stacked columns, cut off the shadows so that they don't overlap
  4229. */
  4230. cutOffPath: function (path, length) {
  4231. var len;
  4232. path = path.split(/[ ,]/);
  4233. len = path.length;
  4234. if (len === 9 || len === 11) {
  4235. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4236. }
  4237. return path.join(' ');
  4238. },
  4239. /**
  4240. * Apply a drop shadow by copying elements and giving them different strokes
  4241. * @param {Boolean|Object} shadowOptions
  4242. */
  4243. shadow: function (shadowOptions, group, cutOff) {
  4244. var shadows = [],
  4245. i,
  4246. element = this.element,
  4247. renderer = this.renderer,
  4248. shadow,
  4249. elemStyle = element.style,
  4250. markup,
  4251. path = element.path,
  4252. strokeWidth,
  4253. modifiedPath,
  4254. shadowWidth,
  4255. shadowElementOpacity;
  4256. // some times empty paths are not strings
  4257. if (path && typeof path.value !== 'string') {
  4258. path = 'x';
  4259. }
  4260. modifiedPath = path;
  4261. if (shadowOptions) {
  4262. shadowWidth = pick(shadowOptions.width, 3);
  4263. shadowElementOpacity = (shadowOptions.opacity || 0.15) / shadowWidth;
  4264. for (i = 1; i <= 3; i++) {
  4265. strokeWidth = (shadowWidth * 2) + 1 - (2 * i);
  4266. // Cut off shadows for stacked column items
  4267. if (cutOff) {
  4268. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4269. }
  4270. markup = ['<shape isShadow="true" strokeweight="', strokeWidth,
  4271. '" filled="false" path="', modifiedPath,
  4272. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4273. shadow = createElement(renderer.prepVML(markup),
  4274. null, {
  4275. left: pInt(elemStyle.left) + pick(shadowOptions.offsetX, 1),
  4276. top: pInt(elemStyle.top) + pick(shadowOptions.offsetY, 1)
  4277. }
  4278. );
  4279. if (cutOff) {
  4280. shadow.cutOff = strokeWidth + 1;
  4281. }
  4282. // apply the opacity
  4283. markup = ['<stroke color="', shadowOptions.color || 'black', '" opacity="', shadowElementOpacity * i, '"/>'];
  4284. createElement(renderer.prepVML(markup), null, null, shadow);
  4285. // insert it
  4286. if (group) {
  4287. group.element.appendChild(shadow);
  4288. } else {
  4289. element.parentNode.insertBefore(shadow, element);
  4290. }
  4291. // record it
  4292. shadows.push(shadow);
  4293. }
  4294. this.shadows = shadows;
  4295. }
  4296. return this;
  4297. }
  4298. };
  4299. VMLElement = extendClass(SVGElement, VMLElement);
  4300. /**
  4301. * The VML renderer
  4302. */
  4303. var VMLRendererExtension = { // inherit SVGRenderer
  4304. Element: VMLElement,
  4305. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4306. /**
  4307. * Initialize the VMLRenderer
  4308. * @param {Object} container
  4309. * @param {Number} width
  4310. * @param {Number} height
  4311. */
  4312. init: function (container, width, height, style) {
  4313. var renderer = this,
  4314. boxWrapper,
  4315. box,
  4316. css;
  4317. renderer.alignedObjects = [];
  4318. boxWrapper = renderer.createElement(DIV)
  4319. .css(extend(this.getStyle(style), { position: RELATIVE}));
  4320. box = boxWrapper.element;
  4321. container.appendChild(boxWrapper.element);
  4322. // generate the containing box
  4323. renderer.isVML = true;
  4324. renderer.box = box;
  4325. renderer.boxWrapper = boxWrapper;
  4326. renderer.cache = {};
  4327. renderer.setSize(width, height, false);
  4328. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4329. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4330. // seems to be to add the xmlns attribute and the behaviour style inline.
  4331. if (!doc.namespaces.hcv) {
  4332. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4333. // Setup default CSS (#2153, #2368, #2384)
  4334. css = 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4335. '{ behavior:url(#default#VML); display: inline-block; } ';
  4336. try {
  4337. doc.createStyleSheet().cssText = css;
  4338. } catch (e) {
  4339. doc.styleSheets[0].cssText += css;
  4340. }
  4341. }
  4342. },
  4343. /**
  4344. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4345. * has display: none
  4346. */
  4347. isHidden: function () {
  4348. return !this.box.offsetWidth;
  4349. },
  4350. /**
  4351. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4352. * for setting the CSS style to all associated members.
  4353. *
  4354. * @param {Number} x
  4355. * @param {Number} y
  4356. * @param {Number} width
  4357. * @param {Number} height
  4358. */
  4359. clipRect: function (x, y, width, height) {
  4360. // create a dummy element
  4361. var clipRect = this.createElement(),
  4362. isObj = isObject(x);
  4363. // mimic a rectangle with its style object for automatic updating in attr
  4364. return extend(clipRect, {
  4365. members: [],
  4366. left: (isObj ? x.x : x) + 1,
  4367. top: (isObj ? x.y : y) + 1,
  4368. width: (isObj ? x.width : width) - 1,
  4369. height: (isObj ? x.height : height) - 1,
  4370. getCSS: function (wrapper) {
  4371. var element = wrapper.element,
  4372. nodeName = element.nodeName,
  4373. isShape = nodeName === 'shape',
  4374. inverted = wrapper.inverted,
  4375. rect = this,
  4376. top = rect.top - (isShape ? element.offsetTop : 0),
  4377. left = rect.left,
  4378. right = left + rect.width,
  4379. bottom = top + rect.height,
  4380. ret = {
  4381. clip: 'rect(' +
  4382. mathRound(inverted ? left : top) + 'px,' +
  4383. mathRound(inverted ? bottom : right) + 'px,' +
  4384. mathRound(inverted ? right : bottom) + 'px,' +
  4385. mathRound(inverted ? top : left) + 'px)'
  4386. };
  4387. // issue 74 workaround
  4388. if (!inverted && docMode8 && nodeName === 'DIV') {
  4389. extend(ret, {
  4390. width: right + PX,
  4391. height: bottom + PX
  4392. });
  4393. }
  4394. return ret;
  4395. },
  4396. // used in attr and animation to update the clipping of all members
  4397. updateClipping: function () {
  4398. each(clipRect.members, function (member) {
  4399. member.css(clipRect.getCSS(member));
  4400. });
  4401. }
  4402. });
  4403. },
  4404. /**
  4405. * Take a color and return it if it's a string, make it a gradient if it's a
  4406. * gradient configuration object, and apply opacity.
  4407. *
  4408. * @param {Object} color The color or config object
  4409. */
  4410. color: function (color, elem, prop, wrapper) {
  4411. var renderer = this,
  4412. colorObject,
  4413. regexRgba = /^rgba/,
  4414. markup,
  4415. fillType,
  4416. ret = NONE;
  4417. // Check for linear or radial gradient
  4418. if (color && color.linearGradient) {
  4419. fillType = 'gradient';
  4420. } else if (color && color.radialGradient) {
  4421. fillType = 'pattern';
  4422. }
  4423. if (fillType) {
  4424. var stopColor,
  4425. stopOpacity,
  4426. gradient = color.linearGradient || color.radialGradient,
  4427. x1,
  4428. y1,
  4429. x2,
  4430. y2,
  4431. opacity1,
  4432. opacity2,
  4433. color1,
  4434. color2,
  4435. fillAttr = '',
  4436. stops = color.stops,
  4437. firstStop,
  4438. lastStop,
  4439. colors = [],
  4440. addFillNode = function () {
  4441. // Add the fill subnode. When colors attribute is used, the meanings of opacity and o:opacity2
  4442. // are reversed.
  4443. markup = ['<fill colors="' + colors.join(',') + '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4444. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  4445. createElement(renderer.prepVML(markup), null, null, elem);
  4446. };
  4447. // Extend from 0 to 1
  4448. firstStop = stops[0];
  4449. lastStop = stops[stops.length - 1];
  4450. if (firstStop[0] > 0) {
  4451. stops.unshift([
  4452. 0,
  4453. firstStop[1]
  4454. ]);
  4455. }
  4456. if (lastStop[0] < 1) {
  4457. stops.push([
  4458. 1,
  4459. lastStop[1]
  4460. ]);
  4461. }
  4462. // Compute the stops
  4463. each(stops, function (stop, i) {
  4464. if (regexRgba.test(stop[1])) {
  4465. colorObject = Color(stop[1]);
  4466. stopColor = colorObject.get('rgb');
  4467. stopOpacity = colorObject.get('a');
  4468. } else {
  4469. stopColor = stop[1];
  4470. stopOpacity = 1;
  4471. }
  4472. // Build the color attribute
  4473. colors.push((stop[0] * 100) + '% ' + stopColor);
  4474. // Only start and end opacities are allowed, so we use the first and the last
  4475. if (!i) {
  4476. opacity1 = stopOpacity;
  4477. color2 = stopColor;
  4478. } else {
  4479. opacity2 = stopOpacity;
  4480. color1 = stopColor;
  4481. }
  4482. });
  4483. // Apply the gradient to fills only.
  4484. if (prop === 'fill') {
  4485. // Handle linear gradient angle
  4486. if (fillType === 'gradient') {
  4487. x1 = gradient.x1 || gradient[0] || 0;
  4488. y1 = gradient.y1 || gradient[1] || 0;
  4489. x2 = gradient.x2 || gradient[2] || 0;
  4490. y2 = gradient.y2 || gradient[3] || 0;
  4491. fillAttr = 'angle="' + (90 - math.atan(
  4492. (y2 - y1) / // y vector
  4493. (x2 - x1) // x vector
  4494. ) * 180 / mathPI) + '"';
  4495. addFillNode();
  4496. // Radial (circular) gradient
  4497. } else {
  4498. var r = gradient.r,
  4499. sizex = r * 2,
  4500. sizey = r * 2,
  4501. cx = gradient.cx,
  4502. cy = gradient.cy,
  4503. radialReference = elem.radialReference,
  4504. bBox,
  4505. applyRadialGradient = function () {
  4506. if (radialReference) {
  4507. bBox = wrapper.getBBox();
  4508. cx += (radialReference[0] - bBox.x) / bBox.width - 0.5;
  4509. cy += (radialReference[1] - bBox.y) / bBox.height - 0.5;
  4510. sizex *= radialReference[2] / bBox.width;
  4511. sizey *= radialReference[2] / bBox.height;
  4512. }
  4513. fillAttr = 'src="' + defaultOptions.global.VMLRadialGradientURL + '" ' +
  4514. 'size="' + sizex + ',' + sizey + '" ' +
  4515. 'origin="0.5,0.5" ' +
  4516. 'position="' + cx + ',' + cy + '" ' +
  4517. 'color2="' + color2 + '" ';
  4518. addFillNode();
  4519. };
  4520. // Apply radial gradient
  4521. if (wrapper.added) {
  4522. applyRadialGradient();
  4523. } else {
  4524. // We need to know the bounding box to get the size and position right
  4525. wrapper.onAdd = applyRadialGradient;
  4526. }
  4527. // The fill element's color attribute is broken in IE8 standards mode, so we
  4528. // need to set the parent shape's fillcolor attribute instead.
  4529. ret = color1;
  4530. }
  4531. // Gradients are not supported for VML stroke, return the first color. #722.
  4532. } else {
  4533. ret = stopColor;
  4534. }
  4535. // if the color is an rgba color, split it and add a fill node
  4536. // to hold the opacity component
  4537. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4538. colorObject = Color(color);
  4539. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4540. createElement(this.prepVML(markup), null, null, elem);
  4541. ret = colorObject.get('rgb');
  4542. } else {
  4543. var propNodes = elem.getElementsByTagName(prop); // 'stroke' or 'fill' node
  4544. if (propNodes.length) {
  4545. propNodes[0].opacity = 1;
  4546. propNodes[0].type = 'solid';
  4547. }
  4548. ret = color;
  4549. }
  4550. return ret;
  4551. },
  4552. /**
  4553. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4554. * @param {Array} markup A string array of the VML markup to prepare
  4555. */
  4556. prepVML: function (markup) {
  4557. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4558. isIE8 = this.isIE8;
  4559. markup = markup.join('');
  4560. if (isIE8) { // add xmlns and style inline
  4561. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4562. if (markup.indexOf('style="') === -1) {
  4563. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4564. } else {
  4565. markup = markup.replace('style="', 'style="' + vmlStyle);
  4566. }
  4567. } else { // add namespace
  4568. markup = markup.replace('<', '<hcv:');
  4569. }
  4570. return markup;
  4571. },
  4572. /**
  4573. * Create rotated and aligned text
  4574. * @param {String} str
  4575. * @param {Number} x
  4576. * @param {Number} y
  4577. */
  4578. text: SVGRenderer.prototype.html,
  4579. /**
  4580. * Create and return a path element
  4581. * @param {Array} path
  4582. */
  4583. path: function (path) {
  4584. var attr = {
  4585. // subpixel precision down to 0.1 (width and height = 1px)
  4586. coordsize: '10 10'
  4587. };
  4588. if (isArray(path)) {
  4589. attr.d = path;
  4590. } else if (isObject(path)) { // attributes
  4591. extend(attr, path);
  4592. }
  4593. // create the shape
  4594. return this.createElement('shape').attr(attr);
  4595. },
  4596. /**
  4597. * Create and return a circle element. In VML circles are implemented as
  4598. * shapes, which is faster than v:oval
  4599. * @param {Number} x
  4600. * @param {Number} y
  4601. * @param {Number} r
  4602. */
  4603. circle: function (x, y, r) {
  4604. var circle = this.symbol('circle');
  4605. if (isObject(x)) {
  4606. r = x.r;
  4607. y = x.y;
  4608. x = x.x;
  4609. }
  4610. circle.isCircle = true; // Causes x and y to mean center (#1682)
  4611. circle.r = r;
  4612. return circle.attr({ x: x, y: y });
  4613. },
  4614. /**
  4615. * Create a group using an outer div and an inner v:group to allow rotating
  4616. * and flipping. A simple v:group would have problems with positioning
  4617. * child HTML elements and CSS clip.
  4618. *
  4619. * @param {String} name The name of the group
  4620. */
  4621. g: function (name) {
  4622. var wrapper,
  4623. attribs;
  4624. // set the class name
  4625. if (name) {
  4626. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  4627. }
  4628. // the div to hold HTML and clipping
  4629. wrapper = this.createElement(DIV).attr(attribs);
  4630. return wrapper;
  4631. },
  4632. /**
  4633. * VML override to create a regular HTML image
  4634. * @param {String} src
  4635. * @param {Number} x
  4636. * @param {Number} y
  4637. * @param {Number} width
  4638. * @param {Number} height
  4639. */
  4640. image: function (src, x, y, width, height) {
  4641. var obj = this.createElement('img')
  4642. .attr({ src: src });
  4643. if (arguments.length > 1) {
  4644. obj.attr({
  4645. x: x,
  4646. y: y,
  4647. width: width,
  4648. height: height
  4649. });
  4650. }
  4651. return obj;
  4652. },
  4653. /**
  4654. * For rectangles, VML uses a shape for rect to overcome bugs and rotation problems
  4655. */
  4656. createElement: function (nodeName) {
  4657. return nodeName === 'rect' ? this.symbol(nodeName) : SVGRenderer.prototype.createElement.call(this, nodeName);
  4658. },
  4659. /**
  4660. * In the VML renderer, each child of an inverted div (group) is inverted
  4661. * @param {Object} element
  4662. * @param {Object} parentNode
  4663. */
  4664. invertChild: function (element, parentNode) {
  4665. var ren = this,
  4666. parentStyle = parentNode.style,
  4667. imgStyle = element.tagName === 'IMG' && element.style; // #1111
  4668. css(element, {
  4669. flip: 'x',
  4670. left: pInt(parentStyle.width) - (imgStyle ? pInt(imgStyle.top) : 1),
  4671. top: pInt(parentStyle.height) - (imgStyle ? pInt(imgStyle.left) : 1),
  4672. rotation: -90
  4673. });
  4674. // Recursively invert child elements, needed for nested composite shapes like box plots and error bars. #1680, #1806.
  4675. each(element.childNodes, function (child) {
  4676. ren.invertChild(child, element);
  4677. });
  4678. },
  4679. /**
  4680. * Symbol definitions that override the parent SVG renderer's symbols
  4681. *
  4682. */
  4683. symbols: {
  4684. // VML specific arc function
  4685. arc: function (x, y, w, h, options) {
  4686. var start = options.start,
  4687. end = options.end,
  4688. radius = options.r || w || h,
  4689. innerRadius = options.innerR,
  4690. cosStart = mathCos(start),
  4691. sinStart = mathSin(start),
  4692. cosEnd = mathCos(end),
  4693. sinEnd = mathSin(end),
  4694. ret;
  4695. if (end - start === 0) { // no angle, don't show it.
  4696. return ['x'];
  4697. }
  4698. ret = [
  4699. 'wa', // clockwise arc to
  4700. x - radius, // left
  4701. y - radius, // top
  4702. x + radius, // right
  4703. y + radius, // bottom
  4704. x + radius * cosStart, // start x
  4705. y + radius * sinStart, // start y
  4706. x + radius * cosEnd, // end x
  4707. y + radius * sinEnd // end y
  4708. ];
  4709. if (options.open && !innerRadius) {
  4710. ret.push(
  4711. 'e',
  4712. M,
  4713. x,// - innerRadius,
  4714. y// - innerRadius
  4715. );
  4716. }
  4717. ret.push(
  4718. 'at', // anti clockwise arc to
  4719. x - innerRadius, // left
  4720. y - innerRadius, // top
  4721. x + innerRadius, // right
  4722. y + innerRadius, // bottom
  4723. x + innerRadius * cosEnd, // start x
  4724. y + innerRadius * sinEnd, // start y
  4725. x + innerRadius * cosStart, // end x
  4726. y + innerRadius * sinStart, // end y
  4727. 'x', // finish path
  4728. 'e' // close
  4729. );
  4730. ret.isArc = true;
  4731. return ret;
  4732. },
  4733. // Add circle symbol path. This performs significantly faster than v:oval.
  4734. circle: function (x, y, w, h, wrapper) {
  4735. if (wrapper) {
  4736. w = h = 2 * wrapper.r;
  4737. }
  4738. // Center correction, #1682
  4739. if (wrapper && wrapper.isCircle) {
  4740. x -= w / 2;
  4741. y -= h / 2;
  4742. }
  4743. // Return the path
  4744. return [
  4745. 'wa', // clockwisearcto
  4746. x, // left
  4747. y, // top
  4748. x + w, // right
  4749. y + h, // bottom
  4750. x + w, // start x
  4751. y + h / 2, // start y
  4752. x + w, // end x
  4753. y + h / 2, // end y
  4754. //'x', // finish path
  4755. 'e' // close
  4756. ];
  4757. },
  4758. /**
  4759. * Add rectangle symbol path which eases rotation and omits arcsize problems
  4760. * compared to the built-in VML roundrect shape
  4761. *
  4762. * @param {Number} left Left position
  4763. * @param {Number} top Top position
  4764. * @param {Number} r Border radius
  4765. * @param {Object} options Width and height
  4766. */
  4767. rect: function (left, top, width, height, options) {
  4768. var right = left + width,
  4769. bottom = top + height,
  4770. ret,
  4771. r;
  4772. // No radius, return the more lightweight square
  4773. if (!defined(options) || !options.r) {
  4774. ret = SVGRenderer.prototype.symbols.square.apply(0, arguments);
  4775. // Has radius add arcs for the corners
  4776. } else {
  4777. r = mathMin(options.r, width, height);
  4778. ret = [
  4779. M,
  4780. left + r, top,
  4781. L,
  4782. right - r, top,
  4783. 'wa',
  4784. right - 2 * r, top,
  4785. right, top + 2 * r,
  4786. right - r, top,
  4787. right, top + r,
  4788. L,
  4789. right, bottom - r,
  4790. 'wa',
  4791. right - 2 * r, bottom - 2 * r,
  4792. right, bottom,
  4793. right, bottom - r,
  4794. right - r, bottom,
  4795. L,
  4796. left + r, bottom,
  4797. 'wa',
  4798. left, bottom - 2 * r,
  4799. left + 2 * r, bottom,
  4800. left + r, bottom,
  4801. left, bottom - r,
  4802. L,
  4803. left, top + r,
  4804. 'wa',
  4805. left, top,
  4806. left + 2 * r, top + 2 * r,
  4807. left, top + r,
  4808. left + r, top,
  4809. 'x',
  4810. 'e'
  4811. ];
  4812. }
  4813. return ret;
  4814. }
  4815. }
  4816. };
  4817. Highcharts.VMLRenderer = VMLRenderer = function () {
  4818. this.init.apply(this, arguments);
  4819. };
  4820. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  4821. // general renderer
  4822. Renderer = VMLRenderer;
  4823. }
  4824. // This method is used with exporting in old IE, when emulating SVG (see #2314)
  4825. SVGRenderer.prototype.measureSpanWidth = function (text, styles) {
  4826. var measuringSpan = doc.createElement('span'),
  4827. offsetWidth,
  4828. textNode = doc.createTextNode(text);
  4829. measuringSpan.appendChild(textNode);
  4830. css(measuringSpan, styles);
  4831. this.box.appendChild(measuringSpan);
  4832. offsetWidth = measuringSpan.offsetWidth;
  4833. discardElement(measuringSpan); // #2463
  4834. return offsetWidth;
  4835. };
  4836. /* ****************************************************************************
  4837. * *
  4838. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4839. * *
  4840. *****************************************************************************/
  4841. /* ****************************************************************************
  4842. * *
  4843. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  4844. * TARGETING THAT SYSTEM. *
  4845. * *
  4846. *****************************************************************************/
  4847. var CanVGRenderer,
  4848. CanVGController;
  4849. if (useCanVG) {
  4850. /**
  4851. * The CanVGRenderer is empty from start to keep the source footprint small.
  4852. * When requested, the CanVGController downloads the rest of the source packaged
  4853. * together with the canvg library.
  4854. */
  4855. Highcharts.CanVGRenderer = CanVGRenderer = function () {
  4856. // Override the global SVG namespace to fake SVG/HTML that accepts CSS
  4857. SVG_NS = 'http://www.w3.org/1999/xhtml';
  4858. };
  4859. /**
  4860. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  4861. * the implementation from SvgRenderer will not be merged in until first render.
  4862. */
  4863. CanVGRenderer.prototype.symbols = {};
  4864. /**
  4865. * Handles on demand download of canvg rendering support.
  4866. */
  4867. CanVGController = (function () {
  4868. // List of renderering calls
  4869. var deferredRenderCalls = [];
  4870. /**
  4871. * When downloaded, we are ready to draw deferred charts.
  4872. */
  4873. function drawDeferred() {
  4874. var callLength = deferredRenderCalls.length,
  4875. callIndex;
  4876. // Draw all pending render calls
  4877. for (callIndex = 0; callIndex < callLength; callIndex++) {
  4878. deferredRenderCalls[callIndex]();
  4879. }
  4880. // Clear the list
  4881. deferredRenderCalls = [];
  4882. }
  4883. return {
  4884. push: function (func, scriptLocation) {
  4885. // Only get the script once
  4886. if (deferredRenderCalls.length === 0) {
  4887. getScript(scriptLocation, drawDeferred);
  4888. }
  4889. // Register render call
  4890. deferredRenderCalls.push(func);
  4891. }
  4892. };
  4893. }());
  4894. Renderer = CanVGRenderer;
  4895. } // end CanVGRenderer
  4896. /* ****************************************************************************
  4897. * *
  4898. * END OF ANDROID < 3 SPECIFIC CODE *
  4899. * *
  4900. *****************************************************************************/
  4901. /**
  4902. * The Tick class
  4903. */
  4904. function Tick(axis, pos, type, noLabel) {
  4905. this.axis = axis;
  4906. this.pos = pos;
  4907. this.type = type || '';
  4908. this.isNew = true;
  4909. if (!type && !noLabel) {
  4910. this.addLabel();
  4911. }
  4912. }
  4913. Tick.prototype = {
  4914. /**
  4915. * Write the tick label
  4916. */
  4917. addLabel: function () {
  4918. var tick = this,
  4919. axis = tick.axis,
  4920. options = axis.options,
  4921. chart = axis.chart,
  4922. horiz = axis.horiz,
  4923. categories = axis.categories,
  4924. names = axis.names,
  4925. pos = tick.pos,
  4926. labelOptions = options.labels,
  4927. str,
  4928. tickPositions = axis.tickPositions,
  4929. width = (horiz && categories &&
  4930. !labelOptions.step && !labelOptions.staggerLines &&
  4931. !labelOptions.rotation &&
  4932. chart.plotWidth / tickPositions.length) ||
  4933. (!horiz && (chart.margin[3] || chart.chartWidth * 0.33)), // #1580, #1931
  4934. isFirst = pos === tickPositions[0],
  4935. isLast = pos === tickPositions[tickPositions.length - 1],
  4936. css,
  4937. attr,
  4938. value = categories ?
  4939. pick(categories[pos], names[pos], pos) :
  4940. pos,
  4941. label = tick.label,
  4942. tickPositionInfo = tickPositions.info,
  4943. dateTimeLabelFormat;
  4944. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  4945. // use the general format.
  4946. if (axis.isDatetimeAxis && tickPositionInfo) {
  4947. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  4948. }
  4949. // set properties for access in render method
  4950. tick.isFirst = isFirst;
  4951. tick.isLast = isLast;
  4952. // get the string
  4953. str = axis.labelFormatter.call({
  4954. axis: axis,
  4955. chart: chart,
  4956. isFirst: isFirst,
  4957. isLast: isLast,
  4958. dateTimeLabelFormat: dateTimeLabelFormat,
  4959. value: axis.isLog ? correctFloat(lin2log(value)) : value
  4960. });
  4961. // prepare CSS
  4962. css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  4963. css = extend(css, labelOptions.style);
  4964. // first call
  4965. if (!defined(label)) {
  4966. attr = {
  4967. align: axis.labelAlign
  4968. };
  4969. if (isNumber(labelOptions.rotation)) {
  4970. attr.rotation = labelOptions.rotation;
  4971. }
  4972. if (width && labelOptions.ellipsis) {
  4973. attr._clipHeight = axis.len / tickPositions.length;
  4974. }
  4975. tick.label =
  4976. defined(str) && labelOptions.enabled ?
  4977. chart.renderer.text(
  4978. str,
  4979. 0,
  4980. 0,
  4981. labelOptions.useHTML
  4982. )
  4983. .attr(attr)
  4984. // without position absolute, IE export sometimes is wrong
  4985. .css(css)
  4986. .add(axis.labelGroup) :
  4987. null;
  4988. // update
  4989. } else if (label) {
  4990. label.attr({
  4991. text: str
  4992. })
  4993. .css(css);
  4994. }
  4995. },
  4996. /**
  4997. * Get the offset height or width of the label
  4998. */
  4999. getLabelSize: function () {
  5000. var label = this.label,
  5001. axis = this.axis;
  5002. return label ?
  5003. label.getBBox()[axis.horiz ? 'height' : 'width'] :
  5004. 0;
  5005. },
  5006. /**
  5007. * Find how far the labels extend to the right and left of the tick's x position. Used for anti-collision
  5008. * detection with overflow logic.
  5009. */
  5010. getLabelSides: function () {
  5011. var bBox = this.label.getBBox(),
  5012. axis = this.axis,
  5013. horiz = axis.horiz,
  5014. options = axis.options,
  5015. labelOptions = options.labels,
  5016. size = horiz ? bBox.width : bBox.height,
  5017. leftSide = horiz ?
  5018. labelOptions.x - size * { left: 0, center: 0.5, right: 1 }[axis.labelAlign] :
  5019. 0,
  5020. rightSide = horiz ?
  5021. size + leftSide :
  5022. size;
  5023. return [leftSide, rightSide];
  5024. },
  5025. /**
  5026. * Handle the label overflow by adjusting the labels to the left and right edge, or
  5027. * hide them if they collide into the neighbour label.
  5028. */
  5029. handleOverflow: function (index, xy) {
  5030. var show = true,
  5031. axis = this.axis,
  5032. isFirst = this.isFirst,
  5033. isLast = this.isLast,
  5034. horiz = axis.horiz,
  5035. pxPos = horiz ? xy.x : xy.y,
  5036. reversed = axis.reversed,
  5037. tickPositions = axis.tickPositions,
  5038. sides = this.getLabelSides(),
  5039. leftSide = sides[0],
  5040. rightSide = sides[1],
  5041. axisLeft,
  5042. axisRight,
  5043. neighbour,
  5044. neighbourEdge,
  5045. line = this.label.line || 0,
  5046. labelEdge = axis.labelEdge,
  5047. justifyLabel = axis.justifyLabels && (isFirst || isLast),
  5048. justifyToPlot;
  5049. // Hide it if it now overlaps the neighbour label
  5050. if (labelEdge[line] === UNDEFINED || pxPos + leftSide > labelEdge[line]) {
  5051. labelEdge[line] = pxPos + rightSide;
  5052. } else if (!justifyLabel) {
  5053. show = false;
  5054. }
  5055. if (justifyLabel) {
  5056. justifyToPlot = axis.justifyToPlot;
  5057. axisLeft = justifyToPlot ? axis.pos : 0;
  5058. axisRight = justifyToPlot ? axisLeft + axis.len : axis.chart.chartWidth;
  5059. // Find the firsth neighbour on the same line
  5060. do {
  5061. index += (isFirst ? 1 : -1);
  5062. neighbour = axis.ticks[tickPositions[index]];
  5063. } while (tickPositions[index] && (!neighbour || neighbour.label.line !== line));
  5064. neighbourEdge = neighbour && neighbour.label.xy && neighbour.label.xy.x + neighbour.getLabelSides()[isFirst ? 0 : 1];
  5065. if ((isFirst && !reversed) || (isLast && reversed)) {
  5066. // Is the label spilling out to the left of the plot area?
  5067. if (pxPos + leftSide < axisLeft) {
  5068. // Align it to plot left
  5069. pxPos = axisLeft - leftSide;
  5070. // Hide it if it now overlaps the neighbour label
  5071. if (neighbour && pxPos + rightSide > neighbourEdge) {
  5072. show = false;
  5073. }
  5074. }
  5075. } else {
  5076. // Is the label spilling out to the right of the plot area?
  5077. if (pxPos + rightSide > axisRight) {
  5078. // Align it to plot right
  5079. pxPos = axisRight - rightSide;
  5080. // Hide it if it now overlaps the neighbour label
  5081. if (neighbour && pxPos + leftSide < neighbourEdge) {
  5082. show = false;
  5083. }
  5084. }
  5085. }
  5086. // Set the modified x position of the label
  5087. xy.x = pxPos;
  5088. }
  5089. return show;
  5090. },
  5091. /**
  5092. * Get the x and y position for ticks and labels
  5093. */
  5094. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5095. var axis = this.axis,
  5096. chart = axis.chart,
  5097. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5098. return {
  5099. x: horiz ?
  5100. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5101. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5102. y: horiz ?
  5103. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5104. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5105. };
  5106. },
  5107. /**
  5108. * Get the x, y position of the tick label
  5109. */
  5110. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5111. var axis = this.axis,
  5112. transA = axis.transA,
  5113. reversed = axis.reversed,
  5114. staggerLines = axis.staggerLines,
  5115. baseline = axis.chart.renderer.fontMetrics(labelOptions.style.fontSize).b,
  5116. rotation = labelOptions.rotation;
  5117. x = x + labelOptions.x - (tickmarkOffset && horiz ?
  5118. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5119. y = y + labelOptions.y - (tickmarkOffset && !horiz ?
  5120. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5121. // Correct for rotation (#1764)
  5122. if (rotation && axis.side === 2) {
  5123. y -= baseline - baseline * mathCos(rotation * deg2rad);
  5124. }
  5125. // Vertically centered
  5126. if (!defined(labelOptions.y) && !rotation) { // #1951
  5127. y += baseline - label.getBBox().height / 2;
  5128. }
  5129. // Correct for staggered labels
  5130. if (staggerLines) {
  5131. label.line = (index / (step || 1) % staggerLines);
  5132. y += label.line * (axis.labelOffset / staggerLines);
  5133. }
  5134. return {
  5135. x: x,
  5136. y: y
  5137. };
  5138. },
  5139. /**
  5140. * Extendible method to return the path of the marker
  5141. */
  5142. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5143. return renderer.crispLine([
  5144. M,
  5145. x,
  5146. y,
  5147. L,
  5148. x + (horiz ? 0 : -tickLength),
  5149. y + (horiz ? tickLength : 0)
  5150. ], tickWidth);
  5151. },
  5152. /**
  5153. * Put everything in place
  5154. *
  5155. * @param index {Number}
  5156. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5157. */
  5158. render: function (index, old, opacity) {
  5159. var tick = this,
  5160. axis = tick.axis,
  5161. options = axis.options,
  5162. chart = axis.chart,
  5163. renderer = chart.renderer,
  5164. horiz = axis.horiz,
  5165. type = tick.type,
  5166. label = tick.label,
  5167. pos = tick.pos,
  5168. labelOptions = options.labels,
  5169. gridLine = tick.gridLine,
  5170. gridPrefix = type ? type + 'Grid' : 'grid',
  5171. tickPrefix = type ? type + 'Tick' : 'tick',
  5172. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5173. gridLineColor = options[gridPrefix + 'LineColor'],
  5174. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5175. tickLength = options[tickPrefix + 'Length'],
  5176. tickWidth = options[tickPrefix + 'Width'] || 0,
  5177. tickColor = options[tickPrefix + 'Color'],
  5178. tickPosition = options[tickPrefix + 'Position'],
  5179. gridLinePath,
  5180. mark = tick.mark,
  5181. markPath,
  5182. step = labelOptions.step,
  5183. attribs,
  5184. show = true,
  5185. tickmarkOffset = axis.tickmarkOffset,
  5186. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5187. x = xy.x,
  5188. y = xy.y,
  5189. reverseCrisp = ((horiz && x === axis.pos + axis.len) || (!horiz && y === axis.pos)) ? -1 : 1; // #1480, #1687
  5190. this.isActive = true;
  5191. // create the grid line
  5192. if (gridLineWidth) {
  5193. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth * reverseCrisp, old, true);
  5194. if (gridLine === UNDEFINED) {
  5195. attribs = {
  5196. stroke: gridLineColor,
  5197. 'stroke-width': gridLineWidth
  5198. };
  5199. if (dashStyle) {
  5200. attribs.dashstyle = dashStyle;
  5201. }
  5202. if (!type) {
  5203. attribs.zIndex = 1;
  5204. }
  5205. if (old) {
  5206. attribs.opacity = 0;
  5207. }
  5208. tick.gridLine = gridLine =
  5209. gridLineWidth ?
  5210. renderer.path(gridLinePath)
  5211. .attr(attribs).add(axis.gridGroup) :
  5212. null;
  5213. }
  5214. // If the parameter 'old' is set, the current call will be followed
  5215. // by another call, therefore do not do any animations this time
  5216. if (!old && gridLine && gridLinePath) {
  5217. gridLine[tick.isNew ? 'attr' : 'animate']({
  5218. d: gridLinePath,
  5219. opacity: opacity
  5220. });
  5221. }
  5222. }
  5223. // create the tick mark
  5224. if (tickWidth && tickLength) {
  5225. // negate the length
  5226. if (tickPosition === 'inside') {
  5227. tickLength = -tickLength;
  5228. }
  5229. if (axis.opposite) {
  5230. tickLength = -tickLength;
  5231. }
  5232. markPath = tick.getMarkPath(x, y, tickLength, tickWidth * reverseCrisp, horiz, renderer);
  5233. if (mark) { // updating
  5234. mark.animate({
  5235. d: markPath,
  5236. opacity: opacity
  5237. });
  5238. } else { // first time
  5239. tick.mark = renderer.path(
  5240. markPath
  5241. ).attr({
  5242. stroke: tickColor,
  5243. 'stroke-width': tickWidth,
  5244. opacity: opacity
  5245. }).add(axis.axisGroup);
  5246. }
  5247. }
  5248. // the label is created on init - now move it into place
  5249. if (label && !isNaN(x)) {
  5250. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5251. // Apply show first and show last. If the tick is both first and last, it is
  5252. // a single centered tick, in which case we show the label anyway (#2100).
  5253. if ((tick.isFirst && !tick.isLast && !pick(options.showFirstLabel, 1)) ||
  5254. (tick.isLast && !tick.isFirst && !pick(options.showLastLabel, 1))) {
  5255. show = false;
  5256. // Handle label overflow and show or hide accordingly
  5257. } else if (!axis.isRadial && !labelOptions.step && !labelOptions.rotation && !old && opacity !== 0) {
  5258. show = tick.handleOverflow(index, xy);
  5259. }
  5260. // apply step
  5261. if (step && index % step) {
  5262. // show those indices dividable by step
  5263. show = false;
  5264. }
  5265. // Set the new position, and show or hide
  5266. if (show && !isNaN(xy.y)) {
  5267. xy.opacity = opacity;
  5268. label[tick.isNew ? 'attr' : 'animate'](xy);
  5269. tick.isNew = false;
  5270. } else {
  5271. label.attr('y', -9999); // #1338
  5272. }
  5273. }
  5274. },
  5275. /**
  5276. * Destructor for the tick prototype
  5277. */
  5278. destroy: function () {
  5279. destroyObjectProperties(this, this.axis);
  5280. }
  5281. };
  5282. /**
  5283. * The object wrapper for plot lines and plot bands
  5284. * @param {Object} options
  5285. */
  5286. Highcharts.PlotLineOrBand = function (axis, options) {
  5287. this.axis = axis;
  5288. if (options) {
  5289. this.options = options;
  5290. this.id = options.id;
  5291. }
  5292. };
  5293. Highcharts.PlotLineOrBand.prototype = {
  5294. /**
  5295. * Render the plot line or plot band. If it is already existing,
  5296. * move it.
  5297. */
  5298. render: function () {
  5299. var plotLine = this,
  5300. axis = plotLine.axis,
  5301. horiz = axis.horiz,
  5302. halfPointRange = (axis.pointRange || 0) / 2,
  5303. options = plotLine.options,
  5304. optionsLabel = options.label,
  5305. label = plotLine.label,
  5306. width = options.width,
  5307. to = options.to,
  5308. from = options.from,
  5309. isBand = defined(from) && defined(to),
  5310. value = options.value,
  5311. dashStyle = options.dashStyle,
  5312. svgElem = plotLine.svgElem,
  5313. path = [],
  5314. addEvent,
  5315. eventType,
  5316. xs,
  5317. ys,
  5318. x,
  5319. y,
  5320. color = options.color,
  5321. zIndex = options.zIndex,
  5322. events = options.events,
  5323. attribs,
  5324. renderer = axis.chart.renderer;
  5325. // logarithmic conversion
  5326. if (axis.isLog) {
  5327. from = log2lin(from);
  5328. to = log2lin(to);
  5329. value = log2lin(value);
  5330. }
  5331. // plot line
  5332. if (width) {
  5333. path = axis.getPlotLinePath(value, width);
  5334. attribs = {
  5335. stroke: color,
  5336. 'stroke-width': width
  5337. };
  5338. if (dashStyle) {
  5339. attribs.dashstyle = dashStyle;
  5340. }
  5341. } else if (isBand) { // plot band
  5342. // keep within plot area
  5343. from = mathMax(from, axis.min - halfPointRange);
  5344. to = mathMin(to, axis.max + halfPointRange);
  5345. path = axis.getPlotBandPath(from, to, options);
  5346. attribs = {
  5347. fill: color
  5348. };
  5349. if (options.borderWidth) {
  5350. attribs.stroke = options.borderColor;
  5351. attribs['stroke-width'] = options.borderWidth;
  5352. }
  5353. } else {
  5354. return;
  5355. }
  5356. // zIndex
  5357. if (defined(zIndex)) {
  5358. attribs.zIndex = zIndex;
  5359. }
  5360. // common for lines and bands
  5361. if (svgElem) {
  5362. if (path) {
  5363. svgElem.animate({
  5364. d: path
  5365. }, null, svgElem.onGetPath);
  5366. } else {
  5367. svgElem.hide();
  5368. svgElem.onGetPath = function () {
  5369. svgElem.show();
  5370. };
  5371. if (label) {
  5372. plotLine.label = label = label.destroy();
  5373. }
  5374. }
  5375. } else if (path && path.length) {
  5376. plotLine.svgElem = svgElem = renderer.path(path)
  5377. .attr(attribs).add();
  5378. // events
  5379. if (events) {
  5380. addEvent = function (eventType) {
  5381. svgElem.on(eventType, function (e) {
  5382. events[eventType].apply(plotLine, [e]);
  5383. });
  5384. };
  5385. for (eventType in events) {
  5386. addEvent(eventType);
  5387. }
  5388. }
  5389. }
  5390. // the plot band/line label
  5391. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axis.width > 0 && axis.height > 0) {
  5392. // apply defaults
  5393. optionsLabel = merge({
  5394. align: horiz && isBand && 'center',
  5395. x: horiz ? !isBand && 4 : 10,
  5396. verticalAlign : !horiz && isBand && 'middle',
  5397. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5398. rotation: horiz && !isBand && 90
  5399. }, optionsLabel);
  5400. // add the SVG element
  5401. if (!label) {
  5402. plotLine.label = label = renderer.text(
  5403. optionsLabel.text,
  5404. 0,
  5405. 0,
  5406. optionsLabel.useHTML
  5407. )
  5408. .attr({
  5409. align: optionsLabel.textAlign || optionsLabel.align,
  5410. rotation: optionsLabel.rotation,
  5411. zIndex: zIndex
  5412. })
  5413. .css(optionsLabel.style)
  5414. .add();
  5415. }
  5416. // get the bounding box and align the label
  5417. xs = [path[1], path[4], pick(path[6], path[1])];
  5418. ys = [path[2], path[5], pick(path[7], path[2])];
  5419. x = arrayMin(xs);
  5420. y = arrayMin(ys);
  5421. label.align(optionsLabel, false, {
  5422. x: x,
  5423. y: y,
  5424. width: arrayMax(xs) - x,
  5425. height: arrayMax(ys) - y
  5426. });
  5427. label.show();
  5428. } else if (label) { // move out of sight
  5429. label.hide();
  5430. }
  5431. // chainable
  5432. return plotLine;
  5433. },
  5434. /**
  5435. * Remove the plot line or band
  5436. */
  5437. destroy: function () {
  5438. // remove it from the lookup
  5439. erase(this.axis.plotLinesAndBands, this);
  5440. delete this.axis;
  5441. destroyObjectProperties(this);
  5442. }
  5443. };
  5444. /**
  5445. * Object with members for extending the Axis prototype
  5446. */
  5447. AxisPlotLineOrBandExtension = {
  5448. /**
  5449. * Create the path for a plot band
  5450. */
  5451. getPlotBandPath: function (from, to) {
  5452. var toPath = this.getPlotLinePath(to),
  5453. path = this.getPlotLinePath(from);
  5454. if (path && toPath) {
  5455. path.push(
  5456. toPath[4],
  5457. toPath[5],
  5458. toPath[1],
  5459. toPath[2]
  5460. );
  5461. } else { // outside the axis area
  5462. path = null;
  5463. }
  5464. return path;
  5465. },
  5466. addPlotBand: function (options) {
  5467. this.addPlotBandOrLine(options, 'plotBands');
  5468. },
  5469. addPlotLine: function (options) {
  5470. this.addPlotBandOrLine(options, 'plotLines');
  5471. },
  5472. /**
  5473. * Add a plot band or plot line after render time
  5474. *
  5475. * @param options {Object} The plotBand or plotLine configuration object
  5476. */
  5477. addPlotBandOrLine: function (options, coll) {
  5478. var obj = new Highcharts.PlotLineOrBand(this, options).render(),
  5479. userOptions = this.userOptions;
  5480. if (obj) { // #2189
  5481. // Add it to the user options for exporting and Axis.update
  5482. if (coll) {
  5483. userOptions[coll] = userOptions[coll] || [];
  5484. userOptions[coll].push(options);
  5485. }
  5486. this.plotLinesAndBands.push(obj);
  5487. }
  5488. return obj;
  5489. },
  5490. /**
  5491. * Remove a plot band or plot line from the chart by id
  5492. * @param {Object} id
  5493. */
  5494. removePlotBandOrLine: function (id) {
  5495. var plotLinesAndBands = this.plotLinesAndBands,
  5496. options = this.options,
  5497. userOptions = this.userOptions,
  5498. i = plotLinesAndBands.length;
  5499. while (i--) {
  5500. if (plotLinesAndBands[i].id === id) {
  5501. plotLinesAndBands[i].destroy();
  5502. }
  5503. }
  5504. each([options.plotLines || [], userOptions.plotLines || [], options.plotBands || [], userOptions.plotBands || []], function (arr) {
  5505. i = arr.length;
  5506. while (i--) {
  5507. if (arr[i].id === id) {
  5508. erase(arr, arr[i]);
  5509. }
  5510. }
  5511. });
  5512. }
  5513. };
  5514. /**
  5515. * Create a new axis object
  5516. * @param {Object} chart
  5517. * @param {Object} options
  5518. */
  5519. function Axis() {
  5520. this.init.apply(this, arguments);
  5521. }
  5522. Axis.prototype = {
  5523. /**
  5524. * Default options for the X axis - the Y axis has extended defaults
  5525. */
  5526. defaultOptions: {
  5527. // allowDecimals: null,
  5528. // alternateGridColor: null,
  5529. // categories: [],
  5530. dateTimeLabelFormats: {
  5531. millisecond: '%H:%M:%S.%L',
  5532. second: '%H:%M:%S',
  5533. minute: '%H:%M',
  5534. hour: '%H:%M',
  5535. day: '%e. %b',
  5536. week: '%e. %b',
  5537. month: '%b \'%y',
  5538. year: '%Y'
  5539. },
  5540. endOnTick: false,
  5541. gridLineColor: '#C0C0C0',
  5542. // gridLineDashStyle: 'solid',
  5543. // gridLineWidth: 0,
  5544. // reversed: false,
  5545. labels: defaultLabelOptions,
  5546. // { step: null },
  5547. lineColor: '#C0D0E0',
  5548. lineWidth: 1,
  5549. //linkedTo: null,
  5550. //max: undefined,
  5551. //min: undefined,
  5552. minPadding: 0.01,
  5553. maxPadding: 0.01,
  5554. //minRange: null,
  5555. minorGridLineColor: '#E0E0E0',
  5556. // minorGridLineDashStyle: null,
  5557. minorGridLineWidth: 1,
  5558. minorTickColor: '#A0A0A0',
  5559. //minorTickInterval: null,
  5560. minorTickLength: 2,
  5561. minorTickPosition: 'outside', // inside or outside
  5562. //minorTickWidth: 0,
  5563. //opposite: false,
  5564. //offset: 0,
  5565. //plotBands: [{
  5566. // events: {},
  5567. // zIndex: 1,
  5568. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5569. //}],
  5570. //plotLines: [{
  5571. // events: {}
  5572. // dashStyle: {}
  5573. // zIndex:
  5574. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5575. //}],
  5576. //reversed: false,
  5577. // showFirstLabel: true,
  5578. // showLastLabel: true,
  5579. startOfWeek: 1,
  5580. startOnTick: false,
  5581. tickColor: '#C0D0E0',
  5582. //tickInterval: null,
  5583. tickLength: 5,
  5584. tickmarkPlacement: 'between', // on or between
  5585. tickPixelInterval: 100,
  5586. tickPosition: 'outside',
  5587. tickWidth: 1,
  5588. title: {
  5589. //text: null,
  5590. align: 'middle', // low, middle or high
  5591. //margin: 0 for horizontal, 10 for vertical axes,
  5592. //rotation: 0,
  5593. //side: 'outside',
  5594. style: {
  5595. color: '#4d759e',
  5596. //font: defaultFont.replace('normal', 'bold')
  5597. fontWeight: 'bold'
  5598. }
  5599. //x: 0,
  5600. //y: 0
  5601. },
  5602. type: 'linear' // linear, logarithmic or datetime
  5603. },
  5604. /**
  5605. * This options set extends the defaultOptions for Y axes
  5606. */
  5607. defaultYAxisOptions: {
  5608. endOnTick: true,
  5609. gridLineWidth: 1,
  5610. tickPixelInterval: 72,
  5611. showLastLabel: true,
  5612. labels: {
  5613. x: -8,
  5614. y: 3
  5615. },
  5616. lineWidth: 0,
  5617. maxPadding: 0.05,
  5618. minPadding: 0.05,
  5619. startOnTick: true,
  5620. tickWidth: 0,
  5621. title: {
  5622. rotation: 270,
  5623. text: 'Values'
  5624. },
  5625. stackLabels: {
  5626. enabled: false,
  5627. //align: dynamic,
  5628. //y: dynamic,
  5629. //x: dynamic,
  5630. //verticalAlign: dynamic,
  5631. //textAlign: dynamic,
  5632. //rotation: 0,
  5633. formatter: function () {
  5634. return numberFormat(this.total, -1);
  5635. },
  5636. style: defaultLabelOptions.style
  5637. }
  5638. },
  5639. /**
  5640. * These options extend the defaultOptions for left axes
  5641. */
  5642. defaultLeftAxisOptions: {
  5643. labels: {
  5644. x: -8,
  5645. y: null
  5646. },
  5647. title: {
  5648. rotation: 270
  5649. }
  5650. },
  5651. /**
  5652. * These options extend the defaultOptions for right axes
  5653. */
  5654. defaultRightAxisOptions: {
  5655. labels: {
  5656. x: 8,
  5657. y: null
  5658. },
  5659. title: {
  5660. rotation: 90
  5661. }
  5662. },
  5663. /**
  5664. * These options extend the defaultOptions for bottom axes
  5665. */
  5666. defaultBottomAxisOptions: {
  5667. labels: {
  5668. x: 0,
  5669. y: 14
  5670. // overflow: undefined,
  5671. // staggerLines: null
  5672. },
  5673. title: {
  5674. rotation: 0
  5675. }
  5676. },
  5677. /**
  5678. * These options extend the defaultOptions for left axes
  5679. */
  5680. defaultTopAxisOptions: {
  5681. labels: {
  5682. x: 0,
  5683. y: -5
  5684. // overflow: undefined
  5685. // staggerLines: null
  5686. },
  5687. title: {
  5688. rotation: 0
  5689. }
  5690. },
  5691. /**
  5692. * Initialize the axis
  5693. */
  5694. init: function (chart, userOptions) {
  5695. var isXAxis = userOptions.isX,
  5696. axis = this;
  5697. // Flag, is the axis horizontal
  5698. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  5699. // Flag, isXAxis
  5700. axis.isXAxis = isXAxis;
  5701. axis.coll = isXAxis ? 'xAxis' : 'yAxis';
  5702. axis.opposite = userOptions.opposite; // needed in setOptions
  5703. axis.side = userOptions.side || (axis.horiz ?
  5704. (axis.opposite ? 0 : 2) : // top : bottom
  5705. (axis.opposite ? 1 : 3)); // right : left
  5706. axis.setOptions(userOptions);
  5707. var options = this.options,
  5708. type = options.type,
  5709. isDatetimeAxis = type === 'datetime';
  5710. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  5711. // Flag, stagger lines or not
  5712. axis.userOptions = userOptions;
  5713. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  5714. axis.minPixelPadding = 0;
  5715. //axis.ignoreMinPadding = UNDEFINED; // can be set to true by a column or bar series
  5716. //axis.ignoreMaxPadding = UNDEFINED;
  5717. axis.chart = chart;
  5718. axis.reversed = options.reversed;
  5719. axis.zoomEnabled = options.zoomEnabled !== false;
  5720. // Initial categories
  5721. axis.categories = options.categories || type === 'category';
  5722. axis.names = [];
  5723. // Elements
  5724. //axis.axisGroup = UNDEFINED;
  5725. //axis.gridGroup = UNDEFINED;
  5726. //axis.axisTitle = UNDEFINED;
  5727. //axis.axisLine = UNDEFINED;
  5728. // Shorthand types
  5729. axis.isLog = type === 'logarithmic';
  5730. axis.isDatetimeAxis = isDatetimeAxis;
  5731. // Flag, if axis is linked to another axis
  5732. axis.isLinked = defined(options.linkedTo);
  5733. // Linked axis.
  5734. //axis.linkedParent = UNDEFINED;
  5735. // Tick positions
  5736. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  5737. // Tick intervals
  5738. //axis.tickInterval = UNDEFINED;
  5739. //axis.minorTickInterval = UNDEFINED;
  5740. axis.tickmarkOffset = (axis.categories && options.tickmarkPlacement === 'between') ? 0.5 : 0;
  5741. // Major ticks
  5742. axis.ticks = {};
  5743. axis.labelEdge = [];
  5744. // Minor ticks
  5745. axis.minorTicks = {};
  5746. //axis.tickAmount = UNDEFINED;
  5747. // List of plotLines/Bands
  5748. axis.plotLinesAndBands = [];
  5749. // Alternate bands
  5750. axis.alternateBands = {};
  5751. // Axis metrics
  5752. //axis.left = UNDEFINED;
  5753. //axis.top = UNDEFINED;
  5754. //axis.width = UNDEFINED;
  5755. //axis.height = UNDEFINED;
  5756. //axis.bottom = UNDEFINED;
  5757. //axis.right = UNDEFINED;
  5758. //axis.transA = UNDEFINED;
  5759. //axis.transB = UNDEFINED;
  5760. //axis.oldTransA = UNDEFINED;
  5761. axis.len = 0;
  5762. //axis.oldMin = UNDEFINED;
  5763. //axis.oldMax = UNDEFINED;
  5764. //axis.oldUserMin = UNDEFINED;
  5765. //axis.oldUserMax = UNDEFINED;
  5766. //axis.oldAxisLength = UNDEFINED;
  5767. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  5768. axis.range = options.range;
  5769. axis.offset = options.offset || 0;
  5770. // Dictionary for stacks
  5771. axis.stacks = {};
  5772. axis.oldStacks = {};
  5773. // Min and max in the data
  5774. //axis.dataMin = UNDEFINED,
  5775. //axis.dataMax = UNDEFINED,
  5776. // The axis range
  5777. axis.max = null;
  5778. axis.min = null;
  5779. // User set min and max
  5780. //axis.userMin = UNDEFINED,
  5781. //axis.userMax = UNDEFINED,
  5782. // Crosshair options
  5783. axis.crosshair = pick(options.crosshair, splat(chart.options.tooltip.crosshairs)[isXAxis ? 0 : 1], false);
  5784. // Run Axis
  5785. var eventType,
  5786. events = axis.options.events;
  5787. // Register
  5788. if (inArray(axis, chart.axes) === -1) { // don't add it again on Axis.update()
  5789. if (isXAxis && !this.isColorAxis) { // #2713
  5790. chart.axes.splice(chart.xAxis.length, 0, axis);
  5791. } else {
  5792. chart.axes.push(axis);
  5793. }
  5794. chart[axis.coll].push(axis);
  5795. }
  5796. axis.series = axis.series || []; // populated by Series
  5797. // inverted charts have reversed xAxes as default
  5798. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  5799. axis.reversed = true;
  5800. }
  5801. axis.removePlotBand = axis.removePlotBandOrLine;
  5802. axis.removePlotLine = axis.removePlotBandOrLine;
  5803. // register event listeners
  5804. for (eventType in events) {
  5805. addEvent(axis, eventType, events[eventType]);
  5806. }
  5807. // extend logarithmic axis
  5808. if (axis.isLog) {
  5809. axis.val2lin = log2lin;
  5810. axis.lin2val = lin2log;
  5811. }
  5812. },
  5813. /**
  5814. * Merge and set options
  5815. */
  5816. setOptions: function (userOptions) {
  5817. this.options = merge(
  5818. this.defaultOptions,
  5819. this.isXAxis ? {} : this.defaultYAxisOptions,
  5820. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  5821. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  5822. merge(
  5823. defaultOptions[this.coll], // if set in setOptions (#1053)
  5824. userOptions
  5825. )
  5826. );
  5827. },
  5828. /**
  5829. * The default label formatter. The context is a special config object for the label.
  5830. */
  5831. defaultLabelFormatter: function () {
  5832. var axis = this.axis,
  5833. value = this.value,
  5834. categories = axis.categories,
  5835. dateTimeLabelFormat = this.dateTimeLabelFormat,
  5836. numericSymbols = defaultOptions.lang.numericSymbols,
  5837. i = numericSymbols && numericSymbols.length,
  5838. multi,
  5839. ret,
  5840. formatOption = axis.options.labels.format,
  5841. // make sure the same symbol is added for all labels on a linear axis
  5842. numericSymbolDetector = axis.isLog ? value : axis.tickInterval;
  5843. if (formatOption) {
  5844. ret = format(formatOption, this);
  5845. } else if (categories) {
  5846. ret = value;
  5847. } else if (dateTimeLabelFormat) { // datetime axis
  5848. ret = dateFormat(dateTimeLabelFormat, value);
  5849. } else if (i && numericSymbolDetector >= 1000) {
  5850. // Decide whether we should add a numeric symbol like k (thousands) or M (millions).
  5851. // If we are to enable this in tooltip or other places as well, we can move this
  5852. // logic to the numberFormatter and enable it by a parameter.
  5853. while (i-- && ret === UNDEFINED) {
  5854. multi = Math.pow(1000, i + 1);
  5855. if (numericSymbolDetector >= multi && numericSymbols[i] !== null) {
  5856. ret = numberFormat(value / multi, -1) + numericSymbols[i];
  5857. }
  5858. }
  5859. }
  5860. if (ret === UNDEFINED) {
  5861. if (value >= 10000) { // add thousands separators
  5862. ret = numberFormat(value, 0);
  5863. } else { // small numbers
  5864. ret = numberFormat(value, -1, UNDEFINED, ''); // #2466
  5865. }
  5866. }
  5867. return ret;
  5868. },
  5869. /**
  5870. * Get the minimum and maximum for the series of each axis
  5871. */
  5872. getSeriesExtremes: function () {
  5873. var axis = this,
  5874. chart = axis.chart;
  5875. axis.hasVisibleSeries = false;
  5876. // reset dataMin and dataMax in case we're redrawing
  5877. axis.dataMin = axis.dataMax = null;
  5878. if (axis.buildStacks) {
  5879. axis.buildStacks();
  5880. }
  5881. // loop through this axis' series
  5882. each(axis.series, function (series) {
  5883. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  5884. var seriesOptions = series.options,
  5885. xData,
  5886. threshold = seriesOptions.threshold,
  5887. seriesDataMin,
  5888. seriesDataMax;
  5889. axis.hasVisibleSeries = true;
  5890. // Validate threshold in logarithmic axes
  5891. if (axis.isLog && threshold <= 0) {
  5892. threshold = null;
  5893. }
  5894. // Get dataMin and dataMax for X axes
  5895. if (axis.isXAxis) {
  5896. xData = series.xData;
  5897. if (xData.length) {
  5898. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), arrayMin(xData));
  5899. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  5900. }
  5901. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  5902. } else {
  5903. // Get this particular series extremes
  5904. series.getExtremes();
  5905. seriesDataMax = series.dataMax;
  5906. seriesDataMin = series.dataMin;
  5907. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  5908. // always 0 and 100. If seriesDataMin and seriesDataMax is null, then series
  5909. // doesn't have active y data, we continue with nulls
  5910. if (defined(seriesDataMin) && defined(seriesDataMax)) {
  5911. axis.dataMin = mathMin(pick(axis.dataMin, seriesDataMin), seriesDataMin);
  5912. axis.dataMax = mathMax(pick(axis.dataMax, seriesDataMax), seriesDataMax);
  5913. }
  5914. // Adjust to threshold
  5915. if (defined(threshold)) {
  5916. if (axis.dataMin >= threshold) {
  5917. axis.dataMin = threshold;
  5918. axis.ignoreMinPadding = true;
  5919. } else if (axis.dataMax < threshold) {
  5920. axis.dataMax = threshold;
  5921. axis.ignoreMaxPadding = true;
  5922. }
  5923. }
  5924. }
  5925. }
  5926. });
  5927. },
  5928. /**
  5929. * Translate from axis value to pixel position on the chart, or back
  5930. *
  5931. */
  5932. translate: function (val, backwards, cvsCoord, old, handleLog, pointPlacement) {
  5933. var axis = this,
  5934. sign = 1,
  5935. cvsOffset = 0,
  5936. localA = old ? axis.oldTransA : axis.transA,
  5937. localMin = old ? axis.oldMin : axis.min,
  5938. returnValue,
  5939. minPixelPadding = axis.minPixelPadding,
  5940. postTranslate = (axis.options.ordinal || (axis.isLog && handleLog)) && axis.lin2val;
  5941. if (!localA) {
  5942. localA = axis.transA;
  5943. }
  5944. // In vertical axes, the canvas coordinates start from 0 at the top like in
  5945. // SVG.
  5946. if (cvsCoord) {
  5947. sign *= -1; // canvas coordinates inverts the value
  5948. cvsOffset = axis.len;
  5949. }
  5950. // Handle reversed axis
  5951. if (axis.reversed) {
  5952. sign *= -1;
  5953. cvsOffset -= sign * (axis.sector || axis.len);
  5954. }
  5955. // From pixels to value
  5956. if (backwards) { // reverse translation
  5957. val = val * sign + cvsOffset;
  5958. val -= minPixelPadding;
  5959. returnValue = val / localA + localMin; // from chart pixel to value
  5960. if (postTranslate) { // log and ordinal axes
  5961. returnValue = axis.lin2val(returnValue);
  5962. }
  5963. // From value to pixels
  5964. } else {
  5965. if (postTranslate) { // log and ordinal axes
  5966. val = axis.val2lin(val);
  5967. }
  5968. if (pointPlacement === 'between') {
  5969. pointPlacement = 0.5;
  5970. }
  5971. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * minPixelPadding) +
  5972. (isNumber(pointPlacement) ? localA * pointPlacement * axis.pointRange : 0);
  5973. }
  5974. return returnValue;
  5975. },
  5976. /**
  5977. * Utility method to translate an axis value to pixel position.
  5978. * @param {Number} value A value in terms of axis units
  5979. * @param {Boolean} paneCoordinates Whether to return the pixel coordinate relative to the chart
  5980. * or just the axis/pane itself.
  5981. */
  5982. toPixels: function (value, paneCoordinates) {
  5983. return this.translate(value, false, !this.horiz, null, true) + (paneCoordinates ? 0 : this.pos);
  5984. },
  5985. /*
  5986. * Utility method to translate a pixel position in to an axis value
  5987. * @param {Number} pixel The pixel value coordinate
  5988. * @param {Boolean} paneCoordiantes Whether the input pixel is relative to the chart or just the
  5989. * axis/pane itself.
  5990. */
  5991. toValue: function (pixel, paneCoordinates) {
  5992. return this.translate(pixel - (paneCoordinates ? 0 : this.pos), true, !this.horiz, null, true);
  5993. },
  5994. /**
  5995. * Create the path for a plot line that goes from the given value on
  5996. * this axis, across the plot to the opposite side
  5997. * @param {Number} value
  5998. * @param {Number} lineWidth Used for calculation crisp line
  5999. * @param {Number] old Use old coordinates (for resizing and rescaling)
  6000. */
  6001. getPlotLinePath: function (value, lineWidth, old, force, translatedValue) {
  6002. var axis = this,
  6003. chart = axis.chart,
  6004. axisLeft = axis.left,
  6005. axisTop = axis.top,
  6006. x1,
  6007. y1,
  6008. x2,
  6009. y2,
  6010. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  6011. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  6012. skip,
  6013. transB = axis.transB;
  6014. translatedValue = pick(translatedValue, axis.translate(value, null, null, old));
  6015. x1 = x2 = mathRound(translatedValue + transB);
  6016. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  6017. if (isNaN(translatedValue)) { // no min or max
  6018. skip = true;
  6019. } else if (axis.horiz) {
  6020. y1 = axisTop;
  6021. y2 = cHeight - axis.bottom;
  6022. if (x1 < axisLeft || x1 > axisLeft + axis.width) {
  6023. skip = true;
  6024. }
  6025. } else {
  6026. x1 = axisLeft;
  6027. x2 = cWidth - axis.right;
  6028. if (y1 < axisTop || y1 > axisTop + axis.height) {
  6029. skip = true;
  6030. }
  6031. }
  6032. return skip && !force ?
  6033. null :
  6034. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 1);
  6035. },
  6036. /**
  6037. * Set the tick positions of a linear axis to round values like whole tens or every five.
  6038. */
  6039. getLinearTickPositions: function (tickInterval, min, max) {
  6040. var pos,
  6041. lastPos,
  6042. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  6043. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  6044. tickPositions = [];
  6045. // Populate the intermediate values
  6046. pos = roundedMin;
  6047. while (pos <= roundedMax) {
  6048. // Place the tick on the rounded value
  6049. tickPositions.push(pos);
  6050. // Always add the raw tickInterval, not the corrected one.
  6051. pos = correctFloat(pos + tickInterval);
  6052. // If the interval is not big enough in the current min - max range to actually increase
  6053. // the loop variable, we need to break out to prevent endless loop. Issue #619
  6054. if (pos === lastPos) {
  6055. break;
  6056. }
  6057. // Record the last value
  6058. lastPos = pos;
  6059. }
  6060. return tickPositions;
  6061. },
  6062. /**
  6063. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  6064. * as for major ticks.
  6065. */
  6066. getMinorTickPositions: function () {
  6067. var axis = this,
  6068. options = axis.options,
  6069. tickPositions = axis.tickPositions,
  6070. minorTickInterval = axis.minorTickInterval,
  6071. minorTickPositions = [],
  6072. pos,
  6073. i,
  6074. len;
  6075. if (axis.isLog) {
  6076. len = tickPositions.length;
  6077. for (i = 1; i < len; i++) {
  6078. minorTickPositions = minorTickPositions.concat(
  6079. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6080. );
  6081. }
  6082. } else if (axis.isDatetimeAxis && options.minorTickInterval === 'auto') { // #1314
  6083. minorTickPositions = minorTickPositions.concat(
  6084. axis.getTimeTicks(
  6085. axis.normalizeTimeTickInterval(minorTickInterval),
  6086. axis.min,
  6087. axis.max,
  6088. options.startOfWeek
  6089. )
  6090. );
  6091. if (minorTickPositions[0] < axis.min) {
  6092. minorTickPositions.shift();
  6093. }
  6094. } else {
  6095. for (pos = axis.min + (tickPositions[0] - axis.min) % minorTickInterval; pos <= axis.max; pos += minorTickInterval) {
  6096. minorTickPositions.push(pos);
  6097. }
  6098. }
  6099. return minorTickPositions;
  6100. },
  6101. /**
  6102. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6103. * not yet processed, so we don't have information on data cropping and grouping, or
  6104. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6105. * we have finally established min and max.
  6106. */
  6107. adjustForMinRange: function () {
  6108. var axis = this,
  6109. options = axis.options,
  6110. min = axis.min,
  6111. max = axis.max,
  6112. zoomOffset,
  6113. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6114. closestDataRange,
  6115. i,
  6116. distance,
  6117. xData,
  6118. loopLength,
  6119. minArgs,
  6120. maxArgs;
  6121. // Set the automatic minimum range based on the closest point distance
  6122. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6123. if (defined(options.min) || defined(options.max)) {
  6124. axis.minRange = null; // don't do this again
  6125. } else {
  6126. // Find the closest distance between raw data points, as opposed to
  6127. // closestPointRange that applies to processed points (cropped and grouped)
  6128. each(axis.series, function (series) {
  6129. xData = series.xData;
  6130. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6131. for (i = loopLength; i > 0; i--) {
  6132. distance = xData[i] - xData[i - 1];
  6133. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6134. closestDataRange = distance;
  6135. }
  6136. }
  6137. });
  6138. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6139. }
  6140. }
  6141. // if minRange is exceeded, adjust
  6142. if (max - min < axis.minRange) {
  6143. var minRange = axis.minRange;
  6144. zoomOffset = (minRange - max + min) / 2;
  6145. // if min and max options have been set, don't go beyond it
  6146. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6147. if (spaceAvailable) { // if space is available, stay within the data range
  6148. minArgs[2] = axis.dataMin;
  6149. }
  6150. min = arrayMax(minArgs);
  6151. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6152. if (spaceAvailable) { // if space is availabe, stay within the data range
  6153. maxArgs[2] = axis.dataMax;
  6154. }
  6155. max = arrayMin(maxArgs);
  6156. // now if the max is adjusted, adjust the min back
  6157. if (max - min < minRange) {
  6158. minArgs[0] = max - minRange;
  6159. minArgs[1] = pick(options.min, max - minRange);
  6160. min = arrayMax(minArgs);
  6161. }
  6162. }
  6163. // Record modified extremes
  6164. axis.min = min;
  6165. axis.max = max;
  6166. },
  6167. /**
  6168. * Update translation information
  6169. */
  6170. setAxisTranslation: function (saveOld) {
  6171. var axis = this,
  6172. range = axis.max - axis.min,
  6173. pointRange = axis.axisPointRange || 0,
  6174. closestPointRange,
  6175. minPointOffset = 0,
  6176. pointRangePadding = 0,
  6177. linkedParent = axis.linkedParent,
  6178. ordinalCorrection,
  6179. hasCategories = !!axis.categories,
  6180. transA = axis.transA;
  6181. // Adjust translation for padding. Y axis with categories need to go through the same (#1784).
  6182. if (axis.isXAxis || hasCategories || pointRange) {
  6183. if (linkedParent) {
  6184. minPointOffset = linkedParent.minPointOffset;
  6185. pointRangePadding = linkedParent.pointRangePadding;
  6186. } else {
  6187. each(axis.series, function (series) {
  6188. var seriesPointRange = mathMax(axis.isXAxis ? series.pointRange : (axis.axisPointRange || 0), +hasCategories),
  6189. pointPlacement = series.options.pointPlacement,
  6190. seriesClosestPointRange = series.closestPointRange;
  6191. if (seriesPointRange > range) { // #1446
  6192. seriesPointRange = 0;
  6193. }
  6194. pointRange = mathMax(pointRange, seriesPointRange);
  6195. // minPointOffset is the value padding to the left of the axis in order to make
  6196. // room for points with a pointRange, typically columns. When the pointPlacement option
  6197. // is 'between' or 'on', this padding does not apply.
  6198. minPointOffset = mathMax(
  6199. minPointOffset,
  6200. isString(pointPlacement) ? 0 : seriesPointRange / 2
  6201. );
  6202. // Determine the total padding needed to the length of the axis to make room for the
  6203. // pointRange. If the series' pointPlacement is 'on', no padding is added.
  6204. pointRangePadding = mathMax(
  6205. pointRangePadding,
  6206. pointPlacement === 'on' ? 0 : seriesPointRange
  6207. );
  6208. // Set the closestPointRange
  6209. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  6210. closestPointRange = defined(closestPointRange) ?
  6211. mathMin(closestPointRange, seriesClosestPointRange) :
  6212. seriesClosestPointRange;
  6213. }
  6214. });
  6215. }
  6216. // Record minPointOffset and pointRangePadding
  6217. ordinalCorrection = axis.ordinalSlope && closestPointRange ? axis.ordinalSlope / closestPointRange : 1; // #988, #1853
  6218. axis.minPointOffset = minPointOffset = minPointOffset * ordinalCorrection;
  6219. axis.pointRangePadding = pointRangePadding = pointRangePadding * ordinalCorrection;
  6220. // pointRange means the width reserved for each point, like in a column chart
  6221. axis.pointRange = mathMin(pointRange, range);
  6222. // closestPointRange means the closest distance between points. In columns
  6223. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6224. // is some other value
  6225. axis.closestPointRange = closestPointRange;
  6226. }
  6227. // Secondary values
  6228. if (saveOld) {
  6229. axis.oldTransA = transA;
  6230. }
  6231. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRangePadding) || 1);
  6232. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6233. axis.minPixelPadding = transA * minPointOffset;
  6234. },
  6235. /**
  6236. * Set the tick positions to round values and optionally extend the extremes
  6237. * to the nearest tick
  6238. */
  6239. setTickPositions: function (secondPass) {
  6240. var axis = this,
  6241. chart = axis.chart,
  6242. options = axis.options,
  6243. isLog = axis.isLog,
  6244. isDatetimeAxis = axis.isDatetimeAxis,
  6245. isXAxis = axis.isXAxis,
  6246. isLinked = axis.isLinked,
  6247. tickPositioner = axis.options.tickPositioner,
  6248. maxPadding = options.maxPadding,
  6249. minPadding = options.minPadding,
  6250. length,
  6251. linkedParentExtremes,
  6252. tickIntervalOption = options.tickInterval,
  6253. minTickIntervalOption = options.minTickInterval,
  6254. tickPixelIntervalOption = options.tickPixelInterval,
  6255. tickPositions,
  6256. keepTwoTicksOnly,
  6257. categories = axis.categories;
  6258. // linked axis gets the extremes from the parent axis
  6259. if (isLinked) {
  6260. axis.linkedParent = chart[axis.coll][options.linkedTo];
  6261. linkedParentExtremes = axis.linkedParent.getExtremes();
  6262. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6263. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6264. if (options.type !== axis.linkedParent.options.type) {
  6265. error(11, 1); // Can't link axes of different type
  6266. }
  6267. } else { // initial min and max from the extreme data values
  6268. axis.min = pick(axis.userMin, options.min, axis.dataMin);
  6269. axis.max = pick(axis.userMax, options.max, axis.dataMax);
  6270. }
  6271. if (isLog) {
  6272. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6273. error(10, 1); // Can't plot negative values on log axis
  6274. }
  6275. axis.min = correctFloat(log2lin(axis.min)); // correctFloat cures #934
  6276. axis.max = correctFloat(log2lin(axis.max));
  6277. }
  6278. // handle zoomed range
  6279. if (axis.range && defined(axis.max)) {
  6280. axis.userMin = axis.min = mathMax(axis.min, axis.max - axis.range); // #618
  6281. axis.userMax = axis.max;
  6282. axis.range = null; // don't use it when running setExtremes
  6283. }
  6284. // Hook for adjusting this.min and this.max. Used by bubble series.
  6285. if (axis.beforePadding) {
  6286. axis.beforePadding();
  6287. }
  6288. // adjust min and max for the minimum range
  6289. axis.adjustForMinRange();
  6290. // Pad the values to get clear of the chart's edges. To avoid tickInterval taking the padding
  6291. // into account, we do this after computing tick interval (#1337).
  6292. if (!categories && !axis.axisPointRange && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6293. length = axis.max - axis.min;
  6294. if (length) {
  6295. if (!defined(options.min) && !defined(axis.userMin) && minPadding && (axis.dataMin < 0 || !axis.ignoreMinPadding)) {
  6296. axis.min -= length * minPadding;
  6297. }
  6298. if (!defined(options.max) && !defined(axis.userMax) && maxPadding && (axis.dataMax > 0 || !axis.ignoreMaxPadding)) {
  6299. axis.max += length * maxPadding;
  6300. }
  6301. }
  6302. }
  6303. // get tickInterval
  6304. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6305. axis.tickInterval = 1;
  6306. } else if (isLinked && !tickIntervalOption &&
  6307. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6308. axis.tickInterval = axis.linkedParent.tickInterval;
  6309. } else {
  6310. axis.tickInterval = pick(
  6311. tickIntervalOption,
  6312. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6313. 1 :
  6314. // don't let it be more than the data range
  6315. (axis.max - axis.min) * tickPixelIntervalOption / mathMax(axis.len, tickPixelIntervalOption)
  6316. );
  6317. // For squished axes, set only two ticks
  6318. if (!defined(tickIntervalOption) && axis.len < tickPixelIntervalOption && !this.isRadial &&
  6319. !this.isLog && !categories && options.startOnTick && options.endOnTick) {
  6320. keepTwoTicksOnly = true;
  6321. axis.tickInterval /= 4; // tick extremes closer to the real values
  6322. }
  6323. }
  6324. // Now we're finished detecting min and max, crop and group series data. This
  6325. // is in turn needed in order to find tick positions in ordinal axes.
  6326. if (isXAxis && !secondPass) {
  6327. each(axis.series, function (series) {
  6328. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6329. });
  6330. }
  6331. // set the translation factor used in translate function
  6332. axis.setAxisTranslation(true);
  6333. // hook for ordinal axes and radial axes
  6334. if (axis.beforeSetTickPositions) {
  6335. axis.beforeSetTickPositions();
  6336. }
  6337. // hook for extensions, used in Highstock ordinal axes
  6338. if (axis.postProcessTickInterval) {
  6339. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6340. }
  6341. // In column-like charts, don't cramp in more ticks than there are points (#1943)
  6342. if (axis.pointRange) {
  6343. axis.tickInterval = mathMax(axis.pointRange, axis.tickInterval);
  6344. }
  6345. // Before normalizing the tick interval, handle minimum tick interval. This applies only if tickInterval is not defined.
  6346. if (!tickIntervalOption && axis.tickInterval < minTickIntervalOption) {
  6347. axis.tickInterval = minTickIntervalOption;
  6348. }
  6349. // for linear axes, get magnitude and normalize the interval
  6350. if (!isDatetimeAxis && !isLog) { // linear
  6351. if (!tickIntervalOption) {
  6352. axis.tickInterval = normalizeTickInterval(axis.tickInterval, null, getMagnitude(axis.tickInterval), options);
  6353. }
  6354. }
  6355. // get minorTickInterval
  6356. axis.minorTickInterval = options.minorTickInterval === 'auto' && axis.tickInterval ?
  6357. axis.tickInterval / 5 : options.minorTickInterval;
  6358. // find the tick positions
  6359. axis.tickPositions = tickPositions = options.tickPositions ?
  6360. [].concat(options.tickPositions) : // Work on a copy (#1565)
  6361. (tickPositioner && tickPositioner.apply(axis, [axis.min, axis.max]));
  6362. if (!tickPositions) {
  6363. // Too many ticks
  6364. if (!axis.ordinalPositions && (axis.max - axis.min) / axis.tickInterval > mathMax(2 * axis.len, 200)) {
  6365. error(19, true);
  6366. }
  6367. if (isDatetimeAxis) {
  6368. tickPositions = axis.getTimeTicks(
  6369. axis.normalizeTimeTickInterval(axis.tickInterval, options.units),
  6370. axis.min,
  6371. axis.max,
  6372. options.startOfWeek,
  6373. axis.ordinalPositions,
  6374. axis.closestPointRange,
  6375. true
  6376. );
  6377. } else if (isLog) {
  6378. tickPositions = axis.getLogTickPositions(axis.tickInterval, axis.min, axis.max);
  6379. } else {
  6380. tickPositions = axis.getLinearTickPositions(axis.tickInterval, axis.min, axis.max);
  6381. }
  6382. if (keepTwoTicksOnly) {
  6383. tickPositions.splice(1, tickPositions.length - 2);
  6384. }
  6385. axis.tickPositions = tickPositions;
  6386. }
  6387. if (!isLinked) {
  6388. // reset min/max or remove extremes based on start/end on tick
  6389. var roundedMin = tickPositions[0],
  6390. roundedMax = tickPositions[tickPositions.length - 1],
  6391. minPointOffset = axis.minPointOffset || 0,
  6392. singlePad;
  6393. if (options.startOnTick) {
  6394. axis.min = roundedMin;
  6395. } else if (axis.min - minPointOffset > roundedMin) {
  6396. tickPositions.shift();
  6397. }
  6398. if (options.endOnTick) {
  6399. axis.max = roundedMax;
  6400. } else if (axis.max + minPointOffset < roundedMax) {
  6401. tickPositions.pop();
  6402. }
  6403. // When there is only one point, or all points have the same value on this axis, then min
  6404. // and max are equal and tickPositions.length is 1. In this case, add some padding
  6405. // in order to center the point, but leave it with one tick. #1337.
  6406. if (tickPositions.length === 1) {
  6407. singlePad = mathAbs(axis.max || 1) * 0.001; // The lowest possible number to avoid extra padding on columns (#2619)
  6408. axis.min -= singlePad;
  6409. axis.max += singlePad;
  6410. }
  6411. }
  6412. },
  6413. /**
  6414. * Set the max ticks of either the x and y axis collection
  6415. */
  6416. setMaxTicks: function () {
  6417. var chart = this.chart,
  6418. maxTicks = chart.maxTicks || {},
  6419. tickPositions = this.tickPositions,
  6420. key = this._maxTicksKey = [this.coll, this.pos, this.len].join('-');
  6421. if (!this.isLinked && !this.isDatetimeAxis && tickPositions && tickPositions.length > (maxTicks[key] || 0) && this.options.alignTicks !== false) {
  6422. maxTicks[key] = tickPositions.length;
  6423. }
  6424. chart.maxTicks = maxTicks;
  6425. },
  6426. /**
  6427. * When using multiple axes, adjust the number of ticks to match the highest
  6428. * number of ticks in that group
  6429. */
  6430. adjustTickAmount: function () {
  6431. var axis = this,
  6432. chart = axis.chart,
  6433. key = axis._maxTicksKey,
  6434. tickPositions = axis.tickPositions,
  6435. maxTicks = chart.maxTicks;
  6436. if (maxTicks && maxTicks[key] && !axis.isDatetimeAxis && !axis.categories && !axis.isLinked &&
  6437. axis.options.alignTicks !== false && this.min !== UNDEFINED) {
  6438. var oldTickAmount = axis.tickAmount,
  6439. calculatedTickAmount = tickPositions.length,
  6440. tickAmount;
  6441. // set the axis-level tickAmount to use below
  6442. axis.tickAmount = tickAmount = maxTicks[key];
  6443. if (calculatedTickAmount < tickAmount) {
  6444. while (tickPositions.length < tickAmount) {
  6445. tickPositions.push(correctFloat(
  6446. tickPositions[tickPositions.length - 1] + axis.tickInterval
  6447. ));
  6448. }
  6449. axis.transA *= (calculatedTickAmount - 1) / (tickAmount - 1);
  6450. axis.max = tickPositions[tickPositions.length - 1];
  6451. }
  6452. if (defined(oldTickAmount) && tickAmount !== oldTickAmount) {
  6453. axis.isDirty = true;
  6454. }
  6455. }
  6456. },
  6457. /**
  6458. * Set the scale based on data min and max, user set min and max or options
  6459. *
  6460. */
  6461. setScale: function () {
  6462. var axis = this,
  6463. stacks = axis.stacks,
  6464. type,
  6465. i,
  6466. isDirtyData,
  6467. isDirtyAxisLength;
  6468. axis.oldMin = axis.min;
  6469. axis.oldMax = axis.max;
  6470. axis.oldAxisLength = axis.len;
  6471. // set the new axisLength
  6472. axis.setAxisSize();
  6473. //axisLength = horiz ? axisWidth : axisHeight;
  6474. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  6475. // is there new data?
  6476. each(axis.series, function (series) {
  6477. if (series.isDirtyData || series.isDirty ||
  6478. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  6479. isDirtyData = true;
  6480. }
  6481. });
  6482. // do we really need to go through all this?
  6483. if (isDirtyAxisLength || isDirtyData || axis.isLinked || axis.forceRedraw ||
  6484. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax) {
  6485. // reset stacks
  6486. if (!axis.isXAxis) {
  6487. for (type in stacks) {
  6488. for (i in stacks[type]) {
  6489. stacks[type][i].total = null;
  6490. stacks[type][i].cum = 0;
  6491. }
  6492. }
  6493. }
  6494. axis.forceRedraw = false;
  6495. // get data extremes if needed
  6496. axis.getSeriesExtremes();
  6497. // get fixed positions based on tickInterval
  6498. axis.setTickPositions();
  6499. // record old values to decide whether a rescale is necessary later on (#540)
  6500. axis.oldUserMin = axis.userMin;
  6501. axis.oldUserMax = axis.userMax;
  6502. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  6503. if (!axis.isDirty) {
  6504. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  6505. }
  6506. } else if (!axis.isXAxis) {
  6507. if (axis.oldStacks) {
  6508. stacks = axis.stacks = axis.oldStacks;
  6509. }
  6510. // reset stacks
  6511. for (type in stacks) {
  6512. for (i in stacks[type]) {
  6513. stacks[type][i].cum = stacks[type][i].total;
  6514. }
  6515. }
  6516. }
  6517. // Set the maximum tick amount
  6518. axis.setMaxTicks();
  6519. },
  6520. /**
  6521. * Set the extremes and optionally redraw
  6522. * @param {Number} newMin
  6523. * @param {Number} newMax
  6524. * @param {Boolean} redraw
  6525. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  6526. * configuration
  6527. * @param {Object} eventArguments
  6528. *
  6529. */
  6530. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  6531. var axis = this,
  6532. chart = axis.chart;
  6533. redraw = pick(redraw, true); // defaults to true
  6534. // Extend the arguments with min and max
  6535. eventArguments = extend(eventArguments, {
  6536. min: newMin,
  6537. max: newMax
  6538. });
  6539. // Fire the event
  6540. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  6541. axis.userMin = newMin;
  6542. axis.userMax = newMax;
  6543. axis.eventArgs = eventArguments;
  6544. // Mark for running afterSetExtremes
  6545. axis.isDirtyExtremes = true;
  6546. // redraw
  6547. if (redraw) {
  6548. chart.redraw(animation);
  6549. }
  6550. });
  6551. },
  6552. /**
  6553. * Overridable method for zooming chart. Pulled out in a separate method to allow overriding
  6554. * in stock charts.
  6555. */
  6556. zoom: function (newMin, newMax) {
  6557. var dataMin = this.dataMin,
  6558. dataMax = this.dataMax,
  6559. options = this.options;
  6560. // Prevent pinch zooming out of range. Check for defined is for #1946. #1734.
  6561. if (!this.allowZoomOutside) {
  6562. if (defined(dataMin) && newMin <= mathMin(dataMin, pick(options.min, dataMin))) {
  6563. newMin = UNDEFINED;
  6564. }
  6565. if (defined(dataMax) && newMax >= mathMax(dataMax, pick(options.max, dataMax))) {
  6566. newMax = UNDEFINED;
  6567. }
  6568. }
  6569. // In full view, displaying the reset zoom button is not required
  6570. this.displayBtn = newMin !== UNDEFINED || newMax !== UNDEFINED;
  6571. // Do it
  6572. this.setExtremes(
  6573. newMin,
  6574. newMax,
  6575. false,
  6576. UNDEFINED,
  6577. { trigger: 'zoom' }
  6578. );
  6579. return true;
  6580. },
  6581. /**
  6582. * Update the axis metrics
  6583. */
  6584. setAxisSize: function () {
  6585. var chart = this.chart,
  6586. options = this.options,
  6587. offsetLeft = options.offsetLeft || 0,
  6588. offsetRight = options.offsetRight || 0,
  6589. horiz = this.horiz,
  6590. width,
  6591. height,
  6592. top,
  6593. left;
  6594. // Expose basic values to use in Series object and navigator
  6595. this.left = left = pick(options.left, chart.plotLeft + offsetLeft);
  6596. this.top = top = pick(options.top, chart.plotTop);
  6597. this.width = width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight);
  6598. this.height = height = pick(options.height, chart.plotHeight);
  6599. this.bottom = chart.chartHeight - height - top;
  6600. this.right = chart.chartWidth - width - left;
  6601. // Direction agnostic properties
  6602. this.len = mathMax(horiz ? width : height, 0); // mathMax fixes #905
  6603. this.pos = horiz ? left : top; // distance from SVG origin
  6604. },
  6605. /**
  6606. * Get the actual axis extremes
  6607. */
  6608. getExtremes: function () {
  6609. var axis = this,
  6610. isLog = axis.isLog;
  6611. return {
  6612. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  6613. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  6614. dataMin: axis.dataMin,
  6615. dataMax: axis.dataMax,
  6616. userMin: axis.userMin,
  6617. userMax: axis.userMax
  6618. };
  6619. },
  6620. /**
  6621. * Get the zero plane either based on zero or on the min or max value.
  6622. * Used in bar and area plots
  6623. */
  6624. getThreshold: function (threshold) {
  6625. var axis = this,
  6626. isLog = axis.isLog;
  6627. var realMin = isLog ? lin2log(axis.min) : axis.min,
  6628. realMax = isLog ? lin2log(axis.max) : axis.max;
  6629. if (realMin > threshold || threshold === null) {
  6630. threshold = realMin;
  6631. } else if (realMax < threshold) {
  6632. threshold = realMax;
  6633. }
  6634. return axis.translate(threshold, 0, 1, 0, 1);
  6635. },
  6636. /**
  6637. * Compute auto alignment for the axis label based on which side the axis is on
  6638. * and the given rotation for the label
  6639. */
  6640. autoLabelAlign: function (rotation) {
  6641. var ret,
  6642. angle = (pick(rotation, 0) - (this.side * 90) + 720) % 360;
  6643. if (angle > 15 && angle < 165) {
  6644. ret = 'right';
  6645. } else if (angle > 195 && angle < 345) {
  6646. ret = 'left';
  6647. } else {
  6648. ret = 'center';
  6649. }
  6650. return ret;
  6651. },
  6652. /**
  6653. * Render the tick labels to a preliminary position to get their sizes
  6654. */
  6655. getOffset: function () {
  6656. var axis = this,
  6657. chart = axis.chart,
  6658. renderer = chart.renderer,
  6659. options = axis.options,
  6660. tickPositions = axis.tickPositions,
  6661. ticks = axis.ticks,
  6662. horiz = axis.horiz,
  6663. side = axis.side,
  6664. invertedSide = chart.inverted ? [1, 0, 3, 2][side] : side,
  6665. hasData,
  6666. showAxis,
  6667. titleOffset = 0,
  6668. titleOffsetOption,
  6669. titleMargin = 0,
  6670. axisTitleOptions = options.title,
  6671. labelOptions = options.labels,
  6672. labelOffset = 0, // reset
  6673. axisOffset = chart.axisOffset,
  6674. clipOffset = chart.clipOffset,
  6675. directionFactor = [-1, 1, 1, -1][side],
  6676. n,
  6677. i,
  6678. autoStaggerLines = 1,
  6679. maxStaggerLines = pick(labelOptions.maxStaggerLines, 5),
  6680. sortedPositions,
  6681. lastRight,
  6682. overlap,
  6683. pos,
  6684. bBox,
  6685. x,
  6686. w,
  6687. lineNo;
  6688. // For reuse in Axis.render
  6689. axis.hasData = hasData = (axis.hasVisibleSeries || (defined(axis.min) && defined(axis.max) && !!tickPositions));
  6690. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  6691. // Set/reset staggerLines
  6692. axis.staggerLines = axis.horiz && labelOptions.staggerLines;
  6693. // Create the axisGroup and gridGroup elements on first iteration
  6694. if (!axis.axisGroup) {
  6695. axis.gridGroup = renderer.g('grid')
  6696. .attr({ zIndex: options.gridZIndex || 1 })
  6697. .add();
  6698. axis.axisGroup = renderer.g('axis')
  6699. .attr({ zIndex: options.zIndex || 2 })
  6700. .add();
  6701. axis.labelGroup = renderer.g('axis-labels')
  6702. .attr({ zIndex: labelOptions.zIndex || 7 })
  6703. .addClass(PREFIX + axis.coll.toLowerCase() + '-labels')
  6704. .add();
  6705. }
  6706. if (hasData || axis.isLinked) {
  6707. // Set the explicit or automatic label alignment
  6708. axis.labelAlign = pick(labelOptions.align || axis.autoLabelAlign(labelOptions.rotation));
  6709. // Generate ticks
  6710. each(tickPositions, function (pos) {
  6711. if (!ticks[pos]) {
  6712. ticks[pos] = new Tick(axis, pos);
  6713. } else {
  6714. ticks[pos].addLabel(); // update labels depending on tick interval
  6715. }
  6716. });
  6717. // Handle automatic stagger lines
  6718. if (axis.horiz && !axis.staggerLines && maxStaggerLines && !labelOptions.rotation) {
  6719. sortedPositions = axis.reversed ? [].concat(tickPositions).reverse() : tickPositions;
  6720. while (autoStaggerLines < maxStaggerLines) {
  6721. lastRight = [];
  6722. overlap = false;
  6723. for (i = 0; i < sortedPositions.length; i++) {
  6724. pos = sortedPositions[i];
  6725. bBox = ticks[pos].label && ticks[pos].label.getBBox();
  6726. w = bBox ? bBox.width : 0;
  6727. lineNo = i % autoStaggerLines;
  6728. if (w) {
  6729. x = axis.translate(pos); // don't handle log
  6730. if (lastRight[lineNo] !== UNDEFINED && x < lastRight[lineNo]) {
  6731. overlap = true;
  6732. }
  6733. lastRight[lineNo] = x + w;
  6734. }
  6735. }
  6736. if (overlap) {
  6737. autoStaggerLines++;
  6738. } else {
  6739. break;
  6740. }
  6741. }
  6742. if (autoStaggerLines > 1) {
  6743. axis.staggerLines = autoStaggerLines;
  6744. }
  6745. }
  6746. each(tickPositions, function (pos) {
  6747. // left side must be align: right and right side must have align: left for labels
  6748. if (side === 0 || side === 2 || { 1: 'left', 3: 'right' }[side] === axis.labelAlign) {
  6749. // get the highest offset
  6750. labelOffset = mathMax(
  6751. ticks[pos].getLabelSize(),
  6752. labelOffset
  6753. );
  6754. }
  6755. });
  6756. if (axis.staggerLines) {
  6757. labelOffset *= axis.staggerLines;
  6758. axis.labelOffset = labelOffset;
  6759. }
  6760. } else { // doesn't have data
  6761. for (n in ticks) {
  6762. ticks[n].destroy();
  6763. delete ticks[n];
  6764. }
  6765. }
  6766. if (axisTitleOptions && axisTitleOptions.text && axisTitleOptions.enabled !== false) {
  6767. if (!axis.axisTitle) {
  6768. axis.axisTitle = renderer.text(
  6769. axisTitleOptions.text,
  6770. 0,
  6771. 0,
  6772. axisTitleOptions.useHTML
  6773. )
  6774. .attr({
  6775. zIndex: 7,
  6776. rotation: axisTitleOptions.rotation || 0,
  6777. align:
  6778. axisTitleOptions.textAlign ||
  6779. { low: 'left', middle: 'center', high: 'right' }[axisTitleOptions.align]
  6780. })
  6781. .addClass(PREFIX + this.coll.toLowerCase() + '-title')
  6782. .css(axisTitleOptions.style)
  6783. .add(axis.axisGroup);
  6784. axis.axisTitle.isNew = true;
  6785. }
  6786. if (showAxis) {
  6787. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  6788. titleMargin = pick(axisTitleOptions.margin, horiz ? 5 : 10);
  6789. titleOffsetOption = axisTitleOptions.offset;
  6790. }
  6791. // hide or show the title depending on whether showEmpty is set
  6792. axis.axisTitle[showAxis ? 'show' : 'hide']();
  6793. }
  6794. // handle automatic or user set offset
  6795. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  6796. axis.axisTitleMargin =
  6797. pick(titleOffsetOption,
  6798. labelOffset + titleMargin +
  6799. (side !== 2 && labelOffset && directionFactor * options.labels[horiz ? 'y' : 'x'])
  6800. );
  6801. axisOffset[side] = mathMax(
  6802. axisOffset[side],
  6803. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset
  6804. );
  6805. clipOffset[invertedSide] = mathMax(clipOffset[invertedSide], mathFloor(options.lineWidth / 2) * 2);
  6806. },
  6807. /**
  6808. * Get the path for the axis line
  6809. */
  6810. getLinePath: function (lineWidth) {
  6811. var chart = this.chart,
  6812. opposite = this.opposite,
  6813. offset = this.offset,
  6814. horiz = this.horiz,
  6815. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  6816. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  6817. if (opposite) {
  6818. lineWidth *= -1; // crispify the other way - #1480, #1687
  6819. }
  6820. return chart.renderer.crispLine([
  6821. M,
  6822. horiz ?
  6823. this.left :
  6824. lineLeft,
  6825. horiz ?
  6826. lineTop :
  6827. this.top,
  6828. L,
  6829. horiz ?
  6830. chart.chartWidth - this.right :
  6831. lineLeft,
  6832. horiz ?
  6833. lineTop :
  6834. chart.chartHeight - this.bottom
  6835. ], lineWidth);
  6836. },
  6837. /**
  6838. * Position the title
  6839. */
  6840. getTitlePosition: function () {
  6841. // compute anchor points for each of the title align options
  6842. var horiz = this.horiz,
  6843. axisLeft = this.left,
  6844. axisTop = this.top,
  6845. axisLength = this.len,
  6846. axisTitleOptions = this.options.title,
  6847. margin = horiz ? axisLeft : axisTop,
  6848. opposite = this.opposite,
  6849. offset = this.offset,
  6850. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  6851. // the position in the length direction of the axis
  6852. alongAxis = {
  6853. low: margin + (horiz ? 0 : axisLength),
  6854. middle: margin + axisLength / 2,
  6855. high: margin + (horiz ? axisLength : 0)
  6856. }[axisTitleOptions.align],
  6857. // the position in the perpendicular direction of the axis
  6858. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  6859. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  6860. (opposite ? -1 : 1) * // so does opposite axes
  6861. this.axisTitleMargin +
  6862. (this.side === 2 ? fontSize : 0);
  6863. return {
  6864. x: horiz ?
  6865. alongAxis :
  6866. offAxis + (opposite ? this.width : 0) + offset +
  6867. (axisTitleOptions.x || 0), // x
  6868. y: horiz ?
  6869. offAxis - (opposite ? this.height : 0) + offset :
  6870. alongAxis + (axisTitleOptions.y || 0) // y
  6871. };
  6872. },
  6873. /**
  6874. * Render the axis
  6875. */
  6876. render: function () {
  6877. var axis = this,
  6878. horiz = axis.horiz,
  6879. reversed = axis.reversed,
  6880. chart = axis.chart,
  6881. renderer = chart.renderer,
  6882. options = axis.options,
  6883. isLog = axis.isLog,
  6884. isLinked = axis.isLinked,
  6885. tickPositions = axis.tickPositions,
  6886. sortedPositions,
  6887. axisTitle = axis.axisTitle,
  6888. ticks = axis.ticks,
  6889. minorTicks = axis.minorTicks,
  6890. alternateBands = axis.alternateBands,
  6891. stackLabelOptions = options.stackLabels,
  6892. alternateGridColor = options.alternateGridColor,
  6893. tickmarkOffset = axis.tickmarkOffset,
  6894. lineWidth = options.lineWidth,
  6895. linePath,
  6896. hasRendered = chart.hasRendered,
  6897. slideInTicks = hasRendered && defined(axis.oldMin) && !isNaN(axis.oldMin),
  6898. hasData = axis.hasData,
  6899. showAxis = axis.showAxis,
  6900. from,
  6901. overflow = options.labels.overflow,
  6902. justifyLabels = axis.justifyLabels = horiz && overflow !== false,
  6903. to;
  6904. // Reset
  6905. axis.labelEdge.length = 0;
  6906. axis.justifyToPlot = overflow === 'justify';
  6907. // Mark all elements inActive before we go over and mark the active ones
  6908. each([ticks, minorTicks, alternateBands], function (coll) {
  6909. var pos;
  6910. for (pos in coll) {
  6911. coll[pos].isActive = false;
  6912. }
  6913. });
  6914. // If the series has data draw the ticks. Else only the line and title
  6915. if (hasData || isLinked) {
  6916. // minor ticks
  6917. if (axis.minorTickInterval && !axis.categories) {
  6918. each(axis.getMinorTickPositions(), function (pos) {
  6919. if (!minorTicks[pos]) {
  6920. minorTicks[pos] = new Tick(axis, pos, 'minor');
  6921. }
  6922. // render new ticks in old position
  6923. if (slideInTicks && minorTicks[pos].isNew) {
  6924. minorTicks[pos].render(null, true);
  6925. }
  6926. minorTicks[pos].render(null, false, 1);
  6927. });
  6928. }
  6929. // Major ticks. Pull out the first item and render it last so that
  6930. // we can get the position of the neighbour label. #808.
  6931. if (tickPositions.length) { // #1300
  6932. sortedPositions = tickPositions.slice();
  6933. if ((horiz && reversed) || (!horiz && !reversed)) {
  6934. sortedPositions.reverse();
  6935. }
  6936. if (justifyLabels) {
  6937. sortedPositions = sortedPositions.slice(1).concat([sortedPositions[0]]);
  6938. }
  6939. each(sortedPositions, function (pos, i) {
  6940. // Reorganize the indices
  6941. if (justifyLabels) {
  6942. i = (i === sortedPositions.length - 1) ? 0 : i + 1;
  6943. }
  6944. // linked axes need an extra check to find out if
  6945. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  6946. if (!ticks[pos]) {
  6947. ticks[pos] = new Tick(axis, pos);
  6948. }
  6949. // render new ticks in old position
  6950. if (slideInTicks && ticks[pos].isNew) {
  6951. ticks[pos].render(i, true, 0.1);
  6952. }
  6953. ticks[pos].render(i, false, 1);
  6954. }
  6955. });
  6956. // In a categorized axis, the tick marks are displayed between labels. So
  6957. // we need to add a tick mark and grid line at the left edge of the X axis.
  6958. if (tickmarkOffset && axis.min === 0) {
  6959. if (!ticks[-1]) {
  6960. ticks[-1] = new Tick(axis, -1, null, true);
  6961. }
  6962. ticks[-1].render(-1);
  6963. }
  6964. }
  6965. // alternate grid color
  6966. if (alternateGridColor) {
  6967. each(tickPositions, function (pos, i) {
  6968. if (i % 2 === 0 && pos < axis.max) {
  6969. if (!alternateBands[pos]) {
  6970. alternateBands[pos] = new Highcharts.PlotLineOrBand(axis);
  6971. }
  6972. from = pos + tickmarkOffset; // #949
  6973. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] + tickmarkOffset : axis.max;
  6974. alternateBands[pos].options = {
  6975. from: isLog ? lin2log(from) : from,
  6976. to: isLog ? lin2log(to) : to,
  6977. color: alternateGridColor
  6978. };
  6979. alternateBands[pos].render();
  6980. alternateBands[pos].isActive = true;
  6981. }
  6982. });
  6983. }
  6984. // custom plot lines and bands
  6985. if (!axis._addedPlotLB) { // only first time
  6986. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  6987. axis.addPlotBandOrLine(plotLineOptions);
  6988. });
  6989. axis._addedPlotLB = true;
  6990. }
  6991. } // end if hasData
  6992. // Remove inactive ticks
  6993. each([ticks, minorTicks, alternateBands], function (coll) {
  6994. var pos,
  6995. i,
  6996. forDestruction = [],
  6997. delay = globalAnimation ? globalAnimation.duration || 500 : 0,
  6998. destroyInactiveItems = function () {
  6999. i = forDestruction.length;
  7000. while (i--) {
  7001. // When resizing rapidly, the same items may be destroyed in different timeouts,
  7002. // or the may be reactivated
  7003. if (coll[forDestruction[i]] && !coll[forDestruction[i]].isActive) {
  7004. coll[forDestruction[i]].destroy();
  7005. delete coll[forDestruction[i]];
  7006. }
  7007. }
  7008. };
  7009. for (pos in coll) {
  7010. if (!coll[pos].isActive) {
  7011. // Render to zero opacity
  7012. coll[pos].render(pos, false, 0);
  7013. coll[pos].isActive = false;
  7014. forDestruction.push(pos);
  7015. }
  7016. }
  7017. // When the objects are finished fading out, destroy them
  7018. if (coll === alternateBands || !chart.hasRendered || !delay) {
  7019. destroyInactiveItems();
  7020. } else if (delay) {
  7021. setTimeout(destroyInactiveItems, delay);
  7022. }
  7023. });
  7024. // Static items. As the axis group is cleared on subsequent calls
  7025. // to render, these items are added outside the group.
  7026. // axis line
  7027. if (lineWidth) {
  7028. linePath = axis.getLinePath(lineWidth);
  7029. if (!axis.axisLine) {
  7030. axis.axisLine = renderer.path(linePath)
  7031. .attr({
  7032. stroke: options.lineColor,
  7033. 'stroke-width': lineWidth,
  7034. zIndex: 7
  7035. })
  7036. .add(axis.axisGroup);
  7037. } else {
  7038. axis.axisLine.animate({ d: linePath });
  7039. }
  7040. // show or hide the line depending on options.showEmpty
  7041. axis.axisLine[showAxis ? 'show' : 'hide']();
  7042. }
  7043. if (axisTitle && showAxis) {
  7044. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  7045. axis.getTitlePosition()
  7046. );
  7047. axisTitle.isNew = false;
  7048. }
  7049. // Stacked totals:
  7050. if (stackLabelOptions && stackLabelOptions.enabled) {
  7051. axis.renderStackTotals();
  7052. }
  7053. // End stacked totals
  7054. axis.isDirty = false;
  7055. },
  7056. /**
  7057. * Redraw the axis to reflect changes in the data or axis extremes
  7058. */
  7059. redraw: function () {
  7060. var axis = this,
  7061. chart = axis.chart,
  7062. pointer = chart.pointer;
  7063. // hide tooltip and hover states
  7064. if (pointer) {
  7065. pointer.reset(true);
  7066. }
  7067. // render the axis
  7068. axis.render();
  7069. // move plot lines and bands
  7070. each(axis.plotLinesAndBands, function (plotLine) {
  7071. plotLine.render();
  7072. });
  7073. // mark associated series as dirty and ready for redraw
  7074. each(axis.series, function (series) {
  7075. series.isDirty = true;
  7076. });
  7077. },
  7078. /**
  7079. * Destroys an Axis instance.
  7080. */
  7081. destroy: function (keepEvents) {
  7082. var axis = this,
  7083. stacks = axis.stacks,
  7084. stackKey,
  7085. plotLinesAndBands = axis.plotLinesAndBands,
  7086. i;
  7087. // Remove the events
  7088. if (!keepEvents) {
  7089. removeEvent(axis);
  7090. }
  7091. // Destroy each stack total
  7092. for (stackKey in stacks) {
  7093. destroyObjectProperties(stacks[stackKey]);
  7094. stacks[stackKey] = null;
  7095. }
  7096. // Destroy collections
  7097. each([axis.ticks, axis.minorTicks, axis.alternateBands], function (coll) {
  7098. destroyObjectProperties(coll);
  7099. });
  7100. i = plotLinesAndBands.length;
  7101. while (i--) { // #1975
  7102. plotLinesAndBands[i].destroy();
  7103. }
  7104. // Destroy local variables
  7105. each(['stackTotalGroup', 'axisLine', 'axisTitle', 'axisGroup', 'cross', 'gridGroup', 'labelGroup'], function (prop) {
  7106. if (axis[prop]) {
  7107. axis[prop] = axis[prop].destroy();
  7108. }
  7109. });
  7110. // Destroy crosshair
  7111. if (this.cross) {
  7112. this.cross.destroy();
  7113. }
  7114. },
  7115. /**
  7116. * Draw the crosshair
  7117. */
  7118. drawCrosshair: function (e, point) {
  7119. if (!this.crosshair) { return; }// Do not draw crosshairs if you don't have too.
  7120. if ((defined(point) || !pick(this.crosshair.snap, true)) === false) {
  7121. this.hideCrosshair();
  7122. return;
  7123. }
  7124. var path,
  7125. options = this.crosshair,
  7126. animation = options.animation,
  7127. pos;
  7128. // Get the path
  7129. if (!pick(options.snap, true)) {
  7130. pos = (this.horiz ? e.chartX - this.pos : this.len - e.chartY + this.pos);
  7131. } else if (defined(point)) {
  7132. /*jslint eqeq: true*/
  7133. pos = (this.chart.inverted != this.horiz) ? point.plotX : this.len - point.plotY;
  7134. /*jslint eqeq: false*/
  7135. }
  7136. if (this.isRadial) {
  7137. path = this.getPlotLinePath(this.isXAxis ? point.x : pick(point.stackY, point.y));
  7138. } else {
  7139. path = this.getPlotLinePath(null, null, null, null, pos);
  7140. }
  7141. if (path === null) {
  7142. this.hideCrosshair();
  7143. return;
  7144. }
  7145. // Draw the cross
  7146. if (this.cross) {
  7147. this.cross
  7148. .attr({ visibility: VISIBLE })[animation ? 'animate' : 'attr']({ d: path }, animation);
  7149. } else {
  7150. var attribs = {
  7151. 'stroke-width': options.width || 1,
  7152. stroke: options.color || '#C0C0C0',
  7153. zIndex: options.zIndex || 2
  7154. };
  7155. if (options.dashStyle) {
  7156. attribs.dashstyle = options.dashStyle;
  7157. }
  7158. this.cross = this.chart.renderer.path(path).attr(attribs).add();
  7159. }
  7160. },
  7161. /**
  7162. * Hide the crosshair.
  7163. */
  7164. hideCrosshair: function () {
  7165. if (this.cross) {
  7166. this.cross.hide();
  7167. }
  7168. }
  7169. }; // end Axis
  7170. extend(Axis.prototype, AxisPlotLineOrBandExtension);
  7171. /**
  7172. * Set the tick positions to a time unit that makes sense, for example
  7173. * on the first of each month or on every Monday. Return an array
  7174. * with the time positions. Used in datetime axes as well as for grouping
  7175. * data on a datetime axis.
  7176. *
  7177. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  7178. * @param {Number} min The minimum in axis values
  7179. * @param {Number} max The maximum in axis values
  7180. * @param {Number} startOfWeek
  7181. */
  7182. Axis.prototype.getTimeTicks = function (normalizedInterval, min, max, startOfWeek) {
  7183. var tickPositions = [],
  7184. i,
  7185. higherRanks = {},
  7186. useUTC = defaultOptions.global.useUTC,
  7187. minYear, // used in months and years as a basis for Date.UTC()
  7188. minDate = new Date(min - timezoneOffset),
  7189. interval = normalizedInterval.unitRange,
  7190. count = normalizedInterval.count;
  7191. if (defined(min)) { // #1300
  7192. if (interval >= timeUnits[SECOND]) { // second
  7193. minDate.setMilliseconds(0);
  7194. minDate.setSeconds(interval >= timeUnits[MINUTE] ? 0 :
  7195. count * mathFloor(minDate.getSeconds() / count));
  7196. }
  7197. if (interval >= timeUnits[MINUTE]) { // minute
  7198. minDate[setMinutes](interval >= timeUnits[HOUR] ? 0 :
  7199. count * mathFloor(minDate[getMinutes]() / count));
  7200. }
  7201. if (interval >= timeUnits[HOUR]) { // hour
  7202. minDate[setHours](interval >= timeUnits[DAY] ? 0 :
  7203. count * mathFloor(minDate[getHours]() / count));
  7204. }
  7205. if (interval >= timeUnits[DAY]) { // day
  7206. minDate[setDate](interval >= timeUnits[MONTH] ? 1 :
  7207. count * mathFloor(minDate[getDate]() / count));
  7208. }
  7209. if (interval >= timeUnits[MONTH]) { // month
  7210. minDate[setMonth](interval >= timeUnits[YEAR] ? 0 :
  7211. count * mathFloor(minDate[getMonth]() / count));
  7212. minYear = minDate[getFullYear]();
  7213. }
  7214. if (interval >= timeUnits[YEAR]) { // year
  7215. minYear -= minYear % count;
  7216. minDate[setFullYear](minYear);
  7217. }
  7218. // week is a special case that runs outside the hierarchy
  7219. if (interval === timeUnits[WEEK]) {
  7220. // get start of current week, independent of count
  7221. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  7222. pick(startOfWeek, 1));
  7223. }
  7224. // get tick positions
  7225. i = 1;
  7226. if (timezoneOffset) {
  7227. minDate = new Date(minDate.getTime() + timezoneOffset);
  7228. }
  7229. minYear = minDate[getFullYear]();
  7230. var time = minDate.getTime(),
  7231. minMonth = minDate[getMonth](),
  7232. minDateDate = minDate[getDate](),
  7233. localTimezoneOffset = useUTC ?
  7234. timezoneOffset :
  7235. (24 * 3600 * 1000 + minDate.getTimezoneOffset() * 60 * 1000) % (24 * 3600 * 1000); // #950
  7236. // iterate and add tick positions at appropriate values
  7237. while (time < max) {
  7238. tickPositions.push(time);
  7239. // if the interval is years, use Date.UTC to increase years
  7240. if (interval === timeUnits[YEAR]) {
  7241. time = makeTime(minYear + i * count, 0);
  7242. // if the interval is months, use Date.UTC to increase months
  7243. } else if (interval === timeUnits[MONTH]) {
  7244. time = makeTime(minYear, minMonth + i * count);
  7245. // if we're using global time, the interval is not fixed as it jumps
  7246. // one hour at the DST crossover
  7247. } else if (!useUTC && (interval === timeUnits[DAY] || interval === timeUnits[WEEK])) {
  7248. time = makeTime(minYear, minMonth, minDateDate +
  7249. i * count * (interval === timeUnits[DAY] ? 1 : 7));
  7250. // else, the interval is fixed and we use simple addition
  7251. } else {
  7252. time += interval * count;
  7253. }
  7254. i++;
  7255. }
  7256. // push the last time
  7257. tickPositions.push(time);
  7258. // mark new days if the time is dividible by day (#1649, #1760)
  7259. each(grep(tickPositions, function (time) {
  7260. return interval <= timeUnits[HOUR] && time % timeUnits[DAY] === localTimezoneOffset;
  7261. }), function (time) {
  7262. higherRanks[time] = DAY;
  7263. });
  7264. }
  7265. // record information on the chosen unit - for dynamic label formatter
  7266. tickPositions.info = extend(normalizedInterval, {
  7267. higherRanks: higherRanks,
  7268. totalRange: interval * count
  7269. });
  7270. return tickPositions;
  7271. };
  7272. /**
  7273. * Get a normalized tick interval for dates. Returns a configuration object with
  7274. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  7275. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  7276. * of segments in stock charts, the normalizing logic was extracted in order to
  7277. * prevent it for running over again for each segment having the same interval.
  7278. * #662, #697.
  7279. */
  7280. Axis.prototype.normalizeTimeTickInterval = function (tickInterval, unitsOption) {
  7281. var units = unitsOption || [[
  7282. MILLISECOND, // unit name
  7283. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  7284. ], [
  7285. SECOND,
  7286. [1, 2, 5, 10, 15, 30]
  7287. ], [
  7288. MINUTE,
  7289. [1, 2, 5, 10, 15, 30]
  7290. ], [
  7291. HOUR,
  7292. [1, 2, 3, 4, 6, 8, 12]
  7293. ], [
  7294. DAY,
  7295. [1, 2]
  7296. ], [
  7297. WEEK,
  7298. [1, 2]
  7299. ], [
  7300. MONTH,
  7301. [1, 2, 3, 4, 6]
  7302. ], [
  7303. YEAR,
  7304. null
  7305. ]],
  7306. unit = units[units.length - 1], // default unit is years
  7307. interval = timeUnits[unit[0]],
  7308. multiples = unit[1],
  7309. count,
  7310. i;
  7311. // loop through the units to find the one that best fits the tickInterval
  7312. for (i = 0; i < units.length; i++) {
  7313. unit = units[i];
  7314. interval = timeUnits[unit[0]];
  7315. multiples = unit[1];
  7316. if (units[i + 1]) {
  7317. // lessThan is in the middle between the highest multiple and the next unit.
  7318. var lessThan = (interval * multiples[multiples.length - 1] +
  7319. timeUnits[units[i + 1][0]]) / 2;
  7320. // break and keep the current unit
  7321. if (tickInterval <= lessThan) {
  7322. break;
  7323. }
  7324. }
  7325. }
  7326. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  7327. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  7328. multiples = [1, 2, 5];
  7329. }
  7330. // get the count
  7331. count = normalizeTickInterval(
  7332. tickInterval / interval,
  7333. multiples,
  7334. unit[0] === YEAR ? mathMax(getMagnitude(tickInterval / interval), 1) : 1 // #1913, #2360
  7335. );
  7336. return {
  7337. unitRange: interval,
  7338. count: count,
  7339. unitName: unit[0]
  7340. };
  7341. };/**
  7342. * Methods defined on the Axis prototype
  7343. */
  7344. /**
  7345. * Set the tick positions of a logarithmic axis
  7346. */
  7347. Axis.prototype.getLogTickPositions = function (interval, min, max, minor) {
  7348. var axis = this,
  7349. options = axis.options,
  7350. axisLength = axis.len,
  7351. // Since we use this method for both major and minor ticks,
  7352. // use a local variable and return the result
  7353. positions = [];
  7354. // Reset
  7355. if (!minor) {
  7356. axis._minorAutoInterval = null;
  7357. }
  7358. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  7359. if (interval >= 0.5) {
  7360. interval = mathRound(interval);
  7361. positions = axis.getLinearTickPositions(interval, min, max);
  7362. // Second case: We need intermediary ticks. For example
  7363. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  7364. } else if (interval >= 0.08) {
  7365. var roundedMin = mathFloor(min),
  7366. intermediate,
  7367. i,
  7368. j,
  7369. len,
  7370. pos,
  7371. lastPos,
  7372. break2;
  7373. if (interval > 0.3) {
  7374. intermediate = [1, 2, 4];
  7375. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  7376. intermediate = [1, 2, 4, 6, 8];
  7377. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  7378. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  7379. }
  7380. for (i = roundedMin; i < max + 1 && !break2; i++) {
  7381. len = intermediate.length;
  7382. for (j = 0; j < len && !break2; j++) {
  7383. pos = log2lin(lin2log(i) * intermediate[j]);
  7384. if (pos > min && (!minor || lastPos <= max)) { // #1670
  7385. positions.push(lastPos);
  7386. }
  7387. if (lastPos > max) {
  7388. break2 = true;
  7389. }
  7390. lastPos = pos;
  7391. }
  7392. }
  7393. // Third case: We are so deep in between whole logarithmic values that
  7394. // we might as well handle the tick positions like a linear axis. For
  7395. // example 1.01, 1.02, 1.03, 1.04.
  7396. } else {
  7397. var realMin = lin2log(min),
  7398. realMax = lin2log(max),
  7399. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  7400. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  7401. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  7402. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  7403. interval = pick(
  7404. filteredTickIntervalOption,
  7405. axis._minorAutoInterval,
  7406. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  7407. );
  7408. interval = normalizeTickInterval(
  7409. interval,
  7410. null,
  7411. getMagnitude(interval)
  7412. );
  7413. positions = map(axis.getLinearTickPositions(
  7414. interval,
  7415. realMin,
  7416. realMax
  7417. ), log2lin);
  7418. if (!minor) {
  7419. axis._minorAutoInterval = interval / 5;
  7420. }
  7421. }
  7422. // Set the axis-level tickInterval variable
  7423. if (!minor) {
  7424. axis.tickInterval = interval;
  7425. }
  7426. return positions;
  7427. };/**
  7428. * The tooltip object
  7429. * @param {Object} chart The chart instance
  7430. * @param {Object} options Tooltip options
  7431. */
  7432. var Tooltip = Highcharts.Tooltip = function () {
  7433. this.init.apply(this, arguments);
  7434. };
  7435. Tooltip.prototype = {
  7436. init: function (chart, options) {
  7437. var borderWidth = options.borderWidth,
  7438. style = options.style,
  7439. padding = pInt(style.padding);
  7440. // Save the chart and options
  7441. this.chart = chart;
  7442. this.options = options;
  7443. // Keep track of the current series
  7444. //this.currentSeries = UNDEFINED;
  7445. // List of crosshairs
  7446. this.crosshairs = [];
  7447. // Current values of x and y when animating
  7448. this.now = { x: 0, y: 0 };
  7449. // The tooltip is initially hidden
  7450. this.isHidden = true;
  7451. // create the label
  7452. this.label = chart.renderer.label('', 0, 0, options.shape, null, null, options.useHTML, null, 'tooltip')
  7453. .attr({
  7454. padding: padding,
  7455. fill: options.backgroundColor,
  7456. 'stroke-width': borderWidth,
  7457. r: options.borderRadius,
  7458. zIndex: 8
  7459. })
  7460. .css(style)
  7461. .css({ padding: 0 }) // Remove it from VML, the padding is applied as an attribute instead (#1117)
  7462. .add()
  7463. .attr({ y: -9999 }); // #2301, #2657
  7464. // When using canVG the shadow shows up as a gray circle
  7465. // even if the tooltip is hidden.
  7466. if (!useCanVG) {
  7467. this.label.shadow(options.shadow);
  7468. }
  7469. // Public property for getting the shared state.
  7470. this.shared = options.shared;
  7471. },
  7472. /**
  7473. * Destroy the tooltip and its elements.
  7474. */
  7475. destroy: function () {
  7476. // Destroy and clear local variables
  7477. if (this.label) {
  7478. this.label = this.label.destroy();
  7479. }
  7480. clearTimeout(this.hideTimer);
  7481. clearTimeout(this.tooltipTimeout);
  7482. },
  7483. /**
  7484. * Provide a soft movement for the tooltip
  7485. *
  7486. * @param {Number} x
  7487. * @param {Number} y
  7488. * @private
  7489. */
  7490. move: function (x, y, anchorX, anchorY) {
  7491. var tooltip = this,
  7492. now = tooltip.now,
  7493. animate = tooltip.options.animation !== false && !tooltip.isHidden;
  7494. // get intermediate values for animation
  7495. extend(now, {
  7496. x: animate ? (2 * now.x + x) / 3 : x,
  7497. y: animate ? (now.y + y) / 2 : y,
  7498. anchorX: animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
  7499. anchorY: animate ? (now.anchorY + anchorY) / 2 : anchorY
  7500. });
  7501. // move to the intermediate value
  7502. tooltip.label.attr(now);
  7503. // run on next tick of the mouse tracker
  7504. if (animate && (mathAbs(x - now.x) > 1 || mathAbs(y - now.y) > 1)) {
  7505. // never allow two timeouts
  7506. clearTimeout(this.tooltipTimeout);
  7507. // set the fixed interval ticking for the smooth tooltip
  7508. this.tooltipTimeout = setTimeout(function () {
  7509. // The interval function may still be running during destroy, so check that the chart is really there before calling.
  7510. if (tooltip) {
  7511. tooltip.move(x, y, anchorX, anchorY);
  7512. }
  7513. }, 32);
  7514. }
  7515. },
  7516. /**
  7517. * Hide the tooltip
  7518. */
  7519. hide: function () {
  7520. var tooltip = this,
  7521. hoverPoints;
  7522. clearTimeout(this.hideTimer); // disallow duplicate timers (#1728, #1766)
  7523. if (!this.isHidden) {
  7524. hoverPoints = this.chart.hoverPoints;
  7525. this.hideTimer = setTimeout(function () {
  7526. tooltip.label.fadeOut();
  7527. tooltip.isHidden = true;
  7528. }, pick(this.options.hideDelay, 500));
  7529. // hide previous hoverPoints and set new
  7530. if (hoverPoints) {
  7531. each(hoverPoints, function (point) {
  7532. point.setState();
  7533. });
  7534. }
  7535. this.chart.hoverPoints = null;
  7536. }
  7537. },
  7538. /**
  7539. * Extendable method to get the anchor position of the tooltip
  7540. * from a point or set of points
  7541. */
  7542. getAnchor: function (points, mouseEvent) {
  7543. var ret,
  7544. chart = this.chart,
  7545. inverted = chart.inverted,
  7546. plotTop = chart.plotTop,
  7547. plotX = 0,
  7548. plotY = 0,
  7549. yAxis;
  7550. points = splat(points);
  7551. // Pie uses a special tooltipPos
  7552. ret = points[0].tooltipPos;
  7553. // When tooltip follows mouse, relate the position to the mouse
  7554. if (this.followPointer && mouseEvent) {
  7555. if (mouseEvent.chartX === UNDEFINED) {
  7556. mouseEvent = chart.pointer.normalize(mouseEvent);
  7557. }
  7558. ret = [
  7559. mouseEvent.chartX - chart.plotLeft,
  7560. mouseEvent.chartY - plotTop
  7561. ];
  7562. }
  7563. // When shared, use the average position
  7564. if (!ret) {
  7565. each(points, function (point) {
  7566. yAxis = point.series.yAxis;
  7567. plotX += point.plotX;
  7568. plotY += (point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY) +
  7569. (!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
  7570. });
  7571. plotX /= points.length;
  7572. plotY /= points.length;
  7573. ret = [
  7574. inverted ? chart.plotWidth - plotY : plotX,
  7575. this.shared && !inverted && points.length > 1 && mouseEvent ?
  7576. mouseEvent.chartY - plotTop : // place shared tooltip next to the mouse (#424)
  7577. inverted ? chart.plotHeight - plotX : plotY
  7578. ];
  7579. }
  7580. return map(ret, mathRound);
  7581. },
  7582. /**
  7583. * Place the tooltip in a chart without spilling over
  7584. * and not covering the point it self.
  7585. */
  7586. getPosition: function (boxWidth, boxHeight, point) {
  7587. // Set up the variables
  7588. var chart = this.chart,
  7589. plotLeft = chart.plotLeft,
  7590. plotTop = chart.plotTop,
  7591. plotWidth = chart.plotWidth,
  7592. plotHeight = chart.plotHeight,
  7593. distance = pick(this.options.distance, 12),
  7594. pointX = (isNaN(point.plotX) ? 0 : point.plotX), //#2599
  7595. pointY = point.plotY,
  7596. x = pointX + plotLeft + (chart.inverted ? distance : -boxWidth - distance),
  7597. y = pointY - boxHeight + plotTop + 15, // 15 means the point is 15 pixels up from the bottom of the tooltip
  7598. alignedRight;
  7599. // It is too far to the left, adjust it
  7600. if (x < 7) {
  7601. x = plotLeft + mathMax(pointX, 0) + distance;
  7602. }
  7603. // Test to see if the tooltip is too far to the right,
  7604. // if it is, move it back to be inside and then up to not cover the point.
  7605. if ((x + boxWidth) > (plotLeft + plotWidth)) {
  7606. x -= (x + boxWidth) - (plotLeft + plotWidth);
  7607. y = pointY - boxHeight + plotTop - distance;
  7608. alignedRight = true;
  7609. }
  7610. // If it is now above the plot area, align it to the top of the plot area
  7611. if (y < plotTop + 5) {
  7612. y = plotTop + 5;
  7613. // If the tooltip is still covering the point, move it below instead
  7614. if (alignedRight && pointY >= y && pointY <= (y + boxHeight)) {
  7615. y = pointY + plotTop + distance; // below
  7616. }
  7617. }
  7618. // Now if the tooltip is below the chart, move it up. It's better to cover the
  7619. // point than to disappear outside the chart. #834.
  7620. if (y + boxHeight > plotTop + plotHeight) {
  7621. y = mathMax(plotTop, plotTop + plotHeight - boxHeight - distance); // below
  7622. }
  7623. return {x: x, y: y};
  7624. },
  7625. /**
  7626. * In case no user defined formatter is given, this will be used. Note that the context
  7627. * here is an object holding point, series, x, y etc.
  7628. */
  7629. defaultFormatter: function (tooltip) {
  7630. var items = this.points || splat(this),
  7631. series = items[0].series,
  7632. s;
  7633. // build the header
  7634. s = [tooltip.tooltipHeaderFormatter(items[0])];
  7635. // build the values
  7636. each(items, function (item) {
  7637. series = item.series;
  7638. s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
  7639. item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
  7640. });
  7641. // footer
  7642. s.push(tooltip.options.footerFormat || '');
  7643. return s.join('');
  7644. },
  7645. /**
  7646. * Refresh the tooltip's text and position.
  7647. * @param {Object} point
  7648. */
  7649. refresh: function (point, mouseEvent) {
  7650. var tooltip = this,
  7651. chart = tooltip.chart,
  7652. label = tooltip.label,
  7653. options = tooltip.options,
  7654. x,
  7655. y,
  7656. anchor,
  7657. textConfig = {},
  7658. text,
  7659. pointConfig = [],
  7660. formatter = options.formatter || tooltip.defaultFormatter,
  7661. hoverPoints = chart.hoverPoints,
  7662. borderColor,
  7663. shared = tooltip.shared,
  7664. currentSeries;
  7665. clearTimeout(this.hideTimer);
  7666. // get the reference point coordinates (pie charts use tooltipPos)
  7667. tooltip.followPointer = splat(point)[0].series.tooltipOptions.followPointer;
  7668. anchor = tooltip.getAnchor(point, mouseEvent);
  7669. x = anchor[0];
  7670. y = anchor[1];
  7671. // shared tooltip, array is sent over
  7672. if (shared && !(point.series && point.series.noSharedTooltip)) {
  7673. // hide previous hoverPoints and set new
  7674. chart.hoverPoints = point;
  7675. if (hoverPoints) {
  7676. each(hoverPoints, function (point) {
  7677. point.setState();
  7678. });
  7679. }
  7680. each(point, function (item) {
  7681. item.setState(HOVER_STATE);
  7682. pointConfig.push(item.getLabelConfig());
  7683. });
  7684. textConfig = {
  7685. x: point[0].category,
  7686. y: point[0].y
  7687. };
  7688. textConfig.points = pointConfig;
  7689. point = point[0];
  7690. // single point tooltip
  7691. } else {
  7692. textConfig = point.getLabelConfig();
  7693. }
  7694. text = formatter.call(textConfig, tooltip);
  7695. // register the current series
  7696. currentSeries = point.series;
  7697. // update the inner HTML
  7698. if (text === false) {
  7699. this.hide();
  7700. } else {
  7701. // show it
  7702. if (tooltip.isHidden) {
  7703. stop(label);
  7704. label.attr('opacity', 1).show();
  7705. }
  7706. // update text
  7707. label.attr({
  7708. text: text
  7709. });
  7710. // set the stroke color of the box
  7711. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  7712. label.attr({
  7713. stroke: borderColor
  7714. });
  7715. tooltip.updatePosition({ plotX: x, plotY: y });
  7716. this.isHidden = false;
  7717. }
  7718. fireEvent(chart, 'tooltipRefresh', {
  7719. text: text,
  7720. x: x + chart.plotLeft,
  7721. y: y + chart.plotTop,
  7722. borderColor: borderColor
  7723. });
  7724. },
  7725. /**
  7726. * Find the new position and perform the move
  7727. */
  7728. updatePosition: function (point) {
  7729. var chart = this.chart,
  7730. label = this.label,
  7731. pos = (this.options.positioner || this.getPosition).call(
  7732. this,
  7733. label.width,
  7734. label.height,
  7735. point
  7736. );
  7737. // do the move
  7738. this.move(
  7739. mathRound(pos.x),
  7740. mathRound(pos.y),
  7741. point.plotX + chart.plotLeft,
  7742. point.plotY + chart.plotTop
  7743. );
  7744. },
  7745. /**
  7746. * Format the header of the tooltip
  7747. */
  7748. tooltipHeaderFormatter: function (point) {
  7749. var series = point.series,
  7750. tooltipOptions = series.tooltipOptions,
  7751. dateTimeLabelFormats = tooltipOptions.dateTimeLabelFormats,
  7752. xDateFormat = tooltipOptions.xDateFormat,
  7753. xAxis = series.xAxis,
  7754. isDateTime = xAxis && xAxis.options.type === 'datetime' && isNumber(point.key),
  7755. headerFormat = tooltipOptions.headerFormat,
  7756. closestPointRange = xAxis && xAxis.closestPointRange,
  7757. n;
  7758. // Guess the best date format based on the closest point distance (#568)
  7759. if (isDateTime && !xDateFormat) {
  7760. if (closestPointRange) {
  7761. for (n in timeUnits) {
  7762. if (timeUnits[n] >= closestPointRange ||
  7763. // If the point is placed every day at 23:59, we need to show
  7764. // the minutes as well. This logic only works for time units less than
  7765. // a day, since all higher time units are dividable by those. #2637.
  7766. (timeUnits[n] <= timeUnits[DAY] && point.key % timeUnits[n] > 0)) {
  7767. xDateFormat = dateTimeLabelFormats[n];
  7768. break;
  7769. }
  7770. }
  7771. } else {
  7772. xDateFormat = dateTimeLabelFormats.day;
  7773. }
  7774. xDateFormat = xDateFormat || dateTimeLabelFormats.year; // #2546, 2581
  7775. }
  7776. // Insert the header date format if any
  7777. if (isDateTime && xDateFormat) {
  7778. headerFormat = headerFormat.replace('{point.key}', '{point.key:' + xDateFormat + '}');
  7779. }
  7780. return format(headerFormat, {
  7781. point: point,
  7782. series: series
  7783. });
  7784. }
  7785. };
  7786. var hoverChartIndex;
  7787. // Global flag for touch support
  7788. hasTouch = doc.documentElement.ontouchstart !== UNDEFINED;
  7789. /**
  7790. * The mouse tracker object. All methods starting with "on" are primary DOM event handlers.
  7791. * Subsequent methods should be named differently from what they are doing.
  7792. * @param {Object} chart The Chart instance
  7793. * @param {Object} options The root options object
  7794. */
  7795. var Pointer = Highcharts.Pointer = function (chart, options) {
  7796. this.init(chart, options);
  7797. };
  7798. Pointer.prototype = {
  7799. /**
  7800. * Initialize Pointer
  7801. */
  7802. init: function (chart, options) {
  7803. var chartOptions = options.chart,
  7804. chartEvents = chartOptions.events,
  7805. zoomType = useCanVG ? '' : chartOptions.zoomType,
  7806. inverted = chart.inverted,
  7807. zoomX,
  7808. zoomY;
  7809. // Store references
  7810. this.options = options;
  7811. this.chart = chart;
  7812. // Zoom status
  7813. this.zoomX = zoomX = /x/.test(zoomType);
  7814. this.zoomY = zoomY = /y/.test(zoomType);
  7815. this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
  7816. this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
  7817. // Do we need to handle click on a touch device?
  7818. this.runChartClick = chartEvents && !!chartEvents.click;
  7819. this.pinchDown = [];
  7820. this.lastValidTouch = {};
  7821. if (Highcharts.Tooltip && options.tooltip.enabled) {
  7822. chart.tooltip = new Tooltip(chart, options.tooltip);
  7823. }
  7824. this.setDOMEvents();
  7825. },
  7826. /**
  7827. * Add crossbrowser support for chartX and chartY
  7828. * @param {Object} e The event object in standard browsers
  7829. */
  7830. normalize: function (e, chartPosition) {
  7831. var chartX,
  7832. chartY,
  7833. ePos;
  7834. // common IE normalizing
  7835. e = e || win.event;
  7836. // Framework specific normalizing (#1165)
  7837. e = washMouseEvent(e);
  7838. // More IE normalizing, needs to go after washMouseEvent
  7839. if (!e.target) {
  7840. e.target = e.srcElement;
  7841. }
  7842. // iOS
  7843. ePos = e.touches ? e.touches.item(0) : e;
  7844. // Get mouse position
  7845. if (!chartPosition) {
  7846. this.chartPosition = chartPosition = offset(this.chart.container);
  7847. }
  7848. // chartX and chartY
  7849. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  7850. chartX = mathMax(e.x, e.clientX - chartPosition.left); // #2005, #2129: the second case is
  7851. // for IE10 quirks mode within framesets
  7852. chartY = e.y;
  7853. } else {
  7854. chartX = ePos.pageX - chartPosition.left;
  7855. chartY = ePos.pageY - chartPosition.top;
  7856. }
  7857. return extend(e, {
  7858. chartX: mathRound(chartX),
  7859. chartY: mathRound(chartY)
  7860. });
  7861. },
  7862. /**
  7863. * Get the click position in terms of axis values.
  7864. *
  7865. * @param {Object} e A pointer event
  7866. */
  7867. getCoordinates: function (e) {
  7868. var coordinates = {
  7869. xAxis: [],
  7870. yAxis: []
  7871. };
  7872. each(this.chart.axes, function (axis) {
  7873. coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
  7874. axis: axis,
  7875. value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
  7876. });
  7877. });
  7878. return coordinates;
  7879. },
  7880. /**
  7881. * Return the index in the tooltipPoints array, corresponding to pixel position in
  7882. * the plot area.
  7883. */
  7884. getIndex: function (e) {
  7885. var chart = this.chart;
  7886. return chart.inverted ?
  7887. chart.plotHeight + chart.plotTop - e.chartY :
  7888. e.chartX - chart.plotLeft;
  7889. },
  7890. /**
  7891. * With line type charts with a single tracker, get the point closest to the mouse.
  7892. * Run Point.onMouseOver and display tooltip for the point or points.
  7893. */
  7894. runPointActions: function (e) {
  7895. var pointer = this,
  7896. chart = pointer.chart,
  7897. series = chart.series,
  7898. tooltip = chart.tooltip,
  7899. point,
  7900. points,
  7901. hoverPoint = chart.hoverPoint,
  7902. hoverSeries = chart.hoverSeries,
  7903. i,
  7904. j,
  7905. distance = chart.chartWidth,
  7906. index = pointer.getIndex(e),
  7907. anchor;
  7908. // shared tooltip
  7909. if (tooltip && pointer.options.tooltip.shared && !(hoverSeries && hoverSeries.noSharedTooltip)) {
  7910. points = [];
  7911. // loop over all series and find the ones with points closest to the mouse
  7912. i = series.length;
  7913. for (j = 0; j < i; j++) {
  7914. if (series[j].visible &&
  7915. series[j].options.enableMouseTracking !== false &&
  7916. !series[j].noSharedTooltip && series[j].singularTooltips !== true && series[j].tooltipPoints.length) {
  7917. point = series[j].tooltipPoints[index];
  7918. if (point && point.series) { // not a dummy point, #1544
  7919. point._dist = mathAbs(index - point.clientX);
  7920. distance = mathMin(distance, point._dist);
  7921. points.push(point);
  7922. }
  7923. }
  7924. }
  7925. // remove furthest points
  7926. i = points.length;
  7927. while (i--) {
  7928. if (points[i]._dist > distance) {
  7929. points.splice(i, 1);
  7930. }
  7931. }
  7932. // refresh the tooltip if necessary
  7933. if (points.length && (points[0].clientX !== pointer.hoverX)) {
  7934. tooltip.refresh(points, e);
  7935. pointer.hoverX = points[0].clientX;
  7936. }
  7937. }
  7938. // separate tooltip and general mouse events
  7939. if (hoverSeries && hoverSeries.tracker && (!tooltip || !tooltip.followPointer)) { // only use for line-type series with common tracker and while not following the pointer #2584
  7940. // get the point
  7941. point = hoverSeries.tooltipPoints[index];
  7942. // a new point is hovered, refresh the tooltip
  7943. if (point && point !== hoverPoint) {
  7944. // trigger the events
  7945. point.onMouseOver(e);
  7946. }
  7947. } else if (tooltip && tooltip.followPointer && !tooltip.isHidden) {
  7948. anchor = tooltip.getAnchor([{}], e);
  7949. tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
  7950. }
  7951. // Start the event listener to pick up the tooltip
  7952. if (tooltip && !pointer._onDocumentMouseMove) {
  7953. pointer._onDocumentMouseMove = function (e) {
  7954. if (defined(hoverChartIndex)) {
  7955. charts[hoverChartIndex].pointer.onDocumentMouseMove(e);
  7956. }
  7957. };
  7958. addEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  7959. }
  7960. // Draw independent crosshairs
  7961. each(chart.axes, function (axis) {
  7962. axis.drawCrosshair(e, pick(point, hoverPoint));
  7963. });
  7964. },
  7965. /**
  7966. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  7967. *
  7968. * @param allowMove {Boolean} Instead of destroying the tooltip altogether, allow moving it if possible
  7969. */
  7970. reset: function (allowMove) {
  7971. var pointer = this,
  7972. chart = pointer.chart,
  7973. hoverSeries = chart.hoverSeries,
  7974. hoverPoint = chart.hoverPoint,
  7975. tooltip = chart.tooltip,
  7976. tooltipPoints = tooltip && tooltip.shared ? chart.hoverPoints : hoverPoint;
  7977. // Narrow in allowMove
  7978. allowMove = allowMove && tooltip && tooltipPoints;
  7979. // Check if the points have moved outside the plot area, #1003
  7980. if (allowMove && splat(tooltipPoints)[0].plotX === UNDEFINED) {
  7981. allowMove = false;
  7982. }
  7983. // Just move the tooltip, #349
  7984. if (allowMove) {
  7985. tooltip.refresh(tooltipPoints);
  7986. if (hoverPoint) { // #2500
  7987. hoverPoint.setState(hoverPoint.state, true);
  7988. }
  7989. // Full reset
  7990. } else {
  7991. if (hoverPoint) {
  7992. hoverPoint.onMouseOut();
  7993. }
  7994. if (hoverSeries) {
  7995. hoverSeries.onMouseOut();
  7996. }
  7997. if (tooltip) {
  7998. tooltip.hide();
  7999. }
  8000. if (pointer._onDocumentMouseMove) {
  8001. removeEvent(doc, 'mousemove', pointer._onDocumentMouseMove);
  8002. pointer._onDocumentMouseMove = null;
  8003. }
  8004. // Remove crosshairs
  8005. each(chart.axes, function (axis) {
  8006. axis.hideCrosshair();
  8007. });
  8008. pointer.hoverX = null;
  8009. }
  8010. },
  8011. /**
  8012. * Scale series groups to a certain scale and translation
  8013. */
  8014. scaleGroups: function (attribs, clip) {
  8015. var chart = this.chart,
  8016. seriesAttribs;
  8017. // Scale each series
  8018. each(chart.series, function (series) {
  8019. seriesAttribs = attribs || series.getPlotBox(); // #1701
  8020. if (series.xAxis && series.xAxis.zoomEnabled) {
  8021. series.group.attr(seriesAttribs);
  8022. if (series.markerGroup) {
  8023. series.markerGroup.attr(seriesAttribs);
  8024. series.markerGroup.clip(clip ? chart.clipRect : null);
  8025. }
  8026. if (series.dataLabelsGroup) {
  8027. series.dataLabelsGroup.attr(seriesAttribs);
  8028. }
  8029. }
  8030. });
  8031. // Clip
  8032. chart.clipRect.attr(clip || chart.clipBox);
  8033. },
  8034. /**
  8035. * Start a drag operation
  8036. */
  8037. dragStart: function (e) {
  8038. var chart = this.chart;
  8039. // Record the start position
  8040. chart.mouseIsDown = e.type;
  8041. chart.cancelClick = false;
  8042. chart.mouseDownX = this.mouseDownX = e.chartX;
  8043. chart.mouseDownY = this.mouseDownY = e.chartY;
  8044. },
  8045. /**
  8046. * Perform a drag operation in response to a mousemove event while the mouse is down
  8047. */
  8048. drag: function (e) {
  8049. var chart = this.chart,
  8050. chartOptions = chart.options.chart,
  8051. chartX = e.chartX,
  8052. chartY = e.chartY,
  8053. zoomHor = this.zoomHor,
  8054. zoomVert = this.zoomVert,
  8055. plotLeft = chart.plotLeft,
  8056. plotTop = chart.plotTop,
  8057. plotWidth = chart.plotWidth,
  8058. plotHeight = chart.plotHeight,
  8059. clickedInside,
  8060. size,
  8061. mouseDownX = this.mouseDownX,
  8062. mouseDownY = this.mouseDownY;
  8063. // If the mouse is outside the plot area, adjust to cooordinates
  8064. // inside to prevent the selection marker from going outside
  8065. if (chartX < plotLeft) {
  8066. chartX = plotLeft;
  8067. } else if (chartX > plotLeft + plotWidth) {
  8068. chartX = plotLeft + plotWidth;
  8069. }
  8070. if (chartY < plotTop) {
  8071. chartY = plotTop;
  8072. } else if (chartY > plotTop + plotHeight) {
  8073. chartY = plotTop + plotHeight;
  8074. }
  8075. // determine if the mouse has moved more than 10px
  8076. this.hasDragged = Math.sqrt(
  8077. Math.pow(mouseDownX - chartX, 2) +
  8078. Math.pow(mouseDownY - chartY, 2)
  8079. );
  8080. if (this.hasDragged > 10) {
  8081. clickedInside = chart.isInsidePlot(mouseDownX - plotLeft, mouseDownY - plotTop);
  8082. // make a selection
  8083. if (chart.hasCartesianSeries && (this.zoomX || this.zoomY) && clickedInside) {
  8084. if (!this.selectionMarker) {
  8085. this.selectionMarker = chart.renderer.rect(
  8086. plotLeft,
  8087. plotTop,
  8088. zoomHor ? 1 : plotWidth,
  8089. zoomVert ? 1 : plotHeight,
  8090. 0
  8091. )
  8092. .attr({
  8093. fill: chartOptions.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  8094. zIndex: 7
  8095. })
  8096. .add();
  8097. }
  8098. }
  8099. // adjust the width of the selection marker
  8100. if (this.selectionMarker && zoomHor) {
  8101. size = chartX - mouseDownX;
  8102. this.selectionMarker.attr({
  8103. width: mathAbs(size),
  8104. x: (size > 0 ? 0 : size) + mouseDownX
  8105. });
  8106. }
  8107. // adjust the height of the selection marker
  8108. if (this.selectionMarker && zoomVert) {
  8109. size = chartY - mouseDownY;
  8110. this.selectionMarker.attr({
  8111. height: mathAbs(size),
  8112. y: (size > 0 ? 0 : size) + mouseDownY
  8113. });
  8114. }
  8115. // panning
  8116. if (clickedInside && !this.selectionMarker && chartOptions.panning) {
  8117. chart.pan(e, chartOptions.panning);
  8118. }
  8119. }
  8120. },
  8121. /**
  8122. * On mouse up or touch end across the entire document, drop the selection.
  8123. */
  8124. drop: function (e) {
  8125. var chart = this.chart,
  8126. hasPinched = this.hasPinched;
  8127. if (this.selectionMarker) {
  8128. var selectionData = {
  8129. xAxis: [],
  8130. yAxis: [],
  8131. originalEvent: e.originalEvent || e
  8132. },
  8133. selectionBox = this.selectionMarker,
  8134. selectionLeft = selectionBox.x,
  8135. selectionTop = selectionBox.y,
  8136. runZoom;
  8137. // a selection has been made
  8138. if (this.hasDragged || hasPinched) {
  8139. // record each axis' min and max
  8140. each(chart.axes, function (axis) {
  8141. if (axis.zoomEnabled) {
  8142. var horiz = axis.horiz,
  8143. selectionMin = axis.toValue((horiz ? selectionLeft : selectionTop)),
  8144. selectionMax = axis.toValue((horiz ? selectionLeft + selectionBox.width : selectionTop + selectionBox.height));
  8145. if (!isNaN(selectionMin) && !isNaN(selectionMax)) { // #859
  8146. selectionData[axis.coll].push({
  8147. axis: axis,
  8148. min: mathMin(selectionMin, selectionMax), // for reversed axes,
  8149. max: mathMax(selectionMin, selectionMax)
  8150. });
  8151. runZoom = true;
  8152. }
  8153. }
  8154. });
  8155. if (runZoom) {
  8156. fireEvent(chart, 'selection', selectionData, function (args) {
  8157. chart.zoom(extend(args, hasPinched ? { animation: false } : null));
  8158. });
  8159. }
  8160. }
  8161. this.selectionMarker = this.selectionMarker.destroy();
  8162. // Reset scaling preview
  8163. if (hasPinched) {
  8164. this.scaleGroups();
  8165. }
  8166. }
  8167. // Reset all
  8168. if (chart) { // it may be destroyed on mouse up - #877
  8169. css(chart.container, { cursor: chart._cursor });
  8170. chart.cancelClick = this.hasDragged > 10; // #370
  8171. chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
  8172. this.pinchDown = [];
  8173. }
  8174. },
  8175. onContainerMouseDown: function (e) {
  8176. e = this.normalize(e);
  8177. // issue #295, dragging not always working in Firefox
  8178. if (e.preventDefault) {
  8179. e.preventDefault();
  8180. }
  8181. this.dragStart(e);
  8182. },
  8183. onDocumentMouseUp: function (e) {
  8184. if (defined(hoverChartIndex)) {
  8185. charts[hoverChartIndex].pointer.drop(e);
  8186. }
  8187. },
  8188. /**
  8189. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  8190. * Issue #149 workaround. The mouseleave event does not always fire.
  8191. */
  8192. onDocumentMouseMove: function (e) {
  8193. var chart = this.chart,
  8194. chartPosition = this.chartPosition,
  8195. hoverSeries = chart.hoverSeries;
  8196. e = this.normalize(e, chartPosition);
  8197. // If we're outside, hide the tooltip
  8198. if (chartPosition && hoverSeries && !this.inClass(e.target, 'highcharts-tracker') &&
  8199. !chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8200. this.reset();
  8201. }
  8202. },
  8203. /**
  8204. * When mouse leaves the container, hide the tooltip.
  8205. */
  8206. onContainerMouseLeave: function () {
  8207. var chart = charts[hoverChartIndex];
  8208. if (chart) {
  8209. chart.pointer.reset();
  8210. chart.pointer.chartPosition = null; // also reset the chart position, used in #149 fix
  8211. }
  8212. hoverChartIndex = null;
  8213. },
  8214. // The mousemove, touchmove and touchstart event handler
  8215. onContainerMouseMove: function (e) {
  8216. var chart = this.chart;
  8217. hoverChartIndex = chart.index;
  8218. // normalize
  8219. e = this.normalize(e);
  8220. if (chart.mouseIsDown === 'mousedown') {
  8221. this.drag(e);
  8222. }
  8223. // Show the tooltip and run mouse over events (#977)
  8224. if ((this.inClass(e.target, 'highcharts-tracker') ||
  8225. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) && !chart.openMenu) {
  8226. this.runPointActions(e);
  8227. }
  8228. },
  8229. /**
  8230. * Utility to detect whether an element has, or has a parent with, a specific
  8231. * class name. Used on detection of tracker objects and on deciding whether
  8232. * hovering the tooltip should cause the active series to mouse out.
  8233. */
  8234. inClass: function (element, className) {
  8235. var elemClassName;
  8236. while (element) {
  8237. elemClassName = attr(element, 'class');
  8238. if (elemClassName) {
  8239. if (elemClassName.indexOf(className) !== -1) {
  8240. return true;
  8241. } else if (elemClassName.indexOf(PREFIX + 'container') !== -1) {
  8242. return false;
  8243. }
  8244. }
  8245. element = element.parentNode;
  8246. }
  8247. },
  8248. onTrackerMouseOut: function (e) {
  8249. var series = this.chart.hoverSeries,
  8250. relatedTarget = e.relatedTarget || e.toElement,
  8251. relatedSeries = relatedTarget && relatedTarget.point && relatedTarget.point.series; // #2499
  8252. if (series && !series.options.stickyTracking && !this.inClass(relatedTarget, PREFIX + 'tooltip') &&
  8253. relatedSeries !== series) {
  8254. series.onMouseOut();
  8255. }
  8256. },
  8257. onContainerClick: function (e) {
  8258. var chart = this.chart,
  8259. hoverPoint = chart.hoverPoint,
  8260. plotLeft = chart.plotLeft,
  8261. plotTop = chart.plotTop,
  8262. inverted = chart.inverted,
  8263. chartPosition,
  8264. plotX,
  8265. plotY;
  8266. e = this.normalize(e);
  8267. e.cancelBubble = true; // IE specific
  8268. if (!chart.cancelClick) {
  8269. // On tracker click, fire the series and point events. #783, #1583
  8270. if (hoverPoint && this.inClass(e.target, PREFIX + 'tracker')) {
  8271. chartPosition = this.chartPosition;
  8272. plotX = hoverPoint.plotX;
  8273. plotY = hoverPoint.plotY;
  8274. // add page position info
  8275. extend(hoverPoint, {
  8276. pageX: chartPosition.left + plotLeft +
  8277. (inverted ? chart.plotWidth - plotY : plotX),
  8278. pageY: chartPosition.top + plotTop +
  8279. (inverted ? chart.plotHeight - plotX : plotY)
  8280. });
  8281. // the series click event
  8282. fireEvent(hoverPoint.series, 'click', extend(e, {
  8283. point: hoverPoint
  8284. }));
  8285. // the point click event
  8286. if (chart.hoverPoint) { // it may be destroyed (#1844)
  8287. hoverPoint.firePointEvent('click', e);
  8288. }
  8289. // When clicking outside a tracker, fire a chart event
  8290. } else {
  8291. extend(e, this.getCoordinates(e));
  8292. // fire a click event in the chart
  8293. if (chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)) {
  8294. fireEvent(chart, 'click', e);
  8295. }
  8296. }
  8297. }
  8298. },
  8299. /**
  8300. * Set the JS DOM events on the container and document. This method should contain
  8301. * a one-to-one assignment between methods and their handlers. Any advanced logic should
  8302. * be moved to the handler reflecting the event's name.
  8303. */
  8304. setDOMEvents: function () {
  8305. var pointer = this,
  8306. container = pointer.chart.container;
  8307. container.onmousedown = function (e) {
  8308. pointer.onContainerMouseDown(e);
  8309. };
  8310. container.onmousemove = function (e) {
  8311. pointer.onContainerMouseMove(e);
  8312. };
  8313. container.onclick = function (e) {
  8314. pointer.onContainerClick(e);
  8315. };
  8316. addEvent(container, 'mouseleave', pointer.onContainerMouseLeave);
  8317. addEvent(doc, 'mouseup', pointer.onDocumentMouseUp);
  8318. if (hasTouch) {
  8319. container.ontouchstart = function (e) {
  8320. pointer.onContainerTouchStart(e);
  8321. };
  8322. container.ontouchmove = function (e) {
  8323. pointer.onContainerTouchMove(e);
  8324. };
  8325. addEvent(doc, 'touchend', pointer.onDocumentTouchEnd);
  8326. }
  8327. },
  8328. /**
  8329. * Destroys the Pointer object and disconnects DOM events.
  8330. */
  8331. destroy: function () {
  8332. var prop;
  8333. removeEvent(this.chart.container, 'mouseleave', this.onContainerMouseLeave);
  8334. removeEvent(doc, 'mouseup', this.onDocumentMouseUp);
  8335. removeEvent(doc, 'touchend', this.onDocumentTouchEnd);
  8336. // memory and CPU leak
  8337. clearInterval(this.tooltipTimeout);
  8338. for (prop in this) {
  8339. this[prop] = null;
  8340. }
  8341. }
  8342. };
  8343. /* Support for touch devices */
  8344. extend(Highcharts.Pointer.prototype, {
  8345. /**
  8346. * Run translation operations
  8347. */
  8348. pinchTranslate: function (zoomHor, zoomVert, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch) {
  8349. if (zoomHor) {
  8350. this.pinchTranslateDirection(true, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8351. }
  8352. if (zoomVert) {
  8353. this.pinchTranslateDirection(false, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8354. }
  8355. },
  8356. /**
  8357. * Run translation operations for each direction (horizontal and vertical) independently
  8358. */
  8359. pinchTranslateDirection: function (horiz, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch, forcedScale) {
  8360. var chart = this.chart,
  8361. xy = horiz ? 'x' : 'y',
  8362. XY = horiz ? 'X' : 'Y',
  8363. sChartXY = 'chart' + XY,
  8364. wh = horiz ? 'width' : 'height',
  8365. plotLeftTop = chart['plot' + (horiz ? 'Left' : 'Top')],
  8366. selectionWH,
  8367. selectionXY,
  8368. clipXY,
  8369. scale = forcedScale || 1,
  8370. inverted = chart.inverted,
  8371. bounds = chart.bounds[horiz ? 'h' : 'v'],
  8372. singleTouch = pinchDown.length === 1,
  8373. touch0Start = pinchDown[0][sChartXY],
  8374. touch0Now = touches[0][sChartXY],
  8375. touch1Start = !singleTouch && pinchDown[1][sChartXY],
  8376. touch1Now = !singleTouch && touches[1][sChartXY],
  8377. outOfBounds,
  8378. transformScale,
  8379. scaleKey,
  8380. setScale = function () {
  8381. if (!singleTouch && mathAbs(touch0Start - touch1Start) > 20) { // Don't zoom if fingers are too close on this axis
  8382. scale = forcedScale || mathAbs(touch0Now - touch1Now) / mathAbs(touch0Start - touch1Start);
  8383. }
  8384. clipXY = ((plotLeftTop - touch0Now) / scale) + touch0Start;
  8385. selectionWH = chart['plot' + (horiz ? 'Width' : 'Height')] / scale;
  8386. };
  8387. // Set the scale, first pass
  8388. setScale();
  8389. selectionXY = clipXY; // the clip position (x or y) is altered if out of bounds, the selection position is not
  8390. // Out of bounds
  8391. if (selectionXY < bounds.min) {
  8392. selectionXY = bounds.min;
  8393. outOfBounds = true;
  8394. } else if (selectionXY + selectionWH > bounds.max) {
  8395. selectionXY = bounds.max - selectionWH;
  8396. outOfBounds = true;
  8397. }
  8398. // Is the chart dragged off its bounds, determined by dataMin and dataMax?
  8399. if (outOfBounds) {
  8400. // Modify the touchNow position in order to create an elastic drag movement. This indicates
  8401. // to the user that the chart is responsive but can't be dragged further.
  8402. touch0Now -= 0.8 * (touch0Now - lastValidTouch[xy][0]);
  8403. if (!singleTouch) {
  8404. touch1Now -= 0.8 * (touch1Now - lastValidTouch[xy][1]);
  8405. }
  8406. // Set the scale, second pass to adapt to the modified touchNow positions
  8407. setScale();
  8408. } else {
  8409. lastValidTouch[xy] = [touch0Now, touch1Now];
  8410. }
  8411. // Set geometry for clipping, selection and transformation
  8412. if (!inverted) { // TODO: implement clipping for inverted charts
  8413. clip[xy] = clipXY - plotLeftTop;
  8414. clip[wh] = selectionWH;
  8415. }
  8416. scaleKey = inverted ? (horiz ? 'scaleY' : 'scaleX') : 'scale' + XY;
  8417. transformScale = inverted ? 1 / scale : scale;
  8418. selectionMarker[wh] = selectionWH;
  8419. selectionMarker[xy] = selectionXY;
  8420. transform[scaleKey] = scale;
  8421. transform['translate' + XY] = (transformScale * plotLeftTop) + (touch0Now - (transformScale * touch0Start));
  8422. },
  8423. /**
  8424. * Handle touch events with two touches
  8425. */
  8426. pinch: function (e) {
  8427. var self = this,
  8428. chart = self.chart,
  8429. pinchDown = self.pinchDown,
  8430. followTouchMove = chart.tooltip && chart.tooltip.options.followTouchMove,
  8431. touches = e.touches,
  8432. touchesLength = touches.length,
  8433. lastValidTouch = self.lastValidTouch,
  8434. zoomHor = self.zoomHor || self.pinchHor,
  8435. zoomVert = self.zoomVert || self.pinchVert,
  8436. hasZoom = zoomHor || zoomVert,
  8437. selectionMarker = self.selectionMarker,
  8438. transform = {},
  8439. fireClickEvent = touchesLength === 1 && ((self.inClass(e.target, PREFIX + 'tracker') &&
  8440. chart.runTrackerClick) || chart.runChartClick),
  8441. clip = {};
  8442. // On touch devices, only proceed to trigger click if a handler is defined
  8443. if ((hasZoom || followTouchMove) && !fireClickEvent) {
  8444. e.preventDefault();
  8445. }
  8446. // Normalize each touch
  8447. map(touches, function (e) {
  8448. return self.normalize(e);
  8449. });
  8450. // Register the touch start position
  8451. if (e.type === 'touchstart') {
  8452. each(touches, function (e, i) {
  8453. pinchDown[i] = { chartX: e.chartX, chartY: e.chartY };
  8454. });
  8455. lastValidTouch.x = [pinchDown[0].chartX, pinchDown[1] && pinchDown[1].chartX];
  8456. lastValidTouch.y = [pinchDown[0].chartY, pinchDown[1] && pinchDown[1].chartY];
  8457. // Identify the data bounds in pixels
  8458. each(chart.axes, function (axis) {
  8459. if (axis.zoomEnabled) {
  8460. var bounds = chart.bounds[axis.horiz ? 'h' : 'v'],
  8461. minPixelPadding = axis.minPixelPadding,
  8462. min = axis.toPixels(axis.dataMin),
  8463. max = axis.toPixels(axis.dataMax),
  8464. absMin = mathMin(min, max),
  8465. absMax = mathMax(min, max);
  8466. // Store the bounds for use in the touchmove handler
  8467. bounds.min = mathMin(axis.pos, absMin - minPixelPadding);
  8468. bounds.max = mathMax(axis.pos + axis.len, absMax + minPixelPadding);
  8469. }
  8470. });
  8471. // Event type is touchmove, handle panning and pinching
  8472. } else if (pinchDown.length) { // can be 0 when releasing, if touchend fires first
  8473. // Set the marker
  8474. if (!selectionMarker) {
  8475. self.selectionMarker = selectionMarker = extend({
  8476. destroy: noop
  8477. }, chart.plotBox);
  8478. }
  8479. self.pinchTranslate(zoomHor, zoomVert, pinchDown, touches, transform, selectionMarker, clip, lastValidTouch);
  8480. self.hasPinched = hasZoom;
  8481. // Scale and translate the groups to provide visual feedback during pinching
  8482. self.scaleGroups(transform, clip);
  8483. // Optionally move the tooltip on touchmove
  8484. if (!hasZoom && followTouchMove && touchesLength === 1) {
  8485. this.runPointActions(self.normalize(e));
  8486. }
  8487. }
  8488. },
  8489. onContainerTouchStart: function (e) {
  8490. var chart = this.chart;
  8491. hoverChartIndex = chart.index;
  8492. if (e.touches.length === 1) {
  8493. e = this.normalize(e);
  8494. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8495. // Prevent the click pseudo event from firing unless it is set in the options
  8496. /*if (!chart.runChartClick) {
  8497. e.preventDefault();
  8498. }*/
  8499. // Run mouse events and display tooltip etc
  8500. this.runPointActions(e);
  8501. this.pinch(e);
  8502. } else {
  8503. // Hide the tooltip on touching outside the plot area (#1203)
  8504. this.reset();
  8505. }
  8506. } else if (e.touches.length === 2) {
  8507. this.pinch(e);
  8508. }
  8509. },
  8510. onContainerTouchMove: function (e) {
  8511. if (e.touches.length === 1 || e.touches.length === 2) {
  8512. this.pinch(e);
  8513. }
  8514. },
  8515. onDocumentTouchEnd: function (e) {
  8516. if (defined(hoverChartIndex)) {
  8517. charts[hoverChartIndex].pointer.drop(e);
  8518. }
  8519. }
  8520. });
  8521. if (win.PointerEvent || win.MSPointerEvent) {
  8522. // The touches object keeps track of the points being touched at all times
  8523. var touches = {},
  8524. hasPointerEvent = !!win.PointerEvent,
  8525. getWebkitTouches = function () {
  8526. var key, fake = [];
  8527. fake.item = function (i) { return this[i]; };
  8528. for (key in touches) {
  8529. if (touches.hasOwnProperty(key)) {
  8530. fake.push({
  8531. pageX: touches[key].pageX,
  8532. pageY: touches[key].pageY,
  8533. target: touches[key].target
  8534. });
  8535. }
  8536. }
  8537. return fake;
  8538. },
  8539. translateMSPointer = function (e, method, wktype, callback) {
  8540. var p;
  8541. e = e.originalEvent || e;
  8542. if ((e.pointerType === 'touch' || e.pointerType === e.MSPOINTER_TYPE_TOUCH) && charts[hoverChartIndex]) {
  8543. callback(e);
  8544. p = charts[hoverChartIndex].pointer;
  8545. p[method]({
  8546. type: wktype,
  8547. target: e.currentTarget,
  8548. preventDefault: noop,
  8549. touches: getWebkitTouches()
  8550. });
  8551. }
  8552. };
  8553. /**
  8554. * Extend the Pointer prototype with methods for each event handler and more
  8555. */
  8556. extend(Pointer.prototype, {
  8557. onContainerPointerDown: function (e) {
  8558. translateMSPointer(e, 'onContainerTouchStart', 'touchstart', function (e) {
  8559. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY, target: e.currentTarget };
  8560. });
  8561. },
  8562. onContainerPointerMove: function (e) {
  8563. translateMSPointer(e, 'onContainerTouchMove', 'touchmove', function (e) {
  8564. touches[e.pointerId] = { pageX: e.pageX, pageY: e.pageY };
  8565. if (!touches[e.pointerId].target) {
  8566. touches[e.pointerId].target = e.currentTarget;
  8567. }
  8568. });
  8569. },
  8570. onDocumentPointerUp: function (e) {
  8571. translateMSPointer(e, 'onContainerTouchEnd', 'touchend', function (e) {
  8572. delete touches[e.pointerId];
  8573. });
  8574. },
  8575. /**
  8576. * Add or remove the MS Pointer specific events
  8577. */
  8578. batchMSEvents: function (fn) {
  8579. fn(this.chart.container, hasPointerEvent ? 'pointerdown' : 'MSPointerDown', this.onContainerPointerDown);
  8580. fn(this.chart.container, hasPointerEvent ? 'pointermove' : 'MSPointerMove', this.onContainerPointerMove);
  8581. fn(doc, hasPointerEvent ? 'pointerup' : 'MSPointerUp', this.onDocumentPointerUp);
  8582. }
  8583. });
  8584. // Disable default IE actions for pinch and such on chart element
  8585. wrap(Pointer.prototype, 'init', function (proceed, chart, options) {
  8586. css(chart.container, {
  8587. '-ms-touch-action': NONE,
  8588. 'touch-action': NONE
  8589. });
  8590. proceed.call(this, chart, options);
  8591. });
  8592. // Add IE specific touch events to chart
  8593. wrap(Pointer.prototype, 'setDOMEvents', function (proceed) {
  8594. proceed.apply(this);
  8595. this.batchMSEvents(addEvent);
  8596. });
  8597. // Destroy MS events also
  8598. wrap(Pointer.prototype, 'destroy', function (proceed) {
  8599. this.batchMSEvents(removeEvent);
  8600. proceed.call(this);
  8601. });
  8602. }
  8603. /**
  8604. * The overview of the chart's series
  8605. */
  8606. var Legend = Highcharts.Legend = function (chart, options) {
  8607. this.init(chart, options);
  8608. };
  8609. Legend.prototype = {
  8610. /**
  8611. * Initialize the legend
  8612. */
  8613. init: function (chart, options) {
  8614. var legend = this,
  8615. itemStyle = options.itemStyle,
  8616. padding = pick(options.padding, 8),
  8617. itemMarginTop = options.itemMarginTop || 0;
  8618. this.options = options;
  8619. if (!options.enabled) {
  8620. return;
  8621. }
  8622. legend.baseline = pInt(itemStyle.fontSize) + 3 + itemMarginTop; // used in Series prototype
  8623. legend.itemStyle = itemStyle;
  8624. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  8625. legend.itemMarginTop = itemMarginTop;
  8626. legend.padding = padding;
  8627. legend.initialItemX = padding;
  8628. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  8629. legend.maxItemWidth = 0;
  8630. legend.chart = chart;
  8631. legend.itemHeight = 0;
  8632. legend.lastLineHeight = 0;
  8633. legend.symbolWidth = pick(options.symbolWidth, 16);
  8634. legend.pages = [];
  8635. // Render it
  8636. legend.render();
  8637. // move checkboxes
  8638. addEvent(legend.chart, 'endResize', function () {
  8639. legend.positionCheckboxes();
  8640. });
  8641. },
  8642. /**
  8643. * Set the colors for the legend item
  8644. * @param {Object} item A Series or Point instance
  8645. * @param {Object} visible Dimmed or colored
  8646. */
  8647. colorizeItem: function (item, visible) {
  8648. var legend = this,
  8649. options = legend.options,
  8650. legendItem = item.legendItem,
  8651. legendLine = item.legendLine,
  8652. legendSymbol = item.legendSymbol,
  8653. hiddenColor = legend.itemHiddenStyle.color,
  8654. textColor = visible ? options.itemStyle.color : hiddenColor,
  8655. symbolColor = visible ? (item.legendColor || item.color || '#CCC') : hiddenColor,
  8656. markerOptions = item.options && item.options.marker,
  8657. symbolAttr = {
  8658. stroke: symbolColor,
  8659. fill: symbolColor
  8660. },
  8661. key,
  8662. val;
  8663. if (legendItem) {
  8664. legendItem.css({ fill: textColor, color: textColor }); // color for #1553, oldIE
  8665. }
  8666. if (legendLine) {
  8667. legendLine.attr({ stroke: symbolColor });
  8668. }
  8669. if (legendSymbol) {
  8670. // Apply marker options
  8671. if (markerOptions && legendSymbol.isMarker) { // #585
  8672. markerOptions = item.convertAttribs(markerOptions);
  8673. for (key in markerOptions) {
  8674. val = markerOptions[key];
  8675. if (val !== UNDEFINED) {
  8676. symbolAttr[key] = val;
  8677. }
  8678. }
  8679. }
  8680. legendSymbol.attr(symbolAttr);
  8681. }
  8682. },
  8683. /**
  8684. * Position the legend item
  8685. * @param {Object} item A Series or Point instance
  8686. */
  8687. positionItem: function (item) {
  8688. var legend = this,
  8689. options = legend.options,
  8690. symbolPadding = options.symbolPadding,
  8691. ltr = !options.rtl,
  8692. legendItemPos = item._legendItemPos,
  8693. itemX = legendItemPos[0],
  8694. itemY = legendItemPos[1],
  8695. checkbox = item.checkbox;
  8696. if (item.legendGroup) {
  8697. item.legendGroup.translate(
  8698. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  8699. itemY
  8700. );
  8701. }
  8702. if (checkbox) {
  8703. checkbox.x = itemX;
  8704. checkbox.y = itemY;
  8705. }
  8706. },
  8707. /**
  8708. * Destroy a single legend item
  8709. * @param {Object} item The series or point
  8710. */
  8711. destroyItem: function (item) {
  8712. var checkbox = item.checkbox;
  8713. // destroy SVG elements
  8714. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  8715. if (item[key]) {
  8716. item[key] = item[key].destroy();
  8717. }
  8718. });
  8719. if (checkbox) {
  8720. discardElement(item.checkbox);
  8721. }
  8722. },
  8723. /**
  8724. * Destroys the legend.
  8725. */
  8726. destroy: function () {
  8727. var legend = this,
  8728. legendGroup = legend.group,
  8729. box = legend.box;
  8730. if (box) {
  8731. legend.box = box.destroy();
  8732. }
  8733. if (legendGroup) {
  8734. legend.group = legendGroup.destroy();
  8735. }
  8736. },
  8737. /**
  8738. * Position the checkboxes after the width is determined
  8739. */
  8740. positionCheckboxes: function (scrollOffset) {
  8741. var alignAttr = this.group.alignAttr,
  8742. translateY,
  8743. clipHeight = this.clipHeight || this.legendHeight;
  8744. if (alignAttr) {
  8745. translateY = alignAttr.translateY;
  8746. each(this.allItems, function (item) {
  8747. var checkbox = item.checkbox,
  8748. top;
  8749. if (checkbox) {
  8750. top = (translateY + checkbox.y + (scrollOffset || 0) + 3);
  8751. css(checkbox, {
  8752. left: (alignAttr.translateX + item.legendItemWidth + checkbox.x - 20) + PX,
  8753. top: top + PX,
  8754. display: top > translateY - 6 && top < translateY + clipHeight - 6 ? '' : NONE
  8755. });
  8756. }
  8757. });
  8758. }
  8759. },
  8760. /**
  8761. * Render the legend title on top of the legend
  8762. */
  8763. renderTitle: function () {
  8764. var options = this.options,
  8765. padding = this.padding,
  8766. titleOptions = options.title,
  8767. titleHeight = 0,
  8768. bBox;
  8769. if (titleOptions.text) {
  8770. if (!this.title) {
  8771. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, null, null, 'legend-title')
  8772. .attr({ zIndex: 1 })
  8773. .css(titleOptions.style)
  8774. .add(this.group);
  8775. }
  8776. bBox = this.title.getBBox();
  8777. titleHeight = bBox.height;
  8778. this.offsetWidth = bBox.width; // #1717
  8779. this.contentGroup.attr({ translateY: titleHeight });
  8780. }
  8781. this.titleHeight = titleHeight;
  8782. },
  8783. /**
  8784. * Render a single specific legend item
  8785. * @param {Object} item A series or point
  8786. */
  8787. renderItem: function (item) {
  8788. var legend = this,
  8789. chart = legend.chart,
  8790. renderer = chart.renderer,
  8791. options = legend.options,
  8792. horizontal = options.layout === 'horizontal',
  8793. symbolWidth = legend.symbolWidth,
  8794. symbolPadding = options.symbolPadding,
  8795. itemStyle = legend.itemStyle,
  8796. itemHiddenStyle = legend.itemHiddenStyle,
  8797. padding = legend.padding,
  8798. itemDistance = horizontal ? pick(options.itemDistance, 8) : 0,
  8799. ltr = !options.rtl,
  8800. itemHeight,
  8801. widthOption = options.width,
  8802. itemMarginBottom = options.itemMarginBottom || 0,
  8803. itemMarginTop = legend.itemMarginTop,
  8804. initialItemX = legend.initialItemX,
  8805. bBox,
  8806. itemWidth,
  8807. li = item.legendItem,
  8808. series = item.series && item.series.drawLegendSymbol ? item.series : item,
  8809. seriesOptions = series.options,
  8810. showCheckbox = legend.createCheckboxForItem && seriesOptions && seriesOptions.showCheckbox,
  8811. useHTML = options.useHTML;
  8812. if (!li) { // generate it once, later move it
  8813. // Generate the group box
  8814. // A group to hold the symbol and text. Text is to be appended in Legend class.
  8815. item.legendGroup = renderer.g('legend-item')
  8816. .attr({ zIndex: 1 })
  8817. .add(legend.scrollGroup);
  8818. // Draw the legend symbol inside the group box
  8819. series.drawLegendSymbol(legend, item);
  8820. // Generate the list item text and add it to the group
  8821. item.legendItem = li = renderer.text(
  8822. options.labelFormat ? format(options.labelFormat, item) : options.labelFormatter.call(item),
  8823. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  8824. legend.baseline,
  8825. useHTML
  8826. )
  8827. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  8828. .attr({
  8829. align: ltr ? 'left' : 'right',
  8830. zIndex: 2
  8831. })
  8832. .add(item.legendGroup);
  8833. if (legend.setItemEvents) {
  8834. legend.setItemEvents(item, li, useHTML, itemStyle, itemHiddenStyle);
  8835. }
  8836. // Colorize the items
  8837. legend.colorizeItem(item, item.visible);
  8838. // add the HTML checkbox on top
  8839. if (showCheckbox) {
  8840. legend.createCheckboxForItem(item);
  8841. }
  8842. }
  8843. // calculate the positions for the next line
  8844. bBox = li.getBBox();
  8845. itemWidth = item.legendItemWidth =
  8846. options.itemWidth || item.legendItemWidth || symbolWidth + symbolPadding + bBox.width + itemDistance +
  8847. (showCheckbox ? 20 : 0);
  8848. legend.itemHeight = itemHeight = mathRound(item.legendItemHeight || bBox.height);
  8849. // if the item exceeds the width, start a new line
  8850. if (horizontal && legend.itemX - initialItemX + itemWidth >
  8851. (widthOption || (chart.chartWidth - 2 * padding - initialItemX - options.x))) {
  8852. legend.itemX = initialItemX;
  8853. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  8854. legend.lastLineHeight = 0; // reset for next line
  8855. }
  8856. // If the item exceeds the height, start a new column
  8857. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  8858. legend.itemY = legend.initialItemY;
  8859. legend.itemX += legend.maxItemWidth;
  8860. legend.maxItemWidth = 0;
  8861. }*/
  8862. // Set the edge positions
  8863. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  8864. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  8865. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  8866. // cache the position of the newly generated or reordered items
  8867. item._legendItemPos = [legend.itemX, legend.itemY];
  8868. // advance
  8869. if (horizontal) {
  8870. legend.itemX += itemWidth;
  8871. } else {
  8872. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  8873. legend.lastLineHeight = itemHeight;
  8874. }
  8875. // the width of the widest item
  8876. legend.offsetWidth = widthOption || mathMax(
  8877. (horizontal ? legend.itemX - initialItemX - itemDistance : itemWidth) + padding,
  8878. legend.offsetWidth
  8879. );
  8880. },
  8881. /**
  8882. * Get all items, which is one item per series for normal series and one item per point
  8883. * for pie series.
  8884. */
  8885. getAllItems: function () {
  8886. var allItems = [];
  8887. each(this.chart.series, function (series) {
  8888. var seriesOptions = series.options;
  8889. // Handle showInLegend. If the series is linked to another series, defaults to false.
  8890. if (!pick(seriesOptions.showInLegend, !defined(seriesOptions.linkedTo) ? UNDEFINED : false, true)) {
  8891. return;
  8892. }
  8893. // use points or series for the legend item depending on legendType
  8894. allItems = allItems.concat(
  8895. series.legendItems ||
  8896. (seriesOptions.legendType === 'point' ?
  8897. series.data :
  8898. series)
  8899. );
  8900. });
  8901. return allItems;
  8902. },
  8903. /**
  8904. * Render the legend. This method can be called both before and after
  8905. * chart.render. If called after, it will only rearrange items instead
  8906. * of creating new ones.
  8907. */
  8908. render: function () {
  8909. var legend = this,
  8910. chart = legend.chart,
  8911. renderer = chart.renderer,
  8912. legendGroup = legend.group,
  8913. allItems,
  8914. display,
  8915. legendWidth,
  8916. legendHeight,
  8917. box = legend.box,
  8918. options = legend.options,
  8919. padding = legend.padding,
  8920. legendBorderWidth = options.borderWidth,
  8921. legendBackgroundColor = options.backgroundColor;
  8922. legend.itemX = legend.initialItemX;
  8923. legend.itemY = legend.initialItemY;
  8924. legend.offsetWidth = 0;
  8925. legend.lastItemY = 0;
  8926. if (!legendGroup) {
  8927. legend.group = legendGroup = renderer.g('legend')
  8928. .attr({ zIndex: 7 })
  8929. .add();
  8930. legend.contentGroup = renderer.g()
  8931. .attr({ zIndex: 1 }) // above background
  8932. .add(legendGroup);
  8933. legend.scrollGroup = renderer.g()
  8934. .add(legend.contentGroup);
  8935. }
  8936. legend.renderTitle();
  8937. // add each series or point
  8938. allItems = legend.getAllItems();
  8939. // sort by legendIndex
  8940. stableSort(allItems, function (a, b) {
  8941. return ((a.options && a.options.legendIndex) || 0) - ((b.options && b.options.legendIndex) || 0);
  8942. });
  8943. // reversed legend
  8944. if (options.reversed) {
  8945. allItems.reverse();
  8946. }
  8947. legend.allItems = allItems;
  8948. legend.display = display = !!allItems.length;
  8949. // render the items
  8950. each(allItems, function (item) {
  8951. legend.renderItem(item);
  8952. });
  8953. // Draw the border
  8954. legendWidth = options.width || legend.offsetWidth;
  8955. legendHeight = legend.lastItemY + legend.lastLineHeight + legend.titleHeight;
  8956. legendHeight = legend.handleOverflow(legendHeight);
  8957. if (legendBorderWidth || legendBackgroundColor) {
  8958. legendWidth += padding;
  8959. legendHeight += padding;
  8960. if (!box) {
  8961. legend.box = box = renderer.rect(
  8962. 0,
  8963. 0,
  8964. legendWidth,
  8965. legendHeight,
  8966. options.borderRadius,
  8967. legendBorderWidth || 0
  8968. ).attr({
  8969. stroke: options.borderColor,
  8970. 'stroke-width': legendBorderWidth || 0,
  8971. fill: legendBackgroundColor || NONE
  8972. })
  8973. .add(legendGroup)
  8974. .shadow(options.shadow);
  8975. box.isNew = true;
  8976. } else if (legendWidth > 0 && legendHeight > 0) {
  8977. box[box.isNew ? 'attr' : 'animate'](
  8978. box.crisp({ width: legendWidth, height: legendHeight })
  8979. );
  8980. box.isNew = false;
  8981. }
  8982. // hide the border if no items
  8983. box[display ? 'show' : 'hide']();
  8984. }
  8985. legend.legendWidth = legendWidth;
  8986. legend.legendHeight = legendHeight;
  8987. // Now that the legend width and height are established, put the items in the
  8988. // final position
  8989. each(allItems, function (item) {
  8990. legend.positionItem(item);
  8991. });
  8992. // 1.x compatibility: positioning based on style
  8993. /*var props = ['left', 'right', 'top', 'bottom'],
  8994. prop,
  8995. i = 4;
  8996. while (i--) {
  8997. prop = props[i];
  8998. if (options.style[prop] && options.style[prop] !== 'auto') {
  8999. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  9000. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  9001. }
  9002. }*/
  9003. if (display) {
  9004. legendGroup.align(extend({
  9005. width: legendWidth,
  9006. height: legendHeight
  9007. }, options), true, 'spacingBox');
  9008. }
  9009. if (!chart.isResizing) {
  9010. this.positionCheckboxes();
  9011. }
  9012. },
  9013. /**
  9014. * Set up the overflow handling by adding navigation with up and down arrows below the
  9015. * legend.
  9016. */
  9017. handleOverflow: function (legendHeight) {
  9018. var legend = this,
  9019. chart = this.chart,
  9020. renderer = chart.renderer,
  9021. options = this.options,
  9022. optionsY = options.y,
  9023. alignTop = options.verticalAlign === 'top',
  9024. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  9025. maxHeight = options.maxHeight,
  9026. clipHeight,
  9027. clipRect = this.clipRect,
  9028. navOptions = options.navigation,
  9029. animation = pick(navOptions.animation, true),
  9030. arrowSize = navOptions.arrowSize || 12,
  9031. nav = this.nav,
  9032. pages = this.pages,
  9033. lastY,
  9034. allItems = this.allItems;
  9035. // Adjust the height
  9036. if (options.layout === 'horizontal') {
  9037. spaceHeight /= 2;
  9038. }
  9039. if (maxHeight) {
  9040. spaceHeight = mathMin(spaceHeight, maxHeight);
  9041. }
  9042. // Reset the legend height and adjust the clipping rectangle
  9043. pages.length = 0;
  9044. if (legendHeight > spaceHeight && !options.useHTML) {
  9045. this.clipHeight = clipHeight = spaceHeight - 20 - this.titleHeight - this.padding;
  9046. this.currentPage = pick(this.currentPage, 1);
  9047. this.fullHeight = legendHeight;
  9048. // Fill pages with Y positions so that the top of each a legend item defines
  9049. // the scroll top for each page (#2098)
  9050. each(allItems, function (item, i) {
  9051. var y = item._legendItemPos[1],
  9052. h = mathRound(item.legendItem.getBBox().height),
  9053. len = pages.length;
  9054. if (!len || (y - pages[len - 1] > clipHeight && (lastY || y) !== pages[len - 1])) {
  9055. pages.push(lastY || y);
  9056. len++;
  9057. }
  9058. if (i === allItems.length - 1 && y + h - pages[len - 1] > clipHeight) {
  9059. pages.push(y);
  9060. }
  9061. if (y !== lastY) {
  9062. lastY = y;
  9063. }
  9064. });
  9065. // Only apply clipping if needed. Clipping causes blurred legend in PDF export (#1787)
  9066. if (!clipRect) {
  9067. clipRect = legend.clipRect = renderer.clipRect(0, this.padding, 9999, 0);
  9068. legend.contentGroup.clip(clipRect);
  9069. }
  9070. clipRect.attr({
  9071. height: clipHeight
  9072. });
  9073. // Add navigation elements
  9074. if (!nav) {
  9075. this.nav = nav = renderer.g().attr({ zIndex: 1 }).add(this.group);
  9076. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  9077. .on('click', function () {
  9078. legend.scroll(-1, animation);
  9079. })
  9080. .add(nav);
  9081. this.pager = renderer.text('', 15, 10)
  9082. .css(navOptions.style)
  9083. .add(nav);
  9084. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  9085. .on('click', function () {
  9086. legend.scroll(1, animation);
  9087. })
  9088. .add(nav);
  9089. }
  9090. // Set initial position
  9091. legend.scroll(0);
  9092. legendHeight = spaceHeight;
  9093. } else if (nav) {
  9094. clipRect.attr({
  9095. height: chart.chartHeight
  9096. });
  9097. nav.hide();
  9098. this.scrollGroup.attr({
  9099. translateY: 1
  9100. });
  9101. this.clipHeight = 0; // #1379
  9102. }
  9103. return legendHeight;
  9104. },
  9105. /**
  9106. * Scroll the legend by a number of pages
  9107. * @param {Object} scrollBy
  9108. * @param {Object} animation
  9109. */
  9110. scroll: function (scrollBy, animation) {
  9111. var pages = this.pages,
  9112. pageCount = pages.length,
  9113. currentPage = this.currentPage + scrollBy,
  9114. clipHeight = this.clipHeight,
  9115. navOptions = this.options.navigation,
  9116. activeColor = navOptions.activeColor,
  9117. inactiveColor = navOptions.inactiveColor,
  9118. pager = this.pager,
  9119. padding = this.padding,
  9120. scrollOffset;
  9121. // When resizing while looking at the last page
  9122. if (currentPage > pageCount) {
  9123. currentPage = pageCount;
  9124. }
  9125. if (currentPage > 0) {
  9126. if (animation !== UNDEFINED) {
  9127. setAnimation(animation, this.chart);
  9128. }
  9129. this.nav.attr({
  9130. translateX: padding,
  9131. translateY: clipHeight + this.padding + 7 + this.titleHeight,
  9132. visibility: VISIBLE
  9133. });
  9134. this.up.attr({
  9135. fill: currentPage === 1 ? inactiveColor : activeColor
  9136. })
  9137. .css({
  9138. cursor: currentPage === 1 ? 'default' : 'pointer'
  9139. });
  9140. pager.attr({
  9141. text: currentPage + '/' + pageCount
  9142. });
  9143. this.down.attr({
  9144. x: 18 + this.pager.getBBox().width, // adjust to text width
  9145. fill: currentPage === pageCount ? inactiveColor : activeColor
  9146. })
  9147. .css({
  9148. cursor: currentPage === pageCount ? 'default' : 'pointer'
  9149. });
  9150. scrollOffset = -pages[currentPage - 1] + this.initialItemY;
  9151. this.scrollGroup.animate({
  9152. translateY: scrollOffset
  9153. });
  9154. this.currentPage = currentPage;
  9155. this.positionCheckboxes(scrollOffset);
  9156. }
  9157. }
  9158. };
  9159. /*
  9160. * LegendSymbolMixin
  9161. */
  9162. var LegendSymbolMixin = Highcharts.LegendSymbolMixin = {
  9163. /**
  9164. * Get the series' symbol in the legend
  9165. *
  9166. * @param {Object} legend The legend object
  9167. * @param {Object} item The series (this) or point
  9168. */
  9169. drawRectangle: function (legend, item) {
  9170. var symbolHeight = legend.options.symbolHeight || 12;
  9171. item.legendSymbol = this.chart.renderer.rect(
  9172. 0,
  9173. legend.baseline - 5 - (symbolHeight / 2),
  9174. legend.symbolWidth,
  9175. symbolHeight,
  9176. pick(legend.options.symbolRadius, 2)
  9177. ).attr({
  9178. zIndex: 3
  9179. }).add(item.legendGroup);
  9180. },
  9181. /**
  9182. * Get the series' symbol in the legend. This method should be overridable to create custom
  9183. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  9184. *
  9185. * @param {Object} legend The legend object
  9186. */
  9187. drawLineMarker: function (legend) {
  9188. var options = this.options,
  9189. markerOptions = options.marker,
  9190. radius,
  9191. legendOptions = legend.options,
  9192. legendSymbol,
  9193. symbolWidth = legend.symbolWidth,
  9194. renderer = this.chart.renderer,
  9195. legendItemGroup = this.legendGroup,
  9196. verticalCenter = legend.baseline - mathRound(renderer.fontMetrics(legendOptions.itemStyle.fontSize).b * 0.3),
  9197. attr;
  9198. // Draw the line
  9199. if (options.lineWidth) {
  9200. attr = {
  9201. 'stroke-width': options.lineWidth
  9202. };
  9203. if (options.dashStyle) {
  9204. attr.dashstyle = options.dashStyle;
  9205. }
  9206. this.legendLine = renderer.path([
  9207. M,
  9208. 0,
  9209. verticalCenter,
  9210. L,
  9211. symbolWidth,
  9212. verticalCenter
  9213. ])
  9214. .attr(attr)
  9215. .add(legendItemGroup);
  9216. }
  9217. // Draw the marker
  9218. if (markerOptions && markerOptions.enabled) {
  9219. radius = markerOptions.radius;
  9220. this.legendSymbol = legendSymbol = renderer.symbol(
  9221. this.symbol,
  9222. (symbolWidth / 2) - radius,
  9223. verticalCenter - radius,
  9224. 2 * radius,
  9225. 2 * radius
  9226. )
  9227. .add(legendItemGroup);
  9228. legendSymbol.isMarker = true;
  9229. }
  9230. }
  9231. };
  9232. // Workaround for #2030, horizontal legend items not displaying in IE11 Preview,
  9233. // and for #2580, a similar drawing flaw in Firefox 26.
  9234. // TODO: Explore if there's a general cause for this. The problem may be related
  9235. // to nested group elements, as the legend item texts are within 4 group elements.
  9236. if (/Trident\/7\.0/.test(userAgent) || isFirefox) {
  9237. wrap(Legend.prototype, 'positionItem', function (proceed, item) {
  9238. var legend = this,
  9239. runPositionItem = function () { // If chart destroyed in sync, this is undefined (#2030)
  9240. if (item._legendItemPos) {
  9241. proceed.call(legend, item);
  9242. }
  9243. };
  9244. if (legend.chart.renderer.forExport) {
  9245. runPositionItem();
  9246. } else {
  9247. setTimeout(runPositionItem);
  9248. }
  9249. });
  9250. }
  9251. /**
  9252. * The chart class
  9253. * @param {Object} options
  9254. * @param {Function} callback Function to run when the chart has loaded
  9255. */
  9256. function Chart() {
  9257. this.init.apply(this, arguments);
  9258. }
  9259. Chart.prototype = {
  9260. /**
  9261. * Initialize the chart
  9262. */
  9263. init: function (userOptions, callback) {
  9264. // Handle regular options
  9265. var options,
  9266. seriesOptions = userOptions.series; // skip merging data points to increase performance
  9267. userOptions.series = null;
  9268. options = merge(defaultOptions, userOptions); // do the merge
  9269. options.series = userOptions.series = seriesOptions; // set back the series data
  9270. this.userOptions = userOptions;
  9271. var optionsChart = options.chart;
  9272. // Create margin & spacing array
  9273. this.margin = this.splashArray('margin', optionsChart);
  9274. this.spacing = this.splashArray('spacing', optionsChart);
  9275. var chartEvents = optionsChart.events;
  9276. //this.runChartClick = chartEvents && !!chartEvents.click;
  9277. this.bounds = { h: {}, v: {} }; // Pixel data bounds for touch zoom
  9278. this.callback = callback;
  9279. this.isResizing = 0;
  9280. this.options = options;
  9281. //chartTitleOptions = UNDEFINED;
  9282. //chartSubtitleOptions = UNDEFINED;
  9283. this.axes = [];
  9284. this.series = [];
  9285. this.hasCartesianSeries = optionsChart.showAxes;
  9286. //this.axisOffset = UNDEFINED;
  9287. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  9288. //this.inverted = UNDEFINED;
  9289. //this.loadingShown = UNDEFINED;
  9290. //this.container = UNDEFINED;
  9291. //this.chartWidth = UNDEFINED;
  9292. //this.chartHeight = UNDEFINED;
  9293. //this.marginRight = UNDEFINED;
  9294. //this.marginBottom = UNDEFINED;
  9295. //this.containerWidth = UNDEFINED;
  9296. //this.containerHeight = UNDEFINED;
  9297. //this.oldChartWidth = UNDEFINED;
  9298. //this.oldChartHeight = UNDEFINED;
  9299. //this.renderTo = UNDEFINED;
  9300. //this.renderToClone = UNDEFINED;
  9301. //this.spacingBox = UNDEFINED
  9302. //this.legend = UNDEFINED;
  9303. // Elements
  9304. //this.chartBackground = UNDEFINED;
  9305. //this.plotBackground = UNDEFINED;
  9306. //this.plotBGImage = UNDEFINED;
  9307. //this.plotBorder = UNDEFINED;
  9308. //this.loadingDiv = UNDEFINED;
  9309. //this.loadingSpan = UNDEFINED;
  9310. var chart = this,
  9311. eventType;
  9312. // Add the chart to the global lookup
  9313. chart.index = charts.length;
  9314. charts.push(chart);
  9315. // Set up auto resize
  9316. if (optionsChart.reflow !== false) {
  9317. addEvent(chart, 'load', function () {
  9318. chart.initReflow();
  9319. });
  9320. }
  9321. // Chart event handlers
  9322. if (chartEvents) {
  9323. for (eventType in chartEvents) {
  9324. addEvent(chart, eventType, chartEvents[eventType]);
  9325. }
  9326. }
  9327. chart.xAxis = [];
  9328. chart.yAxis = [];
  9329. // Expose methods and variables
  9330. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  9331. chart.pointCount = 0;
  9332. chart.counters = new ChartCounters();
  9333. chart.firstRender();
  9334. },
  9335. /**
  9336. * Initialize an individual series, called internally before render time
  9337. */
  9338. initSeries: function (options) {
  9339. var chart = this,
  9340. optionsChart = chart.options.chart,
  9341. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  9342. series,
  9343. constr = seriesTypes[type];
  9344. // No such series type
  9345. if (!constr) {
  9346. error(17, true);
  9347. }
  9348. series = new constr();
  9349. series.init(this, options);
  9350. return series;
  9351. },
  9352. /**
  9353. * Check whether a given point is within the plot area
  9354. *
  9355. * @param {Number} plotX Pixel x relative to the plot area
  9356. * @param {Number} plotY Pixel y relative to the plot area
  9357. * @param {Boolean} inverted Whether the chart is inverted
  9358. */
  9359. isInsidePlot: function (plotX, plotY, inverted) {
  9360. var x = inverted ? plotY : plotX,
  9361. y = inverted ? plotX : plotY;
  9362. return x >= 0 &&
  9363. x <= this.plotWidth &&
  9364. y >= 0 &&
  9365. y <= this.plotHeight;
  9366. },
  9367. /**
  9368. * Adjust all axes tick amounts
  9369. */
  9370. adjustTickAmounts: function () {
  9371. if (this.options.chart.alignTicks !== false) {
  9372. each(this.axes, function (axis) {
  9373. axis.adjustTickAmount();
  9374. });
  9375. }
  9376. this.maxTicks = null;
  9377. },
  9378. /**
  9379. * Redraw legend, axes or series based on updated data
  9380. *
  9381. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9382. * configuration
  9383. */
  9384. redraw: function (animation) {
  9385. var chart = this,
  9386. axes = chart.axes,
  9387. series = chart.series,
  9388. pointer = chart.pointer,
  9389. legend = chart.legend,
  9390. redrawLegend = chart.isDirtyLegend,
  9391. hasStackedSeries,
  9392. hasDirtyStacks,
  9393. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  9394. seriesLength = series.length,
  9395. i = seriesLength,
  9396. serie,
  9397. renderer = chart.renderer,
  9398. isHiddenChart = renderer.isHidden(),
  9399. afterRedraw = [];
  9400. setAnimation(animation, chart);
  9401. if (isHiddenChart) {
  9402. chart.cloneRenderTo();
  9403. }
  9404. // Adjust title layout (reflow multiline text)
  9405. chart.layOutTitles();
  9406. // link stacked series
  9407. while (i--) {
  9408. serie = series[i];
  9409. if (serie.options.stacking) {
  9410. hasStackedSeries = true;
  9411. if (serie.isDirty) {
  9412. hasDirtyStacks = true;
  9413. break;
  9414. }
  9415. }
  9416. }
  9417. if (hasDirtyStacks) { // mark others as dirty
  9418. i = seriesLength;
  9419. while (i--) {
  9420. serie = series[i];
  9421. if (serie.options.stacking) {
  9422. serie.isDirty = true;
  9423. }
  9424. }
  9425. }
  9426. // handle updated data in the series
  9427. each(series, function (serie) {
  9428. if (serie.isDirty) { // prepare the data so axis can read it
  9429. if (serie.options.legendType === 'point') {
  9430. redrawLegend = true;
  9431. }
  9432. }
  9433. });
  9434. // handle added or removed series
  9435. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  9436. // draw legend graphics
  9437. legend.render();
  9438. chart.isDirtyLegend = false;
  9439. }
  9440. // reset stacks
  9441. if (hasStackedSeries) {
  9442. chart.getStacks();
  9443. }
  9444. if (chart.hasCartesianSeries) {
  9445. if (!chart.isResizing) {
  9446. // reset maxTicks
  9447. chart.maxTicks = null;
  9448. // set axes scales
  9449. each(axes, function (axis) {
  9450. axis.setScale();
  9451. });
  9452. }
  9453. chart.adjustTickAmounts();
  9454. chart.getMargins();
  9455. // If one axis is dirty, all axes must be redrawn (#792, #2169)
  9456. each(axes, function (axis) {
  9457. if (axis.isDirty) {
  9458. isDirtyBox = true;
  9459. }
  9460. });
  9461. // redraw axes
  9462. each(axes, function (axis) {
  9463. // Fire 'afterSetExtremes' only if extremes are set
  9464. if (axis.isDirtyExtremes) { // #821
  9465. axis.isDirtyExtremes = false;
  9466. afterRedraw.push(function () { // prevent a recursive call to chart.redraw() (#1119)
  9467. fireEvent(axis, 'afterSetExtremes', extend(axis.eventArgs, axis.getExtremes())); // #747, #751
  9468. delete axis.eventArgs;
  9469. });
  9470. }
  9471. if (isDirtyBox || hasStackedSeries) {
  9472. axis.redraw();
  9473. }
  9474. });
  9475. }
  9476. // the plot areas size has changed
  9477. if (isDirtyBox) {
  9478. chart.drawChartBox();
  9479. }
  9480. // redraw affected series
  9481. each(series, function (serie) {
  9482. if (serie.isDirty && serie.visible &&
  9483. (!serie.isCartesian || serie.xAxis)) { // issue #153
  9484. serie.redraw();
  9485. }
  9486. });
  9487. // move tooltip or reset
  9488. if (pointer) {
  9489. pointer.reset(true);
  9490. }
  9491. // redraw if canvas
  9492. renderer.draw();
  9493. // fire the event
  9494. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  9495. if (isHiddenChart) {
  9496. chart.cloneRenderTo(true);
  9497. }
  9498. // Fire callbacks that are put on hold until after the redraw
  9499. each(afterRedraw, function (callback) {
  9500. callback.call();
  9501. });
  9502. },
  9503. /**
  9504. * Get an axis, series or point object by id.
  9505. * @param id {String} The id as given in the configuration options
  9506. */
  9507. get: function (id) {
  9508. var chart = this,
  9509. axes = chart.axes,
  9510. series = chart.series;
  9511. var i,
  9512. j,
  9513. points;
  9514. // search axes
  9515. for (i = 0; i < axes.length; i++) {
  9516. if (axes[i].options.id === id) {
  9517. return axes[i];
  9518. }
  9519. }
  9520. // search series
  9521. for (i = 0; i < series.length; i++) {
  9522. if (series[i].options.id === id) {
  9523. return series[i];
  9524. }
  9525. }
  9526. // search points
  9527. for (i = 0; i < series.length; i++) {
  9528. points = series[i].points || [];
  9529. for (j = 0; j < points.length; j++) {
  9530. if (points[j].id === id) {
  9531. return points[j];
  9532. }
  9533. }
  9534. }
  9535. return null;
  9536. },
  9537. /**
  9538. * Create the Axis instances based on the config options
  9539. */
  9540. getAxes: function () {
  9541. var chart = this,
  9542. options = this.options,
  9543. xAxisOptions = options.xAxis = splat(options.xAxis || {}),
  9544. yAxisOptions = options.yAxis = splat(options.yAxis || {}),
  9545. optionsArray,
  9546. axis;
  9547. // make sure the options are arrays and add some members
  9548. each(xAxisOptions, function (axis, i) {
  9549. axis.index = i;
  9550. axis.isX = true;
  9551. });
  9552. each(yAxisOptions, function (axis, i) {
  9553. axis.index = i;
  9554. });
  9555. // concatenate all axis options into one array
  9556. optionsArray = xAxisOptions.concat(yAxisOptions);
  9557. each(optionsArray, function (axisOptions) {
  9558. axis = new Axis(chart, axisOptions);
  9559. });
  9560. chart.adjustTickAmounts();
  9561. },
  9562. /**
  9563. * Get the currently selected points from all series
  9564. */
  9565. getSelectedPoints: function () {
  9566. var points = [];
  9567. each(this.series, function (serie) {
  9568. points = points.concat(grep(serie.points || [], function (point) {
  9569. return point.selected;
  9570. }));
  9571. });
  9572. return points;
  9573. },
  9574. /**
  9575. * Get the currently selected series
  9576. */
  9577. getSelectedSeries: function () {
  9578. return grep(this.series, function (serie) {
  9579. return serie.selected;
  9580. });
  9581. },
  9582. /**
  9583. * Generate stacks for each series and calculate stacks total values
  9584. */
  9585. getStacks: function () {
  9586. var chart = this;
  9587. // reset stacks for each yAxis
  9588. each(chart.yAxis, function (axis) {
  9589. if (axis.stacks && axis.hasVisibleSeries) {
  9590. axis.oldStacks = axis.stacks;
  9591. }
  9592. });
  9593. each(chart.series, function (series) {
  9594. if (series.options.stacking && (series.visible === true || chart.options.chart.ignoreHiddenSeries === false)) {
  9595. series.stackKey = series.type + pick(series.options.stack, '');
  9596. }
  9597. });
  9598. },
  9599. /**
  9600. * Show the title and subtitle of the chart
  9601. *
  9602. * @param titleOptions {Object} New title options
  9603. * @param subtitleOptions {Object} New subtitle options
  9604. *
  9605. */
  9606. setTitle: function (titleOptions, subtitleOptions, redraw) {
  9607. var chart = this,
  9608. options = chart.options,
  9609. chartTitleOptions,
  9610. chartSubtitleOptions;
  9611. chartTitleOptions = options.title = merge(options.title, titleOptions);
  9612. chartSubtitleOptions = options.subtitle = merge(options.subtitle, subtitleOptions);
  9613. // add title and subtitle
  9614. each([
  9615. ['title', titleOptions, chartTitleOptions],
  9616. ['subtitle', subtitleOptions, chartSubtitleOptions]
  9617. ], function (arr) {
  9618. var name = arr[0],
  9619. title = chart[name],
  9620. titleOptions = arr[1],
  9621. chartTitleOptions = arr[2];
  9622. if (title && titleOptions) {
  9623. chart[name] = title = title.destroy(); // remove old
  9624. }
  9625. if (chartTitleOptions && chartTitleOptions.text && !title) {
  9626. chart[name] = chart.renderer.text(
  9627. chartTitleOptions.text,
  9628. 0,
  9629. 0,
  9630. chartTitleOptions.useHTML
  9631. )
  9632. .attr({
  9633. align: chartTitleOptions.align,
  9634. 'class': PREFIX + name,
  9635. zIndex: chartTitleOptions.zIndex || 4
  9636. })
  9637. .css(chartTitleOptions.style)
  9638. .add();
  9639. }
  9640. });
  9641. chart.layOutTitles(redraw);
  9642. },
  9643. /**
  9644. * Lay out the chart titles and cache the full offset height for use in getMargins
  9645. */
  9646. layOutTitles: function (redraw) {
  9647. var titleOffset = 0,
  9648. title = this.title,
  9649. subtitle = this.subtitle,
  9650. options = this.options,
  9651. titleOptions = options.title,
  9652. subtitleOptions = options.subtitle,
  9653. requiresDirtyBox,
  9654. autoWidth = this.spacingBox.width - 44; // 44 makes room for default context button
  9655. if (title) {
  9656. title
  9657. .css({ width: (titleOptions.width || autoWidth) + PX })
  9658. .align(extend({ y: 15 }, titleOptions), false, 'spacingBox');
  9659. if (!titleOptions.floating && !titleOptions.verticalAlign) {
  9660. titleOffset = title.getBBox().height;
  9661. // Adjust for browser consistency + backwards compat after #776 fix
  9662. if (titleOffset >= 18 && titleOffset <= 25) {
  9663. titleOffset = 15;
  9664. }
  9665. }
  9666. }
  9667. if (subtitle) {
  9668. subtitle
  9669. .css({ width: (subtitleOptions.width || autoWidth) + PX })
  9670. .align(extend({ y: titleOffset + titleOptions.margin }, subtitleOptions), false, 'spacingBox');
  9671. if (!subtitleOptions.floating && !subtitleOptions.verticalAlign) {
  9672. titleOffset = mathCeil(titleOffset + subtitle.getBBox().height);
  9673. }
  9674. }
  9675. requiresDirtyBox = this.titleOffset !== titleOffset;
  9676. this.titleOffset = titleOffset; // used in getMargins
  9677. if (!this.isDirtyBox && requiresDirtyBox) {
  9678. this.isDirtyBox = requiresDirtyBox;
  9679. // Redraw if necessary (#2719, #2744)
  9680. if (this.hasRendered && pick(redraw, true) && this.isDirtyBox) {
  9681. this.redraw();
  9682. }
  9683. }
  9684. },
  9685. /**
  9686. * Get chart width and height according to options and container size
  9687. */
  9688. getChartSize: function () {
  9689. var chart = this,
  9690. optionsChart = chart.options.chart,
  9691. widthOption = optionsChart.width,
  9692. heightOption = optionsChart.height,
  9693. renderTo = chart.renderToClone || chart.renderTo;
  9694. // get inner width and height from jQuery (#824)
  9695. if (!defined(widthOption)) {
  9696. chart.containerWidth = adapterRun(renderTo, 'width');
  9697. }
  9698. if (!defined(heightOption)) {
  9699. chart.containerHeight = adapterRun(renderTo, 'height');
  9700. }
  9701. chart.chartWidth = mathMax(0, widthOption || chart.containerWidth || 600); // #1393, 1460
  9702. chart.chartHeight = mathMax(0, pick(heightOption,
  9703. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  9704. chart.containerHeight > 19 ? chart.containerHeight : 400));
  9705. },
  9706. /**
  9707. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  9708. * size computation on chart.render and chart.redraw
  9709. */
  9710. cloneRenderTo: function (revert) {
  9711. var clone = this.renderToClone,
  9712. container = this.container;
  9713. // Destroy the clone and bring the container back to the real renderTo div
  9714. if (revert) {
  9715. if (clone) {
  9716. this.renderTo.appendChild(container);
  9717. discardElement(clone);
  9718. delete this.renderToClone;
  9719. }
  9720. // Set up the clone
  9721. } else {
  9722. if (container && container.parentNode === this.renderTo) {
  9723. this.renderTo.removeChild(container); // do not clone this
  9724. }
  9725. this.renderToClone = clone = this.renderTo.cloneNode(0);
  9726. css(clone, {
  9727. position: ABSOLUTE,
  9728. top: '-9999px',
  9729. display: 'block' // #833
  9730. });
  9731. if (clone.style.setProperty) { // #2631
  9732. clone.style.setProperty('display', 'block', 'important');
  9733. }
  9734. doc.body.appendChild(clone);
  9735. if (container) {
  9736. clone.appendChild(container);
  9737. }
  9738. }
  9739. },
  9740. /**
  9741. * Get the containing element, determine the size and create the inner container
  9742. * div to hold the chart
  9743. */
  9744. getContainer: function () {
  9745. var chart = this,
  9746. container,
  9747. optionsChart = chart.options.chart,
  9748. chartWidth,
  9749. chartHeight,
  9750. renderTo,
  9751. indexAttrName = 'data-highcharts-chart',
  9752. oldChartIndex,
  9753. containerId;
  9754. chart.renderTo = renderTo = optionsChart.renderTo;
  9755. containerId = PREFIX + idCounter++;
  9756. if (isString(renderTo)) {
  9757. chart.renderTo = renderTo = doc.getElementById(renderTo);
  9758. }
  9759. // Display an error if the renderTo is wrong
  9760. if (!renderTo) {
  9761. error(13, true);
  9762. }
  9763. // If the container already holds a chart, destroy it. The check for hasRendered is there
  9764. // because web pages that are saved to disk from the browser, will preserve the data-highcharts-chart
  9765. // attribute and the SVG contents, but not an interactive chart. So in this case,
  9766. // charts[oldChartIndex] will point to the wrong chart if any (#2609).
  9767. oldChartIndex = pInt(attr(renderTo, indexAttrName));
  9768. if (!isNaN(oldChartIndex) && charts[oldChartIndex] && charts[oldChartIndex].hasRendered) {
  9769. charts[oldChartIndex].destroy();
  9770. }
  9771. // Make a reference to the chart from the div
  9772. attr(renderTo, indexAttrName, chart.index);
  9773. // remove previous chart
  9774. renderTo.innerHTML = '';
  9775. // If the container doesn't have an offsetWidth, it has or is a child of a node
  9776. // that has display:none. We need to temporarily move it out to a visible
  9777. // state to determine the size, else the legend and tooltips won't render
  9778. // properly. The allowClone option is used in sparklines as a micro optimization,
  9779. // saving about 1-2 ms each chart.
  9780. if (!optionsChart.skipClone && !renderTo.offsetWidth) {
  9781. chart.cloneRenderTo();
  9782. }
  9783. // get the width and height
  9784. chart.getChartSize();
  9785. chartWidth = chart.chartWidth;
  9786. chartHeight = chart.chartHeight;
  9787. // create the inner container
  9788. chart.container = container = createElement(DIV, {
  9789. className: PREFIX + 'container' +
  9790. (optionsChart.className ? ' ' + optionsChart.className : ''),
  9791. id: containerId
  9792. }, extend({
  9793. position: RELATIVE,
  9794. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  9795. // content overflow in IE
  9796. width: chartWidth + PX,
  9797. height: chartHeight + PX,
  9798. textAlign: 'left',
  9799. lineHeight: 'normal', // #427
  9800. zIndex: 0, // #1072
  9801. '-webkit-tap-highlight-color': 'rgba(0,0,0,0)'
  9802. }, optionsChart.style),
  9803. chart.renderToClone || renderTo
  9804. );
  9805. // cache the cursor (#1650)
  9806. chart._cursor = container.style.cursor;
  9807. // Initialize the renderer
  9808. chart.renderer =
  9809. optionsChart.forExport ? // force SVG, used for SVG export
  9810. new SVGRenderer(container, chartWidth, chartHeight, optionsChart.style, true) :
  9811. new Renderer(container, chartWidth, chartHeight, optionsChart.style);
  9812. if (useCanVG) {
  9813. // If we need canvg library, extend and configure the renderer
  9814. // to get the tracker for translating mouse events
  9815. chart.renderer.create(chart, container, chartWidth, chartHeight);
  9816. }
  9817. },
  9818. /**
  9819. * Calculate margins by rendering axis labels in a preliminary position. Title,
  9820. * subtitle and legend have already been rendered at this stage, but will be
  9821. * moved into their final positions
  9822. */
  9823. getMargins: function () {
  9824. var chart = this,
  9825. spacing = chart.spacing,
  9826. axisOffset,
  9827. legend = chart.legend,
  9828. margin = chart.margin,
  9829. legendOptions = chart.options.legend,
  9830. legendMargin = pick(legendOptions.margin, 10),
  9831. legendX = legendOptions.x,
  9832. legendY = legendOptions.y,
  9833. align = legendOptions.align,
  9834. verticalAlign = legendOptions.verticalAlign,
  9835. titleOffset = chart.titleOffset;
  9836. chart.resetMargins();
  9837. axisOffset = chart.axisOffset;
  9838. // Adjust for title and subtitle
  9839. if (titleOffset && !defined(margin[0])) {
  9840. chart.plotTop = mathMax(chart.plotTop, titleOffset + chart.options.title.margin + spacing[0]);
  9841. }
  9842. // Adjust for legend
  9843. if (legend.display && !legendOptions.floating) {
  9844. if (align === 'right') { // horizontal alignment handled first
  9845. if (!defined(margin[1])) {
  9846. chart.marginRight = mathMax(
  9847. chart.marginRight,
  9848. legend.legendWidth - legendX + legendMargin + spacing[1]
  9849. );
  9850. }
  9851. } else if (align === 'left') {
  9852. if (!defined(margin[3])) {
  9853. chart.plotLeft = mathMax(
  9854. chart.plotLeft,
  9855. legend.legendWidth + legendX + legendMargin + spacing[3]
  9856. );
  9857. }
  9858. } else if (verticalAlign === 'top') {
  9859. if (!defined(margin[0])) {
  9860. chart.plotTop = mathMax(
  9861. chart.plotTop,
  9862. legend.legendHeight + legendY + legendMargin + spacing[0]
  9863. );
  9864. }
  9865. } else if (verticalAlign === 'bottom') {
  9866. if (!defined(margin[2])) {
  9867. chart.marginBottom = mathMax(
  9868. chart.marginBottom,
  9869. legend.legendHeight - legendY + legendMargin + spacing[2]
  9870. );
  9871. }
  9872. }
  9873. }
  9874. // adjust for scroller
  9875. if (chart.extraBottomMargin) {
  9876. chart.marginBottom += chart.extraBottomMargin;
  9877. }
  9878. if (chart.extraTopMargin) {
  9879. chart.plotTop += chart.extraTopMargin;
  9880. }
  9881. // pre-render axes to get labels offset width
  9882. if (chart.hasCartesianSeries) {
  9883. each(chart.axes, function (axis) {
  9884. axis.getOffset();
  9885. });
  9886. }
  9887. if (!defined(margin[3])) {
  9888. chart.plotLeft += axisOffset[3];
  9889. }
  9890. if (!defined(margin[0])) {
  9891. chart.plotTop += axisOffset[0];
  9892. }
  9893. if (!defined(margin[2])) {
  9894. chart.marginBottom += axisOffset[2];
  9895. }
  9896. if (!defined(margin[1])) {
  9897. chart.marginRight += axisOffset[1];
  9898. }
  9899. chart.setChartSize();
  9900. },
  9901. /**
  9902. * Resize the chart to its container if size is not explicitly set
  9903. */
  9904. reflow: function (e) {
  9905. var chart = this,
  9906. optionsChart = chart.options.chart,
  9907. renderTo = chart.renderTo,
  9908. width = optionsChart.width || adapterRun(renderTo, 'width'),
  9909. height = optionsChart.height || adapterRun(renderTo, 'height'),
  9910. target = e ? e.target : win, // #805 - MooTools doesn't supply e
  9911. doReflow = function () {
  9912. if (chart.container) { // It may have been destroyed in the meantime (#1257)
  9913. chart.setSize(width, height, false);
  9914. chart.hasUserSize = null;
  9915. }
  9916. };
  9917. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  9918. // win in Firefox, Chrome and IE9.
  9919. if (!chart.hasUserSize && width && height && (target === win || target === doc)) {
  9920. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  9921. clearTimeout(chart.reflowTimeout);
  9922. if (e) { // Called from window.resize
  9923. chart.reflowTimeout = setTimeout(doReflow, 100);
  9924. } else { // Called directly (#2224)
  9925. doReflow();
  9926. }
  9927. }
  9928. chart.containerWidth = width;
  9929. chart.containerHeight = height;
  9930. }
  9931. },
  9932. /**
  9933. * Add the event handlers necessary for auto resizing
  9934. */
  9935. initReflow: function () {
  9936. var chart = this,
  9937. reflow = function (e) {
  9938. chart.reflow(e);
  9939. };
  9940. addEvent(win, 'resize', reflow);
  9941. addEvent(chart, 'destroy', function () {
  9942. removeEvent(win, 'resize', reflow);
  9943. });
  9944. },
  9945. /**
  9946. * Resize the chart to a given width and height
  9947. * @param {Number} width
  9948. * @param {Number} height
  9949. * @param {Object|Boolean} animation
  9950. */
  9951. setSize: function (width, height, animation) {
  9952. var chart = this,
  9953. chartWidth,
  9954. chartHeight,
  9955. fireEndResize;
  9956. // Handle the isResizing counter
  9957. chart.isResizing += 1;
  9958. fireEndResize = function () {
  9959. if (chart) {
  9960. fireEvent(chart, 'endResize', null, function () {
  9961. chart.isResizing -= 1;
  9962. });
  9963. }
  9964. };
  9965. // set the animation for the current process
  9966. setAnimation(animation, chart);
  9967. chart.oldChartHeight = chart.chartHeight;
  9968. chart.oldChartWidth = chart.chartWidth;
  9969. if (defined(width)) {
  9970. chart.chartWidth = chartWidth = mathMax(0, mathRound(width));
  9971. chart.hasUserSize = !!chartWidth;
  9972. }
  9973. if (defined(height)) {
  9974. chart.chartHeight = chartHeight = mathMax(0, mathRound(height));
  9975. }
  9976. // Resize the container with the global animation applied if enabled (#2503)
  9977. (globalAnimation ? animate : css)(chart.container, {
  9978. width: chartWidth + PX,
  9979. height: chartHeight + PX
  9980. }, globalAnimation);
  9981. chart.setChartSize(true);
  9982. chart.renderer.setSize(chartWidth, chartHeight, animation);
  9983. // handle axes
  9984. chart.maxTicks = null;
  9985. each(chart.axes, function (axis) {
  9986. axis.isDirty = true;
  9987. axis.setScale();
  9988. });
  9989. // make sure non-cartesian series are also handled
  9990. each(chart.series, function (serie) {
  9991. serie.isDirty = true;
  9992. });
  9993. chart.isDirtyLegend = true; // force legend redraw
  9994. chart.isDirtyBox = true; // force redraw of plot and chart border
  9995. chart.getMargins();
  9996. chart.redraw(animation);
  9997. chart.oldChartHeight = null;
  9998. fireEvent(chart, 'resize');
  9999. // fire endResize and set isResizing back
  10000. // If animation is disabled, fire without delay
  10001. if (globalAnimation === false) {
  10002. fireEndResize();
  10003. } else { // else set a timeout with the animation duration
  10004. setTimeout(fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  10005. }
  10006. },
  10007. /**
  10008. * Set the public chart properties. This is done before and after the pre-render
  10009. * to determine margin sizes
  10010. */
  10011. setChartSize: function (skipAxes) {
  10012. var chart = this,
  10013. inverted = chart.inverted,
  10014. renderer = chart.renderer,
  10015. chartWidth = chart.chartWidth,
  10016. chartHeight = chart.chartHeight,
  10017. optionsChart = chart.options.chart,
  10018. spacing = chart.spacing,
  10019. clipOffset = chart.clipOffset,
  10020. clipX,
  10021. clipY,
  10022. plotLeft,
  10023. plotTop,
  10024. plotWidth,
  10025. plotHeight,
  10026. plotBorderWidth;
  10027. chart.plotLeft = plotLeft = mathRound(chart.plotLeft);
  10028. chart.plotTop = plotTop = mathRound(chart.plotTop);
  10029. chart.plotWidth = plotWidth = mathMax(0, mathRound(chartWidth - plotLeft - chart.marginRight));
  10030. chart.plotHeight = plotHeight = mathMax(0, mathRound(chartHeight - plotTop - chart.marginBottom));
  10031. chart.plotSizeX = inverted ? plotHeight : plotWidth;
  10032. chart.plotSizeY = inverted ? plotWidth : plotHeight;
  10033. chart.plotBorderWidth = optionsChart.plotBorderWidth || 0;
  10034. // Set boxes used for alignment
  10035. chart.spacingBox = renderer.spacingBox = {
  10036. x: spacing[3],
  10037. y: spacing[0],
  10038. width: chartWidth - spacing[3] - spacing[1],
  10039. height: chartHeight - spacing[0] - spacing[2]
  10040. };
  10041. chart.plotBox = renderer.plotBox = {
  10042. x: plotLeft,
  10043. y: plotTop,
  10044. width: plotWidth,
  10045. height: plotHeight
  10046. };
  10047. plotBorderWidth = 2 * mathFloor(chart.plotBorderWidth / 2);
  10048. clipX = mathCeil(mathMax(plotBorderWidth, clipOffset[3]) / 2);
  10049. clipY = mathCeil(mathMax(plotBorderWidth, clipOffset[0]) / 2);
  10050. chart.clipBox = {
  10051. x: clipX,
  10052. y: clipY,
  10053. width: mathFloor(chart.plotSizeX - mathMax(plotBorderWidth, clipOffset[1]) / 2 - clipX),
  10054. height: mathFloor(chart.plotSizeY - mathMax(plotBorderWidth, clipOffset[2]) / 2 - clipY)
  10055. };
  10056. if (!skipAxes) {
  10057. each(chart.axes, function (axis) {
  10058. axis.setAxisSize();
  10059. axis.setAxisTranslation();
  10060. });
  10061. }
  10062. },
  10063. /**
  10064. * Initial margins before auto size margins are applied
  10065. */
  10066. resetMargins: function () {
  10067. var chart = this,
  10068. spacing = chart.spacing,
  10069. margin = chart.margin;
  10070. chart.plotTop = pick(margin[0], spacing[0]);
  10071. chart.marginRight = pick(margin[1], spacing[1]);
  10072. chart.marginBottom = pick(margin[2], spacing[2]);
  10073. chart.plotLeft = pick(margin[3], spacing[3]);
  10074. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  10075. chart.clipOffset = [0, 0, 0, 0];
  10076. },
  10077. /**
  10078. * Draw the borders and backgrounds for chart and plot area
  10079. */
  10080. drawChartBox: function () {
  10081. var chart = this,
  10082. optionsChart = chart.options.chart,
  10083. renderer = chart.renderer,
  10084. chartWidth = chart.chartWidth,
  10085. chartHeight = chart.chartHeight,
  10086. chartBackground = chart.chartBackground,
  10087. plotBackground = chart.plotBackground,
  10088. plotBorder = chart.plotBorder,
  10089. plotBGImage = chart.plotBGImage,
  10090. chartBorderWidth = optionsChart.borderWidth || 0,
  10091. chartBackgroundColor = optionsChart.backgroundColor,
  10092. plotBackgroundColor = optionsChart.plotBackgroundColor,
  10093. plotBackgroundImage = optionsChart.plotBackgroundImage,
  10094. plotBorderWidth = optionsChart.plotBorderWidth || 0,
  10095. mgn,
  10096. bgAttr,
  10097. plotLeft = chart.plotLeft,
  10098. plotTop = chart.plotTop,
  10099. plotWidth = chart.plotWidth,
  10100. plotHeight = chart.plotHeight,
  10101. plotBox = chart.plotBox,
  10102. clipRect = chart.clipRect,
  10103. clipBox = chart.clipBox;
  10104. // Chart area
  10105. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  10106. if (chartBorderWidth || chartBackgroundColor) {
  10107. if (!chartBackground) {
  10108. bgAttr = {
  10109. fill: chartBackgroundColor || NONE
  10110. };
  10111. if (chartBorderWidth) { // #980
  10112. bgAttr.stroke = optionsChart.borderColor;
  10113. bgAttr['stroke-width'] = chartBorderWidth;
  10114. }
  10115. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  10116. optionsChart.borderRadius, chartBorderWidth)
  10117. .attr(bgAttr)
  10118. .addClass(PREFIX + 'background')
  10119. .add()
  10120. .shadow(optionsChart.shadow);
  10121. } else { // resize
  10122. chartBackground.animate(
  10123. chartBackground.crisp({ width: chartWidth - mgn, height: chartHeight - mgn })
  10124. );
  10125. }
  10126. }
  10127. // Plot background
  10128. if (plotBackgroundColor) {
  10129. if (!plotBackground) {
  10130. chart.plotBackground = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0)
  10131. .attr({
  10132. fill: plotBackgroundColor
  10133. })
  10134. .add()
  10135. .shadow(optionsChart.plotShadow);
  10136. } else {
  10137. plotBackground.animate(plotBox);
  10138. }
  10139. }
  10140. if (plotBackgroundImage) {
  10141. if (!plotBGImage) {
  10142. chart.plotBGImage = renderer.image(plotBackgroundImage, plotLeft, plotTop, plotWidth, plotHeight)
  10143. .add();
  10144. } else {
  10145. plotBGImage.animate(plotBox);
  10146. }
  10147. }
  10148. // Plot clip
  10149. if (!clipRect) {
  10150. chart.clipRect = renderer.clipRect(clipBox);
  10151. } else {
  10152. clipRect.animate({
  10153. width: clipBox.width,
  10154. height: clipBox.height
  10155. });
  10156. }
  10157. // Plot area border
  10158. if (plotBorderWidth) {
  10159. if (!plotBorder) {
  10160. chart.plotBorder = renderer.rect(plotLeft, plotTop, plotWidth, plotHeight, 0, -plotBorderWidth)
  10161. .attr({
  10162. stroke: optionsChart.plotBorderColor,
  10163. 'stroke-width': plotBorderWidth,
  10164. fill: NONE,
  10165. zIndex: 1
  10166. })
  10167. .add();
  10168. } else {
  10169. plotBorder.animate(
  10170. plotBorder.crisp({ x: plotLeft, y: plotTop, width: plotWidth, height: plotHeight })
  10171. );
  10172. }
  10173. }
  10174. // reset
  10175. chart.isDirtyBox = false;
  10176. },
  10177. /**
  10178. * Detect whether a certain chart property is needed based on inspecting its options
  10179. * and series. This mainly applies to the chart.invert property, and in extensions to
  10180. * the chart.angular and chart.polar properties.
  10181. */
  10182. propFromSeries: function () {
  10183. var chart = this,
  10184. optionsChart = chart.options.chart,
  10185. klass,
  10186. seriesOptions = chart.options.series,
  10187. i,
  10188. value;
  10189. each(['inverted', 'angular', 'polar'], function (key) {
  10190. // The default series type's class
  10191. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  10192. // Get the value from available chart-wide properties
  10193. value = (
  10194. chart[key] || // 1. it is set before
  10195. optionsChart[key] || // 2. it is set in the options
  10196. (klass && klass.prototype[key]) // 3. it's default series class requires it
  10197. );
  10198. // 4. Check if any the chart's series require it
  10199. i = seriesOptions && seriesOptions.length;
  10200. while (!value && i--) {
  10201. klass = seriesTypes[seriesOptions[i].type];
  10202. if (klass && klass.prototype[key]) {
  10203. value = true;
  10204. }
  10205. }
  10206. // Set the chart property
  10207. chart[key] = value;
  10208. });
  10209. },
  10210. /**
  10211. * Link two or more series together. This is done initially from Chart.render,
  10212. * and after Chart.addSeries and Series.remove.
  10213. */
  10214. linkSeries: function () {
  10215. var chart = this,
  10216. chartSeries = chart.series;
  10217. // Reset links
  10218. each(chartSeries, function (series) {
  10219. series.linkedSeries.length = 0;
  10220. });
  10221. // Apply new links
  10222. each(chartSeries, function (series) {
  10223. var linkedTo = series.options.linkedTo;
  10224. if (isString(linkedTo)) {
  10225. if (linkedTo === ':previous') {
  10226. linkedTo = chart.series[series.index - 1];
  10227. } else {
  10228. linkedTo = chart.get(linkedTo);
  10229. }
  10230. if (linkedTo) {
  10231. linkedTo.linkedSeries.push(series);
  10232. series.linkedParent = linkedTo;
  10233. }
  10234. }
  10235. });
  10236. },
  10237. /**
  10238. * Render series for the chart
  10239. */
  10240. renderSeries: function () {
  10241. each(this.series, function (serie) {
  10242. serie.translate();
  10243. if (serie.setTooltipPoints) {
  10244. serie.setTooltipPoints();
  10245. }
  10246. serie.render();
  10247. });
  10248. },
  10249. /**
  10250. * Render all graphics for the chart
  10251. */
  10252. render: function () {
  10253. var chart = this,
  10254. axes = chart.axes,
  10255. renderer = chart.renderer,
  10256. options = chart.options;
  10257. var labels = options.labels,
  10258. credits = options.credits,
  10259. creditsHref;
  10260. // Title
  10261. chart.setTitle();
  10262. // Legend
  10263. chart.legend = new Legend(chart, options.legend);
  10264. chart.getStacks(); // render stacks
  10265. // Get margins by pre-rendering axes
  10266. // set axes scales
  10267. each(axes, function (axis) {
  10268. axis.setScale();
  10269. });
  10270. chart.getMargins();
  10271. chart.maxTicks = null; // reset for second pass
  10272. each(axes, function (axis) {
  10273. axis.setTickPositions(true); // update to reflect the new margins
  10274. axis.setMaxTicks();
  10275. });
  10276. chart.adjustTickAmounts();
  10277. chart.getMargins(); // second pass to check for new labels
  10278. // Draw the borders and backgrounds
  10279. chart.drawChartBox();
  10280. // Axes
  10281. if (chart.hasCartesianSeries) {
  10282. each(axes, function (axis) {
  10283. axis.render();
  10284. });
  10285. }
  10286. // The series
  10287. if (!chart.seriesGroup) {
  10288. chart.seriesGroup = renderer.g('series-group')
  10289. .attr({ zIndex: 3 })
  10290. .add();
  10291. }
  10292. chart.renderSeries();
  10293. // Labels
  10294. if (labels.items) {
  10295. each(labels.items, function (label) {
  10296. var style = extend(labels.style, label.style),
  10297. x = pInt(style.left) + chart.plotLeft,
  10298. y = pInt(style.top) + chart.plotTop + 12;
  10299. // delete to prevent rewriting in IE
  10300. delete style.left;
  10301. delete style.top;
  10302. renderer.text(
  10303. label.html,
  10304. x,
  10305. y
  10306. )
  10307. .attr({ zIndex: 2 })
  10308. .css(style)
  10309. .add();
  10310. });
  10311. }
  10312. // Credits
  10313. if (credits.enabled && !chart.credits) {
  10314. creditsHref = credits.href;
  10315. chart.credits = renderer.text(
  10316. credits.text,
  10317. 0,
  10318. 0
  10319. )
  10320. .on('click', function () {
  10321. if (creditsHref) {
  10322. location.href = creditsHref;
  10323. }
  10324. })
  10325. .attr({
  10326. align: credits.position.align,
  10327. zIndex: 8
  10328. })
  10329. .css(credits.style)
  10330. .add()
  10331. .align(credits.position);
  10332. }
  10333. // Set flag
  10334. chart.hasRendered = true;
  10335. },
  10336. /**
  10337. * Clean up memory usage
  10338. */
  10339. destroy: function () {
  10340. var chart = this,
  10341. axes = chart.axes,
  10342. series = chart.series,
  10343. container = chart.container,
  10344. i,
  10345. parentNode = container && container.parentNode;
  10346. // fire the chart.destoy event
  10347. fireEvent(chart, 'destroy');
  10348. // Delete the chart from charts lookup array
  10349. charts[chart.index] = UNDEFINED;
  10350. chart.renderTo.removeAttribute('data-highcharts-chart');
  10351. // remove events
  10352. removeEvent(chart);
  10353. // ==== Destroy collections:
  10354. // Destroy axes
  10355. i = axes.length;
  10356. while (i--) {
  10357. axes[i] = axes[i].destroy();
  10358. }
  10359. // Destroy each series
  10360. i = series.length;
  10361. while (i--) {
  10362. series[i] = series[i].destroy();
  10363. }
  10364. // ==== Destroy chart properties:
  10365. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage',
  10366. 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'pointer', 'scroller',
  10367. 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  10368. var prop = chart[name];
  10369. if (prop && prop.destroy) {
  10370. chart[name] = prop.destroy();
  10371. }
  10372. });
  10373. // remove container and all SVG
  10374. if (container) { // can break in IE when destroyed before finished loading
  10375. container.innerHTML = '';
  10376. removeEvent(container);
  10377. if (parentNode) {
  10378. discardElement(container);
  10379. }
  10380. }
  10381. // clean it all up
  10382. for (i in chart) {
  10383. delete chart[i];
  10384. }
  10385. },
  10386. /**
  10387. * VML namespaces can't be added until after complete. Listening
  10388. * for Perini's doScroll hack is not enough.
  10389. */
  10390. isReadyToRender: function () {
  10391. var chart = this;
  10392. // Note: in spite of JSLint's complaints, win == win.top is required
  10393. /*jslint eqeq: true*/
  10394. if ((!hasSVG && (win == win.top && doc.readyState !== 'complete')) || (useCanVG && !win.canvg)) {
  10395. /*jslint eqeq: false*/
  10396. if (useCanVG) {
  10397. // Delay rendering until canvg library is downloaded and ready
  10398. CanVGController.push(function () { chart.firstRender(); }, chart.options.global.canvasToolsURL);
  10399. } else {
  10400. doc.attachEvent('onreadystatechange', function () {
  10401. doc.detachEvent('onreadystatechange', chart.firstRender);
  10402. if (doc.readyState === 'complete') {
  10403. chart.firstRender();
  10404. }
  10405. });
  10406. }
  10407. return false;
  10408. }
  10409. return true;
  10410. },
  10411. /**
  10412. * Prepare for first rendering after all data are loaded
  10413. */
  10414. firstRender: function () {
  10415. var chart = this,
  10416. options = chart.options,
  10417. callback = chart.callback;
  10418. // Check whether the chart is ready to render
  10419. if (!chart.isReadyToRender()) {
  10420. return;
  10421. }
  10422. // Create the container
  10423. chart.getContainer();
  10424. // Run an early event after the container and renderer are established
  10425. fireEvent(chart, 'init');
  10426. chart.resetMargins();
  10427. chart.setChartSize();
  10428. // Set the common chart properties (mainly invert) from the given series
  10429. chart.propFromSeries();
  10430. // get axes
  10431. chart.getAxes();
  10432. // Initialize the series
  10433. each(options.series || [], function (serieOptions) {
  10434. chart.initSeries(serieOptions);
  10435. });
  10436. chart.linkSeries();
  10437. // Run an event after axes and series are initialized, but before render. At this stage,
  10438. // the series data is indexed and cached in the xData and yData arrays, so we can access
  10439. // those before rendering. Used in Highstock.
  10440. fireEvent(chart, 'beforeRender');
  10441. // depends on inverted and on margins being set
  10442. if (Highcharts.Pointer) {
  10443. chart.pointer = new Pointer(chart, options);
  10444. }
  10445. chart.render();
  10446. // add canvas
  10447. chart.renderer.draw();
  10448. // run callbacks
  10449. if (callback) {
  10450. callback.apply(chart, [chart]);
  10451. }
  10452. each(chart.callbacks, function (fn) {
  10453. fn.apply(chart, [chart]);
  10454. });
  10455. // If the chart was rendered outside the top container, put it back in
  10456. chart.cloneRenderTo(true);
  10457. fireEvent(chart, 'load');
  10458. },
  10459. /**
  10460. * Creates arrays for spacing and margin from given options.
  10461. */
  10462. splashArray: function (target, options) {
  10463. var oVar = options[target],
  10464. tArray = isObject(oVar) ? oVar : [oVar, oVar, oVar, oVar];
  10465. return [pick(options[target + 'Top'], tArray[0]),
  10466. pick(options[target + 'Right'], tArray[1]),
  10467. pick(options[target + 'Bottom'], tArray[2]),
  10468. pick(options[target + 'Left'], tArray[3])];
  10469. }
  10470. }; // end Chart
  10471. // Hook for exporting module
  10472. Chart.prototype.callbacks = [];
  10473. var CenteredSeriesMixin = Highcharts.CenteredSeriesMixin = {
  10474. /**
  10475. * Get the center of the pie based on the size and center options relative to the
  10476. * plot area. Borrowed by the polar and gauge series types.
  10477. */
  10478. getCenter: function () {
  10479. var options = this.options,
  10480. chart = this.chart,
  10481. slicingRoom = 2 * (options.slicedOffset || 0),
  10482. handleSlicingRoom,
  10483. plotWidth = chart.plotWidth - 2 * slicingRoom,
  10484. plotHeight = chart.plotHeight - 2 * slicingRoom,
  10485. centerOption = options.center,
  10486. positions = [pick(centerOption[0], '50%'), pick(centerOption[1], '50%'), options.size || '100%', options.innerSize || 0],
  10487. smallestSize = mathMin(plotWidth, plotHeight),
  10488. isPercent;
  10489. return map(positions, function (length, i) {
  10490. isPercent = /%$/.test(length);
  10491. handleSlicingRoom = i < 2 || (i === 2 && isPercent);
  10492. return (isPercent ?
  10493. // i == 0: centerX, relative to width
  10494. // i == 1: centerY, relative to height
  10495. // i == 2: size, relative to smallestSize
  10496. // i == 4: innerSize, relative to smallestSize
  10497. [plotWidth, plotHeight, smallestSize, smallestSize][i] *
  10498. pInt(length) / 100 :
  10499. length) + (handleSlicingRoom ? slicingRoom : 0);
  10500. });
  10501. }
  10502. };
  10503. /**
  10504. * The Point object and prototype. Inheritable and used as base for PiePoint
  10505. */
  10506. var Point = function () {};
  10507. Point.prototype = {
  10508. /**
  10509. * Initialize the point
  10510. * @param {Object} series The series object containing this point
  10511. * @param {Object} options The data in either number, array or object format
  10512. */
  10513. init: function (series, options, x) {
  10514. var point = this,
  10515. colors;
  10516. point.series = series;
  10517. point.applyOptions(options, x);
  10518. point.pointAttr = {};
  10519. if (series.options.colorByPoint) {
  10520. colors = series.options.colors || series.chart.options.colors;
  10521. point.color = point.color || colors[series.colorCounter++];
  10522. // loop back to zero
  10523. if (series.colorCounter === colors.length) {
  10524. series.colorCounter = 0;
  10525. }
  10526. }
  10527. series.chart.pointCount++;
  10528. return point;
  10529. },
  10530. /**
  10531. * Apply the options containing the x and y data and possible some extra properties.
  10532. * This is called on point init or from point.update.
  10533. *
  10534. * @param {Object} options
  10535. */
  10536. applyOptions: function (options, x) {
  10537. var point = this,
  10538. series = point.series,
  10539. pointValKey = series.pointValKey;
  10540. options = Point.prototype.optionsToObject.call(this, options);
  10541. // copy options directly to point
  10542. extend(point, options);
  10543. point.options = point.options ? extend(point.options, options) : options;
  10544. // For higher dimension series types. For instance, for ranges, point.y is mapped to point.low.
  10545. if (pointValKey) {
  10546. point.y = point[pointValKey];
  10547. }
  10548. // If no x is set by now, get auto incremented value. All points must have an
  10549. // x value, however the y value can be null to create a gap in the series
  10550. if (point.x === UNDEFINED && series) {
  10551. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  10552. }
  10553. return point;
  10554. },
  10555. /**
  10556. * Transform number or array configs into objects
  10557. */
  10558. optionsToObject: function (options) {
  10559. var ret = {},
  10560. series = this.series,
  10561. pointArrayMap = series.pointArrayMap || ['y'],
  10562. valueCount = pointArrayMap.length,
  10563. firstItemType,
  10564. i = 0,
  10565. j = 0;
  10566. if (typeof options === 'number' || options === null) {
  10567. ret[pointArrayMap[0]] = options;
  10568. } else if (isArray(options)) {
  10569. // with leading x value
  10570. if (options.length > valueCount) {
  10571. firstItemType = typeof options[0];
  10572. if (firstItemType === 'string') {
  10573. ret.name = options[0];
  10574. } else if (firstItemType === 'number') {
  10575. ret.x = options[0];
  10576. }
  10577. i++;
  10578. }
  10579. while (j < valueCount) {
  10580. ret[pointArrayMap[j++]] = options[i++];
  10581. }
  10582. } else if (typeof options === 'object') {
  10583. ret = options;
  10584. // This is the fastest way to detect if there are individual point dataLabels that need
  10585. // to be considered in drawDataLabels. These can only occur in object configs.
  10586. if (options.dataLabels) {
  10587. series._hasPointLabels = true;
  10588. }
  10589. // Same approach as above for markers
  10590. if (options.marker) {
  10591. series._hasPointMarkers = true;
  10592. }
  10593. }
  10594. return ret;
  10595. },
  10596. /**
  10597. * Destroy a point to clear memory. Its reference still stays in series.data.
  10598. */
  10599. destroy: function () {
  10600. var point = this,
  10601. series = point.series,
  10602. chart = series.chart,
  10603. hoverPoints = chart.hoverPoints,
  10604. prop;
  10605. chart.pointCount--;
  10606. if (hoverPoints) {
  10607. point.setState();
  10608. erase(hoverPoints, point);
  10609. if (!hoverPoints.length) {
  10610. chart.hoverPoints = null;
  10611. }
  10612. }
  10613. if (point === chart.hoverPoint) {
  10614. point.onMouseOut();
  10615. }
  10616. // remove all events
  10617. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  10618. removeEvent(point);
  10619. point.destroyElements();
  10620. }
  10621. if (point.legendItem) { // pies have legend items
  10622. chart.legend.destroyItem(point);
  10623. }
  10624. for (prop in point) {
  10625. point[prop] = null;
  10626. }
  10627. },
  10628. /**
  10629. * Destroy SVG elements associated with the point
  10630. */
  10631. destroyElements: function () {
  10632. var point = this,
  10633. props = ['graphic', 'dataLabel', 'dataLabelUpper', 'group', 'connector', 'shadowGroup'],
  10634. prop,
  10635. i = 6;
  10636. while (i--) {
  10637. prop = props[i];
  10638. if (point[prop]) {
  10639. point[prop] = point[prop].destroy();
  10640. }
  10641. }
  10642. },
  10643. /**
  10644. * Return the configuration hash needed for the data label and tooltip formatters
  10645. */
  10646. getLabelConfig: function () {
  10647. var point = this;
  10648. return {
  10649. x: point.category,
  10650. y: point.y,
  10651. key: point.name || point.category,
  10652. series: point.series,
  10653. point: point,
  10654. percentage: point.percentage,
  10655. total: point.total || point.stackTotal
  10656. };
  10657. },
  10658. /**
  10659. * Extendable method for formatting each point's tooltip line
  10660. *
  10661. * @return {String} A string to be concatenated in to the common tooltip text
  10662. */
  10663. tooltipFormatter: function (pointFormat) {
  10664. // Insert options for valueDecimals, valuePrefix, and valueSuffix
  10665. var series = this.series,
  10666. seriesTooltipOptions = series.tooltipOptions,
  10667. valueDecimals = pick(seriesTooltipOptions.valueDecimals, ''),
  10668. valuePrefix = seriesTooltipOptions.valuePrefix || '',
  10669. valueSuffix = seriesTooltipOptions.valueSuffix || '';
  10670. // Loop over the point array map and replace unformatted values with sprintf formatting markup
  10671. each(series.pointArrayMap || ['y'], function (key) {
  10672. key = '{point.' + key; // without the closing bracket
  10673. if (valuePrefix || valueSuffix) {
  10674. pointFormat = pointFormat.replace(key + '}', valuePrefix + key + '}' + valueSuffix);
  10675. }
  10676. pointFormat = pointFormat.replace(key + '}', key + ':,.' + valueDecimals + 'f}');
  10677. });
  10678. return format(pointFormat, {
  10679. point: this,
  10680. series: this.series
  10681. });
  10682. }
  10683. };/**
  10684. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  10685. * in various arrays.
  10686. *
  10687. * - First, series.options.data contains all the original config options for
  10688. * each point whether added by options or methods like series.addPoint.
  10689. * - Next, series.data contains those values converted to points, but in case the series data length
  10690. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  10691. * only contains the points that have been created on demand.
  10692. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  10693. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  10694. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  10695. * be correlated one to one.
  10696. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  10697. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  10698. *
  10699. * @param {Object} chart
  10700. * @param {Object} options
  10701. */
  10702. var Series = function () {};
  10703. Series.prototype = {
  10704. isCartesian: true,
  10705. type: 'line',
  10706. pointClass: Point,
  10707. sorted: true, // requires the data to be sorted
  10708. requireSorting: true,
  10709. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  10710. stroke: 'lineColor',
  10711. 'stroke-width': 'lineWidth',
  10712. fill: 'fillColor',
  10713. r: 'radius'
  10714. },
  10715. axisTypes: ['xAxis', 'yAxis'],
  10716. colorCounter: 0,
  10717. parallelArrays: ['x', 'y'], // each point's x and y values are stored in this.xData and this.yData
  10718. init: function (chart, options) {
  10719. var series = this,
  10720. eventType,
  10721. events,
  10722. chartSeries = chart.series,
  10723. sortByIndex = function (a, b) {
  10724. return pick(a.options.index, a._i) - pick(b.options.index, b._i);
  10725. };
  10726. series.chart = chart;
  10727. series.options = options = series.setOptions(options); // merge with plotOptions
  10728. series.linkedSeries = [];
  10729. // bind the axes
  10730. series.bindAxes();
  10731. // set some variables
  10732. extend(series, {
  10733. name: options.name,
  10734. state: NORMAL_STATE,
  10735. pointAttr: {},
  10736. visible: options.visible !== false, // true by default
  10737. selected: options.selected === true // false by default
  10738. });
  10739. // special
  10740. if (useCanVG) {
  10741. options.animation = false;
  10742. }
  10743. // register event listeners
  10744. events = options.events;
  10745. for (eventType in events) {
  10746. addEvent(series, eventType, events[eventType]);
  10747. }
  10748. if (
  10749. (events && events.click) ||
  10750. (options.point && options.point.events && options.point.events.click) ||
  10751. options.allowPointSelect
  10752. ) {
  10753. chart.runTrackerClick = true;
  10754. }
  10755. series.getColor();
  10756. series.getSymbol();
  10757. // Set the data
  10758. each(series.parallelArrays, function (key) {
  10759. series[key + 'Data'] = [];
  10760. });
  10761. series.setData(options.data, false);
  10762. // Mark cartesian
  10763. if (series.isCartesian) {
  10764. chart.hasCartesianSeries = true;
  10765. }
  10766. // Register it in the chart
  10767. chartSeries.push(series);
  10768. series._i = chartSeries.length - 1;
  10769. // Sort series according to index option (#248, #1123, #2456)
  10770. stableSort(chartSeries, sortByIndex);
  10771. if (this.yAxis) {
  10772. stableSort(this.yAxis.series, sortByIndex);
  10773. }
  10774. each(chartSeries, function (series, i) {
  10775. series.index = i;
  10776. series.name = series.name || 'Series ' + (i + 1);
  10777. });
  10778. },
  10779. /**
  10780. * Set the xAxis and yAxis properties of cartesian series, and register the series
  10781. * in the axis.series array
  10782. */
  10783. bindAxes: function () {
  10784. var series = this,
  10785. seriesOptions = series.options,
  10786. chart = series.chart,
  10787. axisOptions;
  10788. each(series.axisTypes || [], function (AXIS) { // repeat for xAxis and yAxis
  10789. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  10790. axisOptions = axis.options;
  10791. // apply if the series xAxis or yAxis option mathches the number of the
  10792. // axis, or if undefined, use the first axis
  10793. if ((seriesOptions[AXIS] === axisOptions.index) ||
  10794. (seriesOptions[AXIS] !== UNDEFINED && seriesOptions[AXIS] === axisOptions.id) ||
  10795. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  10796. // register this series in the axis.series lookup
  10797. axis.series.push(series);
  10798. // set this series.xAxis or series.yAxis reference
  10799. series[AXIS] = axis;
  10800. // mark dirty for redraw
  10801. axis.isDirty = true;
  10802. }
  10803. });
  10804. // The series needs an X and an Y axis
  10805. if (!series[AXIS] && series.optionalAxis !== AXIS) {
  10806. error(18, true);
  10807. }
  10808. });
  10809. },
  10810. /**
  10811. * For simple series types like line and column, the data values are held in arrays like
  10812. * xData and yData for quick lookup to find extremes and more. For multidimensional series
  10813. * like bubble and map, this can be extended with arrays like zData and valueData by
  10814. * adding to the series.parallelArrays array.
  10815. */
  10816. updateParallelArrays: function (point, i) {
  10817. var series = point.series,
  10818. args = arguments,
  10819. fn = typeof i === 'number' ?
  10820. // Insert the value in the given position
  10821. function (key) {
  10822. var val = key === 'y' && series.toYData ? series.toYData(point) : point[key];
  10823. series[key + 'Data'][i] = val;
  10824. } :
  10825. // Apply the method specified in i with the following arguments as arguments
  10826. function (key) {
  10827. Array.prototype[i].apply(series[key + 'Data'], Array.prototype.slice.call(args, 2));
  10828. };
  10829. each(series.parallelArrays, fn);
  10830. },
  10831. /**
  10832. * Return an auto incremented x value based on the pointStart and pointInterval options.
  10833. * This is only used if an x value is not given for the point that calls autoIncrement.
  10834. */
  10835. autoIncrement: function () {
  10836. var series = this,
  10837. options = series.options,
  10838. xIncrement = series.xIncrement;
  10839. xIncrement = pick(xIncrement, options.pointStart, 0);
  10840. series.pointInterval = pick(series.pointInterval, options.pointInterval, 1);
  10841. series.xIncrement = xIncrement + series.pointInterval;
  10842. return xIncrement;
  10843. },
  10844. /**
  10845. * Divide the series data into segments divided by null values.
  10846. */
  10847. getSegments: function () {
  10848. var series = this,
  10849. lastNull = -1,
  10850. segments = [],
  10851. i,
  10852. points = series.points,
  10853. pointsLength = points.length;
  10854. if (pointsLength) { // no action required for []
  10855. // if connect nulls, just remove null points
  10856. if (series.options.connectNulls) {
  10857. i = pointsLength;
  10858. while (i--) {
  10859. if (points[i].y === null) {
  10860. points.splice(i, 1);
  10861. }
  10862. }
  10863. if (points.length) {
  10864. segments = [points];
  10865. }
  10866. // else, split on null points
  10867. } else {
  10868. each(points, function (point, i) {
  10869. if (point.y === null) {
  10870. if (i > lastNull + 1) {
  10871. segments.push(points.slice(lastNull + 1, i));
  10872. }
  10873. lastNull = i;
  10874. } else if (i === pointsLength - 1) { // last value
  10875. segments.push(points.slice(lastNull + 1, i + 1));
  10876. }
  10877. });
  10878. }
  10879. }
  10880. // register it
  10881. series.segments = segments;
  10882. },
  10883. /**
  10884. * Set the series options by merging from the options tree
  10885. * @param {Object} itemOptions
  10886. */
  10887. setOptions: function (itemOptions) {
  10888. var chart = this.chart,
  10889. chartOptions = chart.options,
  10890. plotOptions = chartOptions.plotOptions,
  10891. userOptions = chart.userOptions || {},
  10892. userPlotOptions = userOptions.plotOptions || {},
  10893. typeOptions = plotOptions[this.type],
  10894. options;
  10895. this.userOptions = itemOptions;
  10896. options = merge(
  10897. typeOptions,
  10898. plotOptions.series,
  10899. itemOptions
  10900. );
  10901. // The tooltip options are merged between global and series specific options
  10902. this.tooltipOptions = merge(
  10903. defaultOptions.tooltip,
  10904. defaultOptions.plotOptions[this.type].tooltip,
  10905. userOptions.tooltip,
  10906. userPlotOptions.series && userPlotOptions.series.tooltip,
  10907. userPlotOptions[this.type] && userPlotOptions[this.type].tooltip,
  10908. itemOptions.tooltip
  10909. );
  10910. // Delete marker object if not allowed (#1125)
  10911. if (typeOptions.marker === null) {
  10912. delete options.marker;
  10913. }
  10914. return options;
  10915. },
  10916. /**
  10917. * Get the series' color
  10918. */
  10919. getColor: function () {
  10920. var options = this.options,
  10921. userOptions = this.userOptions,
  10922. defaultColors = this.chart.options.colors,
  10923. counters = this.chart.counters,
  10924. color,
  10925. colorIndex;
  10926. color = options.color || defaultPlotOptions[this.type].color;
  10927. if (!color && !options.colorByPoint) {
  10928. if (defined(userOptions._colorIndex)) { // after Series.update()
  10929. colorIndex = userOptions._colorIndex;
  10930. } else {
  10931. userOptions._colorIndex = counters.color;
  10932. colorIndex = counters.color++;
  10933. }
  10934. color = defaultColors[colorIndex];
  10935. }
  10936. this.color = color;
  10937. counters.wrapColor(defaultColors.length);
  10938. },
  10939. /**
  10940. * Get the series' symbol
  10941. */
  10942. getSymbol: function () {
  10943. var series = this,
  10944. userOptions = series.userOptions,
  10945. seriesMarkerOption = series.options.marker,
  10946. chart = series.chart,
  10947. defaultSymbols = chart.options.symbols,
  10948. counters = chart.counters,
  10949. symbolIndex;
  10950. series.symbol = seriesMarkerOption.symbol;
  10951. if (!series.symbol) {
  10952. if (defined(userOptions._symbolIndex)) { // after Series.update()
  10953. symbolIndex = userOptions._symbolIndex;
  10954. } else {
  10955. userOptions._symbolIndex = counters.symbol;
  10956. symbolIndex = counters.symbol++;
  10957. }
  10958. series.symbol = defaultSymbols[symbolIndex];
  10959. }
  10960. // don't substract radius in image symbols (#604)
  10961. if (/^url/.test(series.symbol)) {
  10962. seriesMarkerOption.radius = 0;
  10963. }
  10964. counters.wrapSymbol(defaultSymbols.length);
  10965. },
  10966. drawLegendSymbol: LegendSymbolMixin.drawLineMarker,
  10967. /**
  10968. * Replace the series data with a new set of data
  10969. * @param {Object} data
  10970. * @param {Object} redraw
  10971. */
  10972. setData: function (data, redraw, animation, updatePoints) {
  10973. var series = this,
  10974. oldData = series.points,
  10975. oldDataLength = (oldData && oldData.length) || 0,
  10976. dataLength,
  10977. options = series.options,
  10978. chart = series.chart,
  10979. firstPoint = null,
  10980. xAxis = series.xAxis,
  10981. hasCategories = xAxis && !!xAxis.categories,
  10982. tooltipPoints = series.tooltipPoints,
  10983. i,
  10984. turboThreshold = options.turboThreshold,
  10985. pt,
  10986. xData = this.xData,
  10987. yData = this.yData,
  10988. pointArrayMap = series.pointArrayMap,
  10989. valueCount = pointArrayMap && pointArrayMap.length;
  10990. data = data || [];
  10991. dataLength = data.length;
  10992. redraw = pick(redraw, true);
  10993. // If the point count is the same as is was, just run Point.update which is
  10994. // cheaper, allows animation, and keeps references to points.
  10995. if (updatePoints !== false && dataLength && oldDataLength === dataLength && !series.cropped && !series.hasGroupedData) {
  10996. each(data, function (point, i) {
  10997. oldData[i].update(point, false);
  10998. });
  10999. } else {
  11000. // Reset properties
  11001. series.xIncrement = null;
  11002. series.pointRange = hasCategories ? 1 : options.pointRange;
  11003. series.colorCounter = 0; // for series with colorByPoint (#1547)
  11004. // Update parallel arrays
  11005. each(this.parallelArrays, function (key) {
  11006. series[key + 'Data'].length = 0;
  11007. });
  11008. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  11009. // first value is tested, and we assume that all the rest are defined the same
  11010. // way. Although the 'for' loops are similar, they are repeated inside each
  11011. // if-else conditional for max performance.
  11012. if (turboThreshold && dataLength > turboThreshold) {
  11013. // find the first non-null point
  11014. i = 0;
  11015. while (firstPoint === null && i < dataLength) {
  11016. firstPoint = data[i];
  11017. i++;
  11018. }
  11019. if (isNumber(firstPoint)) { // assume all points are numbers
  11020. var x = pick(options.pointStart, 0),
  11021. pointInterval = pick(options.pointInterval, 1);
  11022. for (i = 0; i < dataLength; i++) {
  11023. xData[i] = x;
  11024. yData[i] = data[i];
  11025. x += pointInterval;
  11026. }
  11027. series.xIncrement = x;
  11028. } else if (isArray(firstPoint)) { // assume all points are arrays
  11029. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  11030. for (i = 0; i < dataLength; i++) {
  11031. pt = data[i];
  11032. xData[i] = pt[0];
  11033. yData[i] = pt.slice(1, valueCount + 1);
  11034. }
  11035. } else { // [x, y]
  11036. for (i = 0; i < dataLength; i++) {
  11037. pt = data[i];
  11038. xData[i] = pt[0];
  11039. yData[i] = pt[1];
  11040. }
  11041. }
  11042. } else {
  11043. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  11044. }
  11045. } else {
  11046. for (i = 0; i < dataLength; i++) {
  11047. if (data[i] !== UNDEFINED) { // stray commas in oldIE
  11048. pt = { series: series };
  11049. series.pointClass.prototype.applyOptions.apply(pt, [data[i]]);
  11050. series.updateParallelArrays(pt, i);
  11051. if (hasCategories && pt.name) {
  11052. xAxis.names[pt.x] = pt.name; // #2046
  11053. }
  11054. }
  11055. }
  11056. }
  11057. // Forgetting to cast strings to numbers is a common caveat when handling CSV or JSON
  11058. if (isString(yData[0])) {
  11059. error(14, true);
  11060. }
  11061. series.data = [];
  11062. series.options.data = data;
  11063. //series.zData = zData;
  11064. // destroy old points
  11065. i = oldDataLength;
  11066. while (i--) {
  11067. if (oldData[i] && oldData[i].destroy) {
  11068. oldData[i].destroy();
  11069. }
  11070. }
  11071. if (tooltipPoints) { // #2594
  11072. tooltipPoints.length = 0;
  11073. }
  11074. // reset minRange (#878)
  11075. if (xAxis) {
  11076. xAxis.minRange = xAxis.userMinRange;
  11077. }
  11078. // redraw
  11079. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  11080. animation = false;
  11081. }
  11082. if (redraw) {
  11083. chart.redraw(animation);
  11084. }
  11085. },
  11086. /**
  11087. * Process the data by cropping away unused data points if the series is longer
  11088. * than the crop threshold. This saves computing time for lage series.
  11089. */
  11090. processData: function (force) {
  11091. var series = this,
  11092. processedXData = series.xData, // copied during slice operation below
  11093. processedYData = series.yData,
  11094. dataLength = processedXData.length,
  11095. croppedData,
  11096. cropStart = 0,
  11097. cropped,
  11098. distance,
  11099. closestPointRange,
  11100. xAxis = series.xAxis,
  11101. i, // loop variable
  11102. options = series.options,
  11103. cropThreshold = options.cropThreshold,
  11104. isCartesian = series.isCartesian;
  11105. // If the series data or axes haven't changed, don't go through this. Return false to pass
  11106. // the message on to override methods like in data grouping.
  11107. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  11108. return false;
  11109. }
  11110. // optionally filter out points outside the plot area
  11111. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  11112. var min = xAxis.min,
  11113. max = xAxis.max;
  11114. // it's outside current extremes
  11115. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  11116. processedXData = [];
  11117. processedYData = [];
  11118. // only crop if it's actually spilling out
  11119. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  11120. croppedData = this.cropData(series.xData, series.yData, min, max);
  11121. processedXData = croppedData.xData;
  11122. processedYData = croppedData.yData;
  11123. cropStart = croppedData.start;
  11124. cropped = true;
  11125. }
  11126. }
  11127. // Find the closest distance between processed points
  11128. for (i = processedXData.length - 1; i >= 0; i--) {
  11129. distance = processedXData[i] - processedXData[i - 1];
  11130. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  11131. closestPointRange = distance;
  11132. // Unsorted data is not supported by the line tooltip, as well as data grouping and
  11133. // navigation in Stock charts (#725) and width calculation of columns (#1900)
  11134. } else if (distance < 0 && series.requireSorting) {
  11135. error(15);
  11136. }
  11137. }
  11138. // Record the properties
  11139. series.cropped = cropped; // undefined or true
  11140. series.cropStart = cropStart;
  11141. series.processedXData = processedXData;
  11142. series.processedYData = processedYData;
  11143. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  11144. series.pointRange = closestPointRange || 1;
  11145. }
  11146. series.closestPointRange = closestPointRange;
  11147. },
  11148. /**
  11149. * Iterate over xData and crop values between min and max. Returns object containing crop start/end
  11150. * cropped xData with corresponding part of yData, dataMin and dataMax within the cropped range
  11151. */
  11152. cropData: function (xData, yData, min, max) {
  11153. var dataLength = xData.length,
  11154. cropStart = 0,
  11155. cropEnd = dataLength,
  11156. cropShoulder = pick(this.cropShoulder, 1), // line-type series need one point outside
  11157. i;
  11158. // iterate up to find slice start
  11159. for (i = 0; i < dataLength; i++) {
  11160. if (xData[i] >= min) {
  11161. cropStart = mathMax(0, i - cropShoulder);
  11162. break;
  11163. }
  11164. }
  11165. // proceed to find slice end
  11166. for (; i < dataLength; i++) {
  11167. if (xData[i] > max) {
  11168. cropEnd = i + cropShoulder;
  11169. break;
  11170. }
  11171. }
  11172. return {
  11173. xData: xData.slice(cropStart, cropEnd),
  11174. yData: yData.slice(cropStart, cropEnd),
  11175. start: cropStart,
  11176. end: cropEnd
  11177. };
  11178. },
  11179. /**
  11180. * Generate the data point after the data has been processed by cropping away
  11181. * unused points and optionally grouped in Highcharts Stock.
  11182. */
  11183. generatePoints: function () {
  11184. var series = this,
  11185. options = series.options,
  11186. dataOptions = options.data,
  11187. data = series.data,
  11188. dataLength,
  11189. processedXData = series.processedXData,
  11190. processedYData = series.processedYData,
  11191. pointClass = series.pointClass,
  11192. processedDataLength = processedXData.length,
  11193. cropStart = series.cropStart || 0,
  11194. cursor,
  11195. hasGroupedData = series.hasGroupedData,
  11196. point,
  11197. points = [],
  11198. i;
  11199. if (!data && !hasGroupedData) {
  11200. var arr = [];
  11201. arr.length = dataOptions.length;
  11202. data = series.data = arr;
  11203. }
  11204. for (i = 0; i < processedDataLength; i++) {
  11205. cursor = cropStart + i;
  11206. if (!hasGroupedData) {
  11207. if (data[cursor]) {
  11208. point = data[cursor];
  11209. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  11210. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  11211. }
  11212. points[i] = point;
  11213. } else {
  11214. // splat the y data in case of ohlc data array
  11215. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  11216. }
  11217. }
  11218. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  11219. // swithching view from non-grouped data to grouped data (#637)
  11220. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  11221. for (i = 0; i < dataLength; i++) {
  11222. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  11223. i += processedDataLength;
  11224. }
  11225. if (data[i]) {
  11226. data[i].destroyElements();
  11227. data[i].plotX = UNDEFINED; // #1003
  11228. }
  11229. }
  11230. }
  11231. series.data = data;
  11232. series.points = points;
  11233. },
  11234. /**
  11235. * Calculate Y extremes for visible data
  11236. */
  11237. getExtremes: function (yData) {
  11238. var xAxis = this.xAxis,
  11239. yAxis = this.yAxis,
  11240. xData = this.processedXData,
  11241. yDataLength,
  11242. activeYData = [],
  11243. activeCounter = 0,
  11244. xExtremes = xAxis.getExtremes(), // #2117, need to compensate for log X axis
  11245. xMin = xExtremes.min,
  11246. xMax = xExtremes.max,
  11247. validValue,
  11248. withinRange,
  11249. dataMin,
  11250. dataMax,
  11251. x,
  11252. y,
  11253. i,
  11254. j;
  11255. yData = yData || this.stackedYData || this.processedYData;
  11256. yDataLength = yData.length;
  11257. for (i = 0; i < yDataLength; i++) {
  11258. x = xData[i];
  11259. y = yData[i];
  11260. // For points within the visible range, including the first point outside the
  11261. // visible range, consider y extremes
  11262. validValue = y !== null && y !== UNDEFINED && (!yAxis.isLog || (y.length || y > 0));
  11263. withinRange = this.getExtremesFromAll || this.cropped || ((xData[i + 1] || x) >= xMin &&
  11264. (xData[i - 1] || x) <= xMax);
  11265. if (validValue && withinRange) {
  11266. j = y.length;
  11267. if (j) { // array, like ohlc or range data
  11268. while (j--) {
  11269. if (y[j] !== null) {
  11270. activeYData[activeCounter++] = y[j];
  11271. }
  11272. }
  11273. } else {
  11274. activeYData[activeCounter++] = y;
  11275. }
  11276. }
  11277. }
  11278. this.dataMin = pick(dataMin, arrayMin(activeYData));
  11279. this.dataMax = pick(dataMax, arrayMax(activeYData));
  11280. },
  11281. /**
  11282. * Translate data points from raw data values to chart specific positioning data
  11283. * needed later in drawPoints, drawGraph and drawTracker.
  11284. */
  11285. translate: function () {
  11286. if (!this.processedXData) { // hidden series
  11287. this.processData();
  11288. }
  11289. this.generatePoints();
  11290. var series = this,
  11291. options = series.options,
  11292. stacking = options.stacking,
  11293. xAxis = series.xAxis,
  11294. categories = xAxis.categories,
  11295. yAxis = series.yAxis,
  11296. points = series.points,
  11297. dataLength = points.length,
  11298. hasModifyValue = !!series.modifyValue,
  11299. i,
  11300. pointPlacement = options.pointPlacement,
  11301. dynamicallyPlaced = pointPlacement === 'between' || isNumber(pointPlacement),
  11302. threshold = options.threshold;
  11303. // Translate each point
  11304. for (i = 0; i < dataLength; i++) {
  11305. var point = points[i],
  11306. xValue = point.x,
  11307. yValue = point.y,
  11308. yBottom = point.low,
  11309. stack = stacking && yAxis.stacks[(series.negStacks && yValue < threshold ? '-' : '') + series.stackKey],
  11310. pointStack,
  11311. stackValues;
  11312. // Discard disallowed y values for log axes
  11313. if (yAxis.isLog && yValue <= 0) {
  11314. point.y = yValue = null;
  11315. }
  11316. // Get the plotX translation
  11317. point.plotX = xAxis.translate(xValue, 0, 0, 0, 1, pointPlacement, this.type === 'flags'); // Math.round fixes #591
  11318. // Calculate the bottom y value for stacked series
  11319. if (stacking && series.visible && stack && stack[xValue]) {
  11320. pointStack = stack[xValue];
  11321. stackValues = pointStack.points[series.index];
  11322. yBottom = stackValues[0];
  11323. yValue = stackValues[1];
  11324. if (yBottom === 0) {
  11325. yBottom = pick(threshold, yAxis.min);
  11326. }
  11327. if (yAxis.isLog && yBottom <= 0) { // #1200, #1232
  11328. yBottom = null;
  11329. }
  11330. point.total = point.stackTotal = pointStack.total;
  11331. point.percentage = pointStack.total && (point.y / pointStack.total * 100);
  11332. point.stackY = yValue;
  11333. // Place the stack label
  11334. pointStack.setOffset(series.pointXOffset || 0, series.barW || 0);
  11335. }
  11336. // Set translated yBottom or remove it
  11337. point.yBottom = defined(yBottom) ?
  11338. yAxis.translate(yBottom, 0, 1, 0, 1) :
  11339. null;
  11340. // general hook, used for Highstock compare mode
  11341. if (hasModifyValue) {
  11342. yValue = series.modifyValue(yValue, point);
  11343. }
  11344. // Set the the plotY value, reset it for redraws
  11345. point.plotY = (typeof yValue === 'number' && yValue !== Infinity) ?
  11346. //mathRound(yAxis.translate(yValue, 0, 1, 0, 1) * 10) / 10 : // Math.round fixes #591
  11347. yAxis.translate(yValue, 0, 1, 0, 1) :
  11348. UNDEFINED;
  11349. // Set client related positions for mouse tracking
  11350. point.clientX = dynamicallyPlaced ? xAxis.translate(xValue, 0, 0, 0, 1) : point.plotX; // #1514
  11351. point.negative = point.y < (threshold || 0);
  11352. // some API data
  11353. point.category = categories && categories[point.x] !== UNDEFINED ?
  11354. categories[point.x] : point.x;
  11355. }
  11356. // now that we have the cropped data, build the segments
  11357. series.getSegments();
  11358. },
  11359. /**
  11360. * Animate in the series
  11361. */
  11362. animate: function (init) {
  11363. var series = this,
  11364. chart = series.chart,
  11365. renderer = chart.renderer,
  11366. clipRect,
  11367. markerClipRect,
  11368. animation = series.options.animation,
  11369. clipBox = chart.clipBox,
  11370. inverted = chart.inverted,
  11371. sharedClipKey;
  11372. // Animation option is set to true
  11373. if (animation && !isObject(animation)) {
  11374. animation = defaultPlotOptions[series.type].animation;
  11375. }
  11376. sharedClipKey = '_sharedClip' + animation.duration + animation.easing;
  11377. // Initialize the animation. Set up the clipping rectangle.
  11378. if (init) {
  11379. // If a clipping rectangle with the same properties is currently present in the chart, use that.
  11380. clipRect = chart[sharedClipKey];
  11381. markerClipRect = chart[sharedClipKey + 'm'];
  11382. if (!clipRect) {
  11383. chart[sharedClipKey] = clipRect = renderer.clipRect(
  11384. extend(clipBox, { width: 0 })
  11385. );
  11386. chart[sharedClipKey + 'm'] = markerClipRect = renderer.clipRect(
  11387. -99, // include the width of the first marker
  11388. inverted ? -chart.plotLeft : -chart.plotTop,
  11389. 99,
  11390. inverted ? chart.chartWidth : chart.chartHeight
  11391. );
  11392. }
  11393. series.group.clip(clipRect);
  11394. series.markerGroup.clip(markerClipRect);
  11395. series.sharedClipKey = sharedClipKey;
  11396. // Run the animation
  11397. } else {
  11398. clipRect = chart[sharedClipKey];
  11399. if (clipRect) {
  11400. clipRect.animate({
  11401. width: chart.plotSizeX
  11402. }, animation);
  11403. chart[sharedClipKey + 'm'].animate({
  11404. width: chart.plotSizeX + 99
  11405. }, animation);
  11406. }
  11407. // Delete this function to allow it only once
  11408. series.animate = null;
  11409. // Call the afterAnimate function on animation complete (but don't overwrite the animation.complete option
  11410. // which should be available to the user).
  11411. series.animationTimeout = setTimeout(function () {
  11412. series.afterAnimate();
  11413. }, animation.duration);
  11414. }
  11415. },
  11416. /**
  11417. * This runs after animation to land on the final plot clipping
  11418. */
  11419. afterAnimate: function () {
  11420. var chart = this.chart,
  11421. sharedClipKey = this.sharedClipKey,
  11422. group = this.group;
  11423. if (group && this.options.clip !== false) {
  11424. group.clip(chart.clipRect);
  11425. this.markerGroup.clip(); // no clip
  11426. }
  11427. // Remove the shared clipping rectancgle when all series are shown
  11428. setTimeout(function () {
  11429. if (sharedClipKey && chart[sharedClipKey]) {
  11430. chart[sharedClipKey] = chart[sharedClipKey].destroy();
  11431. chart[sharedClipKey + 'm'] = chart[sharedClipKey + 'm'].destroy();
  11432. }
  11433. }, 100);
  11434. },
  11435. /**
  11436. * Draw the markers
  11437. */
  11438. drawPoints: function () {
  11439. var series = this,
  11440. pointAttr,
  11441. points = series.points,
  11442. chart = series.chart,
  11443. plotX,
  11444. plotY,
  11445. i,
  11446. point,
  11447. radius,
  11448. symbol,
  11449. isImage,
  11450. graphic,
  11451. options = series.options,
  11452. seriesMarkerOptions = options.marker,
  11453. seriesPointAttr = series.pointAttr[''],
  11454. pointMarkerOptions,
  11455. enabled,
  11456. isInside,
  11457. markerGroup = series.markerGroup;
  11458. if (seriesMarkerOptions.enabled || series._hasPointMarkers) {
  11459. i = points.length;
  11460. while (i--) {
  11461. point = points[i];
  11462. plotX = mathFloor(point.plotX); // #1843
  11463. plotY = point.plotY;
  11464. graphic = point.graphic;
  11465. pointMarkerOptions = point.marker || {};
  11466. enabled = (seriesMarkerOptions.enabled && pointMarkerOptions.enabled === UNDEFINED) || pointMarkerOptions.enabled;
  11467. isInside = chart.isInsidePlot(mathRound(plotX), plotY, chart.inverted); // #1858
  11468. // only draw the point if y is defined
  11469. if (enabled && plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  11470. // shortcuts
  11471. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE] || seriesPointAttr;
  11472. radius = pointAttr.r;
  11473. symbol = pick(pointMarkerOptions.symbol, series.symbol);
  11474. isImage = symbol.indexOf('url') === 0;
  11475. if (graphic) { // update
  11476. graphic
  11477. .attr({ // Since the marker group isn't clipped, each individual marker must be toggled
  11478. visibility: isInside ? 'inherit' : HIDDEN
  11479. })
  11480. .animate(extend({
  11481. x: plotX - radius,
  11482. y: plotY - radius
  11483. }, graphic.symbolName ? { // don't apply to image symbols #507
  11484. width: 2 * radius,
  11485. height: 2 * radius
  11486. } : {}));
  11487. } else if (isInside && (radius > 0 || isImage)) {
  11488. point.graphic = graphic = chart.renderer.symbol(
  11489. symbol,
  11490. plotX - radius,
  11491. plotY - radius,
  11492. 2 * radius,
  11493. 2 * radius
  11494. )
  11495. .attr(pointAttr)
  11496. .add(markerGroup);
  11497. }
  11498. } else if (graphic) {
  11499. point.graphic = graphic.destroy(); // #1269
  11500. }
  11501. }
  11502. }
  11503. },
  11504. /**
  11505. * Convert state properties from API naming conventions to SVG attributes
  11506. *
  11507. * @param {Object} options API options object
  11508. * @param {Object} base1 SVG attribute object to inherit from
  11509. * @param {Object} base2 Second level SVG attribute object to inherit from
  11510. */
  11511. convertAttribs: function (options, base1, base2, base3) {
  11512. var conversion = this.pointAttrToOptions,
  11513. attr,
  11514. option,
  11515. obj = {};
  11516. options = options || {};
  11517. base1 = base1 || {};
  11518. base2 = base2 || {};
  11519. base3 = base3 || {};
  11520. for (attr in conversion) {
  11521. option = conversion[attr];
  11522. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  11523. }
  11524. return obj;
  11525. },
  11526. /**
  11527. * Get the state attributes. Each series type has its own set of attributes
  11528. * that are allowed to change on a point's state change. Series wide attributes are stored for
  11529. * all series, and additionally point specific attributes are stored for all
  11530. * points with individual marker options. If such options are not defined for the point,
  11531. * a reference to the series wide attributes is stored in point.pointAttr.
  11532. */
  11533. getAttribs: function () {
  11534. var series = this,
  11535. seriesOptions = series.options,
  11536. normalOptions = defaultPlotOptions[series.type].marker ? seriesOptions.marker : seriesOptions,
  11537. stateOptions = normalOptions.states,
  11538. stateOptionsHover = stateOptions[HOVER_STATE],
  11539. pointStateOptionsHover,
  11540. seriesColor = series.color,
  11541. normalDefaults = {
  11542. stroke: seriesColor,
  11543. fill: seriesColor
  11544. },
  11545. points = series.points || [], // #927
  11546. i,
  11547. point,
  11548. seriesPointAttr = [],
  11549. pointAttr,
  11550. pointAttrToOptions = series.pointAttrToOptions,
  11551. hasPointSpecificOptions = series.hasPointSpecificOptions,
  11552. negativeColor = seriesOptions.negativeColor,
  11553. defaultLineColor = normalOptions.lineColor,
  11554. defaultFillColor = normalOptions.fillColor,
  11555. turboThreshold = seriesOptions.turboThreshold,
  11556. attr,
  11557. key;
  11558. // series type specific modifications
  11559. if (seriesOptions.marker) { // line, spline, area, areaspline, scatter
  11560. // if no hover radius is given, default to normal radius + 2
  11561. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + 2;
  11562. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + 1;
  11563. } else { // column, bar, pie
  11564. // if no hover color is given, brighten the normal color
  11565. stateOptionsHover.color = stateOptionsHover.color ||
  11566. Color(stateOptionsHover.color || seriesColor)
  11567. .brighten(stateOptionsHover.brightness).get();
  11568. }
  11569. // general point attributes for the series normal state
  11570. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  11571. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  11572. each([HOVER_STATE, SELECT_STATE], function (state) {
  11573. seriesPointAttr[state] =
  11574. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  11575. });
  11576. // set it
  11577. series.pointAttr = seriesPointAttr;
  11578. // Generate the point-specific attribute collections if specific point
  11579. // options are given. If not, create a referance to the series wide point
  11580. // attributes
  11581. i = points.length;
  11582. if (!turboThreshold || i < turboThreshold || hasPointSpecificOptions) {
  11583. while (i--) {
  11584. point = points[i];
  11585. normalOptions = (point.options && point.options.marker) || point.options;
  11586. if (normalOptions && normalOptions.enabled === false) {
  11587. normalOptions.radius = 0;
  11588. }
  11589. if (point.negative && negativeColor) {
  11590. point.color = point.fillColor = negativeColor;
  11591. }
  11592. hasPointSpecificOptions = seriesOptions.colorByPoint || point.color; // #868
  11593. // check if the point has specific visual options
  11594. if (point.options) {
  11595. for (key in pointAttrToOptions) {
  11596. if (defined(normalOptions[pointAttrToOptions[key]])) {
  11597. hasPointSpecificOptions = true;
  11598. }
  11599. }
  11600. }
  11601. // a specific marker config object is defined for the individual point:
  11602. // create it's own attribute collection
  11603. if (hasPointSpecificOptions) {
  11604. normalOptions = normalOptions || {};
  11605. pointAttr = [];
  11606. stateOptions = normalOptions.states || {}; // reassign for individual point
  11607. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  11608. // Handle colors for column and pies
  11609. if (!seriesOptions.marker) { // column, bar, point
  11610. // If no hover color is given, brighten the normal color. #1619, #2579
  11611. pointStateOptionsHover.color = pointStateOptionsHover.color || (!point.options.color && stateOptionsHover.color) ||
  11612. Color(point.color)
  11613. .brighten(pointStateOptionsHover.brightness || stateOptionsHover.brightness)
  11614. .get();
  11615. }
  11616. // normal point state inherits series wide normal state
  11617. attr = { color: point.color }; // #868
  11618. if (!defaultFillColor) { // Individual point color or negative color markers (#2219)
  11619. attr.fillColor = point.color;
  11620. }
  11621. if (!defaultLineColor) {
  11622. attr.lineColor = point.color; // Bubbles take point color, line markers use white
  11623. }
  11624. pointAttr[NORMAL_STATE] = series.convertAttribs(extend(attr, normalOptions), seriesPointAttr[NORMAL_STATE]);
  11625. // inherit from point normal and series hover
  11626. pointAttr[HOVER_STATE] = series.convertAttribs(
  11627. stateOptions[HOVER_STATE],
  11628. seriesPointAttr[HOVER_STATE],
  11629. pointAttr[NORMAL_STATE]
  11630. );
  11631. // inherit from point normal and series hover
  11632. pointAttr[SELECT_STATE] = series.convertAttribs(
  11633. stateOptions[SELECT_STATE],
  11634. seriesPointAttr[SELECT_STATE],
  11635. pointAttr[NORMAL_STATE]
  11636. );
  11637. // no marker config object is created: copy a reference to the series-wide
  11638. // attribute collection
  11639. } else {
  11640. pointAttr = seriesPointAttr;
  11641. }
  11642. point.pointAttr = pointAttr;
  11643. }
  11644. }
  11645. },
  11646. /**
  11647. * Clear DOM objects and free up memory
  11648. */
  11649. destroy: function () {
  11650. var series = this,
  11651. chart = series.chart,
  11652. issue134 = /AppleWebKit\/533/.test(userAgent),
  11653. destroy,
  11654. i,
  11655. data = series.data || [],
  11656. point,
  11657. prop,
  11658. axis;
  11659. // add event hook
  11660. fireEvent(series, 'destroy');
  11661. // remove all events
  11662. removeEvent(series);
  11663. // erase from axes
  11664. each(series.axisTypes || [], function (AXIS) {
  11665. axis = series[AXIS];
  11666. if (axis) {
  11667. erase(axis.series, series);
  11668. axis.isDirty = axis.forceRedraw = true;
  11669. }
  11670. });
  11671. // remove legend items
  11672. if (series.legendItem) {
  11673. series.chart.legend.destroyItem(series);
  11674. }
  11675. // destroy all points with their elements
  11676. i = data.length;
  11677. while (i--) {
  11678. point = data[i];
  11679. if (point && point.destroy) {
  11680. point.destroy();
  11681. }
  11682. }
  11683. series.points = null;
  11684. // Clear the animation timeout if we are destroying the series during initial animation
  11685. clearTimeout(series.animationTimeout);
  11686. // destroy all SVGElements associated to the series
  11687. each(['area', 'graph', 'dataLabelsGroup', 'group', 'markerGroup', 'tracker',
  11688. 'graphNeg', 'areaNeg', 'posClip', 'negClip'], function (prop) {
  11689. if (series[prop]) {
  11690. // issue 134 workaround
  11691. destroy = issue134 && prop === 'group' ?
  11692. 'hide' :
  11693. 'destroy';
  11694. series[prop][destroy]();
  11695. }
  11696. });
  11697. // remove from hoverSeries
  11698. if (chart.hoverSeries === series) {
  11699. chart.hoverSeries = null;
  11700. }
  11701. erase(chart.series, series);
  11702. // clear all members
  11703. for (prop in series) {
  11704. delete series[prop];
  11705. }
  11706. },
  11707. /**
  11708. * Return the graph path of a segment
  11709. */
  11710. getSegmentPath: function (segment) {
  11711. var series = this,
  11712. segmentPath = [],
  11713. step = series.options.step;
  11714. // build the segment line
  11715. each(segment, function (point, i) {
  11716. var plotX = point.plotX,
  11717. plotY = point.plotY,
  11718. lastPoint;
  11719. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  11720. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  11721. } else {
  11722. // moveTo or lineTo
  11723. segmentPath.push(i ? L : M);
  11724. // step line?
  11725. if (step && i) {
  11726. lastPoint = segment[i - 1];
  11727. if (step === 'right') {
  11728. segmentPath.push(
  11729. lastPoint.plotX,
  11730. plotY
  11731. );
  11732. } else if (step === 'center') {
  11733. segmentPath.push(
  11734. (lastPoint.plotX + plotX) / 2,
  11735. lastPoint.plotY,
  11736. (lastPoint.plotX + plotX) / 2,
  11737. plotY
  11738. );
  11739. } else {
  11740. segmentPath.push(
  11741. plotX,
  11742. lastPoint.plotY
  11743. );
  11744. }
  11745. }
  11746. // normal line to next point
  11747. segmentPath.push(
  11748. point.plotX,
  11749. point.plotY
  11750. );
  11751. }
  11752. });
  11753. return segmentPath;
  11754. },
  11755. /**
  11756. * Get the graph path
  11757. */
  11758. getGraphPath: function () {
  11759. var series = this,
  11760. graphPath = [],
  11761. segmentPath,
  11762. singlePoints = []; // used in drawTracker
  11763. // Divide into segments and build graph and area paths
  11764. each(series.segments, function (segment) {
  11765. segmentPath = series.getSegmentPath(segment);
  11766. // add the segment to the graph, or a single point for tracking
  11767. if (segment.length > 1) {
  11768. graphPath = graphPath.concat(segmentPath);
  11769. } else {
  11770. singlePoints.push(segment[0]);
  11771. }
  11772. });
  11773. // Record it for use in drawGraph and drawTracker, and return graphPath
  11774. series.singlePoints = singlePoints;
  11775. series.graphPath = graphPath;
  11776. return graphPath;
  11777. },
  11778. /**
  11779. * Draw the actual graph
  11780. */
  11781. drawGraph: function () {
  11782. var series = this,
  11783. options = this.options,
  11784. props = [['graph', options.lineColor || this.color]],
  11785. lineWidth = options.lineWidth,
  11786. dashStyle = options.dashStyle,
  11787. roundCap = options.linecap !== 'square',
  11788. graphPath = this.getGraphPath(),
  11789. negativeColor = options.negativeColor;
  11790. if (negativeColor) {
  11791. props.push(['graphNeg', negativeColor]);
  11792. }
  11793. // draw the graph
  11794. each(props, function (prop, i) {
  11795. var graphKey = prop[0],
  11796. graph = series[graphKey],
  11797. attribs;
  11798. if (graph) {
  11799. stop(graph); // cancel running animations, #459
  11800. graph.animate({ d: graphPath });
  11801. } else if (lineWidth && graphPath.length) { // #1487
  11802. attribs = {
  11803. stroke: prop[1],
  11804. 'stroke-width': lineWidth,
  11805. fill: NONE,
  11806. zIndex: 1 // #1069
  11807. };
  11808. if (dashStyle) {
  11809. attribs.dashstyle = dashStyle;
  11810. } else if (roundCap) {
  11811. attribs['stroke-linecap'] = attribs['stroke-linejoin'] = 'round';
  11812. }
  11813. series[graphKey] = series.chart.renderer.path(graphPath)
  11814. .attr(attribs)
  11815. .add(series.group)
  11816. .shadow(!i && options.shadow);
  11817. }
  11818. });
  11819. },
  11820. /**
  11821. * Clip the graphs into the positive and negative coloured graphs
  11822. */
  11823. clipNeg: function () {
  11824. var options = this.options,
  11825. chart = this.chart,
  11826. renderer = chart.renderer,
  11827. negativeColor = options.negativeColor || options.negativeFillColor,
  11828. translatedThreshold,
  11829. posAttr,
  11830. negAttr,
  11831. graph = this.graph,
  11832. area = this.area,
  11833. posClip = this.posClip,
  11834. negClip = this.negClip,
  11835. chartWidth = chart.chartWidth,
  11836. chartHeight = chart.chartHeight,
  11837. chartSizeMax = mathMax(chartWidth, chartHeight),
  11838. yAxis = this.yAxis,
  11839. above,
  11840. below;
  11841. if (negativeColor && (graph || area)) {
  11842. translatedThreshold = mathRound(yAxis.toPixels(options.threshold || 0, true));
  11843. if (translatedThreshold < 0) {
  11844. chartSizeMax -= translatedThreshold; // #2534
  11845. }
  11846. above = {
  11847. x: 0,
  11848. y: 0,
  11849. width: chartSizeMax,
  11850. height: translatedThreshold
  11851. };
  11852. below = {
  11853. x: 0,
  11854. y: translatedThreshold,
  11855. width: chartSizeMax,
  11856. height: chartSizeMax
  11857. };
  11858. if (chart.inverted) {
  11859. above.height = below.y = chart.plotWidth - translatedThreshold;
  11860. if (renderer.isVML) {
  11861. above = {
  11862. x: chart.plotWidth - translatedThreshold - chart.plotLeft,
  11863. y: 0,
  11864. width: chartWidth,
  11865. height: chartHeight
  11866. };
  11867. below = {
  11868. x: translatedThreshold + chart.plotLeft - chartWidth,
  11869. y: 0,
  11870. width: chart.plotLeft + translatedThreshold,
  11871. height: chartWidth
  11872. };
  11873. }
  11874. }
  11875. if (yAxis.reversed) {
  11876. posAttr = below;
  11877. negAttr = above;
  11878. } else {
  11879. posAttr = above;
  11880. negAttr = below;
  11881. }
  11882. if (posClip) { // update
  11883. posClip.animate(posAttr);
  11884. negClip.animate(negAttr);
  11885. } else {
  11886. this.posClip = posClip = renderer.clipRect(posAttr);
  11887. this.negClip = negClip = renderer.clipRect(negAttr);
  11888. if (graph && this.graphNeg) {
  11889. graph.clip(posClip);
  11890. this.graphNeg.clip(negClip);
  11891. }
  11892. if (area) {
  11893. area.clip(posClip);
  11894. this.areaNeg.clip(negClip);
  11895. }
  11896. }
  11897. }
  11898. },
  11899. /**
  11900. * Initialize and perform group inversion on series.group and series.markerGroup
  11901. */
  11902. invertGroups: function () {
  11903. var series = this,
  11904. chart = series.chart;
  11905. // Pie, go away (#1736)
  11906. if (!series.xAxis) {
  11907. return;
  11908. }
  11909. // A fixed size is needed for inversion to work
  11910. function setInvert() {
  11911. var size = {
  11912. width: series.yAxis.len,
  11913. height: series.xAxis.len
  11914. };
  11915. each(['group', 'markerGroup'], function (groupName) {
  11916. if (series[groupName]) {
  11917. series[groupName].attr(size).invert();
  11918. }
  11919. });
  11920. }
  11921. addEvent(chart, 'resize', setInvert); // do it on resize
  11922. addEvent(series, 'destroy', function () {
  11923. removeEvent(chart, 'resize', setInvert);
  11924. });
  11925. // Do it now
  11926. setInvert(); // do it now
  11927. // On subsequent render and redraw, just do setInvert without setting up events again
  11928. series.invertGroups = setInvert;
  11929. },
  11930. /**
  11931. * General abstraction for creating plot groups like series.group, series.dataLabelsGroup and
  11932. * series.markerGroup. On subsequent calls, the group will only be adjusted to the updated plot size.
  11933. */
  11934. plotGroup: function (prop, name, visibility, zIndex, parent) {
  11935. var group = this[prop],
  11936. isNew = !group;
  11937. // Generate it on first call
  11938. if (isNew) {
  11939. this[prop] = group = this.chart.renderer.g(name)
  11940. .attr({
  11941. visibility: visibility,
  11942. zIndex: zIndex || 0.1 // IE8 needs this
  11943. })
  11944. .add(parent);
  11945. }
  11946. // Place it on first and subsequent (redraw) calls
  11947. group[isNew ? 'attr' : 'animate'](this.getPlotBox());
  11948. return group;
  11949. },
  11950. /**
  11951. * Get the translation and scale for the plot area of this series
  11952. */
  11953. getPlotBox: function () {
  11954. return {
  11955. translateX: this.xAxis ? this.xAxis.left : this.chart.plotLeft,
  11956. translateY: this.yAxis ? this.yAxis.top : this.chart.plotTop,
  11957. scaleX: 1, // #1623
  11958. scaleY: 1
  11959. };
  11960. },
  11961. /**
  11962. * Render the graph and markers
  11963. */
  11964. render: function () {
  11965. var series = this,
  11966. chart = series.chart,
  11967. group,
  11968. options = series.options,
  11969. animation = options.animation,
  11970. doAnimation = animation && !!series.animate &&
  11971. chart.renderer.isSVG, // this animation doesn't work in IE8 quirks when the group div is hidden,
  11972. // and looks bad in other oldIE
  11973. visibility = series.visible ? VISIBLE : HIDDEN,
  11974. zIndex = options.zIndex,
  11975. hasRendered = series.hasRendered,
  11976. chartSeriesGroup = chart.seriesGroup;
  11977. // the group
  11978. group = series.plotGroup(
  11979. 'group',
  11980. 'series',
  11981. visibility,
  11982. zIndex,
  11983. chartSeriesGroup
  11984. );
  11985. series.markerGroup = series.plotGroup(
  11986. 'markerGroup',
  11987. 'markers',
  11988. visibility,
  11989. zIndex,
  11990. chartSeriesGroup
  11991. );
  11992. // initiate the animation
  11993. if (doAnimation) {
  11994. series.animate(true);
  11995. }
  11996. // cache attributes for shapes
  11997. series.getAttribs();
  11998. // SVGRenderer needs to know this before drawing elements (#1089, #1795)
  11999. group.inverted = series.isCartesian ? chart.inverted : false;
  12000. // draw the graph if any
  12001. if (series.drawGraph) {
  12002. series.drawGraph();
  12003. series.clipNeg();
  12004. }
  12005. // draw the data labels (inn pies they go before the points)
  12006. if (series.drawDataLabels) {
  12007. series.drawDataLabels();
  12008. }
  12009. // draw the points
  12010. if (series.visible) {
  12011. series.drawPoints();
  12012. }
  12013. // draw the mouse tracking area
  12014. if (series.drawTracker && series.options.enableMouseTracking !== false) {
  12015. series.drawTracker();
  12016. }
  12017. // Handle inverted series and tracker groups
  12018. if (chart.inverted) {
  12019. series.invertGroups();
  12020. }
  12021. // Initial clipping, must be defined after inverting groups for VML
  12022. if (options.clip !== false && !series.sharedClipKey && !hasRendered) {
  12023. group.clip(chart.clipRect);
  12024. }
  12025. // Run the animation
  12026. if (doAnimation) {
  12027. series.animate();
  12028. } else if (!hasRendered) {
  12029. series.afterAnimate();
  12030. }
  12031. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12032. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12033. series.hasRendered = true;
  12034. },
  12035. /**
  12036. * Redraw the series after an update in the axes.
  12037. */
  12038. redraw: function () {
  12039. var series = this,
  12040. chart = series.chart,
  12041. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  12042. group = series.group,
  12043. xAxis = series.xAxis,
  12044. yAxis = series.yAxis;
  12045. // reposition on resize
  12046. if (group) {
  12047. if (chart.inverted) {
  12048. group.attr({
  12049. width: chart.plotWidth,
  12050. height: chart.plotHeight
  12051. });
  12052. }
  12053. group.animate({
  12054. translateX: pick(xAxis && xAxis.left, chart.plotLeft),
  12055. translateY: pick(yAxis && yAxis.top, chart.plotTop)
  12056. });
  12057. }
  12058. series.translate();
  12059. series.setTooltipPoints(true);
  12060. series.render();
  12061. if (wasDirtyData) {
  12062. fireEvent(series, 'updatedData');
  12063. }
  12064. }
  12065. }; // end Series prototype
  12066. /**
  12067. * The class for stack items
  12068. */
  12069. function StackItem(axis, options, isNegative, x, stackOption, stacking) {
  12070. var inverted = axis.chart.inverted;
  12071. this.axis = axis;
  12072. // Tells if the stack is negative
  12073. this.isNegative = isNegative;
  12074. // Save the options to be able to style the label
  12075. this.options = options;
  12076. // Save the x value to be able to position the label later
  12077. this.x = x;
  12078. // Initialize total value
  12079. this.total = null;
  12080. // This will keep each points' extremes stored by series.index
  12081. this.points = {};
  12082. // Save the stack option on the series configuration object, and whether to treat it as percent
  12083. this.stack = stackOption;
  12084. this.percent = stacking === 'percent';
  12085. // The align options and text align varies on whether the stack is negative and
  12086. // if the chart is inverted or not.
  12087. // First test the user supplied value, then use the dynamic.
  12088. this.alignOptions = {
  12089. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  12090. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  12091. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  12092. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  12093. };
  12094. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  12095. }
  12096. StackItem.prototype = {
  12097. destroy: function () {
  12098. destroyObjectProperties(this, this.axis);
  12099. },
  12100. /**
  12101. * Renders the stack total label and adds it to the stack label group.
  12102. */
  12103. render: function (group) {
  12104. var options = this.options,
  12105. formatOption = options.format,
  12106. str = formatOption ?
  12107. format(formatOption, this) :
  12108. options.formatter.call(this); // format the text in the label
  12109. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  12110. if (this.label) {
  12111. this.label.attr({text: str, visibility: HIDDEN});
  12112. // Create new label
  12113. } else {
  12114. this.label =
  12115. this.axis.chart.renderer.text(str, 0, 0, options.useHTML) // dummy positions, actual position updated with setOffset method in columnseries
  12116. .css(options.style) // apply style
  12117. .attr({
  12118. align: this.textAlign, // fix the text-anchor
  12119. rotation: options.rotation, // rotation
  12120. visibility: HIDDEN // hidden until setOffset is called
  12121. })
  12122. .add(group); // add to the labels-group
  12123. }
  12124. },
  12125. /**
  12126. * Sets the offset that the stack has from the x value and repositions the label.
  12127. */
  12128. setOffset: function (xOffset, xWidth) {
  12129. var stackItem = this,
  12130. axis = stackItem.axis,
  12131. chart = axis.chart,
  12132. inverted = chart.inverted,
  12133. neg = this.isNegative, // special treatment is needed for negative stacks
  12134. y = axis.translate(this.percent ? 100 : this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  12135. yZero = axis.translate(0), // stack origin
  12136. h = mathAbs(y - yZero), // stack height
  12137. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  12138. plotHeight = chart.plotHeight,
  12139. stackBox = { // this is the box for the complete stack
  12140. x: inverted ? (neg ? y : y - h) : x,
  12141. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  12142. width: inverted ? h : xWidth,
  12143. height: inverted ? xWidth : h
  12144. },
  12145. label = this.label,
  12146. alignAttr;
  12147. if (label) {
  12148. label.align(this.alignOptions, null, stackBox); // align the label to the box
  12149. // Set visibility (#678)
  12150. alignAttr = label.alignAttr;
  12151. label[this.options.crop === false || chart.isInsidePlot(alignAttr.x, alignAttr.y) ? 'show' : 'hide'](true);
  12152. }
  12153. }
  12154. };
  12155. // Stacking methods defined on the Axis prototype
  12156. /**
  12157. * Build the stacks from top down
  12158. */
  12159. Axis.prototype.buildStacks = function () {
  12160. var series = this.series,
  12161. reversedStacks = pick(this.options.reversedStacks, true),
  12162. i = series.length;
  12163. if (!this.isXAxis) {
  12164. this.usePercentage = false;
  12165. while (i--) {
  12166. series[reversedStacks ? i : series.length - i - 1].setStackedPoints();
  12167. }
  12168. // Loop up again to compute percent stack
  12169. if (this.usePercentage) {
  12170. for (i = 0; i < series.length; i++) {
  12171. series[i].setPercentStacks();
  12172. }
  12173. }
  12174. }
  12175. };
  12176. Axis.prototype.renderStackTotals = function () {
  12177. var axis = this,
  12178. chart = axis.chart,
  12179. renderer = chart.renderer,
  12180. stacks = axis.stacks,
  12181. stackKey,
  12182. oneStack,
  12183. stackCategory,
  12184. stackTotalGroup = axis.stackTotalGroup;
  12185. // Create a separate group for the stack total labels
  12186. if (!stackTotalGroup) {
  12187. axis.stackTotalGroup = stackTotalGroup =
  12188. renderer.g('stack-labels')
  12189. .attr({
  12190. visibility: VISIBLE,
  12191. zIndex: 6
  12192. })
  12193. .add();
  12194. }
  12195. // plotLeft/Top will change when y axis gets wider so we need to translate the
  12196. // stackTotalGroup at every render call. See bug #506 and #516
  12197. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  12198. // Render each stack total
  12199. for (stackKey in stacks) {
  12200. oneStack = stacks[stackKey];
  12201. for (stackCategory in oneStack) {
  12202. oneStack[stackCategory].render(stackTotalGroup);
  12203. }
  12204. }
  12205. };
  12206. // Stacking methods defnied for Series prototype
  12207. /**
  12208. * Adds series' points value to corresponding stack
  12209. */
  12210. Series.prototype.setStackedPoints = function () {
  12211. if (!this.options.stacking || (this.visible !== true && this.chart.options.chart.ignoreHiddenSeries !== false)) {
  12212. return;
  12213. }
  12214. var series = this,
  12215. xData = series.processedXData,
  12216. yData = series.processedYData,
  12217. stackedYData = [],
  12218. yDataLength = yData.length,
  12219. seriesOptions = series.options,
  12220. threshold = seriesOptions.threshold,
  12221. stackOption = seriesOptions.stack,
  12222. stacking = seriesOptions.stacking,
  12223. stackKey = series.stackKey,
  12224. negKey = '-' + stackKey,
  12225. negStacks = series.negStacks,
  12226. yAxis = series.yAxis,
  12227. stacks = yAxis.stacks,
  12228. oldStacks = yAxis.oldStacks,
  12229. isNegative,
  12230. stack,
  12231. other,
  12232. key,
  12233. i,
  12234. x,
  12235. y;
  12236. // loop over the non-null y values and read them into a local array
  12237. for (i = 0; i < yDataLength; i++) {
  12238. x = xData[i];
  12239. y = yData[i];
  12240. // Read stacked values into a stack based on the x value,
  12241. // the sign of y and the stack key. Stacking is also handled for null values (#739)
  12242. isNegative = negStacks && y < threshold;
  12243. key = isNegative ? negKey : stackKey;
  12244. // Create empty object for this stack if it doesn't exist yet
  12245. if (!stacks[key]) {
  12246. stacks[key] = {};
  12247. }
  12248. // Initialize StackItem for this x
  12249. if (!stacks[key][x]) {
  12250. if (oldStacks[key] && oldStacks[key][x]) {
  12251. stacks[key][x] = oldStacks[key][x];
  12252. stacks[key][x].total = null;
  12253. } else {
  12254. stacks[key][x] = new StackItem(yAxis, yAxis.options.stackLabels, isNegative, x, stackOption, stacking);
  12255. }
  12256. }
  12257. // If the StackItem doesn't exist, create it first
  12258. stack = stacks[key][x];
  12259. stack.points[series.index] = [stack.cum || 0];
  12260. // Add value to the stack total
  12261. if (stacking === 'percent') {
  12262. // Percent stacked column, totals are the same for the positive and negative stacks
  12263. other = isNegative ? stackKey : negKey;
  12264. if (negStacks && stacks[other] && stacks[other][x]) {
  12265. other = stacks[other][x];
  12266. stack.total = other.total = mathMax(other.total, stack.total) + mathAbs(y) || 0;
  12267. // Percent stacked areas
  12268. } else {
  12269. stack.total = correctFloat(stack.total + (mathAbs(y) || 0));
  12270. }
  12271. } else {
  12272. stack.total = correctFloat(stack.total + (y || 0));
  12273. }
  12274. stack.cum = (stack.cum || 0) + (y || 0);
  12275. stack.points[series.index].push(stack.cum);
  12276. stackedYData[i] = stack.cum;
  12277. }
  12278. if (stacking === 'percent') {
  12279. yAxis.usePercentage = true;
  12280. }
  12281. this.stackedYData = stackedYData; // To be used in getExtremes
  12282. // Reset old stacks
  12283. yAxis.oldStacks = {};
  12284. };
  12285. /**
  12286. * Iterate over all stacks and compute the absolute values to percent
  12287. */
  12288. Series.prototype.setPercentStacks = function () {
  12289. var series = this,
  12290. stackKey = series.stackKey,
  12291. stacks = series.yAxis.stacks,
  12292. processedXData = series.processedXData;
  12293. each([stackKey, '-' + stackKey], function (key) {
  12294. var i = processedXData.length,
  12295. x,
  12296. stack,
  12297. pointExtremes,
  12298. totalFactor;
  12299. while (i--) {
  12300. x = processedXData[i];
  12301. stack = stacks[key] && stacks[key][x];
  12302. pointExtremes = stack && stack.points[series.index];
  12303. if (pointExtremes) {
  12304. totalFactor = stack.total ? 100 / stack.total : 0;
  12305. pointExtremes[0] = correctFloat(pointExtremes[0] * totalFactor); // Y bottom value
  12306. pointExtremes[1] = correctFloat(pointExtremes[1] * totalFactor); // Y value
  12307. series.stackedYData[i] = pointExtremes[1];
  12308. }
  12309. }
  12310. });
  12311. };
  12312. // Extend the Chart prototype for dynamic methods
  12313. extend(Chart.prototype, {
  12314. /**
  12315. * Add a series dynamically after time
  12316. *
  12317. * @param {Object} options The config options
  12318. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  12319. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12320. * configuration
  12321. *
  12322. * @return {Object} series The newly created series object
  12323. */
  12324. addSeries: function (options, redraw, animation) {
  12325. var series,
  12326. chart = this;
  12327. if (options) {
  12328. redraw = pick(redraw, true); // defaults to true
  12329. fireEvent(chart, 'addSeries', { options: options }, function () {
  12330. series = chart.initSeries(options);
  12331. chart.isDirtyLegend = true; // the series array is out of sync with the display
  12332. chart.linkSeries();
  12333. if (redraw) {
  12334. chart.redraw(animation);
  12335. }
  12336. });
  12337. }
  12338. return series;
  12339. },
  12340. /**
  12341. * Add an axis to the chart
  12342. * @param {Object} options The axis option
  12343. * @param {Boolean} isX Whether it is an X axis or a value axis
  12344. */
  12345. addAxis: function (options, isX, redraw, animation) {
  12346. var key = isX ? 'xAxis' : 'yAxis',
  12347. chartOptions = this.options,
  12348. axis;
  12349. /*jslint unused: false*/
  12350. axis = new Axis(this, merge(options, {
  12351. index: this[key].length,
  12352. isX: isX
  12353. }));
  12354. /*jslint unused: true*/
  12355. // Push the new axis options to the chart options
  12356. chartOptions[key] = splat(chartOptions[key] || {});
  12357. chartOptions[key].push(options);
  12358. if (pick(redraw, true)) {
  12359. this.redraw(animation);
  12360. }
  12361. },
  12362. /**
  12363. * Dim the chart and show a loading text or symbol
  12364. * @param {String} str An optional text to show in the loading label instead of the default one
  12365. */
  12366. showLoading: function (str) {
  12367. var chart = this,
  12368. options = chart.options,
  12369. loadingDiv = chart.loadingDiv;
  12370. var loadingOptions = options.loading;
  12371. // create the layer at the first call
  12372. if (!loadingDiv) {
  12373. chart.loadingDiv = loadingDiv = createElement(DIV, {
  12374. className: PREFIX + 'loading'
  12375. }, extend(loadingOptions.style, {
  12376. zIndex: 10,
  12377. display: NONE
  12378. }), chart.container);
  12379. chart.loadingSpan = createElement(
  12380. 'span',
  12381. null,
  12382. loadingOptions.labelStyle,
  12383. loadingDiv
  12384. );
  12385. }
  12386. // update text
  12387. chart.loadingSpan.innerHTML = str || options.lang.loading;
  12388. // show it
  12389. if (!chart.loadingShown) {
  12390. css(loadingDiv, {
  12391. opacity: 0,
  12392. display: '',
  12393. left: chart.plotLeft + PX,
  12394. top: chart.plotTop + PX,
  12395. width: chart.plotWidth + PX,
  12396. height: chart.plotHeight + PX
  12397. });
  12398. animate(loadingDiv, {
  12399. opacity: loadingOptions.style.opacity
  12400. }, {
  12401. duration: loadingOptions.showDuration || 0
  12402. });
  12403. chart.loadingShown = true;
  12404. }
  12405. },
  12406. /**
  12407. * Hide the loading layer
  12408. */
  12409. hideLoading: function () {
  12410. var options = this.options,
  12411. loadingDiv = this.loadingDiv;
  12412. if (loadingDiv) {
  12413. animate(loadingDiv, {
  12414. opacity: 0
  12415. }, {
  12416. duration: options.loading.hideDuration || 100,
  12417. complete: function () {
  12418. css(loadingDiv, { display: NONE });
  12419. }
  12420. });
  12421. }
  12422. this.loadingShown = false;
  12423. }
  12424. });
  12425. // extend the Point prototype for dynamic methods
  12426. extend(Point.prototype, {
  12427. /**
  12428. * Update the point with new options (typically x/y data) and optionally redraw the series.
  12429. *
  12430. * @param {Object} options Point options as defined in the series.data array
  12431. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12432. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12433. * configuration
  12434. *
  12435. */
  12436. update: function (options, redraw, animation) {
  12437. var point = this,
  12438. series = point.series,
  12439. graphic = point.graphic,
  12440. i,
  12441. data = series.data,
  12442. chart = series.chart,
  12443. seriesOptions = series.options;
  12444. redraw = pick(redraw, true);
  12445. // fire the event with a default handler of doing the update
  12446. point.firePointEvent('update', { options: options }, function () {
  12447. point.applyOptions(options);
  12448. // update visuals
  12449. if (isObject(options)) {
  12450. series.getAttribs();
  12451. if (graphic) {
  12452. if (options && options.marker && options.marker.symbol) {
  12453. point.graphic = graphic.destroy();
  12454. } else {
  12455. graphic.attr(point.pointAttr[point.state || '']);
  12456. }
  12457. }
  12458. if (options && options.dataLabels && point.dataLabel) { // #2468
  12459. point.dataLabel = point.dataLabel.destroy();
  12460. }
  12461. }
  12462. // record changes in the parallel arrays
  12463. i = inArray(point, data);
  12464. series.updateParallelArrays(point, i);
  12465. seriesOptions.data[i] = point.options;
  12466. // redraw
  12467. series.isDirty = series.isDirtyData = true;
  12468. if (!series.fixedBox && series.hasCartesianSeries) { // #1906, #2320
  12469. chart.isDirtyBox = true;
  12470. }
  12471. if (seriesOptions.legendType === 'point') { // #1831, #1885
  12472. chart.legend.destroyItem(point);
  12473. }
  12474. if (redraw) {
  12475. chart.redraw(animation);
  12476. }
  12477. });
  12478. },
  12479. /**
  12480. * Remove a point and optionally redraw the series and if necessary the axes
  12481. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12482. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12483. * configuration
  12484. */
  12485. remove: function (redraw, animation) {
  12486. var point = this,
  12487. series = point.series,
  12488. points = series.points,
  12489. chart = series.chart,
  12490. i,
  12491. data = series.data;
  12492. setAnimation(animation, chart);
  12493. redraw = pick(redraw, true);
  12494. // fire the event with a default handler of removing the point
  12495. point.firePointEvent('remove', null, function () {
  12496. // splice all the parallel arrays
  12497. i = inArray(point, data);
  12498. if (data.length === points.length) {
  12499. points.splice(i, 1);
  12500. }
  12501. data.splice(i, 1);
  12502. series.options.data.splice(i, 1);
  12503. series.updateParallelArrays(point, 'splice', i, 1);
  12504. point.destroy();
  12505. // redraw
  12506. series.isDirty = true;
  12507. series.isDirtyData = true;
  12508. if (redraw) {
  12509. chart.redraw();
  12510. }
  12511. });
  12512. }
  12513. });
  12514. // Extend the series prototype for dynamic methods
  12515. extend(Series.prototype, {
  12516. /**
  12517. * Add a point dynamically after chart load time
  12518. * @param {Object} options Point options as given in series.data
  12519. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12520. * @param {Boolean} shift If shift is true, a point is shifted off the start
  12521. * of the series as one is appended to the end.
  12522. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12523. * configuration
  12524. */
  12525. addPoint: function (options, redraw, shift, animation) {
  12526. var series = this,
  12527. seriesOptions = series.options,
  12528. data = series.data,
  12529. graph = series.graph,
  12530. area = series.area,
  12531. chart = series.chart,
  12532. names = series.xAxis && series.xAxis.names,
  12533. currentShift = (graph && graph.shift) || 0,
  12534. dataOptions = seriesOptions.data,
  12535. point,
  12536. isInTheMiddle,
  12537. xData = series.xData,
  12538. x,
  12539. i;
  12540. setAnimation(animation, chart);
  12541. // Make graph animate sideways
  12542. if (shift) {
  12543. each([graph, area, series.graphNeg, series.areaNeg], function (shape) {
  12544. if (shape) {
  12545. shape.shift = currentShift + 1;
  12546. }
  12547. });
  12548. }
  12549. if (area) {
  12550. area.isArea = true; // needed in animation, both with and without shift
  12551. }
  12552. // Optional redraw, defaults to true
  12553. redraw = pick(redraw, true);
  12554. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  12555. // the Point instance will be created on demand and pushed to the series.data array.
  12556. point = { series: series };
  12557. series.pointClass.prototype.applyOptions.apply(point, [options]);
  12558. x = point.x;
  12559. // Get the insertion point
  12560. i = xData.length;
  12561. if (series.requireSorting && x < xData[i - 1]) {
  12562. isInTheMiddle = true;
  12563. while (i && xData[i - 1] > x) {
  12564. i--;
  12565. }
  12566. }
  12567. series.updateParallelArrays(point, 'splice', i, 0, 0); // insert undefined item
  12568. series.updateParallelArrays(point, i); // update it
  12569. if (names) {
  12570. names[x] = point.name;
  12571. }
  12572. dataOptions.splice(i, 0, options);
  12573. if (isInTheMiddle) {
  12574. series.data.splice(i, 0, null);
  12575. series.processData();
  12576. }
  12577. // Generate points to be added to the legend (#1329)
  12578. if (seriesOptions.legendType === 'point') {
  12579. series.generatePoints();
  12580. }
  12581. // Shift the first point off the parallel arrays
  12582. // todo: consider series.removePoint(i) method
  12583. if (shift) {
  12584. if (data[0] && data[0].remove) {
  12585. data[0].remove(false);
  12586. } else {
  12587. data.shift();
  12588. series.updateParallelArrays(point, 'shift');
  12589. dataOptions.shift();
  12590. }
  12591. }
  12592. // redraw
  12593. series.isDirty = true;
  12594. series.isDirtyData = true;
  12595. if (redraw) {
  12596. series.getAttribs(); // #1937
  12597. chart.redraw();
  12598. }
  12599. },
  12600. /**
  12601. * Remove a series and optionally redraw the chart
  12602. *
  12603. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  12604. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  12605. * configuration
  12606. */
  12607. remove: function (redraw, animation) {
  12608. var series = this,
  12609. chart = series.chart;
  12610. redraw = pick(redraw, true);
  12611. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  12612. (calling the remove function from the remove event) */
  12613. series.isRemoving = true;
  12614. // fire the event with a default handler of removing the point
  12615. fireEvent(series, 'remove', null, function () {
  12616. // destroy elements
  12617. series.destroy();
  12618. // redraw
  12619. chart.isDirtyLegend = chart.isDirtyBox = true;
  12620. chart.linkSeries();
  12621. if (redraw) {
  12622. chart.redraw(animation);
  12623. }
  12624. });
  12625. }
  12626. series.isRemoving = false;
  12627. },
  12628. /**
  12629. * Update the series with a new set of options
  12630. */
  12631. update: function (newOptions, redraw) {
  12632. var chart = this.chart,
  12633. // must use user options when changing type because this.options is merged
  12634. // in with type specific plotOptions
  12635. oldOptions = this.userOptions,
  12636. oldType = this.type,
  12637. proto = seriesTypes[oldType].prototype,
  12638. n;
  12639. // Do the merge, with some forced options
  12640. newOptions = merge(oldOptions, {
  12641. animation: false,
  12642. index: this.index,
  12643. pointStart: this.xData[0] // when updating after addPoint
  12644. }, { data: this.options.data }, newOptions);
  12645. // Destroy the series and reinsert methods from the type prototype
  12646. this.remove(false);
  12647. for (n in proto) { // Overwrite series-type specific methods (#2270)
  12648. if (proto.hasOwnProperty(n)) {
  12649. this[n] = UNDEFINED;
  12650. }
  12651. }
  12652. extend(this, seriesTypes[newOptions.type || oldType].prototype);
  12653. this.init(chart, newOptions);
  12654. if (pick(redraw, true)) {
  12655. chart.redraw(false);
  12656. }
  12657. }
  12658. });
  12659. // Extend the Axis.prototype for dynamic methods
  12660. extend(Axis.prototype, {
  12661. /**
  12662. * Update the axis with a new options structure
  12663. */
  12664. update: function (newOptions, redraw) {
  12665. var chart = this.chart;
  12666. newOptions = chart.options[this.coll][this.options.index] = merge(this.userOptions, newOptions);
  12667. this.destroy(true);
  12668. this._addedPlotLB = this.userMin = this.userMax = UNDEFINED; // #1611, #2306
  12669. this.init(chart, extend(newOptions, { events: UNDEFINED }));
  12670. chart.isDirtyBox = true;
  12671. if (pick(redraw, true)) {
  12672. chart.redraw();
  12673. }
  12674. },
  12675. /**
  12676. * Remove the axis from the chart
  12677. */
  12678. remove: function (redraw) {
  12679. var chart = this.chart,
  12680. key = this.coll, // xAxis or yAxis
  12681. axisSeries = this.series,
  12682. i = axisSeries.length;
  12683. // Remove associated series (#2687)
  12684. while (i--) {
  12685. if (axisSeries[i]) {
  12686. axisSeries[i].remove(false);
  12687. }
  12688. }
  12689. // Remove the axis
  12690. erase(chart.axes, this);
  12691. erase(chart[key], this);
  12692. chart.options[key].splice(this.options.index, 1);
  12693. each(chart[key], function (axis, i) { // Re-index, #1706
  12694. axis.options.index = i;
  12695. });
  12696. this.destroy();
  12697. chart.isDirtyBox = true;
  12698. if (pick(redraw, true)) {
  12699. chart.redraw();
  12700. }
  12701. },
  12702. /**
  12703. * Update the axis title by options
  12704. */
  12705. setTitle: function (newTitleOptions, redraw) {
  12706. this.update({ title: newTitleOptions }, redraw);
  12707. },
  12708. /**
  12709. * Set new axis categories and optionally redraw
  12710. * @param {Array} categories
  12711. * @param {Boolean} redraw
  12712. */
  12713. setCategories: function (categories, redraw) {
  12714. this.update({ categories: categories }, redraw);
  12715. }
  12716. });
  12717. /**
  12718. * LineSeries object
  12719. */
  12720. var LineSeries = extendClass(Series);
  12721. seriesTypes.line = LineSeries;
  12722. /**
  12723. * Set the default options for area
  12724. */
  12725. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  12726. threshold: 0
  12727. // trackByArea: false,
  12728. // lineColor: null, // overrides color, but lets fillColor be unaltered
  12729. // fillOpacity: 0.75,
  12730. // fillColor: null
  12731. });
  12732. /**
  12733. * AreaSeries object
  12734. */
  12735. var AreaSeries = extendClass(Series, {
  12736. type: 'area',
  12737. /**
  12738. * For stacks, don't split segments on null values. Instead, draw null values with
  12739. * no marker. Also insert dummy points for any X position that exists in other series
  12740. * in the stack.
  12741. */
  12742. getSegments: function () {
  12743. var segments = [],
  12744. segment = [],
  12745. keys = [],
  12746. xAxis = this.xAxis,
  12747. yAxis = this.yAxis,
  12748. stack = yAxis.stacks[this.stackKey],
  12749. pointMap = {},
  12750. plotX,
  12751. plotY,
  12752. points = this.points,
  12753. connectNulls = this.options.connectNulls,
  12754. val,
  12755. i,
  12756. x;
  12757. if (this.options.stacking && !this.cropped) { // cropped causes artefacts in Stock, and perf issue
  12758. // Create a map where we can quickly look up the points by their X value.
  12759. for (i = 0; i < points.length; i++) {
  12760. pointMap[points[i].x] = points[i];
  12761. }
  12762. // Sort the keys (#1651)
  12763. for (x in stack) {
  12764. if (stack[x].total !== null) { // nulled after switching between grouping and not (#1651, #2336)
  12765. keys.push(+x);
  12766. }
  12767. }
  12768. keys.sort(function (a, b) {
  12769. return a - b;
  12770. });
  12771. each(keys, function (x) {
  12772. if (connectNulls && (!pointMap[x] || pointMap[x].y === null)) { // #1836
  12773. return;
  12774. // The point exists, push it to the segment
  12775. } else if (pointMap[x]) {
  12776. segment.push(pointMap[x]);
  12777. // There is no point for this X value in this series, so we
  12778. // insert a dummy point in order for the areas to be drawn
  12779. // correctly.
  12780. } else {
  12781. plotX = xAxis.translate(x);
  12782. val = stack[x].percent ? (stack[x].total ? stack[x].cum * 100 / stack[x].total : 0) : stack[x].cum; // #1991
  12783. plotY = yAxis.toPixels(val, true);
  12784. segment.push({
  12785. y: null,
  12786. plotX: plotX,
  12787. clientX: plotX,
  12788. plotY: plotY,
  12789. yBottom: plotY,
  12790. onMouseOver: noop
  12791. });
  12792. }
  12793. });
  12794. if (segment.length) {
  12795. segments.push(segment);
  12796. }
  12797. } else {
  12798. Series.prototype.getSegments.call(this);
  12799. segments = this.segments;
  12800. }
  12801. this.segments = segments;
  12802. },
  12803. /**
  12804. * Extend the base Series getSegmentPath method by adding the path for the area.
  12805. * This path is pushed to the series.areaPath property.
  12806. */
  12807. getSegmentPath: function (segment) {
  12808. var segmentPath = Series.prototype.getSegmentPath.call(this, segment), // call base method
  12809. areaSegmentPath = [].concat(segmentPath), // work on a copy for the area path
  12810. i,
  12811. options = this.options,
  12812. segLength = segmentPath.length,
  12813. translatedThreshold = this.yAxis.getThreshold(options.threshold), // #2181
  12814. yBottom;
  12815. if (segLength === 3) { // for animation from 1 to two points
  12816. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  12817. }
  12818. if (options.stacking && !this.closedStacks) {
  12819. // Follow stack back. Todo: implement areaspline. A general solution could be to
  12820. // reverse the entire graphPath of the previous series, though may be hard with
  12821. // splines and with series with different extremes
  12822. for (i = segment.length - 1; i >= 0; i--) {
  12823. yBottom = pick(segment[i].yBottom, translatedThreshold);
  12824. // step line?
  12825. if (i < segment.length - 1 && options.step) {
  12826. areaSegmentPath.push(segment[i + 1].plotX, yBottom);
  12827. }
  12828. areaSegmentPath.push(segment[i].plotX, yBottom);
  12829. }
  12830. } else { // follow zero line back
  12831. this.closeSegment(areaSegmentPath, segment, translatedThreshold);
  12832. }
  12833. this.areaPath = this.areaPath.concat(areaSegmentPath);
  12834. return segmentPath;
  12835. },
  12836. /**
  12837. * Extendable method to close the segment path of an area. This is overridden in polar
  12838. * charts.
  12839. */
  12840. closeSegment: function (path, segment, translatedThreshold) {
  12841. path.push(
  12842. L,
  12843. segment[segment.length - 1].plotX,
  12844. translatedThreshold,
  12845. L,
  12846. segment[0].plotX,
  12847. translatedThreshold
  12848. );
  12849. },
  12850. /**
  12851. * Draw the graph and the underlying area. This method calls the Series base
  12852. * function and adds the area. The areaPath is calculated in the getSegmentPath
  12853. * method called from Series.prototype.drawGraph.
  12854. */
  12855. drawGraph: function () {
  12856. // Define or reset areaPath
  12857. this.areaPath = [];
  12858. // Call the base method
  12859. Series.prototype.drawGraph.apply(this);
  12860. // Define local variables
  12861. var series = this,
  12862. areaPath = this.areaPath,
  12863. options = this.options,
  12864. negativeColor = options.negativeColor,
  12865. negativeFillColor = options.negativeFillColor,
  12866. props = [['area', this.color, options.fillColor]]; // area name, main color, fill color
  12867. if (negativeColor || negativeFillColor) {
  12868. props.push(['areaNeg', negativeColor, negativeFillColor]);
  12869. }
  12870. each(props, function (prop) {
  12871. var areaKey = prop[0],
  12872. area = series[areaKey];
  12873. // Create or update the area
  12874. if (area) { // update
  12875. area.animate({ d: areaPath });
  12876. } else { // create
  12877. series[areaKey] = series.chart.renderer.path(areaPath)
  12878. .attr({
  12879. fill: pick(
  12880. prop[2],
  12881. Color(prop[1]).setOpacity(pick(options.fillOpacity, 0.75)).get()
  12882. ),
  12883. zIndex: 0 // #1069
  12884. }).add(series.group);
  12885. }
  12886. });
  12887. },
  12888. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  12889. });
  12890. seriesTypes.area = AreaSeries;
  12891. /**
  12892. * Set the default options for spline
  12893. */
  12894. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  12895. /**
  12896. * SplineSeries object
  12897. */
  12898. var SplineSeries = extendClass(Series, {
  12899. type: 'spline',
  12900. /**
  12901. * Get the spline segment from a given point's previous neighbour to the given point
  12902. */
  12903. getPointSpline: function (segment, point, i) {
  12904. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  12905. denom = smoothing + 1,
  12906. plotX = point.plotX,
  12907. plotY = point.plotY,
  12908. lastPoint = segment[i - 1],
  12909. nextPoint = segment[i + 1],
  12910. leftContX,
  12911. leftContY,
  12912. rightContX,
  12913. rightContY,
  12914. ret;
  12915. // find control points
  12916. if (lastPoint && nextPoint) {
  12917. var lastX = lastPoint.plotX,
  12918. lastY = lastPoint.plotY,
  12919. nextX = nextPoint.plotX,
  12920. nextY = nextPoint.plotY,
  12921. correction;
  12922. leftContX = (smoothing * plotX + lastX) / denom;
  12923. leftContY = (smoothing * plotY + lastY) / denom;
  12924. rightContX = (smoothing * plotX + nextX) / denom;
  12925. rightContY = (smoothing * plotY + nextY) / denom;
  12926. // have the two control points make a straight line through main point
  12927. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  12928. (rightContX - leftContX) + plotY - rightContY;
  12929. leftContY += correction;
  12930. rightContY += correction;
  12931. // to prevent false extremes, check that control points are between
  12932. // neighbouring points' y values
  12933. if (leftContY > lastY && leftContY > plotY) {
  12934. leftContY = mathMax(lastY, plotY);
  12935. rightContY = 2 * plotY - leftContY; // mirror of left control point
  12936. } else if (leftContY < lastY && leftContY < plotY) {
  12937. leftContY = mathMin(lastY, plotY);
  12938. rightContY = 2 * plotY - leftContY;
  12939. }
  12940. if (rightContY > nextY && rightContY > plotY) {
  12941. rightContY = mathMax(nextY, plotY);
  12942. leftContY = 2 * plotY - rightContY;
  12943. } else if (rightContY < nextY && rightContY < plotY) {
  12944. rightContY = mathMin(nextY, plotY);
  12945. leftContY = 2 * plotY - rightContY;
  12946. }
  12947. // record for drawing in next point
  12948. point.rightContX = rightContX;
  12949. point.rightContY = rightContY;
  12950. }
  12951. // Visualize control points for debugging
  12952. /*
  12953. if (leftContX) {
  12954. this.chart.renderer.circle(leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop, 2)
  12955. .attr({
  12956. stroke: 'red',
  12957. 'stroke-width': 1,
  12958. fill: 'none'
  12959. })
  12960. .add();
  12961. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft, leftContY + this.chart.plotTop,
  12962. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  12963. .attr({
  12964. stroke: 'red',
  12965. 'stroke-width': 1
  12966. })
  12967. .add();
  12968. this.chart.renderer.circle(rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop, 2)
  12969. .attr({
  12970. stroke: 'green',
  12971. 'stroke-width': 1,
  12972. fill: 'none'
  12973. })
  12974. .add();
  12975. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft, rightContY + this.chart.plotTop,
  12976. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  12977. .attr({
  12978. stroke: 'green',
  12979. 'stroke-width': 1
  12980. })
  12981. .add();
  12982. }
  12983. */
  12984. // moveTo or lineTo
  12985. if (!i) {
  12986. ret = [M, plotX, plotY];
  12987. } else { // curve from last point to this
  12988. ret = [
  12989. 'C',
  12990. lastPoint.rightContX || lastPoint.plotX,
  12991. lastPoint.rightContY || lastPoint.plotY,
  12992. leftContX || plotX,
  12993. leftContY || plotY,
  12994. plotX,
  12995. plotY
  12996. ];
  12997. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  12998. }
  12999. return ret;
  13000. }
  13001. });
  13002. seriesTypes.spline = SplineSeries;
  13003. /**
  13004. * Set the default options for areaspline
  13005. */
  13006. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  13007. /**
  13008. * AreaSplineSeries object
  13009. */
  13010. var areaProto = AreaSeries.prototype,
  13011. AreaSplineSeries = extendClass(SplineSeries, {
  13012. type: 'areaspline',
  13013. closedStacks: true, // instead of following the previous graph back, follow the threshold back
  13014. // Mix in methods from the area series
  13015. getSegmentPath: areaProto.getSegmentPath,
  13016. closeSegment: areaProto.closeSegment,
  13017. drawGraph: areaProto.drawGraph,
  13018. drawLegendSymbol: LegendSymbolMixin.drawRectangle
  13019. });
  13020. seriesTypes.areaspline = AreaSplineSeries;
  13021. /**
  13022. * Set the default options for column
  13023. */
  13024. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  13025. borderColor: '#FFFFFF',
  13026. borderWidth: 1,
  13027. borderRadius: 0,
  13028. //colorByPoint: undefined,
  13029. groupPadding: 0.2,
  13030. //grouping: true,
  13031. marker: null, // point options are specified in the base options
  13032. pointPadding: 0.1,
  13033. //pointWidth: null,
  13034. minPointLength: 0,
  13035. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  13036. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  13037. states: {
  13038. hover: {
  13039. brightness: 0.1,
  13040. shadow: false
  13041. },
  13042. select: {
  13043. color: '#C0C0C0',
  13044. borderColor: '#000000',
  13045. shadow: false
  13046. }
  13047. },
  13048. dataLabels: {
  13049. align: null, // auto
  13050. verticalAlign: null, // auto
  13051. y: null
  13052. },
  13053. stickyTracking: false,
  13054. threshold: 0
  13055. });
  13056. /**
  13057. * ColumnSeries object
  13058. */
  13059. var ColumnSeries = extendClass(Series, {
  13060. type: 'column',
  13061. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13062. stroke: 'borderColor',
  13063. 'stroke-width': 'borderWidth',
  13064. fill: 'color',
  13065. r: 'borderRadius'
  13066. },
  13067. cropShoulder: 0,
  13068. trackerGroups: ['group', 'dataLabelsGroup'],
  13069. negStacks: true, // use separate negative stacks, unlike area stacks where a negative
  13070. // point is substracted from previous (#1910)
  13071. /**
  13072. * Initialize the series
  13073. */
  13074. init: function () {
  13075. Series.prototype.init.apply(this, arguments);
  13076. var series = this,
  13077. chart = series.chart;
  13078. // if the series is added dynamically, force redraw of other
  13079. // series affected by a new column
  13080. if (chart.hasRendered) {
  13081. each(chart.series, function (otherSeries) {
  13082. if (otherSeries.type === series.type) {
  13083. otherSeries.isDirty = true;
  13084. }
  13085. });
  13086. }
  13087. },
  13088. /**
  13089. * Return the width and x offset of the columns adjusted for grouping, groupPadding, pointPadding,
  13090. * pointWidth etc.
  13091. */
  13092. getColumnMetrics: function () {
  13093. var series = this,
  13094. options = series.options,
  13095. xAxis = series.xAxis,
  13096. yAxis = series.yAxis,
  13097. reversedXAxis = xAxis.reversed,
  13098. stackKey,
  13099. stackGroups = {},
  13100. columnIndex,
  13101. columnCount = 0;
  13102. // Get the total number of column type series.
  13103. // This is called on every series. Consider moving this logic to a
  13104. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  13105. if (options.grouping === false) {
  13106. columnCount = 1;
  13107. } else {
  13108. each(series.chart.series, function (otherSeries) {
  13109. var otherOptions = otherSeries.options,
  13110. otherYAxis = otherSeries.yAxis;
  13111. if (otherSeries.type === series.type && otherSeries.visible &&
  13112. yAxis.len === otherYAxis.len && yAxis.pos === otherYAxis.pos) { // #642, #2086
  13113. if (otherOptions.stacking) {
  13114. stackKey = otherSeries.stackKey;
  13115. if (stackGroups[stackKey] === UNDEFINED) {
  13116. stackGroups[stackKey] = columnCount++;
  13117. }
  13118. columnIndex = stackGroups[stackKey];
  13119. } else if (otherOptions.grouping !== false) { // #1162
  13120. columnIndex = columnCount++;
  13121. }
  13122. otherSeries.columnIndex = columnIndex;
  13123. }
  13124. });
  13125. }
  13126. var categoryWidth = mathMin(
  13127. mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || xAxis.tickInterval || 1), // #2610
  13128. xAxis.len // #1535
  13129. ),
  13130. groupPadding = categoryWidth * options.groupPadding,
  13131. groupWidth = categoryWidth - 2 * groupPadding,
  13132. pointOffsetWidth = groupWidth / columnCount,
  13133. optionPointWidth = options.pointWidth,
  13134. pointPadding = defined(optionPointWidth) ? (pointOffsetWidth - optionPointWidth) / 2 :
  13135. pointOffsetWidth * options.pointPadding,
  13136. pointWidth = pick(optionPointWidth, pointOffsetWidth - 2 * pointPadding), // exact point width, used in polar charts
  13137. colIndex = (reversedXAxis ?
  13138. columnCount - (series.columnIndex || 0) : // #1251
  13139. series.columnIndex) || 0,
  13140. pointXOffset = pointPadding + (groupPadding + colIndex *
  13141. pointOffsetWidth - (categoryWidth / 2)) *
  13142. (reversedXAxis ? -1 : 1);
  13143. // Save it for reading in linked series (Error bars particularly)
  13144. return (series.columnMetrics = {
  13145. width: pointWidth,
  13146. offset: pointXOffset
  13147. });
  13148. },
  13149. /**
  13150. * Translate each point to the plot area coordinate system and find shape positions
  13151. */
  13152. translate: function () {
  13153. var series = this,
  13154. chart = series.chart,
  13155. options = series.options,
  13156. borderWidth = options.borderWidth,
  13157. yAxis = series.yAxis,
  13158. threshold = options.threshold,
  13159. translatedThreshold = series.translatedThreshold = yAxis.getThreshold(threshold),
  13160. minPointLength = pick(options.minPointLength, 5),
  13161. metrics = series.getColumnMetrics(),
  13162. pointWidth = metrics.width,
  13163. seriesBarW = series.barW = mathCeil(mathMax(pointWidth, 1 + 2 * borderWidth)), // rounded and postprocessed for border width
  13164. pointXOffset = series.pointXOffset = metrics.offset,
  13165. xCrisp = -(borderWidth % 2 ? 0.5 : 0),
  13166. yCrisp = borderWidth % 2 ? 0.5 : 1;
  13167. if (chart.renderer.isVML && chart.inverted) {
  13168. yCrisp += 1;
  13169. }
  13170. Series.prototype.translate.apply(series);
  13171. // record the new values
  13172. each(series.points, function (point) {
  13173. var yBottom = pick(point.yBottom, translatedThreshold),
  13174. plotY = mathMin(mathMax(-999 - yBottom, point.plotY), yAxis.len + 999 + yBottom), // Don't draw too far outside plot area (#1303, #2241)
  13175. barX = point.plotX + pointXOffset,
  13176. barW = seriesBarW,
  13177. barY = mathMin(plotY, yBottom),
  13178. right,
  13179. bottom,
  13180. fromTop,
  13181. fromLeft,
  13182. barH = mathMax(plotY, yBottom) - barY;
  13183. // Handle options.minPointLength
  13184. if (mathAbs(barH) < minPointLength) {
  13185. if (minPointLength) {
  13186. barH = minPointLength;
  13187. barY =
  13188. mathRound(mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  13189. yBottom - minPointLength : // keep position
  13190. translatedThreshold - (yAxis.translate(point.y, 0, 1, 0, 1) <= translatedThreshold ? minPointLength : 0)); // use exact yAxis.translation (#1485)
  13191. }
  13192. }
  13193. // Cache for access in polar
  13194. point.barX = barX;
  13195. point.pointWidth = pointWidth;
  13196. // Round off to obtain crisp edges
  13197. fromLeft = mathAbs(barX) < 0.5;
  13198. right = mathRound(barX + barW) + xCrisp;
  13199. barX = mathRound(barX) + xCrisp;
  13200. barW = right - barX;
  13201. fromTop = mathAbs(barY) < 0.5;
  13202. bottom = mathRound(barY + barH) + yCrisp;
  13203. barY = mathRound(barY) + yCrisp;
  13204. barH = bottom - barY;
  13205. // Top and left edges are exceptions
  13206. if (fromLeft) {
  13207. barX += 1;
  13208. barW -= 1;
  13209. }
  13210. if (fromTop) {
  13211. barY -= 1;
  13212. barH += 1;
  13213. }
  13214. // Register shape type and arguments to be used in drawPoints
  13215. point.shapeType = 'rect';
  13216. point.shapeArgs = {
  13217. x: barX,
  13218. y: barY,
  13219. width: barW,
  13220. height: barH
  13221. };
  13222. });
  13223. },
  13224. getSymbol: noop,
  13225. /**
  13226. * Use a solid rectangle like the area series types
  13227. */
  13228. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  13229. /**
  13230. * Columns have no graph
  13231. */
  13232. drawGraph: noop,
  13233. /**
  13234. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  13235. * apply for columns and bars. This method is inherited by scatter series.
  13236. *
  13237. */
  13238. drawPoints: function () {
  13239. var series = this,
  13240. chart = this.chart,
  13241. options = series.options,
  13242. renderer = chart.renderer,
  13243. animationLimit = options.animationLimit || 250,
  13244. shapeArgs;
  13245. // draw the columns
  13246. each(series.points, function (point) {
  13247. var plotY = point.plotY,
  13248. graphic = point.graphic;
  13249. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  13250. shapeArgs = point.shapeArgs;
  13251. if (graphic) { // update
  13252. stop(graphic);
  13253. graphic[series.points.length < animationLimit ? 'animate' : 'attr'](merge(shapeArgs));
  13254. } else {
  13255. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13256. .attr(point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE])
  13257. .add(series.group)
  13258. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  13259. }
  13260. } else if (graphic) {
  13261. point.graphic = graphic.destroy(); // #1269
  13262. }
  13263. });
  13264. },
  13265. /**
  13266. * Animate the column heights one by one from zero
  13267. * @param {Boolean} init Whether to initialize the animation or run it
  13268. */
  13269. animate: function (init) {
  13270. var series = this,
  13271. yAxis = this.yAxis,
  13272. options = series.options,
  13273. inverted = this.chart.inverted,
  13274. attr = {},
  13275. translatedThreshold;
  13276. if (hasSVG) { // VML is too slow anyway
  13277. if (init) {
  13278. attr.scaleY = 0.001;
  13279. translatedThreshold = mathMin(yAxis.pos + yAxis.len, mathMax(yAxis.pos, yAxis.toPixels(options.threshold)));
  13280. if (inverted) {
  13281. attr.translateX = translatedThreshold - yAxis.len;
  13282. } else {
  13283. attr.translateY = translatedThreshold;
  13284. }
  13285. series.group.attr(attr);
  13286. } else { // run the animation
  13287. attr.scaleY = 1;
  13288. attr[inverted ? 'translateX' : 'translateY'] = yAxis.pos;
  13289. series.group.animate(attr, series.options.animation);
  13290. // delete this function to allow it only once
  13291. series.animate = null;
  13292. }
  13293. }
  13294. },
  13295. /**
  13296. * Remove this series from the chart
  13297. */
  13298. remove: function () {
  13299. var series = this,
  13300. chart = series.chart;
  13301. // column and bar series affects other series of the same type
  13302. // as they are either stacked or grouped
  13303. if (chart.hasRendered) {
  13304. each(chart.series, function (otherSeries) {
  13305. if (otherSeries.type === series.type) {
  13306. otherSeries.isDirty = true;
  13307. }
  13308. });
  13309. }
  13310. Series.prototype.remove.apply(series, arguments);
  13311. }
  13312. });
  13313. seriesTypes.column = ColumnSeries;
  13314. /**
  13315. * Set the default options for bar
  13316. */
  13317. defaultPlotOptions.bar = merge(defaultPlotOptions.column);
  13318. /**
  13319. * The Bar series class
  13320. */
  13321. var BarSeries = extendClass(ColumnSeries, {
  13322. type: 'bar',
  13323. inverted: true
  13324. });
  13325. seriesTypes.bar = BarSeries;
  13326. /**
  13327. * Set the default options for scatter
  13328. */
  13329. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  13330. lineWidth: 0,
  13331. tooltip: {
  13332. headerFormat: '<span style="font-size: 10px; color:{series.color}">{series.name}</span><br/>',
  13333. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>',
  13334. followPointer: true
  13335. },
  13336. stickyTracking: false
  13337. });
  13338. /**
  13339. * The scatter series class
  13340. */
  13341. var ScatterSeries = extendClass(Series, {
  13342. type: 'scatter',
  13343. sorted: false,
  13344. requireSorting: false,
  13345. noSharedTooltip: true,
  13346. trackerGroups: ['markerGroup'],
  13347. takeOrdinalPosition: false, // #2342
  13348. singularTooltips: true,
  13349. drawGraph: function () {
  13350. if (this.options.lineWidth) {
  13351. Series.prototype.drawGraph.call(this);
  13352. }
  13353. }
  13354. });
  13355. seriesTypes.scatter = ScatterSeries;
  13356. /**
  13357. * Set the default options for pie
  13358. */
  13359. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  13360. borderColor: '#FFFFFF',
  13361. borderWidth: 1,
  13362. center: [null, null],
  13363. clip: false,
  13364. colorByPoint: true, // always true for pies
  13365. dataLabels: {
  13366. // align: null,
  13367. // connectorWidth: 1,
  13368. // connectorColor: point.color,
  13369. // connectorPadding: 5,
  13370. distance: 30,
  13371. enabled: true,
  13372. formatter: function () {
  13373. return this.point.name;
  13374. }
  13375. // softConnector: true,
  13376. //y: 0
  13377. },
  13378. ignoreHiddenPoint: true,
  13379. //innerSize: 0,
  13380. legendType: 'point',
  13381. marker: null, // point options are specified in the base options
  13382. size: null,
  13383. showInLegend: false,
  13384. slicedOffset: 10,
  13385. states: {
  13386. hover: {
  13387. brightness: 0.1,
  13388. shadow: false
  13389. }
  13390. },
  13391. stickyTracking: false,
  13392. tooltip: {
  13393. followPointer: true
  13394. }
  13395. });
  13396. /**
  13397. * Extended point object for pies
  13398. */
  13399. var PiePoint = extendClass(Point, {
  13400. /**
  13401. * Initiate the pie slice
  13402. */
  13403. init: function () {
  13404. Point.prototype.init.apply(this, arguments);
  13405. var point = this,
  13406. toggleSlice;
  13407. // Disallow negative values (#1530)
  13408. if (point.y < 0) {
  13409. point.y = null;
  13410. }
  13411. //visible: options.visible !== false,
  13412. extend(point, {
  13413. visible: point.visible !== false,
  13414. name: pick(point.name, 'Slice')
  13415. });
  13416. // add event listener for select
  13417. toggleSlice = function (e) {
  13418. point.slice(e.type === 'select');
  13419. };
  13420. addEvent(point, 'select', toggleSlice);
  13421. addEvent(point, 'unselect', toggleSlice);
  13422. return point;
  13423. },
  13424. /**
  13425. * Toggle the visibility of the pie slice
  13426. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  13427. * visibility is toggled
  13428. */
  13429. setVisible: function (vis) {
  13430. var point = this,
  13431. series = point.series,
  13432. chart = series.chart;
  13433. // if called without an argument, toggle visibility
  13434. point.visible = point.options.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  13435. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13436. // Show and hide associated elements
  13437. each(['graphic', 'dataLabel', 'connector', 'shadowGroup'], function (key) {
  13438. if (point[key]) {
  13439. point[key][vis ? 'show' : 'hide'](true);
  13440. }
  13441. });
  13442. if (point.legendItem) {
  13443. chart.legend.colorizeItem(point, vis);
  13444. }
  13445. // Handle ignore hidden slices
  13446. if (!series.isDirty && series.options.ignoreHiddenPoint) {
  13447. series.isDirty = true;
  13448. chart.redraw();
  13449. }
  13450. },
  13451. /**
  13452. * Set or toggle whether the slice is cut out from the pie
  13453. * @param {Boolean} sliced When undefined, the slice state is toggled
  13454. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  13455. */
  13456. slice: function (sliced, redraw, animation) {
  13457. var point = this,
  13458. series = point.series,
  13459. chart = series.chart,
  13460. translation;
  13461. setAnimation(animation, chart);
  13462. // redraw is true by default
  13463. redraw = pick(redraw, true);
  13464. // if called without an argument, toggle
  13465. point.sliced = point.options.sliced = sliced = defined(sliced) ? sliced : !point.sliced;
  13466. series.options.data[inArray(point, series.data)] = point.options; // update userOptions.data
  13467. translation = sliced ? point.slicedTranslation : {
  13468. translateX: 0,
  13469. translateY: 0
  13470. };
  13471. point.graphic.animate(translation);
  13472. if (point.shadowGroup) {
  13473. point.shadowGroup.animate(translation);
  13474. }
  13475. }
  13476. });
  13477. /**
  13478. * The Pie series class
  13479. */
  13480. var PieSeries = {
  13481. type: 'pie',
  13482. isCartesian: false,
  13483. pointClass: PiePoint,
  13484. requireSorting: false,
  13485. noSharedTooltip: true,
  13486. trackerGroups: ['group', 'dataLabelsGroup'],
  13487. axisTypes: [],
  13488. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13489. stroke: 'borderColor',
  13490. 'stroke-width': 'borderWidth',
  13491. fill: 'color'
  13492. },
  13493. singularTooltips: true,
  13494. /**
  13495. * Pies have one color each point
  13496. */
  13497. getColor: noop,
  13498. /**
  13499. * Animate the pies in
  13500. */
  13501. animate: function (init) {
  13502. var series = this,
  13503. points = series.points,
  13504. startAngleRad = series.startAngleRad;
  13505. if (!init) {
  13506. each(points, function (point) {
  13507. var graphic = point.graphic,
  13508. args = point.shapeArgs;
  13509. if (graphic) {
  13510. // start values
  13511. graphic.attr({
  13512. r: series.center[3] / 2, // animate from inner radius (#779)
  13513. start: startAngleRad,
  13514. end: startAngleRad
  13515. });
  13516. // animate
  13517. graphic.animate({
  13518. r: args.r,
  13519. start: args.start,
  13520. end: args.end
  13521. }, series.options.animation);
  13522. }
  13523. });
  13524. // delete this function to allow it only once
  13525. series.animate = null;
  13526. }
  13527. },
  13528. /**
  13529. * Extend the basic setData method by running processData and generatePoints immediately,
  13530. * in order to access the points from the legend.
  13531. */
  13532. setData: function (data, redraw, animation, updatePoints) {
  13533. Series.prototype.setData.call(this, data, false, animation, updatePoints);
  13534. this.processData();
  13535. this.generatePoints();
  13536. if (pick(redraw, true)) {
  13537. this.chart.redraw(animation);
  13538. }
  13539. },
  13540. /**
  13541. * Extend the generatePoints method by adding total and percentage properties to each point
  13542. */
  13543. generatePoints: function () {
  13544. var i,
  13545. total = 0,
  13546. points,
  13547. len,
  13548. point,
  13549. ignoreHiddenPoint = this.options.ignoreHiddenPoint;
  13550. Series.prototype.generatePoints.call(this);
  13551. // Populate local vars
  13552. points = this.points;
  13553. len = points.length;
  13554. // Get the total sum
  13555. for (i = 0; i < len; i++) {
  13556. point = points[i];
  13557. total += (ignoreHiddenPoint && !point.visible) ? 0 : point.y;
  13558. }
  13559. this.total = total;
  13560. // Set each point's properties
  13561. for (i = 0; i < len; i++) {
  13562. point = points[i];
  13563. point.percentage = total > 0 ? (point.y / total) * 100 : 0;
  13564. point.total = total;
  13565. }
  13566. },
  13567. /**
  13568. * Do translation for pie slices
  13569. */
  13570. translate: function (positions) {
  13571. this.generatePoints();
  13572. var series = this,
  13573. cumulative = 0,
  13574. precision = 1000, // issue #172
  13575. options = series.options,
  13576. slicedOffset = options.slicedOffset,
  13577. connectorOffset = slicedOffset + options.borderWidth,
  13578. start,
  13579. end,
  13580. angle,
  13581. startAngle = options.startAngle || 0,
  13582. startAngleRad = series.startAngleRad = mathPI / 180 * (startAngle - 90),
  13583. endAngleRad = series.endAngleRad = mathPI / 180 * ((pick(options.endAngle, startAngle + 360)) - 90),
  13584. circ = endAngleRad - startAngleRad, //2 * mathPI,
  13585. points = series.points,
  13586. radiusX, // the x component of the radius vector for a given point
  13587. radiusY,
  13588. labelDistance = options.dataLabels.distance,
  13589. ignoreHiddenPoint = options.ignoreHiddenPoint,
  13590. i,
  13591. len = points.length,
  13592. point;
  13593. // Get positions - either an integer or a percentage string must be given.
  13594. // If positions are passed as a parameter, we're in a recursive loop for adjusting
  13595. // space for data labels.
  13596. if (!positions) {
  13597. series.center = positions = series.getCenter();
  13598. }
  13599. // utility for getting the x value from a given y, used for anticollision logic in data labels
  13600. series.getX = function (y, left) {
  13601. angle = math.asin(mathMin((y - positions[1]) / (positions[2] / 2 + labelDistance), 1));
  13602. return positions[0] +
  13603. (left ? -1 : 1) *
  13604. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  13605. };
  13606. // Calculate the geometry for each point
  13607. for (i = 0; i < len; i++) {
  13608. point = points[i];
  13609. // set start and end angle
  13610. start = startAngleRad + (cumulative * circ);
  13611. if (!ignoreHiddenPoint || point.visible) {
  13612. cumulative += point.percentage / 100;
  13613. }
  13614. end = startAngleRad + (cumulative * circ);
  13615. // set the shape
  13616. point.shapeType = 'arc';
  13617. point.shapeArgs = {
  13618. x: positions[0],
  13619. y: positions[1],
  13620. r: positions[2] / 2,
  13621. innerR: positions[3] / 2,
  13622. start: mathRound(start * precision) / precision,
  13623. end: mathRound(end * precision) / precision
  13624. };
  13625. // The angle must stay within -90 and 270 (#2645)
  13626. angle = (end + start) / 2;
  13627. if (angle > 1.5 * mathPI) {
  13628. angle -= 2 * mathPI;
  13629. } else if (angle < -mathPI / 2) {
  13630. angle += 2 * mathPI;
  13631. }
  13632. // Center for the sliced out slice
  13633. point.slicedTranslation = {
  13634. translateX: mathRound(mathCos(angle) * slicedOffset),
  13635. translateY: mathRound(mathSin(angle) * slicedOffset)
  13636. };
  13637. // set the anchor point for tooltips
  13638. radiusX = mathCos(angle) * positions[2] / 2;
  13639. radiusY = mathSin(angle) * positions[2] / 2;
  13640. point.tooltipPos = [
  13641. positions[0] + radiusX * 0.7,
  13642. positions[1] + radiusY * 0.7
  13643. ];
  13644. point.half = angle < -mathPI / 2 || angle > mathPI / 2 ? 1 : 0;
  13645. point.angle = angle;
  13646. // set the anchor point for data labels
  13647. connectorOffset = mathMin(connectorOffset, labelDistance / 2); // #1678
  13648. point.labelPos = [
  13649. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  13650. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  13651. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  13652. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  13653. positions[0] + radiusX, // landing point for connector
  13654. positions[1] + radiusY, // a/a
  13655. labelDistance < 0 ? // alignment
  13656. 'center' :
  13657. point.half ? 'right' : 'left', // alignment
  13658. angle // center angle
  13659. ];
  13660. }
  13661. },
  13662. drawGraph: null,
  13663. /**
  13664. * Draw the data points
  13665. */
  13666. drawPoints: function () {
  13667. var series = this,
  13668. chart = series.chart,
  13669. renderer = chart.renderer,
  13670. groupTranslation,
  13671. //center,
  13672. graphic,
  13673. //group,
  13674. shadow = series.options.shadow,
  13675. shadowGroup,
  13676. shapeArgs;
  13677. if (shadow && !series.shadowGroup) {
  13678. series.shadowGroup = renderer.g('shadow')
  13679. .add(series.group);
  13680. }
  13681. // draw the slices
  13682. each(series.points, function (point) {
  13683. graphic = point.graphic;
  13684. shapeArgs = point.shapeArgs;
  13685. shadowGroup = point.shadowGroup;
  13686. // put the shadow behind all points
  13687. if (shadow && !shadowGroup) {
  13688. shadowGroup = point.shadowGroup = renderer.g('shadow')
  13689. .add(series.shadowGroup);
  13690. }
  13691. // if the point is sliced, use special translation, else use plot area traslation
  13692. groupTranslation = point.sliced ? point.slicedTranslation : {
  13693. translateX: 0,
  13694. translateY: 0
  13695. };
  13696. //group.translate(groupTranslation[0], groupTranslation[1]);
  13697. if (shadowGroup) {
  13698. shadowGroup.attr(groupTranslation);
  13699. }
  13700. // draw the slice
  13701. if (graphic) {
  13702. graphic.animate(extend(shapeArgs, groupTranslation));
  13703. } else {
  13704. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13705. .setRadialReference(series.center)
  13706. .attr(
  13707. point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE]
  13708. )
  13709. .attr({
  13710. 'stroke-linejoin': 'round'
  13711. //zIndex: 1 // #2722 (reversed)
  13712. })
  13713. .attr(groupTranslation)
  13714. .add(series.group)
  13715. .shadow(shadow, shadowGroup);
  13716. }
  13717. // detect point specific visibility (#2430)
  13718. if (point.visible !== undefined) {
  13719. point.setVisible(point.visible);
  13720. }
  13721. });
  13722. },
  13723. /**
  13724. * Utility for sorting data labels
  13725. */
  13726. sortByAngle: function (points, sign) {
  13727. points.sort(function (a, b) {
  13728. return a.angle !== undefined && (b.angle - a.angle) * sign;
  13729. });
  13730. },
  13731. /**
  13732. * Use a simple symbol from LegendSymbolMixin
  13733. */
  13734. drawLegendSymbol: LegendSymbolMixin.drawRectangle,
  13735. /**
  13736. * Use the getCenter method from drawLegendSymbol
  13737. */
  13738. getCenter: CenteredSeriesMixin.getCenter,
  13739. /**
  13740. * Pies don't have point marker symbols
  13741. */
  13742. getSymbol: noop
  13743. };
  13744. PieSeries = extendClass(Series, PieSeries);
  13745. seriesTypes.pie = PieSeries;
  13746. /**
  13747. * Draw the data labels
  13748. */
  13749. Series.prototype.drawDataLabels = function () {
  13750. var series = this,
  13751. seriesOptions = series.options,
  13752. cursor = seriesOptions.cursor,
  13753. options = seriesOptions.dataLabels,
  13754. points = series.points,
  13755. pointOptions,
  13756. generalOptions,
  13757. str,
  13758. dataLabelsGroup;
  13759. if (options.enabled || series._hasPointLabels) {
  13760. // Process default alignment of data labels for columns
  13761. if (series.dlProcessOptions) {
  13762. series.dlProcessOptions(options);
  13763. }
  13764. // Create a separate group for the data labels to avoid rotation
  13765. dataLabelsGroup = series.plotGroup(
  13766. 'dataLabelsGroup',
  13767. 'data-labels',
  13768. series.visible ? VISIBLE : HIDDEN,
  13769. options.zIndex || 6
  13770. );
  13771. // Make the labels for each point
  13772. generalOptions = options;
  13773. each(points, function (point) {
  13774. var enabled,
  13775. dataLabel = point.dataLabel,
  13776. labelConfig,
  13777. attr,
  13778. name,
  13779. rotation,
  13780. connector = point.connector,
  13781. isNew = true;
  13782. // Determine if each data label is enabled
  13783. pointOptions = point.options && point.options.dataLabels;
  13784. enabled = pick(pointOptions && pointOptions.enabled, generalOptions.enabled); // #2282
  13785. // If the point is outside the plot area, destroy it. #678, #820
  13786. if (dataLabel && !enabled) {
  13787. point.dataLabel = dataLabel.destroy();
  13788. // Individual labels are disabled if the are explicitly disabled
  13789. // in the point options, or if they fall outside the plot area.
  13790. } else if (enabled) {
  13791. // Create individual options structure that can be extended without
  13792. // affecting others
  13793. options = merge(generalOptions, pointOptions);
  13794. rotation = options.rotation;
  13795. // Get the string
  13796. labelConfig = point.getLabelConfig();
  13797. str = options.format ?
  13798. format(options.format, labelConfig) :
  13799. options.formatter.call(labelConfig, options);
  13800. // Determine the color
  13801. options.style.color = pick(options.color, options.style.color, series.color, 'black');
  13802. // update existing label
  13803. if (dataLabel) {
  13804. if (defined(str)) {
  13805. dataLabel
  13806. .attr({
  13807. text: str
  13808. });
  13809. isNew = false;
  13810. } else { // #1437 - the label is shown conditionally
  13811. point.dataLabel = dataLabel = dataLabel.destroy();
  13812. if (connector) {
  13813. point.connector = connector.destroy();
  13814. }
  13815. }
  13816. // create new label
  13817. } else if (defined(str)) {
  13818. attr = {
  13819. //align: align,
  13820. fill: options.backgroundColor,
  13821. stroke: options.borderColor,
  13822. 'stroke-width': options.borderWidth,
  13823. r: options.borderRadius || 0,
  13824. rotation: rotation,
  13825. padding: options.padding,
  13826. zIndex: 1
  13827. };
  13828. // Remove unused attributes (#947)
  13829. for (name in attr) {
  13830. if (attr[name] === UNDEFINED) {
  13831. delete attr[name];
  13832. }
  13833. }
  13834. dataLabel = point.dataLabel = series.chart.renderer[rotation ? 'text' : 'label']( // labels don't support rotation
  13835. str,
  13836. 0,
  13837. -999,
  13838. null,
  13839. null,
  13840. null,
  13841. options.useHTML
  13842. )
  13843. .attr(attr)
  13844. .css(extend(options.style, cursor && { cursor: cursor }))
  13845. .add(dataLabelsGroup)
  13846. .shadow(options.shadow);
  13847. }
  13848. if (dataLabel) {
  13849. // Now the data label is created and placed at 0,0, so we need to align it
  13850. series.alignDataLabel(point, dataLabel, options, null, isNew);
  13851. }
  13852. }
  13853. });
  13854. }
  13855. };
  13856. /**
  13857. * Align each individual data label
  13858. */
  13859. Series.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  13860. var chart = this.chart,
  13861. inverted = chart.inverted,
  13862. plotX = pick(point.plotX, -999),
  13863. plotY = pick(point.plotY, -999),
  13864. bBox = dataLabel.getBBox(),
  13865. // Math.round for rounding errors (#2683), alignTo to allow column labels (#2700)
  13866. visible = this.visible && (point.series.forceDL || chart.isInsidePlot(plotX, mathRound(plotY), inverted) ||
  13867. (alignTo && chart.isInsidePlot(plotX, inverted ? alignTo.x + 1 : alignTo.y + alignTo.height - 1, inverted))),
  13868. alignAttr; // the final position;
  13869. if (visible) {
  13870. // The alignment box is a singular point
  13871. alignTo = extend({
  13872. x: inverted ? chart.plotWidth - plotY : plotX,
  13873. y: mathRound(inverted ? chart.plotHeight - plotX : plotY),
  13874. width: 0,
  13875. height: 0
  13876. }, alignTo);
  13877. // Add the text size for alignment calculation
  13878. extend(options, {
  13879. width: bBox.width,
  13880. height: bBox.height
  13881. });
  13882. // Allow a hook for changing alignment in the last moment, then do the alignment
  13883. if (options.rotation) { // Fancy box alignment isn't supported for rotated text
  13884. alignAttr = {
  13885. align: options.align,
  13886. x: alignTo.x + options.x + alignTo.width / 2,
  13887. y: alignTo.y + options.y + alignTo.height / 2
  13888. };
  13889. dataLabel[isNew ? 'attr' : 'animate'](alignAttr);
  13890. } else {
  13891. dataLabel.align(options, null, alignTo);
  13892. alignAttr = dataLabel.alignAttr;
  13893. // Handle justify or crop
  13894. if (pick(options.overflow, 'justify') === 'justify') {
  13895. this.justifyDataLabel(dataLabel, options, alignAttr, bBox, alignTo, isNew);
  13896. } else if (pick(options.crop, true)) {
  13897. // Now check that the data label is within the plot area
  13898. visible = chart.isInsidePlot(alignAttr.x, alignAttr.y) && chart.isInsidePlot(alignAttr.x + bBox.width, alignAttr.y + bBox.height);
  13899. }
  13900. }
  13901. }
  13902. // Show or hide based on the final aligned position
  13903. if (!visible) {
  13904. dataLabel.attr({ y: -999 });
  13905. dataLabel.placed = false; // don't animate back in
  13906. }
  13907. };
  13908. /**
  13909. * If data labels fall partly outside the plot area, align them back in, in a way that
  13910. * doesn't hide the point.
  13911. */
  13912. Series.prototype.justifyDataLabel = function (dataLabel, options, alignAttr, bBox, alignTo, isNew) {
  13913. var chart = this.chart,
  13914. align = options.align,
  13915. verticalAlign = options.verticalAlign,
  13916. off,
  13917. justified;
  13918. // Off left
  13919. off = alignAttr.x;
  13920. if (off < 0) {
  13921. if (align === 'right') {
  13922. options.align = 'left';
  13923. } else {
  13924. options.x = -off;
  13925. }
  13926. justified = true;
  13927. }
  13928. // Off right
  13929. off = alignAttr.x + bBox.width;
  13930. if (off > chart.plotWidth) {
  13931. if (align === 'left') {
  13932. options.align = 'right';
  13933. } else {
  13934. options.x = chart.plotWidth - off;
  13935. }
  13936. justified = true;
  13937. }
  13938. // Off top
  13939. off = alignAttr.y;
  13940. if (off < 0) {
  13941. if (verticalAlign === 'bottom') {
  13942. options.verticalAlign = 'top';
  13943. } else {
  13944. options.y = -off;
  13945. }
  13946. justified = true;
  13947. }
  13948. // Off bottom
  13949. off = alignAttr.y + bBox.height;
  13950. if (off > chart.plotHeight) {
  13951. if (verticalAlign === 'top') {
  13952. options.verticalAlign = 'bottom';
  13953. } else {
  13954. options.y = chart.plotHeight - off;
  13955. }
  13956. justified = true;
  13957. }
  13958. if (justified) {
  13959. dataLabel.placed = !isNew;
  13960. dataLabel.align(options, null, alignTo);
  13961. }
  13962. };
  13963. /**
  13964. * Override the base drawDataLabels method by pie specific functionality
  13965. */
  13966. if (seriesTypes.pie) {
  13967. seriesTypes.pie.prototype.drawDataLabels = function () {
  13968. var series = this,
  13969. data = series.data,
  13970. point,
  13971. chart = series.chart,
  13972. options = series.options.dataLabels,
  13973. connectorPadding = pick(options.connectorPadding, 10),
  13974. connectorWidth = pick(options.connectorWidth, 1),
  13975. plotWidth = chart.plotWidth,
  13976. plotHeight = chart.plotHeight,
  13977. connector,
  13978. connectorPath,
  13979. softConnector = pick(options.softConnector, true),
  13980. distanceOption = options.distance,
  13981. seriesCenter = series.center,
  13982. radius = seriesCenter[2] / 2,
  13983. centerY = seriesCenter[1],
  13984. outside = distanceOption > 0,
  13985. dataLabel,
  13986. dataLabelWidth,
  13987. labelPos,
  13988. labelHeight,
  13989. halves = [// divide the points into right and left halves for anti collision
  13990. [], // right
  13991. [] // left
  13992. ],
  13993. x,
  13994. y,
  13995. visibility,
  13996. rankArr,
  13997. i,
  13998. j,
  13999. overflow = [0, 0, 0, 0], // top, right, bottom, left
  14000. sort = function (a, b) {
  14001. return b.y - a.y;
  14002. };
  14003. // get out if not enabled
  14004. if (!series.visible || (!options.enabled && !series._hasPointLabels)) {
  14005. return;
  14006. }
  14007. // run parent method
  14008. Series.prototype.drawDataLabels.apply(series);
  14009. // arrange points for detection collision
  14010. each(data, function (point) {
  14011. if (point.dataLabel && point.visible) { // #407, #2510
  14012. halves[point.half].push(point);
  14013. }
  14014. });
  14015. // assume equal label heights
  14016. i = 0;
  14017. while (!labelHeight && data[i]) { // #1569
  14018. labelHeight = data[i] && data[i].dataLabel && (data[i].dataLabel.getBBox().height || 21); // 21 is for #968
  14019. i++;
  14020. }
  14021. /* Loop over the points in each half, starting from the top and bottom
  14022. * of the pie to detect overlapping labels.
  14023. */
  14024. i = 2;
  14025. while (i--) {
  14026. var slots = [],
  14027. slotsLength,
  14028. usedSlots = [],
  14029. points = halves[i],
  14030. pos,
  14031. length = points.length,
  14032. slotIndex;
  14033. // Sort by angle
  14034. series.sortByAngle(points, i - 0.5);
  14035. // Only do anti-collision when we are outside the pie and have connectors (#856)
  14036. if (distanceOption > 0) {
  14037. // build the slots
  14038. for (pos = centerY - radius - distanceOption; pos <= centerY + radius + distanceOption; pos += labelHeight) {
  14039. slots.push(pos);
  14040. // visualize the slot
  14041. /*
  14042. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  14043. slotY = pos + chart.plotTop;
  14044. if (!isNaN(slotX)) {
  14045. chart.renderer.rect(slotX, slotY - 7, 100, labelHeight, 1)
  14046. .attr({
  14047. 'stroke-width': 1,
  14048. stroke: 'silver'
  14049. })
  14050. .add();
  14051. chart.renderer.text('Slot '+ (slots.length - 1), slotX, slotY + 4)
  14052. .attr({
  14053. fill: 'silver'
  14054. }).add();
  14055. }
  14056. */
  14057. }
  14058. slotsLength = slots.length;
  14059. // if there are more values than available slots, remove lowest values
  14060. if (length > slotsLength) {
  14061. // create an array for sorting and ranking the points within each quarter
  14062. rankArr = [].concat(points);
  14063. rankArr.sort(sort);
  14064. j = length;
  14065. while (j--) {
  14066. rankArr[j].rank = j;
  14067. }
  14068. j = length;
  14069. while (j--) {
  14070. if (points[j].rank >= slotsLength) {
  14071. points.splice(j, 1);
  14072. }
  14073. }
  14074. length = points.length;
  14075. }
  14076. // The label goes to the nearest open slot, but not closer to the edge than
  14077. // the label's index.
  14078. for (j = 0; j < length; j++) {
  14079. point = points[j];
  14080. labelPos = point.labelPos;
  14081. var closest = 9999,
  14082. distance,
  14083. slotI;
  14084. // find the closest slot index
  14085. for (slotI = 0; slotI < slotsLength; slotI++) {
  14086. distance = mathAbs(slots[slotI] - labelPos[1]);
  14087. if (distance < closest) {
  14088. closest = distance;
  14089. slotIndex = slotI;
  14090. }
  14091. }
  14092. // if that slot index is closer to the edges of the slots, move it
  14093. // to the closest appropriate slot
  14094. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  14095. slotIndex = j;
  14096. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  14097. slotIndex = slotsLength - length + j;
  14098. while (slots[slotIndex] === null) { // make sure it is not taken
  14099. slotIndex++;
  14100. }
  14101. } else {
  14102. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  14103. // slot above these, because it is the closest one
  14104. while (slots[slotIndex] === null) { // make sure it is not taken
  14105. slotIndex++;
  14106. }
  14107. }
  14108. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  14109. slots[slotIndex] = null; // mark as taken
  14110. }
  14111. // sort them in order to fill in from the top
  14112. usedSlots.sort(sort);
  14113. }
  14114. // now the used slots are sorted, fill them up sequentially
  14115. for (j = 0; j < length; j++) {
  14116. var slot, naturalY;
  14117. point = points[j];
  14118. labelPos = point.labelPos;
  14119. dataLabel = point.dataLabel;
  14120. visibility = point.visible === false ? HIDDEN : VISIBLE;
  14121. naturalY = labelPos[1];
  14122. if (distanceOption > 0) {
  14123. slot = usedSlots.pop();
  14124. slotIndex = slot.i;
  14125. // if the slot next to currrent slot is free, the y value is allowed
  14126. // to fall back to the natural position
  14127. y = slot.y;
  14128. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  14129. (naturalY < y && slots[slotIndex - 1] !== null)) {
  14130. y = naturalY;
  14131. }
  14132. } else {
  14133. y = naturalY;
  14134. }
  14135. // get the x - use the natural x position for first and last slot, to prevent the top
  14136. // and botton slice connectors from touching each other on either side
  14137. x = options.justify ?
  14138. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  14139. series.getX(slotIndex === 0 || slotIndex === slots.length - 1 ? naturalY : y, i);
  14140. // Record the placement and visibility
  14141. dataLabel._attr = {
  14142. visibility: visibility,
  14143. align: labelPos[6]
  14144. };
  14145. dataLabel._pos = {
  14146. x: x + options.x +
  14147. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  14148. y: y + options.y - 10 // 10 is for the baseline (label vs text)
  14149. };
  14150. dataLabel.connX = x;
  14151. dataLabel.connY = y;
  14152. // Detect overflowing data labels
  14153. if (this.options.size === null) {
  14154. dataLabelWidth = dataLabel.width;
  14155. // Overflow left
  14156. if (x - dataLabelWidth < connectorPadding) {
  14157. overflow[3] = mathMax(mathRound(dataLabelWidth - x + connectorPadding), overflow[3]);
  14158. // Overflow right
  14159. } else if (x + dataLabelWidth > plotWidth - connectorPadding) {
  14160. overflow[1] = mathMax(mathRound(x + dataLabelWidth - plotWidth + connectorPadding), overflow[1]);
  14161. }
  14162. // Overflow top
  14163. if (y - labelHeight / 2 < 0) {
  14164. overflow[0] = mathMax(mathRound(-y + labelHeight / 2), overflow[0]);
  14165. // Overflow left
  14166. } else if (y + labelHeight / 2 > plotHeight) {
  14167. overflow[2] = mathMax(mathRound(y + labelHeight / 2 - plotHeight), overflow[2]);
  14168. }
  14169. }
  14170. } // for each point
  14171. } // for each half
  14172. // Do not apply the final placement and draw the connectors until we have verified
  14173. // that labels are not spilling over.
  14174. if (arrayMax(overflow) === 0 || this.verifyDataLabelOverflow(overflow)) {
  14175. // Place the labels in the final position
  14176. this.placeDataLabels();
  14177. // Draw the connectors
  14178. if (outside && connectorWidth) {
  14179. each(this.points, function (point) {
  14180. connector = point.connector;
  14181. labelPos = point.labelPos;
  14182. dataLabel = point.dataLabel;
  14183. if (dataLabel && dataLabel._pos) {
  14184. visibility = dataLabel._attr.visibility;
  14185. x = dataLabel.connX;
  14186. y = dataLabel.connY;
  14187. connectorPath = softConnector ? [
  14188. M,
  14189. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14190. 'C',
  14191. x, y, // first break, next to the label
  14192. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  14193. labelPos[2], labelPos[3], // second break
  14194. L,
  14195. labelPos[4], labelPos[5] // base
  14196. ] : [
  14197. M,
  14198. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14199. L,
  14200. labelPos[2], labelPos[3], // second break
  14201. L,
  14202. labelPos[4], labelPos[5] // base
  14203. ];
  14204. if (connector) {
  14205. connector.animate({ d: connectorPath });
  14206. connector.attr('visibility', visibility);
  14207. } else {
  14208. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  14209. 'stroke-width': connectorWidth,
  14210. stroke: options.connectorColor || point.color || '#606060',
  14211. visibility: visibility
  14212. //zIndex: 0 // #2722 (reversed)
  14213. })
  14214. .add(series.group);
  14215. }
  14216. } else if (connector) {
  14217. point.connector = connector.destroy();
  14218. }
  14219. });
  14220. }
  14221. }
  14222. };
  14223. /**
  14224. * Perform the final placement of the data labels after we have verified that they
  14225. * fall within the plot area.
  14226. */
  14227. seriesTypes.pie.prototype.placeDataLabels = function () {
  14228. each(this.points, function (point) {
  14229. var dataLabel = point.dataLabel,
  14230. _pos;
  14231. if (dataLabel) {
  14232. _pos = dataLabel._pos;
  14233. if (_pos) {
  14234. dataLabel.attr(dataLabel._attr);
  14235. dataLabel[dataLabel.moved ? 'animate' : 'attr'](_pos);
  14236. dataLabel.moved = true;
  14237. } else if (dataLabel) {
  14238. dataLabel.attr({ y: -999 });
  14239. }
  14240. }
  14241. });
  14242. };
  14243. seriesTypes.pie.prototype.alignDataLabel = noop;
  14244. /**
  14245. * Verify whether the data labels are allowed to draw, or we should run more translation and data
  14246. * label positioning to keep them inside the plot area. Returns true when data labels are ready
  14247. * to draw.
  14248. */
  14249. seriesTypes.pie.prototype.verifyDataLabelOverflow = function (overflow) {
  14250. var center = this.center,
  14251. options = this.options,
  14252. centerOption = options.center,
  14253. minSize = options.minSize || 80,
  14254. newSize = minSize,
  14255. ret;
  14256. // Handle horizontal size and center
  14257. if (centerOption[0] !== null) { // Fixed center
  14258. newSize = mathMax(center[2] - mathMax(overflow[1], overflow[3]), minSize);
  14259. } else { // Auto center
  14260. newSize = mathMax(
  14261. center[2] - overflow[1] - overflow[3], // horizontal overflow
  14262. minSize
  14263. );
  14264. center[0] += (overflow[3] - overflow[1]) / 2; // horizontal center
  14265. }
  14266. // Handle vertical size and center
  14267. if (centerOption[1] !== null) { // Fixed center
  14268. newSize = mathMax(mathMin(newSize, center[2] - mathMax(overflow[0], overflow[2])), minSize);
  14269. } else { // Auto center
  14270. newSize = mathMax(
  14271. mathMin(
  14272. newSize,
  14273. center[2] - overflow[0] - overflow[2] // vertical overflow
  14274. ),
  14275. minSize
  14276. );
  14277. center[1] += (overflow[0] - overflow[2]) / 2; // vertical center
  14278. }
  14279. // If the size must be decreased, we need to run translate and drawDataLabels again
  14280. if (newSize < center[2]) {
  14281. center[2] = newSize;
  14282. this.translate(center);
  14283. each(this.points, function (point) {
  14284. if (point.dataLabel) {
  14285. point.dataLabel._pos = null; // reset
  14286. }
  14287. });
  14288. if (this.drawDataLabels) {
  14289. this.drawDataLabels();
  14290. }
  14291. // Else, return true to indicate that the pie and its labels is within the plot area
  14292. } else {
  14293. ret = true;
  14294. }
  14295. return ret;
  14296. };
  14297. }
  14298. if (seriesTypes.column) {
  14299. /**
  14300. * Override the basic data label alignment by adjusting for the position of the column
  14301. */
  14302. seriesTypes.column.prototype.alignDataLabel = function (point, dataLabel, options, alignTo, isNew) {
  14303. var chart = this.chart,
  14304. inverted = chart.inverted,
  14305. dlBox = point.dlBox || point.shapeArgs, // data label box for alignment
  14306. below = point.below || (point.plotY > pick(this.translatedThreshold, chart.plotSizeY)),
  14307. inside = pick(options.inside, !!this.options.stacking); // draw it inside the box?
  14308. // Align to the column itself, or the top of it
  14309. if (dlBox) { // Area range uses this method but not alignTo
  14310. alignTo = merge(dlBox);
  14311. if (inverted) {
  14312. alignTo = {
  14313. x: chart.plotWidth - alignTo.y - alignTo.height,
  14314. y: chart.plotHeight - alignTo.x - alignTo.width,
  14315. width: alignTo.height,
  14316. height: alignTo.width
  14317. };
  14318. }
  14319. // Compute the alignment box
  14320. if (!inside) {
  14321. if (inverted) {
  14322. alignTo.x += below ? 0 : alignTo.width;
  14323. alignTo.width = 0;
  14324. } else {
  14325. alignTo.y += below ? alignTo.height : 0;
  14326. alignTo.height = 0;
  14327. }
  14328. }
  14329. }
  14330. // When alignment is undefined (typically columns and bars), display the individual
  14331. // point below or above the point depending on the threshold
  14332. options.align = pick(
  14333. options.align,
  14334. !inverted || inside ? 'center' : below ? 'right' : 'left'
  14335. );
  14336. options.verticalAlign = pick(
  14337. options.verticalAlign,
  14338. inverted || inside ? 'middle' : below ? 'top' : 'bottom'
  14339. );
  14340. // Call the parent method
  14341. Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  14342. };
  14343. }
  14344. /**
  14345. * TrackerMixin for points and graphs
  14346. */
  14347. var TrackerMixin = Highcharts.TrackerMixin = {
  14348. drawTrackerPoint: function () {
  14349. var series = this,
  14350. chart = series.chart,
  14351. pointer = chart.pointer,
  14352. cursor = series.options.cursor,
  14353. css = cursor && { cursor: cursor },
  14354. onMouseOver = function (e) {
  14355. var target = e.target,
  14356. point;
  14357. if (chart.hoverSeries !== series) {
  14358. series.onMouseOver();
  14359. }
  14360. while (target && !point) {
  14361. point = target.point;
  14362. target = target.parentNode;
  14363. }
  14364. if (point !== UNDEFINED && point !== chart.hoverPoint) { // undefined on graph in scatterchart
  14365. point.onMouseOver(e);
  14366. }
  14367. };
  14368. // Add reference to the point
  14369. each(series.points, function (point) {
  14370. if (point.graphic) {
  14371. point.graphic.element.point = point;
  14372. }
  14373. if (point.dataLabel) {
  14374. point.dataLabel.element.point = point;
  14375. }
  14376. });
  14377. // Add the event listeners, we need to do this only once
  14378. if (!series._hasTracking) {
  14379. each(series.trackerGroups, function (key) {
  14380. if (series[key]) { // we don't always have dataLabelsGroup
  14381. series[key]
  14382. .addClass(PREFIX + 'tracker')
  14383. .on('mouseover', onMouseOver)
  14384. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  14385. .css(css);
  14386. if (hasTouch) {
  14387. series[key].on('touchstart', onMouseOver);
  14388. }
  14389. }
  14390. });
  14391. series._hasTracking = true;
  14392. }
  14393. },
  14394. /**
  14395. * Draw the tracker object that sits above all data labels and markers to
  14396. * track mouse events on the graph or points. For the line type charts
  14397. * the tracker uses the same graphPath, but with a greater stroke width
  14398. * for better control.
  14399. */
  14400. drawTrackerGraph: function () {
  14401. var series = this,
  14402. options = series.options,
  14403. trackByArea = options.trackByArea,
  14404. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  14405. trackerPathLength = trackerPath.length,
  14406. chart = series.chart,
  14407. pointer = chart.pointer,
  14408. renderer = chart.renderer,
  14409. snap = chart.options.tooltip.snap,
  14410. tracker = series.tracker,
  14411. cursor = options.cursor,
  14412. css = cursor && { cursor: cursor },
  14413. singlePoints = series.singlePoints,
  14414. singlePoint,
  14415. i,
  14416. onMouseOver = function () {
  14417. if (chart.hoverSeries !== series) {
  14418. series.onMouseOver();
  14419. }
  14420. },
  14421. /*
  14422. * Empirical lowest possible opacities for TRACKER_FILL for an element to stay invisible but clickable
  14423. * IE6: 0.002
  14424. * IE7: 0.002
  14425. * IE8: 0.002
  14426. * IE9: 0.00000000001 (unlimited)
  14427. * IE10: 0.0001 (exporting only)
  14428. * FF: 0.00000000001 (unlimited)
  14429. * Chrome: 0.000001
  14430. * Safari: 0.000001
  14431. * Opera: 0.00000000001 (unlimited)
  14432. */
  14433. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.0001 : 0.002) + ')';
  14434. // Extend end points. A better way would be to use round linecaps,
  14435. // but those are not clickable in VML.
  14436. if (trackerPathLength && !trackByArea) {
  14437. i = trackerPathLength + 1;
  14438. while (i--) {
  14439. if (trackerPath[i] === M) { // extend left side
  14440. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  14441. }
  14442. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  14443. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  14444. }
  14445. }
  14446. }
  14447. // handle single points
  14448. for (i = 0; i < singlePoints.length; i++) {
  14449. singlePoint = singlePoints[i];
  14450. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  14451. L, singlePoint.plotX + snap, singlePoint.plotY);
  14452. }
  14453. // draw the tracker
  14454. if (tracker) {
  14455. tracker.attr({ d: trackerPath });
  14456. } else { // create
  14457. series.tracker = renderer.path(trackerPath)
  14458. .attr({
  14459. 'stroke-linejoin': 'round', // #1225
  14460. visibility: series.visible ? VISIBLE : HIDDEN,
  14461. stroke: TRACKER_FILL,
  14462. fill: trackByArea ? TRACKER_FILL : NONE,
  14463. 'stroke-width' : options.lineWidth + (trackByArea ? 0 : 2 * snap),
  14464. zIndex: 2
  14465. })
  14466. .add(series.group);
  14467. // The tracker is added to the series group, which is clipped, but is covered
  14468. // by the marker group. So the marker group also needs to capture events.
  14469. each([series.tracker, series.markerGroup], function (tracker) {
  14470. tracker.addClass(PREFIX + 'tracker')
  14471. .on('mouseover', onMouseOver)
  14472. .on('mouseout', function (e) { pointer.onTrackerMouseOut(e); })
  14473. .css(css);
  14474. if (hasTouch) {
  14475. tracker.on('touchstart', onMouseOver);
  14476. }
  14477. });
  14478. }
  14479. }
  14480. };
  14481. /* End TrackerMixin */
  14482. /**
  14483. * Add tracking event listener to the series group, so the point graphics
  14484. * themselves act as trackers
  14485. */
  14486. if (seriesTypes.column) {
  14487. ColumnSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  14488. }
  14489. if (seriesTypes.pie) {
  14490. seriesTypes.pie.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  14491. }
  14492. if (seriesTypes.scatter) {
  14493. ScatterSeries.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
  14494. }
  14495. /*
  14496. * Extend Legend for item events
  14497. */
  14498. extend(Legend.prototype, {
  14499. setItemEvents: function (item, legendItem, useHTML, itemStyle, itemHiddenStyle) {
  14500. var legend = this;
  14501. // Set the events on the item group, or in case of useHTML, the item itself (#1249)
  14502. (useHTML ? legendItem : item.legendGroup).on('mouseover', function () {
  14503. item.setState(HOVER_STATE);
  14504. legendItem.css(legend.options.itemHoverStyle);
  14505. })
  14506. .on('mouseout', function () {
  14507. legendItem.css(item.visible ? itemStyle : itemHiddenStyle);
  14508. item.setState();
  14509. })
  14510. .on('click', function (event) {
  14511. var strLegendItemClick = 'legendItemClick',
  14512. fnLegendItemClick = function () {
  14513. item.setVisible();
  14514. };
  14515. // Pass over the click/touch event. #4.
  14516. event = {
  14517. browserEvent: event
  14518. };
  14519. // click the name or symbol
  14520. if (item.firePointEvent) { // point
  14521. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  14522. } else {
  14523. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  14524. }
  14525. });
  14526. },
  14527. createCheckboxForItem: function (item) {
  14528. var legend = this;
  14529. item.checkbox = createElement('input', {
  14530. type: 'checkbox',
  14531. checked: item.selected,
  14532. defaultChecked: item.selected // required by IE7
  14533. }, legend.options.itemCheckboxStyle, legend.chart.container);
  14534. addEvent(item.checkbox, 'click', function (event) {
  14535. var target = event.target;
  14536. fireEvent(item, 'checkboxClick', {
  14537. checked: target.checked
  14538. },
  14539. function () {
  14540. item.select();
  14541. }
  14542. );
  14543. });
  14544. }
  14545. });
  14546. /*
  14547. * Add pointer cursor to legend itemstyle in defaultOptions
  14548. */
  14549. defaultOptions.legend.itemStyle.cursor = 'pointer';
  14550. /*
  14551. * Extend the Chart object with interaction
  14552. */
  14553. extend(Chart.prototype, {
  14554. /**
  14555. * Display the zoom button
  14556. */
  14557. showResetZoom: function () {
  14558. var chart = this,
  14559. lang = defaultOptions.lang,
  14560. btnOptions = chart.options.chart.resetZoomButton,
  14561. theme = btnOptions.theme,
  14562. states = theme.states,
  14563. alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
  14564. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, function () { chart.zoomOut(); }, theme, states && states.hover)
  14565. .attr({
  14566. align: btnOptions.position.align,
  14567. title: lang.resetZoomTitle
  14568. })
  14569. .add()
  14570. .align(btnOptions.position, false, alignTo);
  14571. },
  14572. /**
  14573. * Zoom out to 1:1
  14574. */
  14575. zoomOut: function () {
  14576. var chart = this;
  14577. fireEvent(chart, 'selection', { resetSelection: true }, function () {
  14578. chart.zoom();
  14579. });
  14580. },
  14581. /**
  14582. * Zoom into a given portion of the chart given by axis coordinates
  14583. * @param {Object} event
  14584. */
  14585. zoom: function (event) {
  14586. var chart = this,
  14587. hasZoomed,
  14588. pointer = chart.pointer,
  14589. displayButton = false,
  14590. resetZoomButton;
  14591. // If zoom is called with no arguments, reset the axes
  14592. if (!event || event.resetSelection) {
  14593. each(chart.axes, function (axis) {
  14594. hasZoomed = axis.zoom();
  14595. });
  14596. } else { // else, zoom in on all axes
  14597. each(event.xAxis.concat(event.yAxis), function (axisData) {
  14598. var axis = axisData.axis,
  14599. isXAxis = axis.isXAxis;
  14600. // don't zoom more than minRange
  14601. if (pointer[isXAxis ? 'zoomX' : 'zoomY'] || pointer[isXAxis ? 'pinchX' : 'pinchY']) {
  14602. hasZoomed = axis.zoom(axisData.min, axisData.max);
  14603. if (axis.displayBtn) {
  14604. displayButton = true;
  14605. }
  14606. }
  14607. });
  14608. }
  14609. // Show or hide the Reset zoom button
  14610. resetZoomButton = chart.resetZoomButton;
  14611. if (displayButton && !resetZoomButton) {
  14612. chart.showResetZoom();
  14613. } else if (!displayButton && isObject(resetZoomButton)) {
  14614. chart.resetZoomButton = resetZoomButton.destroy();
  14615. }
  14616. // Redraw
  14617. if (hasZoomed) {
  14618. chart.redraw(
  14619. pick(chart.options.chart.animation, event && event.animation, chart.pointCount < 100) // animation
  14620. );
  14621. }
  14622. },
  14623. /**
  14624. * Pan the chart by dragging the mouse across the pane. This function is called
  14625. * on mouse move, and the distance to pan is computed from chartX compared to
  14626. * the first chartX position in the dragging operation.
  14627. */
  14628. pan: function (e, panning) {
  14629. var chart = this,
  14630. hoverPoints = chart.hoverPoints,
  14631. doRedraw;
  14632. // remove active points for shared tooltip
  14633. if (hoverPoints) {
  14634. each(hoverPoints, function (point) {
  14635. point.setState();
  14636. });
  14637. }
  14638. each(panning === 'xy' ? [1, 0] : [1], function (isX) { // xy is used in maps
  14639. var mousePos = e[isX ? 'chartX' : 'chartY'],
  14640. axis = chart[isX ? 'xAxis' : 'yAxis'][0],
  14641. startPos = chart[isX ? 'mouseDownX' : 'mouseDownY'],
  14642. halfPointRange = (axis.pointRange || 0) / 2,
  14643. extremes = axis.getExtremes(),
  14644. newMin = axis.toValue(startPos - mousePos, true) + halfPointRange,
  14645. newMax = axis.toValue(startPos + chart[isX ? 'plotWidth' : 'plotHeight'] - mousePos, true) - halfPointRange;
  14646. if (axis.series.length && newMin > mathMin(extremes.dataMin, extremes.min) && newMax < mathMax(extremes.dataMax, extremes.max)) {
  14647. axis.setExtremes(newMin, newMax, false, false, { trigger: 'pan' });
  14648. doRedraw = true;
  14649. }
  14650. chart[isX ? 'mouseDownX' : 'mouseDownY'] = mousePos; // set new reference for next run
  14651. });
  14652. if (doRedraw) {
  14653. chart.redraw(false);
  14654. }
  14655. css(chart.container, { cursor: 'move' });
  14656. }
  14657. });
  14658. /*
  14659. * Extend the Point object with interaction
  14660. */
  14661. extend(Point.prototype, {
  14662. /**
  14663. * Toggle the selection status of a point
  14664. * @param {Boolean} selected Whether to select or unselect the point.
  14665. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  14666. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  14667. */
  14668. select: function (selected, accumulate) {
  14669. var point = this,
  14670. series = point.series,
  14671. chart = series.chart;
  14672. selected = pick(selected, !point.selected);
  14673. // fire the event with the defalut handler
  14674. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  14675. point.selected = point.options.selected = selected;
  14676. series.options.data[inArray(point, series.data)] = point.options;
  14677. point.setState(selected && SELECT_STATE);
  14678. // unselect all other points unless Ctrl or Cmd + click
  14679. if (!accumulate) {
  14680. each(chart.getSelectedPoints(), function (loopPoint) {
  14681. if (loopPoint.selected && loopPoint !== point) {
  14682. loopPoint.selected = loopPoint.options.selected = false;
  14683. series.options.data[inArray(loopPoint, series.data)] = loopPoint.options;
  14684. loopPoint.setState(NORMAL_STATE);
  14685. loopPoint.firePointEvent('unselect');
  14686. }
  14687. });
  14688. }
  14689. });
  14690. },
  14691. /**
  14692. * Runs on mouse over the point
  14693. */
  14694. onMouseOver: function (e) {
  14695. var point = this,
  14696. series = point.series,
  14697. chart = series.chart,
  14698. tooltip = chart.tooltip,
  14699. hoverPoint = chart.hoverPoint;
  14700. // set normal state to previous series
  14701. if (hoverPoint && hoverPoint !== point) {
  14702. hoverPoint.onMouseOut();
  14703. }
  14704. // trigger the event
  14705. point.firePointEvent('mouseOver');
  14706. // update the tooltip
  14707. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  14708. tooltip.refresh(point, e);
  14709. }
  14710. // hover this
  14711. point.setState(HOVER_STATE);
  14712. chart.hoverPoint = point;
  14713. },
  14714. /**
  14715. * Runs on mouse out from the point
  14716. */
  14717. onMouseOut: function () {
  14718. var chart = this.series.chart,
  14719. hoverPoints = chart.hoverPoints;
  14720. if (!hoverPoints || inArray(this, hoverPoints) === -1) { // #887
  14721. this.firePointEvent('mouseOut');
  14722. this.setState();
  14723. chart.hoverPoint = null;
  14724. }
  14725. },
  14726. /**
  14727. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  14728. * causes a name clash in MooTools
  14729. * @param {String} eventType
  14730. * @param {Object} eventArgs Additional event arguments
  14731. * @param {Function} defaultFunction Default event handler
  14732. */
  14733. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  14734. var point = this,
  14735. series = this.series,
  14736. seriesOptions = series.options;
  14737. // load event handlers on demand to save time on mouseover/out
  14738. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  14739. this.importEvents();
  14740. }
  14741. // add default handler if in selection mode
  14742. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  14743. defaultFunction = function (event) {
  14744. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  14745. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  14746. };
  14747. }
  14748. fireEvent(this, eventType, eventArgs, defaultFunction);
  14749. },
  14750. /**
  14751. * Import events from the series' and point's options. Only do it on
  14752. * demand, to save processing time on hovering.
  14753. */
  14754. importEvents: function () {
  14755. if (!this.hasImportedEvents) {
  14756. var point = this,
  14757. options = merge(point.series.options.point, point.options),
  14758. events = options.events,
  14759. eventType;
  14760. point.events = events;
  14761. for (eventType in events) {
  14762. addEvent(point, eventType, events[eventType]);
  14763. }
  14764. this.hasImportedEvents = true;
  14765. }
  14766. },
  14767. /**
  14768. * Set the point's state
  14769. * @param {String} state
  14770. */
  14771. setState: function (state, move) {
  14772. var point = this,
  14773. plotX = point.plotX,
  14774. plotY = point.plotY,
  14775. series = point.series,
  14776. stateOptions = series.options.states,
  14777. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  14778. normalDisabled = markerOptions && !markerOptions.enabled,
  14779. markerStateOptions = markerOptions && markerOptions.states[state],
  14780. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  14781. stateMarkerGraphic = series.stateMarkerGraphic,
  14782. pointMarker = point.marker || {},
  14783. chart = series.chart,
  14784. radius,
  14785. newSymbol,
  14786. pointAttr = point.pointAttr;
  14787. state = state || NORMAL_STATE; // empty string
  14788. move = move && stateMarkerGraphic;
  14789. if (
  14790. // already has this state
  14791. (state === point.state && !move) ||
  14792. // selected points don't respond to hover
  14793. (point.selected && state !== SELECT_STATE) ||
  14794. // series' state options is disabled
  14795. (stateOptions[state] && stateOptions[state].enabled === false) ||
  14796. // general point marker's state options is disabled
  14797. (state && (stateDisabled || (normalDisabled && !markerStateOptions.enabled))) ||
  14798. // individual point marker's state options is disabled
  14799. (state && pointMarker.states && pointMarker.states[state] && pointMarker.states[state].enabled === false) // #1610
  14800. ) {
  14801. return;
  14802. }
  14803. // apply hover styles to the existing point
  14804. if (point.graphic) {
  14805. radius = markerOptions && point.graphic.symbolName && pointAttr[state].r;
  14806. point.graphic.attr(merge(
  14807. pointAttr[state],
  14808. radius ? { // new symbol attributes (#507, #612)
  14809. x: plotX - radius,
  14810. y: plotY - radius,
  14811. width: 2 * radius,
  14812. height: 2 * radius
  14813. } : {}
  14814. ));
  14815. } else {
  14816. // if a graphic is not applied to each point in the normal state, create a shared
  14817. // graphic for the hover state
  14818. if (state && markerStateOptions) {
  14819. radius = markerStateOptions.radius;
  14820. newSymbol = pointMarker.symbol || series.symbol;
  14821. // If the point has another symbol than the previous one, throw away the
  14822. // state marker graphic and force a new one (#1459)
  14823. if (stateMarkerGraphic && stateMarkerGraphic.currentSymbol !== newSymbol) {
  14824. stateMarkerGraphic = stateMarkerGraphic.destroy();
  14825. }
  14826. // Add a new state marker graphic
  14827. if (!stateMarkerGraphic) {
  14828. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  14829. newSymbol,
  14830. plotX - radius,
  14831. plotY - radius,
  14832. 2 * radius,
  14833. 2 * radius
  14834. )
  14835. .attr(pointAttr[state])
  14836. .add(series.markerGroup);
  14837. stateMarkerGraphic.currentSymbol = newSymbol;
  14838. // Move the existing graphic
  14839. } else {
  14840. stateMarkerGraphic[move ? 'animate' : 'attr']({ // #1054
  14841. x: plotX - radius,
  14842. y: plotY - radius
  14843. });
  14844. }
  14845. }
  14846. if (stateMarkerGraphic) {
  14847. stateMarkerGraphic[state && chart.isInsidePlot(plotX, plotY, chart.inverted) ? 'show' : 'hide'](); // #2450
  14848. }
  14849. }
  14850. point.state = state;
  14851. }
  14852. });
  14853. /*
  14854. * Extend the Series object with interaction
  14855. */
  14856. extend(Series.prototype, {
  14857. /**
  14858. * Series mouse over handler
  14859. */
  14860. onMouseOver: function () {
  14861. var series = this,
  14862. chart = series.chart,
  14863. hoverSeries = chart.hoverSeries;
  14864. // set normal state to previous series
  14865. if (hoverSeries && hoverSeries !== series) {
  14866. hoverSeries.onMouseOut();
  14867. }
  14868. // trigger the event, but to save processing time,
  14869. // only if defined
  14870. if (series.options.events.mouseOver) {
  14871. fireEvent(series, 'mouseOver');
  14872. }
  14873. // hover this
  14874. series.setState(HOVER_STATE);
  14875. chart.hoverSeries = series;
  14876. },
  14877. /**
  14878. * Series mouse out handler
  14879. */
  14880. onMouseOut: function () {
  14881. // trigger the event only if listeners exist
  14882. var series = this,
  14883. options = series.options,
  14884. chart = series.chart,
  14885. tooltip = chart.tooltip,
  14886. hoverPoint = chart.hoverPoint;
  14887. // trigger mouse out on the point, which must be in this series
  14888. if (hoverPoint) {
  14889. hoverPoint.onMouseOut();
  14890. }
  14891. // fire the mouse out event
  14892. if (series && options.events.mouseOut) {
  14893. fireEvent(series, 'mouseOut');
  14894. }
  14895. // hide the tooltip
  14896. if (tooltip && !options.stickyTracking && (!tooltip.shared || series.noSharedTooltip)) {
  14897. tooltip.hide();
  14898. }
  14899. // set normal state
  14900. series.setState();
  14901. chart.hoverSeries = null;
  14902. },
  14903. /**
  14904. * Set the state of the graph
  14905. */
  14906. setState: function (state) {
  14907. var series = this,
  14908. options = series.options,
  14909. graph = series.graph,
  14910. graphNeg = series.graphNeg,
  14911. stateOptions = options.states,
  14912. lineWidth = options.lineWidth,
  14913. attribs;
  14914. state = state || NORMAL_STATE;
  14915. if (series.state !== state) {
  14916. series.state = state;
  14917. if (stateOptions[state] && stateOptions[state].enabled === false) {
  14918. return;
  14919. }
  14920. if (state) {
  14921. lineWidth = stateOptions[state].lineWidth || lineWidth + 1;
  14922. }
  14923. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  14924. attribs = {
  14925. 'stroke-width': lineWidth
  14926. };
  14927. // use attr because animate will cause any other animation on the graph to stop
  14928. graph.attr(attribs);
  14929. if (graphNeg) {
  14930. graphNeg.attr(attribs);
  14931. }
  14932. }
  14933. }
  14934. },
  14935. /**
  14936. * Set the visibility of the graph
  14937. *
  14938. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  14939. * the visibility is toggled.
  14940. */
  14941. setVisible: function (vis, redraw) {
  14942. var series = this,
  14943. chart = series.chart,
  14944. legendItem = series.legendItem,
  14945. showOrHide,
  14946. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  14947. oldVisibility = series.visible;
  14948. // if called without an argument, toggle visibility
  14949. series.visible = vis = series.userOptions.visible = vis === UNDEFINED ? !oldVisibility : vis;
  14950. showOrHide = vis ? 'show' : 'hide';
  14951. // show or hide elements
  14952. each(['group', 'dataLabelsGroup', 'markerGroup', 'tracker'], function (key) {
  14953. if (series[key]) {
  14954. series[key][showOrHide]();
  14955. }
  14956. });
  14957. // hide tooltip (#1361)
  14958. if (chart.hoverSeries === series) {
  14959. series.onMouseOut();
  14960. }
  14961. if (legendItem) {
  14962. chart.legend.colorizeItem(series, vis);
  14963. }
  14964. // rescale or adapt to resized chart
  14965. series.isDirty = true;
  14966. // in a stack, all other series are affected
  14967. if (series.options.stacking) {
  14968. each(chart.series, function (otherSeries) {
  14969. if (otherSeries.options.stacking && otherSeries.visible) {
  14970. otherSeries.isDirty = true;
  14971. }
  14972. });
  14973. }
  14974. // show or hide linked series
  14975. each(series.linkedSeries, function (otherSeries) {
  14976. otherSeries.setVisible(vis, false);
  14977. });
  14978. if (ignoreHiddenSeries) {
  14979. chart.isDirtyBox = true;
  14980. }
  14981. if (redraw !== false) {
  14982. chart.redraw();
  14983. }
  14984. fireEvent(series, showOrHide);
  14985. },
  14986. /**
  14987. * Memorize tooltip texts and positions
  14988. */
  14989. setTooltipPoints: function (renew) {
  14990. var series = this,
  14991. points = [],
  14992. pointsLength,
  14993. low,
  14994. high,
  14995. xAxis = series.xAxis,
  14996. xExtremes = xAxis && xAxis.getExtremes(),
  14997. axisLength = xAxis ? (xAxis.tooltipLen || xAxis.len) : series.chart.plotSizeX, // tooltipLen and tooltipPosName used in polar
  14998. point,
  14999. pointX,
  15000. nextPoint,
  15001. i,
  15002. tooltipPoints = []; // a lookup array for each pixel in the x dimension
  15003. // don't waste resources if tracker is disabled
  15004. if (series.options.enableMouseTracking === false || series.singularTooltips) {
  15005. return;
  15006. }
  15007. // renew
  15008. if (renew) {
  15009. series.tooltipPoints = null;
  15010. }
  15011. // concat segments to overcome null values
  15012. each(series.segments || series.points, function (segment) {
  15013. points = points.concat(segment);
  15014. });
  15015. // Reverse the points in case the X axis is reversed
  15016. if (xAxis && xAxis.reversed) {
  15017. points = points.reverse();
  15018. }
  15019. // Polar needs additional shaping
  15020. if (series.orderTooltipPoints) {
  15021. series.orderTooltipPoints(points);
  15022. }
  15023. // Assign each pixel position to the nearest point
  15024. pointsLength = points.length;
  15025. for (i = 0; i < pointsLength; i++) {
  15026. point = points[i];
  15027. pointX = point.x;
  15028. if (pointX >= xExtremes.min && pointX <= xExtremes.max) { // #1149
  15029. nextPoint = points[i + 1];
  15030. // Set this range's low to the last range's high plus one
  15031. low = high === UNDEFINED ? 0 : high + 1;
  15032. // Now find the new high
  15033. high = points[i + 1] ?
  15034. mathMin(mathMax(0, mathFloor( // #2070
  15035. (point.clientX + (nextPoint ? (nextPoint.wrappedClientX || nextPoint.clientX) : axisLength)) / 2
  15036. )), axisLength) :
  15037. axisLength;
  15038. while (low >= 0 && low <= high) {
  15039. tooltipPoints[low++] = point;
  15040. }
  15041. }
  15042. }
  15043. series.tooltipPoints = tooltipPoints;
  15044. },
  15045. /**
  15046. * Show the graph
  15047. */
  15048. show: function () {
  15049. this.setVisible(true);
  15050. },
  15051. /**
  15052. * Hide the graph
  15053. */
  15054. hide: function () {
  15055. this.setVisible(false);
  15056. },
  15057. /**
  15058. * Set the selected state of the graph
  15059. *
  15060. * @param selected {Boolean} True to select the series, false to unselect. If
  15061. * UNDEFINED, the selection state is toggled.
  15062. */
  15063. select: function (selected) {
  15064. var series = this;
  15065. // if called without an argument, toggle
  15066. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  15067. if (series.checkbox) {
  15068. series.checkbox.checked = selected;
  15069. }
  15070. fireEvent(series, selected ? 'select' : 'unselect');
  15071. },
  15072. drawTracker: TrackerMixin.drawTrackerGraph
  15073. });
  15074. // global variables
  15075. extend(Highcharts, {
  15076. // Constructors
  15077. Axis: Axis,
  15078. Chart: Chart,
  15079. Color: Color,
  15080. Point: Point,
  15081. Tick: Tick,
  15082. Renderer: Renderer,
  15083. Series: Series,
  15084. SVGElement: SVGElement,
  15085. SVGRenderer: SVGRenderer,
  15086. // Various
  15087. arrayMin: arrayMin,
  15088. arrayMax: arrayMax,
  15089. charts: charts,
  15090. dateFormat: dateFormat,
  15091. format: format,
  15092. pathAnim: pathAnim,
  15093. getOptions: getOptions,
  15094. hasBidiBug: hasBidiBug,
  15095. isTouchDevice: isTouchDevice,
  15096. numberFormat: numberFormat,
  15097. seriesTypes: seriesTypes,
  15098. setOptions: setOptions,
  15099. addEvent: addEvent,
  15100. removeEvent: removeEvent,
  15101. createElement: createElement,
  15102. discardElement: discardElement,
  15103. css: css,
  15104. each: each,
  15105. extend: extend,
  15106. map: map,
  15107. merge: merge,
  15108. pick: pick,
  15109. splat: splat,
  15110. extendClass: extendClass,
  15111. pInt: pInt,
  15112. wrap: wrap,
  15113. svg: hasSVG,
  15114. canvas: useCanVG,
  15115. vml: !hasSVG && !useCanVG,
  15116. product: PRODUCT,
  15117. version: VERSION
  15118. });
  15119. }());