Guest User

Untitled

a guest
Apr 20th, 2013
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 382.35 KB | None | 0 0
  1. // ==ClosureCompiler==
  2. // @compilation_level SIMPLE_OPTIMIZATIONS
  3.  
  4. /**
  5. * @license Highcharts JS v2.2.5 (2012-06-08)
  6. *
  7. * (c) 2009-2011 Torstein Hønsi
  8. *
  9. * License: www.highcharts.com/license
  10. */
  11.  
  12. // JSLint options:
  13. /*global Highcharts, document, window, navigator, setInterval, clearInterval, clearTimeout, setTimeout, location, jQuery, $, console */
  14.  
  15. (function () {
  16. // encapsulated variables
  17. var UNDEFINED,
  18. doc = document,
  19. win = window,
  20. math = Math,
  21. mathRound = math.round,
  22. mathFloor = math.floor,
  23. mathCeil = math.ceil,
  24. mathMax = math.max,
  25. mathMin = math.min,
  26. mathAbs = math.abs,
  27. mathCos = math.cos,
  28. mathSin = math.sin,
  29. mathPI = math.PI,
  30. deg2rad = mathPI * 2 / 360,
  31.  
  32.  
  33. // some variables
  34. userAgent = navigator.userAgent,
  35. isIE = /msie/i.test(userAgent) && !win.opera,
  36. docMode8 = doc.documentMode === 8,
  37. isWebKit = /AppleWebKit/.test(userAgent),
  38. isFirefox = /Firefox/.test(userAgent),
  39. SVG_NS = 'http://www.w3.org/2000/svg',
  40. hasSVG = !!doc.createElementNS && !!doc.createElementNS(SVG_NS, 'svg').createSVGRect,
  41. hasBidiBug = isFirefox && parseInt(userAgent.split('Firefox/')[1], 10) < 4, // issue #38
  42. useCanVG = !hasSVG && !isIE && !!doc.createElement('canvas').getContext,
  43. Renderer,
  44. hasTouch = doc.documentElement.ontouchstart !== UNDEFINED,
  45. symbolSizes = {},
  46. idCounter = 0,
  47. garbageBin,
  48. defaultOptions,
  49. dateFormat, // function
  50. globalAnimation,
  51. pathAnim,
  52. timeUnits,
  53. noop = function () {},
  54.  
  55. // some constants for frequently used strings
  56. DIV = 'div',
  57. ABSOLUTE = 'absolute',
  58. RELATIVE = 'relative',
  59. HIDDEN = 'hidden',
  60. PREFIX = 'highcharts-',
  61. VISIBLE = 'visible',
  62. PX = 'px',
  63. NONE = 'none',
  64. M = 'M',
  65. L = 'L',
  66. /*
  67. * Empirical lowest possible opacities for TRACKER_FILL
  68. * IE6: 0.002
  69. * IE7: 0.002
  70. * IE8: 0.002
  71. * IE9: 0.00000000001 (unlimited)
  72. * FF: 0.00000000001 (unlimited)
  73. * Chrome: 0.000001
  74. * Safari: 0.000001
  75. * Opera: 0.00000000001 (unlimited)
  76. */
  77. TRACKER_FILL = 'rgba(192,192,192,' + (hasSVG ? 0.000001 : 0.002) + ')', // invisible but clickable
  78. //TRACKER_FILL = 'rgba(192,192,192,0.5)',
  79. NORMAL_STATE = '',
  80. HOVER_STATE = 'hover',
  81. SELECT_STATE = 'select',
  82. MILLISECOND = 'millisecond',
  83. SECOND = 'second',
  84. MINUTE = 'minute',
  85. HOUR = 'hour',
  86. DAY = 'day',
  87. WEEK = 'week',
  88. MONTH = 'month',
  89. YEAR = 'year',
  90.  
  91. // constants for attributes
  92. FILL = 'fill',
  93. LINEAR_GRADIENT = 'linearGradient',
  94. STOPS = 'stops',
  95. STROKE = 'stroke',
  96. STROKE_WIDTH = 'stroke-width',
  97.  
  98. // time methods, changed based on whether or not UTC is used
  99. makeTime,
  100. getMinutes,
  101. getHours,
  102. getDay,
  103. getDate,
  104. getMonth,
  105. getFullYear,
  106. setMinutes,
  107. setHours,
  108. setDate,
  109. setMonth,
  110. setFullYear,
  111.  
  112.  
  113. // lookup over the types and the associated classes
  114. seriesTypes = {};
  115.  
  116. // The Highcharts namespace
  117. win.Highcharts = {};
  118.  
  119. /**
  120. * Extend an object with the members of another
  121. * @param {Object} a The object to be extended
  122. * @param {Object} b The object to add to the first one
  123. */
  124. function extend(a, b) {
  125. var n;
  126. if (!a) {
  127. a = {};
  128. }
  129. for (n in b) {
  130. a[n] = b[n];
  131. }
  132. return a;
  133. }
  134.  
  135. /**
  136. * Take an array and turn into a hash with even number arguments as keys and odd numbers as
  137. * values. Allows creating constants for commonly used style properties, attributes etc.
  138. * Avoid it in performance critical situations like looping
  139. */
  140. function hash() {
  141. var i = 0,
  142. args = arguments,
  143. length = args.length,
  144. obj = {};
  145. for (; i < length; i++) {
  146. obj[args[i++]] = args[i];
  147. }
  148. return obj;
  149. }
  150.  
  151. /**
  152. * Shortcut for parseInt
  153. * @param {Object} s
  154. * @param {Number} mag Magnitude
  155. */
  156. function pInt(s, mag) {
  157. return parseInt(s, mag || 10);
  158. }
  159.  
  160. /**
  161. * Check for string
  162. * @param {Object} s
  163. */
  164. function isString(s) {
  165. return typeof s === 'string';
  166. }
  167.  
  168. /**
  169. * Check for object
  170. * @param {Object} obj
  171. */
  172. function isObject(obj) {
  173. return typeof obj === 'object';
  174. }
  175.  
  176. /**
  177. * Check for array
  178. * @param {Object} obj
  179. */
  180. function isArray(obj) {
  181. return Object.prototype.toString.call(obj) === '[object Array]';
  182. }
  183.  
  184. /**
  185. * Check for number
  186. * @param {Object} n
  187. */
  188. function isNumber(n) {
  189. return typeof n === 'number';
  190. }
  191.  
  192. function log2lin(num) {
  193. return math.log(num) / math.LN10;
  194. }
  195. function lin2log(num) {
  196. return math.pow(10, num);
  197. }
  198.  
  199. /**
  200. * Remove last occurence of an item from an array
  201. * @param {Array} arr
  202. * @param {Mixed} item
  203. */
  204. function erase(arr, item) {
  205. var i = arr.length;
  206. while (i--) {
  207. if (arr[i] === item) {
  208. arr.splice(i, 1);
  209. break;
  210. }
  211. }
  212. //return arr;
  213. }
  214.  
  215. /**
  216. * Returns true if the object is not null or undefined. Like MooTools' $.defined.
  217. * @param {Object} obj
  218. */
  219. function defined(obj) {
  220. return obj !== UNDEFINED && obj !== null;
  221. }
  222.  
  223. /**
  224. * Set or get an attribute or an object of attributes. Can't use jQuery attr because
  225. * it attempts to set expando properties on the SVG element, which is not allowed.
  226. *
  227. * @param {Object} elem The DOM element to receive the attribute(s)
  228. * @param {String|Object} prop The property or an abject of key-value pairs
  229. * @param {String} value The value if a single property is set
  230. */
  231. function attr(elem, prop, value) {
  232. var key,
  233. setAttribute = 'setAttribute',
  234. ret;
  235.  
  236. // if the prop is a string
  237. if (isString(prop)) {
  238. // set the value
  239. if (defined(value)) {
  240.  
  241. elem[setAttribute](prop, value);
  242.  
  243. // get the value
  244. } else if (elem && elem.getAttribute) { // elem not defined when printing pie demo...
  245. ret = elem.getAttribute(prop);
  246. }
  247.  
  248. // else if prop is defined, it is a hash of key/value pairs
  249. } else if (defined(prop) && isObject(prop)) {
  250. for (key in prop) {
  251. elem[setAttribute](key, prop[key]);
  252. }
  253. }
  254. return ret;
  255. }
  256. /**
  257. * Check if an element is an array, and if not, make it into an array. Like
  258. * MooTools' $.splat.
  259. */
  260. function splat(obj) {
  261. return isArray(obj) ? obj : [obj];
  262. }
  263.  
  264.  
  265. /**
  266. * Return the first value that is defined. Like MooTools' $.pick.
  267. */
  268. function pick() {
  269. var args = arguments,
  270. i,
  271. arg,
  272. length = args.length;
  273. for (i = 0; i < length; i++) {
  274. arg = args[i];
  275. if (typeof arg !== 'undefined' && arg !== null) {
  276. return arg;
  277. }
  278. }
  279. }
  280.  
  281. /**
  282. * Set CSS on a given element
  283. * @param {Object} el
  284. * @param {Object} styles Style object with camel case property names
  285. */
  286. function css(el, styles) {
  287. if (isIE) {
  288. if (styles && styles.opacity !== UNDEFINED) {
  289. styles.filter = 'alpha(opacity=' + (styles.opacity * 100) + ')';
  290. }
  291. }
  292. extend(el.style, styles);
  293. }
  294.  
  295. /**
  296. * Utility function to create element with attributes and styles
  297. * @param {Object} tag
  298. * @param {Object} attribs
  299. * @param {Object} styles
  300. * @param {Object} parent
  301. * @param {Object} nopad
  302. */
  303. function createElement(tag, attribs, styles, parent, nopad) {
  304. var el = doc.createElement(tag);
  305. if (attribs) {
  306. extend(el, attribs);
  307. }
  308. if (nopad) {
  309. css(el, {padding: 0, border: NONE, margin: 0});
  310. }
  311. if (styles) {
  312. css(el, styles);
  313. }
  314. if (parent) {
  315. parent.appendChild(el);
  316. }
  317. return el;
  318. }
  319.  
  320. /**
  321. * Extend a prototyped class by new members
  322. * @param {Object} parent
  323. * @param {Object} members
  324. */
  325. function extendClass(parent, members) {
  326. var object = function () {};
  327. object.prototype = new parent();
  328. extend(object.prototype, members);
  329. return object;
  330. }
  331.  
  332. /**
  333. * How many decimals are there in a number
  334. */
  335. function getDecimals(number) {
  336.  
  337. number = (number || 0).toString();
  338.  
  339. return number.indexOf('.') > -1 ?
  340. number.split('.')[1].length :
  341. 0;
  342. }
  343.  
  344. /**
  345. * Format a number and return a string based on input settings
  346. * @param {Number} number The input number to format
  347. * @param {Number} decimals The amount of decimals
  348. * @param {String} decPoint The decimal point, defaults to the one given in the lang options
  349. * @param {String} thousandsSep The thousands separator, defaults to the one given in the lang options
  350. */
  351. function numberFormat(number, decimals, decPoint, thousandsSep) {
  352. var lang = defaultOptions.lang,
  353. // http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
  354. n = number,
  355. c = decimals === -1 ?
  356. getDecimals(number) :
  357. (isNaN(decimals = mathAbs(decimals)) ? 2 : decimals),
  358. d = decPoint === undefined ? lang.decimalPoint : decPoint,
  359. t = thousandsSep === undefined ? lang.thousandsSep : thousandsSep,
  360. s = n < 0 ? "-" : "",
  361. i = String(pInt(n = mathAbs(+n || 0).toFixed(c))),
  362. j = i.length > 3 ? i.length % 3 : 0;
  363.  
  364. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
  365. (c ? d + mathAbs(n - i).toFixed(c).slice(2) : "");
  366. }
  367.  
  368. /**
  369. * Pad a string to a given length by adding 0 to the beginning
  370. * @param {Number} number
  371. * @param {Number} length
  372. */
  373. function pad(number, length) {
  374. // Create an array of the remaining length +1 and join it with 0's
  375. return new Array((length || 2) + 1 - String(number).length).join(0) + number;
  376. }
  377.  
  378. /**
  379. * Based on http://www.php.net/manual/en/function.strftime.php
  380. * @param {String} format
  381. * @param {Number} timestamp
  382. * @param {Boolean} capitalize
  383. */
  384. dateFormat = function (format, timestamp, capitalize) {
  385. if (!defined(timestamp) || isNaN(timestamp)) {
  386. return 'Invalid date';
  387. }
  388. format = pick(format, '%Y-%m-%d %H:%M:%S');
  389.  
  390. var date = new Date(timestamp),
  391. key, // used in for constuct below
  392. // get the basic time values
  393. hours = date[getHours](),
  394. day = date[getDay](),
  395. dayOfMonth = date[getDate](),
  396. month = date[getMonth](),
  397. fullYear = date[getFullYear](),
  398. lang = defaultOptions.lang,
  399. langWeekdays = lang.weekdays,
  400. /* // uncomment this and the 'W' format key below to enable week numbers
  401. weekNumber = function () {
  402. var clone = new Date(date.valueOf()),
  403. day = clone[getDay]() == 0 ? 7 : clone[getDay](),
  404. dayNumber;
  405. clone.setDate(clone[getDate]() + 4 - day);
  406. dayNumber = mathFloor((clone.getTime() - new Date(clone[getFullYear](), 0, 1, -6)) / 86400000);
  407. return 1 + mathFloor(dayNumber / 7);
  408. },
  409. */
  410.  
  411. // list all format keys
  412. replacements = {
  413.  
  414. // Day
  415. 'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
  416. 'A': langWeekdays[day], // Long weekday, like 'Monday'
  417. 'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
  418. 'e': dayOfMonth, // Day of the month, 1 through 31
  419.  
  420. // Week (none implemented)
  421. //'W': weekNumber(),
  422.  
  423. // Month
  424. 'b': lang.shortMonths[month], // Short month, like 'Jan'
  425. 'B': lang.months[month], // Long month, like 'January'
  426. 'm': pad(month + 1), // Two digit month number, 01 through 12
  427.  
  428. // Year
  429. 'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
  430. 'Y': fullYear, // Four digits year, like 2009
  431.  
  432. // Time
  433. 'H': pad(hours), // Two digits hours in 24h format, 00 through 23
  434. 'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
  435. 'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
  436. 'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
  437. 'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
  438. 'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
  439. 'S': pad(date.getSeconds()), // Two digits seconds, 00 through 59
  440. 'L': pad(mathRound(timestamp % 1000), 3) // Milliseconds (naming from Ruby)
  441. };
  442.  
  443.  
  444. // do the replaces
  445. for (key in replacements) {
  446. format = format.replace('%' + key, replacements[key]);
  447. }
  448.  
  449. // Optionally capitalize the string and return
  450. return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
  451. };
  452.  
  453. /**
  454. * Take an interval and normalize it to multiples of 1, 2, 2.5 and 5
  455. * @param {Number} interval
  456. * @param {Array} multiples
  457. * @param {Number} magnitude
  458. * @param {Object} options
  459. */
  460. function normalizeTickInterval(interval, multiples, magnitude, options) {
  461. var normalized, i;
  462.  
  463. // round to a tenfold of 1, 2, 2.5 or 5
  464. magnitude = pick(magnitude, 1);
  465. normalized = interval / magnitude;
  466.  
  467. // multiples for a linear scale
  468. if (!multiples) {
  469. multiples = [1, 2, 2.5, 5, 10];
  470.  
  471. // the allowDecimals option
  472. if (options && options.allowDecimals === false) {
  473. if (magnitude === 1) {
  474. multiples = [1, 2, 5, 10];
  475. } else if (magnitude <= 0.1) {
  476. multiples = [1 / magnitude];
  477. }
  478. }
  479. }
  480.  
  481. // normalize the interval to the nearest multiple
  482. for (i = 0; i < multiples.length; i++) {
  483. interval = multiples[i];
  484. if (normalized <= (multiples[i] + (multiples[i + 1] || multiples[i])) / 2) {
  485. break;
  486. }
  487. }
  488.  
  489. // multiply back to the correct magnitude
  490. interval *= magnitude;
  491.  
  492. return interval;
  493. }
  494.  
  495. /**
  496. * Get a normalized tick interval for dates. Returns a configuration object with
  497. * unit range (interval), count and name. Used to prepare data for getTimeTicks.
  498. * Previously this logic was part of getTimeTicks, but as getTimeTicks now runs
  499. * of segments in stock charts, the normalizing logic was extracted in order to
  500. * prevent it for running over again for each segment having the same interval.
  501. * #662, #697.
  502. */
  503. function normalizeTimeTickInterval(tickInterval, unitsOption) {
  504. var units = unitsOption || [[
  505. MILLISECOND, // unit name
  506. [1, 2, 5, 10, 20, 25, 50, 100, 200, 500] // allowed multiples
  507. ], [
  508. SECOND,
  509. [1, 2, 5, 10, 15, 30]
  510. ], [
  511. MINUTE,
  512. [1, 2, 5, 10, 15, 30]
  513. ], [
  514. HOUR,
  515. [1, 2, 3, 4, 6, 8, 12]
  516. ], [
  517. DAY,
  518. [1, 2]
  519. ], [
  520. WEEK,
  521. [1, 2]
  522. ], [
  523. MONTH,
  524. [1, 2, 3, 4, 6]
  525. ], [
  526. YEAR,
  527. null
  528. ]],
  529. unit = units[units.length - 1], // default unit is years
  530. interval = timeUnits[unit[0]],
  531. multiples = unit[1],
  532. count,
  533. i;
  534.  
  535. // loop through the units to find the one that best fits the tickInterval
  536. for (i = 0; i < units.length; i++) {
  537. unit = units[i];
  538. interval = timeUnits[unit[0]];
  539. multiples = unit[1];
  540.  
  541.  
  542. if (units[i + 1]) {
  543. // lessThan is in the middle between the highest multiple and the next unit.
  544. var lessThan = (interval * multiples[multiples.length - 1] +
  545. timeUnits[units[i + 1][0]]) / 2;
  546.  
  547. // break and keep the current unit
  548. if (tickInterval <= lessThan) {
  549. break;
  550. }
  551. }
  552. }
  553.  
  554. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  555. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  556. multiples = [1, 2, 5];
  557. }
  558.  
  559. // prevent 2.5 years intervals, though 25, 250 etc. are allowed
  560. if (interval === timeUnits[YEAR] && tickInterval < 5 * interval) {
  561. multiples = [1, 2, 5];
  562. }
  563.  
  564. // get the count
  565. count = normalizeTickInterval(tickInterval / interval, multiples);
  566.  
  567. return {
  568. unitRange: interval,
  569. count: count,
  570. unitName: unit[0]
  571. };
  572. }
  573.  
  574. /**
  575. * Set the tick positions to a time unit that makes sense, for example
  576. * on the first of each month or on every Monday. Return an array
  577. * with the time positions. Used in datetime axes as well as for grouping
  578. * data on a datetime axis.
  579. *
  580. * @param {Object} normalizedInterval The interval in axis values (ms) and the count
  581. * @param {Number} min The minimum in axis values
  582. * @param {Number} max The maximum in axis values
  583. * @param {Number} startOfWeek
  584. */
  585. function getTimeTicks(normalizedInterval, min, max, startOfWeek) {
  586. var tickPositions = [],
  587. i,
  588. higherRanks = {},
  589. useUTC = defaultOptions.global.useUTC,
  590. minYear, // used in months and years as a basis for Date.UTC()
  591. minDate = new Date(min),
  592. interval = normalizedInterval.unitRange,
  593. count = normalizedInterval.count;
  594.  
  595.  
  596.  
  597. if (interval >= timeUnits[SECOND]) { // second
  598. minDate.setMilliseconds(0);
  599. minDate.setSeconds(interval >= timeUnits[MINUTE] ? 0 :
  600. count * mathFloor(minDate.getSeconds() / count));
  601. }
  602.  
  603. if (interval >= timeUnits[MINUTE]) { // minute
  604. minDate[setMinutes](interval >= timeUnits[HOUR] ? 0 :
  605. count * mathFloor(minDate[getMinutes]() / count));
  606. }
  607.  
  608. if (interval >= timeUnits[HOUR]) { // hour
  609. minDate[setHours](interval >= timeUnits[DAY] ? 0 :
  610. count * mathFloor(minDate[getHours]() / count));
  611. }
  612.  
  613. if (interval >= timeUnits[DAY]) { // day
  614. minDate[setDate](interval >= timeUnits[MONTH] ? 1 :
  615. count * mathFloor(minDate[getDate]() / count));
  616. }
  617.  
  618. if (interval >= timeUnits[MONTH]) { // month
  619. minDate[setMonth](interval >= timeUnits[YEAR] ? 0 :
  620. count * mathFloor(minDate[getMonth]() / count));
  621. minYear = minDate[getFullYear]();
  622. }
  623.  
  624. if (interval >= timeUnits[YEAR]) { // year
  625. minYear -= minYear % count;
  626. minDate[setFullYear](minYear);
  627. }
  628.  
  629. // week is a special case that runs outside the hierarchy
  630. if (interval === timeUnits[WEEK]) {
  631. // get start of current week, independent of count
  632. minDate[setDate](minDate[getDate]() - minDate[getDay]() +
  633. pick(startOfWeek, 1));
  634. }
  635.  
  636.  
  637. // get tick positions
  638. i = 1;
  639. minYear = minDate[getFullYear]();
  640. var time = minDate.getTime(),
  641. minMonth = minDate[getMonth](),
  642. minDateDate = minDate[getDate](),
  643. timezoneOffset = useUTC ?
  644. 0 :
  645. (24 * 3600 * 1000 + minDate.getTimezoneOffset() * 60 * 1000) % (24 * 3600 * 1000); // #950
  646.  
  647. // iterate and add tick positions at appropriate values
  648. while (time < max) {
  649. tickPositions.push(time);
  650.  
  651. // if the interval is years, use Date.UTC to increase years
  652. if (interval === timeUnits[YEAR]) {
  653. time = makeTime(minYear + i * count, 0);
  654.  
  655. // if the interval is months, use Date.UTC to increase months
  656. } else if (interval === timeUnits[MONTH]) {
  657. time = makeTime(minYear, minMonth + i * count);
  658.  
  659. // if we're using global time, the interval is not fixed as it jumps
  660. // one hour at the DST crossover
  661. } else if (!useUTC && (interval === timeUnits[DAY] || interval === timeUnits[WEEK])) {
  662. time = makeTime(minYear, minMonth, minDateDate +
  663. i * count * (interval === timeUnits[DAY] ? 1 : 7));
  664.  
  665. // else, the interval is fixed and we use simple addition
  666. } else {
  667. time += interval * count;
  668.  
  669. // mark new days if the time is dividable by day
  670. if (interval <= timeUnits[HOUR] && time % timeUnits[DAY] === timezoneOffset) {
  671. higherRanks[time] = DAY;
  672. }
  673. }
  674.  
  675. i++;
  676. }
  677.  
  678. // push the last time
  679. tickPositions.push(time);
  680.  
  681. // record information on the chosen unit - for dynamic label formatter
  682. tickPositions.info = extend(normalizedInterval, {
  683. higherRanks: higherRanks,
  684. totalRange: interval * count
  685. });
  686.  
  687. return tickPositions;
  688. }
  689.  
  690. /**
  691. * Helper class that contains variuos counters that are local to the chart.
  692. */
  693. function ChartCounters() {
  694. this.color = 0;
  695. this.symbol = 0;
  696. }
  697.  
  698. ChartCounters.prototype = {
  699. /**
  700. * Wraps the color counter if it reaches the specified length.
  701. */
  702. wrapColor: function (length) {
  703. if (this.color >= length) {
  704. this.color = 0;
  705. }
  706. },
  707.  
  708. /**
  709. * Wraps the symbol counter if it reaches the specified length.
  710. */
  711. wrapSymbol: function (length) {
  712. if (this.symbol >= length) {
  713. this.symbol = 0;
  714. }
  715. }
  716. };
  717.  
  718.  
  719. /**
  720. * Utility method that sorts an object array and keeping the order of equal items.
  721. * ECMA script standard does not specify the behaviour when items are equal.
  722. */
  723. function stableSort(arr, sortFunction) {
  724. var length = arr.length,
  725. sortValue,
  726. i;
  727.  
  728. // Add index to each item
  729. for (i = 0; i < length; i++) {
  730. arr[i].ss_i = i; // stable sort index
  731. }
  732.  
  733. arr.sort(function (a, b) {
  734. sortValue = sortFunction(a, b);
  735. return sortValue === 0 ? a.ss_i - b.ss_i : sortValue;
  736. });
  737.  
  738. // Remove index from items
  739. for (i = 0; i < length; i++) {
  740. delete arr[i].ss_i; // stable sort index
  741. }
  742. }
  743.  
  744. /**
  745. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  746. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  747. * method is slightly slower, but safe.
  748. */
  749. function arrayMin(data) {
  750. var i = data.length,
  751. min = data[0];
  752.  
  753. while (i--) {
  754. if (data[i] < min) {
  755. min = data[i];
  756. }
  757. }
  758. return min;
  759. }
  760.  
  761. /**
  762. * Non-recursive method to find the lowest member of an array. Math.min raises a maximum
  763. * call stack size exceeded error in Chrome when trying to apply more than 150.000 points. This
  764. * method is slightly slower, but safe.
  765. */
  766. function arrayMax(data) {
  767. var i = data.length,
  768. max = data[0];
  769.  
  770. while (i--) {
  771. if (data[i] > max) {
  772. max = data[i];
  773. }
  774. }
  775. return max;
  776. }
  777.  
  778. /**
  779. * Utility method that destroys any SVGElement or VMLElement that are properties on the given object.
  780. * It loops all properties and invokes destroy if there is a destroy method. The property is
  781. * then delete'ed.
  782. * @param {Object} The object to destroy properties on
  783. * @param {Object} Exception, do not destroy this property, only delete it.
  784. */
  785. function destroyObjectProperties(obj, except) {
  786. var n;
  787. for (n in obj) {
  788. // If the object is non-null and destroy is defined
  789. if (obj[n] && obj[n] !== except && obj[n].destroy) {
  790. // Invoke the destroy
  791. obj[n].destroy();
  792. }
  793.  
  794. // Delete the property from the object.
  795. delete obj[n];
  796. }
  797. }
  798.  
  799.  
  800. /**
  801. * Discard an element by moving it to the bin and delete
  802. * @param {Object} The HTML node to discard
  803. */
  804. function discardElement(element) {
  805. // create a garbage bin element, not part of the DOM
  806. if (!garbageBin) {
  807. garbageBin = createElement(DIV);
  808. }
  809.  
  810. // move the node and empty bin
  811. if (element) {
  812. garbageBin.appendChild(element);
  813. }
  814. garbageBin.innerHTML = '';
  815. }
  816.  
  817. /**
  818. * Provide error messages for debugging, with links to online explanation
  819. */
  820. function error(code, stop) {
  821. var msg = 'Highcharts error #' + code + ': www.highcharts.com/errors/' + code;
  822. if (stop) {
  823. throw msg;
  824. } else if (win.console) {
  825. console.log(msg);
  826. }
  827. }
  828.  
  829. /**
  830. * Fix JS round off float errors
  831. * @param {Number} num
  832. */
  833. function correctFloat(num) {
  834. return parseFloat(
  835. num.toPrecision(14)
  836. );
  837. }
  838.  
  839. /**
  840. * The time unit lookup
  841. */
  842. /*jslint white: true*/
  843. timeUnits = hash(
  844. MILLISECOND, 1,
  845. SECOND, 1000,
  846. MINUTE, 60000,
  847. HOUR, 3600000,
  848. DAY, 24 * 3600000,
  849. WEEK, 7 * 24 * 3600000,
  850. MONTH, 30 * 24 * 3600000,
  851. YEAR, 31556952000
  852. );
  853. /*jslint white: false*/
  854. /**
  855. * Path interpolation algorithm used across adapters
  856. */
  857. pathAnim = {
  858. /**
  859. * Prepare start and end values so that the path can be animated one to one
  860. */
  861. init: function (elem, fromD, toD) {
  862. fromD = fromD || '';
  863. var shift = elem.shift,
  864. bezier = fromD.indexOf('C') > -1,
  865. numParams = bezier ? 7 : 3,
  866. endLength,
  867. slice,
  868. i,
  869. start = fromD.split(' '),
  870. end = [].concat(toD), // copy
  871. startBaseLine,
  872. endBaseLine,
  873. sixify = function (arr) { // in splines make move points have six parameters like bezier curves
  874. i = arr.length;
  875. while (i--) {
  876. if (arr[i] === M) {
  877. arr.splice(i + 1, 0, arr[i + 1], arr[i + 2], arr[i + 1], arr[i + 2]);
  878. }
  879. }
  880. };
  881.  
  882. if (bezier) {
  883. sixify(start);
  884. sixify(end);
  885. }
  886.  
  887. // pull out the base lines before padding
  888. if (elem.isArea) {
  889. startBaseLine = start.splice(start.length - 6, 6);
  890. endBaseLine = end.splice(end.length - 6, 6);
  891. }
  892.  
  893. // if shifting points, prepend a dummy point to the end path
  894. if (shift <= end.length / numParams) {
  895. while (shift--) {
  896. end = [].concat(end).splice(0, numParams).concat(end);
  897. }
  898. }
  899. elem.shift = 0; // reset for following animations
  900.  
  901. // copy and append last point until the length matches the end length
  902. if (start.length) {
  903. endLength = end.length;
  904. while (start.length < endLength) {
  905.  
  906. //bezier && sixify(start);
  907. slice = [].concat(start).splice(start.length - numParams, numParams);
  908. if (bezier) { // disable first control point
  909. slice[numParams - 6] = slice[numParams - 2];
  910. slice[numParams - 5] = slice[numParams - 1];
  911. }
  912. start = start.concat(slice);
  913. }
  914. }
  915.  
  916. if (startBaseLine) { // append the base lines for areas
  917. start = start.concat(startBaseLine);
  918. end = end.concat(endBaseLine);
  919. }
  920. return [start, end];
  921. },
  922.  
  923. /**
  924. * Interpolate each value of the path and return the array
  925. */
  926. step: function (start, end, pos, complete) {
  927. var ret = [],
  928. i = start.length,
  929. startVal;
  930.  
  931. if (pos === 1) { // land on the final path without adjustment points appended in the ends
  932. ret = complete;
  933.  
  934. } else if (i === end.length && pos < 1) {
  935. while (i--) {
  936. startVal = parseFloat(start[i]);
  937. ret[i] =
  938. isNaN(startVal) ? // a letter instruction like M or L
  939. start[i] :
  940. pos * (parseFloat(end[i] - startVal)) + startVal;
  941.  
  942. }
  943. } else { // if animation is finished or length not matching, land on right value
  944. ret = end;
  945. }
  946. return ret;
  947. }
  948. };
  949.  
  950.  
  951. /**
  952. * Set the global animation to either a given value, or fall back to the
  953. * given chart's animation option
  954. * @param {Object} animation
  955. * @param {Object} chart
  956. */
  957. function setAnimation(animation, chart) {
  958. globalAnimation = pick(animation, chart.animation);
  959. }
  960.  
  961.  
  962.  
  963. // check for a custom HighchartsAdapter defined prior to this file
  964. var globalAdapter = win.HighchartsAdapter,
  965. adapter = globalAdapter || {},
  966.  
  967. // Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
  968. // and all the utility functions will be null. In that case they are populated by the
  969. // default adapters below.
  970. adapterRun = adapter.adapterRun,
  971. getScript = adapter.getScript,
  972. each = adapter.each,
  973. grep = adapter.grep,
  974. offset = adapter.offset,
  975. map = adapter.map,
  976. merge = adapter.merge,
  977. addEvent = adapter.addEvent,
  978. removeEvent = adapter.removeEvent,
  979. fireEvent = adapter.fireEvent,
  980. washMouseEvent = adapter.washMouseEvent,
  981. animate = adapter.animate,
  982. stop = adapter.stop;
  983.  
  984. /*
  985. * Define the adapter for frameworks. If an external adapter is not defined,
  986. * Highcharts reverts to the built-in jQuery adapter.
  987. */
  988. if (globalAdapter && globalAdapter.init) {
  989. // Initialize the adapter with the pathAnim object that takes care
  990. // of path animations.
  991. globalAdapter.init(pathAnim);
  992. }
  993. if (!globalAdapter && win.jQuery) {
  994. var jQ = jQuery;
  995.  
  996. /**
  997. * Downloads a script and executes a callback when done.
  998. * @param {String} scriptLocation
  999. * @param {Function} callback
  1000. */
  1001. getScript = jQ.getScript;
  1002.  
  1003. /**
  1004. * A direct link to jQuery methods. MooTools and Prototype adapters must be implemented for each case of method.
  1005. * @param {Object} elem The HTML element
  1006. * @param {String} method Which method to run on the wrapped element
  1007. */
  1008. adapterRun = function (elem, method) {
  1009. return jQ(elem)[method]();
  1010. };
  1011.  
  1012. /**
  1013. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  1014. * @param {Array} arr
  1015. * @param {Function} fn
  1016. */
  1017. each = function (arr, fn) {
  1018. var i = 0,
  1019. len = arr.length;
  1020. for (; i < len; i++) {
  1021. if (fn.call(arr[i], arr[i], i, arr) === false) {
  1022. return i;
  1023. }
  1024. }
  1025. };
  1026.  
  1027. /**
  1028. * Filter an array
  1029. */
  1030. grep = jQ.grep;
  1031.  
  1032. /**
  1033. * Map an array
  1034. * @param {Array} arr
  1035. * @param {Function} fn
  1036. */
  1037. map = function (arr, fn) {
  1038. //return jQuery.map(arr, fn);
  1039. var results = [],
  1040. i = 0,
  1041. len = arr.length;
  1042. for (; i < len; i++) {
  1043. results[i] = fn.call(arr[i], arr[i], i, arr);
  1044. }
  1045. return results;
  1046.  
  1047. };
  1048.  
  1049. /**
  1050. * Deep merge two objects and return a third object
  1051. */
  1052. merge = function () {
  1053. var args = arguments;
  1054. return jQ.extend(true, null, args[0], args[1], args[2], args[3]);
  1055. };
  1056.  
  1057. /**
  1058. * Get the position of an element relative to the top left of the page
  1059. */
  1060. offset = function (el) {
  1061. return jQ(el).offset();
  1062. };
  1063.  
  1064. /**
  1065. * Add an event listener
  1066. * @param {Object} el A HTML element or custom object
  1067. * @param {String} event The event type
  1068. * @param {Function} fn The event handler
  1069. */
  1070. addEvent = function (el, event, fn) {
  1071. jQ(el).bind(event, fn);
  1072. };
  1073.  
  1074. /**
  1075. * Remove event added with addEvent
  1076. * @param {Object} el The object
  1077. * @param {String} eventType The event type. Leave blank to remove all events.
  1078. * @param {Function} handler The function to remove
  1079. */
  1080. removeEvent = function (el, eventType, handler) {
  1081. // workaround for jQuery issue with unbinding custom events:
  1082. // http://forum.jquery.com/topic/javascript-error-when-unbinding-a-custom-event-using-jquery-1-4-2
  1083. var func = doc.removeEventListener ? 'removeEventListener' : 'detachEvent';
  1084. if (doc[func] && !el[func]) {
  1085. el[func] = function () {};
  1086. }
  1087.  
  1088. jQ(el).unbind(eventType, handler);
  1089. };
  1090.  
  1091. /**
  1092. * Fire an event on a custom object
  1093. * @param {Object} el
  1094. * @param {String} type
  1095. * @param {Object} eventArguments
  1096. * @param {Function} defaultFunction
  1097. */
  1098. fireEvent = function (el, type, eventArguments, defaultFunction) {
  1099. var event = jQ.Event(type),
  1100. detachedType = 'detached' + type,
  1101. defaultPrevented;
  1102.  
  1103. // Remove warnings in Chrome when accessing layerX and layerY. Although Highcharts
  1104. // never uses these properties, Chrome includes them in the default click event and
  1105. // raises the warning when they are copied over in the extend statement below.
  1106. //
  1107. // To avoid problems in IE (see #1010) where we cannot delete the properties and avoid
  1108. // testing if they are there (warning in chrome) the only option is to test if running IE.
  1109. if (!isIE && eventArguments) {
  1110. delete eventArguments.layerX;
  1111. delete eventArguments.layerY;
  1112. }
  1113.  
  1114. extend(event, eventArguments);
  1115.  
  1116. // Prevent jQuery from triggering the object method that is named the
  1117. // same as the event. For example, if the event is 'select', jQuery
  1118. // attempts calling el.select and it goes into a loop.
  1119. if (el[type]) {
  1120. el[detachedType] = el[type];
  1121. el[type] = null;
  1122. }
  1123.  
  1124. // Wrap preventDefault and stopPropagation in try/catch blocks in
  1125. // order to prevent JS errors when cancelling events on non-DOM
  1126. // objects. #615.
  1127. each(['preventDefault', 'stopPropagation'], function (fn) {
  1128. var base = event[fn];
  1129. event[fn] = function () {
  1130. try {
  1131. base.call(event);
  1132. } catch (e) {
  1133. if (fn === 'preventDefault') {
  1134. defaultPrevented = true;
  1135. }
  1136. }
  1137. };
  1138. });
  1139.  
  1140. // trigger it
  1141. jQ(el).trigger(event);
  1142.  
  1143. // attach the method
  1144. if (el[detachedType]) {
  1145. el[type] = el[detachedType];
  1146. el[detachedType] = null;
  1147. }
  1148.  
  1149. if (defaultFunction && !event.isDefaultPrevented() && !defaultPrevented) {
  1150. defaultFunction(event);
  1151. }
  1152. };
  1153.  
  1154. /**
  1155. * Extension method needed for MooTools
  1156. */
  1157. washMouseEvent = function (e) {
  1158. return e;
  1159. };
  1160.  
  1161. /**
  1162. * Animate a HTML element or SVG element wrapper
  1163. * @param {Object} el
  1164. * @param {Object} params
  1165. * @param {Object} options jQuery-like animation options: duration, easing, callback
  1166. */
  1167. animate = function (el, params, options) {
  1168. var $el = jQ(el);
  1169. if (params.d) {
  1170. el.toD = params.d; // keep the array form for paths, used in jQ.fx.step.d
  1171. params.d = 1; // because in jQuery, animating to an array has a different meaning
  1172. }
  1173.  
  1174. $el.stop();
  1175. $el.animate(params, options);
  1176.  
  1177. };
  1178. /**
  1179. * Stop running animation
  1180. */
  1181. stop = function (el) {
  1182. jQ(el).stop();
  1183. };
  1184.  
  1185.  
  1186. //=== Extend jQuery on init
  1187.  
  1188. /*jslint unparam: true*//* allow unused param x in this function */
  1189. jQ.extend(jQ.easing, {
  1190. easeOutQuad: function (x, t, b, c, d) {
  1191. return -c * (t /= d) * (t - 2) + b;
  1192. }
  1193. });
  1194. /*jslint unparam: false*/
  1195.  
  1196. // extend the animate function to allow SVG animations
  1197. var jFx = jQ.fx,
  1198. jStep = jFx.step;
  1199.  
  1200. // extend some methods to check for elem.attr, which means it is a Highcharts SVG object
  1201. each(['cur', '_default', 'width', 'height'], function (fn, i) {
  1202. var obj = jStep,
  1203. base,
  1204. elem;
  1205.  
  1206. // Handle different parent objects
  1207. if (fn === 'cur') {
  1208. obj = jFx.prototype; // 'cur', the getter, relates to jFx.prototype
  1209.  
  1210. } else if (fn === '_default' && jQ.Tween) { // jQuery 1.8 model
  1211. obj = jQ.Tween.propHooks[fn];
  1212. fn = 'set';
  1213. }
  1214.  
  1215. // Overwrite the method
  1216. base = obj[fn];
  1217. if (base) { // step.width and step.height don't exist in jQuery < 1.7
  1218.  
  1219. // create the extended function replacement
  1220. obj[fn] = function (fx) {
  1221.  
  1222. // jFx.prototype.cur does not use fx argument
  1223. fx = i ? fx : this;
  1224.  
  1225. // shortcut
  1226. elem = fx.elem;
  1227.  
  1228. // jFX.prototype.cur returns the current value. The other ones are setters
  1229. // and returning a value has no effect.
  1230. return elem.attr ? // is SVG element wrapper
  1231. elem.attr(fx.prop, fn === 'cur' ? UNDEFINED : fx.now) : // apply the SVG wrapper's method
  1232. base.apply(this, arguments); // use jQuery's built-in method
  1233. };
  1234. }
  1235. });
  1236.  
  1237. // animate paths
  1238. jStep.d = function (fx) {
  1239. var elem = fx.elem;
  1240.  
  1241.  
  1242. // Normally start and end should be set in state == 0, but sometimes,
  1243. // for reasons unknown, this doesn't happen. Perhaps state == 0 is skipped
  1244. // in these cases
  1245. if (!fx.started) {
  1246. var ends = pathAnim.init(elem, elem.d, elem.toD);
  1247. fx.start = ends[0];
  1248. fx.end = ends[1];
  1249. fx.started = true;
  1250. }
  1251.  
  1252.  
  1253. // interpolate each value of the path
  1254. elem.attr('d', pathAnim.step(fx.start, fx.end, fx.pos, elem.toD));
  1255.  
  1256. };
  1257. }
  1258.  
  1259. /* ****************************************************************************
  1260. * Handle the options *
  1261. *****************************************************************************/
  1262. var
  1263.  
  1264. defaultLabelOptions = {
  1265. enabled: true,
  1266. // rotation: 0,
  1267. align: 'center',
  1268. x: 0,
  1269. y: 15,
  1270. /*formatter: function () {
  1271. return this.value;
  1272. },*/
  1273. style: {
  1274. color: '#666',
  1275. fontSize: '11px',
  1276. lineHeight: '14px'
  1277. }
  1278. };
  1279.  
  1280. defaultOptions = {
  1281. colors: [ '#4bacc6', '#4572A7', '#AA4643', '#89A54E', '#8064a2'],
  1282. symbols: [ 'square'],
  1283. lang: {
  1284. loading: 'Loading...',
  1285. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  1286. 'August', 'September', 'October', 'November', 'December'],
  1287. shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  1288. weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  1289. decimalPoint: '.',
  1290. resetZoom: 'Reset zoom',
  1291. resetZoomTitle: 'Reset zoom level 1:1',
  1292. thousandsSep: ','
  1293. },
  1294. global: {
  1295. useUTC: true,
  1296. canvasToolsURL: 'http://code.highcharts.com/2.2.5/modules/canvas-tools.js'
  1297. },
  1298. chart: {
  1299. //animation: true,
  1300. //alignTicks: false,
  1301. //reflow: true,
  1302. //className: null,
  1303. //events: { load, selection },
  1304. //margin: [null],
  1305. //marginTop: null,
  1306. //marginRight: null,
  1307. //marginBottom: null,
  1308. //marginLeft: null,
  1309. borderColor: '#4572A7',
  1310. //borderWidth: 0,
  1311. borderRadius: 5,
  1312. defaultSeriesType: 'line',
  1313. ignoreHiddenSeries: true,
  1314. //inverted: false,
  1315. //shadow: false,
  1316. spacingTop: 10,
  1317. spacingRight: 10,
  1318. spacingBottom: 15,
  1319. spacingLeft: 10,
  1320. style: {
  1321. fontFamily: '"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif', // default font
  1322. fontSize: '15px'
  1323. },
  1324. backgroundColor: '#FFFFFF',
  1325. //plotBackgroundColor: null,
  1326. plotBorderColor: '#C0C0C0',
  1327. //plotBorderWidth: 0,
  1328. //plotShadow: false,
  1329. //zoomType: ''
  1330. resetZoomButton: {
  1331. theme: {
  1332. zIndex: 20
  1333. },
  1334. position: {
  1335. align: 'right',
  1336. x: -10,
  1337. //verticalAlign: 'top',
  1338. y: 10
  1339. }
  1340. // relativeTo: 'plot'
  1341. }
  1342. },
  1343. title: {
  1344. text: '',
  1345. align: 'center',
  1346. // floating: false,
  1347. // margin: 15,
  1348. // x: 0,
  1349. // verticalAlign: 'top',
  1350. y: 15,
  1351. style: {
  1352. color: '#3E576F',
  1353. fontSize: '16px'
  1354. }
  1355.  
  1356. },
  1357. subtitle: {
  1358. text: '',
  1359. align: 'center',
  1360. // floating: false
  1361. // x: 0,
  1362. // verticalAlign: 'top',
  1363. y: 30,
  1364. style: {
  1365. color: '#6D869F'
  1366. }
  1367. },
  1368.  
  1369. plotOptions: {
  1370. line: { // base series options
  1371. allowPointSelect: false,
  1372. showCheckbox: false,
  1373. animation: {
  1374. duration: 1000
  1375. },
  1376. //connectNulls: false,
  1377. //cursor: 'default',
  1378. //clip: true,
  1379. //dashStyle: null,
  1380. //enableMouseTracking: true,
  1381. events: {},
  1382. //legendIndex: 0,
  1383. lineWidth: 2,
  1384. shadow: true,
  1385. // stacking: null,
  1386. marker: {
  1387. enabled: true,
  1388. //symbol: null,
  1389. lineWidth: 0,
  1390. radius: 4,
  1391. lineColor: '#FFFFFF',
  1392. //fillColor: null,
  1393. states: { // states for a single point
  1394. hover: {
  1395. //radius: base + 2
  1396. },
  1397. select: {
  1398. fillColor: '#FFFFFF',
  1399. lineColor: '#000000',
  1400. lineWidth: 2
  1401. }
  1402. }
  1403. },
  1404. point: {
  1405. events: {}
  1406. },
  1407. dataLabels: merge(defaultLabelOptions, {
  1408. enabled: false,
  1409. y: -6,
  1410. formatter: function () {
  1411. return this.y;
  1412. }
  1413. // backgroundColor: undefined,
  1414. // borderColor: undefined,
  1415. // borderRadius: undefined,
  1416. // borderWidth: undefined,
  1417. // padding: 3,
  1418. // shadow: false
  1419. }),
  1420. cropThreshold: 300, // draw points outside the plot area when the number of points is less than this
  1421. pointRange: 0,
  1422. //pointStart: 0,
  1423. //pointInterval: 1,
  1424. showInLegend: true,
  1425. states: { // states for the entire series
  1426. hover: {
  1427. //enabled: false,
  1428. //lineWidth: base + 1,
  1429. marker: {
  1430. // lineWidth: base + 1,
  1431. // radius: base + 1
  1432. }
  1433. },
  1434. select: {
  1435. marker: {}
  1436. }
  1437. },
  1438. stickyTracking: true
  1439. //tooltip: {
  1440. //pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b>'
  1441. //valueDecimals: null,
  1442. //xDateFormat: '%A, %b %e, %Y',
  1443. //valuePrefix: '',
  1444. //ySuffix: ''
  1445. //}
  1446. // turboThreshold: 1000
  1447. // zIndex: null
  1448. }
  1449. },
  1450. labels: {
  1451. //items: [],
  1452. style: {
  1453. //font: defaultFont,
  1454. position: ABSOLUTE,
  1455. color: '#3E576F'
  1456. }
  1457. },
  1458. legend: {
  1459. enabled: false,
  1460. align: 'center',
  1461. //floating: false,
  1462. layout: 'horizontal',
  1463. labelFormatter: function () {
  1464. return this.name;
  1465. },
  1466. borderWidth: 1,
  1467. borderColor: '#909090',
  1468. borderRadius: 5,
  1469. navigation: {
  1470. // animation: true,
  1471. activeColor: '#3E576F',
  1472. // arrowSize: 12
  1473. inactiveColor: '#CCC'
  1474. // style: {} // text styles
  1475. },
  1476. // margin: 10,
  1477. // reversed: false,
  1478. shadow: false,
  1479. // backgroundColor: null,
  1480. /*style: {
  1481. padding: '5px'
  1482. },*/
  1483. itemStyle: {
  1484. cursor: 'pointer',
  1485. color: '#3E576F',
  1486. fontSize: '12px'
  1487. },
  1488. itemHoverStyle: {
  1489. //cursor: 'pointer', removed as of #601
  1490. color: '#000'
  1491. },
  1492. itemHiddenStyle: {
  1493. color: '#CCC'
  1494. },
  1495. itemCheckboxStyle: {
  1496. position: ABSOLUTE,
  1497. width: '13px', // for IE precision
  1498. height: '13px'
  1499. },
  1500. // itemWidth: undefined,
  1501. symbolWidth: 16,
  1502. symbolPadding: 5,
  1503. verticalAlign: 'bottom',
  1504. // width: undefined,
  1505. x: 0,
  1506. y: 0
  1507. },
  1508.  
  1509. loading: {
  1510. // hideDuration: 100,
  1511. labelStyle: {
  1512. fontWeight: 'bold',
  1513. position: RELATIVE,
  1514. top: '1em'
  1515. },
  1516. // showDuration: 0,
  1517. style: {
  1518. position: ABSOLUTE,
  1519. backgroundColor: 'white',
  1520. opacity: 0.5,
  1521. textAlign: 'center'
  1522. }
  1523. },
  1524.  
  1525. tooltip: {
  1526. enabled: true,
  1527. //crosshairs: null,
  1528. backgroundColor: 'rgba(255, 255, 255, .85)',
  1529. borderWidth: 2,
  1530. borderRadius: 5,
  1531. dateTimeLabelFormats: {
  1532. millisecond: '%A, %b %e, %H:%M:%S.%L',
  1533. second: '%A, %b %e, %H:%M:%S',
  1534. minute: '%A, %b %e, %H:%M',
  1535. hour: '%A, %b %e, %H:%M',
  1536. day: '%A, %b %e, %Y',
  1537. week: 'Week from %A, %b %e, %Y',
  1538. month: '%B %Y',
  1539. year: '%Y'
  1540. },
  1541. //formatter: defaultFormatter,
  1542. headerFormat: '',
  1543. pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
  1544. shadow: true,
  1545. shared: useCanVG,
  1546. snap: hasTouch ? 25 : 10,
  1547. style: {
  1548. color: '#333333',
  1549. fontSize: '12px',
  1550. padding: '5px',
  1551. whiteSpace: 'nowrap'
  1552. }
  1553. //xDateFormat: '%A, %b %e, %Y',
  1554. //valueDecimals: null,
  1555. //valuePrefix: '',
  1556. //valueSuffix: ''
  1557. },
  1558.  
  1559. credits: {
  1560. enabled: false,
  1561. text: 'Highcharts.com',
  1562. href: 'http://www.highcharts.com',
  1563. position: {
  1564. align: 'right',
  1565. x: -10,
  1566. verticalAlign: 'bottom',
  1567. y: -5
  1568. },
  1569. style: {
  1570. cursor: 'pointer',
  1571. color: '#909090',
  1572. fontSize: '10px'
  1573. }
  1574. }
  1575. };
  1576.  
  1577.  
  1578.  
  1579.  
  1580. // Series defaults
  1581. var defaultPlotOptions = defaultOptions.plotOptions,
  1582. defaultSeriesOptions = defaultPlotOptions.line;
  1583.  
  1584. // set the default time methods
  1585. setTimeMethods();
  1586.  
  1587.  
  1588.  
  1589. /**
  1590. * Set the time methods globally based on the useUTC option. Time method can be either
  1591. * local time or UTC (default).
  1592. */
  1593. function setTimeMethods() {
  1594. var useUTC = defaultOptions.global.useUTC,
  1595. GET = useUTC ? 'getUTC' : 'get',
  1596. SET = useUTC ? 'setUTC' : 'set';
  1597.  
  1598. makeTime = useUTC ? Date.UTC : function (year, month, date, hours, minutes, seconds) {
  1599. return new Date(
  1600. year,
  1601. month,
  1602. pick(date, 1),
  1603. pick(hours, 0),
  1604. pick(minutes, 0),
  1605. pick(seconds, 0)
  1606. ).getTime();
  1607. };
  1608. getMinutes = GET + 'Minutes';
  1609. getHours = GET + 'Hours';
  1610. getDay = GET + 'Day';
  1611. getDate = GET + 'Date';
  1612. getMonth = GET + 'Month';
  1613. getFullYear = GET + 'FullYear';
  1614. setMinutes = SET + 'Minutes';
  1615. setHours = SET + 'Hours';
  1616. setDate = SET + 'Date';
  1617. setMonth = SET + 'Month';
  1618. setFullYear = SET + 'FullYear';
  1619.  
  1620. }
  1621.  
  1622. /**
  1623. * Merge the default options with custom options and return the new options structure
  1624. * @param {Object} options The new custom options
  1625. */
  1626. function setOptions(options) {
  1627.  
  1628. // Pull out axis options and apply them to the respective default axis options
  1629. /*defaultXAxisOptions = merge(defaultXAxisOptions, options.xAxis);
  1630. defaultYAxisOptions = merge(defaultYAxisOptions, options.yAxis);
  1631. options.xAxis = options.yAxis = UNDEFINED;*/
  1632.  
  1633. // Merge in the default options
  1634. defaultOptions = merge(defaultOptions, options);
  1635.  
  1636. // Apply UTC
  1637. setTimeMethods();
  1638.  
  1639. return defaultOptions;
  1640. }
  1641.  
  1642. /**
  1643. * Get the updated default options. Merely exposing defaultOptions for outside modules
  1644. * isn't enough because the setOptions method creates a new object.
  1645. */
  1646. function getOptions() {
  1647. return defaultOptions;
  1648. }
  1649.  
  1650.  
  1651.  
  1652. /**
  1653. * Handle color operations. The object methods are chainable.
  1654. * @param {String} input The input color in either rbga or hex format
  1655. */
  1656. var Color = function (input) {
  1657. // declare variables
  1658. var rgba = [], result;
  1659.  
  1660. /**
  1661. * Parse the input color to rgba array
  1662. * @param {String} input
  1663. */
  1664. function init(input) {
  1665.  
  1666. // rgba
  1667. result = /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*\)/.exec(input);
  1668. if (result) {
  1669. rgba = [pInt(result[1]), pInt(result[2]), pInt(result[3]), parseFloat(result[4], 10)];
  1670. } else { // hex
  1671. result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(input);
  1672. if (result) {
  1673. rgba = [pInt(result[1], 16), pInt(result[2], 16), pInt(result[3], 16), 1];
  1674. }
  1675. }
  1676.  
  1677. }
  1678. /**
  1679. * Return the color a specified format
  1680. * @param {String} format
  1681. */
  1682. function get(format) {
  1683. var ret;
  1684.  
  1685. // it's NaN if gradient colors on a column chart
  1686. if (rgba && !isNaN(rgba[0])) {
  1687. if (format === 'rgb') {
  1688. ret = 'rgb(' + rgba[0] + ',' + rgba[1] + ',' + rgba[2] + ')';
  1689. } else if (format === 'a') {
  1690. ret = rgba[3];
  1691. } else {
  1692. ret = 'rgba(' + rgba.join(',') + ')';
  1693. }
  1694. } else {
  1695. ret = input;
  1696. }
  1697. return ret;
  1698. }
  1699.  
  1700. /**
  1701. * Brighten the color
  1702. * @param {Number} alpha
  1703. */
  1704. function brighten(alpha) {
  1705. if (isNumber(alpha) && alpha !== 0) {
  1706. var i;
  1707. for (i = 0; i < 3; i++) {
  1708. rgba[i] += pInt(alpha * 255);
  1709.  
  1710. if (rgba[i] < 0) {
  1711. rgba[i] = 0;
  1712. }
  1713. if (rgba[i] > 255) {
  1714. rgba[i] = 255;
  1715. }
  1716. }
  1717. }
  1718. return this;
  1719. }
  1720. /**
  1721. * Set the color's opacity to a given alpha value
  1722. * @param {Number} alpha
  1723. */
  1724. function setOpacity(alpha) {
  1725. rgba[3] = alpha;
  1726. return this;
  1727. }
  1728.  
  1729. // initialize: parse the input
  1730. init(input);
  1731.  
  1732. // public methods
  1733. return {
  1734. get: get,
  1735. brighten: brighten,
  1736. setOpacity: setOpacity
  1737. };
  1738. };
  1739.  
  1740.  
  1741. /**
  1742. * A wrapper object for SVG elements
  1743. */
  1744. function SVGElement() {}
  1745.  
  1746. SVGElement.prototype = {
  1747. /**
  1748. * Initialize the SVG renderer
  1749. * @param {Object} renderer
  1750. * @param {String} nodeName
  1751. */
  1752. init: function (renderer, nodeName) {
  1753. var wrapper = this;
  1754. wrapper.element = nodeName === 'span' ?
  1755. createElement(nodeName) :
  1756. doc.createElementNS(SVG_NS, nodeName);
  1757. wrapper.renderer = renderer;
  1758. /**
  1759. * A collection of attribute setters. These methods, if defined, are called right before a certain
  1760. * attribute is set on an element wrapper. Returning false prevents the default attribute
  1761. * setter to run. Returning a value causes the default setter to set that value. Used in
  1762. * Renderer.label.
  1763. */
  1764. wrapper.attrSetters = {};
  1765. },
  1766. /**
  1767. * Animate a given attribute
  1768. * @param {Object} params
  1769. * @param {Number} options The same options as in jQuery animation
  1770. * @param {Function} complete Function to perform at the end of animation
  1771. */
  1772. animate: function (params, options, complete) {
  1773. var animOptions = pick(options, globalAnimation, true);
  1774. stop(this); // stop regardless of animation actually running, or reverting to .attr (#607)
  1775. if (animOptions) {
  1776. animOptions = merge(animOptions);
  1777. if (complete) { // allows using a callback with the global animation without overwriting it
  1778. animOptions.complete = complete;
  1779. }
  1780. animate(this, params, animOptions);
  1781. } else {
  1782. this.attr(params);
  1783. if (complete) {
  1784. complete();
  1785. }
  1786. }
  1787. },
  1788. /**
  1789. * Set or get a given attribute
  1790. * @param {Object|String} hash
  1791. * @param {Mixed|Undefined} val
  1792. */
  1793. attr: function (hash, val) {
  1794. var wrapper = this,
  1795. key,
  1796. value,
  1797. result,
  1798. i,
  1799. child,
  1800. element = wrapper.element,
  1801. nodeName = element.nodeName,
  1802. renderer = wrapper.renderer,
  1803. skipAttr,
  1804. titleNode,
  1805. attrSetters = wrapper.attrSetters,
  1806. shadows = wrapper.shadows,
  1807. hasSetSymbolSize,
  1808. doTransform,
  1809. ret = wrapper;
  1810.  
  1811. // single key-value pair
  1812. if (isString(hash) && defined(val)) {
  1813. key = hash;
  1814. hash = {};
  1815. hash[key] = val;
  1816. }
  1817.  
  1818. // used as a getter: first argument is a string, second is undefined
  1819. if (isString(hash)) {
  1820. key = hash;
  1821. if (nodeName === 'circle') {
  1822. key = { x: 'cx', y: 'cy' }[key] || key;
  1823. } else if (key === 'strokeWidth') {
  1824. key = 'stroke-width';
  1825. }
  1826. ret = attr(element, key) || wrapper[key] || 0;
  1827.  
  1828. if (key !== 'd' && key !== 'visibility') { // 'd' is string in animation step
  1829. ret = parseFloat(ret);
  1830. }
  1831.  
  1832. // setter
  1833. } else {
  1834.  
  1835. for (key in hash) {
  1836. skipAttr = false; // reset
  1837. value = hash[key];
  1838.  
  1839. // check for a specific attribute setter
  1840. result = attrSetters[key] && attrSetters[key](value, key);
  1841.  
  1842. if (result !== false) {
  1843. if (result !== UNDEFINED) {
  1844. value = result; // the attribute setter has returned a new value to set
  1845. }
  1846.  
  1847. // paths
  1848. if (key === 'd') {
  1849. if (value && value.join) { // join path
  1850. value = value.join(' ');
  1851. }
  1852. if (/(NaN| {2}|^$)/.test(value)) {
  1853. value = 'M 0 0';
  1854. }
  1855. //wrapper.d = value; // shortcut for animations
  1856.  
  1857. // update child tspans x values
  1858. } else if (key === 'x' && nodeName === 'text') {
  1859. for (i = 0; i < element.childNodes.length; i++) {
  1860. child = element.childNodes[i];
  1861. // if the x values are equal, the tspan represents a linebreak
  1862. if (attr(child, 'x') === attr(element, 'x')) {
  1863. //child.setAttribute('x', value);
  1864. attr(child, 'x', value);
  1865. }
  1866. }
  1867.  
  1868. if (wrapper.rotation) {
  1869. attr(element, 'transform', 'rotate(' + wrapper.rotation + ' ' + value + ' ' +
  1870. pInt(hash.y || attr(element, 'y')) + ')');
  1871. }
  1872.  
  1873. // apply gradients
  1874. } else if (key === 'fill') {
  1875. value = renderer.color(value, element, key);
  1876.  
  1877. // circle x and y
  1878. } else if (nodeName === 'circle' && (key === 'x' || key === 'y')) {
  1879. key = { x: 'cx', y: 'cy' }[key] || key;
  1880.  
  1881. // rectangle border radius
  1882. } else if (nodeName === 'rect' && key === 'r') {
  1883. attr(element, {
  1884. rx: value,
  1885. ry: value
  1886. });
  1887. skipAttr = true;
  1888.  
  1889. // translation and text rotation
  1890. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation' || key === 'verticalAlign') {
  1891. doTransform = true;
  1892. skipAttr = true;
  1893.  
  1894. // apply opacity as subnode (required by legacy WebKit and Batik)
  1895. } else if (key === 'stroke') {
  1896. value = renderer.color(value, element, key);
  1897.  
  1898. // emulate VML's dashstyle implementation
  1899. } else if (key === 'dashstyle') {
  1900. key = 'stroke-dasharray';
  1901. value = value && value.toLowerCase();
  1902. if (value === 'solid') {
  1903. value = NONE;
  1904. } else if (value) {
  1905. value = value
  1906. .replace('shortdashdotdot', '3,1,1,1,1,1,')
  1907. .replace('shortdashdot', '3,1,1,1')
  1908. .replace('shortdot', '1,1,')
  1909. .replace('shortdash', '3,1,')
  1910. .replace('longdash', '8,3,')
  1911. .replace(/dot/g, '1,3,')
  1912. .replace('dash', '4,3,')
  1913. .replace(/,$/, '')
  1914. .split(','); // ending comma
  1915.  
  1916. i = value.length;
  1917. while (i--) {
  1918. value[i] = pInt(value[i]) * hash['stroke-width'];
  1919. }
  1920. value = value.join(',');
  1921. }
  1922.  
  1923. // special
  1924. } else if (key === 'isTracker') {
  1925. wrapper[key] = value;
  1926.  
  1927. // IE9/MooTools combo: MooTools returns objects instead of numbers and IE9 Beta 2
  1928. // is unable to cast them. Test again with final IE9.
  1929. } else if (key === 'width') {
  1930. value = pInt(value);
  1931.  
  1932. // Text alignment
  1933. } else if (key === 'align') {
  1934. key = 'text-anchor';
  1935. value = { left: 'start', center: 'middle', right: 'end' }[value];
  1936.  
  1937. // Title requires a subnode, #431
  1938. } else if (key === 'title') {
  1939. titleNode = element.getElementsByTagName('title')[0];
  1940. if (!titleNode) {
  1941. titleNode = doc.createElementNS(SVG_NS, 'title');
  1942. element.appendChild(titleNode);
  1943. }
  1944. titleNode.textContent = value;
  1945. }
  1946.  
  1947. // jQuery animate changes case
  1948. if (key === 'strokeWidth') {
  1949. key = 'stroke-width';
  1950. }
  1951.  
  1952. // Chrome/Win < 6 bug (http://code.google.com/p/chromium/issues/detail?id=15461)
  1953. if (isWebKit && key === 'stroke-width' && value === 0) {
  1954. value = 0.000001;
  1955. }
  1956.  
  1957. // symbols
  1958. if (wrapper.symbolName && /^(x|y|width|height|r|start|end|innerR|anchorX|anchorY)/.test(key)) {
  1959.  
  1960.  
  1961. if (!hasSetSymbolSize) {
  1962. wrapper.symbolAttr(hash);
  1963. hasSetSymbolSize = true;
  1964. }
  1965. skipAttr = true;
  1966. }
  1967.  
  1968. // let the shadow follow the main element
  1969. if (shadows && /^(width|height|visibility|x|y|d|transform)$/.test(key)) {
  1970. i = shadows.length;
  1971. while (i--) {
  1972. attr(
  1973. shadows[i],
  1974. key,
  1975. key === 'height' ?
  1976. mathMax(value - (shadows[i].cutHeight || 0), 0) :
  1977. value
  1978. );
  1979. }
  1980. }
  1981.  
  1982. // validate heights
  1983. if ((key === 'width' || key === 'height') && nodeName === 'rect' && value < 0) {
  1984. value = 0;
  1985. }
  1986.  
  1987. // Record for animation and quick access without polling the DOM
  1988. wrapper[key] = value;
  1989.  
  1990. // Update transform
  1991. if (doTransform) {
  1992. wrapper.updateTransform();
  1993. }
  1994.  
  1995.  
  1996. if (key === 'text') {
  1997. // only one node allowed
  1998. wrapper.textStr = value;
  1999. if (wrapper.added) {
  2000. renderer.buildText(wrapper);
  2001. }
  2002. } else if (!skipAttr) {
  2003. attr(element, key, value);
  2004. }
  2005.  
  2006. }
  2007.  
  2008. }
  2009.  
  2010. }
  2011.  
  2012. // Workaround for our #732, WebKit's issue https://bugs.webkit.org/show_bug.cgi?id=78385
  2013. // TODO: If the WebKit team fix this bug before the final release of Chrome 18, remove the workaround.
  2014. if (isWebKit && /Chrome\/(18|19)/.test(userAgent)) {
  2015. if (nodeName === 'text' && (hash.x !== UNDEFINED || hash.y !== UNDEFINED)) {
  2016. var parent = element.parentNode,
  2017. next = element.nextSibling;
  2018.  
  2019. if (parent) {
  2020. parent.removeChild(element);
  2021. if (next) {
  2022. parent.insertBefore(element, next);
  2023. } else {
  2024. parent.appendChild(element);
  2025. }
  2026. }
  2027. }
  2028. }
  2029. // End of workaround for #732
  2030.  
  2031. return ret;
  2032. },
  2033.  
  2034. /**
  2035. * If one of the symbol size affecting parameters are changed,
  2036. * check all the others only once for each call to an element's
  2037. * .attr() method
  2038. * @param {Object} hash
  2039. */
  2040. symbolAttr: function (hash) {
  2041. var wrapper = this;
  2042.  
  2043. each(['x', 'y', 'r', 'start', 'end', 'width', 'height', 'innerR', 'anchorX', 'anchorY'], function (key) {
  2044. wrapper[key] = pick(hash[key], wrapper[key]);
  2045. });
  2046.  
  2047. wrapper.attr({
  2048. d: wrapper.renderer.symbols[wrapper.symbolName](wrapper.x, wrapper.y, wrapper.width, wrapper.height, wrapper)
  2049. });
  2050. },
  2051.  
  2052. /**
  2053. * Apply a clipping path to this object
  2054. * @param {String} id
  2055. */
  2056. clip: function (clipRect) {
  2057. return this.attr('clip-path', 'url(' + this.renderer.url + '#' + clipRect.id + ')');
  2058. },
  2059.  
  2060. /**
  2061. * Calculate the coordinates needed for drawing a rectangle crisply and return the
  2062. * calculated attributes
  2063. * @param {Number} strokeWidth
  2064. * @param {Number} x
  2065. * @param {Number} y
  2066. * @param {Number} width
  2067. * @param {Number} height
  2068. */
  2069. crisp: function (strokeWidth, x, y, width, height) {
  2070.  
  2071. var wrapper = this,
  2072. key,
  2073. attribs = {},
  2074. values = {},
  2075. normalizer;
  2076.  
  2077. strokeWidth = strokeWidth || wrapper.strokeWidth || (wrapper.attr && wrapper.attr('stroke-width')) || 0;
  2078. normalizer = mathRound(strokeWidth) % 2 / 2; // mathRound because strokeWidth can sometimes have roundoff errors
  2079.  
  2080. // normalize for crisp edges
  2081. values.x = mathFloor(x || wrapper.x || 0) + normalizer;
  2082. values.y = mathFloor(y || wrapper.y || 0) + normalizer;
  2083. values.width = mathFloor((width || wrapper.width || 0) - 2 * normalizer);
  2084. values.height = mathFloor((height || wrapper.height || 0) - 2 * normalizer);
  2085. values.strokeWidth = strokeWidth;
  2086.  
  2087. for (key in values) {
  2088. if (wrapper[key] !== values[key]) { // only set attribute if changed
  2089. wrapper[key] = attribs[key] = values[key];
  2090. }
  2091. }
  2092.  
  2093. return attribs;
  2094. },
  2095.  
  2096. /**
  2097. * Set styles for the element
  2098. * @param {Object} styles
  2099. */
  2100. css: function (styles) {
  2101. /*jslint unparam: true*//* allow unused param a in the regexp function below */
  2102. var elemWrapper = this,
  2103. elem = elemWrapper.element,
  2104. textWidth = styles && styles.width && elem.nodeName === 'text',
  2105. n,
  2106. serializedCss = '',
  2107. hyphenate = function (a, b) { return '-' + b.toLowerCase(); };
  2108. /*jslint unparam: false*/
  2109.  
  2110. // convert legacy
  2111. if (styles && styles.color) {
  2112. styles.fill = styles.color;
  2113. }
  2114.  
  2115. // Merge the new styles with the old ones
  2116. styles = extend(
  2117. elemWrapper.styles,
  2118. styles
  2119. );
  2120.  
  2121. // store object
  2122. elemWrapper.styles = styles;
  2123.  
  2124. // serialize and set style attribute
  2125. if (isIE && !hasSVG) { // legacy IE doesn't support setting style attribute
  2126. if (textWidth) {
  2127. delete styles.width;
  2128. }
  2129. css(elemWrapper.element, styles);
  2130. } else {
  2131. for (n in styles) {
  2132. serializedCss += n.replace(/([A-Z])/g, hyphenate) + ':' + styles[n] + ';';
  2133. }
  2134. elemWrapper.attr({
  2135. style: serializedCss
  2136. });
  2137. }
  2138.  
  2139.  
  2140. // re-build text
  2141. if (textWidth && elemWrapper.added) {
  2142. elemWrapper.renderer.buildText(elemWrapper);
  2143. }
  2144.  
  2145. return elemWrapper;
  2146. },
  2147.  
  2148. /**
  2149. * Add an event listener
  2150. * @param {String} eventType
  2151. * @param {Function} handler
  2152. */
  2153. on: function (eventType, handler) {
  2154. var fn = handler;
  2155. // touch
  2156. if (hasTouch && eventType === 'click') {
  2157. eventType = 'touchstart';
  2158. fn = function (e) {
  2159. e.preventDefault();
  2160. handler();
  2161. };
  2162. }
  2163. // simplest possible event model for internal use
  2164. this.element['on' + eventType] = fn;
  2165. return this;
  2166. },
  2167.  
  2168. /**
  2169. * Set the coordinates needed to draw a consistent radial gradient across
  2170. * pie slices regardless of positioning inside the chart. The format is
  2171. * [centerX, centerY, diameter] in pixels.
  2172. */
  2173. setRadialReference: function (coordinates) {
  2174. this.element.radialReference = coordinates;
  2175. return this;
  2176. },
  2177.  
  2178. /**
  2179. * Move an object and its children by x and y values
  2180. * @param {Number} x
  2181. * @param {Number} y
  2182. */
  2183. translate: function (x, y) {
  2184. return this.attr({
  2185. translateX: x,
  2186. translateY: y
  2187. });
  2188. },
  2189.  
  2190. /**
  2191. * Invert a group, rotate and flip
  2192. */
  2193. invert: function () {
  2194. var wrapper = this;
  2195. wrapper.inverted = true;
  2196. wrapper.updateTransform();
  2197. return wrapper;
  2198. },
  2199.  
  2200. /**
  2201. * Apply CSS to HTML elements. This is used in text within SVG rendering and
  2202. * by the VML renderer
  2203. */
  2204. htmlCss: function (styles) {
  2205. var wrapper = this,
  2206. element = wrapper.element,
  2207. textWidth = styles && element.tagName === 'SPAN' && styles.width;
  2208.  
  2209. if (textWidth) {
  2210. delete styles.width;
  2211. wrapper.textWidth = textWidth;
  2212. wrapper.updateTransform();
  2213. }
  2214.  
  2215. wrapper.styles = extend(wrapper.styles, styles);
  2216. css(wrapper.element, styles);
  2217.  
  2218. return wrapper;
  2219. },
  2220.  
  2221.  
  2222.  
  2223. /**
  2224. * VML and useHTML method for calculating the bounding box based on offsets
  2225. * @param {Boolean} refresh Whether to force a fresh value from the DOM or to
  2226. * use the cached value
  2227. *
  2228. * @return {Object} A hash containing values for x, y, width and height
  2229. */
  2230.  
  2231. htmlGetBBox: function (refresh) {
  2232. var wrapper = this,
  2233. element = wrapper.element,
  2234. bBox = wrapper.bBox;
  2235.  
  2236. // faking getBBox in exported SVG in legacy IE
  2237. if (!bBox || refresh) {
  2238. // faking getBBox in exported SVG in legacy IE
  2239. if (element.nodeName === 'text') {
  2240. element.style.position = ABSOLUTE;
  2241. }
  2242.  
  2243. bBox = wrapper.bBox = {
  2244. x: element.offsetLeft,
  2245. y: element.offsetTop,
  2246. width: element.offsetWidth,
  2247. height: element.offsetHeight
  2248. };
  2249. }
  2250.  
  2251. return bBox;
  2252. },
  2253.  
  2254. /**
  2255. * VML override private method to update elements based on internal
  2256. * properties based on SVG transform
  2257. */
  2258. htmlUpdateTransform: function () {
  2259. // aligning non added elements is expensive
  2260. if (!this.added) {
  2261. this.alignOnAdd = true;
  2262. return;
  2263. }
  2264.  
  2265. var wrapper = this,
  2266. renderer = wrapper.renderer,
  2267. elem = wrapper.element,
  2268. translateX = wrapper.translateX || 0,
  2269. translateY = wrapper.translateY || 0,
  2270. x = wrapper.x || 0,
  2271. y = wrapper.y || 0,
  2272. align = wrapper.textAlign || 'left',
  2273. alignCorrection = { left: 0, center: 0.5, right: 1 }[align],
  2274. nonLeft = align && align !== 'left',
  2275. shadows = wrapper.shadows;
  2276.  
  2277. // apply translate
  2278. if (translateX || translateY) {
  2279. css(elem, {
  2280. marginLeft: translateX,
  2281. marginTop: translateY
  2282. });
  2283. if (shadows) { // used in labels/tooltip
  2284. each(shadows, function (shadow) {
  2285. css(shadow, {
  2286. marginLeft: translateX + 1,
  2287. marginTop: translateY + 1
  2288. });
  2289. });
  2290. }
  2291. }
  2292.  
  2293. // apply inversion
  2294. if (wrapper.inverted) { // wrapper is a group
  2295. each(elem.childNodes, function (child) {
  2296. renderer.invertChild(child, elem);
  2297. });
  2298. }
  2299.  
  2300. if (elem.tagName === 'SPAN') {
  2301.  
  2302. var width, height,
  2303. rotation = wrapper.rotation,
  2304. baseline,
  2305. radians = 0,
  2306. costheta = 1,
  2307. sintheta = 0,
  2308. quad,
  2309. textWidth = pInt(wrapper.textWidth),
  2310. xCorr = wrapper.xCorr || 0,
  2311. yCorr = wrapper.yCorr || 0,
  2312. currentTextTransform = [rotation, align, elem.innerHTML, wrapper.textWidth].join(',');
  2313.  
  2314. if (currentTextTransform !== wrapper.cTT) { // do the calculations and DOM access only if properties changed
  2315.  
  2316. if (defined(rotation)) {
  2317. radians = rotation * deg2rad; // deg to rad
  2318. costheta = mathCos(radians);
  2319. sintheta = mathSin(radians);
  2320.  
  2321. // Adjust for alignment and rotation. Rotation of useHTML content is not yet implemented
  2322. // but it can probably be implemented for Firefox 3.5+ on user request. FF3.5+
  2323. // has support for CSS3 transform. The getBBox method also needs to be updated
  2324. // to compensate for the rotation, like it currently does for SVG.
  2325. // Test case: http://highcharts.com/tests/?file=text-rotation
  2326. css(elem, {
  2327. filter: rotation ? ['progid:DXImageTransform.Microsoft.Matrix(M11=', costheta,
  2328. ', M12=', -sintheta, ', M21=', sintheta, ', M22=', costheta,
  2329. ', sizingMethod=\'auto expand\')'].join('') : NONE
  2330. });
  2331. }
  2332.  
  2333. width = pick(wrapper.elemWidth, elem.offsetWidth);
  2334. height = pick(wrapper.elemHeight, elem.offsetHeight);
  2335.  
  2336. // update textWidth
  2337. if (width > textWidth && /[ \-]/.test(elem.innerText)) { // #983
  2338. css(elem, {
  2339. width: textWidth + PX,
  2340. display: 'block',
  2341. whiteSpace: 'normal'
  2342. });
  2343. width = textWidth;
  2344. }
  2345.  
  2346. // correct x and y
  2347. baseline = renderer.fontMetrics(elem.style.fontSize).b;
  2348. xCorr = costheta < 0 && -width;
  2349. yCorr = sintheta < 0 && -height;
  2350.  
  2351. // correct for baseline and corners spilling out after rotation
  2352. quad = costheta * sintheta < 0;
  2353. xCorr += sintheta * baseline * (quad ? 1 - alignCorrection : alignCorrection);
  2354. yCorr -= costheta * baseline * (rotation ? (quad ? alignCorrection : 1 - alignCorrection) : 1);
  2355.  
  2356. // correct for the length/height of the text
  2357. if (nonLeft) {
  2358. xCorr -= width * alignCorrection * (costheta < 0 ? -1 : 1);
  2359. if (rotation) {
  2360. yCorr -= height * alignCorrection * (sintheta < 0 ? -1 : 1);
  2361. }
  2362. css(elem, {
  2363. textAlign: align
  2364. });
  2365. }
  2366.  
  2367. // record correction
  2368. wrapper.xCorr = xCorr;
  2369. wrapper.yCorr = yCorr;
  2370. }
  2371.  
  2372. // apply position with correction
  2373. css(elem, {
  2374. left: (x + xCorr) + PX,
  2375. top: (y + yCorr) + PX
  2376. });
  2377.  
  2378. // record current text transform
  2379. wrapper.cTT = currentTextTransform;
  2380. }
  2381. },
  2382.  
  2383. /**
  2384. * Private method to update the transform attribute based on internal
  2385. * properties
  2386. */
  2387. updateTransform: function () {
  2388. var wrapper = this,
  2389. translateX = wrapper.translateX || 0,
  2390. translateY = wrapper.translateY || 0,
  2391. inverted = wrapper.inverted,
  2392. rotation = wrapper.rotation,
  2393. transform = [];
  2394.  
  2395. // flipping affects translate as adjustment for flipping around the group's axis
  2396. if (inverted) {
  2397. translateX += wrapper.attr('width');
  2398. translateY += wrapper.attr('height');
  2399. }
  2400.  
  2401. // apply translate
  2402. if (translateX || translateY) {
  2403. transform.push('translate(' + translateX + ',' + translateY + ')');
  2404. }
  2405.  
  2406. // apply rotation
  2407. if (inverted) {
  2408. transform.push('rotate(90) scale(-1,1)');
  2409. } else if (rotation) { // text rotation
  2410. transform.push('rotate(' + rotation + ' ' + (wrapper.x || 0) + ' ' + (wrapper.y || 0) + ')');
  2411. }
  2412.  
  2413. if (transform.length) {
  2414. attr(wrapper.element, 'transform', transform.join(' '));
  2415. }
  2416. },
  2417. /**
  2418. * Bring the element to the front
  2419. */
  2420. toFront: function () {
  2421. var element = this.element;
  2422. element.parentNode.appendChild(element);
  2423. return this;
  2424. },
  2425.  
  2426.  
  2427. /**
  2428. * Break down alignment options like align, verticalAlign, x and y
  2429. * to x and y relative to the chart.
  2430. *
  2431. * @param {Object} alignOptions
  2432. * @param {Boolean} alignByTranslate
  2433. * @param {Object} box The box to align to, needs a width and height
  2434. *
  2435. */
  2436. align: function (alignOptions, alignByTranslate, box) {
  2437. var elemWrapper = this;
  2438.  
  2439. if (!alignOptions) { // called on resize
  2440. alignOptions = elemWrapper.alignOptions;
  2441. alignByTranslate = elemWrapper.alignByTranslate;
  2442. } else { // first call on instanciate
  2443. elemWrapper.alignOptions = alignOptions;
  2444. elemWrapper.alignByTranslate = alignByTranslate;
  2445. if (!box) { // boxes other than renderer handle this internally
  2446. elemWrapper.renderer.alignedObjects.push(elemWrapper);
  2447. }
  2448. }
  2449.  
  2450. box = pick(box, elemWrapper.renderer);
  2451.  
  2452. var align = alignOptions.align,
  2453. vAlign = alignOptions.verticalAlign,
  2454. x = (box.x || 0) + (alignOptions.x || 0), // default: left align
  2455. y = (box.y || 0) + (alignOptions.y || 0), // default: top align
  2456. attribs = {};
  2457.  
  2458.  
  2459. // align
  2460. if (/^(right|center)$/.test(align)) {
  2461. x += (box.width - (alignOptions.width || 0)) /
  2462. { right: 1, center: 2 }[align];
  2463. }
  2464. attribs[alignByTranslate ? 'translateX' : 'x'] = mathRound(x);
  2465.  
  2466.  
  2467. // vertical align
  2468. if (/^(bottom|middle)$/.test(vAlign)) {
  2469. y += (box.height - (alignOptions.height || 0)) /
  2470. ({ bottom: 1, middle: 2 }[vAlign] || 1);
  2471.  
  2472. }
  2473. attribs[alignByTranslate ? 'translateY' : 'y'] = mathRound(y);
  2474.  
  2475. // animate only if already placed
  2476. elemWrapper[elemWrapper.placed ? 'animate' : 'attr'](attribs);
  2477. elemWrapper.placed = true;
  2478. elemWrapper.alignAttr = attribs;
  2479.  
  2480. return elemWrapper;
  2481. },
  2482.  
  2483. /**
  2484. * Get the bounding box (width, height, x and y) for the element
  2485. */
  2486. getBBox: function (refresh) {
  2487. var wrapper = this,
  2488. bBox,
  2489. width,
  2490. height,
  2491. rotation = wrapper.rotation,
  2492. element = wrapper.element,
  2493. rad = rotation * deg2rad;
  2494.  
  2495. // SVG elements
  2496. if (element.namespaceURI === SVG_NS || wrapper.renderer.forExport) {
  2497. try { // Fails in Firefox if the container has display: none.
  2498.  
  2499. bBox = element.getBBox ?
  2500. // SVG: use extend because IE9 is not allowed to change width and height in case
  2501. // of rotation (below)
  2502. extend({}, element.getBBox()) :
  2503. // Canvas renderer and legacy IE in export mode
  2504. {
  2505. width: element.offsetWidth,
  2506. height: element.offsetHeight
  2507. };
  2508. } catch (e) {}
  2509.  
  2510. // If the bBox is not set, the try-catch block above failed. The other condition
  2511. // is for Opera that returns a width of -Infinity on hidden elements.
  2512. if (!bBox || bBox.width < 0) {
  2513. bBox = { width: 0, height: 0 };
  2514. }
  2515.  
  2516. width = bBox.width;
  2517. height = bBox.height;
  2518.  
  2519. // adjust for rotated text
  2520. if (rotation) {
  2521. bBox.width = mathAbs(height * mathSin(rad)) + mathAbs(width * mathCos(rad));
  2522. bBox.height = mathAbs(height * mathCos(rad)) + mathAbs(width * mathSin(rad));
  2523. }
  2524.  
  2525. // VML Renderer or useHTML within SVG
  2526. } else {
  2527. bBox = wrapper.htmlGetBBox(refresh);
  2528. }
  2529.  
  2530. return bBox;
  2531. },
  2532.  
  2533. /**
  2534. * Show the element
  2535. */
  2536. show: function () {
  2537. return this.attr({ visibility: VISIBLE });
  2538. },
  2539.  
  2540. /**
  2541. * Hide the element
  2542. */
  2543. hide: function () {
  2544. return this.attr({ visibility: HIDDEN });
  2545. },
  2546.  
  2547. /**
  2548. * Add the element
  2549. * @param {Object|Undefined} parent Can be an element, an element wrapper or undefined
  2550. * to append the element to the renderer.box.
  2551. */
  2552. add: function (parent) {
  2553.  
  2554. var renderer = this.renderer,
  2555. parentWrapper = parent || renderer,
  2556. parentNode = parentWrapper.element || renderer.box,
  2557. childNodes = parentNode.childNodes,
  2558. element = this.element,
  2559. zIndex = attr(element, 'zIndex'),
  2560. otherElement,
  2561. otherZIndex,
  2562. i,
  2563. inserted;
  2564.  
  2565. // mark as inverted
  2566. this.parentInverted = parent && parent.inverted;
  2567.  
  2568. // build formatted text
  2569. if (this.textStr !== undefined) {
  2570. renderer.buildText(this);
  2571. }
  2572.  
  2573. // mark the container as having z indexed children
  2574. if (zIndex) {
  2575. parentWrapper.handleZ = true;
  2576. zIndex = pInt(zIndex);
  2577. }
  2578.  
  2579. // insert according to this and other elements' zIndex
  2580. if (parentWrapper.handleZ) { // this element or any of its siblings has a z index
  2581. for (i = 0; i < childNodes.length; i++) {
  2582. otherElement = childNodes[i];
  2583. otherZIndex = attr(otherElement, 'zIndex');
  2584. if (otherElement !== element && (
  2585. // insert before the first element with a higher zIndex
  2586. pInt(otherZIndex) > zIndex ||
  2587. // if no zIndex given, insert before the first element with a zIndex
  2588. (!defined(zIndex) && defined(otherZIndex))
  2589.  
  2590. )) {
  2591. parentNode.insertBefore(element, otherElement);
  2592. inserted = true;
  2593. break;
  2594. }
  2595. }
  2596. }
  2597.  
  2598. // default: append at the end
  2599. if (!inserted) {
  2600. parentNode.appendChild(element);
  2601. }
  2602.  
  2603. // mark as added
  2604. this.added = true;
  2605.  
  2606. // fire an event for internal hooks
  2607. fireEvent(this, 'add');
  2608.  
  2609. return this;
  2610. },
  2611.  
  2612. /**
  2613. * Removes a child either by removeChild or move to garbageBin.
  2614. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  2615. */
  2616. safeRemoveChild: function (element) {
  2617. var parentNode = element.parentNode;
  2618. if (parentNode) {
  2619. parentNode.removeChild(element);
  2620. }
  2621. },
  2622.  
  2623. /**
  2624. * Destroy the element and element wrapper
  2625. */
  2626. destroy: function () {
  2627. var wrapper = this,
  2628. element = wrapper.element || {},
  2629. shadows = wrapper.shadows,
  2630. box = wrapper.box,
  2631. key,
  2632. i;
  2633.  
  2634. // remove events
  2635. element.onclick = element.onmouseout = element.onmouseover = element.onmousemove = null;
  2636. stop(wrapper); // stop running animations
  2637.  
  2638. if (wrapper.clipPath) {
  2639. wrapper.clipPath = wrapper.clipPath.destroy();
  2640. }
  2641.  
  2642. // Destroy stops in case this is a gradient object
  2643. if (wrapper.stops) {
  2644. for (i = 0; i < wrapper.stops.length; i++) {
  2645. wrapper.stops[i] = wrapper.stops[i].destroy();
  2646. }
  2647. wrapper.stops = null;
  2648. }
  2649.  
  2650. // remove element
  2651. wrapper.safeRemoveChild(element);
  2652.  
  2653. // destroy shadows
  2654. if (shadows) {
  2655. each(shadows, function (shadow) {
  2656. wrapper.safeRemoveChild(shadow);
  2657. });
  2658. }
  2659.  
  2660. // destroy label box
  2661. if (box) {
  2662. box.destroy();
  2663. }
  2664.  
  2665. // remove from alignObjects
  2666. erase(wrapper.renderer.alignedObjects, wrapper);
  2667.  
  2668. for (key in wrapper) {
  2669. delete wrapper[key];
  2670. }
  2671.  
  2672. return null;
  2673. },
  2674.  
  2675. /**
  2676. * Empty a group element
  2677. */
  2678. empty: function () {
  2679. var element = this.element,
  2680. childNodes = element.childNodes,
  2681. i = childNodes.length;
  2682.  
  2683. while (i--) {
  2684. element.removeChild(childNodes[i]);
  2685. }
  2686. },
  2687.  
  2688. /**
  2689. * Add a shadow to the element. Must be done after the element is added to the DOM
  2690. * @param {Boolean} apply
  2691. */
  2692. shadow: function (apply, group, cutOff) {
  2693. var shadows = [],
  2694. i,
  2695. shadow,
  2696. element = this.element,
  2697. strokeWidth,
  2698.  
  2699. // compensate for inverted plot area
  2700. transform = this.parentInverted ? '(-1,-1)' : '(1,1)';
  2701.  
  2702.  
  2703. if (apply) {
  2704. for (i = 1; i <= 3; i++) {
  2705. shadow = element.cloneNode(0);
  2706. strokeWidth = 7 - 2 * i;
  2707. attr(shadow, {
  2708. 'isShadow': 'true',
  2709. 'stroke': 'rgb(0, 0, 0)',
  2710. 'stroke-opacity': 0.05 * i,
  2711. 'stroke-width': strokeWidth,
  2712. 'transform': 'translate' + transform,
  2713. 'fill': NONE
  2714. });
  2715. if (cutOff) {
  2716. attr(shadow, 'height', mathMax(attr(shadow, 'height') - strokeWidth, 0));
  2717. shadow.cutHeight = strokeWidth;
  2718. }
  2719.  
  2720. if (group) {
  2721. group.element.appendChild(shadow);
  2722. } else {
  2723. element.parentNode.insertBefore(shadow, element);
  2724. }
  2725.  
  2726. shadows.push(shadow);
  2727. }
  2728.  
  2729. this.shadows = shadows;
  2730. }
  2731. return this;
  2732.  
  2733. }
  2734. };
  2735.  
  2736.  
  2737. /**
  2738. * The default SVG renderer
  2739. */
  2740. var SVGRenderer = function () {
  2741. this.init.apply(this, arguments);
  2742. };
  2743. SVGRenderer.prototype = {
  2744. Element: SVGElement,
  2745.  
  2746. /**
  2747. * Initialize the SVGRenderer
  2748. * @param {Object} container
  2749. * @param {Number} width
  2750. * @param {Number} height
  2751. * @param {Boolean} forExport
  2752. */
  2753. init: function (container, width, height, forExport) {
  2754. var renderer = this,
  2755. loc = location,
  2756. boxWrapper;
  2757.  
  2758. boxWrapper = renderer.createElement('svg')
  2759. .attr({
  2760. xmlns: SVG_NS,
  2761. version: '1.1'
  2762. });
  2763. container.appendChild(boxWrapper.element);
  2764.  
  2765. // object properties
  2766. renderer.isSVG = true;
  2767. renderer.box = boxWrapper.element;
  2768. renderer.boxWrapper = boxWrapper;
  2769. renderer.alignedObjects = [];
  2770. renderer.url = isIE ? '' : loc.href.replace(/#.*?$/, '')
  2771. .replace(/([\('\)])/g, '\\$1'); // Page url used for internal references. #24, #672.
  2772. renderer.defs = this.createElement('defs').add();
  2773. renderer.forExport = forExport;
  2774. renderer.gradients = {}; // Object where gradient SvgElements are stored
  2775.  
  2776. renderer.setSize(width, height, false);
  2777.  
  2778.  
  2779.  
  2780. // Issue 110 workaround:
  2781. // In Firefox, if a div is positioned by percentage, its pixel position may land
  2782. // between pixels. The container itself doesn't display this, but an SVG element
  2783. // inside this container will be drawn at subpixel precision. In order to draw
  2784. // sharp lines, this must be compensated for. This doesn't seem to work inside
  2785. // iframes though (like in jsFiddle).
  2786. var subPixelFix, rect;
  2787. if (isFirefox && container.getBoundingClientRect) {
  2788. renderer.subPixelFix = subPixelFix = function () {
  2789. css(container, { left: 0, top: 0 });
  2790. rect = container.getBoundingClientRect();
  2791. css(container, {
  2792. left: (mathCeil(rect.left) - rect.left) + PX,
  2793. top: (mathCeil(rect.top) - rect.top) + PX
  2794. });
  2795. };
  2796.  
  2797. // run the fix now
  2798. subPixelFix();
  2799.  
  2800. // run it on resize
  2801. addEvent(win, 'resize', subPixelFix);
  2802. }
  2803. },
  2804.  
  2805. /**
  2806. * Detect whether the renderer is hidden. This happens when one of the parent elements
  2807. * has display: none. #608.
  2808. */
  2809. isHidden: function () {
  2810. return !this.boxWrapper.getBBox().width;
  2811. },
  2812.  
  2813. /**
  2814. * Destroys the renderer and its allocated members.
  2815. */
  2816. destroy: function () {
  2817. var renderer = this,
  2818. rendererDefs = renderer.defs;
  2819. renderer.box = null;
  2820. renderer.boxWrapper = renderer.boxWrapper.destroy();
  2821.  
  2822. // Call destroy on all gradient elements
  2823. destroyObjectProperties(renderer.gradients || {});
  2824. renderer.gradients = null;
  2825.  
  2826. // Defs are null in VMLRenderer
  2827. // Otherwise, destroy them here.
  2828. if (rendererDefs) {
  2829. renderer.defs = rendererDefs.destroy();
  2830. }
  2831.  
  2832. // Remove sub pixel fix handler
  2833. // We need to check that there is a handler, otherwise all functions that are registered for event 'resize' are removed
  2834. // See issue #982
  2835. if (renderer.subPixelFix) {
  2836. removeEvent(win, 'resize', renderer.subPixelFix);
  2837. }
  2838.  
  2839. renderer.alignedObjects = null;
  2840.  
  2841. return null;
  2842. },
  2843.  
  2844. /**
  2845. * Create a wrapper for an SVG element
  2846. * @param {Object} nodeName
  2847. */
  2848. createElement: function (nodeName) {
  2849. var wrapper = new this.Element();
  2850. wrapper.init(this, nodeName);
  2851. return wrapper;
  2852. },
  2853.  
  2854. /**
  2855. * Dummy function for use in canvas renderer
  2856. */
  2857. draw: function () {},
  2858.  
  2859. /**
  2860. * Parse a simple HTML string into SVG tspans
  2861. *
  2862. * @param {Object} textNode The parent text SVG node
  2863. */
  2864. buildText: function (wrapper) {
  2865. var textNode = wrapper.element,
  2866. lines = pick(wrapper.textStr, '').toString()
  2867. .replace(/<(b|strong)>/g, '<span style="font-weight:bold">')
  2868. .replace(/<(i|em)>/g, '<span style="font-style:italic">')
  2869. .replace(/<a/g, '<span')
  2870. .replace(/<\/(b|strong|i|em|a)>/g, '</span>')
  2871. .split(/<br.*?>/g),
  2872. childNodes = textNode.childNodes,
  2873. styleRegex = /style="([^"]+)"/,
  2874. hrefRegex = /href="([^"]+)"/,
  2875. parentX = attr(textNode, 'x'),
  2876. textStyles = wrapper.styles,
  2877. width = textStyles && pInt(textStyles.width),
  2878. textLineHeight = textStyles && textStyles.lineHeight,
  2879. lastLine,
  2880. GET_COMPUTED_STYLE = 'getComputedStyle',
  2881. i = childNodes.length,
  2882. linePositions = [];
  2883.  
  2884. // Needed in IE9 because it doesn't report tspan's offsetHeight (#893)
  2885. function getLineHeightByBBox(lineNo) {
  2886. linePositions[lineNo] = textNode.getBBox().height;
  2887. return mathRound(linePositions[lineNo] - (linePositions[lineNo - 1] || 0));
  2888. }
  2889.  
  2890. // remove old text
  2891. while (i--) {
  2892. textNode.removeChild(childNodes[i]);
  2893. }
  2894.  
  2895. if (width && !wrapper.added) {
  2896. this.box.appendChild(textNode); // attach it to the DOM to read offset width
  2897. }
  2898.  
  2899. // remove empty line at end
  2900. if (lines[lines.length - 1] === '') {
  2901. lines.pop();
  2902. }
  2903.  
  2904. // build the lines
  2905. each(lines, function (line, lineNo) {
  2906. var spans, spanNo = 0, lineHeight;
  2907.  
  2908. line = line.replace(/<span/g, '|||<span').replace(/<\/span>/g, '</span>|||');
  2909. spans = line.split('|||');
  2910.  
  2911. each(spans, function (span) {
  2912. if (span !== '' || spans.length === 1) {
  2913. var attributes = {},
  2914. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2915. if (styleRegex.test(span)) {
  2916. attr(
  2917. tspan,
  2918. 'style',
  2919. span.match(styleRegex)[1].replace(/(;| |^)color([ :])/, '$1fill$2')
  2920. );
  2921. }
  2922. if (hrefRegex.test(span)) {
  2923. attr(tspan, 'onclick', 'location.href=\"' + span.match(hrefRegex)[1] + '\"');
  2924. css(tspan, { cursor: 'pointer' });
  2925. }
  2926.  
  2927. span = (span.replace(/<(.|\n)*?>/g, '') || ' ')
  2928. .replace(/&lt;/g, '<')
  2929. .replace(/&gt;/g, '>');
  2930.  
  2931. // issue #38 workaround.
  2932. /*if (reverse) {
  2933. arr = [];
  2934. i = span.length;
  2935. while (i--) {
  2936. arr.push(span.charAt(i));
  2937. }
  2938. span = arr.join('');
  2939. }*/
  2940.  
  2941. // add the text node
  2942. tspan.appendChild(doc.createTextNode(span));
  2943.  
  2944. if (!spanNo) { // first span in a line, align it to the left
  2945. attributes.x = parentX;
  2946. } else {
  2947. // Firefox ignores spaces at the front or end of the tspan
  2948. attributes.dx = 3; // space
  2949. }
  2950.  
  2951. // first span on subsequent line, add the line height
  2952. if (!spanNo) {
  2953. if (lineNo) {
  2954.  
  2955. // allow getting the right offset height in exporting in IE
  2956. if (!hasSVG && wrapper.renderer.forExport) {
  2957. css(tspan, { display: 'block' });
  2958. }
  2959.  
  2960. // Webkit and opera sometimes return 'normal' as the line height. In that
  2961. // case, webkit uses offsetHeight, while Opera falls back to 18
  2962. lineHeight = win[GET_COMPUTED_STYLE] &&
  2963. pInt(win[GET_COMPUTED_STYLE](lastLine, null).getPropertyValue('line-height'));
  2964.  
  2965. if (!lineHeight || isNaN(lineHeight)) {
  2966. lineHeight = textLineHeight || lastLine.offsetHeight || getLineHeightByBBox(lineNo) || 18;
  2967. }
  2968. attr(tspan, 'dy', lineHeight);
  2969. }
  2970. lastLine = tspan; // record for use in next line
  2971. }
  2972.  
  2973. // add attributes
  2974. attr(tspan, attributes);
  2975.  
  2976. // append it
  2977. textNode.appendChild(tspan);
  2978.  
  2979. spanNo++;
  2980.  
  2981. // check width and apply soft breaks
  2982. if (width) {
  2983. var words = span.replace(/-/g, '- ').split(' '),
  2984. tooLong,
  2985. actualWidth,
  2986. rest = [];
  2987.  
  2988. while (words.length || rest.length) {
  2989. actualWidth = wrapper.getBBox().width;
  2990. tooLong = actualWidth > width;
  2991. if (!tooLong || words.length === 1) { // new line needed
  2992. words = rest;
  2993. rest = [];
  2994. if (words.length) {
  2995. tspan = doc.createElementNS(SVG_NS, 'tspan');
  2996. attr(tspan, {
  2997. dy: textLineHeight || 16,
  2998. x: parentX
  2999. });
  3000. textNode.appendChild(tspan);
  3001.  
  3002. if (actualWidth > width) { // a single word is pressing it out
  3003. width = actualWidth;
  3004. }
  3005. }
  3006. } else { // append to existing line tspan
  3007. tspan.removeChild(tspan.firstChild);
  3008. rest.unshift(words.pop());
  3009. }
  3010. if (words.length) {
  3011. tspan.appendChild(doc.createTextNode(words.join(' ').replace(/- /g, '-')));
  3012. }
  3013. }
  3014. }
  3015. }
  3016. });
  3017. });
  3018. },
  3019.  
  3020. /**
  3021. * Create a button with preset states
  3022. * @param {String} text
  3023. * @param {Number} x
  3024. * @param {Number} y
  3025. * @param {Function} callback
  3026. * @param {Object} normalState
  3027. * @param {Object} hoverState
  3028. * @param {Object} pressedState
  3029. */
  3030. button: function (text, x, y, callback, normalState, hoverState, pressedState) {
  3031. var label = this.label(text, x, y),
  3032. curState = 0,
  3033. stateOptions,
  3034. stateStyle,
  3035. normalStyle,
  3036. hoverStyle,
  3037. pressedStyle,
  3038. STYLE = 'style',
  3039. verticalGradient = { x1: 0, y1: 0, x2: 0, y2: 1 };
  3040.  
  3041. // prepare the attributes
  3042. /*jslint white: true*/
  3043. normalState = merge(hash(
  3044. STROKE_WIDTH, 1,
  3045. STROKE, '#999',
  3046. FILL, hash(
  3047. LINEAR_GRADIENT, verticalGradient,
  3048. STOPS, [
  3049. [0, '#FFF'],
  3050. [1, '#DDD']
  3051. ]
  3052. ),
  3053. 'r', 3,
  3054. 'padding', 3,
  3055. STYLE, hash(
  3056. 'color', 'black'
  3057. )
  3058. ), normalState);
  3059. /*jslint white: false*/
  3060. normalStyle = normalState[STYLE];
  3061. delete normalState[STYLE];
  3062.  
  3063. /*jslint white: true*/
  3064. hoverState = merge(normalState, hash(
  3065. STROKE, '#68A',
  3066. FILL, hash(
  3067. LINEAR_GRADIENT, verticalGradient,
  3068. STOPS, [
  3069. [0, '#FFF'],
  3070. [1, '#ACF']
  3071. ]
  3072. )
  3073. ), hoverState);
  3074. /*jslint white: false*/
  3075. hoverStyle = hoverState[STYLE];
  3076. delete hoverState[STYLE];
  3077.  
  3078. /*jslint white: true*/
  3079. pressedState = merge(normalState, hash(
  3080. STROKE, '#68A',
  3081. FILL, hash(
  3082. LINEAR_GRADIENT, verticalGradient,
  3083. STOPS, [
  3084. [0, '#9BD'],
  3085. [1, '#CDF']
  3086. ]
  3087. )
  3088. ), pressedState);
  3089. /*jslint white: false*/
  3090. pressedStyle = pressedState[STYLE];
  3091. delete pressedState[STYLE];
  3092.  
  3093. // add the events
  3094. addEvent(label.element, 'mouseenter', function () {
  3095. label.attr(hoverState)
  3096. .css(hoverStyle);
  3097. });
  3098. addEvent(label.element, 'mouseleave', function () {
  3099. stateOptions = [normalState, hoverState, pressedState][curState];
  3100. stateStyle = [normalStyle, hoverStyle, pressedStyle][curState];
  3101. label.attr(stateOptions)
  3102. .css(stateStyle);
  3103. });
  3104.  
  3105. label.setState = function (state) {
  3106. curState = state;
  3107. if (!state) {
  3108. label.attr(normalState)
  3109. .css(normalStyle);
  3110. } else if (state === 2) {
  3111. label.attr(pressedState)
  3112. .css(pressedStyle);
  3113. }
  3114. };
  3115.  
  3116. return label
  3117. .on('click', function () {
  3118. callback.call(label);
  3119. })
  3120. .attr(normalState)
  3121. .css(extend({ cursor: 'default' }, normalStyle));
  3122. },
  3123.  
  3124. /**
  3125. * Make a straight line crisper by not spilling out to neighbour pixels
  3126. * @param {Array} points
  3127. * @param {Number} width
  3128. */
  3129. crispLine: function (points, width) {
  3130. // points format: [M, 0, 0, L, 100, 0]
  3131. // normalize to a crisp line
  3132. if (points[1] === points[4]) {
  3133. points[1] = points[4] = mathRound(points[1]) + (width % 2 / 2);
  3134. }
  3135. if (points[2] === points[5]) {
  3136. points[2] = points[5] = mathRound(points[2]) + (width % 2 / 2);
  3137. }
  3138. return points;
  3139. },
  3140.  
  3141.  
  3142. /**
  3143. * Draw a path
  3144. * @param {Array} path An SVG path in array form
  3145. */
  3146. path: function (path) {
  3147. var attr = {
  3148. fill: NONE
  3149. };
  3150. if (isArray(path)) {
  3151. attr.d = path;
  3152. } else if (isObject(path)) { // attributes
  3153. extend(attr, path);
  3154. }
  3155. return this.createElement('path').attr(attr);
  3156. },
  3157.  
  3158. /**
  3159. * Draw and return an SVG circle
  3160. * @param {Number} x The x position
  3161. * @param {Number} y The y position
  3162. * @param {Number} r The radius
  3163. */
  3164. circle: function (x, y, r) {
  3165. var attr = isObject(x) ?
  3166. x :
  3167. {
  3168. x: x,
  3169. y: y,
  3170. r: r
  3171. };
  3172.  
  3173. return this.createElement('circle').attr(attr);
  3174. },
  3175.  
  3176. /**
  3177. * Draw and return an arc
  3178. * @param {Number} x X position
  3179. * @param {Number} y Y position
  3180. * @param {Number} r Radius
  3181. * @param {Number} innerR Inner radius like used in donut charts
  3182. * @param {Number} start Starting angle
  3183. * @param {Number} end Ending angle
  3184. */
  3185. arc: function (x, y, r, innerR, start, end) {
  3186. // arcs are defined as symbols for the ability to set
  3187. // attributes in attr and animate
  3188.  
  3189. if (isObject(x)) {
  3190. y = x.y;
  3191. r = x.r;
  3192. innerR = x.innerR;
  3193. start = x.start;
  3194. end = x.end;
  3195. x = x.x;
  3196. }
  3197. return this.symbol('arc', x || 0, y || 0, r || 0, r || 0, {
  3198. innerR: innerR || 0,
  3199. start: start || 0,
  3200. end: end || 0
  3201. });
  3202. },
  3203.  
  3204. /**
  3205. * Draw and return a rectangle
  3206. * @param {Number} x Left position
  3207. * @param {Number} y Top position
  3208. * @param {Number} width
  3209. * @param {Number} height
  3210. * @param {Number} r Border corner radius
  3211. * @param {Number} strokeWidth A stroke width can be supplied to allow crisp drawing
  3212. */
  3213. rect: function (x, y, width, height, r, strokeWidth) {
  3214.  
  3215. r = isObject(x) ? x.r : r;
  3216.  
  3217. var wrapper = this.createElement('rect').attr({
  3218. rx: r,
  3219. ry: r,
  3220. fill: NONE
  3221. });
  3222. return wrapper.attr(
  3223. isObject(x) ?
  3224. x :
  3225. // do not crispify when an object is passed in (as in column charts)
  3226. wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0))
  3227. );
  3228. },
  3229.  
  3230. /**
  3231. * Resize the box and re-align all aligned elements
  3232. * @param {Object} width
  3233. * @param {Object} height
  3234. * @param {Boolean} animate
  3235. *
  3236. */
  3237. setSize: function (width, height, animate) {
  3238. var renderer = this,
  3239. alignedObjects = renderer.alignedObjects,
  3240. i = alignedObjects.length;
  3241.  
  3242. renderer.width = width;
  3243. renderer.height = height;
  3244.  
  3245. renderer.boxWrapper[pick(animate, true) ? 'animate' : 'attr']({
  3246. width: width,
  3247. height: height
  3248. });
  3249.  
  3250. while (i--) {
  3251. alignedObjects[i].align();
  3252. }
  3253. },
  3254.  
  3255. /**
  3256. * Create a group
  3257. * @param {String} name The group will be given a class name of 'highcharts-{name}'.
  3258. * This can be used for styling and scripting.
  3259. */
  3260. g: function (name) {
  3261. var elem = this.createElement('g');
  3262. return defined(name) ? elem.attr({ 'class': PREFIX + name }) : elem;
  3263. },
  3264.  
  3265. /**
  3266. * Display an image
  3267. * @param {String} src
  3268. * @param {Number} x
  3269. * @param {Number} y
  3270. * @param {Number} width
  3271. * @param {Number} height
  3272. */
  3273. image: function (src, x, y, width, height) {
  3274. var attribs = {
  3275. preserveAspectRatio: NONE
  3276. },
  3277. elemWrapper;
  3278.  
  3279. // optional properties
  3280. if (arguments.length > 1) {
  3281. extend(attribs, {
  3282. x: x,
  3283. y: y,
  3284. width: width,
  3285. height: height
  3286. });
  3287. }
  3288.  
  3289. elemWrapper = this.createElement('image').attr(attribs);
  3290.  
  3291. // set the href in the xlink namespace
  3292. if (elemWrapper.element.setAttributeNS) {
  3293. elemWrapper.element.setAttributeNS('http://www.w3.org/1999/xlink',
  3294. 'href', src);
  3295. } else {
  3296. // could be exporting in IE
  3297. // using href throws "not supported" in ie7 and under, requries regex shim to fix later
  3298. elemWrapper.element.setAttribute('hc-svg-href', src);
  3299. }
  3300.  
  3301. return elemWrapper;
  3302. },
  3303.  
  3304. /**
  3305. * Draw a symbol out of pre-defined shape paths from the namespace 'symbol' object.
  3306. *
  3307. * @param {Object} symbol
  3308. * @param {Object} x
  3309. * @param {Object} y
  3310. * @param {Object} radius
  3311. * @param {Object} options
  3312. */
  3313. symbol: function (symbol, x, y, width, height, options) {
  3314.  
  3315. var obj,
  3316.  
  3317. // get the symbol definition function
  3318. symbolFn = this.symbols[symbol],
  3319.  
  3320. // check if there's a path defined for this symbol
  3321. path = symbolFn && symbolFn(
  3322. mathRound(x),
  3323. mathRound(y),
  3324. width,
  3325. height,
  3326. options
  3327. ),
  3328.  
  3329. imageRegex = /^url\((.*?)\)$/,
  3330. imageSrc,
  3331. imageSize,
  3332. centerImage;
  3333.  
  3334. if (path) {
  3335.  
  3336. obj = this.path(path);
  3337. // expando properties for use in animate and attr
  3338. extend(obj, {
  3339. symbolName: symbol,
  3340. x: x,
  3341. y: y,
  3342. width: width,
  3343. height: height
  3344. });
  3345. if (options) {
  3346. extend(obj, options);
  3347. }
  3348.  
  3349.  
  3350. // image symbols
  3351. } else if (imageRegex.test(symbol)) {
  3352.  
  3353. // On image load, set the size and position
  3354. centerImage = function (img, size) {
  3355. img.attr({
  3356. width: size[0],
  3357. height: size[1]
  3358. });
  3359.  
  3360. if (!img.alignByTranslate) { // #185
  3361. img.translate(
  3362. -mathRound(size[0] / 2),
  3363. -mathRound(size[1] / 2)
  3364. );
  3365. }
  3366. };
  3367.  
  3368. imageSrc = symbol.match(imageRegex)[1];
  3369. imageSize = symbolSizes[imageSrc];
  3370.  
  3371. // create the image synchronously, add attribs async
  3372. obj = this.image(imageSrc)
  3373. .attr({
  3374. x: x,
  3375. y: y
  3376. });
  3377.  
  3378. if (imageSize) {
  3379. centerImage(obj, imageSize);
  3380. } else {
  3381. // initialize image to be 0 size so export will still function if there's no cached sizes
  3382. obj.attr({ width: 0, height: 0 });
  3383.  
  3384. // create a dummy JavaScript image to get the width and height
  3385. createElement('img', {
  3386. onload: function () {
  3387. var img = this;
  3388.  
  3389. centerImage(obj, symbolSizes[imageSrc] = [img.width, img.height]);
  3390. },
  3391. src: imageSrc
  3392. });
  3393. }
  3394. }
  3395.  
  3396. return obj;
  3397. },
  3398.  
  3399. /**
  3400. * An extendable collection of functions for defining symbol paths.
  3401. */
  3402. symbols: {
  3403. 'circle': function (x, y, w, h) {
  3404. var cpw = 0.166 * w;
  3405. return [
  3406. M, x + w / 2, y,
  3407. 'C', x + w + cpw, y, x + w + cpw, y + h, x + w / 2, y + h,
  3408. 'C', x - cpw, y + h, x - cpw, y, x + w / 2, y,
  3409. 'Z'
  3410. ];
  3411. },
  3412.  
  3413. 'square': function (x, y, w, h) {
  3414. return [
  3415. M, x, y,
  3416. L, x + w, y,
  3417. x + w, y + h,
  3418. x, y + h,
  3419. 'Z'
  3420. ];
  3421. },
  3422.  
  3423. 'triangle': function (x, y, w, h) {
  3424. return [
  3425. M, x + w / 2, y,
  3426. L, x + w, y + h,
  3427. x, y + h,
  3428. 'Z'
  3429. ];
  3430. },
  3431.  
  3432. 'triangle-down': function (x, y, w, h) {
  3433. return [
  3434. M, x, y,
  3435. L, x + w, y,
  3436. x + w / 2, y + h,
  3437. 'Z'
  3438. ];
  3439. },
  3440. 'diamond': function (x, y, w, h) {
  3441. return [
  3442. M, x + w / 2, y,
  3443. L, x + w, y + h / 2,
  3444. x + w / 2, y + h,
  3445. x, y + h / 2,
  3446. 'Z'
  3447. ];
  3448. },
  3449. 'arc': function (x, y, w, h, options) {
  3450. var start = options.start,
  3451. radius = options.r || w || h,
  3452. end = options.end - 0.000001, // to prevent cos and sin of start and end from becoming equal on 360 arcs
  3453. innerRadius = options.innerR,
  3454. open = options.open,
  3455. cosStart = mathCos(start),
  3456. sinStart = mathSin(start),
  3457. cosEnd = mathCos(end),
  3458. sinEnd = mathSin(end),
  3459. longArc = options.end - start < mathPI ? 0 : 1;
  3460.  
  3461. return [
  3462. M,
  3463. x + radius * cosStart,
  3464. y + radius * sinStart,
  3465. 'A', // arcTo
  3466. radius, // x radius
  3467. radius, // y radius
  3468. 0, // slanting
  3469. longArc, // long or short arc
  3470. 1, // clockwise
  3471. x + radius * cosEnd,
  3472. y + radius * sinEnd,
  3473. open ? M : L,
  3474. x + innerRadius * cosEnd,
  3475. y + innerRadius * sinEnd,
  3476. 'A', // arcTo
  3477. innerRadius, // x radius
  3478. innerRadius, // y radius
  3479. 0, // slanting
  3480. longArc, // long or short arc
  3481. 0, // clockwise
  3482. x + innerRadius * cosStart,
  3483. y + innerRadius * sinStart,
  3484.  
  3485. open ? '' : 'Z' // close
  3486. ];
  3487. }
  3488. },
  3489.  
  3490. /**
  3491. * Define a clipping rectangle
  3492. * @param {String} id
  3493. * @param {Number} x
  3494. * @param {Number} y
  3495. * @param {Number} width
  3496. * @param {Number} height
  3497. */
  3498. clipRect: function (x, y, width, height) {
  3499. var wrapper,
  3500. id = PREFIX + idCounter++,
  3501.  
  3502. clipPath = this.createElement('clipPath').attr({
  3503. id: id
  3504. }).add(this.defs);
  3505.  
  3506. wrapper = this.rect(x, y, width, height, 0).add(clipPath);
  3507. wrapper.id = id;
  3508. wrapper.clipPath = clipPath;
  3509.  
  3510. return wrapper;
  3511. },
  3512.  
  3513.  
  3514. /**
  3515. * Take a color and return it if it's a string, make it a gradient if it's a
  3516. * gradient configuration object. Prior to Highstock, an array was used to define
  3517. * a linear gradient with pixel positions relative to the SVG. In newer versions
  3518. * we change the coordinates to apply relative to the shape, using coordinates
  3519. * 0-1 within the shape. To preserve backwards compatibility, linearGradient
  3520. * in this definition is an object of x1, y1, x2 and y2.
  3521. *
  3522. * @param {Object} color The color or config object
  3523. */
  3524. color: function (color, elem, prop) {
  3525. var renderer = this,
  3526. colorObject,
  3527. regexRgba = /^rgba/,
  3528. gradName;
  3529.  
  3530. // Apply linear or radial gradients
  3531. if (color && color.linearGradient) {
  3532. gradName = 'linearGradient';
  3533. } else if (color && color.radialGradient) {
  3534. gradName = 'radialGradient';
  3535. }
  3536.  
  3537. if (gradName) {
  3538. var gradAttr = color[gradName],
  3539. gradients = renderer.gradients,
  3540. gradientObject,
  3541. stopColor,
  3542. stopOpacity,
  3543. radialReference = elem.radialReference;
  3544.  
  3545. // Check if a gradient object with the same config object is created within this renderer
  3546. if (!gradAttr.id || !gradients[gradAttr.id]) {
  3547.  
  3548. // Keep < 2.2 kompatibility
  3549. if (isArray(gradAttr)) {
  3550. color[gradName] = gradAttr = {
  3551. x1: gradAttr[0],
  3552. y1: gradAttr[1],
  3553. x2: gradAttr[2],
  3554. y2: gradAttr[3],
  3555. gradientUnits: 'userSpaceOnUse'
  3556. };
  3557. }
  3558.  
  3559. // Correct the radial gradient for the radial reference system
  3560. if (gradName === 'radialGradient' && radialReference && !defined(gradAttr.gradientUnits)) {
  3561. extend(gradAttr, {
  3562. cx: (radialReference[0] - radialReference[2] / 2) + gradAttr.cx * radialReference[2],
  3563. cy: (radialReference[1] - radialReference[2] / 2) + gradAttr.cy * radialReference[2],
  3564. r: gradAttr.r * radialReference[2],
  3565. gradientUnits: 'userSpaceOnUse'
  3566. });
  3567. }
  3568.  
  3569. // Set the id and create the element
  3570. gradAttr.id = PREFIX + idCounter++;
  3571. gradients[gradAttr.id] = gradientObject = renderer.createElement(gradName)
  3572. .attr(gradAttr)
  3573. .add(renderer.defs);
  3574.  
  3575.  
  3576. // The gradient needs to keep a list of stops to be able to destroy them
  3577. gradientObject.stops = [];
  3578. each(color.stops, function (stop) {
  3579. var stopObject;
  3580. if (regexRgba.test(stop[1])) {
  3581. colorObject = Color(stop[1]);
  3582. stopColor = colorObject.get('rgb');
  3583. stopOpacity = colorObject.get('a');
  3584. } else {
  3585. stopColor = stop[1];
  3586. stopOpacity = 1;
  3587. }
  3588. stopObject = renderer.createElement('stop').attr({
  3589. offset: stop[0],
  3590. 'stop-color': stopColor,
  3591. 'stop-opacity': stopOpacity
  3592. }).add(gradientObject);
  3593.  
  3594. // Add the stop element to the gradient
  3595. gradientObject.stops.push(stopObject);
  3596. });
  3597. }
  3598.  
  3599. // Return the reference to the gradient object
  3600. return 'url(' + renderer.url + '#' + gradAttr.id + ')';
  3601.  
  3602. // Webkit and Batik can't show rgba.
  3603. } else if (regexRgba.test(color)) {
  3604. colorObject = Color(color);
  3605. attr(elem, prop + '-opacity', colorObject.get('a'));
  3606.  
  3607. return colorObject.get('rgb');
  3608.  
  3609.  
  3610. } else {
  3611. // Remove the opacity attribute added above. Does not throw if the attribute is not there.
  3612. elem.removeAttribute(prop + '-opacity');
  3613.  
  3614. return color;
  3615. }
  3616.  
  3617. },
  3618.  
  3619.  
  3620. /**
  3621. * Add text to the SVG object
  3622. * @param {String} str
  3623. * @param {Number} x Left position
  3624. * @param {Number} y Top position
  3625. * @param {Boolean} useHTML Use HTML to render the text
  3626. */
  3627. text: function (str, x, y, useHTML) {
  3628.  
  3629. // declare variables
  3630. var renderer = this,
  3631. defaultChartStyle = defaultOptions.chart.style,
  3632. wrapper;
  3633.  
  3634. if (useHTML && !renderer.forExport) {
  3635. return renderer.html(str, x, y);
  3636. }
  3637.  
  3638. x = mathRound(pick(x, 0));
  3639. y = mathRound(pick(y, 0));
  3640.  
  3641. wrapper = renderer.createElement('text')
  3642. .attr({
  3643. x: x,
  3644. y: y,
  3645. text: str
  3646. })
  3647. .css({
  3648. fontFamily: defaultChartStyle.fontFamily,
  3649. fontSize: defaultChartStyle.fontSize
  3650. });
  3651.  
  3652. wrapper.x = x;
  3653. wrapper.y = y;
  3654. return wrapper;
  3655. },
  3656.  
  3657.  
  3658. /**
  3659. * Create HTML text node. This is used by the VML renderer as well as the SVG
  3660. * renderer through the useHTML option.
  3661. *
  3662. * @param {String} str
  3663. * @param {Number} x
  3664. * @param {Number} y
  3665. */
  3666. html: function (str, x, y) {
  3667. var defaultChartStyle = defaultOptions.chart.style,
  3668. wrapper = this.createElement('span'),
  3669. attrSetters = wrapper.attrSetters,
  3670. element = wrapper.element,
  3671. renderer = wrapper.renderer;
  3672.  
  3673. // Text setter
  3674. attrSetters.text = function (value) {
  3675. element.innerHTML = value;
  3676. return false;
  3677. };
  3678.  
  3679. // Various setters which rely on update transform
  3680. attrSetters.x = attrSetters.y = attrSetters.align = function (value, key) {
  3681. if (key === 'align') {
  3682. key = 'textAlign'; // Do not overwrite the SVGElement.align method. Same as VML.
  3683. }
  3684. wrapper[key] = value;
  3685. wrapper.htmlUpdateTransform();
  3686. return false;
  3687. };
  3688.  
  3689. // Set the default attributes
  3690. wrapper.attr({
  3691. text: str,
  3692. x: mathRound(x),
  3693. y: mathRound(y)
  3694. })
  3695. .css({
  3696. position: ABSOLUTE,
  3697. whiteSpace: 'nowrap',
  3698. fontFamily: defaultChartStyle.fontFamily,
  3699. fontSize: defaultChartStyle.fontSize
  3700. });
  3701.  
  3702. // Use the HTML specific .css method
  3703. wrapper.css = wrapper.htmlCss;
  3704.  
  3705. // This is specific for HTML within SVG
  3706. if (renderer.isSVG) {
  3707. wrapper.add = function (svgGroupWrapper) {
  3708.  
  3709. var htmlGroup,
  3710. htmlGroupStyle,
  3711. container = renderer.box.parentNode;
  3712.  
  3713. // Create a mock group to hold the HTML elements
  3714. if (svgGroupWrapper) {
  3715. htmlGroup = svgGroupWrapper.div;
  3716. if (!htmlGroup) {
  3717. htmlGroup = svgGroupWrapper.div = createElement(DIV, {
  3718. className: attr(svgGroupWrapper.element, 'class')
  3719. }, {
  3720. position: ABSOLUTE,
  3721. left: svgGroupWrapper.attr('translateX') + PX,
  3722. top: svgGroupWrapper.attr('translateY') + PX
  3723. }, container);
  3724.  
  3725. // Ensure dynamic updating position
  3726. htmlGroupStyle = htmlGroup.style;
  3727. extend(svgGroupWrapper.attrSetters, {
  3728. translateX: function (value) {
  3729. htmlGroupStyle.left = value + PX;
  3730. },
  3731. translateY: function (value) {
  3732. htmlGroupStyle.top = value + PX;
  3733. },
  3734. visibility: function (value, key) {
  3735. htmlGroupStyle[key] = value;
  3736. }
  3737. });
  3738.  
  3739. }
  3740. } else {
  3741. htmlGroup = container;
  3742. }
  3743.  
  3744. htmlGroup.appendChild(element);
  3745.  
  3746. // Shared with VML:
  3747. wrapper.added = true;
  3748. if (wrapper.alignOnAdd) {
  3749. wrapper.htmlUpdateTransform();
  3750. }
  3751.  
  3752. return wrapper;
  3753. };
  3754. }
  3755. return wrapper;
  3756. },
  3757.  
  3758. /**
  3759. * Utility to return the baseline offset and total line height from the font size
  3760. */
  3761. fontMetrics: function (fontSize) {
  3762. fontSize = pInt(fontSize || 11);
  3763.  
  3764. // Empirical values found by comparing font size and bounding box height.
  3765. // Applies to the default font family. http://jsfiddle.net/highcharts/7xvn7/
  3766. var lineHeight = fontSize < 24 ? fontSize + 4 : mathRound(fontSize * 1.2),
  3767. baseline = mathRound(lineHeight * 0.8);
  3768.  
  3769. return {
  3770. h: lineHeight,
  3771. b: baseline
  3772. };
  3773. },
  3774.  
  3775. /**
  3776. * Add a label, a text item that can hold a colored or gradient background
  3777. * as well as a border and shadow.
  3778. * @param {string} str
  3779. * @param {Number} x
  3780. * @param {Number} y
  3781. * @param {String} shape
  3782. * @param {Number} anchorX In case the shape has a pointer, like a flag, this is the
  3783. * coordinates it should be pinned to
  3784. * @param {Number} anchorY
  3785. * @param {Boolean} baseline Whether to position the label relative to the text baseline,
  3786. * like renderer.text, or to the upper border of the rectangle.
  3787. * @param {String} className Class name for the group
  3788. */
  3789. label: function (str, x, y, shape, anchorX, anchorY, useHTML, baseline, className) {
  3790.  
  3791. var renderer = this,
  3792. wrapper = renderer.g(className),
  3793. text = renderer.text('', 0, 0, useHTML)
  3794. .attr({
  3795. zIndex: 1
  3796. })
  3797. .add(wrapper),
  3798. box,
  3799. bBox,
  3800. alignFactor = 0,
  3801. padding = 3,
  3802. width,
  3803. height,
  3804. wrapperX,
  3805. wrapperY,
  3806. crispAdjust = 0,
  3807. deferredAttr = {},
  3808. baselineOffset,
  3809. attrSetters = wrapper.attrSetters;
  3810.  
  3811. /**
  3812. * This function runs after the label is added to the DOM (when the bounding box is
  3813. * available), and after the text of the label is updated to detect the new bounding
  3814. * box and reflect it in the border box.
  3815. */
  3816. function updateBoxSize() {
  3817. var boxY,
  3818. style = text.element.style;
  3819.  
  3820. bBox = (width === undefined || height === undefined || wrapper.styles.textAlign) &&
  3821. text.getBBox(true);
  3822. wrapper.width = (width || bBox.width || 0) + 2 * padding;
  3823. wrapper.height = (height || bBox.height || 0) + 2 * padding;
  3824.  
  3825. // update the label-scoped y offset
  3826. baselineOffset = padding + renderer.fontMetrics(style && style.fontSize).b;
  3827.  
  3828.  
  3829. // create the border box if it is not already present
  3830. if (!box) {
  3831. boxY = baseline ? -baselineOffset : 0;
  3832.  
  3833. wrapper.box = box = shape ?
  3834. renderer.symbol(shape, -alignFactor * padding, boxY, wrapper.width, wrapper.height) :
  3835. renderer.rect(-alignFactor * padding, boxY, wrapper.width, wrapper.height, 0, deferredAttr[STROKE_WIDTH]);
  3836. box.add(wrapper);
  3837. }
  3838.  
  3839. // apply the box attributes
  3840. box.attr(merge({
  3841. width: wrapper.width,
  3842. height: wrapper.height
  3843. }, deferredAttr));
  3844. deferredAttr = null;
  3845. }
  3846.  
  3847. /**
  3848. * This function runs after setting text or padding, but only if padding is changed
  3849. */
  3850. function updateTextPadding() {
  3851. var styles = wrapper.styles,
  3852. textAlign = styles && styles.textAlign,
  3853. x = padding * (1 - alignFactor),
  3854. y;
  3855.  
  3856. // determin y based on the baseline
  3857. y = baseline ? 0 : baselineOffset;
  3858.  
  3859. // compensate for alignment
  3860. if (defined(width) && (textAlign === 'center' || textAlign === 'right')) {
  3861. x += { center: 0.5, right: 1 }[textAlign] * (width - bBox.width);
  3862. }
  3863.  
  3864. // update if anything changed
  3865. if (x !== text.x || y !== text.y) {
  3866. text.attr({
  3867. x: x,
  3868. y: y
  3869. });
  3870. }
  3871.  
  3872. // record current values
  3873. text.x = x;
  3874. text.y = y;
  3875. }
  3876.  
  3877. /**
  3878. * Set a box attribute, or defer it if the box is not yet created
  3879. * @param {Object} key
  3880. * @param {Object} value
  3881. */
  3882. function boxAttr(key, value) {
  3883. if (box) {
  3884. box.attr(key, value);
  3885. } else {
  3886. deferredAttr[key] = value;
  3887. }
  3888. }
  3889.  
  3890. function getSizeAfterAdd() {
  3891. wrapper.attr({
  3892. text: str, // alignment is available now
  3893. x: x,
  3894. y: y
  3895. });
  3896.  
  3897. if (defined(anchorX)) {
  3898. wrapper.attr({
  3899. anchorX: anchorX,
  3900. anchorY: anchorY
  3901. });
  3902. }
  3903. }
  3904.  
  3905. /**
  3906. * After the text element is added, get the desired size of the border box
  3907. * and add it before the text in the DOM.
  3908. */
  3909. addEvent(wrapper, 'add', getSizeAfterAdd);
  3910.  
  3911. /*
  3912. * Add specific attribute setters.
  3913. */
  3914.  
  3915. // only change local variables
  3916. attrSetters.width = function (value) {
  3917. width = value;
  3918. return false;
  3919. };
  3920. attrSetters.height = function (value) {
  3921. height = value;
  3922. return false;
  3923. };
  3924. attrSetters.padding = function (value) {
  3925. if (defined(value) && value !== padding) {
  3926. padding = value;
  3927. updateTextPadding();
  3928. }
  3929.  
  3930. return false;
  3931. };
  3932.  
  3933. // change local variable and set attribue as well
  3934. attrSetters.align = function (value) {
  3935. alignFactor = { left: 0, center: 0.5, right: 1 }[value];
  3936. return false; // prevent setting text-anchor on the group
  3937. };
  3938.  
  3939. // apply these to the box and the text alike
  3940. attrSetters.text = function (value, key) {
  3941. text.attr(key, value);
  3942. updateBoxSize();
  3943. updateTextPadding();
  3944. return false;
  3945. };
  3946.  
  3947. // apply these to the box but not to the text
  3948. attrSetters[STROKE_WIDTH] = function (value, key) {
  3949. crispAdjust = value % 2 / 2;
  3950. boxAttr(key, value);
  3951. return false;
  3952. };
  3953. attrSetters.stroke = attrSetters.fill = attrSetters.r = function (value, key) {
  3954. boxAttr(key, value);
  3955. return false;
  3956. };
  3957. attrSetters.anchorX = function (value, key) {
  3958. anchorX = value;
  3959. boxAttr(key, value + crispAdjust - wrapperX);
  3960. return false;
  3961. };
  3962. attrSetters.anchorY = function (value, key) {
  3963. anchorY = value;
  3964. boxAttr(key, value - wrapperY);
  3965. return false;
  3966. };
  3967.  
  3968. // rename attributes
  3969. attrSetters.x = function (value) {
  3970. wrapper.x = value; // for animation getter
  3971. value -= alignFactor * ((width || bBox.width) + padding);
  3972. wrapperX = mathRound(value);
  3973.  
  3974. wrapper.attr('translateX', wrapperX);
  3975. return false;
  3976. };
  3977. attrSetters.y = function (value) {
  3978. wrapperY = wrapper.y = mathRound(value);
  3979. wrapper.attr('translateY', value);
  3980. return false;
  3981. };
  3982.  
  3983. // Redirect certain methods to either the box or the text
  3984. var baseCss = wrapper.css;
  3985. return extend(wrapper, {
  3986. /**
  3987. * Pick up some properties and apply them to the text instead of the wrapper
  3988. */
  3989. css: function (styles) {
  3990. if (styles) {
  3991. var textStyles = {};
  3992. styles = merge({}, styles); // create a copy to avoid altering the original object (#537)
  3993. each(['fontSize', 'fontWeight', 'fontFamily', 'color', 'lineHeight', 'width'], function (prop) {
  3994. if (styles[prop] !== UNDEFINED) {
  3995. textStyles[prop] = styles[prop];
  3996. delete styles[prop];
  3997. }
  3998. });
  3999. text.css(textStyles);
  4000. }
  4001. return baseCss.call(wrapper, styles);
  4002. },
  4003. /**
  4004. * Return the bounding box of the box, not the group
  4005. */
  4006. getBBox: function () {
  4007. return box.getBBox();
  4008. },
  4009. /**
  4010. * Apply the shadow to the box
  4011. */
  4012. shadow: function (b) {
  4013. box.shadow(b);
  4014. return wrapper;
  4015. },
  4016. /**
  4017. * Destroy and release memory.
  4018. */
  4019. destroy: function () {
  4020. removeEvent(wrapper, 'add', getSizeAfterAdd);
  4021.  
  4022. // Added by button implementation
  4023. removeEvent(wrapper.element, 'mouseenter');
  4024. removeEvent(wrapper.element, 'mouseleave');
  4025.  
  4026. if (text) {
  4027. // Destroy the text element
  4028. text = text.destroy();
  4029. }
  4030. // Call base implementation to destroy the rest
  4031. SVGElement.prototype.destroy.call(wrapper);
  4032. }
  4033. });
  4034. }
  4035. }; // end SVGRenderer
  4036.  
  4037.  
  4038. // general renderer
  4039. Renderer = SVGRenderer;
  4040.  
  4041.  
  4042. /* ****************************************************************************
  4043. * *
  4044. * START OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  4045. * *
  4046. * For applications and websites that don't need IE support, like platform *
  4047. * targeted mobile apps and web apps, this code can be removed. *
  4048. * *
  4049. *****************************************************************************/
  4050.  
  4051. /**
  4052. * @constructor
  4053. */
  4054. var VMLRenderer;
  4055. if (!hasSVG && !useCanVG) {
  4056.  
  4057. /**
  4058. * The VML element wrapper.
  4059. */
  4060. var VMLElement = {
  4061.  
  4062. /**
  4063. * Initialize a new VML element wrapper. It builds the markup as a string
  4064. * to minimize DOM traffic.
  4065. * @param {Object} renderer
  4066. * @param {Object} nodeName
  4067. */
  4068. init: function (renderer, nodeName) {
  4069. var wrapper = this,
  4070. markup = ['<', nodeName, ' filled="f" stroked="f"'],
  4071. style = ['position: ', ABSOLUTE, ';'];
  4072.  
  4073. // divs and shapes need size
  4074. if (nodeName === 'shape' || nodeName === DIV) {
  4075. style.push('left:0;top:0;width:1px;height:1px;');
  4076. }
  4077. if (docMode8) {
  4078. style.push('visibility: ', nodeName === DIV ? HIDDEN : VISIBLE);
  4079. }
  4080.  
  4081. markup.push(' style="', style.join(''), '"/>');
  4082.  
  4083. // create element with default attributes and style
  4084. if (nodeName) {
  4085. markup = nodeName === DIV || nodeName === 'span' || nodeName === 'img' ?
  4086. markup.join('')
  4087. : renderer.prepVML(markup);
  4088. wrapper.element = createElement(markup);
  4089. }
  4090.  
  4091. wrapper.renderer = renderer;
  4092. wrapper.attrSetters = {};
  4093. },
  4094.  
  4095. /**
  4096. * Add the node to the given parent
  4097. * @param {Object} parent
  4098. */
  4099. add: function (parent) {
  4100. var wrapper = this,
  4101. renderer = wrapper.renderer,
  4102. element = wrapper.element,
  4103. box = renderer.box,
  4104. inverted = parent && parent.inverted,
  4105.  
  4106. // get the parent node
  4107. parentNode = parent ?
  4108. parent.element || parent :
  4109. box;
  4110.  
  4111.  
  4112. // if the parent group is inverted, apply inversion on all children
  4113. if (inverted) { // only on groups
  4114. renderer.invertChild(element, parentNode);
  4115. }
  4116.  
  4117. // issue #140 workaround - related to #61 and #74
  4118. if (docMode8 && parentNode.gVis === HIDDEN) {
  4119. css(element, { visibility: HIDDEN });
  4120. }
  4121.  
  4122. // append it
  4123. parentNode.appendChild(element);
  4124.  
  4125. // align text after adding to be able to read offset
  4126. wrapper.added = true;
  4127. if (wrapper.alignOnAdd && !wrapper.deferUpdateTransform) {
  4128. wrapper.updateTransform();
  4129. }
  4130.  
  4131. // fire an event for internal hooks
  4132. fireEvent(wrapper, 'add');
  4133.  
  4134. return wrapper;
  4135. },
  4136.  
  4137. /**
  4138. * In IE8 documentMode 8, we need to recursively set the visibility down in the DOM
  4139. * tree for nested groups. Related to #61, #586.
  4140. */
  4141. toggleChildren: function (element, visibility) {
  4142. var childNodes = element.childNodes,
  4143. i = childNodes.length;
  4144.  
  4145. while (i--) {
  4146.  
  4147. // apply the visibility
  4148. css(childNodes[i], { visibility: visibility });
  4149.  
  4150. // we have a nested group, apply it to its children again
  4151. if (childNodes[i].nodeName === 'DIV') {
  4152. this.toggleChildren(childNodes[i], visibility);
  4153. }
  4154. }
  4155. },
  4156.  
  4157. /**
  4158. * VML always uses htmlUpdateTransform
  4159. */
  4160. updateTransform: SVGElement.prototype.htmlUpdateTransform,
  4161.  
  4162. /**
  4163. * Get or set attributes
  4164. */
  4165. attr: function (hash, val) {
  4166. var wrapper = this,
  4167. key,
  4168. value,
  4169. i,
  4170. result,
  4171. element = wrapper.element || {},
  4172. elemStyle = element.style,
  4173. nodeName = element.nodeName,
  4174. renderer = wrapper.renderer,
  4175. symbolName = wrapper.symbolName,
  4176. hasSetSymbolSize,
  4177. shadows = wrapper.shadows,
  4178. skipAttr,
  4179. attrSetters = wrapper.attrSetters,
  4180. ret = wrapper;
  4181.  
  4182. // single key-value pair
  4183. if (isString(hash) && defined(val)) {
  4184. key = hash;
  4185. hash = {};
  4186. hash[key] = val;
  4187. }
  4188.  
  4189. // used as a getter, val is undefined
  4190. if (isString(hash)) {
  4191. key = hash;
  4192. if (key === 'strokeWidth' || key === 'stroke-width') {
  4193. ret = wrapper.strokeweight;
  4194. } else {
  4195. ret = wrapper[key];
  4196. }
  4197.  
  4198. // setter
  4199. } else {
  4200. for (key in hash) {
  4201. value = hash[key];
  4202. skipAttr = false;
  4203.  
  4204. // check for a specific attribute setter
  4205. result = attrSetters[key] && attrSetters[key](value, key);
  4206.  
  4207. if (result !== false && value !== null) { // #620
  4208.  
  4209. if (result !== UNDEFINED) {
  4210. value = result; // the attribute setter has returned a new value to set
  4211. }
  4212.  
  4213.  
  4214. // prepare paths
  4215. // symbols
  4216. if (symbolName && /^(x|y|r|start|end|width|height|innerR|anchorX|anchorY)/.test(key)) {
  4217. // if one of the symbol size affecting parameters are changed,
  4218. // check all the others only once for each call to an element's
  4219. // .attr() method
  4220. if (!hasSetSymbolSize) {
  4221. wrapper.symbolAttr(hash);
  4222.  
  4223. hasSetSymbolSize = true;
  4224. }
  4225. skipAttr = true;
  4226.  
  4227. } else if (key === 'd') {
  4228. value = value || [];
  4229. wrapper.d = value.join(' '); // used in getter for animation
  4230.  
  4231. // convert paths
  4232. i = value.length;
  4233. var convertedPath = [];
  4234. while (i--) {
  4235.  
  4236. // Multiply by 10 to allow subpixel precision.
  4237. // Substracting half a pixel seems to make the coordinates
  4238. // align with SVG, but this hasn't been tested thoroughly
  4239. if (isNumber(value[i])) {
  4240. convertedPath[i] = mathRound(value[i] * 10) - 5;
  4241. } else if (value[i] === 'Z') { // close the path
  4242. convertedPath[i] = 'x';
  4243. } else {
  4244. convertedPath[i] = value[i];
  4245. }
  4246.  
  4247. }
  4248. value = convertedPath.join(' ') || 'x';
  4249. element.path = value;
  4250.  
  4251. // update shadows
  4252. if (shadows) {
  4253. i = shadows.length;
  4254. while (i--) {
  4255. shadows[i].path = shadows[i].cutOff ? this.cutOffPath(value, shadows[i].cutOff) : value;
  4256. }
  4257. }
  4258. skipAttr = true;
  4259.  
  4260. // directly mapped to css
  4261. } else if (key === 'zIndex' || key === 'visibility') {
  4262.  
  4263. // workaround for #61 and #586
  4264. if (docMode8 && key === 'visibility' && nodeName === 'DIV') {
  4265. element.gVis = value;
  4266. wrapper.toggleChildren(element, value);
  4267. if (value === VISIBLE) { // #74
  4268. value = null;
  4269. }
  4270. }
  4271.  
  4272. if (value) {
  4273. elemStyle[key] = value;
  4274. }
  4275.  
  4276.  
  4277.  
  4278. skipAttr = true;
  4279.  
  4280. // width and height
  4281. } else if (key === 'width' || key === 'height') {
  4282.  
  4283. value = mathMax(0, value); // don't set width or height below zero (#311)
  4284.  
  4285. this[key] = value; // used in getter
  4286.  
  4287. // clipping rectangle special
  4288. if (wrapper.updateClipping) {
  4289. wrapper[key] = value;
  4290. wrapper.updateClipping();
  4291. } else {
  4292. // normal
  4293. elemStyle[key] = value;
  4294. }
  4295.  
  4296. skipAttr = true;
  4297.  
  4298. // x and y
  4299. } else if (key === 'x' || key === 'y') {
  4300.  
  4301. wrapper[key] = value; // used in getter
  4302. elemStyle[{ x: 'left', y: 'top' }[key]] = value;
  4303.  
  4304. // class name
  4305. } else if (key === 'class') {
  4306. // IE8 Standards mode has problems retrieving the className
  4307. element.className = value;
  4308.  
  4309. // stroke
  4310. } else if (key === 'stroke') {
  4311.  
  4312. value = renderer.color(value, element, key);
  4313.  
  4314. key = 'strokecolor';
  4315.  
  4316. // stroke width
  4317. } else if (key === 'stroke-width' || key === 'strokeWidth') {
  4318. element.stroked = value ? true : false;
  4319. key = 'strokeweight';
  4320. wrapper[key] = value; // used in getter, issue #113
  4321. if (isNumber(value)) {
  4322. value += PX;
  4323. }
  4324.  
  4325. // dashStyle
  4326. } else if (key === 'dashstyle') {
  4327. var strokeElem = element.getElementsByTagName('stroke')[0] ||
  4328. createElement(renderer.prepVML(['<stroke/>']), null, null, element);
  4329. strokeElem[key] = value || 'solid';
  4330. wrapper.dashstyle = value; /* because changing stroke-width will change the dash length
  4331. and cause an epileptic effect */
  4332. skipAttr = true;
  4333.  
  4334. // fill
  4335. } else if (key === 'fill') {
  4336.  
  4337. if (nodeName === 'SPAN') { // text color
  4338. elemStyle.color = value;
  4339. } else {
  4340. element.filled = value !== NONE ? true : false;
  4341.  
  4342. value = renderer.color(value, element, key);
  4343.  
  4344. key = 'fillcolor';
  4345. }
  4346.  
  4347. // rotation on VML elements
  4348. } else if (nodeName === 'shape' && key === 'rotation') {
  4349. wrapper[key] = value;
  4350.  
  4351. // translation for animation
  4352. } else if (key === 'translateX' || key === 'translateY' || key === 'rotation') {
  4353. wrapper[key] = value;
  4354. wrapper.updateTransform();
  4355.  
  4356. skipAttr = true;
  4357.  
  4358. // text for rotated and non-rotated elements
  4359. } else if (key === 'text') {
  4360. this.bBox = null;
  4361. element.innerHTML = value;
  4362. skipAttr = true;
  4363. }
  4364.  
  4365. // let the shadow follow the main element
  4366. if (shadows && key === 'visibility') {
  4367. i = shadows.length;
  4368. while (i--) {
  4369. shadows[i].style[key] = value;
  4370. }
  4371. }
  4372.  
  4373.  
  4374.  
  4375. if (!skipAttr) {
  4376. if (docMode8) { // IE8 setAttribute bug
  4377. element[key] = value;
  4378. } else {
  4379. attr(element, key, value);
  4380. }
  4381. }
  4382.  
  4383. }
  4384. }
  4385. }
  4386. return ret;
  4387. },
  4388.  
  4389. /**
  4390. * Set the element's clipping to a predefined rectangle
  4391. *
  4392. * @param {String} id The id of the clip rectangle
  4393. */
  4394. clip: function (clipRect) {
  4395. var wrapper = this,
  4396. clipMembers = clipRect.members,
  4397. element = wrapper.element,
  4398. parentNode = element.parentNode;
  4399.  
  4400. clipMembers.push(wrapper);
  4401. wrapper.destroyClip = function () {
  4402. erase(clipMembers, wrapper);
  4403. };
  4404.  
  4405. // Issue #863 workaround - related to #140, #61, #74
  4406. if (parentNode && parentNode.className === 'highcharts-tracker' && !docMode8) {
  4407. css(element, { visibility: HIDDEN });
  4408. }
  4409.  
  4410. return wrapper.css(clipRect.getCSS(wrapper));
  4411. },
  4412.  
  4413. /**
  4414. * Set styles for the element
  4415. * @param {Object} styles
  4416. */
  4417. css: SVGElement.prototype.htmlCss,
  4418.  
  4419. /**
  4420. * Removes a child either by removeChild or move to garbageBin.
  4421. * Issue 490; in VML removeChild results in Orphaned nodes according to sIEve, discardElement does not.
  4422. */
  4423. safeRemoveChild: function (element) {
  4424. // discardElement will detach the node from its parent before attaching it
  4425. // to the garbage bin. Therefore it is important that the node is attached and have parent.
  4426. var parentNode = element.parentNode;
  4427. if (parentNode) {
  4428. discardElement(element);
  4429. }
  4430. },
  4431.  
  4432. /**
  4433. * Extend element.destroy by removing it from the clip members array
  4434. */
  4435. destroy: function () {
  4436. var wrapper = this;
  4437.  
  4438. if (wrapper.destroyClip) {
  4439. wrapper.destroyClip();
  4440. }
  4441.  
  4442. return SVGElement.prototype.destroy.apply(wrapper);
  4443. },
  4444.  
  4445. /**
  4446. * Remove all child nodes of a group, except the v:group element
  4447. */
  4448. empty: function () {
  4449. var element = this.element,
  4450. childNodes = element.childNodes,
  4451. i = childNodes.length,
  4452. node;
  4453.  
  4454. while (i--) {
  4455. node = childNodes[i];
  4456. node.parentNode.removeChild(node);
  4457. }
  4458. },
  4459.  
  4460. /**
  4461. * Add an event listener. VML override for normalizing event parameters.
  4462. * @param {String} eventType
  4463. * @param {Function} handler
  4464. */
  4465. on: function (eventType, handler) {
  4466. // simplest possible event model for internal use
  4467. this.element['on' + eventType] = function () {
  4468. var evt = win.event;
  4469. evt.target = evt.srcElement;
  4470. handler(evt);
  4471. };
  4472. return this;
  4473. },
  4474.  
  4475. /**
  4476. * In stacked columns, cut off the shadows so that they don't overlap
  4477. */
  4478. cutOffPath: function (path, length) {
  4479.  
  4480. var len;
  4481.  
  4482. path = path.split(/[ ,]/);
  4483. len = path.length;
  4484.  
  4485. if (len === 9 || len === 11) {
  4486. path[len - 4] = path[len - 2] = pInt(path[len - 2]) - 10 * length;
  4487. }
  4488. return path.join(' ');
  4489. },
  4490.  
  4491. /**
  4492. * Apply a drop shadow by copying elements and giving them different strokes
  4493. * @param {Boolean} apply
  4494. */
  4495. shadow: function (apply, group, cutOff) {
  4496. var shadows = [],
  4497. i,
  4498. element = this.element,
  4499. renderer = this.renderer,
  4500. shadow,
  4501. elemStyle = element.style,
  4502. markup,
  4503. path = element.path,
  4504. strokeWidth,
  4505. modifiedPath;
  4506.  
  4507. // some times empty paths are not strings
  4508. if (path && typeof path.value !== 'string') {
  4509. path = 'x';
  4510. }
  4511. modifiedPath = path;
  4512.  
  4513. if (apply) {
  4514. for (i = 1; i <= 3; i++) {
  4515.  
  4516. strokeWidth = 7 - 2 * i;
  4517.  
  4518. // Cut off shadows for stacked column items
  4519. if (cutOff) {
  4520. modifiedPath = this.cutOffPath(path.value, strokeWidth + 0.5);
  4521. }
  4522.  
  4523. markup = ['<shape isShadow="true" strokeweight="', (7 - 2 * i),
  4524. '" filled="false" path="', modifiedPath,
  4525. '" coordsize="10 10" style="', element.style.cssText, '" />'];
  4526.  
  4527. shadow = createElement(renderer.prepVML(markup),
  4528. null, {
  4529. left: pInt(elemStyle.left) + 1,
  4530. top: pInt(elemStyle.top) + 1
  4531. }
  4532. );
  4533. if (cutOff) {
  4534. shadow.cutOff = strokeWidth + 1;
  4535. }
  4536.  
  4537. // apply the opacity
  4538. markup = ['<stroke color="black" opacity="', (0.05 * i), '"/>'];
  4539. createElement(renderer.prepVML(markup), null, null, shadow);
  4540.  
  4541.  
  4542. // insert it
  4543. if (group) {
  4544. group.element.appendChild(shadow);
  4545. } else {
  4546. element.parentNode.insertBefore(shadow, element);
  4547. }
  4548.  
  4549. // record it
  4550. shadows.push(shadow);
  4551.  
  4552. }
  4553.  
  4554. this.shadows = shadows;
  4555. }
  4556. return this;
  4557.  
  4558. }
  4559. };
  4560. VMLElement = extendClass(SVGElement, VMLElement);
  4561.  
  4562. /**
  4563. * The VML renderer
  4564. */
  4565. var VMLRendererExtension = { // inherit SVGRenderer
  4566.  
  4567. Element: VMLElement,
  4568. isIE8: userAgent.indexOf('MSIE 8.0') > -1,
  4569.  
  4570.  
  4571. /**
  4572. * Initialize the VMLRenderer
  4573. * @param {Object} container
  4574. * @param {Number} width
  4575. * @param {Number} height
  4576. */
  4577. init: function (container, width, height) {
  4578. var renderer = this,
  4579. boxWrapper,
  4580. box;
  4581.  
  4582. renderer.alignedObjects = [];
  4583.  
  4584. boxWrapper = renderer.createElement(DIV);
  4585. box = boxWrapper.element;
  4586. box.style.position = RELATIVE; // for freeform drawing using renderer directly
  4587. container.appendChild(boxWrapper.element);
  4588.  
  4589.  
  4590. // generate the containing box
  4591. renderer.box = box;
  4592. renderer.boxWrapper = boxWrapper;
  4593.  
  4594.  
  4595. renderer.setSize(width, height, false);
  4596.  
  4597. // The only way to make IE6 and IE7 print is to use a global namespace. However,
  4598. // with IE8 the only way to make the dynamic shapes visible in screen and print mode
  4599. // seems to be to add the xmlns attribute and the behaviour style inline.
  4600. if (!doc.namespaces.hcv) {
  4601.  
  4602. doc.namespaces.add('hcv', 'urn:schemas-microsoft-com:vml');
  4603.  
  4604. // setup default css
  4605. doc.createStyleSheet().cssText =
  4606. 'hcv\\:fill, hcv\\:path, hcv\\:shape, hcv\\:stroke' +
  4607. '{ behavior:url(#default#VML); display: inline-block; } ';
  4608.  
  4609. }
  4610. },
  4611.  
  4612.  
  4613. /**
  4614. * Detect whether the renderer is hidden. This happens when one of the parent elements
  4615. * has display: none
  4616. */
  4617. isHidden: function () {
  4618. return !this.box.offsetWidth;
  4619. },
  4620.  
  4621. /**
  4622. * Define a clipping rectangle. In VML it is accomplished by storing the values
  4623. * for setting the CSS style to all associated members.
  4624. *
  4625. * @param {Number} x
  4626. * @param {Number} y
  4627. * @param {Number} width
  4628. * @param {Number} height
  4629. */
  4630. clipRect: function (x, y, width, height) {
  4631.  
  4632. // create a dummy element
  4633. var clipRect = this.createElement();
  4634.  
  4635. // mimic a rectangle with its style object for automatic updating in attr
  4636. return extend(clipRect, {
  4637. members: [],
  4638. left: x,
  4639. top: y,
  4640. width: width,
  4641. height: height,
  4642. getCSS: function (wrapper) {
  4643. var inverted = wrapper.inverted,
  4644. rect = this,
  4645. top = rect.top,
  4646. left = rect.left,
  4647. right = left + rect.width,
  4648. bottom = top + rect.height,
  4649. ret = {
  4650. clip: 'rect(' +
  4651. mathRound(inverted ? left : top) + 'px,' +
  4652. mathRound(inverted ? bottom : right) + 'px,' +
  4653. mathRound(inverted ? right : bottom) + 'px,' +
  4654. mathRound(inverted ? top : left) + 'px)'
  4655. };
  4656.  
  4657. // issue 74 workaround
  4658. if (!inverted && docMode8 && wrapper.element.nodeName !== 'IMG') {
  4659. extend(ret, {
  4660. width: right + PX,
  4661. height: bottom + PX
  4662. });
  4663. }
  4664.  
  4665. return ret;
  4666. },
  4667.  
  4668. // used in attr and animation to update the clipping of all members
  4669. updateClipping: function () {
  4670. each(clipRect.members, function (member) {
  4671. member.css(clipRect.getCSS(member));
  4672. });
  4673. }
  4674. });
  4675.  
  4676. },
  4677.  
  4678.  
  4679. /**
  4680. * Take a color and return it if it's a string, make it a gradient if it's a
  4681. * gradient configuration object, and apply opacity.
  4682. *
  4683. * @param {Object} color The color or config object
  4684. */
  4685. color: function (color, elem, prop) {
  4686. var colorObject,
  4687. regexRgba = /^rgba/,
  4688. markup,
  4689. fillType,
  4690. ret = NONE;
  4691.  
  4692. // Check for linear or radial gradient
  4693. if (color && color.linearGradient) {
  4694. fillType = 'gradient';
  4695. } else if (color && color.radialGradient) {
  4696. fillType = 'pattern';
  4697. }
  4698.  
  4699.  
  4700. if (fillType) {
  4701.  
  4702. var stopColor,
  4703. stopOpacity,
  4704. gradient = color.linearGradient || color.radialGradient,
  4705. x1,
  4706. y1,
  4707. x2,
  4708. y2,
  4709. angle,
  4710. opacity1,
  4711. opacity2,
  4712. color1,
  4713. color2,
  4714. fillAttr = '',
  4715. stops = color.stops,
  4716. firstStop,
  4717. lastStop,
  4718. colors = [];
  4719.  
  4720. // Extend from 0 to 1
  4721. firstStop = stops[0];
  4722. lastStop = stops[stops.length - 1];
  4723. if (firstStop[0] > 0) {
  4724. stops.unshift([
  4725. 0,
  4726. firstStop[1]
  4727. ]);
  4728. }
  4729. if (lastStop[0] < 1) {
  4730. stops.push([
  4731. 1,
  4732. lastStop[1]
  4733. ]);
  4734. }
  4735.  
  4736. // Compute the stops
  4737. each(stops, function (stop, i) {
  4738. if (regexRgba.test(stop[1])) {
  4739. colorObject = Color(stop[1]);
  4740. stopColor = colorObject.get('rgb');
  4741. stopOpacity = colorObject.get('a');
  4742. } else {
  4743. stopColor = stop[1];
  4744. stopOpacity = 1;
  4745. }
  4746.  
  4747. // Build the color attribute
  4748. colors.push((stop[0] * 100) + '% ' + stopColor);
  4749.  
  4750. // Only start and end opacities are allowed, so we use the first and the last
  4751. if (!i) {
  4752. opacity1 = stopOpacity;
  4753. color2 = stopColor;
  4754. } else {
  4755. opacity2 = stopOpacity;
  4756. color1 = stopColor;
  4757. }
  4758. });
  4759.  
  4760. // Handle linear gradient angle
  4761. if (fillType === 'gradient') {
  4762. x1 = gradient.x1 || gradient[0] || 0;
  4763. y1 = gradient.y1 || gradient[1] || 0;
  4764. x2 = gradient.x2 || gradient[2] || 0;
  4765. y2 = gradient.y2 || gradient[3] || 0;
  4766. angle = 90 - math.atan(
  4767. (y2 - y1) / // y vector
  4768. (x2 - x1) // x vector
  4769. ) * 180 / mathPI;
  4770.  
  4771. // Radial (circular) gradient
  4772. } else {
  4773. // pie: http://jsfiddle.net/highcharts/66g8H/
  4774. // reference: http://jsfiddle.net/highcharts/etznJ/
  4775. // http://jsfiddle.net/highcharts/XRbCc/
  4776. // http://jsfiddle.net/highcharts/F3fwR/
  4777. // TODO:
  4778. // - correct for radialRefeence
  4779. // - check whether gradient stops are supported
  4780. // - add global option for gradient image (must relate to version)
  4781. var r = gradient.r,
  4782. size = r * 2,
  4783. cx = gradient.cx,
  4784. cy = gradient.cy;
  4785. //radialReference = elem.radialReference;
  4786.  
  4787. //if (radialReference) {
  4788. // Try setting pixel size, or other way to adjust the gradient size to the bounding box
  4789. //}
  4790. fillAttr = 'src="http://code.highcharts.com/gfx/radial-gradient.png" ' +
  4791. 'size="' + size + ',' + size + '" ' +
  4792. 'origin="0.5,0.5" ' +
  4793. 'position="' + cx + ',' + cy + '" ' +
  4794. 'color2="' + color2 + '" ';
  4795.  
  4796. // The fill element's color attribute is broken in IE8 standards mode, so we
  4797. // need to set the parent shape's fillcolor attribute instead.
  4798. ret = color1;
  4799. }
  4800.  
  4801.  
  4802.  
  4803. // Apply the gradient to fills only.
  4804. if (prop === 'fill') {
  4805.  
  4806. // when colors attribute is used, the meanings of opacity and o:opacity2
  4807. // are reversed.
  4808. markup = ['<fill colors="' + colors.join(',') + '" angle="', angle,
  4809. '" opacity="', opacity2, '" o:opacity2="', opacity1,
  4810. '" type="', fillType, '" ', fillAttr, 'focus="100%" method="any" />'];
  4811. createElement(this.prepVML(markup), null, null, elem);
  4812.  
  4813. // Gradients are not supported for VML stroke, return the first color. #722.
  4814. } else {
  4815. ret = stopColor;
  4816. }
  4817.  
  4818.  
  4819. // if the color is an rgba color, split it and add a fill node
  4820. // to hold the opacity component
  4821. } else if (regexRgba.test(color) && elem.tagName !== 'IMG') {
  4822.  
  4823. colorObject = Color(color);
  4824.  
  4825. markup = ['<', prop, ' opacity="', colorObject.get('a'), '"/>'];
  4826. createElement(this.prepVML(markup), null, null, elem);
  4827.  
  4828. ret = colorObject.get('rgb');
  4829.  
  4830.  
  4831. } else {
  4832. var strokeNodes = elem.getElementsByTagName(prop);
  4833. if (strokeNodes.length) {
  4834. strokeNodes[0].opacity = 1;
  4835. }
  4836. ret = color;
  4837. }
  4838.  
  4839. return ret;
  4840. },
  4841.  
  4842. /**
  4843. * Take a VML string and prepare it for either IE8 or IE6/IE7.
  4844. * @param {Array} markup A string array of the VML markup to prepare
  4845. */
  4846. prepVML: function (markup) {
  4847. var vmlStyle = 'display:inline-block;behavior:url(#default#VML);',
  4848. isIE8 = this.isIE8;
  4849.  
  4850. markup = markup.join('');
  4851.  
  4852. if (isIE8) { // add xmlns and style inline
  4853. markup = markup.replace('/>', ' xmlns="urn:schemas-microsoft-com:vml" />');
  4854. if (markup.indexOf('style="') === -1) {
  4855. markup = markup.replace('/>', ' style="' + vmlStyle + '" />');
  4856. } else {
  4857. markup = markup.replace('style="', 'style="' + vmlStyle);
  4858. }
  4859.  
  4860. } else { // add namespace
  4861. markup = markup.replace('<', '<hcv:');
  4862. }
  4863.  
  4864. return markup;
  4865. },
  4866.  
  4867. /**
  4868. * Create rotated and aligned text
  4869. * @param {String} str
  4870. * @param {Number} x
  4871. * @param {Number} y
  4872. */
  4873. text: SVGRenderer.prototype.html,
  4874.  
  4875. /**
  4876. * Create and return a path element
  4877. * @param {Array} path
  4878. */
  4879. path: function (path) {
  4880. var attr = {
  4881. // subpixel precision down to 0.1 (width and height = 1px)
  4882. coordsize: '10 10'
  4883. };
  4884. if (isArray(path)) {
  4885. attr.d = path;
  4886. } else if (isObject(path)) { // attributes
  4887. extend(attr, path);
  4888. }
  4889. // create the shape
  4890. return this.createElement('shape').attr(attr);
  4891. },
  4892.  
  4893. /**
  4894. * Create and return a circle element. In VML circles are implemented as
  4895. * shapes, which is faster than v:oval
  4896. * @param {Number} x
  4897. * @param {Number} y
  4898. * @param {Number} r
  4899. */
  4900. circle: function (x, y, r) {
  4901. return this.symbol('circle').attr({ x: x - r, y: y - r, width: 2 * r, height: 2 * r });
  4902. },
  4903.  
  4904. /**
  4905. * Create a group using an outer div and an inner v:group to allow rotating
  4906. * and flipping. A simple v:group would have problems with positioning
  4907. * child HTML elements and CSS clip.
  4908. *
  4909. * @param {String} name The name of the group
  4910. */
  4911. g: function (name) {
  4912. var wrapper,
  4913. attribs;
  4914.  
  4915. // set the class name
  4916. if (name) {
  4917. attribs = { 'className': PREFIX + name, 'class': PREFIX + name };
  4918. }
  4919.  
  4920. // the div to hold HTML and clipping
  4921. wrapper = this.createElement(DIV).attr(attribs);
  4922.  
  4923. return wrapper;
  4924. },
  4925.  
  4926. /**
  4927. * VML override to create a regular HTML image
  4928. * @param {String} src
  4929. * @param {Number} x
  4930. * @param {Number} y
  4931. * @param {Number} width
  4932. * @param {Number} height
  4933. */
  4934. image: function (src, x, y, width, height) {
  4935. var obj = this.createElement('img')
  4936. .attr({ src: src });
  4937.  
  4938. if (arguments.length > 1) {
  4939. obj.css({
  4940. left: x,
  4941. top: y,
  4942. width: width,
  4943. height: height
  4944. });
  4945. }
  4946. return obj;
  4947. },
  4948.  
  4949. /**
  4950. * VML uses a shape for rect to overcome bugs and rotation problems
  4951. */
  4952. rect: function (x, y, width, height, r, strokeWidth) {
  4953.  
  4954. if (isObject(x)) {
  4955. y = x.y;
  4956. width = x.width;
  4957. height = x.height;
  4958. strokeWidth = x.strokeWidth;
  4959. x = x.x;
  4960. }
  4961. var wrapper = this.symbol('rect');
  4962. wrapper.r = r;
  4963.  
  4964. return wrapper.attr(wrapper.crisp(strokeWidth, x, y, mathMax(width, 0), mathMax(height, 0)));
  4965. },
  4966.  
  4967. /**
  4968. * In the VML renderer, each child of an inverted div (group) is inverted
  4969. * @param {Object} element
  4970. * @param {Object} parentNode
  4971. */
  4972. invertChild: function (element, parentNode) {
  4973. var parentStyle = parentNode.style;
  4974. css(element, {
  4975. flip: 'x',
  4976. left: pInt(parentStyle.width) - 1,
  4977. top: pInt(parentStyle.height) - 1,
  4978. rotation: -90
  4979. });
  4980. },
  4981.  
  4982. /**
  4983. * Symbol definitions that override the parent SVG renderer's symbols
  4984. *
  4985. */
  4986. symbols: {
  4987. // VML specific arc function
  4988. arc: function (x, y, w, h, options) {
  4989. var start = options.start,
  4990. end = options.end,
  4991. radius = options.r || w || h,
  4992. cosStart = mathCos(start),
  4993. sinStart = mathSin(start),
  4994. cosEnd = mathCos(end),
  4995. sinEnd = mathSin(end),
  4996. innerRadius = options.innerR,
  4997. circleCorrection = 0.08 / radius, // #760
  4998. innerCorrection = (innerRadius && 0.1 / innerRadius) || 0,
  4999. ret;
  5000.  
  5001. if (end - start === 0) { // no angle, don't show it.
  5002. return ['x'];
  5003.  
  5004. } else if (2 * mathPI - end + start < circleCorrection) { // full circle
  5005. // empirical correction found by trying out the limits for different radii
  5006. cosEnd = -circleCorrection;
  5007. } else if (end - start < innerCorrection) { // issue #186, another mysterious VML arc problem
  5008. cosEnd = mathCos(start + innerCorrection);
  5009. }
  5010.  
  5011. ret = [
  5012. 'wa', // clockwise arc to
  5013. x - radius, // left
  5014. y - radius, // top
  5015. x + radius, // right
  5016. y + radius, // bottom
  5017. x + radius * cosStart, // start x
  5018. y + radius * sinStart, // start y
  5019. x + radius * cosEnd, // end x
  5020. y + radius * sinEnd // end y
  5021. ];
  5022.  
  5023. if (options.open) {
  5024. ret.push(
  5025. M,
  5026. x - innerRadius,
  5027. y - innerRadius
  5028. );
  5029. }
  5030.  
  5031. ret.push(
  5032. 'at', // anti clockwise arc to
  5033. x - innerRadius, // left
  5034. y - innerRadius, // top
  5035. x + innerRadius, // right
  5036. y + innerRadius, // bottom
  5037. x + innerRadius * cosEnd, // start x
  5038. y + innerRadius * sinEnd, // start y
  5039. x + innerRadius * cosStart, // end x
  5040. y + innerRadius * sinStart, // end y
  5041. 'x', // finish path
  5042. 'e' // close
  5043. );
  5044.  
  5045. return ret;
  5046.  
  5047. },
  5048. // Add circle symbol path. This performs significantly faster than v:oval.
  5049. circle: function (x, y, w, h) {
  5050.  
  5051. return [
  5052. 'wa', // clockwisearcto
  5053. x, // left
  5054. y, // top
  5055. x + w, // right
  5056. y + h, // bottom
  5057. x + w, // start x
  5058. y + h / 2, // start y
  5059. x + w, // end x
  5060. y + h / 2, // end y
  5061. //'x', // finish path
  5062. 'e' // close
  5063. ];
  5064. },
  5065. /**
  5066. * Add rectangle symbol path which eases rotation and omits arcsize problems
  5067. * compared to the built-in VML roundrect shape
  5068. *
  5069. * @param {Number} left Left position
  5070. * @param {Number} top Top position
  5071. * @param {Number} r Border radius
  5072. * @param {Object} options Width and height
  5073. */
  5074.  
  5075. rect: function (left, top, width, height, options) {
  5076.  
  5077. var right = left + width,
  5078. bottom = top + height,
  5079. ret,
  5080. r;
  5081.  
  5082. // No radius, return the more lightweight square
  5083. if (!defined(options) || !options.r) {
  5084. ret = SVGRenderer.prototype.symbols.square.apply(0, arguments);
  5085.  
  5086. // Has radius add arcs for the corners
  5087. } else {
  5088.  
  5089. r = mathMin(options.r, width, height);
  5090. ret = [
  5091. M,
  5092. left + r, top,
  5093.  
  5094. L,
  5095. right - r, top,
  5096. 'wa',
  5097. right - 2 * r, top,
  5098. right, top + 2 * r,
  5099. right - r, top,
  5100. right, top + r,
  5101.  
  5102. L,
  5103. right, bottom - r,
  5104. 'wa',
  5105. right - 2 * r, bottom - 2 * r,
  5106. right, bottom,
  5107. right, bottom - r,
  5108. right - r, bottom,
  5109.  
  5110. L,
  5111. left + r, bottom,
  5112. 'wa',
  5113. left, bottom - 2 * r,
  5114. left + 2 * r, bottom,
  5115. left + r, bottom,
  5116. left, bottom - r,
  5117.  
  5118. L,
  5119. left, top + r,
  5120. 'wa',
  5121. left, top,
  5122. left + 2 * r, top + 2 * r,
  5123. left, top + r,
  5124. left + r, top,
  5125.  
  5126.  
  5127. 'x',
  5128. 'e'
  5129. ];
  5130. }
  5131. return ret;
  5132. }
  5133. }
  5134. };
  5135. VMLRenderer = function () {
  5136. this.init.apply(this, arguments);
  5137. };
  5138. VMLRenderer.prototype = merge(SVGRenderer.prototype, VMLRendererExtension);
  5139.  
  5140. // general renderer
  5141. Renderer = VMLRenderer;
  5142. }
  5143.  
  5144. /* ****************************************************************************
  5145. * *
  5146. * END OF INTERNET EXPLORER <= 8 SPECIFIC CODE *
  5147. * *
  5148. *****************************************************************************/
  5149. /* ****************************************************************************
  5150. * *
  5151. * START OF ANDROID < 3 SPECIFIC CODE. THIS CAN BE REMOVED IF YOU'RE NOT *
  5152. * TARGETING THAT SYSTEM. *
  5153. * *
  5154. *****************************************************************************/
  5155. var CanVGRenderer,
  5156. CanVGController;
  5157.  
  5158. if (useCanVG) {
  5159. /**
  5160. * The CanVGRenderer is empty from start to keep the source footprint small.
  5161. * When requested, the CanVGController downloads the rest of the source packaged
  5162. * together with the canvg library.
  5163. */
  5164. CanVGRenderer = function () {
  5165. // Empty constructor
  5166. };
  5167.  
  5168. /**
  5169. * Start with an empty symbols object. This is needed when exporting is used (exporting.src.js will add a few symbols), but
  5170. * the implementation from SvgRenderer will not be merged in until first render.
  5171. */
  5172. CanVGRenderer.prototype.symbols = {};
  5173.  
  5174. /**
  5175. * Handles on demand download of canvg rendering support.
  5176. */
  5177. CanVGController = (function () {
  5178. // List of renderering calls
  5179. var deferredRenderCalls = [];
  5180.  
  5181. /**
  5182. * When downloaded, we are ready to draw deferred charts.
  5183. */
  5184. function drawDeferred() {
  5185. var callLength = deferredRenderCalls.length,
  5186. callIndex;
  5187.  
  5188. // Draw all pending render calls
  5189. for (callIndex = 0; callIndex < callLength; callIndex++) {
  5190. deferredRenderCalls[callIndex]();
  5191. }
  5192. // Clear the list
  5193. deferredRenderCalls = [];
  5194. }
  5195.  
  5196. return {
  5197. push: function (func, scriptLocation) {
  5198. // Only get the script once
  5199. if (deferredRenderCalls.length === 0) {
  5200. getScript(scriptLocation, drawDeferred);
  5201. }
  5202. // Register render call
  5203. deferredRenderCalls.push(func);
  5204. }
  5205. };
  5206. }());
  5207. } // end CanVGRenderer
  5208.  
  5209. /* ****************************************************************************
  5210. * *
  5211. * END OF ANDROID < 3 SPECIFIC CODE *
  5212. * *
  5213. *****************************************************************************/
  5214.  
  5215. /**
  5216. * General renderer
  5217. */
  5218. Renderer = VMLRenderer || CanVGRenderer || SVGRenderer;
  5219. /**
  5220. * The Tick class
  5221. */
  5222. function Tick(axis, pos, type) {
  5223. this.axis = axis;
  5224. this.pos = pos;
  5225. this.type = type || '';
  5226. this.isNew = true;
  5227.  
  5228. if (!type) {
  5229. this.addLabel();
  5230. }
  5231. }
  5232.  
  5233. Tick.prototype = {
  5234. /**
  5235. * Write the tick label
  5236. */
  5237. addLabel: function () {
  5238. var tick = this,
  5239. axis = tick.axis,
  5240. options = axis.options,
  5241. chart = axis.chart,
  5242. horiz = axis.horiz,
  5243. categories = axis.categories,
  5244. pos = tick.pos,
  5245. labelOptions = options.labels,
  5246. str,
  5247. tickPositions = axis.tickPositions,
  5248. width = (categories && horiz && categories.length &&
  5249. !labelOptions.step && !labelOptions.staggerLines &&
  5250. !labelOptions.rotation &&
  5251. chart.plotWidth / tickPositions.length) ||
  5252. (!horiz && chart.plotWidth / 2),
  5253. isFirst = pos === tickPositions[0],
  5254. isLast = pos === tickPositions[tickPositions.length - 1],
  5255. css,
  5256. attr,
  5257. value = categories && defined(categories[pos]) ? categories[pos] : pos,
  5258. label = tick.label,
  5259. tickPositionInfo = tickPositions.info,
  5260. dateTimeLabelFormat;
  5261.  
  5262. // Set the datetime label format. If a higher rank is set for this position, use that. If not,
  5263. // use the general format.
  5264. if (axis.isDatetimeAxis && tickPositionInfo) {
  5265. dateTimeLabelFormat = options.dateTimeLabelFormats[tickPositionInfo.higherRanks[pos] || tickPositionInfo.unitName];
  5266. }
  5267.  
  5268. // set properties for access in render method
  5269. tick.isFirst = isFirst;
  5270. tick.isLast = isLast;
  5271.  
  5272. // get the string
  5273. str = axis.labelFormatter.call({
  5274. axis: axis,
  5275. chart: chart,
  5276. isFirst: isFirst,
  5277. isLast: isLast,
  5278. dateTimeLabelFormat: dateTimeLabelFormat,
  5279. value: axis.isLog ? correctFloat(lin2log(value)) : value
  5280. });
  5281.  
  5282. // prepare CSS
  5283. css = width && { width: mathMax(1, mathRound(width - 2 * (labelOptions.padding || 10))) + PX };
  5284. css = extend(css, labelOptions.style);
  5285.  
  5286. // first call
  5287. if (!defined(label)) {
  5288. attr = {
  5289. align: labelOptions.align
  5290. };
  5291. if (isNumber(labelOptions.rotation)) {
  5292. attr.rotation = labelOptions.rotation;
  5293. }
  5294. tick.label =
  5295. defined(str) && labelOptions.enabled ?
  5296. chart.renderer.text(
  5297. str,
  5298. 0,
  5299. 0,
  5300. labelOptions.useHTML
  5301. )
  5302. .attr(attr)
  5303. // without position absolute, IE export sometimes is wrong
  5304. .css(css)
  5305. .add(axis.axisGroup) :
  5306. null;
  5307.  
  5308. // update
  5309. } else if (label) {
  5310. label.attr({
  5311. text: str
  5312. })
  5313. .css(css);
  5314. }
  5315. },
  5316.  
  5317. /**
  5318. * Get the offset height or width of the label
  5319. */
  5320. getLabelSize: function () {
  5321. var label = this.label,
  5322. axis = this.axis;
  5323. return label ?
  5324. ((this.labelBBox = label.getBBox(true)))[axis.horiz ? 'height' : 'width'] :
  5325. 0;
  5326. },
  5327.  
  5328. /**
  5329. * Find how far the labels extend to the right and left of the tick's x position. Used for anti-collision
  5330. * detection with overflow logic.
  5331. */
  5332. getLabelSides: function () {
  5333. var bBox = this.labelBBox, // assume getLabelSize has run at this point
  5334. axis = this.axis,
  5335. options = axis.options,
  5336. labelOptions = options.labels,
  5337. width = bBox.width,
  5338. leftSide = width * { left: 0, center: 0.5, right: 1 }[labelOptions.align] - labelOptions.x;
  5339.  
  5340. return [-leftSide, width - leftSide];
  5341. },
  5342.  
  5343. /**
  5344. * Handle the label overflow by adjusting the labels to the left and right edge, or
  5345. * hide them if they collide into the neighbour label.
  5346. */
  5347. handleOverflow: function (index, xy) {
  5348. var show = true,
  5349. axis = this.axis,
  5350. chart = axis.chart,
  5351. isFirst = this.isFirst,
  5352. isLast = this.isLast,
  5353. x = xy.x,
  5354. reversed = axis.reversed,
  5355. tickPositions = axis.tickPositions;
  5356.  
  5357. if (isFirst || isLast) {
  5358.  
  5359. var sides = this.getLabelSides(),
  5360. leftSide = sides[0],
  5361. rightSide = sides[1],
  5362. plotLeft = chart.plotLeft,
  5363. plotRight = plotLeft + axis.len,
  5364. neighbour = axis.ticks[tickPositions[index + (isFirst ? 1 : -1)]],
  5365. neighbourEdge = neighbour && neighbour.label.xy.x + neighbour.getLabelSides()[isFirst ? 0 : 1];
  5366.  
  5367. if ((isFirst && !reversed) || (isLast && reversed)) {
  5368. // Is the label spilling out to the left of the plot area?
  5369. if (x + leftSide < plotLeft) {
  5370.  
  5371. // Align it to plot left
  5372. x = plotLeft - leftSide;
  5373.  
  5374. // Hide it if it now overlaps the neighbour label
  5375. if (neighbour && x + rightSide > neighbourEdge) {
  5376. show = false;
  5377. }
  5378. }
  5379.  
  5380. } else {
  5381. // Is the label spilling out to the right of the plot area?
  5382. if (x + rightSide > plotRight) {
  5383.  
  5384. // Align it to plot right
  5385. x = plotRight - rightSide;
  5386.  
  5387. // Hide it if it now overlaps the neighbour label
  5388. if (neighbour && x + leftSide < neighbourEdge) {
  5389. show = false;
  5390. }
  5391.  
  5392. }
  5393. }
  5394.  
  5395. // Set the modified x position of the label
  5396. xy.x = x;
  5397. }
  5398. return show;
  5399. },
  5400.  
  5401. /**
  5402. * Get the x and y position for ticks and labels
  5403. */
  5404. getPosition: function (horiz, pos, tickmarkOffset, old) {
  5405. var axis = this.axis,
  5406. chart = axis.chart,
  5407. cHeight = (old && chart.oldChartHeight) || chart.chartHeight;
  5408.  
  5409. return {
  5410. x: horiz ?
  5411. axis.translate(pos + tickmarkOffset, null, null, old) + axis.transB :
  5412. axis.left + axis.offset + (axis.opposite ? ((old && chart.oldChartWidth) || chart.chartWidth) - axis.right - axis.left : 0),
  5413.  
  5414. y: horiz ?
  5415. cHeight - axis.bottom + axis.offset - (axis.opposite ? axis.height : 0) :
  5416. cHeight - axis.translate(pos + tickmarkOffset, null, null, old) - axis.transB
  5417. };
  5418.  
  5419. },
  5420.  
  5421. /**
  5422. * Get the x, y position of the tick label
  5423. */
  5424. getLabelPosition: function (x, y, label, horiz, labelOptions, tickmarkOffset, index, step) {
  5425. var axis = this.axis,
  5426. transA = axis.transA,
  5427. reversed = axis.reversed,
  5428. staggerLines = axis.staggerLines;
  5429.  
  5430. x = x + labelOptions.x - (tickmarkOffset && horiz ?
  5431. tickmarkOffset * transA * (reversed ? -1 : 1) : 0);
  5432. y = y + labelOptions.y - (tickmarkOffset && !horiz ?
  5433. tickmarkOffset * transA * (reversed ? 1 : -1) : 0);
  5434.  
  5435. // Vertically centered
  5436. if (!defined(labelOptions.y)) {
  5437. y += pInt(label.styles.lineHeight) * 0.9 - label.getBBox().height / 2;
  5438. }
  5439.  
  5440. // Correct for staggered labels
  5441. if (staggerLines) {
  5442. y += (index / (step || 1) % staggerLines) * 16;
  5443. }
  5444.  
  5445. return {
  5446. x: x,
  5447. y: y
  5448. };
  5449. },
  5450.  
  5451. /**
  5452. * Extendible method to return the path of the marker
  5453. */
  5454. getMarkPath: function (x, y, tickLength, tickWidth, horiz, renderer) {
  5455. return renderer.crispLine([
  5456. M,
  5457. x,
  5458. y,
  5459. L,
  5460. x + (horiz ? 0 : -tickLength),
  5461. y + (horiz ? tickLength : 0)
  5462. ], tickWidth);
  5463. },
  5464.  
  5465. /**
  5466. * Put everything in place
  5467. *
  5468. * @param index {Number}
  5469. * @param old {Boolean} Use old coordinates to prepare an animation into new position
  5470. */
  5471. render: function (index, old) {
  5472. var tick = this,
  5473. axis = tick.axis,
  5474. options = axis.options,
  5475. chart = axis.chart,
  5476. renderer = chart.renderer,
  5477. horiz = axis.horiz,
  5478. type = tick.type,
  5479. label = tick.label,
  5480. pos = tick.pos,
  5481. labelOptions = options.labels,
  5482. gridLine = tick.gridLine,
  5483. gridPrefix = type ? type + 'Grid' : 'grid',
  5484. tickPrefix = type ? type + 'Tick' : 'tick',
  5485. gridLineWidth = options[gridPrefix + 'LineWidth'],
  5486. gridLineColor = options[gridPrefix + 'LineColor'],
  5487. dashStyle = options[gridPrefix + 'LineDashStyle'],
  5488. tickLength = options[tickPrefix + 'Length'],
  5489. tickWidth = options[tickPrefix + 'Width'] || 0,
  5490. tickColor = options[tickPrefix + 'Color'],
  5491. tickPosition = options[tickPrefix + 'Position'],
  5492. gridLinePath,
  5493. mark = tick.mark,
  5494. markPath,
  5495. step = labelOptions.step,
  5496. attribs,
  5497. show = true,
  5498. tickmarkOffset = (options.categories && options.tickmarkPlacement === 'between') ? 0.5 : 0,
  5499. xy = tick.getPosition(horiz, pos, tickmarkOffset, old),
  5500. x = xy.x,
  5501. y = xy.y,
  5502. staggerLines = axis.staggerLines;
  5503.  
  5504. // create the grid line
  5505. if (gridLineWidth) {
  5506. gridLinePath = axis.getPlotLinePath(pos + tickmarkOffset, gridLineWidth, old);
  5507.  
  5508. if (gridLine === UNDEFINED) {
  5509. attribs = {
  5510. stroke: gridLineColor,
  5511. 'stroke-width': gridLineWidth
  5512. };
  5513. if (dashStyle) {
  5514. attribs.dashstyle = dashStyle;
  5515. }
  5516. if (!type) {
  5517. attribs.zIndex = 1;
  5518. }
  5519. tick.gridLine = gridLine =
  5520. gridLineWidth ?
  5521. renderer.path(gridLinePath)
  5522. .attr(attribs).add(axis.gridGroup) :
  5523. null;
  5524. }
  5525.  
  5526. // If the parameter 'old' is set, the current call will be followed
  5527. // by another call, therefore do not do any animations this time
  5528. if (!old && gridLine && gridLinePath) {
  5529. gridLine[tick.isNew ? 'attr' : 'animate']({
  5530. d: gridLinePath
  5531. });
  5532. }
  5533. }
  5534.  
  5535. // create the tick mark
  5536. if (tickWidth) {
  5537.  
  5538. // negate the length
  5539. if (tickPosition === 'inside') {
  5540. tickLength = -tickLength;
  5541. }
  5542. if (axis.opposite) {
  5543. tickLength = -tickLength;
  5544. }
  5545.  
  5546. markPath = tick.getMarkPath(x, y, tickLength, tickWidth, horiz, renderer);
  5547.  
  5548. if (mark) { // updating
  5549. mark.animate({
  5550. d: markPath
  5551. });
  5552. } else { // first time
  5553. tick.mark = renderer.path(
  5554. markPath
  5555. ).attr({
  5556. stroke: tickColor,
  5557. 'stroke-width': tickWidth
  5558. }).add(axis.axisGroup);
  5559. }
  5560. }
  5561.  
  5562. // the label is created on init - now move it into place
  5563. if (label && !isNaN(x)) {
  5564. label.xy = xy = tick.getLabelPosition(x, y, label, horiz, labelOptions, tickmarkOffset, index, step);
  5565.  
  5566. // apply show first and show last
  5567. if ((tick.isFirst && !pick(options.showFirstLabel, 1)) ||
  5568. (tick.isLast && !pick(options.showLastLabel, 1))) {
  5569. show = false;
  5570.  
  5571. // Handle label overflow and show or hide accordingly
  5572. } else if (!staggerLines && horiz && labelOptions.overflow === 'justify' && !tick.handleOverflow(index, xy)) {
  5573. show = false;
  5574. }
  5575.  
  5576. // apply step
  5577. if (step && index % step) {
  5578. // show those indices dividable by step
  5579. show = false;
  5580. }
  5581.  
  5582. // Set the new position, and show or hide
  5583. if (show) {
  5584. label[tick.isNew ? 'attr' : 'animate'](xy);
  5585. label.show();
  5586. tick.isNew = false;
  5587. } else {
  5588. label.hide();
  5589. }
  5590. }
  5591. },
  5592.  
  5593. /**
  5594. * Destructor for the tick prototype
  5595. */
  5596. destroy: function () {
  5597. destroyObjectProperties(this, this.axis);
  5598. }
  5599. };
  5600.  
  5601. /**
  5602. * The object wrapper for plot lines and plot bands
  5603. * @param {Object} options
  5604. */
  5605. function PlotLineOrBand(axis, options) {
  5606. this.axis = axis;
  5607.  
  5608. if (options) {
  5609. this.options = options;
  5610. this.id = options.id;
  5611. }
  5612.  
  5613. //plotLine.render()
  5614. return this;
  5615. }
  5616.  
  5617. PlotLineOrBand.prototype = {
  5618.  
  5619. /**
  5620. * Render the plot line or plot band. If it is already existing,
  5621. * move it.
  5622. */
  5623. render: function () {
  5624. var plotLine = this,
  5625. axis = plotLine.axis,
  5626. horiz = axis.horiz,
  5627. halfPointRange = (axis.pointRange || 0) / 2,
  5628. options = plotLine.options,
  5629. optionsLabel = options.label,
  5630. label = plotLine.label,
  5631. width = options.width,
  5632. to = options.to,
  5633. from = options.from,
  5634. isBand = defined(from) && defined(to),
  5635. value = options.value,
  5636. dashStyle = options.dashStyle,
  5637. svgElem = plotLine.svgElem,
  5638. path = [],
  5639. addEvent,
  5640. eventType,
  5641. xs,
  5642. ys,
  5643. x,
  5644. y,
  5645. color = options.color,
  5646. zIndex = options.zIndex,
  5647. events = options.events,
  5648. attribs,
  5649. renderer = axis.chart.renderer;
  5650.  
  5651. // logarithmic conversion
  5652. if (axis.isLog) {
  5653. from = log2lin(from);
  5654. to = log2lin(to);
  5655. value = log2lin(value);
  5656. }
  5657.  
  5658. // plot line
  5659. if (width) {
  5660. path = axis.getPlotLinePath(value, width);
  5661. attribs = {
  5662. stroke: color,
  5663. 'stroke-width': width
  5664. };
  5665. if (dashStyle) {
  5666. attribs.dashstyle = dashStyle;
  5667. }
  5668. } else if (isBand) { // plot band
  5669.  
  5670. // keep within plot area
  5671. from = mathMax(from, axis.min - halfPointRange);
  5672. to = mathMin(to, axis.max + halfPointRange);
  5673.  
  5674. path = axis.getPlotBandPath(from, to, options);
  5675. attribs = {
  5676. fill: color
  5677. };
  5678. if (options.borderWidth) {
  5679. attribs.stroke = options.borderColor;
  5680. attribs['stroke-width'] = options.borderWidth;
  5681. }
  5682. } else {
  5683. return;
  5684. }
  5685. // zIndex
  5686. if (defined(zIndex)) {
  5687. attribs.zIndex = zIndex;
  5688. }
  5689.  
  5690. // common for lines and bands
  5691. if (svgElem) {
  5692. if (path) {
  5693. svgElem.animate({
  5694. d: path
  5695. }, null, svgElem.onGetPath);
  5696. } else {
  5697. svgElem.hide();
  5698. svgElem.onGetPath = function () {
  5699. svgElem.show();
  5700. };
  5701. }
  5702. } else if (path && path.length) {
  5703. plotLine.svgElem = svgElem = renderer.path(path)
  5704. .attr(attribs).add();
  5705.  
  5706. // events
  5707. if (events) {
  5708. addEvent = function (eventType) {
  5709. svgElem.on(eventType, function (e) {
  5710. events[eventType].apply(plotLine, [e]);
  5711. });
  5712. };
  5713. for (eventType in events) {
  5714. addEvent(eventType);
  5715. }
  5716. }
  5717. }
  5718.  
  5719. // the plot band/line label
  5720. if (optionsLabel && defined(optionsLabel.text) && path && path.length && axis.width > 0 && axis.height > 0) {
  5721. // apply defaults
  5722. optionsLabel = merge({
  5723. align: horiz && isBand && 'center',
  5724. x: horiz ? !isBand && 4 : 10,
  5725. verticalAlign : !horiz && isBand && 'middle',
  5726. y: horiz ? isBand ? 16 : 10 : isBand ? 6 : -4,
  5727. rotation: horiz && !isBand && 90
  5728. }, optionsLabel);
  5729.  
  5730. // add the SVG element
  5731. if (!label) {
  5732. plotLine.label = label = renderer.text(
  5733. optionsLabel.text,
  5734. 0,
  5735. 0
  5736. )
  5737. .attr({
  5738. align: optionsLabel.textAlign || optionsLabel.align,
  5739. rotation: optionsLabel.rotation,
  5740. zIndex: zIndex
  5741. })
  5742. .css(optionsLabel.style)
  5743. .add();
  5744. }
  5745.  
  5746. // get the bounding box and align the label
  5747. xs = [path[1], path[4], pick(path[6], path[1])];
  5748. ys = [path[2], path[5], pick(path[7], path[2])];
  5749. x = arrayMin(xs);
  5750. y = arrayMin(ys);
  5751.  
  5752. label.align(optionsLabel, false, {
  5753. x: x,
  5754. y: y,
  5755. width: arrayMax(xs) - x,
  5756. height: arrayMax(ys) - y
  5757. });
  5758. label.show();
  5759.  
  5760. } else if (label) { // move out of sight
  5761. label.hide();
  5762. }
  5763.  
  5764. // chainable
  5765. return plotLine;
  5766. },
  5767.  
  5768. /**
  5769. * Remove the plot line or band
  5770. */
  5771. destroy: function () {
  5772. var plotLine = this,
  5773. axis = plotLine.axis;
  5774.  
  5775. // remove it from the lookup
  5776. erase(axis.plotLinesAndBands, plotLine);
  5777.  
  5778. destroyObjectProperties(plotLine, this.axis);
  5779. }
  5780. };
  5781. /**
  5782. * The class for stack items
  5783. */
  5784. function StackItem(axis, options, isNegative, x, stackOption) {
  5785. var inverted = axis.chart.inverted;
  5786.  
  5787. this.axis = axis;
  5788.  
  5789. // Tells if the stack is negative
  5790. this.isNegative = isNegative;
  5791.  
  5792. // Save the options to be able to style the label
  5793. this.options = options;
  5794.  
  5795. // Save the x value to be able to position the label later
  5796. this.x = x;
  5797.  
  5798. // Save the stack option on the series configuration object
  5799. this.stack = stackOption;
  5800.  
  5801. // The align options and text align varies on whether the stack is negative and
  5802. // if the chart is inverted or not.
  5803. // First test the user supplied value, then use the dynamic.
  5804. this.alignOptions = {
  5805. align: options.align || (inverted ? (isNegative ? 'left' : 'right') : 'center'),
  5806. verticalAlign: options.verticalAlign || (inverted ? 'middle' : (isNegative ? 'bottom' : 'top')),
  5807. y: pick(options.y, inverted ? 4 : (isNegative ? 14 : -6)),
  5808. x: pick(options.x, inverted ? (isNegative ? -6 : 6) : 0)
  5809. };
  5810.  
  5811. this.textAlign = options.textAlign || (inverted ? (isNegative ? 'right' : 'left') : 'center');
  5812. }
  5813.  
  5814. StackItem.prototype = {
  5815. destroy: function () {
  5816. destroyObjectProperties(this, this.axis);
  5817. },
  5818.  
  5819. /**
  5820. * Sets the total of this stack. Should be called when a serie is hidden or shown
  5821. * since that will affect the total of other stacks.
  5822. */
  5823. setTotal: function (total) {
  5824. this.total = total;
  5825. this.cum = total;
  5826. },
  5827.  
  5828. /**
  5829. * Renders the stack total label and adds it to the stack label group.
  5830. */
  5831. render: function (group) {
  5832. var str = this.options.formatter.call(this); // format the text in the label
  5833.  
  5834. // Change the text to reflect the new total and set visibility to hidden in case the serie is hidden
  5835. if (this.label) {
  5836. this.label.attr({text: str, visibility: HIDDEN});
  5837. // Create new label
  5838. } else {
  5839. this.label =
  5840. this.axis.chart.renderer.text(str, 0, 0) // dummy positions, actual position updated with setOffset method in columnseries
  5841. .css(this.options.style) // apply style
  5842. .attr({align: this.textAlign, // fix the text-anchor
  5843. rotation: this.options.rotation, // rotation
  5844. visibility: HIDDEN }) // hidden until setOffset is called
  5845. .add(group); // add to the labels-group
  5846. }
  5847. },
  5848.  
  5849. /**
  5850. * Sets the offset that the stack has from the x value and repositions the label.
  5851. */
  5852. setOffset: function (xOffset, xWidth) {
  5853. var stackItem = this,
  5854. axis = stackItem.axis,
  5855. chart = axis.chart,
  5856. inverted = chart.inverted,
  5857. neg = this.isNegative, // special treatment is needed for negative stacks
  5858. y = axis.translate(this.total, 0, 0, 0, 1), // stack value translated mapped to chart coordinates
  5859. yZero = axis.translate(0), // stack origin
  5860. h = mathAbs(y - yZero), // stack height
  5861. x = chart.xAxis[0].translate(this.x) + xOffset, // stack x position
  5862. plotHeight = chart.plotHeight,
  5863. stackBox = { // this is the box for the complete stack
  5864. x: inverted ? (neg ? y : y - h) : x,
  5865. y: inverted ? plotHeight - x - xWidth : (neg ? (plotHeight - y - h) : plotHeight - y),
  5866. width: inverted ? h : xWidth,
  5867. height: inverted ? xWidth : h
  5868. };
  5869.  
  5870. if (this.label) {
  5871. this.label
  5872. .align(this.alignOptions, null, stackBox) // align the label to the box
  5873. .attr({visibility: VISIBLE}); // set visibility
  5874. }
  5875.  
  5876. }
  5877. };
  5878. /**
  5879. * Create a new axis object
  5880. * @param {Object} chart
  5881. * @param {Object} options
  5882. */
  5883. function Axis() {
  5884. this.init.apply(this, arguments);
  5885. }
  5886.  
  5887. Axis.prototype = {
  5888.  
  5889. /**
  5890. * Default options for the X axis - the Y axis has extended defaults
  5891. */
  5892. defaultOptions: {
  5893. // allowDecimals: null,
  5894. // alternateGridColor: null,
  5895. // categories: [],
  5896. dateTimeLabelFormats: {
  5897. millisecond: '%H:%M:%S.%L',
  5898. second: '%H:%M:%S',
  5899. minute: '%H:%M',
  5900. hour: '%H:%M',
  5901. day: '%e. %b',
  5902. week: '%e. %b',
  5903. month: '%b \'%y',
  5904. year: '%Y'
  5905. },
  5906. endOnTick: false,
  5907. gridLineColor: '#C0C0C0',
  5908. // gridLineDashStyle: 'solid',
  5909. // gridLineWidth: 0,
  5910. // reversed: false,
  5911.  
  5912. labels: defaultLabelOptions,
  5913. // { step: null },
  5914. lineColor: '#C0D0E0',
  5915. lineWidth: 1,
  5916. //linkedTo: null,
  5917. //max: undefined,
  5918. //min: undefined,
  5919. minPadding: 0.01,
  5920. maxPadding: 0.01,
  5921. //minRange: null,
  5922. minorGridLineColor: '#E0E0E0',
  5923. // minorGridLineDashStyle: null,
  5924. minorGridLineWidth: 1,
  5925. minorTickColor: '#A0A0A0',
  5926. //minorTickInterval: null,
  5927. minorTickLength: 2,
  5928. minorTickPosition: 'outside', // inside or outside
  5929. //minorTickWidth: 0,
  5930. //opposite: false,
  5931. //offset: 0,
  5932. //plotBands: [{
  5933. // events: {},
  5934. // zIndex: 1,
  5935. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5936. //}],
  5937. //plotLines: [{
  5938. // events: {}
  5939. // dashStyle: {}
  5940. // zIndex:
  5941. // labels: { align, x, verticalAlign, y, style, rotation, textAlign }
  5942. //}],
  5943. //reversed: false,
  5944. // showFirstLabel: true,
  5945. // showLastLabel: true,
  5946. startOfWeek: 1,
  5947. startOnTick: false,
  5948. tickColor: '#C0D0E0',
  5949. //tickInterval: null,
  5950. tickLength: 5,
  5951. tickmarkPlacement: 'between', // on or between
  5952. tickPixelInterval: 100,
  5953. tickPosition: 'outside',
  5954. tickWidth: 1,
  5955. title: {
  5956. //text: null,
  5957. align: 'middle', // low, middle or high
  5958. //margin: 0 for horizontal, 10 for vertical axes,
  5959. //rotation: 0,
  5960. //side: 'outside',
  5961. style: {
  5962. color: '#6D869F',
  5963. //font: defaultFont.replace('normal', 'bold')
  5964. fontWeight: 'bold'
  5965. }
  5966. //x: 0,
  5967. //y: 0
  5968. },
  5969. type: 'linear' // linear, logarithmic or datetime
  5970. },
  5971.  
  5972. /**
  5973. * This options set extends the defaultOptions for Y axes
  5974. */
  5975. defaultYAxisOptions: {
  5976. endOnTick: true,
  5977. gridLineWidth: 1,
  5978. tickPixelInterval: 72,
  5979. showLastLabel: true,
  5980. labels: {
  5981. align: 'right',
  5982. x: -8,
  5983. y: 3
  5984. },
  5985. lineWidth: 0,
  5986. maxPadding: 0.05,
  5987. minPadding: 0.05,
  5988. startOnTick: true,
  5989. tickWidth: 0,
  5990. title: {
  5991. rotation: 270,
  5992. text: 'Y-values'
  5993. },
  5994. stackLabels: {
  5995. enabled: false,
  5996. //align: dynamic,
  5997. //y: dynamic,
  5998. //x: dynamic,
  5999. //verticalAlign: dynamic,
  6000. //textAlign: dynamic,
  6001. //rotation: 0,
  6002. formatter: function () {
  6003. return this.total;
  6004. },
  6005. style: defaultLabelOptions.style
  6006. }
  6007. },
  6008.  
  6009. /**
  6010. * These options extend the defaultOptions for left axes
  6011. */
  6012. defaultLeftAxisOptions: {
  6013. labels: {
  6014. align: 'right',
  6015. x: -8,
  6016. y: null
  6017. },
  6018. title: {
  6019. rotation: 270
  6020. }
  6021. },
  6022.  
  6023. /**
  6024. * These options extend the defaultOptions for right axes
  6025. */
  6026. defaultRightAxisOptions: {
  6027. labels: {
  6028. align: 'left',
  6029. x: 8,
  6030. y: null
  6031. },
  6032. title: {
  6033. rotation: 90
  6034. }
  6035. },
  6036.  
  6037. /**
  6038. * These options extend the defaultOptions for bottom axes
  6039. */
  6040. defaultBottomAxisOptions: {
  6041. labels: {
  6042. align: 'center',
  6043. x: 0,
  6044. y: 14
  6045. // overflow: undefined,
  6046. // staggerLines: null
  6047. },
  6048. title: {
  6049. rotation: 0
  6050. }
  6051. },
  6052. /**
  6053. * These options extend the defaultOptions for left axes
  6054. */
  6055. defaultTopAxisOptions: {
  6056. labels: {
  6057. align: 'center',
  6058. x: 0,
  6059. y: -5
  6060. // overflow: undefined
  6061. // staggerLines: null
  6062. },
  6063. title: {
  6064. rotation: 0
  6065. }
  6066. },
  6067.  
  6068. /**
  6069. * Initialize the axis
  6070. */
  6071. init: function (chart, userOptions) {
  6072.  
  6073.  
  6074. var isXAxis = userOptions.isX,
  6075. axis = this;
  6076.  
  6077. // Flag, is the axis horizontal
  6078. axis.horiz = chart.inverted ? !isXAxis : isXAxis;
  6079.  
  6080. // Flag, isXAxis
  6081. axis.isXAxis = isXAxis;
  6082. axis.xOrY = isXAxis ? 'x' : 'y';
  6083.  
  6084.  
  6085. axis.opposite = userOptions.opposite; // needed in setOptions
  6086. axis.side = axis.horiz ?
  6087. (axis.opposite ? 0 : 2) : // top : bottom
  6088. (axis.opposite ? 1 : 3); // right : left
  6089.  
  6090. axis.setOptions(userOptions);
  6091.  
  6092.  
  6093. var options = this.options,
  6094. type = options.type,
  6095. isDatetimeAxis = type === 'datetime';
  6096.  
  6097. axis.labelFormatter = options.labels.formatter || axis.defaultLabelFormatter; // can be overwritten by dynamic format
  6098.  
  6099.  
  6100. // Flag, stagger lines or not
  6101. axis.staggerLines = axis.horiz && options.labels.staggerLines;
  6102. axis.userOptions = userOptions;
  6103.  
  6104. //axis.axisTitleMargin = UNDEFINED,// = options.title.margin,
  6105. axis.minPixelPadding = 0;
  6106. //axis.ignoreMinPadding = UNDEFINED; // can be set to true by a column or bar series
  6107. //axis.ignoreMaxPadding = UNDEFINED;
  6108.  
  6109. axis.chart = chart;
  6110. axis.reversed = options.reversed;
  6111.  
  6112. // Initial categories
  6113. axis.categories = options.categories;
  6114.  
  6115. // Elements
  6116. //axis.axisGroup = UNDEFINED;
  6117. //axis.gridGroup = UNDEFINED;
  6118. //axis.axisTitle = UNDEFINED;
  6119. //axis.axisLine = UNDEFINED;
  6120.  
  6121. // Flag if type === logarithmic
  6122. axis.isLog = type === 'logarithmic';
  6123.  
  6124. // Flag, if axis is linked to another axis
  6125. axis.isLinked = defined(options.linkedTo);
  6126. // Linked axis.
  6127. //axis.linkedParent = UNDEFINED;
  6128.  
  6129. // Flag if type === datetime
  6130. axis.isDatetimeAxis = isDatetimeAxis;
  6131.  
  6132. // Flag if percentage mode
  6133. //axis.usePercentage = UNDEFINED;
  6134.  
  6135.  
  6136. // Tick positions
  6137. //axis.tickPositions = UNDEFINED; // array containing predefined positions
  6138. // Tick intervals
  6139. //axis.tickInterval = UNDEFINED;
  6140. //axis.minorTickInterval = UNDEFINED;
  6141.  
  6142. // Major ticks
  6143. axis.ticks = {};
  6144. // Minor ticks
  6145. axis.minorTicks = {};
  6146. //axis.tickAmount = UNDEFINED;
  6147.  
  6148. // List of plotLines/Bands
  6149. axis.plotLinesAndBands = [];
  6150.  
  6151. // Alternate bands
  6152. axis.alternateBands = {};
  6153.  
  6154. // Axis metrics
  6155. //axis.left = UNDEFINED;
  6156. //axis.top = UNDEFINED;
  6157. //axis.width = UNDEFINED;
  6158. //axis.height = UNDEFINED;
  6159. //axis.bottom = UNDEFINED;
  6160. //axis.right = UNDEFINED;
  6161. //axis.transA = UNDEFINED;
  6162. //axis.transB = UNDEFINED;
  6163. //axis.oldTransA = UNDEFINED;
  6164. axis.len = 0;
  6165. //axis.oldMin = UNDEFINED;
  6166. //axis.oldMax = UNDEFINED;
  6167. //axis.oldUserMin = UNDEFINED;
  6168. //axis.oldUserMax = UNDEFINED;
  6169. //axis.oldAxisLength = UNDEFINED;
  6170. axis.minRange = axis.userMinRange = options.minRange || options.maxZoom;
  6171. axis.range = options.range;
  6172. axis.offset = options.offset || 0;
  6173.  
  6174.  
  6175. // Dictionary for stacks
  6176. axis.stacks = {};
  6177.  
  6178. // Min and max in the data
  6179. //axis.dataMin = UNDEFINED,
  6180. //axis.dataMax = UNDEFINED,
  6181.  
  6182. // The axis range
  6183. axis.max = null;
  6184. axis.min = null;
  6185.  
  6186. // User set min and max
  6187. //axis.userMin = UNDEFINED,
  6188. //axis.userMax = UNDEFINED,
  6189.  
  6190. // Run Axis
  6191.  
  6192. var eventType,
  6193. events = axis.options.events;
  6194.  
  6195. // Register
  6196. chart.axes.push(axis);
  6197. chart[isXAxis ? 'xAxis' : 'yAxis'].push(axis);
  6198.  
  6199. axis.series = []; // populated by Series
  6200.  
  6201. // inverted charts have reversed xAxes as default
  6202. if (chart.inverted && isXAxis && axis.reversed === UNDEFINED) {
  6203. axis.reversed = true;
  6204. }
  6205.  
  6206. axis.removePlotBand = axis.removePlotBandOrLine;
  6207. axis.removePlotLine = axis.removePlotBandOrLine;
  6208. axis.addPlotBand = axis.addPlotBandOrLine;
  6209. axis.addPlotLine = axis.addPlotBandOrLine;
  6210.  
  6211.  
  6212. // register event listeners
  6213. for (eventType in events) {
  6214. addEvent(axis, eventType, events[eventType]);
  6215. }
  6216.  
  6217. // extend logarithmic axis
  6218. if (axis.isLog) {
  6219. axis.val2lin = log2lin;
  6220. axis.lin2val = lin2log;
  6221. }
  6222. },
  6223.  
  6224. /**
  6225. * Merge and set options
  6226. */
  6227. setOptions: function (userOptions) {
  6228. this.options = merge(
  6229. this.defaultOptions,
  6230. this.isXAxis ? {} : this.defaultYAxisOptions,
  6231. [this.defaultTopAxisOptions, this.defaultRightAxisOptions,
  6232. this.defaultBottomAxisOptions, this.defaultLeftAxisOptions][this.side],
  6233. userOptions
  6234. );
  6235. },
  6236.  
  6237.  
  6238. /**
  6239. * The default label formatter. The context is a special config object for the label.
  6240. */
  6241. defaultLabelFormatter: function () {
  6242. var axis = this.axis,
  6243. value = this.value,
  6244. categories = axis.categories,
  6245. tickInterval = axis.tickInterval,
  6246. dateTimeLabelFormat = this.dateTimeLabelFormat,
  6247. ret;
  6248.  
  6249. if (categories) {
  6250. ret = value;
  6251.  
  6252. } else if (dateTimeLabelFormat) { // datetime axis
  6253. ret = dateFormat(dateTimeLabelFormat, value);
  6254.  
  6255. } else if (tickInterval % 1000000 === 0) { // use M abbreviation
  6256. ret = (value / 1000000) + 'M';
  6257.  
  6258. } else if (tickInterval % 1000 === 0) { // use k abbreviation
  6259. ret = (value / 1000) + 'k';
  6260.  
  6261. } else if (value >= 1000) { // add thousands separators
  6262. ret = numberFormat(value, 0);
  6263.  
  6264. } else { // small numbers
  6265. ret = numberFormat(value, -1);
  6266. }
  6267. return ret;
  6268. },
  6269.  
  6270. /**
  6271. * Get the minimum and maximum for the series of each axis
  6272. */
  6273. getSeriesExtremes: function () {
  6274. var axis = this,
  6275. chart = axis.chart,
  6276. stacks = axis.stacks,
  6277. posStack = [],
  6278. negStack = [],
  6279. i;
  6280.  
  6281. // reset dataMin and dataMax in case we're redrawing
  6282. axis.dataMin = axis.dataMax = null;
  6283.  
  6284. // loop through this axis' series
  6285. each(axis.series, function (series) {
  6286.  
  6287. if (series.visible || !chart.options.chart.ignoreHiddenSeries) {
  6288.  
  6289. var seriesOptions = series.options,
  6290. stacking,
  6291. posPointStack,
  6292. negPointStack,
  6293. stackKey,
  6294. stackOption,
  6295. negKey,
  6296. xData,
  6297. yData,
  6298. x,
  6299. y,
  6300. threshold = seriesOptions.threshold,
  6301. yDataLength,
  6302. activeYData = [],
  6303. activeCounter = 0;
  6304.  
  6305. // Validate threshold in logarithmic axes
  6306. if (axis.isLog && threshold <= 0) {
  6307. threshold = seriesOptions.threshold = null;
  6308. }
  6309.  
  6310. // Get dataMin and dataMax for X axes
  6311. if (axis.isXAxis) {
  6312. xData = series.xData;
  6313. if (xData.length) {
  6314. axis.dataMin = mathMin(pick(axis.dataMin, xData[0]), arrayMin(xData));
  6315. axis.dataMax = mathMax(pick(axis.dataMax, xData[0]), arrayMax(xData));
  6316. }
  6317.  
  6318. // Get dataMin and dataMax for Y axes, as well as handle stacking and processed data
  6319. } else {
  6320. var isNegative,
  6321. pointStack,
  6322. key,
  6323. cropped = series.cropped,
  6324. xExtremes = series.xAxis.getExtremes(),
  6325. //findPointRange,
  6326. //pointRange,
  6327. j,
  6328. hasModifyValue = !!series.modifyValue;
  6329.  
  6330.  
  6331. // Handle stacking
  6332. stacking = seriesOptions.stacking;
  6333. axis.usePercentage = stacking === 'percent';
  6334.  
  6335. // create a stack for this particular series type
  6336. if (stacking) {
  6337. stackOption = seriesOptions.stack;
  6338. stackKey = series.type + pick(stackOption, '');
  6339. negKey = '-' + stackKey;
  6340. series.stackKey = stackKey; // used in translate
  6341.  
  6342. posPointStack = posStack[stackKey] || []; // contains the total values for each x
  6343. posStack[stackKey] = posPointStack;
  6344.  
  6345. negPointStack = negStack[negKey] || [];
  6346. negStack[negKey] = negPointStack;
  6347. }
  6348. if (axis.usePercentage) {
  6349. axis.dataMin = 0;
  6350. axis.dataMax = 99;
  6351. }
  6352.  
  6353. // processData can alter series.pointRange, so this goes after
  6354. //findPointRange = series.pointRange === null;
  6355.  
  6356. xData = series.processedXData;
  6357. yData = series.processedYData;
  6358. yDataLength = yData.length;
  6359.  
  6360. // loop over the non-null y values and read them into a local array
  6361. for (i = 0; i < yDataLength; i++) {
  6362. x = xData[i];
  6363. y = yData[i];
  6364. if (y !== null && y !== UNDEFINED) {
  6365.  
  6366. // read stacked values into a stack based on the x value,
  6367. // the sign of y and the stack key
  6368. if (stacking) {
  6369. isNegative = y < threshold;
  6370. pointStack = isNegative ? negPointStack : posPointStack;
  6371. key = isNegative ? negKey : stackKey;
  6372.  
  6373. y = pointStack[x] =
  6374. defined(pointStack[x]) ?
  6375. pointStack[x] + y : y;
  6376.  
  6377.  
  6378. // add the series
  6379. if (!stacks[key]) {
  6380. stacks[key] = {};
  6381. }
  6382.  
  6383. // If the StackItem is there, just update the values,
  6384. // if not, create one first
  6385. if (!stacks[key][x]) {
  6386. stacks[key][x] = new StackItem(axis, axis.options.stackLabels, isNegative, x, stackOption);
  6387. }
  6388. stacks[key][x].setTotal(y);
  6389.  
  6390.  
  6391. // general hook, used for Highstock compare values feature
  6392. } else if (hasModifyValue) {
  6393. y = series.modifyValue(y);
  6394. }
  6395.  
  6396. // get the smallest distance between points
  6397. /*if (i) {
  6398. distance = mathAbs(xData[i] - xData[i - 1]);
  6399. pointRange = pointRange === UNDEFINED ? distance : mathMin(distance, pointRange);
  6400. }*/
  6401.  
  6402. // for points within the visible range, including the first point outside the
  6403. // visible range, consider y extremes
  6404. if (cropped || ((xData[i + 1] || x) >= xExtremes.min && (xData[i - 1] || x) <= xExtremes.max)) {
  6405.  
  6406. j = y.length;
  6407. if (j) { // array, like ohlc or range data
  6408. while (j--) {
  6409. if (y[j] !== null) {
  6410. activeYData[activeCounter++] = y[j];
  6411. }
  6412. }
  6413. } else {
  6414. activeYData[activeCounter++] = y;
  6415. }
  6416. }
  6417. }
  6418. }
  6419.  
  6420. // record the least unit distance
  6421. /*if (findPointRange) {
  6422. series.pointRange = pointRange || 1;
  6423. }
  6424. series.closestPointRange = pointRange;*/
  6425.  
  6426. // Get the dataMin and dataMax so far. If percentage is used, the min and max are
  6427. // always 0 and 100. If the length of activeYData is 0, continue with null values.
  6428. if (!axis.usePercentage && activeYData.length) {
  6429. axis.dataMin = mathMin(pick(axis.dataMin, activeYData[0]), arrayMin(activeYData));
  6430. axis.dataMax = mathMax(pick(axis.dataMax, activeYData[0]), arrayMax(activeYData));
  6431. }
  6432.  
  6433. // Adjust to threshold
  6434. if (defined(threshold)) {
  6435. if (axis.dataMin >= threshold) {
  6436. axis.dataMin = threshold;
  6437. axis.ignoreMinPadding = true;
  6438. } else if (axis.dataMax < threshold) {
  6439. axis.dataMax = threshold;
  6440. axis.ignoreMaxPadding = true;
  6441. }
  6442. }
  6443. }
  6444. }
  6445. });
  6446. },
  6447.  
  6448. /**
  6449. * Translate from axis value to pixel position on the chart, or back
  6450. *
  6451. */
  6452. translate: function (val, backwards, cvsCoord, old, handleLog) {
  6453. var axis = this,
  6454. axisLength = axis.len,
  6455. sign = 1,
  6456. cvsOffset = 0,
  6457. localA = old ? axis.oldTransA : axis.transA,
  6458. localMin = old ? axis.oldMin : axis.min,
  6459. returnValue,
  6460. postTranslate = axis.options.ordinal || (axis.isLog && handleLog);
  6461.  
  6462. if (!localA) {
  6463. localA = axis.transA;
  6464. }
  6465.  
  6466. if (cvsCoord) {
  6467. sign *= -1; // canvas coordinates inverts the value
  6468. cvsOffset = axisLength;
  6469. }
  6470. if (axis.reversed) { // reversed axis
  6471. sign *= -1;
  6472. cvsOffset -= sign * axisLength;
  6473. }
  6474.  
  6475. if (backwards) { // reverse translation
  6476. if (axis.reversed) {
  6477. val = axisLength - val;
  6478. }
  6479. returnValue = val / localA + localMin; // from chart pixel to value
  6480. if (postTranslate) { // log and ordinal axes
  6481. returnValue = axis.lin2val(returnValue);
  6482. }
  6483.  
  6484. } else { // normal translation, from axis value to pixel, relative to plot
  6485. if (postTranslate) { // log and ordinal axes
  6486. val = axis.val2lin(val);
  6487. }
  6488.  
  6489. returnValue = sign * (val - localMin) * localA + cvsOffset + (sign * axis.minPixelPadding);
  6490. }
  6491.  
  6492. return returnValue;
  6493. },
  6494.  
  6495. /**
  6496. * Create the path for a plot line that goes from the given value on
  6497. * this axis, across the plot to the opposite side
  6498. * @param {Number} value
  6499. * @param {Number} lineWidth Used for calculation crisp line
  6500. * @param {Number] old Use old coordinates (for resizing and rescaling)
  6501. */
  6502. getPlotLinePath: function (value, lineWidth, old) {
  6503. var axis = this,
  6504. chart = axis.chart,
  6505. axisLeft = axis.left,
  6506. axisTop = axis.top,
  6507. x1,
  6508. y1,
  6509. x2,
  6510. y2,
  6511. translatedValue = axis.translate(value, null, null, old),
  6512. cHeight = (old && chart.oldChartHeight) || chart.chartHeight,
  6513. cWidth = (old && chart.oldChartWidth) || chart.chartWidth,
  6514. skip,
  6515. transB = axis.transB;
  6516.  
  6517. x1 = x2 = mathRound(translatedValue + transB);
  6518. y1 = y2 = mathRound(cHeight - translatedValue - transB);
  6519.  
  6520. if (isNaN(translatedValue)) { // no min or max
  6521. skip = true;
  6522.  
  6523. } else if (axis.horiz) {
  6524. y1 = axisTop;
  6525. y2 = cHeight - axis.bottom;
  6526. if (x1 < axisLeft || x1 > axisLeft + axis.width) {
  6527. skip = true;
  6528. }
  6529. } else {
  6530. x1 = axisLeft;
  6531. x2 = cWidth - axis.right;
  6532.  
  6533. if (y1 < axisTop || y1 > axisTop + axis.height) {
  6534. skip = true;
  6535. }
  6536. }
  6537. return skip ?
  6538. null :
  6539. chart.renderer.crispLine([M, x1, y1, L, x2, y2], lineWidth || 0);
  6540. },
  6541.  
  6542. /**
  6543. * Create the path for a plot band
  6544. */
  6545. getPlotBandPath: function (from, to) {
  6546.  
  6547. var toPath = this.getPlotLinePath(to),
  6548. path = this.getPlotLinePath(from);
  6549.  
  6550. if (path && toPath) {
  6551. path.push(
  6552. toPath[4],
  6553. toPath[5],
  6554. toPath[1],
  6555. toPath[2]
  6556. );
  6557. } else { // outside the axis area
  6558. path = null;
  6559. }
  6560.  
  6561. return path;
  6562. },
  6563.  
  6564. /**
  6565. * Set the tick positions of a linear axis to round values like whole tens or every five.
  6566. */
  6567. getLinearTickPositions: function (tickInterval, min, max) {
  6568. var pos,
  6569. lastPos,
  6570. roundedMin = correctFloat(mathFloor(min / tickInterval) * tickInterval),
  6571. roundedMax = correctFloat(mathCeil(max / tickInterval) * tickInterval),
  6572. tickPositions = [];
  6573.  
  6574. // Populate the intermediate values
  6575. pos = roundedMin;
  6576. while (pos <= roundedMax) {
  6577.  
  6578. // Place the tick on the rounded value
  6579. tickPositions.push(pos);
  6580.  
  6581. // Always add the raw tickInterval, not the corrected one.
  6582. pos = correctFloat(pos + tickInterval);
  6583.  
  6584. // If the interval is not big enough in the current min - max range to actually increase
  6585. // the loop variable, we need to break out to prevent endless loop. Issue #619
  6586. if (pos === lastPos) {
  6587. break;
  6588. }
  6589.  
  6590. // Record the last value
  6591. lastPos = pos;
  6592. }
  6593. return tickPositions;
  6594. },
  6595.  
  6596. /**
  6597. * Set the tick positions of a logarithmic axis
  6598. */
  6599. getLogTickPositions: function (interval, min, max, minor) {
  6600. var axis = this,
  6601. options = axis.options,
  6602. axisLength = axis.len;
  6603.  
  6604. // Since we use this method for both major and minor ticks,
  6605. // use a local variable and return the result
  6606. var positions = [];
  6607.  
  6608. // Reset
  6609. if (!minor) {
  6610. axis._minorAutoInterval = null;
  6611. }
  6612.  
  6613. // First case: All ticks fall on whole logarithms: 1, 10, 100 etc.
  6614. if (interval >= 0.5) {
  6615. interval = mathRound(interval);
  6616. positions = axis.getLinearTickPositions(interval, min, max);
  6617.  
  6618. // Second case: We need intermediary ticks. For example
  6619. // 1, 2, 4, 6, 8, 10, 20, 40 etc.
  6620. } else if (interval >= 0.08) {
  6621. var roundedMin = mathFloor(min),
  6622. intermediate,
  6623. i,
  6624. j,
  6625. len,
  6626. pos,
  6627. lastPos,
  6628. break2;
  6629.  
  6630. if (interval > 0.3) {
  6631. intermediate = [1, 2, 4];
  6632. } else if (interval > 0.15) { // 0.2 equals five minor ticks per 1, 10, 100 etc
  6633. intermediate = [1, 2, 4, 6, 8];
  6634. } else { // 0.1 equals ten minor ticks per 1, 10, 100 etc
  6635. intermediate = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  6636. }
  6637.  
  6638. for (i = roundedMin; i < max + 1 && !break2; i++) {
  6639. len = intermediate.length;
  6640. for (j = 0; j < len && !break2; j++) {
  6641. pos = log2lin(lin2log(i) * intermediate[j]);
  6642.  
  6643. if (pos > min) {
  6644. positions.push(lastPos);
  6645. }
  6646.  
  6647. if (lastPos > max) {
  6648. break2 = true;
  6649. }
  6650. lastPos = pos;
  6651. }
  6652. }
  6653.  
  6654. // Third case: We are so deep in between whole logarithmic values that
  6655. // we might as well handle the tick positions like a linear axis. For
  6656. // example 1.01, 1.02, 1.03, 1.04.
  6657. } else {
  6658. var realMin = lin2log(min),
  6659. realMax = lin2log(max),
  6660. tickIntervalOption = options[minor ? 'minorTickInterval' : 'tickInterval'],
  6661. filteredTickIntervalOption = tickIntervalOption === 'auto' ? null : tickIntervalOption,
  6662. tickPixelIntervalOption = options.tickPixelInterval / (minor ? 5 : 1),
  6663. totalPixelLength = minor ? axisLength / axis.tickPositions.length : axisLength;
  6664.  
  6665. interval = pick(
  6666. filteredTickIntervalOption,
  6667. axis._minorAutoInterval,
  6668. (realMax - realMin) * tickPixelIntervalOption / (totalPixelLength || 1)
  6669. );
  6670.  
  6671. interval = normalizeTickInterval(
  6672. interval,
  6673. null,
  6674. math.pow(10, mathFloor(math.log(interval) / math.LN10))
  6675. );
  6676.  
  6677. positions = map(axis.getLinearTickPositions(
  6678. interval,
  6679. realMin,
  6680. realMax
  6681. ), log2lin);
  6682.  
  6683. if (!minor) {
  6684. axis._minorAutoInterval = interval / 5;
  6685. }
  6686. }
  6687.  
  6688. // Set the axis-level tickInterval variable
  6689. if (!minor) {
  6690. axis.tickInterval = interval;
  6691. }
  6692. return positions;
  6693. },
  6694.  
  6695. /**
  6696. * Return the minor tick positions. For logarithmic axes, reuse the same logic
  6697. * as for major ticks.
  6698. */
  6699. getMinorTickPositions: function () {
  6700. var axis = this,
  6701. tickPositions = axis.tickPositions,
  6702. minorTickInterval = axis.minorTickInterval;
  6703.  
  6704. var minorTickPositions = [],
  6705. pos,
  6706. i,
  6707. len;
  6708.  
  6709. if (axis.isLog) {
  6710. len = tickPositions.length;
  6711. for (i = 1; i < len; i++) {
  6712. minorTickPositions = minorTickPositions.concat(
  6713. axis.getLogTickPositions(minorTickInterval, tickPositions[i - 1], tickPositions[i], true)
  6714. );
  6715. }
  6716.  
  6717. } else {
  6718. for (pos = axis.min + (tickPositions[0] - axis.min) % minorTickInterval; pos <= axis.max; pos += minorTickInterval) {
  6719. minorTickPositions.push(pos);
  6720. }
  6721. }
  6722.  
  6723. return minorTickPositions;
  6724. },
  6725.  
  6726. /**
  6727. * Adjust the min and max for the minimum range. Keep in mind that the series data is
  6728. * not yet processed, so we don't have information on data cropping and grouping, or
  6729. * updated axis.pointRange or series.pointRange. The data can't be processed until
  6730. * we have finally established min and max.
  6731. */
  6732. adjustForMinRange: function () {
  6733. var axis = this,
  6734. options = axis.options,
  6735. min = axis.min,
  6736. max = axis.max,
  6737. zoomOffset,
  6738. spaceAvailable = axis.dataMax - axis.dataMin >= axis.minRange,
  6739. closestDataRange,
  6740. i,
  6741. distance,
  6742. xData,
  6743. loopLength,
  6744. minArgs,
  6745. maxArgs;
  6746.  
  6747. // Set the automatic minimum range based on the closest point distance
  6748. if (axis.isXAxis && axis.minRange === UNDEFINED && !axis.isLog) {
  6749.  
  6750. if (defined(options.min) || defined(options.max)) {
  6751. axis.minRange = null; // don't do this again
  6752.  
  6753. } else {
  6754.  
  6755. // Find the closest distance between raw data points, as opposed to
  6756. // closestPointRange that applies to processed points (cropped and grouped)
  6757. each(axis.series, function (series) {
  6758. xData = series.xData;
  6759. loopLength = series.xIncrement ? 1 : xData.length - 1;
  6760. for (i = loopLength; i > 0; i--) {
  6761. distance = xData[i] - xData[i - 1];
  6762. if (closestDataRange === UNDEFINED || distance < closestDataRange) {
  6763. closestDataRange = distance;
  6764. }
  6765. }
  6766. });
  6767. axis.minRange = mathMin(closestDataRange * 5, axis.dataMax - axis.dataMin);
  6768. }
  6769. }
  6770.  
  6771. // if minRange is exceeded, adjust
  6772. if (max - min < axis.minRange) {
  6773. var minRange = axis.minRange;
  6774. zoomOffset = (minRange - max + min) / 2;
  6775.  
  6776. // if min and max options have been set, don't go beyond it
  6777. minArgs = [min - zoomOffset, pick(options.min, min - zoomOffset)];
  6778. if (spaceAvailable) { // if space is available, stay within the data range
  6779. minArgs[2] = axis.dataMin;
  6780. }
  6781. min = arrayMax(minArgs);
  6782.  
  6783. maxArgs = [min + minRange, pick(options.max, min + minRange)];
  6784. if (spaceAvailable) { // if space is availabe, stay within the data range
  6785. maxArgs[2] = axis.dataMax;
  6786. }
  6787.  
  6788. max = arrayMin(maxArgs);
  6789.  
  6790. // now if the max is adjusted, adjust the min back
  6791. if (max - min < minRange) {
  6792. minArgs[0] = max - minRange;
  6793. minArgs[1] = pick(options.min, max - minRange);
  6794. min = arrayMax(minArgs);
  6795. }
  6796. }
  6797.  
  6798. // Record modified extremes
  6799. axis.min = min;
  6800. axis.max = max;
  6801. },
  6802.  
  6803. /**
  6804. * Update translation information
  6805. */
  6806. setAxisTranslation: function () {
  6807. var axis = this,
  6808. range = axis.max - axis.min,
  6809. pointRange = 0,
  6810. closestPointRange,
  6811. seriesClosestPointRange,
  6812. transA = axis.transA;
  6813.  
  6814. // adjust translation for padding
  6815. if (axis.isXAxis) {
  6816. if (axis.isLinked) {
  6817. pointRange = axis.linkedParent.pointRange;
  6818. } else {
  6819. each(axis.series, function (series) {
  6820. pointRange = mathMax(pointRange, series.pointRange);
  6821. seriesClosestPointRange = series.closestPointRange;
  6822. if (!series.noSharedTooltip && defined(seriesClosestPointRange)) {
  6823. closestPointRange = defined(closestPointRange) ?
  6824. mathMin(closestPointRange, seriesClosestPointRange) :
  6825. seriesClosestPointRange;
  6826. }
  6827. });
  6828. }
  6829.  
  6830. // pointRange means the width reserved for each point, like in a column chart
  6831. axis.pointRange = pointRange;
  6832.  
  6833. // closestPointRange means the closest distance between points. In columns
  6834. // it is mostly equal to pointRange, but in lines pointRange is 0 while closestPointRange
  6835. // is some other value
  6836. axis.closestPointRange = closestPointRange;
  6837. }
  6838.  
  6839. // secondary values
  6840. axis.oldTransA = transA;
  6841. axis.translationSlope = axis.transA = transA = axis.len / ((range + pointRange) || 1);
  6842. axis.transB = axis.horiz ? axis.left : axis.bottom; // translation addend
  6843. axis.minPixelPadding = transA * (pointRange / 2);
  6844. },
  6845.  
  6846. /**
  6847. * Set the tick positions to round values and optionally extend the extremes
  6848. * to the nearest tick
  6849. */
  6850. setTickPositions: function (secondPass) {
  6851. var axis = this,
  6852. chart = axis.chart,
  6853. options = axis.options,
  6854. isLog = axis.isLog,
  6855. isDatetimeAxis = axis.isDatetimeAxis,
  6856. isXAxis = axis.isXAxis,
  6857. isLinked = axis.isLinked,
  6858. tickPositioner = axis.options.tickPositioner,
  6859. magnitude,
  6860. maxPadding = options.maxPadding,
  6861. minPadding = options.minPadding,
  6862. length,
  6863. linkedParentExtremes,
  6864. tickIntervalOption = options.tickInterval,
  6865. tickPixelIntervalOption = options.tickPixelInterval,
  6866. tickPositions,
  6867. categories = axis.categories;
  6868.  
  6869. // linked axis gets the extremes from the parent axis
  6870. if (isLinked) {
  6871. axis.linkedParent = chart[isXAxis ? 'xAxis' : 'yAxis'][options.linkedTo];
  6872. linkedParentExtremes = axis.linkedParent.getExtremes();
  6873. axis.min = pick(linkedParentExtremes.min, linkedParentExtremes.dataMin);
  6874. axis.max = pick(linkedParentExtremes.max, linkedParentExtremes.dataMax);
  6875. if (options.type !== axis.linkedParent.options.type) {
  6876. error(11, 1); // Can't link axes of different type
  6877. }
  6878. } else { // initial min and max from the extreme data values
  6879. axis.min = pick(axis.userMin, options.min, axis.dataMin);
  6880. axis.max = pick(axis.userMax, options.max, axis.dataMax);
  6881. }
  6882.  
  6883. if (isLog) {
  6884. if (!secondPass && mathMin(axis.min, pick(axis.dataMin, axis.min)) <= 0) { // #978
  6885. error(10, 1); // Can't plot negative values on log axis
  6886. }
  6887. axis.min = correctFloat(log2lin(axis.min)); // correctFloat cures #934
  6888. axis.max = correctFloat(log2lin(axis.max));
  6889. }
  6890.  
  6891. // handle zoomed range
  6892. if (axis.range) {
  6893. axis.userMin = axis.min = mathMax(axis.min, axis.max - axis.range); // #618
  6894. axis.userMax = axis.max;
  6895. if (secondPass) {
  6896. axis.range = null; // don't use it when running setExtremes
  6897. }
  6898. }
  6899.  
  6900. // adjust min and max for the minimum range
  6901. axis.adjustForMinRange();
  6902.  
  6903. // pad the values to get clear of the chart's edges
  6904. if (!categories && !axis.usePercentage && !isLinked && defined(axis.min) && defined(axis.max)) {
  6905. length = (axis.max - axis.min) || 1;
  6906. if (!defined(options.min) && !defined(axis.userMin) && minPadding && (axis.dataMin < 0 || !axis.ignoreMinPadding)) {
  6907. axis.min -= length * minPadding;
  6908. }
  6909. if (!defined(options.max) && !defined(axis.userMax) && maxPadding && (axis.dataMax > 0 || !axis.ignoreMaxPadding)) {
  6910. axis.max += length * maxPadding;
  6911. }
  6912. }
  6913.  
  6914. // get tickInterval
  6915. if (axis.min === axis.max || axis.min === undefined || axis.max === undefined) {
  6916. axis.tickInterval = 1;
  6917. } else if (isLinked && !tickIntervalOption &&
  6918. tickPixelIntervalOption === axis.linkedParent.options.tickPixelInterval) {
  6919. axis.tickInterval = axis.linkedParent.tickInterval;
  6920. } else {
  6921. axis.tickInterval = pick(
  6922. tickIntervalOption,
  6923. categories ? // for categoried axis, 1 is default, for linear axis use tickPix
  6924. 1 :
  6925. (axis.max - axis.min) * tickPixelIntervalOption / (axis.len || 1)
  6926. );
  6927. }
  6928.  
  6929. // Now we're finished detecting min and max, crop and group series data. This
  6930. // is in turn needed in order to find tick positions in ordinal axes.
  6931. if (isXAxis && !secondPass) {
  6932. each(axis.series, function (series) {
  6933. series.processData(axis.min !== axis.oldMin || axis.max !== axis.oldMax);
  6934. });
  6935. }
  6936.  
  6937. // set the translation factor used in translate function
  6938. axis.setAxisTranslation();
  6939.  
  6940. // hook for ordinal axes. To do: merge with below
  6941. if (axis.beforeSetTickPositions) {
  6942. axis.beforeSetTickPositions();
  6943. }
  6944.  
  6945. // hook for extensions, used in Highstock ordinal axes
  6946. if (axis.postProcessTickInterval) {
  6947. axis.tickInterval = axis.postProcessTickInterval(axis.tickInterval);
  6948. }
  6949.  
  6950. // for linear axes, get magnitude and normalize the interval
  6951. if (!isDatetimeAxis && !isLog) { // linear
  6952. magnitude = math.pow(10, mathFloor(math.log(axis.tickInterval) / math.LN10));
  6953. if (!defined(options.tickInterval)) {
  6954. axis.tickInterval = normalizeTickInterval(axis.tickInterval, null, magnitude, options);
  6955. }
  6956. }
  6957.  
  6958. // get minorTickInterval
  6959. axis.minorTickInterval = options.minorTickInterval === 'auto' && axis.tickInterval ?
  6960. axis.tickInterval / 5 : options.minorTickInterval;
  6961.  
  6962. // find the tick positions
  6963. axis.tickPositions = tickPositions = options.tickPositions || (tickPositioner && tickPositioner.apply(axis, [axis.min, axis.max]));
  6964. if (!tickPositions) {
  6965. if (isDatetimeAxis) {
  6966. tickPositions = (axis.getNonLinearTimeTicks || getTimeTicks)(
  6967. normalizeTimeTickInterval(axis.tickInterval, options.units),
  6968. axis.min,
  6969. axis.max,
  6970. options.startOfWeek,
  6971. axis.ordinalPositions,
  6972. axis.closestPointRange,
  6973. true
  6974. );
  6975. } else if (isLog) {
  6976. tickPositions = axis.getLogTickPositions(axis.tickInterval, axis.min, axis.max);
  6977. } else {
  6978. tickPositions = axis.getLinearTickPositions(axis.tickInterval, axis.min, axis.max);
  6979. }
  6980. axis.tickPositions = tickPositions;
  6981. }
  6982.  
  6983. if (!isLinked) {
  6984.  
  6985. // reset min/max or remove extremes based on start/end on tick
  6986. var roundedMin = tickPositions[0],
  6987. roundedMax = tickPositions[tickPositions.length - 1];
  6988.  
  6989. if (options.startOnTick) {
  6990. axis.min = roundedMin;
  6991. } else if (axis.min > roundedMin) {
  6992. tickPositions.shift();
  6993. }
  6994.  
  6995. if (options.endOnTick) {
  6996. axis.max = roundedMax;
  6997. } else if (axis.max < roundedMax) {
  6998. tickPositions.pop();
  6999. }
  7000.  
  7001. }
  7002. },
  7003.  
  7004. /**
  7005. * Set the max ticks of either the x and y axis collection
  7006. */
  7007. setMaxTicks: function () {
  7008.  
  7009. var chart = this.chart,
  7010. maxTicks = chart.maxTicks,
  7011. tickPositions = this.tickPositions,
  7012. xOrY = this.xOrY;
  7013.  
  7014. if (!maxTicks) { // first call, or maxTicks have been reset after a zoom operation
  7015. maxTicks = {
  7016. x: 0,
  7017. y: 0
  7018. };
  7019. }
  7020.  
  7021. if (!this.isLinked && !this.isDatetimeAxis && tickPositions.length > maxTicks[xOrY] && this.options.alignTicks !== false) {
  7022. maxTicks[xOrY] = tickPositions.length;
  7023. }
  7024. chart.maxTicks = maxTicks;
  7025. },
  7026.  
  7027. /**
  7028. * When using multiple axes, adjust the number of ticks to match the highest
  7029. * number of ticks in that group
  7030. */
  7031. adjustTickAmount: function () {
  7032. var axis = this,
  7033. chart = axis.chart,
  7034. xOrY = axis.xOrY,
  7035. tickPositions = axis.tickPositions,
  7036. maxTicks = chart.maxTicks;
  7037.  
  7038. if (maxTicks && maxTicks[xOrY] && !axis.isDatetimeAxis && !axis.categories && !axis.isLinked && axis.options.alignTicks !== false) { // only apply to linear scale
  7039. var oldTickAmount = axis.tickAmount,
  7040. calculatedTickAmount = tickPositions.length,
  7041. tickAmount;
  7042.  
  7043. // set the axis-level tickAmount to use below
  7044. axis.tickAmount = tickAmount = maxTicks[xOrY];
  7045.  
  7046. if (calculatedTickAmount < tickAmount) {
  7047. while (tickPositions.length < tickAmount) {
  7048. tickPositions.push(correctFloat(
  7049. tickPositions[tickPositions.length - 1] + axis.tickInterval
  7050. ));
  7051. }
  7052. axis.transA *= (calculatedTickAmount - 1) / (tickAmount - 1);
  7053. axis.max = tickPositions[tickPositions.length - 1];
  7054.  
  7055. }
  7056. if (defined(oldTickAmount) && tickAmount !== oldTickAmount) {
  7057. axis.isDirty = true;
  7058. }
  7059. }
  7060. },
  7061.  
  7062. /**
  7063. * Set the scale based on data min and max, user set min and max or options
  7064. *
  7065. */
  7066. setScale: function () {
  7067. var axis = this,
  7068. stacks = axis.stacks,
  7069. type,
  7070. i,
  7071. isDirtyData,
  7072. isDirtyAxisLength;
  7073.  
  7074. axis.oldMin = axis.min;
  7075. axis.oldMax = axis.max;
  7076. axis.oldAxisLength = axis.len;
  7077.  
  7078. // set the new axisLength
  7079. axis.setAxisSize();
  7080. //axisLength = horiz ? axisWidth : axisHeight;
  7081. isDirtyAxisLength = axis.len !== axis.oldAxisLength;
  7082.  
  7083. // is there new data?
  7084. each(axis.series, function (series) {
  7085. if (series.isDirtyData || series.isDirty ||
  7086. series.xAxis.isDirty) { // when x axis is dirty, we need new data extremes for y as well
  7087. isDirtyData = true;
  7088. }
  7089. });
  7090.  
  7091. // do we really need to go through all this?
  7092. if (isDirtyAxisLength || isDirtyData || axis.isLinked ||
  7093. axis.userMin !== axis.oldUserMin || axis.userMax !== axis.oldUserMax) {
  7094.  
  7095. // get data extremes if needed
  7096. axis.getSeriesExtremes();
  7097.  
  7098. // get fixed positions based on tickInterval
  7099. axis.setTickPositions();
  7100.  
  7101. // record old values to decide whether a rescale is necessary later on (#540)
  7102. axis.oldUserMin = axis.userMin;
  7103. axis.oldUserMax = axis.userMax;
  7104.  
  7105. // Mark as dirty if it is not already set to dirty and extremes have changed. #595.
  7106. if (!axis.isDirty) {
  7107. axis.isDirty = isDirtyAxisLength || axis.min !== axis.oldMin || axis.max !== axis.oldMax;
  7108. }
  7109. }
  7110.  
  7111.  
  7112. // reset stacks
  7113. if (!axis.isXAxis) {
  7114. for (type in stacks) {
  7115. for (i in stacks[type]) {
  7116. stacks[type][i].cum = stacks[type][i].total;
  7117. }
  7118. }
  7119. }
  7120.  
  7121. // Set the maximum tick amount
  7122. axis.setMaxTicks();
  7123. },
  7124.  
  7125. /**
  7126. * Set the extremes and optionally redraw
  7127. * @param {Number} newMin
  7128. * @param {Number} newMax
  7129. * @param {Boolean} redraw
  7130. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  7131. * configuration
  7132. * @param {Object} eventArguments
  7133. *
  7134. */
  7135. setExtremes: function (newMin, newMax, redraw, animation, eventArguments) {
  7136. var axis = this,
  7137. chart = axis.chart;
  7138.  
  7139. redraw = pick(redraw, true); // defaults to true
  7140.  
  7141. // Extend the arguments with min and max
  7142. eventArguments = extend(eventArguments, {
  7143. min: newMin,
  7144. max: newMax
  7145. });
  7146.  
  7147. // Fire the event
  7148. fireEvent(axis, 'setExtremes', eventArguments, function () { // the default event handler
  7149.  
  7150. axis.userMin = newMin;
  7151. axis.userMax = newMax;
  7152.  
  7153. // Mark for running afterSetExtremes
  7154. axis.isDirtyExtremes = true;
  7155.  
  7156. // redraw
  7157. if (redraw) {
  7158. chart.redraw(animation);
  7159. }
  7160. });
  7161. },
  7162.  
  7163. /**
  7164. * Update the axis metrics
  7165. */
  7166. setAxisSize: function () {
  7167. var axis = this,
  7168. chart = axis.chart,
  7169. options = axis.options;
  7170.  
  7171. var offsetLeft = options.offsetLeft || 0,
  7172. offsetRight = options.offsetRight || 0;
  7173.  
  7174. // basic values
  7175. // expose to use in Series object and navigator
  7176. axis.left = pick(options.left, chart.plotLeft + offsetLeft);
  7177. axis.top = pick(options.top, chart.plotTop);
  7178. axis.width = pick(options.width, chart.plotWidth - offsetLeft + offsetRight);
  7179. axis.height = pick(options.height, chart.plotHeight);
  7180. axis.bottom = chart.chartHeight - axis.height - axis.top;
  7181. axis.right = chart.chartWidth - axis.width - axis.left;
  7182. axis.len = mathMax(axis.horiz ? axis.width : axis.height, 0); // mathMax fixes #905
  7183. },
  7184.  
  7185. /**
  7186. * Get the actual axis extremes
  7187. */
  7188. getExtremes: function () {
  7189. var axis = this,
  7190. isLog = axis.isLog;
  7191.  
  7192. return {
  7193. min: isLog ? correctFloat(lin2log(axis.min)) : axis.min,
  7194. max: isLog ? correctFloat(lin2log(axis.max)) : axis.max,
  7195. dataMin: axis.dataMin,
  7196. dataMax: axis.dataMax,
  7197. userMin: axis.userMin,
  7198. userMax: axis.userMax
  7199. };
  7200. },
  7201.  
  7202. /**
  7203. * Get the zero plane either based on zero or on the min or max value.
  7204. * Used in bar and area plots
  7205. */
  7206. getThreshold: function (threshold) {
  7207. var axis = this,
  7208. isLog = axis.isLog;
  7209.  
  7210. var realMin = isLog ? lin2log(axis.min) : axis.min,
  7211. realMax = isLog ? lin2log(axis.max) : axis.max;
  7212.  
  7213. if (realMin > threshold || threshold === null) {
  7214. threshold = realMin;
  7215. } else if (realMax < threshold) {
  7216. threshold = realMax;
  7217. }
  7218.  
  7219. return axis.translate(threshold, 0, 1, 0, 1);
  7220. },
  7221.  
  7222. /**
  7223. * Add a plot band or plot line after render time
  7224. *
  7225. * @param options {Object} The plotBand or plotLine configuration object
  7226. */
  7227. addPlotBandOrLine: function (options) {
  7228. var obj = new PlotLineOrBand(this, options).render();
  7229. this.plotLinesAndBands.push(obj);
  7230. return obj;
  7231. },
  7232.  
  7233. /**
  7234. * Render the tick labels to a preliminary position to get their sizes
  7235. */
  7236. getOffset: function () {
  7237. var axis = this,
  7238. chart = axis.chart,
  7239. renderer = chart.renderer,
  7240. options = axis.options,
  7241. tickPositions = axis.tickPositions,
  7242. ticks = axis.ticks,
  7243. horiz = axis.horiz,
  7244. side = axis.side,
  7245. hasData,
  7246. showAxis,
  7247. titleOffset = 0,
  7248. titleOffsetOption,
  7249. titleMargin = 0,
  7250. axisTitleOptions = options.title,
  7251. labelOptions = options.labels,
  7252. labelOffset = 0, // reset
  7253. axisOffset = chart.axisOffset,
  7254. directionFactor = [-1, 1, 1, -1][side],
  7255. n;
  7256.  
  7257.  
  7258. // For reuse in Axis.render
  7259. axis.hasData = hasData = axis.series.length && defined(axis.min) && defined(axis.max);
  7260. axis.showAxis = showAxis = hasData || pick(options.showEmpty, true);
  7261.  
  7262. // Create the axisGroup and gridGroup elements on first iteration
  7263. if (!axis.axisGroup) {
  7264. axis.axisGroup = renderer.g('axis')
  7265. .attr({ zIndex: options.zIndex || 7 })
  7266. .add();
  7267. axis.gridGroup = renderer.g('grid')
  7268. .attr({ zIndex: options.gridZIndex || 1 })
  7269. .add();
  7270. }
  7271.  
  7272. if (hasData || axis.isLinked) {
  7273. each(tickPositions, function (pos) {
  7274. if (!ticks[pos]) {
  7275. ticks[pos] = new Tick(axis, pos);
  7276. } else {
  7277. ticks[pos].addLabel(); // update labels depending on tick interval
  7278. }
  7279.  
  7280. });
  7281.  
  7282. each(tickPositions, function (pos) {
  7283. // left side must be align: right and right side must have align: left for labels
  7284. if (side === 0 || side === 2 || { 1: 'left', 3: 'right' }[side] === labelOptions.align) {
  7285.  
  7286. // get the highest offset
  7287. labelOffset = mathMax(
  7288. ticks[pos].getLabelSize(),
  7289. labelOffset
  7290. );
  7291. }
  7292.  
  7293. });
  7294.  
  7295. if (axis.staggerLines) {
  7296. labelOffset += (axis.staggerLines - 1) * 16;
  7297. }
  7298.  
  7299. } else { // doesn't have data
  7300. for (n in ticks) {
  7301. ticks[n].destroy();
  7302. delete ticks[n];
  7303. }
  7304. }
  7305.  
  7306. if (axisTitleOptions && axisTitleOptions.text) {
  7307. if (!axis.axisTitle) {
  7308. axis.axisTitle = renderer.text(
  7309. axisTitleOptions.text,
  7310. 0,
  7311. 0,
  7312. axisTitleOptions.useHTML
  7313. )
  7314. .attr({
  7315. zIndex: 7,
  7316. rotation: axisTitleOptions.rotation || 0,
  7317. align:
  7318. axisTitleOptions.textAlign ||
  7319. { low: 'left', middle: 'center', high: 'right' }[axisTitleOptions.align]
  7320. })
  7321. .css(axisTitleOptions.style)
  7322. .add(axis.axisGroup);
  7323. axis.axisTitle.isNew = true;
  7324. }
  7325.  
  7326. if (showAxis) {
  7327. titleOffset = axis.axisTitle.getBBox()[horiz ? 'height' : 'width'];
  7328. titleMargin = pick(axisTitleOptions.margin, horiz ? 5 : 10);
  7329. titleOffsetOption = axisTitleOptions.offset;
  7330. }
  7331.  
  7332. // hide or show the title depending on whether showEmpty is set
  7333. axis.axisTitle[showAxis ? 'show' : 'hide']();
  7334. }
  7335.  
  7336. // handle automatic or user set offset
  7337. axis.offset = directionFactor * pick(options.offset, axisOffset[side]);
  7338.  
  7339.  
  7340. axis.axisTitleMargin =
  7341. pick(titleOffsetOption,
  7342. labelOffset + titleMargin +
  7343. (side !== 2 && labelOffset && directionFactor * options.labels[horiz ? 'y' : 'x'])
  7344. );
  7345.  
  7346. axisOffset[side] = mathMax(
  7347. axisOffset[side],
  7348. axis.axisTitleMargin + titleOffset + directionFactor * axis.offset
  7349. );
  7350.  
  7351. },
  7352.  
  7353. /**
  7354. * Get the path for the axis line
  7355. */
  7356. getLinePath: function (lineWidth) {
  7357. var chart = this.chart,
  7358. opposite = this.opposite,
  7359. offset = this.offset,
  7360. horiz = this.horiz,
  7361. lineLeft = this.left + (opposite ? this.width : 0) + offset,
  7362. lineTop = chart.chartHeight - this.bottom - (opposite ? this.height : 0) + offset;
  7363.  
  7364. return chart.renderer.crispLine([
  7365. M,
  7366. horiz ?
  7367. this.left :
  7368. lineLeft,
  7369. horiz ?
  7370. lineTop :
  7371. this.top,
  7372. L,
  7373. horiz ?
  7374. chart.chartWidth - this.right :
  7375. lineLeft,
  7376. horiz ?
  7377. lineTop :
  7378. chart.chartHeight - this.bottom
  7379. ], lineWidth);
  7380. },
  7381.  
  7382. /**
  7383. * Position the title
  7384. */
  7385. getTitlePosition: function () {
  7386. // compute anchor points for each of the title align options
  7387. var horiz = this.horiz,
  7388. axisLeft = this.left,
  7389. axisTop = this.top,
  7390. axisLength = this.len,
  7391. axisTitleOptions = this.options.title,
  7392. margin = horiz ? axisLeft : axisTop,
  7393. opposite = this.opposite,
  7394. offset = this.offset,
  7395. fontSize = pInt(axisTitleOptions.style.fontSize || 12),
  7396.  
  7397. // the position in the length direction of the axis
  7398. alongAxis = {
  7399. low: margin + (horiz ? 0 : axisLength),
  7400. middle: margin + axisLength / 2,
  7401. high: margin + (horiz ? axisLength : 0)
  7402. }[axisTitleOptions.align],
  7403.  
  7404. // the position in the perpendicular direction of the axis
  7405. offAxis = (horiz ? axisTop + this.height : axisLeft) +
  7406. (horiz ? 1 : -1) * // horizontal axis reverses the margin
  7407. (opposite ? -1 : 1) * // so does opposite axes
  7408. this.axisTitleMargin +
  7409. (this.side === 2 ? fontSize : 0);
  7410.  
  7411. return {
  7412. x: horiz ?
  7413. alongAxis :
  7414. offAxis + (opposite ? this.width : 0) + offset +
  7415. (axisTitleOptions.x || 0), // x
  7416. y: horiz ?
  7417. offAxis - (opposite ? this.height : 0) + offset :
  7418. alongAxis + (axisTitleOptions.y || 0) // y
  7419. };
  7420. },
  7421.  
  7422. /**
  7423. * Render the axis
  7424. */
  7425. render: function () {
  7426. var axis = this,
  7427. chart = axis.chart,
  7428. renderer = chart.renderer,
  7429. options = axis.options,
  7430. isLog = axis.isLog,
  7431. isLinked = axis.isLinked,
  7432. tickPositions = axis.tickPositions,
  7433. axisTitle = axis.axisTitle,
  7434. stacks = axis.stacks,
  7435. ticks = axis.ticks,
  7436. minorTicks = axis.minorTicks,
  7437. alternateBands = axis.alternateBands,
  7438. stackLabelOptions = options.stackLabels,
  7439. alternateGridColor = options.alternateGridColor,
  7440. lineWidth = options.lineWidth,
  7441. linePath,
  7442. hasRendered = chart.hasRendered,
  7443. slideInTicks = hasRendered && defined(axis.oldMin) && !isNaN(axis.oldMin),
  7444. hasData = axis.hasData,
  7445. showAxis = axis.showAxis,
  7446. from,
  7447. to;
  7448.  
  7449. // If the series has data draw the ticks. Else only the line and title
  7450. if (hasData || isLinked) {
  7451.  
  7452. // minor ticks
  7453. if (axis.minorTickInterval && !axis.categories) {
  7454. each(axis.getMinorTickPositions(), function (pos) {
  7455. if (!minorTicks[pos]) {
  7456. minorTicks[pos] = new Tick(axis, pos, 'minor');
  7457. }
  7458.  
  7459. // render new ticks in old position
  7460. if (slideInTicks && minorTicks[pos].isNew) {
  7461. minorTicks[pos].render(null, true);
  7462. }
  7463.  
  7464.  
  7465. minorTicks[pos].isActive = true;
  7466. minorTicks[pos].render();
  7467. });
  7468. }
  7469.  
  7470. // Major ticks. Pull out the first item and render it last so that
  7471. // we can get the position of the neighbour label. #808.
  7472. each(tickPositions.slice(1).concat([tickPositions[0]]), function (pos, i) {
  7473.  
  7474. // Reorganize the indices
  7475. i = (i === tickPositions.length - 1) ? 0 : i + 1;
  7476.  
  7477. // linked axes need an extra check to find out if
  7478. if (!isLinked || (pos >= axis.min && pos <= axis.max)) {
  7479.  
  7480. if (!ticks[pos]) {
  7481. ticks[pos] = new Tick(axis, pos);
  7482. }
  7483.  
  7484. // render new ticks in old position
  7485. if (slideInTicks && ticks[pos].isNew) {
  7486. ticks[pos].render(i, true);
  7487. }
  7488.  
  7489. ticks[pos].isActive = true;
  7490. ticks[pos].render(i);
  7491. }
  7492.  
  7493. });
  7494.  
  7495. // alternate grid color
  7496. if (alternateGridColor) {
  7497. each(tickPositions, function (pos, i) {
  7498. if (i % 2 === 0 && pos < axis.max) {
  7499. if (!alternateBands[pos]) {
  7500. alternateBands[pos] = new PlotLineOrBand(axis);
  7501. }
  7502. from = pos;
  7503. to = tickPositions[i + 1] !== UNDEFINED ? tickPositions[i + 1] : axis.max;
  7504. alternateBands[pos].options = {
  7505. from: isLog ? lin2log(from) : from,
  7506. to: isLog ? lin2log(to) : to,
  7507. color: alternateGridColor
  7508. };
  7509. alternateBands[pos].render();
  7510. alternateBands[pos].isActive = true;
  7511. }
  7512. });
  7513. }
  7514.  
  7515. // custom plot lines and bands
  7516. if (!axis._addedPlotLB) { // only first time
  7517. each((options.plotLines || []).concat(options.plotBands || []), function (plotLineOptions) {
  7518. //plotLinesAndBands.push(new PlotLineOrBand(plotLineOptions).render());
  7519. axis.addPlotBandOrLine(plotLineOptions);
  7520. });
  7521. axis._addedPlotLB = true;
  7522. }
  7523.  
  7524. } // end if hasData
  7525.  
  7526. // remove inactive ticks
  7527. each([ticks, minorTicks, alternateBands], function (coll) {
  7528. var pos;
  7529. for (pos in coll) {
  7530. if (!coll[pos].isActive) {
  7531. coll[pos].destroy();
  7532. delete coll[pos];
  7533. } else {
  7534. coll[pos].isActive = false; // reset
  7535. }
  7536. }
  7537. });
  7538.  
  7539. // Static items. As the axis group is cleared on subsequent calls
  7540. // to render, these items are added outside the group.
  7541. // axis line
  7542. if (lineWidth) {
  7543. linePath = axis.getLinePath(lineWidth);
  7544. if (!axis.axisLine) {
  7545. axis.axisLine = renderer.path(linePath)
  7546. .attr({
  7547. stroke: options.lineColor,
  7548. 'stroke-width': lineWidth,
  7549. zIndex: 7
  7550. })
  7551. .add();
  7552. } else {
  7553. axis.axisLine.animate({ d: linePath });
  7554. }
  7555.  
  7556. // show or hide the line depending on options.showEmpty
  7557. axis.axisLine[showAxis ? 'show' : 'hide']();
  7558.  
  7559. }
  7560.  
  7561. if (axisTitle && showAxis) {
  7562.  
  7563. axisTitle[axisTitle.isNew ? 'attr' : 'animate'](
  7564. axis.getTitlePosition()
  7565. );
  7566. axisTitle.isNew = false;
  7567. }
  7568.  
  7569. // Stacked totals:
  7570. if (stackLabelOptions && stackLabelOptions.enabled) {
  7571. var stackKey, oneStack, stackCategory,
  7572. stackTotalGroup = axis.stackTotalGroup;
  7573.  
  7574. // Create a separate group for the stack total labels
  7575. if (!stackTotalGroup) {
  7576. axis.stackTotalGroup = stackTotalGroup =
  7577. renderer.g('stack-labels')
  7578. .attr({
  7579. visibility: VISIBLE,
  7580. zIndex: 6
  7581. })
  7582. .add();
  7583. }
  7584.  
  7585. // plotLeft/Top will change when y axis gets wider so we need to translate the
  7586. // stackTotalGroup at every render call. See bug #506 and #516
  7587. stackTotalGroup.translate(chart.plotLeft, chart.plotTop);
  7588.  
  7589. // Render each stack total
  7590. for (stackKey in stacks) {
  7591. oneStack = stacks[stackKey];
  7592. for (stackCategory in oneStack) {
  7593. oneStack[stackCategory].render(stackTotalGroup);
  7594. }
  7595. }
  7596. }
  7597. // End stacked totals
  7598.  
  7599. axis.isDirty = false;
  7600. },
  7601.  
  7602. /**
  7603. * Remove a plot band or plot line from the chart by id
  7604. * @param {Object} id
  7605. */
  7606. removePlotBandOrLine: function (id) {
  7607. var plotLinesAndBands = this.plotLinesAndBands,
  7608. i = plotLinesAndBands.length;
  7609. while (i--) {
  7610. if (plotLinesAndBands[i].id === id) {
  7611. plotLinesAndBands[i].destroy();
  7612. }
  7613. }
  7614. },
  7615.  
  7616. /**
  7617. * Update the axis title by options
  7618. */
  7619. setTitle: function (newTitleOptions, redraw) {
  7620. var axis = this,
  7621. chart = axis.chart,
  7622. options = axis.options,
  7623. axisTitle;
  7624.  
  7625. options.title = merge(options.title, newTitleOptions);
  7626.  
  7627. axis.axisTitle = axisTitle && axisTitle.destroy(); // #922
  7628. axis.isDirty = true;
  7629.  
  7630. if (pick(redraw, true)) {
  7631. chart.redraw();
  7632. }
  7633. },
  7634.  
  7635. /**
  7636. * Redraw the axis to reflect changes in the data or axis extremes
  7637. */
  7638. redraw: function () {
  7639. var axis = this,
  7640. chart = axis.chart;
  7641.  
  7642. // hide tooltip and hover states
  7643. if (chart.tracker.resetTracker) {
  7644. chart.tracker.resetTracker(true);
  7645. }
  7646.  
  7647. // render the axis
  7648. axis.render();
  7649.  
  7650. // move plot lines and bands
  7651. each(axis.plotLinesAndBands, function (plotLine) {
  7652. plotLine.render();
  7653. });
  7654.  
  7655. // mark associated series as dirty and ready for redraw
  7656. each(axis.series, function (series) {
  7657. series.isDirty = true;
  7658. });
  7659.  
  7660. },
  7661.  
  7662. /**
  7663. * Set new axis categories and optionally redraw
  7664. * @param {Array} newCategories
  7665. * @param {Boolean} doRedraw
  7666. */
  7667. setCategories: function (newCategories, doRedraw) {
  7668. var axis = this,
  7669. chart = axis.chart;
  7670.  
  7671. // set the categories
  7672. axis.categories = axis.userOptions.categories = newCategories;
  7673.  
  7674. // force reindexing tooltips
  7675. each(axis.series, function (series) {
  7676. series.translate();
  7677. series.setTooltipPoints(true);
  7678. });
  7679.  
  7680.  
  7681. // optionally redraw
  7682. axis.isDirty = true;
  7683.  
  7684. if (pick(doRedraw, true)) {
  7685. chart.redraw();
  7686. }
  7687. },
  7688.  
  7689. /**
  7690. * Destroys an Axis instance.
  7691. */
  7692. destroy: function () {
  7693. var axis = this,
  7694. stacks = axis.stacks,
  7695. stackKey;
  7696.  
  7697. // Remove the events
  7698. removeEvent(axis);
  7699.  
  7700. // Destroy each stack total
  7701. for (stackKey in stacks) {
  7702. destroyObjectProperties(stacks[stackKey]);
  7703.  
  7704. stacks[stackKey] = null;
  7705. }
  7706.  
  7707. // Destroy collections
  7708. each([axis.ticks, axis.minorTicks, axis.alternateBands, axis.plotLinesAndBands], function (coll) {
  7709. destroyObjectProperties(coll);
  7710. });
  7711.  
  7712. // Destroy local variables
  7713. each(['stackTotalGroup', 'axisLine', 'axisGroup', 'gridGroup', 'axisTitle'], function (prop) {
  7714. if (axis[prop]) {
  7715. axis[prop] = axis[prop].destroy();
  7716. }
  7717. });
  7718. }
  7719.  
  7720.  
  7721. }; // end Axis
  7722.  
  7723. /**
  7724. * The tooltip object
  7725. * @param {Object} chart The chart instance
  7726. * @param {Object} options Tooltip options
  7727. */
  7728. function Tooltip(chart, options) {
  7729. var borderWidth = options.borderWidth,
  7730. style = options.style,
  7731. shared = options.shared,
  7732. padding = pInt(style.padding);
  7733.  
  7734. // Save the chart and options
  7735. this.chart = chart;
  7736. this.options = options;
  7737.  
  7738. // remove padding CSS and apply padding on box instead
  7739. style.padding = 0;
  7740.  
  7741. // Keep track of the current series
  7742. //this.currentSeries = UNDEFINED;
  7743.  
  7744. // List of crosshairs
  7745. this.crosshairs = [];
  7746.  
  7747. // Current values of x and y when animating
  7748. this.currentX = 0;
  7749. this.currentY = 0;
  7750.  
  7751. // The tooltipTick function, initialized to nothing
  7752. //this.tooltipTick = UNDEFINED;
  7753.  
  7754. // The tooltip is initially hidden
  7755. this.tooltipIsHidden = false;
  7756.  
  7757. // create the label
  7758. this.label = chart.renderer.label('', 0, 0, null, null, null, options.useHTML, null, 'tooltip')
  7759. .attr({
  7760. padding: padding,
  7761. fill: options.backgroundColor,
  7762. 'stroke-width': borderWidth,
  7763. r: options.borderRadius,
  7764. zIndex: 8
  7765. })
  7766. .css(style)
  7767. .hide()
  7768. .add();
  7769.  
  7770. // When using canVG the shadow shows up as a gray circle
  7771. // even if the tooltip is hidden.
  7772. if (!useCanVG) {
  7773. this.label.shadow(options.shadow);
  7774. }
  7775.  
  7776. // Public property for getting the shared state.
  7777. this.shared = shared;
  7778. }
  7779.  
  7780. Tooltip.prototype = {
  7781. /**
  7782. * Destroy the tooltip and its elements.
  7783. */
  7784. destroy: function () {
  7785. each(this.crosshairs, function (crosshair) {
  7786. if (crosshair) {
  7787. crosshair.destroy();
  7788. }
  7789. });
  7790.  
  7791. // Destroy and clear local variables
  7792. if (this.label) {
  7793. this.label = this.label.destroy();
  7794. }
  7795. },
  7796.  
  7797. /**
  7798. * Provide a soft movement for the tooltip
  7799. *
  7800. * @param {Number} finalX
  7801. * @param {Number} finalY
  7802. * @private
  7803. */
  7804. move: function (finalX, finalY) {
  7805. var tooltip = this;
  7806.  
  7807. // get intermediate values for animation
  7808. tooltip.currentX = tooltip.tooltipIsHidden ? finalX : (2 * tooltip.currentX + finalX) / 3;
  7809. tooltip.currentY = tooltip.tooltipIsHidden ? finalY : (tooltip.currentY + finalY) / 2;
  7810.  
  7811. // move to the intermediate value
  7812. tooltip.label.attr({ x: tooltip.currentX, y: tooltip.currentY });
  7813.  
  7814. // run on next tick of the mouse tracker
  7815. if (mathAbs(finalX - tooltip.currentX) > 1 || mathAbs(finalY - tooltip.currentY) > 1) {
  7816. tooltip.tooltipTick = function () {
  7817. tooltip.move(finalX, finalY);
  7818. };
  7819. } else {
  7820. tooltip.tooltipTick = null;
  7821. }
  7822. },
  7823.  
  7824. /**
  7825. * Hide the tooltip
  7826. */
  7827. hide: function () {
  7828. if (!this.tooltipIsHidden) {
  7829. var hoverPoints = this.chart.hoverPoints;
  7830.  
  7831. this.label.hide();
  7832.  
  7833. // hide previous hoverPoints and set new
  7834. if (hoverPoints) {
  7835. each(hoverPoints, function (point) {
  7836. point.setState();
  7837. });
  7838. }
  7839.  
  7840. this.chart.hoverPoints = null;
  7841. this.tooltipIsHidden = true;
  7842. }
  7843. },
  7844.  
  7845. /**
  7846. * Hide the crosshairs
  7847. */
  7848. hideCrosshairs: function () {
  7849. each(this.crosshairs, function (crosshair) {
  7850. if (crosshair) {
  7851. crosshair.hide();
  7852. }
  7853. });
  7854. },
  7855.  
  7856. /**
  7857. * Extendable method to get the anchor position of the tooltip
  7858. * from a point or set of points
  7859. */
  7860. getAnchor: function (points, mouseEvent) {
  7861. var ret,
  7862. chart = this.chart,
  7863. inverted = chart.inverted,
  7864. plotX = 0,
  7865. plotY = 0;
  7866.  
  7867. points = splat(points);
  7868.  
  7869. // Pie uses a special tooltipPos
  7870. ret = points[0].tooltipPos;
  7871.  
  7872. // When shared, use the average position
  7873. if (!ret) {
  7874. each(points, function (point) {
  7875. plotX += point.plotX;
  7876. plotY += point.plotLow ? (point.plotLow + point.plotHigh) / 2 : point.plotY;
  7877. });
  7878.  
  7879. plotX /= points.length;
  7880. plotY /= points.length;
  7881.  
  7882. ret = [
  7883. inverted ? chart.plotWidth - plotY : plotX,
  7884. this.shared && !inverted && points.length > 1 && mouseEvent ?
  7885. mouseEvent.chartY - chart.plotTop : // place shared tooltip next to the mouse (#424)
  7886. inverted ? chart.plotHeight - plotX : plotY
  7887. ];
  7888. }
  7889.  
  7890. return map(ret, mathRound);
  7891. },
  7892.  
  7893. /**
  7894. * Place the tooltip in a chart without spilling over
  7895. * and not covering the point it self.
  7896. */
  7897. getPosition: function (boxWidth, boxHeight, point) {
  7898.  
  7899. // Set up the variables
  7900. var chart = this.chart,
  7901. plotLeft = chart.plotLeft,
  7902. plotTop = chart.plotTop,
  7903. plotWidth = chart.plotWidth,
  7904. plotHeight = chart.plotHeight,
  7905. distance = pick(this.options.distance, 12),
  7906. pointX = point.plotX,
  7907. pointY = point.plotY,
  7908. x = pointX + plotLeft + (chart.inverted ? distance : -boxWidth - distance),
  7909. y = pointY - boxHeight + plotTop + 15, // 15 means the point is 15 pixels up from the bottom of the tooltip
  7910. alignedRight;
  7911.  
  7912. // It is too far to the left, adjust it
  7913. if (x < 7) {
  7914. x = plotLeft + pointX + distance;
  7915. }
  7916.  
  7917. // Test to see if the tooltip is too far to the right,
  7918. // if it is, move it back to be inside and then up to not cover the point.
  7919. if ((x + boxWidth) > (plotLeft + plotWidth)) {
  7920. x -= (x + boxWidth) - (plotLeft + plotWidth);
  7921. y = pointY - boxHeight + plotTop - distance;
  7922. alignedRight = true;
  7923. }
  7924.  
  7925. // If it is now above the plot area, align it to the top of the plot area
  7926. if (y < plotTop + 5) {
  7927. y = plotTop + 5;
  7928.  
  7929. // If the tooltip is still covering the point, move it below instead
  7930. if (alignedRight && pointY >= y && pointY <= (y + boxHeight)) {
  7931. y = pointY + plotTop + distance; // below
  7932. }
  7933. }
  7934.  
  7935. // Now if the tooltip is below the chart, move it up. It's better to cover the
  7936. // point than to disappear outside the chart. #834.
  7937. if (y + boxHeight > plotTop + plotHeight) {
  7938. y = mathMax(plotTop, plotTop + plotHeight - boxHeight - distance); // below
  7939. }
  7940.  
  7941.  
  7942. return {x: x, y: y};
  7943. },
  7944.  
  7945. /**
  7946. * Refresh the tooltip's text and position.
  7947. * @param {Object} point
  7948. */
  7949. refresh: function (point, mouseEvent) {
  7950. var tooltip = this,
  7951. chart = tooltip.chart,
  7952. label = tooltip.label,
  7953. options = tooltip.options;
  7954.  
  7955. /**
  7956. * In case no user defined formatter is given, this will be used
  7957. */
  7958. function defaultFormatter() {
  7959. var pThis = this,
  7960. items = pThis.points || splat(pThis),
  7961. series = items[0].series,
  7962. s;
  7963.  
  7964. // build the header
  7965. s = [series.tooltipHeaderFormatter(items[0].key)];
  7966.  
  7967. // build the values
  7968. each(items, function (item) {
  7969. series = item.series;
  7970. s.push((series.tooltipFormatter && series.tooltipFormatter(item)) ||
  7971. item.point.tooltipFormatter(series.tooltipOptions.pointFormat));
  7972. });
  7973.  
  7974. // footer
  7975. s.push(options.footerFormat || '');
  7976.  
  7977. return s.join('');
  7978. }
  7979.  
  7980. var x,
  7981. y,
  7982. show,
  7983. anchor,
  7984. textConfig = {},
  7985. text,
  7986. pointConfig = [],
  7987. formatter = options.formatter || defaultFormatter,
  7988. hoverPoints = chart.hoverPoints,
  7989. placedTooltipPoint,
  7990. borderColor,
  7991. crosshairsOptions = options.crosshairs,
  7992. shared = tooltip.shared,
  7993. currentSeries;
  7994.  
  7995. // get the reference point coordinates (pie charts use tooltipPos)
  7996. anchor = tooltip.getAnchor(point, mouseEvent);
  7997. x = anchor[0];
  7998. y = anchor[1];
  7999.  
  8000. // shared tooltip, array is sent over
  8001. if (shared && !(point.series && point.series.noSharedTooltip)) {
  8002.  
  8003. // hide previous hoverPoints and set new
  8004. if (hoverPoints) {
  8005. each(hoverPoints, function (point) {
  8006. point.setState();
  8007. });
  8008. }
  8009. chart.hoverPoints = point;
  8010.  
  8011. each(point, function (item) {
  8012. item.setState(HOVER_STATE);
  8013.  
  8014. pointConfig.push(item.getLabelConfig());
  8015. });
  8016.  
  8017. textConfig = {
  8018. x: point[0].category,
  8019. y: point[0].y
  8020. };
  8021. textConfig.points = pointConfig;
  8022. point = point[0];
  8023.  
  8024. // single point tooltip
  8025. } else {
  8026. textConfig = point.getLabelConfig();
  8027. }
  8028. text = formatter.call(textConfig);
  8029.  
  8030. // register the current series
  8031. currentSeries = point.series;
  8032.  
  8033.  
  8034. // For line type series, hide tooltip if the point falls outside the plot
  8035. show = shared || !currentSeries.isCartesian || currentSeries.tooltipOutsidePlot || chart.isInsidePlot(x, y);
  8036.  
  8037. // update the inner HTML
  8038. if (text === false || !show) {
  8039. this.hide();
  8040. } else {
  8041.  
  8042. // show it
  8043. if (tooltip.tooltipIsHidden) {
  8044. label.show();
  8045. }
  8046.  
  8047. // update text
  8048. label.attr({
  8049. text: text
  8050. });
  8051.  
  8052. // set the stroke color of the box
  8053. borderColor = options.borderColor || point.color || currentSeries.color || '#606060';
  8054. label.attr({
  8055. stroke: borderColor
  8056. });
  8057.  
  8058. placedTooltipPoint = (options.positioner || tooltip.getPosition).call(
  8059. tooltip,
  8060. label.width,
  8061. label.height,
  8062. { plotX: x, plotY: y }
  8063. );
  8064.  
  8065. // do the move
  8066. tooltip.move(mathRound(placedTooltipPoint.x), mathRound(placedTooltipPoint.y));
  8067.  
  8068.  
  8069. tooltip.tooltipIsHidden = false;
  8070. }
  8071.  
  8072. // crosshairs
  8073. if (crosshairsOptions) {
  8074. crosshairsOptions = splat(crosshairsOptions); // [x, y]
  8075.  
  8076. var path,
  8077. i = crosshairsOptions.length,
  8078. attribs,
  8079. axis;
  8080.  
  8081. while (i--) {
  8082. axis = point.series[i ? 'yAxis' : 'xAxis'];
  8083. if (crosshairsOptions[i] && axis) {
  8084.  
  8085. path = axis.getPlotLinePath(
  8086. i ? pick(point.stackY, point.y) : point.x, // #814
  8087. 1
  8088. );
  8089.  
  8090. if (tooltip.crosshairs[i]) {
  8091. tooltip.crosshairs[i].attr({ d: path, visibility: VISIBLE });
  8092. } else {
  8093. attribs = {
  8094. 'stroke-width': crosshairsOptions[i].width || 1,
  8095. stroke: crosshairsOptions[i].color || '#C0C0C0',
  8096. zIndex: crosshairsOptions[i].zIndex || 2
  8097. };
  8098. if (crosshairsOptions[i].dashStyle) {
  8099. attribs.dashstyle = crosshairsOptions[i].dashStyle;
  8100. }
  8101. tooltip.crosshairs[i] = chart.renderer.path(path)
  8102. .attr(attribs)
  8103. .add();
  8104. }
  8105. }
  8106. }
  8107. }
  8108. fireEvent(chart, 'tooltipRefresh', {
  8109. text: text,
  8110. x: x + chart.plotLeft,
  8111. y: y + chart.plotTop,
  8112. borderColor: borderColor
  8113. });
  8114. },
  8115.  
  8116. /**
  8117. * Runs the tooltip animation one tick.
  8118. */
  8119. tick: function () {
  8120. if (this.tooltipTick) {
  8121. this.tooltipTick();
  8122. }
  8123. }
  8124. };
  8125. /**
  8126. * The mouse tracker object
  8127. * @param {Object} chart The Chart instance
  8128. * @param {Object} options The root options object
  8129. */
  8130. function MouseTracker(chart, options) {
  8131. var zoomType = useCanVG ? '' : options.chart.zoomType;
  8132.  
  8133. // Zoom status
  8134. this.zoomX = /x/.test(zoomType);
  8135. this.zoomY = /y/.test(zoomType);
  8136.  
  8137. // Store reference to options
  8138. this.options = options;
  8139.  
  8140. // Reference to the chart
  8141. this.chart = chart;
  8142.  
  8143. // The interval id
  8144. //this.tooltipInterval = UNDEFINED;
  8145.  
  8146. // The cached x hover position
  8147. //this.hoverX = UNDEFINED;
  8148.  
  8149. // The chart position
  8150. //this.chartPosition = UNDEFINED;
  8151.  
  8152. // The selection marker element
  8153. //this.selectionMarker = UNDEFINED;
  8154.  
  8155. // False or a value > 0 if a dragging operation
  8156. //this.mouseDownX = UNDEFINED;
  8157. //this.mouseDownY = UNDEFINED;
  8158. this.init(chart, options.tooltip);
  8159. }
  8160.  
  8161. MouseTracker.prototype = {
  8162. /**
  8163. * Add crossbrowser support for chartX and chartY
  8164. * @param {Object} e The event object in standard browsers
  8165. */
  8166. normalizeMouseEvent: function (e) {
  8167. var chartPosition,
  8168. chartX,
  8169. chartY,
  8170. ePos;
  8171.  
  8172. // common IE normalizing
  8173. e = e || win.event;
  8174. if (!e.target) {
  8175. e.target = e.srcElement;
  8176. }
  8177.  
  8178. // jQuery only copies over some properties. IE needs e.x and iOS needs touches.
  8179. if (e.originalEvent) {
  8180. e = e.originalEvent;
  8181. }
  8182.  
  8183. // The same for MooTools. It renames e.pageX to e.page.x. #445.
  8184. if (e.event) {
  8185. e = e.event;
  8186. }
  8187.  
  8188. // iOS
  8189. ePos = e.touches ? e.touches.item(0) : e;
  8190.  
  8191. // get mouse position
  8192. this.chartPosition = chartPosition = offset(this.chart.container);
  8193.  
  8194. // chartX and chartY
  8195. if (ePos.pageX === UNDEFINED) { // IE < 9. #886.
  8196. chartX = e.x;
  8197. chartY = e.y;
  8198. } else {
  8199. chartX = ePos.pageX - chartPosition.left;
  8200. chartY = ePos.pageY - chartPosition.top;
  8201. }
  8202.  
  8203. return extend(e, {
  8204. chartX: mathRound(chartX),
  8205. chartY: mathRound(chartY)
  8206.  
  8207. });
  8208. },
  8209.  
  8210. /**
  8211. * Get the click position in terms of axis values.
  8212. *
  8213. * @param {Object} e A mouse event
  8214. */
  8215. getMouseCoordinates: function (e) {
  8216. var coordinates = {
  8217. xAxis: [],
  8218. yAxis: []
  8219. },
  8220. chart = this.chart;
  8221.  
  8222. each(chart.axes, function (axis) {
  8223. var isXAxis = axis.isXAxis,
  8224. isHorizontal = chart.inverted ? !isXAxis : isXAxis;
  8225.  
  8226. coordinates[isXAxis ? 'xAxis' : 'yAxis'].push({
  8227. axis: axis,
  8228. value: axis.translate(
  8229. isHorizontal ?
  8230. e.chartX - chart.plotLeft :
  8231. chart.plotHeight - e.chartY + chart.plotTop,
  8232. true
  8233. )
  8234. });
  8235. });
  8236. return coordinates;
  8237. },
  8238.  
  8239. /**
  8240. * With line type charts with a single tracker, get the point closest to the mouse
  8241. */
  8242. onmousemove: function (e) {
  8243. var mouseTracker = this,
  8244. chart = mouseTracker.chart,
  8245. series = chart.series,
  8246. point,
  8247. points,
  8248. hoverPoint = chart.hoverPoint,
  8249. hoverSeries = chart.hoverSeries,
  8250. i,
  8251. j,
  8252. distance = chart.chartWidth,
  8253. // the index in the tooltipPoints array, corresponding to pixel position in plot area
  8254. index = chart.inverted ? chart.plotHeight + chart.plotTop - e.chartY : e.chartX - chart.plotLeft;
  8255.  
  8256. // shared tooltip
  8257. if (chart.tooltip && mouseTracker.options.tooltip.shared && !(hoverSeries && hoverSeries.noSharedTooltip)) {
  8258. points = [];
  8259.  
  8260. // loop over all series and find the ones with points closest to the mouse
  8261. i = series.length;
  8262. for (j = 0; j < i; j++) {
  8263. if (series[j].visible &&
  8264. series[j].options.enableMouseTracking !== false &&
  8265. !series[j].noSharedTooltip && series[j].tooltipPoints.length) {
  8266. point = series[j].tooltipPoints[index];
  8267. point._dist = mathAbs(index - point.plotX);
  8268. distance = mathMin(distance, point._dist);
  8269. points.push(point);
  8270. }
  8271. }
  8272. // remove furthest points
  8273. i = points.length;
  8274. while (i--) {
  8275. if (points[i]._dist > distance) {
  8276. points.splice(i, 1);
  8277. }
  8278. }
  8279. // refresh the tooltip if necessary
  8280. if (points.length && (points[0].plotX !== mouseTracker.hoverX)) {
  8281. chart.tooltip.refresh(points, e);
  8282. mouseTracker.hoverX = points[0].plotX;
  8283. }
  8284. }
  8285.  
  8286. // separate tooltip and general mouse events
  8287. if (hoverSeries && hoverSeries.tracker) { // only use for line-type series with common tracker
  8288.  
  8289. // get the point
  8290. point = hoverSeries.tooltipPoints[index];
  8291.  
  8292. // a new point is hovered, refresh the tooltip
  8293. if (point && point !== hoverPoint) {
  8294.  
  8295. // trigger the events
  8296. point.onMouseOver();
  8297.  
  8298. }
  8299. }
  8300. },
  8301.  
  8302.  
  8303.  
  8304. /**
  8305. * Reset the tracking by hiding the tooltip, the hover series state and the hover point
  8306. */
  8307. resetTracker: function (allowMove) {
  8308. var mouseTracker = this,
  8309. chart = mouseTracker.chart,
  8310. hoverSeries = chart.hoverSeries,
  8311. hoverPoint = chart.hoverPoint,
  8312. tooltipPoints = chart.hoverPoints || hoverPoint,
  8313. tooltip = chart.tooltip;
  8314.  
  8315. // Narrow in allowMove
  8316. allowMove = allowMove && tooltip && tooltipPoints;
  8317.  
  8318. // Check if the points have moved outside the plot area, #1003
  8319. if (allowMove && splat(tooltipPoints)[0].plotX === UNDEFINED) {
  8320. allowMove = false;
  8321. }
  8322.  
  8323. // Just move the tooltip, #349
  8324. if (allowMove) {
  8325. tooltip.refresh(tooltipPoints);
  8326.  
  8327. // Full reset
  8328. } else {
  8329.  
  8330. if (hoverPoint) {
  8331. hoverPoint.onMouseOut();
  8332. }
  8333.  
  8334. if (hoverSeries) {
  8335. hoverSeries.onMouseOut();
  8336. }
  8337.  
  8338. if (tooltip) {
  8339. tooltip.hide();
  8340. tooltip.hideCrosshairs();
  8341. }
  8342.  
  8343. mouseTracker.hoverX = null;
  8344.  
  8345. }
  8346. },
  8347.  
  8348. /**
  8349. * Set the JS events on the container element
  8350. */
  8351. setDOMEvents: function () {
  8352. var lastWasOutsidePlot = true,
  8353. mouseTracker = this,
  8354. chart = mouseTracker.chart,
  8355. container = chart.container,
  8356. hasDragged,
  8357. zoomHor = (mouseTracker.zoomX && !chart.inverted) || (mouseTracker.zoomY && chart.inverted),
  8358. zoomVert = (mouseTracker.zoomY && !chart.inverted) || (mouseTracker.zoomX && chart.inverted);
  8359.  
  8360. /**
  8361. * Mouse up or outside the plot area
  8362. */
  8363. function drop() {
  8364. if (mouseTracker.selectionMarker) {
  8365. var selectionData = {
  8366. xAxis: [],
  8367. yAxis: []
  8368. },
  8369. selectionBox = mouseTracker.selectionMarker.getBBox(),
  8370. selectionLeft = selectionBox.x - chart.plotLeft,
  8371. selectionTop = selectionBox.y - chart.plotTop,
  8372. runZoom;
  8373.  
  8374. // a selection has been made
  8375. if (hasDragged) {
  8376.  
  8377. // record each axis' min and max
  8378. each(chart.axes, function (axis) {
  8379. if (axis.options.zoomEnabled !== false) {
  8380. var isXAxis = axis.isXAxis,
  8381. isHorizontal = chart.inverted ? !isXAxis : isXAxis,
  8382. selectionMin = axis.translate(
  8383. isHorizontal ?
  8384. selectionLeft :
  8385. chart.plotHeight - selectionTop - selectionBox.height,
  8386. true,
  8387. 0,
  8388. 0,
  8389. 1
  8390. ),
  8391. selectionMax = axis.translate(
  8392. isHorizontal ?
  8393. selectionLeft + selectionBox.width :
  8394. chart.plotHeight - selectionTop,
  8395. true,
  8396. 0,
  8397. 0,
  8398. 1
  8399. );
  8400.  
  8401. if (!isNaN(selectionMin) && !isNaN(selectionMax)) { // #859
  8402. selectionData[isXAxis ? 'xAxis' : 'yAxis'].push({
  8403. axis: axis,
  8404. min: mathMin(selectionMin, selectionMax), // for reversed axes,
  8405. max: mathMax(selectionMin, selectionMax)
  8406. });
  8407. runZoom = true;
  8408. }
  8409. }
  8410. });
  8411. if (runZoom) {
  8412. fireEvent(chart, 'selection', selectionData, function (args) { chart.zoom(args); });
  8413. }
  8414.  
  8415. }
  8416. mouseTracker.selectionMarker = mouseTracker.selectionMarker.destroy();
  8417. }
  8418.  
  8419. if (chart) { // it may be destroyed on mouse up - #877
  8420. css(container, { cursor: 'auto' });
  8421. chart.cancelClick = hasDragged; // #370
  8422. chart.mouseIsDown = hasDragged = false;
  8423. }
  8424.  
  8425. removeEvent(doc, hasTouch ? 'touchend' : 'mouseup', drop);
  8426. }
  8427.  
  8428. /**
  8429. * Special handler for mouse move that will hide the tooltip when the mouse leaves the plotarea.
  8430. */
  8431. mouseTracker.hideTooltipOnMouseMove = function (e) {
  8432.  
  8433. // Get e.pageX and e.pageY back in MooTools
  8434. washMouseEvent(e);
  8435.  
  8436. // If we're outside, hide the tooltip
  8437. if (mouseTracker.chartPosition && chart.hoverSeries && chart.hoverSeries.isCartesian &&
  8438. !chart.isInsidePlot(e.pageX - mouseTracker.chartPosition.left - chart.plotLeft,
  8439. e.pageY - mouseTracker.chartPosition.top - chart.plotTop)) {
  8440. mouseTracker.resetTracker();
  8441. }
  8442. };
  8443.  
  8444. /**
  8445. * When mouse leaves the container, hide the tooltip.
  8446. */
  8447. mouseTracker.hideTooltipOnMouseLeave = function () {
  8448. mouseTracker.resetTracker();
  8449. mouseTracker.chartPosition = null; // also reset the chart position, used in #149 fix
  8450. };
  8451.  
  8452.  
  8453. /*
  8454. * Record the starting position of a dragoperation
  8455. */
  8456. container.onmousedown = function (e) {
  8457. e = mouseTracker.normalizeMouseEvent(e);
  8458.  
  8459. // issue #295, dragging not always working in Firefox
  8460. if (!hasTouch && e.preventDefault) {
  8461. e.preventDefault();
  8462. }
  8463.  
  8464. // record the start position
  8465. chart.mouseIsDown = true;
  8466. chart.cancelClick = false;
  8467. chart.mouseDownX = mouseTracker.mouseDownX = e.chartX;
  8468. mouseTracker.mouseDownY = e.chartY;
  8469.  
  8470. addEvent(doc, hasTouch ? 'touchend' : 'mouseup', drop);
  8471. };
  8472.  
  8473. // The mousemove, touchmove and touchstart event handler
  8474. var mouseMove = function (e) {
  8475. // let the system handle multitouch operations like two finger scroll
  8476. // and pinching
  8477. if (e && e.touches && e.touches.length > 1) {
  8478. return;
  8479. }
  8480.  
  8481. // normalize
  8482. e = mouseTracker.normalizeMouseEvent(e);
  8483. if (!hasTouch) { // not for touch devices
  8484. e.returnValue = false;
  8485. }
  8486.  
  8487. var chartX = e.chartX,
  8488. chartY = e.chartY,
  8489. isOutsidePlot = !chart.isInsidePlot(chartX - chart.plotLeft, chartY - chart.plotTop);
  8490.  
  8491. // on touch devices, only trigger click if a handler is defined
  8492. if (hasTouch && e.type === 'touchstart') {
  8493. if (attr(e.target, 'isTracker')) {
  8494. if (!chart.runTrackerClick) {
  8495. e.preventDefault();
  8496. }
  8497. } else if (!chart.runChartClick && !isOutsidePlot) {
  8498. e.preventDefault();
  8499. }
  8500. }
  8501.  
  8502. // cancel on mouse outside
  8503. if (isOutsidePlot) {
  8504.  
  8505. /*if (!lastWasOutsidePlot) {
  8506. // reset the tracker
  8507. resetTracker();
  8508. }*/
  8509.  
  8510. // drop the selection if any and reset mouseIsDown and hasDragged
  8511. //drop();
  8512. if (chartX < chart.plotLeft) {
  8513. chartX = chart.plotLeft;
  8514. } else if (chartX > chart.plotLeft + chart.plotWidth) {
  8515. chartX = chart.plotLeft + chart.plotWidth;
  8516. }
  8517.  
  8518. if (chartY < chart.plotTop) {
  8519. chartY = chart.plotTop;
  8520. } else if (chartY > chart.plotTop + chart.plotHeight) {
  8521. chartY = chart.plotTop + chart.plotHeight;
  8522. }
  8523. }
  8524.  
  8525. if (chart.mouseIsDown && e.type !== 'touchstart') { // make selection
  8526.  
  8527. // determine if the mouse has moved more than 10px
  8528. hasDragged = Math.sqrt(
  8529. Math.pow(mouseTracker.mouseDownX - chartX, 2) +
  8530. Math.pow(mouseTracker.mouseDownY - chartY, 2)
  8531. );
  8532. if (hasDragged > 10) {
  8533. var clickedInside = chart.isInsidePlot(mouseTracker.mouseDownX - chart.plotLeft, mouseTracker.mouseDownY - chart.plotTop);
  8534.  
  8535. // make a selection
  8536. if (chart.hasCartesianSeries && (mouseTracker.zoomX || mouseTracker.zoomY) && clickedInside) {
  8537. if (!mouseTracker.selectionMarker) {
  8538. mouseTracker.selectionMarker = chart.renderer.rect(
  8539. chart.plotLeft,
  8540. chart.plotTop,
  8541. zoomHor ? 1 : chart.plotWidth,
  8542. zoomVert ? 1 : chart.plotHeight,
  8543. 0
  8544. )
  8545. .attr({
  8546. fill: mouseTracker.options.chart.selectionMarkerFill || 'rgba(69,114,167,0.25)',
  8547. zIndex: 7
  8548. })
  8549. .add();
  8550. }
  8551. }
  8552.  
  8553. // adjust the width of the selection marker
  8554. if (mouseTracker.selectionMarker && zoomHor) {
  8555. var xSize = chartX - mouseTracker.mouseDownX;
  8556. mouseTracker.selectionMarker.attr({
  8557. width: mathAbs(xSize),
  8558. x: (xSize > 0 ? 0 : xSize) + mouseTracker.mouseDownX
  8559. });
  8560. }
  8561. // adjust the height of the selection marker
  8562. if (mouseTracker.selectionMarker && zoomVert) {
  8563. var ySize = chartY - mouseTracker.mouseDownY;
  8564. mouseTracker.selectionMarker.attr({
  8565. height: mathAbs(ySize),
  8566. y: (ySize > 0 ? 0 : ySize) + mouseTracker.mouseDownY
  8567. });
  8568. }
  8569.  
  8570. // panning
  8571. if (clickedInside && !mouseTracker.selectionMarker && mouseTracker.options.chart.panning) {
  8572. chart.pan(chartX);
  8573. }
  8574. }
  8575.  
  8576. } else if (!isOutsidePlot) {
  8577. // show the tooltip
  8578. mouseTracker.onmousemove(e);
  8579. }
  8580.  
  8581. lastWasOutsidePlot = isOutsidePlot;
  8582.  
  8583. // when outside plot, allow touch-drag by returning true
  8584. return isOutsidePlot || !chart.hasCartesianSeries;
  8585. };
  8586.  
  8587. /*
  8588. * When the mouse enters the container, run mouseMove
  8589. */
  8590. container.onmousemove = mouseMove;
  8591.  
  8592. /*
  8593. * When the mouse leaves the container, hide the tracking (tooltip).
  8594. */
  8595. addEvent(container, 'mouseleave', mouseTracker.hideTooltipOnMouseLeave);
  8596.  
  8597. // issue #149 workaround
  8598. // The mouseleave event above does not always fire. Whenever the mouse is moving
  8599. // outside the plotarea, hide the tooltip
  8600. addEvent(doc, 'mousemove', mouseTracker.hideTooltipOnMouseMove);
  8601.  
  8602. container.ontouchstart = function (e) {
  8603. // For touch devices, use touchmove to zoom
  8604. if (mouseTracker.zoomX || mouseTracker.zoomY) {
  8605. container.onmousedown(e);
  8606. }
  8607. // Show tooltip and prevent the lower mouse pseudo event
  8608. mouseMove(e);
  8609. };
  8610.  
  8611. /*
  8612. * Allow dragging the finger over the chart to read the values on touch
  8613. * devices
  8614. */
  8615. container.ontouchmove = mouseMove;
  8616.  
  8617. /*
  8618. * Allow dragging the finger over the chart to read the values on touch
  8619. * devices
  8620. */
  8621. container.ontouchend = function () {
  8622. if (hasDragged) {
  8623. mouseTracker.resetTracker();
  8624. }
  8625. };
  8626.  
  8627.  
  8628. // MooTools 1.2.3 doesn't fire this in IE when using addEvent
  8629. container.onclick = function (e) {
  8630. var hoverPoint = chart.hoverPoint,
  8631. plotX,
  8632. plotY;
  8633. e = mouseTracker.normalizeMouseEvent(e);
  8634.  
  8635. e.cancelBubble = true; // IE specific
  8636.  
  8637.  
  8638. if (!chart.cancelClick) {
  8639. // Detect clicks on trackers or tracker groups, #783
  8640. if (hoverPoint && (attr(e.target, 'isTracker') || attr(e.target.parentNode, 'isTracker'))) {
  8641. plotX = hoverPoint.plotX;
  8642. plotY = hoverPoint.plotY;
  8643.  
  8644. // add page position info
  8645. extend(hoverPoint, {
  8646. pageX: mouseTracker.chartPosition.left + chart.plotLeft +
  8647. (chart.inverted ? chart.plotWidth - plotY : plotX),
  8648. pageY: mouseTracker.chartPosition.top + chart.plotTop +
  8649. (chart.inverted ? chart.plotHeight - plotX : plotY)
  8650. });
  8651.  
  8652. // the series click event
  8653. fireEvent(hoverPoint.series, 'click', extend(e, {
  8654. point: hoverPoint
  8655. }));
  8656.  
  8657. // the point click event
  8658. hoverPoint.firePointEvent('click', e);
  8659.  
  8660. } else {
  8661. extend(e, mouseTracker.getMouseCoordinates(e));
  8662.  
  8663. // fire a click event in the chart
  8664. if (chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  8665. fireEvent(chart, 'click', e);
  8666. }
  8667. }
  8668.  
  8669.  
  8670. }
  8671. };
  8672.  
  8673. },
  8674.  
  8675. /**
  8676. * Destroys the MouseTracker object and disconnects DOM events.
  8677. */
  8678. destroy: function () {
  8679. var mouseTracker = this,
  8680. chart = mouseTracker.chart,
  8681. container = chart.container;
  8682.  
  8683. // Destroy the tracker group element
  8684. if (chart.trackerGroup) {
  8685. chart.trackerGroup = chart.trackerGroup.destroy();
  8686. }
  8687.  
  8688. removeEvent(container, 'mouseleave', mouseTracker.hideTooltipOnMouseLeave);
  8689. removeEvent(doc, 'mousemove', mouseTracker.hideTooltipOnMouseMove);
  8690. container.onclick = container.onmousedown = container.onmousemove = container.ontouchstart = container.ontouchend = container.ontouchmove = null;
  8691.  
  8692. // memory and CPU leak
  8693. clearInterval(this.tooltipInterval);
  8694. },
  8695.  
  8696. // Run MouseTracker
  8697. init: function (chart, options) {
  8698. if (!chart.trackerGroup) {
  8699. chart.trackerGroup = chart.renderer.g('tracker')
  8700. .attr({ zIndex: 9 })
  8701. .add();
  8702. }
  8703.  
  8704. if (options.enabled) {
  8705. chart.tooltip = new Tooltip(chart, options);
  8706.  
  8707. // set the fixed interval ticking for the smooth tooltip
  8708. this.tooltipInterval = setInterval(function () { chart.tooltip.tick(); }, 32);
  8709. }
  8710.  
  8711. this.setDOMEvents();
  8712. }
  8713. };
  8714. /**
  8715. * The overview of the chart's series
  8716. */
  8717. function Legend(chart) {
  8718.  
  8719. this.init(chart);
  8720. }
  8721.  
  8722. Legend.prototype = {
  8723.  
  8724. /**
  8725. * Initialize the legend
  8726. */
  8727. init: function (chart) {
  8728. var legend = this,
  8729. options = legend.options = chart.options.legend;
  8730.  
  8731. if (!options.enabled) {
  8732. return;
  8733. }
  8734.  
  8735. var //style = options.style || {}, // deprecated
  8736. itemStyle = options.itemStyle,
  8737. padding = pick(options.padding, 8),
  8738. itemMarginTop = options.itemMarginTop || 0;
  8739.  
  8740. legend.baseline = pInt(itemStyle.fontSize) + 3 + itemMarginTop; // used in Series prototype
  8741. legend.itemStyle = itemStyle;
  8742. legend.itemHiddenStyle = merge(itemStyle, options.itemHiddenStyle);
  8743. legend.itemMarginTop = itemMarginTop;
  8744. legend.padding = padding;
  8745. legend.initialItemX = padding;
  8746. legend.initialItemY = padding - 5; // 5 is the number of pixels above the text
  8747. legend.maxItemWidth = 0;
  8748. legend.chart = chart;
  8749. //legend.allItems = UNDEFINED;
  8750. //legend.legendWidth = UNDEFINED;
  8751. //legend.legendHeight = UNDEFINED;
  8752. //legend.offsetWidth = UNDEFINED;
  8753. legend.itemHeight = 0;
  8754. legend.lastLineHeight = 0;
  8755. //legend.itemX = UNDEFINED;
  8756. //legend.itemY = UNDEFINED;
  8757. //legend.lastItemY = UNDEFINED;
  8758.  
  8759. // Elements
  8760. //legend.group = UNDEFINED;
  8761. //legend.box = UNDEFINED;
  8762.  
  8763. // run legend
  8764. legend.render();
  8765.  
  8766. // move checkboxes
  8767. addEvent(legend.chart, 'endResize', function () { legend.positionCheckboxes(); });
  8768.  
  8769. /* // expose
  8770. return {
  8771. colorizeItem: colorizeItem,
  8772. destroyItem: destroyItem,
  8773. render: render,
  8774. destroy: destroy,
  8775. getLegendWidth: getLegendWidth,
  8776. getLegendHeight: getLegendHeight
  8777. };*/
  8778. },
  8779.  
  8780. /**
  8781. * Set the colors for the legend item
  8782. * @param {Object} item A Series or Point instance
  8783. * @param {Object} visible Dimmed or colored
  8784. */
  8785. colorizeItem: function (item, visible) {
  8786. var legend = this,
  8787. options = legend.options,
  8788. legendItem = item.legendItem,
  8789. legendLine = item.legendLine,
  8790. legendSymbol = item.legendSymbol,
  8791. hiddenColor = legend.itemHiddenStyle.color,
  8792. textColor = visible ? options.itemStyle.color : hiddenColor,
  8793. symbolColor = visible ? item.color : hiddenColor;
  8794.  
  8795. if (legendItem) {
  8796. legendItem.css({ fill: textColor });
  8797. }
  8798. if (legendLine) {
  8799. legendLine.attr({ stroke: symbolColor });
  8800. }
  8801. if (legendSymbol) {
  8802. legendSymbol.attr({
  8803. stroke: symbolColor,
  8804. fill: symbolColor
  8805. });
  8806. }
  8807. },
  8808.  
  8809. /**
  8810. * Position the legend item
  8811. * @param {Object} item A Series or Point instance
  8812. */
  8813. positionItem: function (item) {
  8814. var legend = this,
  8815. options = legend.options,
  8816. symbolPadding = options.symbolPadding,
  8817. ltr = !options.rtl,
  8818. legendItemPos = item._legendItemPos,
  8819. itemX = legendItemPos[0],
  8820. itemY = legendItemPos[1],
  8821. checkbox = item.checkbox;
  8822.  
  8823. if (item.legendGroup) {
  8824. item.legendGroup.translate(
  8825. ltr ? itemX : legend.legendWidth - itemX - 2 * symbolPadding - 4,
  8826. itemY
  8827. );
  8828. }
  8829.  
  8830. if (checkbox) {
  8831. checkbox.x = itemX;
  8832. checkbox.y = itemY;
  8833. }
  8834. },
  8835.  
  8836. /**
  8837. * Destroy a single legend item
  8838. * @param {Object} item The series or point
  8839. */
  8840. destroyItem: function (item) {
  8841. var checkbox = item.checkbox;
  8842.  
  8843. // destroy SVG elements
  8844. each(['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'], function (key) {
  8845. if (item[key]) {
  8846. item[key].destroy();
  8847. }
  8848. });
  8849.  
  8850. if (checkbox) {
  8851. discardElement(item.checkbox);
  8852. }
  8853. },
  8854.  
  8855. /**
  8856. * Destroys the legend.
  8857. */
  8858. destroy: function () {
  8859. var legend = this,
  8860. legendGroup = legend.group,
  8861. box = legend.box;
  8862.  
  8863. if (box) {
  8864. legend.box = box.destroy();
  8865. }
  8866.  
  8867. if (legendGroup) {
  8868. legend.group = legendGroup.destroy();
  8869. }
  8870. },
  8871.  
  8872. /**
  8873. * Position the checkboxes after the width is determined
  8874. */
  8875. positionCheckboxes: function () {
  8876. var legend = this;
  8877.  
  8878. each(legend.allItems, function (item) {
  8879. var checkbox = item.checkbox,
  8880. alignAttr = legend.group.alignAttr;
  8881. if (checkbox) {
  8882. css(checkbox, {
  8883. left: (alignAttr.translateX + item.legendItemWidth + checkbox.x - 20) + PX,
  8884. top: (alignAttr.translateY + checkbox.y + 3) + PX
  8885. });
  8886. }
  8887. });
  8888. },
  8889.  
  8890. /**
  8891. * Render a single specific legend item
  8892. * @param {Object} item A series or point
  8893. */
  8894. renderItem: function (item) {
  8895. var legend = this,
  8896. chart = legend.chart,
  8897. renderer = chart.renderer,
  8898. options = legend.options,
  8899. horizontal = options.layout === 'horizontal',
  8900. symbolWidth = options.symbolWidth,
  8901. symbolPadding = options.symbolPadding,
  8902. itemStyle = legend.itemStyle,
  8903. itemHiddenStyle = legend.itemHiddenStyle,
  8904. padding = legend.padding,
  8905. ltr = !options.rtl,
  8906. itemHeight,
  8907. widthOption = options.width,
  8908. itemMarginBottom = options.itemMarginBottom || 0,
  8909. itemMarginTop = legend.itemMarginTop,
  8910. initialItemX = legend.initialItemX,
  8911. bBox,
  8912. itemWidth,
  8913. li = item.legendItem,
  8914. series = item.series || item,
  8915. itemOptions = series.options,
  8916. showCheckbox = itemOptions.showCheckbox;
  8917.  
  8918. if (!li) { // generate it once, later move it
  8919.  
  8920. // Generate the group box
  8921. // A group to hold the symbol and text. Text is to be appended in Legend class.
  8922. item.legendGroup = renderer.g('legend-item')
  8923. .attr({ zIndex: 1 })
  8924. .add(legend.scrollGroup);
  8925.  
  8926. // Draw the legend symbol inside the group box
  8927. series.drawLegendSymbol(legend, item);
  8928.  
  8929. // Generate the list item text and add it to the group
  8930. item.legendItem = li = renderer.text(
  8931. options.labelFormatter.call(item),
  8932. ltr ? symbolWidth + symbolPadding : -symbolPadding,
  8933. legend.baseline,
  8934. options.useHTML
  8935. )
  8936. .css(merge(item.visible ? itemStyle : itemHiddenStyle)) // merge to prevent modifying original (#1021)
  8937. .attr({
  8938. align: ltr ? 'left' : 'right',
  8939. zIndex: 2
  8940. })
  8941. .add(item.legendGroup);
  8942.  
  8943. // Set the events on the item group
  8944. item.legendGroup.on('mouseover', function () {
  8945. item.setState(HOVER_STATE);
  8946. li.css(legend.options.itemHoverStyle);
  8947. })
  8948. .on('mouseout', function () {
  8949. li.css(item.visible ? itemStyle : itemHiddenStyle);
  8950. item.setState();
  8951. })
  8952. .on('click', function (event) {
  8953. var strLegendItemClick = 'legendItemClick',
  8954. fnLegendItemClick = function () {
  8955. item.setVisible();
  8956. };
  8957.  
  8958. // Pass over the click/touch event. #4.
  8959. event = {
  8960. browserEvent: event
  8961. };
  8962.  
  8963. // click the name or symbol
  8964. if (item.firePointEvent) { // point
  8965. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  8966. } else {
  8967. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  8968. }
  8969. });
  8970.  
  8971. // Colorize the items
  8972. legend.colorizeItem(item, item.visible);
  8973.  
  8974. // add the HTML checkbox on top
  8975. if (itemOptions && showCheckbox) {
  8976. item.checkbox = createElement('input', {
  8977. type: 'checkbox',
  8978. checked: item.selected,
  8979. defaultChecked: item.selected // required by IE7
  8980. }, options.itemCheckboxStyle, chart.container);
  8981.  
  8982. addEvent(item.checkbox, 'click', function (event) {
  8983. var target = event.target;
  8984. fireEvent(item, 'checkboxClick', {
  8985. checked: target.checked
  8986. },
  8987. function () {
  8988. item.select();
  8989. }
  8990. );
  8991. });
  8992. }
  8993. }
  8994.  
  8995. // calculate the positions for the next line
  8996. bBox = li.getBBox();
  8997.  
  8998. itemWidth = item.legendItemWidth =
  8999. options.itemWidth || symbolWidth + symbolPadding + bBox.width + padding +
  9000. (showCheckbox ? 20 : 0);
  9001. legend.itemHeight = itemHeight = bBox.height;
  9002.  
  9003. // if the item exceeds the width, start a new line
  9004. if (horizontal && legend.itemX - initialItemX + itemWidth >
  9005. (widthOption || (chart.chartWidth - 2 * padding - initialItemX))) {
  9006. legend.itemX = initialItemX;
  9007. legend.itemY += itemMarginTop + legend.lastLineHeight + itemMarginBottom;
  9008. legend.lastLineHeight = 0; // reset for next line
  9009. }
  9010.  
  9011. // If the item exceeds the height, start a new column
  9012. /*if (!horizontal && legend.itemY + options.y + itemHeight > chart.chartHeight - spacingTop - spacingBottom) {
  9013. legend.itemY = legend.initialItemY;
  9014. legend.itemX += legend.maxItemWidth;
  9015. legend.maxItemWidth = 0;
  9016. }*/
  9017.  
  9018. // Set the edge positions
  9019. legend.maxItemWidth = mathMax(legend.maxItemWidth, itemWidth);
  9020. legend.lastItemY = itemMarginTop + legend.itemY + itemMarginBottom;
  9021. legend.lastLineHeight = mathMax(itemHeight, legend.lastLineHeight); // #915
  9022.  
  9023. // cache the position of the newly generated or reordered items
  9024. item._legendItemPos = [legend.itemX, legend.itemY];
  9025.  
  9026. // advance
  9027. if (horizontal) {
  9028. legend.itemX += itemWidth;
  9029.  
  9030. } else {
  9031. legend.itemY += itemMarginTop + itemHeight + itemMarginBottom;
  9032. legend.lastLineHeight = itemHeight;
  9033. }
  9034.  
  9035. // the width of the widest item
  9036. legend.offsetWidth = widthOption || mathMax(
  9037. horizontal ? legend.itemX - initialItemX : itemWidth,
  9038. legend.offsetWidth
  9039. );
  9040. },
  9041.  
  9042. /**
  9043. * Render the legend. This method can be called both before and after
  9044. * chart.render. If called after, it will only rearrange items instead
  9045. * of creating new ones.
  9046. */
  9047. render: function () {
  9048. var legend = this,
  9049. chart = legend.chart,
  9050. renderer = chart.renderer,
  9051. legendGroup = legend.group,
  9052. allItems,
  9053. display,
  9054. legendWidth,
  9055. legendHeight,
  9056. box = legend.box,
  9057. options = legend.options,
  9058. padding = legend.padding,
  9059. legendBorderWidth = options.borderWidth,
  9060. legendBackgroundColor = options.backgroundColor;
  9061.  
  9062. legend.itemX = legend.initialItemX;
  9063. legend.itemY = legend.initialItemY;
  9064. legend.offsetWidth = 0;
  9065. legend.lastItemY = 0;
  9066.  
  9067. if (!legendGroup) {
  9068. legend.group = legendGroup = renderer.g('legend')
  9069. // #414, #759. Trackers will be drawn above the legend, but we have
  9070. // to sacrifice that because tooltips need to be above the legend
  9071. // and trackers above tooltips
  9072. .attr({ zIndex: 7 })
  9073. .add();
  9074. legend.contentGroup = renderer.g()
  9075. .attr({ zIndex: 1 }) // above background
  9076. .add(legendGroup);
  9077. legend.scrollGroup = renderer.g()
  9078. .add(legend.contentGroup);
  9079. legend.clipRect = renderer.clipRect(0, 0, 9999, chart.chartHeight);
  9080. legend.contentGroup.clip(legend.clipRect);
  9081. }
  9082.  
  9083. // add each series or point
  9084. allItems = [];
  9085. each(chart.series, function (serie) {
  9086. var seriesOptions = serie.options;
  9087.  
  9088. if (!seriesOptions.showInLegend) {
  9089. return;
  9090. }
  9091.  
  9092. // use points or series for the legend item depending on legendType
  9093. allItems = allItems.concat(
  9094. serie.legendItems ||
  9095. (seriesOptions.legendType === 'point' ?
  9096. serie.data :
  9097. serie)
  9098. );
  9099. });
  9100.  
  9101. // sort by legendIndex
  9102. stableSort(allItems, function (a, b) {
  9103. return (a.options.legendIndex || 0) - (b.options.legendIndex || 0);
  9104. });
  9105.  
  9106. // reversed legend
  9107. if (options.reversed) {
  9108. allItems.reverse();
  9109. }
  9110.  
  9111. legend.allItems = allItems;
  9112. legend.display = display = !!allItems.length;
  9113.  
  9114. // render the items
  9115. each(allItems, function (item) {
  9116. legend.renderItem(item);
  9117. });
  9118.  
  9119. // Draw the border
  9120. legendWidth = options.width || legend.offsetWidth;
  9121. legendHeight = legend.lastItemY + legend.lastLineHeight;
  9122.  
  9123.  
  9124. legendHeight = legend.handleOverflow(legendHeight);
  9125.  
  9126. if (legendBorderWidth || legendBackgroundColor) {
  9127. legendWidth += padding;
  9128. legendHeight += padding;
  9129.  
  9130. if (!box) {
  9131. legend.box = box = renderer.rect(
  9132. 0,
  9133. 0,
  9134. legendWidth,
  9135. legendHeight,
  9136. options.borderRadius,
  9137. legendBorderWidth || 0
  9138. ).attr({
  9139. stroke: options.borderColor,
  9140. 'stroke-width': legendBorderWidth || 0,
  9141. fill: legendBackgroundColor || NONE
  9142. })
  9143. .add(legendGroup)
  9144. .shadow(options.shadow);
  9145. box.isNew = true;
  9146.  
  9147. } else if (legendWidth > 0 && legendHeight > 0) {
  9148. box[box.isNew ? 'attr' : 'animate'](
  9149. box.crisp(null, null, null, legendWidth, legendHeight)
  9150. );
  9151. box.isNew = false;
  9152. }
  9153.  
  9154. // hide the border if no items
  9155. box[display ? 'show' : 'hide']();
  9156. }
  9157.  
  9158. legend.legendWidth = legendWidth;
  9159. legend.legendHeight = legendHeight;
  9160.  
  9161. // Now that the legend width and height are established, put the items in the
  9162. // final position
  9163. each(allItems, function (item) {
  9164. legend.positionItem(item);
  9165. });
  9166.  
  9167. // 1.x compatibility: positioning based on style
  9168. /*var props = ['left', 'right', 'top', 'bottom'],
  9169. prop,
  9170. i = 4;
  9171. while (i--) {
  9172. prop = props[i];
  9173. if (options.style[prop] && options.style[prop] !== 'auto') {
  9174. options[i < 2 ? 'align' : 'verticalAlign'] = prop;
  9175. options[i < 2 ? 'x' : 'y'] = pInt(options.style[prop]) * (i % 2 ? -1 : 1);
  9176. }
  9177. }*/
  9178.  
  9179. if (display) {
  9180. legendGroup.align(extend({
  9181. width: legendWidth,
  9182. height: legendHeight
  9183. }, options), true, chart.spacingBox);
  9184. }
  9185.  
  9186. if (!chart.isResizing) {
  9187. this.positionCheckboxes();
  9188. }
  9189. },
  9190.  
  9191. /**
  9192. * Set up the overflow handling by adding navigation with up and down arrows below the
  9193. * legend.
  9194. */
  9195. handleOverflow: function (legendHeight) {
  9196. var legend = this,
  9197. chart = this.chart,
  9198. renderer = chart.renderer,
  9199. pageCount,
  9200. options = this.options,
  9201. optionsY = options.y,
  9202. alignTop = options.verticalAlign === 'top',
  9203. spaceHeight = chart.spacingBox.height + (alignTop ? -optionsY : optionsY) - this.padding,
  9204. maxHeight = options.maxHeight, // docs
  9205. clipHeight,
  9206. clipRect = this.clipRect,
  9207. navOptions = options.navigation,
  9208. animation = pick(navOptions.animation, true),
  9209. arrowSize = navOptions.arrowSize || 12,
  9210. nav = this.nav;
  9211.  
  9212. // Adjust the height
  9213. if (options.layout === 'horizontal') {
  9214. spaceHeight /= 2;
  9215. }
  9216. if (maxHeight) {
  9217. spaceHeight = mathMin(spaceHeight, maxHeight);
  9218. }
  9219.  
  9220. // Reset the legend height and adjust the clipping rectangle
  9221. if (legendHeight > spaceHeight) {
  9222.  
  9223. this.clipHeight = clipHeight = spaceHeight - 20;
  9224. this.pageCount = pageCount = mathCeil(legendHeight / clipHeight);
  9225. this.currentPage = pick(this.currentPage, 1);
  9226. this.fullHeight = legendHeight;
  9227.  
  9228. clipRect.attr({
  9229. height: clipHeight
  9230. });
  9231.  
  9232. // Add navigation elements
  9233. if (!nav) {
  9234. this.nav = nav = renderer.g().attr({ zIndex: 1 }).add(this.group);
  9235. this.up = renderer.symbol('triangle', 0, 0, arrowSize, arrowSize)
  9236. .on('click', function () {
  9237. legend.scroll(-1, animation);
  9238. })
  9239. .add(nav);
  9240. this.pager = renderer.text('', 15, 10)
  9241. .css(navOptions.style)
  9242. .add(nav);
  9243. this.down = renderer.symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  9244. .on('click', function () {
  9245. legend.scroll(1, animation);
  9246. })
  9247. .add(nav);
  9248. }
  9249.  
  9250. // Set initial position
  9251. legend.scroll(0);
  9252.  
  9253. legendHeight = spaceHeight;
  9254.  
  9255. } else if (nav) {
  9256. clipRect.attr({
  9257. height: chart.chartHeight
  9258. });
  9259. nav.hide();
  9260. this.scrollGroup.attr({
  9261. translateY: 1
  9262. });
  9263. }
  9264.  
  9265. return legendHeight;
  9266. },
  9267.  
  9268. /**
  9269. * Scroll the legend by a number of pages
  9270. * @param {Object} scrollBy
  9271. * @param {Object} animation
  9272. */
  9273. scroll: function (scrollBy, animation) {
  9274. var pageCount = this.pageCount,
  9275. currentPage = this.currentPage + scrollBy,
  9276. clipHeight = this.clipHeight,
  9277. navOptions = this.options.navigation,
  9278. activeColor = navOptions.activeColor,
  9279. inactiveColor = navOptions.inactiveColor,
  9280. pager = this.pager,
  9281. padding = this.padding;
  9282.  
  9283. // When resizing while looking at the last page
  9284. if (currentPage > pageCount) {
  9285. currentPage = pageCount;
  9286. }
  9287.  
  9288. if (currentPage > 0) {
  9289.  
  9290. if (animation !== UNDEFINED) {
  9291. setAnimation(animation, this.chart);
  9292. }
  9293.  
  9294. this.nav.attr({
  9295. translateX: padding,
  9296. translateY: clipHeight + 7,
  9297. visibility: VISIBLE
  9298. });
  9299. this.up.attr({
  9300. fill: currentPage === 1 ? inactiveColor : activeColor
  9301. })
  9302. .css({
  9303. cursor: currentPage === 1 ? 'default' : 'pointer'
  9304. });
  9305. pager.attr({
  9306. text: currentPage + '/' + this.pageCount
  9307. });
  9308. this.down.attr({
  9309. x: 18 + this.pager.getBBox().width, // adjust to text width
  9310. fill: currentPage === pageCount ? inactiveColor : activeColor
  9311. })
  9312. .css({
  9313. cursor: currentPage === pageCount ? 'default' : 'pointer'
  9314. });
  9315.  
  9316. this.scrollGroup.animate({
  9317. translateY: -mathMin(clipHeight * (currentPage - 1), this.fullHeight - clipHeight + padding) + 1
  9318. });
  9319. pager.attr({
  9320. text: currentPage + '/' + pageCount
  9321. });
  9322.  
  9323.  
  9324. this.currentPage = currentPage;
  9325. }
  9326.  
  9327. }
  9328.  
  9329. };
  9330.  
  9331.  
  9332. /**
  9333. * The chart class
  9334. * @param {Object} options
  9335. * @param {Function} callback Function to run when the chart has loaded
  9336. */
  9337. function Chart(userOptions, callback) {
  9338. // Handle regular options
  9339. var options,
  9340. seriesOptions = userOptions.series; // skip merging data points to increase performance
  9341. userOptions.series = null;
  9342. options = merge(defaultOptions, userOptions); // do the merge
  9343. options.series = userOptions.series = seriesOptions; // set back the series data
  9344.  
  9345. var optionsChart = options.chart,
  9346. optionsMargin = optionsChart.margin,
  9347. margin = isObject(optionsMargin) ?
  9348. optionsMargin :
  9349. [optionsMargin, optionsMargin, optionsMargin, optionsMargin];
  9350.  
  9351. this.optionsMarginTop = pick(optionsChart.marginTop, margin[0]);
  9352. this.optionsMarginRight = pick(optionsChart.marginRight, margin[1]);
  9353. this.optionsMarginBottom = pick(optionsChart.marginBottom, margin[2]);
  9354. this.optionsMarginLeft = pick(optionsChart.marginLeft, margin[3]);
  9355.  
  9356. var chartEvents = optionsChart.events;
  9357.  
  9358. this.runChartClick = chartEvents && !!chartEvents.click;
  9359. this.callback = callback;
  9360. this.isResizing = 0;
  9361. this.options = options;
  9362. //chartTitleOptions = UNDEFINED;
  9363. //chartSubtitleOptions = UNDEFINED;
  9364.  
  9365. this.axes = [];
  9366. this.series = [];
  9367. this.hasCartesianSeries = optionsChart.showAxes;
  9368. //this.axisOffset = UNDEFINED;
  9369. //this.maxTicks = UNDEFINED; // handle the greatest amount of ticks on grouped axes
  9370. //this.inverted = UNDEFINED;
  9371. //this.loadingShown = UNDEFINED;
  9372. //this.container = UNDEFINED;
  9373. //this.chartWidth = UNDEFINED;
  9374. //this.chartHeight = UNDEFINED;
  9375. //this.marginRight = UNDEFINED;
  9376. //this.marginBottom = UNDEFINED;
  9377. //this.containerWidth = UNDEFINED;
  9378. //this.containerHeight = UNDEFINED;
  9379. //this.oldChartWidth = UNDEFINED;
  9380. //this.oldChartHeight = UNDEFINED;
  9381.  
  9382. //this.renderTo = UNDEFINED;
  9383. //this.renderToClone = UNDEFINED;
  9384. //this.tracker = UNDEFINED;
  9385.  
  9386. //this.spacingBox = UNDEFINED
  9387.  
  9388. //this.legend = UNDEFINED;
  9389.  
  9390. // Elements
  9391. //this.chartBackground = UNDEFINED;
  9392. //this.plotBackground = UNDEFINED;
  9393. //this.plotBGImage = UNDEFINED;
  9394. //this.plotBorder = UNDEFINED;
  9395. //this.loadingDiv = UNDEFINED;
  9396. //this.loadingSpan = UNDEFINED;
  9397.  
  9398. this.init(chartEvents);
  9399. }
  9400.  
  9401. Chart.prototype = {
  9402.  
  9403. /**
  9404. * Initialize an individual series, called internally before render time
  9405. */
  9406. initSeries: function (options) {
  9407. var chart = this,
  9408. optionsChart = chart.options.chart,
  9409. type = options.type || optionsChart.type || optionsChart.defaultSeriesType,
  9410. series = new seriesTypes[type]();
  9411.  
  9412. series.init(this, options);
  9413. return series;
  9414. },
  9415.  
  9416. /**
  9417. * Add a series dynamically after time
  9418. *
  9419. * @param {Object} options The config options
  9420. * @param {Boolean} redraw Whether to redraw the chart after adding. Defaults to true.
  9421. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9422. * configuration
  9423. *
  9424. * @return {Object} series The newly created series object
  9425. */
  9426. addSeries: function (options, redraw, animation) {
  9427. var series,
  9428. chart = this;
  9429.  
  9430. if (options) {
  9431. setAnimation(animation, chart);
  9432. redraw = pick(redraw, true); // defaults to true
  9433.  
  9434. fireEvent(chart, 'addSeries', { options: options }, function () {
  9435. chart.initSeries(options);
  9436. //series = chart.initSeries(options);
  9437. //series.isDirty = true;
  9438.  
  9439. chart.isDirtyLegend = true; // the series array is out of sync with the display
  9440. if (redraw) {
  9441. chart.redraw();
  9442. }
  9443. });
  9444. }
  9445.  
  9446. return series;
  9447. },
  9448.  
  9449. /**
  9450. * Check whether a given point is within the plot area
  9451. *
  9452. * @param {Number} x Pixel x relative to the plot area
  9453. * @param {Number} y Pixel y relative to the plot area
  9454. */
  9455. isInsidePlot: function (x, y) {
  9456. return x >= 0 &&
  9457. x <= this.plotWidth &&
  9458. y >= 0 &&
  9459. y <= this.plotHeight;
  9460. },
  9461.  
  9462. /**
  9463. * Adjust all axes tick amounts
  9464. */
  9465. adjustTickAmounts: function () {
  9466. if (this.options.chart.alignTicks !== false) {
  9467. each(this.axes, function (axis) {
  9468. axis.adjustTickAmount();
  9469. });
  9470. }
  9471. this.maxTicks = null;
  9472. },
  9473.  
  9474. /**
  9475. * Redraw legend, axes or series based on updated data
  9476. *
  9477. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  9478. * configuration
  9479. */
  9480. redraw: function (animation) {
  9481. var chart = this,
  9482. axes = chart.axes,
  9483. series = chart.series,
  9484. tracker = chart.tracker,
  9485. legend = chart.legend,
  9486. redrawLegend = chart.isDirtyLegend,
  9487. hasStackedSeries,
  9488. isDirtyBox = chart.isDirtyBox, // todo: check if it has actually changed?
  9489. seriesLength = series.length,
  9490. i = seriesLength,
  9491. clipRect = chart.clipRect,
  9492. serie,
  9493. renderer = chart.renderer,
  9494. isHiddenChart = renderer.isHidden();
  9495.  
  9496. setAnimation(animation, chart);
  9497.  
  9498. if (isHiddenChart) {
  9499. chart.cloneRenderTo();
  9500. }
  9501.  
  9502. // link stacked series
  9503. while (i--) {
  9504. serie = series[i];
  9505. if (serie.isDirty && serie.options.stacking) {
  9506. hasStackedSeries = true;
  9507. break;
  9508. }
  9509. }
  9510. if (hasStackedSeries) { // mark others as dirty
  9511. i = seriesLength;
  9512. while (i--) {
  9513. serie = series[i];
  9514. if (serie.options.stacking) {
  9515. serie.isDirty = true;
  9516. }
  9517. }
  9518. }
  9519.  
  9520. // handle updated data in the series
  9521. each(series, function (serie) {
  9522. if (serie.isDirty) { // prepare the data so axis can read it
  9523. if (serie.options.legendType === 'point') {
  9524. redrawLegend = true;
  9525. }
  9526. }
  9527. });
  9528.  
  9529. // handle added or removed series
  9530. if (redrawLegend && legend.options.enabled) { // series or pie points are added or removed
  9531. // draw legend graphics
  9532. legend.render();
  9533.  
  9534. chart.isDirtyLegend = false;
  9535. }
  9536.  
  9537.  
  9538. if (chart.hasCartesianSeries) {
  9539. if (!chart.isResizing) {
  9540.  
  9541. // reset maxTicks
  9542. chart.maxTicks = null;
  9543.  
  9544. // set axes scales
  9545. each(axes, function (axis) {
  9546. axis.setScale();
  9547. });
  9548. }
  9549. chart.adjustTickAmounts();
  9550. chart.getMargins();
  9551.  
  9552. // redraw axes
  9553. each(axes, function (axis) {
  9554.  
  9555. // Fire 'afterSetExtremes' only if extremes are set
  9556. if (axis.isDirtyExtremes) { // #821
  9557. axis.isDirtyExtremes = false;
  9558. fireEvent(axis, 'afterSetExtremes', axis.getExtremes()); // #747, #751
  9559. }
  9560.  
  9561. if (axis.isDirty || isDirtyBox || hasStackedSeries) {
  9562. axis.redraw();
  9563. isDirtyBox = true; // #792
  9564. }
  9565. });
  9566.  
  9567.  
  9568. }
  9569.  
  9570. // the plot areas size has changed
  9571. if (isDirtyBox) {
  9572. chart.drawChartBox();
  9573.  
  9574. // move clip rect
  9575. if (clipRect) {
  9576. stop(clipRect);
  9577. clipRect.animate({ // for chart resize
  9578. width: chart.plotSizeX,
  9579. height: chart.plotSizeY + 1
  9580. });
  9581. }
  9582.  
  9583. }
  9584.  
  9585.  
  9586. // redraw affected series
  9587. each(series, function (serie) {
  9588. if (serie.isDirty && serie.visible &&
  9589. (!serie.isCartesian || serie.xAxis)) { // issue #153
  9590. serie.redraw();
  9591. }
  9592. });
  9593.  
  9594.  
  9595. // move tooltip or reset
  9596. if (tracker && tracker.resetTracker) {
  9597. tracker.resetTracker(true);
  9598. }
  9599.  
  9600. // redraw if canvas
  9601. renderer.draw();
  9602.  
  9603. // fire the event
  9604. fireEvent(chart, 'redraw'); // jQuery breaks this when calling it from addEvent. Overwrites chart.redraw
  9605.  
  9606. if (isHiddenChart) {
  9607. chart.cloneRenderTo(true);
  9608. }
  9609. },
  9610.  
  9611.  
  9612.  
  9613. /**
  9614. * Dim the chart and show a loading text or symbol
  9615. * @param {String} str An optional text to show in the loading label instead of the default one
  9616. */
  9617. showLoading: function (str) {
  9618. var chart = this,
  9619. options = chart.options,
  9620. loadingDiv = chart.loadingDiv;
  9621.  
  9622. var loadingOptions = options.loading;
  9623.  
  9624. // create the layer at the first call
  9625. if (!loadingDiv) {
  9626. chart.loadingDiv = loadingDiv = createElement(DIV, {
  9627. className: PREFIX + 'loading'
  9628. }, extend(loadingOptions.style, {
  9629. left: chart.plotLeft + PX,
  9630. top: chart.plotTop + PX,
  9631. width: chart.plotWidth + PX,
  9632. height: chart.plotHeight + PX,
  9633. zIndex: 10,
  9634. display: NONE
  9635. }), chart.container);
  9636.  
  9637. chart.loadingSpan = createElement(
  9638. 'span',
  9639. null,
  9640. loadingOptions.labelStyle,
  9641. loadingDiv
  9642. );
  9643.  
  9644. }
  9645.  
  9646. // update text
  9647. chart.loadingSpan.innerHTML = str || options.lang.loading;
  9648.  
  9649. // show it
  9650. if (!chart.loadingShown) {
  9651. css(loadingDiv, { opacity: 0, display: '' });
  9652. animate(loadingDiv, {
  9653. opacity: loadingOptions.style.opacity
  9654. }, {
  9655. duration: loadingOptions.showDuration || 0
  9656. });
  9657. chart.loadingShown = true;
  9658. }
  9659. },
  9660.  
  9661. /**
  9662. * Hide the loading layer
  9663. */
  9664. hideLoading: function () {
  9665. var options = this.options,
  9666. loadingDiv = this.loadingDiv;
  9667.  
  9668. if (loadingDiv) {
  9669. animate(loadingDiv, {
  9670. opacity: 0
  9671. }, {
  9672. duration: options.loading.hideDuration || 100,
  9673. complete: function () {
  9674. css(loadingDiv, { display: NONE });
  9675. }
  9676. });
  9677. }
  9678. this.loadingShown = false;
  9679. },
  9680.  
  9681. /**
  9682. * Get an axis, series or point object by id.
  9683. * @param id {String} The id as given in the configuration options
  9684. */
  9685. get: function (id) {
  9686. var chart = this,
  9687. axes = chart.axes,
  9688. series = chart.series;
  9689.  
  9690. var i,
  9691. j,
  9692. points;
  9693.  
  9694. // search axes
  9695. for (i = 0; i < axes.length; i++) {
  9696. if (axes[i].options.id === id) {
  9697. return axes[i];
  9698. }
  9699. }
  9700.  
  9701. // search series
  9702. for (i = 0; i < series.length; i++) {
  9703. if (series[i].options.id === id) {
  9704. return series[i];
  9705. }
  9706. }
  9707.  
  9708. // search points
  9709. for (i = 0; i < series.length; i++) {
  9710. points = series[i].points || [];
  9711. for (j = 0; j < points.length; j++) {
  9712. if (points[j].id === id) {
  9713. return points[j];
  9714. }
  9715. }
  9716. }
  9717. return null;
  9718. },
  9719.  
  9720. /**
  9721. * Create the Axis instances based on the config options
  9722. */
  9723. getAxes: function () {
  9724. var chart = this,
  9725. options = this.options;
  9726.  
  9727. var xAxisOptions = options.xAxis || {},
  9728. yAxisOptions = options.yAxis || {},
  9729. optionsArray,
  9730. axis;
  9731.  
  9732. // make sure the options are arrays and add some members
  9733. xAxisOptions = splat(xAxisOptions);
  9734. each(xAxisOptions, function (axis, i) {
  9735. axis.index = i;
  9736. axis.isX = true;
  9737. });
  9738.  
  9739. yAxisOptions = splat(yAxisOptions);
  9740. each(yAxisOptions, function (axis, i) {
  9741. axis.index = i;
  9742. });
  9743.  
  9744. // concatenate all axis options into one array
  9745. optionsArray = xAxisOptions.concat(yAxisOptions);
  9746.  
  9747. each(optionsArray, function (axisOptions) {
  9748. axis = new Axis(chart, axisOptions);
  9749. });
  9750.  
  9751. chart.adjustTickAmounts();
  9752. },
  9753.  
  9754.  
  9755. /**
  9756. * Get the currently selected points from all series
  9757. */
  9758. getSelectedPoints: function () {
  9759. var points = [];
  9760. each(this.series, function (serie) {
  9761. points = points.concat(grep(serie.points, function (point) {
  9762. return point.selected;
  9763. }));
  9764. });
  9765. return points;
  9766. },
  9767.  
  9768. /**
  9769. * Get the currently selected series
  9770. */
  9771. getSelectedSeries: function () {
  9772. return grep(this.series, function (serie) {
  9773. return serie.selected;
  9774. });
  9775. },
  9776.  
  9777. /**
  9778. * Display the zoom button
  9779. */
  9780. showResetZoom: function () {
  9781. var chart = this,
  9782. lang = defaultOptions.lang,
  9783. btnOptions = chart.options.chart.resetZoomButton,
  9784. theme = btnOptions.theme,
  9785. states = theme.states,
  9786. box = btnOptions.relativeTo === 'chart' ? null : {
  9787. x: chart.plotLeft,
  9788. y: chart.plotTop,
  9789. width: chart.plotWidth,
  9790. height: chart.plotHeight
  9791. };
  9792. this.resetZoomButton = chart.renderer.button(lang.resetZoom, null, null, function () { chart.zoomOut(); }, theme, states && states.hover)
  9793. .attr({
  9794. align: btnOptions.position.align,
  9795. title: lang.resetZoomTitle
  9796. })
  9797. .add()
  9798. .align(btnOptions.position, false, box);
  9799. },
  9800.  
  9801. /**
  9802. * Zoom out to 1:1
  9803. */
  9804. zoomOut: function () {
  9805. var chart = this,
  9806. resetZoomButton = chart.resetZoomButton;
  9807.  
  9808. fireEvent(chart, 'selection', { resetSelection: true }, function () { chart.zoom(); });
  9809. if (resetZoomButton) {
  9810. chart.resetZoomButton = resetZoomButton.destroy();
  9811. }
  9812. },
  9813.  
  9814. /**
  9815. * Zoom into a given portion of the chart given by axis coordinates
  9816. * @param {Object} event
  9817. */
  9818. zoom: function (event) {
  9819. var chart = this,
  9820. optionsChart = chart.options.chart;
  9821.  
  9822. // add button to reset selection
  9823. var hasZoomed;
  9824.  
  9825. if (chart.resetZoomEnabled !== false && !chart.resetZoomButton) { // hook for Stock charts etc.
  9826. chart.showResetZoom();
  9827. }
  9828.  
  9829. // if zoom is called with no arguments, reset the axes
  9830. if (!event || event.resetSelection) {
  9831. each(chart.axes, function (axis) {
  9832. if (axis.options.zoomEnabled !== false) {
  9833. axis.setExtremes(null, null, false);
  9834. hasZoomed = true;
  9835. }
  9836. });
  9837. } else { // else, zoom in on all axes
  9838. each(event.xAxis.concat(event.yAxis), function (axisData) {
  9839. var axis = axisData.axis;
  9840.  
  9841. // don't zoom more than minRange
  9842. if (chart.tracker[axis.isXAxis ? 'zoomX' : 'zoomY']) {
  9843. axis.setExtremes(axisData.min, axisData.max, false);
  9844. hasZoomed = true;
  9845. }
  9846. });
  9847. }
  9848.  
  9849. // Redraw
  9850. if (hasZoomed) {
  9851. chart.redraw(
  9852. pick(optionsChart.animation, chart.pointCount < 100) // animation
  9853. );
  9854. }
  9855. },
  9856.  
  9857. /**
  9858. * Pan the chart by dragging the mouse across the pane. This function is called
  9859. * on mouse move, and the distance to pan is computed from chartX compared to
  9860. * the first chartX position in the dragging operation.
  9861. */
  9862. pan: function (chartX) {
  9863. var chart = this;
  9864.  
  9865. var xAxis = chart.xAxis[0],
  9866. mouseDownX = chart.mouseDownX,
  9867. halfPointRange = xAxis.pointRange / 2,
  9868. extremes = xAxis.getExtremes(),
  9869. newMin = xAxis.translate(mouseDownX - chartX, true) + halfPointRange,
  9870. newMax = xAxis.translate(mouseDownX + chart.plotWidth - chartX, true) - halfPointRange,
  9871. hoverPoints = chart.hoverPoints;
  9872.  
  9873. // remove active points for shared tooltip
  9874. if (hoverPoints) {
  9875. each(hoverPoints, function (point) {
  9876. point.setState();
  9877. });
  9878. }
  9879.  
  9880. if (xAxis.series.length && newMin > mathMin(extremes.dataMin, extremes.min) && newMax < mathMax(extremes.dataMax, extremes.max)) {
  9881. xAxis.setExtremes(newMin, newMax, true, false);
  9882. }
  9883.  
  9884. chart.mouseDownX = chartX; // set new reference for next run
  9885. css(chart.container, { cursor: 'move' });
  9886. },
  9887.  
  9888. /**
  9889. * Show the title and subtitle of the chart
  9890. *
  9891. * @param titleOptions {Object} New title options
  9892. * @param subtitleOptions {Object} New subtitle options
  9893. *
  9894. */
  9895. setTitle: function (titleOptions, subtitleOptions) {
  9896. var chart = this,
  9897. options = chart.options,
  9898. chartTitleOptions,
  9899. chartSubtitleOptions;
  9900.  
  9901. chart.chartTitleOptions = chartTitleOptions = merge(options.title, titleOptions);
  9902. chart.chartSubtitleOptions = chartSubtitleOptions = merge(options.subtitle, subtitleOptions);
  9903.  
  9904. // add title and subtitle
  9905. each([
  9906. ['title', titleOptions, chartTitleOptions],
  9907. ['subtitle', subtitleOptions, chartSubtitleOptions]
  9908. ], function (arr) {
  9909. var name = arr[0],
  9910. title = chart[name],
  9911. titleOptions = arr[1],
  9912. chartTitleOptions = arr[2];
  9913.  
  9914. if (title && titleOptions) {
  9915. title = title.destroy(); // remove old
  9916. }
  9917. if (chartTitleOptions && chartTitleOptions.text && !title) {
  9918. chart[name] = chart.renderer.text(
  9919. chartTitleOptions.text,
  9920. 0,
  9921. 0,
  9922. chartTitleOptions.useHTML
  9923. )
  9924. .attr({
  9925. align: chartTitleOptions.align,
  9926. 'class': PREFIX + name,
  9927. zIndex: chartTitleOptions.zIndex || 4
  9928. })
  9929. .css(chartTitleOptions.style)
  9930. .add()
  9931. .align(chartTitleOptions, false, chart.spacingBox);
  9932. }
  9933. });
  9934.  
  9935. },
  9936.  
  9937. /**
  9938. * Get chart width and height according to options and container size
  9939. */
  9940. getChartSize: function () {
  9941. var chart = this,
  9942. optionsChart = chart.options.chart,
  9943. renderTo = chart.renderToClone || chart.renderTo;
  9944.  
  9945. // get inner width and height from jQuery (#824)
  9946. chart.containerWidth = adapterRun(renderTo, 'width');
  9947. chart.containerHeight = adapterRun(renderTo, 'height');
  9948.  
  9949. chart.chartWidth = optionsChart.width || chart.containerWidth || 600;
  9950. chart.chartHeight = optionsChart.height ||
  9951. // the offsetHeight of an empty container is 0 in standard browsers, but 19 in IE7:
  9952. (chart.containerHeight > 19 ? chart.containerHeight : 400);
  9953. },
  9954.  
  9955. /**
  9956. * Create a clone of the chart's renderTo div and place it outside the viewport to allow
  9957. * size computation on chart.render and chart.redraw
  9958. */
  9959. cloneRenderTo: function (revert) {
  9960. var clone = this.renderToClone,
  9961. container = this.container;
  9962.  
  9963. // Destroy the clone and bring the container back to the real renderTo div
  9964. if (revert) {
  9965. if (clone) {
  9966. this.renderTo.appendChild(container);
  9967. discardElement(clone);
  9968. delete this.renderToClone;
  9969. }
  9970.  
  9971. // Set up the clone
  9972. } else {
  9973. if (container) {
  9974. this.renderTo.removeChild(container); // do not clone this
  9975. }
  9976. this.renderToClone = clone = this.renderTo.cloneNode(0);
  9977. css(clone, {
  9978. position: ABSOLUTE,
  9979. top: '-9999px',
  9980. display: 'block' // #833
  9981. });
  9982. doc.body.appendChild(clone);
  9983. if (container) {
  9984. clone.appendChild(container);
  9985. }
  9986. }
  9987. },
  9988.  
  9989. /**
  9990. * Get the containing element, determine the size and create the inner container
  9991. * div to hold the chart
  9992. */
  9993. getContainer: function () {
  9994. var chart = this,
  9995. container,
  9996. optionsChart = chart.options.chart,
  9997. chartWidth,
  9998. chartHeight,
  9999. renderTo,
  10000. containerId;
  10001.  
  10002. chart.renderTo = renderTo = optionsChart.renderTo;
  10003. containerId = PREFIX + idCounter++;
  10004.  
  10005. if (isString(renderTo)) {
  10006. chart.renderTo = renderTo = doc.getElementById(renderTo);
  10007. }
  10008.  
  10009. // Display an error if the renderTo is wrong
  10010. if (!renderTo) {
  10011. error(13, true);
  10012. }
  10013.  
  10014. // remove previous chart
  10015. renderTo.innerHTML = '';
  10016.  
  10017. // If the container doesn't have an offsetWidth, it has or is a child of a node
  10018. // that has display:none. We need to temporarily move it out to a visible
  10019. // state to determine the size, else the legend and tooltips won't render
  10020. // properly
  10021. if (!renderTo.offsetWidth) {
  10022. chart.cloneRenderTo();
  10023. }
  10024.  
  10025. // get the width and height
  10026. chart.getChartSize();
  10027. chartWidth = chart.chartWidth;
  10028. chartHeight = chart.chartHeight;
  10029.  
  10030. // create the inner container
  10031. chart.container = container = createElement(DIV, {
  10032. className: PREFIX + 'container' +
  10033. (optionsChart.className ? ' ' + optionsChart.className : ''),
  10034. id: containerId
  10035. }, extend({
  10036. position: RELATIVE,
  10037. overflow: HIDDEN, // needed for context menu (avoid scrollbars) and
  10038. // content overflow in IE
  10039. width: chartWidth + PX,
  10040. height: chartHeight + PX,
  10041. textAlign: 'left',
  10042. lineHeight: 'normal' // #427
  10043. }, optionsChart.style),
  10044. chart.renderToClone || renderTo
  10045. );
  10046.  
  10047. chart.renderer =
  10048. optionsChart.forExport ? // force SVG, used for SVG export
  10049. new SVGRenderer(container, chartWidth, chartHeight, true) :
  10050. new Renderer(container, chartWidth, chartHeight);
  10051.  
  10052. if (useCanVG) {
  10053. // If we need canvg library, extend and configure the renderer
  10054. // to get the tracker for translating mouse events
  10055. chart.renderer.create(chart, container, chartWidth, chartHeight);
  10056. }
  10057. },
  10058.  
  10059. /**
  10060. * Calculate margins by rendering axis labels in a preliminary position. Title,
  10061. * subtitle and legend have already been rendered at this stage, but will be
  10062. * moved into their final positions
  10063. */
  10064. getMargins: function () {
  10065. var chart = this,
  10066. optionsChart = chart.options.chart,
  10067. spacingTop = optionsChart.spacingTop,
  10068. spacingRight = optionsChart.spacingRight,
  10069. spacingBottom = optionsChart.spacingBottom,
  10070. spacingLeft = optionsChart.spacingLeft,
  10071. axisOffset,
  10072. legend = chart.legend,
  10073. optionsMarginTop = chart.optionsMarginTop,
  10074. optionsMarginLeft = chart.optionsMarginLeft,
  10075. optionsMarginRight = chart.optionsMarginRight,
  10076. optionsMarginBottom = chart.optionsMarginBottom,
  10077. chartTitleOptions = chart.chartTitleOptions,
  10078. chartSubtitleOptions = chart.chartSubtitleOptions,
  10079. legendOptions = chart.options.legend,
  10080. legendMargin = pick(legendOptions.margin, 10),
  10081. legendX = legendOptions.x,
  10082. legendY = legendOptions.y,
  10083. align = legendOptions.align,
  10084. verticalAlign = legendOptions.verticalAlign,
  10085. titleOffset;
  10086.  
  10087. chart.resetMargins();
  10088. axisOffset = chart.axisOffset;
  10089.  
  10090. // adjust for title and subtitle
  10091. if ((chart.title || chart.subtitle) && !defined(chart.optionsMarginTop)) {
  10092. titleOffset = mathMax(
  10093. (chart.title && !chartTitleOptions.floating && !chartTitleOptions.verticalAlign && chartTitleOptions.y) || 0,
  10094. (chart.subtitle && !chartSubtitleOptions.floating && !chartSubtitleOptions.verticalAlign && chartSubtitleOptions.y) || 0
  10095. );
  10096. if (titleOffset) {
  10097. chart.plotTop = mathMax(chart.plotTop, titleOffset + pick(chartTitleOptions.margin, 15) + spacingTop);
  10098. }
  10099. }
  10100. // adjust for legend
  10101. if (legend.display && !legendOptions.floating) {
  10102. if (align === 'right') { // horizontal alignment handled first
  10103. if (!defined(optionsMarginRight)) {
  10104. chart.marginRight = mathMax(
  10105. chart.marginRight,
  10106. legend.legendWidth - legendX + legendMargin + spacingRight
  10107. );
  10108. }
  10109. } else if (align === 'left') {
  10110. if (!defined(optionsMarginLeft)) {
  10111. chart.plotLeft = mathMax(
  10112. chart.plotLeft,
  10113. legend.legendWidth + legendX + legendMargin + spacingLeft
  10114. );
  10115. }
  10116.  
  10117. } else if (verticalAlign === 'top') {
  10118. if (!defined(optionsMarginTop)) {
  10119. chart.plotTop = mathMax(
  10120. chart.plotTop,
  10121. legend.legendHeight + legendY + legendMargin + spacingTop
  10122. );
  10123. }
  10124.  
  10125. } else if (verticalAlign === 'bottom') {
  10126. if (!defined(optionsMarginBottom)) {
  10127. chart.marginBottom = mathMax(
  10128. chart.marginBottom,
  10129. legend.legendHeight - legendY + legendMargin + spacingBottom
  10130. );
  10131. }
  10132. }
  10133. }
  10134.  
  10135. // adjust for scroller
  10136. if (chart.extraBottomMargin) {
  10137. chart.marginBottom += chart.extraBottomMargin;
  10138. }
  10139. if (chart.extraTopMargin) {
  10140. chart.plotTop += chart.extraTopMargin;
  10141. }
  10142.  
  10143. // pre-render axes to get labels offset width
  10144. if (chart.hasCartesianSeries) {
  10145. each(chart.axes, function (axis) {
  10146. axis.getOffset();
  10147. });
  10148. }
  10149.  
  10150. if (!defined(optionsMarginLeft)) {
  10151. chart.plotLeft += axisOffset[3];
  10152. }
  10153. if (!defined(optionsMarginTop)) {
  10154. chart.plotTop += axisOffset[0];
  10155. }
  10156. if (!defined(optionsMarginBottom)) {
  10157. chart.marginBottom += axisOffset[2];
  10158. }
  10159. if (!defined(optionsMarginRight)) {
  10160. chart.marginRight += axisOffset[1];
  10161. }
  10162.  
  10163. chart.setChartSize();
  10164.  
  10165. },
  10166.  
  10167. /**
  10168. * Add the event handlers necessary for auto resizing
  10169. *
  10170. */
  10171. initReflow: function () {
  10172. var chart = this,
  10173. optionsChart = chart.options.chart,
  10174. renderTo = chart.renderTo;
  10175.  
  10176. var reflowTimeout;
  10177. function reflow(e) {
  10178. var width = optionsChart.width || adapterRun(renderTo, 'width'),
  10179. height = optionsChart.height || adapterRun(renderTo, 'height'),
  10180. target = e ? e.target : win; // #805 - MooTools doesn't supply e
  10181.  
  10182. // Width and height checks for display:none. Target is doc in IE8 and Opera,
  10183. // win in Firefox, Chrome and IE9.
  10184. if (width && height && (target === win || target === doc)) {
  10185.  
  10186. if (width !== chart.containerWidth || height !== chart.containerHeight) {
  10187. clearTimeout(reflowTimeout);
  10188. reflowTimeout = setTimeout(function () {
  10189. chart.resize(width, height, false);
  10190. }, 100);
  10191. }
  10192. chart.containerWidth = width;
  10193. chart.containerHeight = height;
  10194. }
  10195. }
  10196. addEvent(win, 'resize', reflow);
  10197. addEvent(chart, 'destroy', function () {
  10198. removeEvent(win, 'resize', reflow);
  10199. });
  10200. },
  10201.  
  10202. /**
  10203. * Fires endResize event on chart instance.
  10204. */
  10205. fireEndResize: function () {
  10206. var chart = this;
  10207.  
  10208. if (chart) {
  10209. fireEvent(chart, 'endResize', null, function () {
  10210. chart.isResizing -= 1;
  10211. });
  10212. }
  10213. },
  10214.  
  10215. /**
  10216. * Resize the chart to a given width and height
  10217. * @param {Number} width
  10218. * @param {Number} height
  10219. * @param {Object|Boolean} animation
  10220. */
  10221. // TODO: This method is called setSize in the api
  10222. resize: function (width, height, animation) {
  10223. var chart = this,
  10224. chartWidth,
  10225. chartHeight,
  10226. spacingBox,
  10227. chartTitle = chart.title,
  10228. chartSubtitle = chart.subtitle;
  10229.  
  10230. chart.isResizing += 1;
  10231.  
  10232. // set the animation for the current process
  10233. setAnimation(animation, chart);
  10234.  
  10235. chart.oldChartHeight = chart.chartHeight;
  10236. chart.oldChartWidth = chart.chartWidth;
  10237. if (defined(width)) {
  10238. chart.chartWidth = chartWidth = mathRound(width);
  10239. }
  10240. if (defined(height)) {
  10241. chart.chartHeight = chartHeight = mathRound(height);
  10242. }
  10243.  
  10244. css(chart.container, {
  10245. width: chartWidth + PX,
  10246. height: chartHeight + PX
  10247. });
  10248. chart.renderer.setSize(chartWidth, chartHeight, animation);
  10249.  
  10250. // update axis lengths for more correct tick intervals:
  10251. chart.plotWidth = chartWidth - chart.plotLeft - chart.marginRight;
  10252. chart.plotHeight = chartHeight - chart.plotTop - chart.marginBottom;
  10253.  
  10254. // handle axes
  10255. chart.maxTicks = null;
  10256. each(chart.axes, function (axis) {
  10257. axis.isDirty = true;
  10258. axis.setScale();
  10259. });
  10260.  
  10261. // make sure non-cartesian series are also handled
  10262. each(chart.series, function (serie) {
  10263. serie.isDirty = true;
  10264. });
  10265.  
  10266. chart.isDirtyLegend = true; // force legend redraw
  10267. chart.isDirtyBox = true; // force redraw of plot and chart border
  10268.  
  10269. chart.getMargins();
  10270.  
  10271. // move titles
  10272. spacingBox = chart.spacingBox;
  10273. if (chartTitle) {
  10274. chartTitle.align(null, null, spacingBox);
  10275. }
  10276. if (chartSubtitle) {
  10277. chartSubtitle.align(null, null, spacingBox);
  10278. }
  10279.  
  10280. chart.redraw(animation);
  10281.  
  10282.  
  10283. chart.oldChartHeight = null;
  10284. fireEvent(chart, 'resize');
  10285.  
  10286. // fire endResize and set isResizing back
  10287. // If animation is disabled, fire without delay
  10288. if (globalAnimation === false) {
  10289. chart.fireEndResize();
  10290. } else { // else set a timeout with the animation duration
  10291. setTimeout(chart.fireEndResize, (globalAnimation && globalAnimation.duration) || 500);
  10292. }
  10293. },
  10294.  
  10295. /**
  10296. * Set the public chart properties. This is done before and after the pre-render
  10297. * to determine margin sizes
  10298. */
  10299. setChartSize: function () {
  10300. var chart = this,
  10301. inverted = chart.inverted,
  10302. chartWidth = chart.chartWidth,
  10303. chartHeight = chart.chartHeight,
  10304. optionsChart = chart.options.chart,
  10305. spacingTop = optionsChart.spacingTop,
  10306. spacingRight = optionsChart.spacingRight,
  10307. spacingBottom = optionsChart.spacingBottom,
  10308. spacingLeft = optionsChart.spacingLeft;
  10309.  
  10310. chart.plotLeft = mathRound(chart.plotLeft);
  10311. chart.plotTop = mathRound(chart.plotTop);
  10312. chart.plotWidth = mathRound(chartWidth - chart.plotLeft - chart.marginRight);
  10313. chart.plotHeight = mathRound(chartHeight - chart.plotTop - chart.marginBottom);
  10314.  
  10315. chart.plotSizeX = inverted ? chart.plotHeight : chart.plotWidth;
  10316. chart.plotSizeY = inverted ? chart.plotWidth : chart.plotHeight;
  10317.  
  10318. chart.spacingBox = {
  10319. x: spacingLeft,
  10320. y: spacingTop,
  10321. width: chartWidth - spacingLeft - spacingRight,
  10322. height: chartHeight - spacingTop - spacingBottom
  10323. };
  10324.  
  10325. each(chart.axes, function (axis) {
  10326. axis.setAxisSize();
  10327. axis.setAxisTranslation();
  10328. });
  10329. },
  10330.  
  10331. /**
  10332. * Initial margins before auto size margins are applied
  10333. */
  10334. resetMargins: function () {
  10335. var chart = this,
  10336. optionsChart = chart.options.chart,
  10337. spacingTop = optionsChart.spacingTop,
  10338. spacingRight = optionsChart.spacingRight,
  10339. spacingBottom = optionsChart.spacingBottom,
  10340. spacingLeft = optionsChart.spacingLeft;
  10341.  
  10342. chart.plotTop = pick(chart.optionsMarginTop, spacingTop);
  10343. chart.marginRight = pick(chart.optionsMarginRight, spacingRight);
  10344. chart.marginBottom = pick(chart.optionsMarginBottom, spacingBottom);
  10345. chart.plotLeft = pick(chart.optionsMarginLeft, spacingLeft);
  10346. chart.axisOffset = [0, 0, 0, 0]; // top, right, bottom, left
  10347. },
  10348.  
  10349. /**
  10350. * Draw the borders and backgrounds for chart and plot area
  10351. */
  10352. drawChartBox: function () {
  10353. var chart = this,
  10354. optionsChart = chart.options.chart,
  10355. renderer = chart.renderer,
  10356. chartWidth = chart.chartWidth,
  10357. chartHeight = chart.chartHeight,
  10358. chartBackground = chart.chartBackground,
  10359. plotBackground = chart.plotBackground,
  10360. plotBorder = chart.plotBorder,
  10361. plotBGImage = chart.plotBGImage,
  10362. chartBorderWidth = optionsChart.borderWidth || 0,
  10363. chartBackgroundColor = optionsChart.backgroundColor,
  10364. plotBackgroundColor = optionsChart.plotBackgroundColor,
  10365. plotBackgroundImage = optionsChart.plotBackgroundImage,
  10366. mgn,
  10367. bgAttr,
  10368. plotSize = {
  10369. x: chart.plotLeft,
  10370. y: chart.plotTop,
  10371. width: chart.plotWidth,
  10372. height: chart.plotHeight
  10373. };
  10374.  
  10375. // Chart area
  10376. mgn = chartBorderWidth + (optionsChart.shadow ? 8 : 0);
  10377.  
  10378. if (chartBorderWidth || chartBackgroundColor) {
  10379. if (!chartBackground) {
  10380.  
  10381. bgAttr = {
  10382. fill: chartBackgroundColor || NONE
  10383. };
  10384. if (chartBorderWidth) { // #980
  10385. bgAttr.stroke = optionsChart.borderColor;
  10386. bgAttr['stroke-width'] = chartBorderWidth;
  10387. }
  10388. chart.chartBackground = renderer.rect(mgn / 2, mgn / 2, chartWidth - mgn, chartHeight - mgn,
  10389. optionsChart.borderRadius, chartBorderWidth)
  10390. .attr(bgAttr)
  10391. .add()
  10392. .shadow(optionsChart.shadow);
  10393. } else { // resize
  10394. chartBackground.animate(
  10395. chartBackground.crisp(null, null, null, chartWidth - mgn, chartHeight - mgn)
  10396. );
  10397. }
  10398. }
  10399.  
  10400.  
  10401. // Plot background
  10402. if (plotBackgroundColor) {
  10403. if (!plotBackground) {
  10404. chart.plotBackground = renderer.rect(chart.plotLeft, chart.plotTop, chart.plotWidth, chart.plotHeight, 0)
  10405. .attr({
  10406. fill: plotBackgroundColor
  10407. })
  10408. .add()
  10409. .shadow(optionsChart.plotShadow);
  10410. } else {
  10411. plotBackground.animate(plotSize);
  10412. }
  10413. }
  10414. if (plotBackgroundImage) {
  10415. if (!plotBGImage) {
  10416. chart.plotBGImage = renderer.image(plotBackgroundImage, chart.plotLeft, chart.plotTop, chart.plotWidth, chart.plotHeight)
  10417. .add();
  10418. } else {
  10419. plotBGImage.animate(plotSize);
  10420. }
  10421. }
  10422.  
  10423. // Plot area border
  10424. if (optionsChart.plotBorderWidth) {
  10425. if (!plotBorder) {
  10426. chart.plotBorder = renderer.rect(chart.plotLeft, chart.plotTop, chart.plotWidth, chart.plotHeight, 0, optionsChart.plotBorderWidth)
  10427. .attr({
  10428. stroke: optionsChart.plotBorderColor,
  10429. 'stroke-width': optionsChart.plotBorderWidth,
  10430. zIndex: 4
  10431. })
  10432. .add();
  10433. } else {
  10434. plotBorder.animate(
  10435. plotBorder.crisp(null, chart.plotLeft, chart.plotTop, chart.plotWidth, chart.plotHeight)
  10436. );
  10437. }
  10438. }
  10439.  
  10440. // reset
  10441. chart.isDirtyBox = false;
  10442. },
  10443.  
  10444. /**
  10445. * Detect whether a certain chart property is needed based on inspecting its options
  10446. * and series. This mainly applies to the chart.invert property, and in extensions to
  10447. * the chart.angular and chart.polar properties.
  10448. */
  10449. propFromSeries: function () {
  10450. var chart = this,
  10451. optionsChart = chart.options.chart,
  10452. klass,
  10453. seriesOptions = chart.options.series,
  10454. i,
  10455. value;
  10456.  
  10457.  
  10458. each(['inverted', 'angular', 'polar'], function (key) {
  10459.  
  10460. // The default series type's class
  10461. klass = seriesTypes[optionsChart.type || optionsChart.defaultSeriesType];
  10462.  
  10463. // Get the value from available chart-wide properties
  10464. value = (
  10465. chart[key] || // 1. it is set before
  10466. optionsChart[key] || // 2. it is set in the options
  10467. (klass && klass.prototype[key]) // 3. it's default series class requires it
  10468. );
  10469.  
  10470. // 4. Check if any the chart's series require it
  10471. i = seriesOptions && seriesOptions.length;
  10472. while (!value && i--) {
  10473. klass = seriesTypes[seriesOptions[i].type];
  10474. if (klass && klass.prototype[key]) {
  10475. value = true;
  10476. }
  10477. }
  10478.  
  10479. // Set the chart property
  10480. chart[key] = value;
  10481. });
  10482.  
  10483. },
  10484.  
  10485. /**
  10486. * Render all graphics for the chart
  10487. */
  10488. render: function () {
  10489. var chart = this,
  10490. axes = chart.axes,
  10491. renderer = chart.renderer,
  10492. options = chart.options;
  10493.  
  10494. var labels = options.labels,
  10495. credits = options.credits,
  10496. creditsHref;
  10497.  
  10498. // Title
  10499. chart.setTitle();
  10500.  
  10501.  
  10502. // Legend
  10503. chart.legend = new Legend(chart);
  10504.  
  10505. // Get margins by pre-rendering axes
  10506. // set axes scales
  10507. each(axes, function (axis) {
  10508. axis.setScale();
  10509. });
  10510. chart.getMargins();
  10511.  
  10512. chart.maxTicks = null; // reset for second pass
  10513. each(axes, function (axis) {
  10514. axis.setTickPositions(true); // update to reflect the new margins
  10515. axis.setMaxTicks();
  10516. });
  10517. chart.adjustTickAmounts();
  10518. chart.getMargins(); // second pass to check for new labels
  10519.  
  10520.  
  10521. // Draw the borders and backgrounds
  10522. chart.drawChartBox();
  10523.  
  10524.  
  10525. // Axes
  10526. if (chart.hasCartesianSeries) {
  10527. each(axes, function (axis) {
  10528. axis.render();
  10529. });
  10530. }
  10531.  
  10532. // The series
  10533. if (!chart.seriesGroup) {
  10534. chart.seriesGroup = renderer.g('series-group')
  10535. .attr({ zIndex: 3 })
  10536. .add();
  10537. }
  10538. each(chart.series, function (serie) {
  10539. serie.translate();
  10540. serie.setTooltipPoints();
  10541. serie.render();
  10542. });
  10543.  
  10544. // Labels
  10545. if (labels.items) {
  10546. each(labels.items, function () {
  10547. var style = extend(labels.style, this.style),
  10548. x = pInt(style.left) + chart.plotLeft,
  10549. y = pInt(style.top) + chart.plotTop + 12;
  10550.  
  10551. // delete to prevent rewriting in IE
  10552. delete style.left;
  10553. delete style.top;
  10554.  
  10555. renderer.text(
  10556. this.html,
  10557. x,
  10558. y
  10559. )
  10560. .attr({ zIndex: 2 })
  10561. .css(style)
  10562. .add();
  10563.  
  10564. });
  10565. }
  10566.  
  10567. // Credits
  10568. if (credits.enabled && !chart.credits) {
  10569. creditsHref = credits.href;
  10570. chart.credits = renderer.text(
  10571. credits.text,
  10572. 0,
  10573. 0
  10574. )
  10575. .on('click', function () {
  10576. if (creditsHref) {
  10577. location.href = creditsHref;
  10578. }
  10579. })
  10580. .attr({
  10581. align: credits.position.align,
  10582. zIndex: 8
  10583. })
  10584. .css(credits.style)
  10585. .add()
  10586. .align(credits.position);
  10587. }
  10588.  
  10589. // Set flag
  10590. chart.hasRendered = true;
  10591.  
  10592. },
  10593.  
  10594. /**
  10595. * Clean up memory usage
  10596. */
  10597. destroy: function () {
  10598. var chart = this,
  10599. axes = chart.axes,
  10600. series = chart.series,
  10601. container = chart.container;
  10602.  
  10603. var i,
  10604. parentNode = container && container.parentNode;
  10605.  
  10606. // If the chart is destroyed already, do nothing.
  10607. // This will happen if if a script invokes chart.destroy and
  10608. // then it will be called again on win.unload
  10609. if (chart === null) {
  10610. return;
  10611. }
  10612.  
  10613. // fire the chart.destoy event
  10614. fireEvent(chart, 'destroy');
  10615.  
  10616. // remove events
  10617. removeEvent(chart);
  10618.  
  10619. // ==== Destroy collections:
  10620. // Destroy axes
  10621. i = axes.length;
  10622. while (i--) {
  10623. axes[i] = axes[i].destroy();
  10624. }
  10625.  
  10626. // Destroy each series
  10627. i = series.length;
  10628. while (i--) {
  10629. series[i] = series[i].destroy();
  10630. }
  10631.  
  10632. // ==== Destroy chart properties:
  10633. each(['title', 'subtitle', 'chartBackground', 'plotBackground', 'plotBGImage', 'plotBorder', 'seriesGroup', 'clipRect', 'credits', 'tracker', 'scroller', 'rangeSelector', 'legend', 'resetZoomButton', 'tooltip', 'renderer'], function (name) {
  10634. var prop = chart[name];
  10635.  
  10636. if (prop) {
  10637. chart[name] = prop.destroy();
  10638. }
  10639. });
  10640.  
  10641. // remove container and all SVG
  10642. if (container) { // can break in IE when destroyed before finished loading
  10643. container.innerHTML = '';
  10644. removeEvent(container);
  10645. if (parentNode) {
  10646. discardElement(container);
  10647. }
  10648.  
  10649. // IE6 leak
  10650. container = null;
  10651. }
  10652.  
  10653. // clean it all up
  10654. for (i in chart) {
  10655. delete chart[i];
  10656. }
  10657.  
  10658. chart.options = null;
  10659. chart = null;
  10660. },
  10661.  
  10662. /**
  10663. * Prepare for first rendering after all data are loaded
  10664. */
  10665. firstRender: function () {
  10666. var chart = this,
  10667. options = chart.options,
  10668. callback = chart.callback;
  10669.  
  10670. // VML namespaces can't be added until after complete. Listening
  10671. // for Perini's doScroll hack is not enough.
  10672. var ONREADYSTATECHANGE = 'onreadystatechange',
  10673. COMPLETE = 'complete';
  10674. // Note: in spite of JSLint's complaints, win == win.top is required
  10675. /*jslint eqeq: true*/
  10676. if ((!hasSVG && (win == win.top && doc.readyState !== COMPLETE)) || (useCanVG && !win.canvg)) {
  10677. /*jslint eqeq: false*/
  10678. if (useCanVG) {
  10679. // Delay rendering until canvg library is downloaded and ready
  10680. CanVGController.push(function () { chart.firstRender(); }, options.global.canvasToolsURL);
  10681. } else {
  10682. doc.attachEvent(ONREADYSTATECHANGE, function () {
  10683. doc.detachEvent(ONREADYSTATECHANGE, chart.firstRender);
  10684. if (doc.readyState === COMPLETE) {
  10685. chart.firstRender();
  10686. }
  10687. });
  10688. }
  10689. return;
  10690. }
  10691.  
  10692. // create the container
  10693. chart.getContainer();
  10694.  
  10695. // Run an early event after the container and renderer are established
  10696. fireEvent(chart, 'init');
  10697.  
  10698. // Initialize range selector for stock charts
  10699. if (Highcharts.RangeSelector && options.rangeSelector.enabled) {
  10700. chart.rangeSelector = new Highcharts.RangeSelector(chart);
  10701. }
  10702.  
  10703. chart.resetMargins();
  10704. chart.setChartSize();
  10705.  
  10706. // Set the common chart properties (mainly invert) from the given series
  10707. chart.propFromSeries();
  10708.  
  10709. // get axes
  10710. chart.getAxes();
  10711.  
  10712. // Initialize the series
  10713. each(options.series || [], function (serieOptions) {
  10714. chart.initSeries(serieOptions);
  10715. });
  10716.  
  10717. // Run an event where series and axes can be added
  10718. //fireEvent(chart, 'beforeRender');
  10719.  
  10720. // Initialize scroller for stock charts
  10721. if (Highcharts.Scroller && (options.navigator.enabled || options.scrollbar.enabled)) {
  10722. chart.scroller = new Highcharts.Scroller(chart);
  10723. }
  10724.  
  10725. // depends on inverted and on margins being set
  10726. chart.tracker = new MouseTracker(chart, options);
  10727.  
  10728. chart.render();
  10729.  
  10730. // add canvas
  10731. chart.renderer.draw();
  10732. // run callbacks
  10733. if (callback) {
  10734. callback.apply(chart, [chart]);
  10735. }
  10736. each(chart.callbacks, function (fn) {
  10737. fn.apply(chart, [chart]);
  10738. });
  10739.  
  10740.  
  10741. // If the chart was rendered outside the top container, put it back in
  10742. chart.cloneRenderTo(true);
  10743.  
  10744. fireEvent(chart, 'load');
  10745.  
  10746. },
  10747.  
  10748. init: function (chartEvents) {
  10749. var chart = this,
  10750. optionsChart = chart.options.chart,
  10751. eventType;
  10752.  
  10753. // Run chart
  10754.  
  10755. // Set up auto resize
  10756. if (optionsChart.reflow !== false) {
  10757. addEvent(chart, 'load', chart.initReflow);
  10758. }
  10759.  
  10760. // Chart event handlers
  10761. if (chartEvents) {
  10762. for (eventType in chartEvents) {
  10763. addEvent(chart, eventType, chartEvents[eventType]);
  10764. }
  10765. }
  10766.  
  10767. chart.xAxis = [];
  10768. chart.yAxis = [];
  10769.  
  10770. // Expose methods and variables
  10771. chart.animation = useCanVG ? false : pick(optionsChart.animation, true);
  10772. chart.setSize = chart.resize;
  10773. chart.pointCount = 0;
  10774. chart.counters = new ChartCounters();
  10775. /*
  10776. if ($) $(function () {
  10777. $container = $('#container');
  10778. var origChartWidth,
  10779. origChartHeight;
  10780. if ($container) {
  10781. $('<button>+</button>')
  10782. .insertBefore($container)
  10783. .click(function () {
  10784. if (origChartWidth === UNDEFINED) {
  10785. origChartWidth = chartWidth;
  10786. origChartHeight = chartHeight;
  10787. }
  10788. chart.resize(chartWidth *= 1.1, chartHeight *= 1.1);
  10789. });
  10790. $('<button>-</button>')
  10791. .insertBefore($container)
  10792. .click(function () {
  10793. if (origChartWidth === UNDEFINED) {
  10794. origChartWidth = chartWidth;
  10795. origChartHeight = chartHeight;
  10796. }
  10797. chart.resize(chartWidth *= 0.9, chartHeight *= 0.9);
  10798. });
  10799. $('<button>1:1</button>')
  10800. .insertBefore($container)
  10801. .click(function () {
  10802. if (origChartWidth === UNDEFINED) {
  10803. origChartWidth = chartWidth;
  10804. origChartHeight = chartHeight;
  10805. }
  10806. chart.resize(origChartWidth, origChartHeight);
  10807. });
  10808. }
  10809. })
  10810. */
  10811.  
  10812. chart.firstRender();
  10813. }
  10814. }; // end Chart
  10815.  
  10816. // Hook for exporting module
  10817. Chart.prototype.callbacks = [];
  10818. /**
  10819. * The Point object and prototype. Inheritable and used as base for PiePoint
  10820. */
  10821. var Point = function () {};
  10822. Point.prototype = {
  10823.  
  10824. /**
  10825. * Initialize the point
  10826. * @param {Object} series The series object containing this point
  10827. * @param {Object} options The data in either number, array or object format
  10828. */
  10829. init: function (series, options, x) {
  10830. var point = this,
  10831. counters = series.chart.counters,
  10832. defaultColors;
  10833. point.series = series;
  10834. point.applyOptions(options, x);
  10835. point.pointAttr = {};
  10836.  
  10837. if (series.options.colorByPoint) {
  10838. defaultColors = series.chart.options.colors;
  10839. if (!point.options) {
  10840. point.options = {};
  10841. }
  10842. point.color = point.options.color = point.color || defaultColors[counters.color++];
  10843.  
  10844. // loop back to zero
  10845. counters.wrapColor(defaultColors.length);
  10846. }
  10847.  
  10848. series.chart.pointCount++;
  10849. return point;
  10850. },
  10851. /**
  10852. * Apply the options containing the x and y data and possible some extra properties.
  10853. * This is called on point init or from point.update.
  10854. *
  10855. * @param {Object} options
  10856. */
  10857. applyOptions: function (options, x) {
  10858. var point = this,
  10859. series = point.series,
  10860. optionsType = typeof options;
  10861.  
  10862. point.config = options;
  10863.  
  10864. // onedimensional array input
  10865. if (optionsType === 'number' || options === null) {
  10866. point.y = options;
  10867. } else if (typeof options[0] === 'number') { // two-dimentional array
  10868. point.x = options[0];
  10869. point.y = options[1];
  10870. } else if (optionsType === 'object' && typeof options.length !== 'number') { // object input
  10871. // copy options directly to point
  10872. extend(point, options);
  10873. point.options = options;
  10874.  
  10875. // This is the fastest way to detect if there are individual point dataLabels that need
  10876. // to be considered in drawDataLabels. These can only occur in object configs.
  10877. if (options.dataLabels) {
  10878. series._hasPointLabels = true;
  10879. }
  10880. } else if (typeof options[0] === 'string') { // categorized data with name in first position
  10881. point.name = options[0];
  10882. point.y = options[1];
  10883. }
  10884.  
  10885. /*
  10886. * If no x is set by now, get auto incremented value. All points must have an
  10887. * x value, however the y value can be null to create a gap in the series
  10888. */
  10889. // todo: skip this? It is only used in applyOptions, in translate it should not be used
  10890. if (point.x === UNDEFINED) {
  10891. point.x = x === UNDEFINED ? series.autoIncrement() : x;
  10892. }
  10893.  
  10894.  
  10895.  
  10896. },
  10897.  
  10898. /**
  10899. * Destroy a point to clear memory. Its reference still stays in series.data.
  10900. */
  10901. destroy: function () {
  10902. var point = this,
  10903. series = point.series,
  10904. chart = series.chart,
  10905. hoverPoints = chart.hoverPoints,
  10906. prop;
  10907.  
  10908. chart.pointCount--;
  10909.  
  10910. if (hoverPoints) {
  10911. point.setState();
  10912. erase(hoverPoints, point);
  10913. if (!hoverPoints.length) {
  10914. chart.hoverPoints = null;
  10915. }
  10916.  
  10917. }
  10918. if (point === chart.hoverPoint) {
  10919. point.onMouseOut();
  10920. }
  10921.  
  10922. // remove all events
  10923. if (point.graphic || point.dataLabel) { // removeEvent and destroyElements are performance expensive
  10924. removeEvent(point);
  10925. point.destroyElements();
  10926. }
  10927.  
  10928. if (point.legendItem) { // pies have legend items
  10929. chart.legend.destroyItem(point);
  10930. }
  10931.  
  10932. for (prop in point) {
  10933. point[prop] = null;
  10934. }
  10935.  
  10936.  
  10937. },
  10938.  
  10939. /**
  10940. * Destroy SVG elements associated with the point
  10941. */
  10942. destroyElements: function () {
  10943. var point = this,
  10944. props = ['graphic', 'tracker', 'dataLabel', 'group', 'connector', 'shadowGroup'],
  10945. prop,
  10946. i = 6;
  10947. while (i--) {
  10948. prop = props[i];
  10949. if (point[prop]) {
  10950. point[prop] = point[prop].destroy();
  10951. }
  10952. }
  10953. },
  10954.  
  10955. /**
  10956. * Return the configuration hash needed for the data label and tooltip formatters
  10957. */
  10958. getLabelConfig: function () {
  10959. var point = this;
  10960. return {
  10961. x: point.category,
  10962. y: point.y,
  10963. key: point.name || point.category,
  10964. series: point.series,
  10965. point: point,
  10966. percentage: point.percentage,
  10967. total: point.total || point.stackTotal
  10968. };
  10969. },
  10970.  
  10971. /**
  10972. * Toggle the selection status of a point
  10973. * @param {Boolean} selected Whether to select or unselect the point.
  10974. * @param {Boolean} accumulate Whether to add to the previous selection. By default,
  10975. * this happens if the control key (Cmd on Mac) was pressed during clicking.
  10976. */
  10977. select: function (selected, accumulate) {
  10978. var point = this,
  10979. series = point.series,
  10980. chart = series.chart;
  10981.  
  10982. selected = pick(selected, !point.selected);
  10983.  
  10984. // fire the event with the defalut handler
  10985. point.firePointEvent(selected ? 'select' : 'unselect', { accumulate: accumulate }, function () {
  10986. point.selected = selected;
  10987. point.setState(selected && SELECT_STATE);
  10988.  
  10989. // unselect all other points unless Ctrl or Cmd + click
  10990. if (!accumulate) {
  10991. each(chart.getSelectedPoints(), function (loopPoint) {
  10992. if (loopPoint.selected && loopPoint !== point) {
  10993. loopPoint.selected = false;
  10994. loopPoint.setState(NORMAL_STATE);
  10995. loopPoint.firePointEvent('unselect');
  10996. }
  10997. });
  10998. }
  10999. });
  11000. },
  11001.  
  11002. onMouseOver: function () {
  11003. var point = this,
  11004. series = point.series,
  11005. chart = series.chart,
  11006. tooltip = chart.tooltip,
  11007. hoverPoint = chart.hoverPoint;
  11008.  
  11009. // set normal state to previous series
  11010. if (hoverPoint && hoverPoint !== point) {
  11011. hoverPoint.onMouseOut();
  11012. }
  11013.  
  11014. // trigger the event
  11015. point.firePointEvent('mouseOver');
  11016.  
  11017. // update the tooltip
  11018. if (tooltip && (!tooltip.shared || series.noSharedTooltip)) {
  11019. tooltip.refresh(point);
  11020. }
  11021.  
  11022. // hover this
  11023. point.setState(HOVER_STATE);
  11024. chart.hoverPoint = point;
  11025. },
  11026.  
  11027. onMouseOut: function () {
  11028. var point = this;
  11029. point.firePointEvent('mouseOut');
  11030.  
  11031. point.setState();
  11032. point.series.chart.hoverPoint = null;
  11033. },
  11034.  
  11035. /**
  11036. * Extendable method for formatting each point's tooltip line
  11037. *
  11038. * @return {String} A string to be concatenated in to the common tooltip text
  11039. */
  11040. tooltipFormatter: function (pointFormat) {
  11041. var point = this,
  11042. series = point.series,
  11043. seriesTooltipOptions = series.tooltipOptions,
  11044. match = pointFormat.match(/\{(series|point)\.[a-zA-Z]+\}/g),
  11045. splitter = /[{\.}]/,
  11046. obj,
  11047. key,
  11048. replacement,
  11049. repOptionKey,
  11050. parts,
  11051. prop,
  11052. i,
  11053. cfg = { // docs: percentageDecimals, percentagePrefix, percentageSuffix, totalDecimals, totalPrefix, totalSuffix
  11054. y: 0, // 0: use 'value' for repOptionKey
  11055. open: 0,
  11056. high: 0,
  11057. low: 0,
  11058. close: 0,
  11059. percentage: 1, // 1: use the self name for repOptionKey
  11060. total: 1
  11061. };
  11062.  
  11063. // Backwards compatibility to y naming in early Highstock
  11064. seriesTooltipOptions.valuePrefix = seriesTooltipOptions.valuePrefix || seriesTooltipOptions.yPrefix;
  11065. seriesTooltipOptions.valueDecimals = seriesTooltipOptions.valueDecimals || seriesTooltipOptions.yDecimals;
  11066. seriesTooltipOptions.valueSuffix = seriesTooltipOptions.valueSuffix || seriesTooltipOptions.ySuffix;
  11067.  
  11068. // loop over the variables defined on the form {series.name}, {point.y} etc
  11069. for (i in match) {
  11070. key = match[i];
  11071. if (isString(key) && key !== pointFormat) { // IE matches more than just the variables
  11072.  
  11073. // Split it further into parts
  11074. parts = (' ' + key).split(splitter); // add empty string because IE and the rest handles it differently
  11075. obj = { 'point': point, 'series': series }[parts[1]];
  11076. prop = parts[2];
  11077.  
  11078. // Add some preformatting
  11079. if (obj === point && cfg.hasOwnProperty(prop)) {
  11080. repOptionKey = cfg[prop] ? prop : 'value';
  11081. replacement = (seriesTooltipOptions[repOptionKey + 'Prefix'] || '') +
  11082. numberFormat(point[prop], pick(seriesTooltipOptions[repOptionKey + 'Decimals'], -1)) +
  11083. (seriesTooltipOptions[repOptionKey + 'Suffix'] || '');
  11084.  
  11085. // Automatic replacement
  11086. } else {
  11087. replacement = obj[prop];
  11088. }
  11089.  
  11090. pointFormat = pointFormat.replace(key, replacement);
  11091. }
  11092. }
  11093.  
  11094. return pointFormat;
  11095. },
  11096.  
  11097. /**
  11098. * Update the point with new options (typically x/y data) and optionally redraw the series.
  11099. *
  11100. * @param {Object} options Point options as defined in the series.data array
  11101. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11102. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11103. * configuration
  11104. *
  11105. */
  11106. update: function (options, redraw, animation) {
  11107. var point = this,
  11108. series = point.series,
  11109. graphic = point.graphic,
  11110. i,
  11111. data = series.data,
  11112. dataLength = data.length,
  11113. chart = series.chart;
  11114.  
  11115. redraw = pick(redraw, true);
  11116.  
  11117. // fire the event with a default handler of doing the update
  11118. point.firePointEvent('update', { options: options }, function () {
  11119.  
  11120. point.applyOptions(options);
  11121.  
  11122. // update visuals
  11123. if (isObject(options)) {
  11124. series.getAttribs();
  11125. if (graphic) {
  11126. graphic.attr(point.pointAttr[series.state]);
  11127. }
  11128. }
  11129.  
  11130. // record changes in the parallel arrays
  11131. for (i = 0; i < dataLength; i++) {
  11132. if (data[i] === point) {
  11133. series.xData[i] = point.x;
  11134. series.yData[i] = point.y;
  11135. series.options.data[i] = options;
  11136. break;
  11137. }
  11138. }
  11139.  
  11140. // redraw
  11141. series.isDirty = true;
  11142. series.isDirtyData = true;
  11143. if (redraw) {
  11144. chart.redraw(animation);
  11145. }
  11146. });
  11147. },
  11148.  
  11149. /**
  11150. * Remove a point and optionally redraw the series and if necessary the axes
  11151. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11152. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11153. * configuration
  11154. */
  11155. remove: function (redraw, animation) {
  11156. var point = this,
  11157. series = point.series,
  11158. chart = series.chart,
  11159. i,
  11160. data = series.data,
  11161. dataLength = data.length;
  11162.  
  11163. setAnimation(animation, chart);
  11164. redraw = pick(redraw, true);
  11165.  
  11166. // fire the event with a default handler of removing the point
  11167. point.firePointEvent('remove', null, function () {
  11168.  
  11169. //erase(series.data, point);
  11170.  
  11171. for (i = 0; i < dataLength; i++) {
  11172. if (data[i] === point) {
  11173.  
  11174. // splice all the parallel arrays
  11175. data.splice(i, 1);
  11176. series.options.data.splice(i, 1);
  11177. series.xData.splice(i, 1);
  11178. series.yData.splice(i, 1);
  11179. break;
  11180. }
  11181. }
  11182.  
  11183. point.destroy();
  11184.  
  11185.  
  11186. // redraw
  11187. series.isDirty = true;
  11188. series.isDirtyData = true;
  11189. if (redraw) {
  11190. chart.redraw();
  11191. }
  11192. });
  11193.  
  11194.  
  11195. },
  11196.  
  11197. /**
  11198. * Fire an event on the Point object. Must not be renamed to fireEvent, as this
  11199. * causes a name clash in MooTools
  11200. * @param {String} eventType
  11201. * @param {Object} eventArgs Additional event arguments
  11202. * @param {Function} defaultFunction Default event handler
  11203. */
  11204. firePointEvent: function (eventType, eventArgs, defaultFunction) {
  11205. var point = this,
  11206. series = this.series,
  11207. seriesOptions = series.options;
  11208.  
  11209. // load event handlers on demand to save time on mouseover/out
  11210. if (seriesOptions.point.events[eventType] || (point.options && point.options.events && point.options.events[eventType])) {
  11211. this.importEvents();
  11212. }
  11213.  
  11214. // add default handler if in selection mode
  11215. if (eventType === 'click' && seriesOptions.allowPointSelect) {
  11216. defaultFunction = function (event) {
  11217. // Control key is for Windows, meta (= Cmd key) for Mac, Shift for Opera
  11218. point.select(null, event.ctrlKey || event.metaKey || event.shiftKey);
  11219. };
  11220. }
  11221.  
  11222. fireEvent(this, eventType, eventArgs, defaultFunction);
  11223. },
  11224. /**
  11225. * Import events from the series' and point's options. Only do it on
  11226. * demand, to save processing time on hovering.
  11227. */
  11228. importEvents: function () {
  11229. if (!this.hasImportedEvents) {
  11230. var point = this,
  11231. options = merge(point.series.options.point, point.options),
  11232. events = options.events,
  11233. eventType;
  11234.  
  11235. point.events = events;
  11236.  
  11237. for (eventType in events) {
  11238. addEvent(point, eventType, events[eventType]);
  11239. }
  11240. this.hasImportedEvents = true;
  11241.  
  11242. }
  11243. },
  11244.  
  11245. /**
  11246. * Set the point's state
  11247. * @param {String} state
  11248. */
  11249. setState: function (state) {
  11250. var point = this,
  11251. plotX = point.plotX,
  11252. plotY = point.plotY,
  11253. series = point.series,
  11254. stateOptions = series.options.states,
  11255. markerOptions = defaultPlotOptions[series.type].marker && series.options.marker,
  11256. normalDisabled = markerOptions && !markerOptions.enabled,
  11257. markerStateOptions = markerOptions && markerOptions.states[state],
  11258. stateDisabled = markerStateOptions && markerStateOptions.enabled === false,
  11259. stateMarkerGraphic = series.stateMarkerGraphic,
  11260. chart = series.chart,
  11261. radius,
  11262. pointAttr = point.pointAttr;
  11263.  
  11264. state = state || NORMAL_STATE; // empty string
  11265.  
  11266. if (
  11267. // already has this state
  11268. state === point.state ||
  11269. // selected points don't respond to hover
  11270. (point.selected && state !== SELECT_STATE) ||
  11271. // series' state options is disabled
  11272. (stateOptions[state] && stateOptions[state].enabled === false) ||
  11273. // point marker's state options is disabled
  11274. (state && (stateDisabled || (normalDisabled && !markerStateOptions.enabled)))
  11275.  
  11276. ) {
  11277. return;
  11278. }
  11279.  
  11280. // apply hover styles to the existing point
  11281. if (point.graphic) {
  11282. radius = markerOptions && point.graphic.symbolName && pointAttr[state].r;
  11283. point.graphic.attr(merge(
  11284. pointAttr[state],
  11285. radius ? { // new symbol attributes (#507, #612)
  11286. x: plotX - radius,
  11287. y: plotY - radius,
  11288. width: 2 * radius,
  11289. height: 2 * radius
  11290. } : {}
  11291. ));
  11292. } else {
  11293. // if a graphic is not applied to each point in the normal state, create a shared
  11294. // graphic for the hover state
  11295. if (state && markerStateOptions) {
  11296. if (!stateMarkerGraphic) {
  11297. radius = markerStateOptions.radius;
  11298. series.stateMarkerGraphic = stateMarkerGraphic = chart.renderer.symbol(
  11299. series.symbol,
  11300. -radius,
  11301. -radius,
  11302. 2 * radius,
  11303. 2 * radius
  11304. )
  11305. .attr(pointAttr[state])
  11306. .add(series.group);
  11307. }
  11308.  
  11309. stateMarkerGraphic.translate(
  11310. plotX,
  11311. plotY
  11312. );
  11313. }
  11314.  
  11315. if (stateMarkerGraphic) {
  11316. stateMarkerGraphic[state ? 'show' : 'hide']();
  11317. }
  11318. }
  11319.  
  11320. point.state = state;
  11321. }
  11322. };
  11323.  
  11324. /**
  11325. * @classDescription The base function which all other series types inherit from. The data in the series is stored
  11326. * in various arrays.
  11327. *
  11328. * - First, series.options.data contains all the original config options for
  11329. * each point whether added by options or methods like series.addPoint.
  11330. * - Next, series.data contains those values converted to points, but in case the series data length
  11331. * exceeds the cropThreshold, or if the data is grouped, series.data doesn't contain all the points. It
  11332. * only contains the points that have been created on demand.
  11333. * - Then there's series.points that contains all currently visible point objects. In case of cropping,
  11334. * the cropped-away points are not part of this array. The series.points array starts at series.cropStart
  11335. * compared to series.data and series.options.data. If however the series data is grouped, these can't
  11336. * be correlated one to one.
  11337. * - series.xData and series.processedXData contain clean x values, equivalent to series.data and series.points.
  11338. * - series.yData and series.processedYData contain clean x values, equivalent to series.data and series.points.
  11339. *
  11340. * @param {Object} chart
  11341. * @param {Object} options
  11342. */
  11343. var Series = function () {};
  11344.  
  11345. Series.prototype = {
  11346.  
  11347. isCartesian: true,
  11348. type: 'line',
  11349. pointClass: Point,
  11350. sorted: true, // requires the data to be sorted
  11351. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  11352. stroke: 'lineColor',
  11353. 'stroke-width': 'lineWidth',
  11354. fill: 'fillColor',
  11355. r: 'radius'
  11356. },
  11357. init: function (chart, options) {
  11358. var series = this,
  11359. eventType,
  11360. events,
  11361. //pointEvent,
  11362. index = chart.series.length;
  11363.  
  11364. series.chart = chart;
  11365. series.options = options = series.setOptions(options); // merge with plotOptions
  11366.  
  11367. // bind the axes
  11368. series.bindAxes();
  11369.  
  11370. // set some variables
  11371. extend(series, {
  11372. index: index,
  11373. name: options.name || 'Series ' + (index + 1),
  11374. state: NORMAL_STATE,
  11375. pointAttr: {},
  11376. visible: options.visible !== false, // true by default
  11377. selected: options.selected === true // false by default
  11378. });
  11379.  
  11380. // special
  11381. if (useCanVG) {
  11382. options.animation = false;
  11383. }
  11384.  
  11385. // register event listeners
  11386. events = options.events;
  11387. for (eventType in events) {
  11388. addEvent(series, eventType, events[eventType]);
  11389. }
  11390. if (
  11391. (events && events.click) ||
  11392. (options.point && options.point.events && options.point.events.click) ||
  11393. options.allowPointSelect
  11394. ) {
  11395. chart.runTrackerClick = false;
  11396. }
  11397.  
  11398. series.getColor();
  11399. series.getSymbol();
  11400.  
  11401. // set the data
  11402. series.setData(options.data, false);
  11403.  
  11404. // Mark cartesian
  11405. if (series.isCartesian) {
  11406. chart.hasCartesianSeries = true;
  11407. }
  11408.  
  11409. // Register it in the chart
  11410. chart.series.push(series);
  11411. },
  11412.  
  11413. /**
  11414. * Set the xAxis and yAxis properties of cartesian series, and register the series
  11415. * in the axis.series array
  11416. */
  11417. bindAxes: function () {
  11418. var series = this,
  11419. seriesOptions = series.options,
  11420. chart = series.chart,
  11421. axisOptions;
  11422.  
  11423. if (series.isCartesian) {
  11424.  
  11425. each(['xAxis', 'yAxis'], function (AXIS) { // repeat for xAxis and yAxis
  11426.  
  11427. each(chart[AXIS], function (axis) { // loop through the chart's axis objects
  11428.  
  11429. axisOptions = axis.options;
  11430.  
  11431. // apply if the series xAxis or yAxis option mathches the number of the
  11432. // axis, or if undefined, use the first axis
  11433. if ((seriesOptions[AXIS] === axisOptions.index) ||
  11434. (seriesOptions[AXIS] === UNDEFINED && axisOptions.index === 0)) {
  11435.  
  11436. // register this series in the axis.series lookup
  11437. axis.series.push(series);
  11438.  
  11439. // set this series.xAxis or series.yAxis reference
  11440. series[AXIS] = axis;
  11441.  
  11442. // mark dirty for redraw
  11443. axis.isDirty = true;
  11444. }
  11445. });
  11446.  
  11447. });
  11448. }
  11449. },
  11450.  
  11451.  
  11452. /**
  11453. * Return an auto incremented x value based on the pointStart and pointInterval options.
  11454. * This is only used if an x value is not given for the point that calls autoIncrement.
  11455. */
  11456. autoIncrement: function () {
  11457. var series = this,
  11458. options = series.options,
  11459. xIncrement = series.xIncrement;
  11460.  
  11461. xIncrement = pick(xIncrement, options.pointStart, 0);
  11462.  
  11463. series.pointInterval = pick(series.pointInterval, options.pointInterval, 1);
  11464.  
  11465. series.xIncrement = xIncrement + series.pointInterval;
  11466. return xIncrement;
  11467. },
  11468.  
  11469. /**
  11470. * Divide the series data into segments divided by null values.
  11471. */
  11472. getSegments: function () {
  11473. var series = this,
  11474. lastNull = -1,
  11475. segments = [],
  11476. i,
  11477. points = series.points,
  11478. pointsLength = points.length;
  11479.  
  11480. if (pointsLength) { // no action required for []
  11481.  
  11482. // if connect nulls, just remove null points
  11483. if (series.options.connectNulls) {
  11484. i = pointsLength;
  11485. while (i--) {
  11486. if (points[i].y === null) {
  11487. points.splice(i, 1);
  11488. }
  11489. }
  11490. if (points.length) {
  11491. segments = [points];
  11492. }
  11493.  
  11494. // else, split on null points
  11495. } else {
  11496. each(points, function (point, i) {
  11497. if (point.y === null) {
  11498. if (i > lastNull + 1) {
  11499. segments.push(points.slice(lastNull + 1, i));
  11500. }
  11501. lastNull = i;
  11502. } else if (i === pointsLength - 1) { // last value
  11503. segments.push(points.slice(lastNull + 1, i + 1));
  11504. }
  11505. });
  11506. }
  11507. }
  11508.  
  11509. // register it
  11510. series.segments = segments;
  11511. },
  11512. /**
  11513. * Set the series options by merging from the options tree
  11514. * @param {Object} itemOptions
  11515. */
  11516. setOptions: function (itemOptions) {
  11517. var series = this,
  11518. chart = series.chart,
  11519. chartOptions = chart.options,
  11520. plotOptions = chartOptions.plotOptions,
  11521. data = itemOptions.data,
  11522. options;
  11523.  
  11524. itemOptions.data = null; // remove from merge to prevent looping over the data set
  11525.  
  11526. options = merge(
  11527. plotOptions[this.type],
  11528. plotOptions.series,
  11529. itemOptions
  11530. );
  11531.  
  11532. // Re-insert the data array to the options and the original config (#717)
  11533. options.data = itemOptions.data = data;
  11534.  
  11535. // the tooltip options are merged between global and series specific options
  11536. series.tooltipOptions = merge(chartOptions.tooltip, options.tooltip);
  11537.  
  11538. return options;
  11539.  
  11540. },
  11541. /**
  11542. * Get the series' color
  11543. */
  11544. getColor: function () {
  11545. var options = this.options,
  11546. defaultColors = this.chart.options.colors,
  11547. counters = this.chart.counters;
  11548. this.color = options.color ||
  11549. (!options.colorByPoint && defaultColors[counters.color++]) || 'gray';
  11550. counters.wrapColor(defaultColors.length);
  11551. },
  11552. /**
  11553. * Get the series' symbol
  11554. */
  11555. getSymbol: function () {
  11556. var series = this,
  11557. seriesMarkerOption = series.options.marker,
  11558. chart = series.chart,
  11559. defaultSymbols = chart.options.symbols,
  11560. counters = chart.counters;
  11561. series.symbol = seriesMarkerOption.symbol || defaultSymbols[counters.symbol++];
  11562.  
  11563. // don't substract radius in image symbols (#604)
  11564. if (/^url/.test(series.symbol)) {
  11565. seriesMarkerOption.radius = 0;
  11566. }
  11567. counters.wrapSymbol(defaultSymbols.length);
  11568. },
  11569.  
  11570. /**
  11571. * Get the series' symbol in the legend. This method should be overridable to create custom
  11572. * symbols through Highcharts.seriesTypes[type].prototype.drawLegendSymbols.
  11573. *
  11574. * @param {Object} legend The legend object
  11575. */
  11576. drawLegendSymbol: function (legend) {
  11577.  
  11578. var options = this.options,
  11579. markerOptions = options.marker,
  11580. radius,
  11581. legendOptions = legend.options,
  11582. legendSymbol,
  11583. symbolWidth = legendOptions.symbolWidth,
  11584. renderer = this.chart.renderer,
  11585. legendItemGroup = this.legendGroup,
  11586. baseline = legend.baseline,
  11587. attr;
  11588.  
  11589. // Draw the line
  11590. if (options.lineWidth) {
  11591. attr = {
  11592. 'stroke-width': options.lineWidth
  11593. };
  11594. if (options.dashStyle) {
  11595. attr.dashstyle = options.dashStyle;
  11596. }
  11597. this.legendLine = renderer.path([
  11598. M,
  11599. 0,
  11600. baseline - 4,
  11601. L,
  11602. symbolWidth,
  11603. baseline - 4
  11604. ])
  11605. .attr(attr)
  11606. .add(legendItemGroup);
  11607. }
  11608.  
  11609. // Draw the marker
  11610. if (markerOptions && markerOptions.enabled) {
  11611. radius = markerOptions.radius;
  11612. this.legendSymbol = legendSymbol = renderer.symbol(
  11613. this.symbol,
  11614. (symbolWidth / 2) - radius,
  11615. baseline - 4 - radius,
  11616. 2 * radius,
  11617. 2 * radius
  11618. )
  11619. .attr(this.pointAttr[NORMAL_STATE])
  11620. .add(legendItemGroup);
  11621. }
  11622. },
  11623.  
  11624. /**
  11625. * Add a point dynamically after chart load time
  11626. * @param {Object} options Point options as given in series.data
  11627. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11628. * @param {Boolean} shift If shift is true, a point is shifted off the start
  11629. * of the series as one is appended to the end.
  11630. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11631. * configuration
  11632. */
  11633. addPoint: function (options, redraw, shift, animation) {
  11634. var series = this,
  11635. data = series.data,
  11636. graph = series.graph,
  11637. area = series.area,
  11638. chart = series.chart,
  11639. xData = series.xData,
  11640. yData = series.yData,
  11641. currentShift = (graph && graph.shift) || 0,
  11642. dataOptions = series.options.data,
  11643. point;
  11644. //point = (new series.pointClass()).init(series, options);
  11645.  
  11646. setAnimation(animation, chart);
  11647.  
  11648. // Make graph animate sideways
  11649. if (graph && shift) {
  11650. graph.shift = currentShift + 1;
  11651. }
  11652. if (area) {
  11653. if (shift) { // #780
  11654. area.shift = currentShift + 1;
  11655. }
  11656. area.isArea = true; // needed in animation, both with and without shift
  11657. }
  11658.  
  11659. // Optional redraw, defaults to true
  11660. redraw = pick(redraw, true);
  11661.  
  11662. // Get options and push the point to xData, yData and series.options. In series.generatePoints
  11663. // the Point instance will be created on demand and pushed to the series.data array.
  11664. point = { series: series };
  11665. series.pointClass.prototype.applyOptions.apply(point, [options]);
  11666. xData.push(point.x);
  11667. yData.push(series.valueCount === 4 ? [point.open, point.high, point.low, point.close] : point.y);
  11668. dataOptions.push(options);
  11669.  
  11670.  
  11671. // Shift the first point off the parallel arrays
  11672. // todo: consider series.removePoint(i) method
  11673. if (shift) {
  11674. if (data[0] && data[0].remove) {
  11675. data[0].remove(false);
  11676. } else {
  11677. data.shift();
  11678. xData.shift();
  11679. yData.shift();
  11680. dataOptions.shift();
  11681. }
  11682. }
  11683. series.getAttribs();
  11684.  
  11685. // redraw
  11686. series.isDirty = true;
  11687. series.isDirtyData = true;
  11688. if (redraw) {
  11689. chart.redraw();
  11690. }
  11691. },
  11692.  
  11693. /**
  11694. * Replace the series data with a new set of data
  11695. * @param {Object} data
  11696. * @param {Object} redraw
  11697. */
  11698. setData: function (data, redraw) {
  11699. var series = this,
  11700. oldData = series.points,
  11701. options = series.options,
  11702. initialColor = series.initialColor,
  11703. chart = series.chart,
  11704. firstPoint = null,
  11705. xAxis = series.xAxis,
  11706. i,
  11707. pointProto = series.pointClass.prototype;
  11708.  
  11709. // reset properties
  11710. series.xIncrement = null;
  11711. series.pointRange = (xAxis && xAxis.categories && 1) || options.pointRange;
  11712.  
  11713. if (defined(initialColor)) { // reset colors for pie
  11714. chart.counters.color = initialColor;
  11715. }
  11716.  
  11717. // parallel arrays
  11718. var xData = [],
  11719. yData = [],
  11720. dataLength = data ? data.length : [],
  11721. turboThreshold = options.turboThreshold || 1000,
  11722. pt,
  11723. valueCount = series.valueCount;
  11724.  
  11725. // In turbo mode, only one- or twodimensional arrays of numbers are allowed. The
  11726. // first value is tested, and we assume that all the rest are defined the same
  11727. // way. Although the 'for' loops are similar, they are repeated inside each
  11728. // if-else conditional for max performance.
  11729. if (dataLength > turboThreshold) {
  11730.  
  11731. // find the first non-null point
  11732. i = 0;
  11733. while (firstPoint === null && i < dataLength) {
  11734. firstPoint = data[i];
  11735. i++;
  11736. }
  11737.  
  11738.  
  11739. if (isNumber(firstPoint)) { // assume all points are numbers
  11740. var x = pick(options.pointStart, 0),
  11741. pointInterval = pick(options.pointInterval, 1);
  11742.  
  11743. for (i = 0; i < dataLength; i++) {
  11744. xData[i] = x;
  11745. yData[i] = data[i];
  11746. x += pointInterval;
  11747. }
  11748. series.xIncrement = x;
  11749. } else if (isArray(firstPoint)) { // assume all points are arrays
  11750. if (valueCount) { // [x, low, high] or [x, o, h, l, c]
  11751. for (i = 0; i < dataLength; i++) {
  11752. pt = data[i];
  11753. xData[i] = pt[0];
  11754. yData[i] = pt.slice(1, valueCount + 1);
  11755. }
  11756. } else { // [x, y]
  11757. for (i = 0; i < dataLength; i++) {
  11758. pt = data[i];
  11759. xData[i] = pt[0];
  11760. yData[i] = pt[1];
  11761. }
  11762. }
  11763. } /* else {
  11764. error(12); // Highcharts expects configs to be numbers or arrays in turbo mode
  11765. }*/
  11766. } else {
  11767. for (i = 0; i < dataLength; i++) {
  11768. pt = { series: series };
  11769. pointProto.applyOptions.apply(pt, [data[i]]);
  11770. xData[i] = pt.x;
  11771. yData[i] = pointProto.toYData ? pointProto.toYData.apply(pt) : pt.y;
  11772. }
  11773. }
  11774.  
  11775. series.data = [];
  11776. series.options.data = data;
  11777. series.xData = xData;
  11778. series.yData = yData;
  11779.  
  11780. // destroy old points
  11781. i = (oldData && oldData.length) || 0;
  11782. while (i--) {
  11783. if (oldData[i] && oldData[i].destroy) {
  11784. oldData[i].destroy();
  11785. }
  11786. }
  11787.  
  11788. // reset minRange (#878)
  11789. if (xAxis) {
  11790. xAxis.minRange = xAxis.userMinRange;
  11791. }
  11792.  
  11793. // redraw
  11794. series.isDirty = series.isDirtyData = chart.isDirtyBox = true;
  11795. if (pick(redraw, true)) {
  11796. chart.redraw(false);
  11797. }
  11798. },
  11799.  
  11800. /**
  11801. * Remove a series and optionally redraw the chart
  11802. *
  11803. * @param {Boolean} redraw Whether to redraw the chart or wait for an explicit call
  11804. * @param {Boolean|Object} animation Whether to apply animation, and optionally animation
  11805. * configuration
  11806. */
  11807.  
  11808. remove: function (redraw, animation) {
  11809. var series = this,
  11810. chart = series.chart;
  11811. redraw = pick(redraw, true);
  11812.  
  11813. if (!series.isRemoving) { /* prevent triggering native event in jQuery
  11814. (calling the remove function from the remove event) */
  11815. series.isRemoving = true;
  11816.  
  11817. // fire the event with a default handler of removing the point
  11818. fireEvent(series, 'remove', null, function () {
  11819.  
  11820.  
  11821. // destroy elements
  11822. series.destroy();
  11823.  
  11824.  
  11825. // redraw
  11826. chart.isDirtyLegend = chart.isDirtyBox = true;
  11827. if (redraw) {
  11828. chart.redraw(animation);
  11829. }
  11830. });
  11831.  
  11832. }
  11833. series.isRemoving = false;
  11834. },
  11835.  
  11836. /**
  11837. * Process the data by cropping away unused data points if the series is longer
  11838. * than the crop threshold. This saves computing time for lage series.
  11839. */
  11840. processData: function (force) {
  11841. var series = this,
  11842. processedXData = series.xData, // copied during slice operation below
  11843. processedYData = series.yData,
  11844. dataLength = processedXData.length,
  11845. cropStart = 0,
  11846. cropEnd = dataLength,
  11847. cropped,
  11848. distance,
  11849. closestPointRange,
  11850. xAxis = series.xAxis,
  11851. i, // loop variable
  11852. options = series.options,
  11853. cropThreshold = options.cropThreshold,
  11854. isCartesian = series.isCartesian;
  11855.  
  11856. // If the series data or axes haven't changed, don't go through this. Return false to pass
  11857. // the message on to override methods like in data grouping.
  11858. if (isCartesian && !series.isDirty && !xAxis.isDirty && !series.yAxis.isDirty && !force) {
  11859. return false;
  11860. }
  11861.  
  11862. // optionally filter out points outside the plot area
  11863. if (isCartesian && series.sorted && (!cropThreshold || dataLength > cropThreshold || series.forceCrop)) {
  11864. var extremes = xAxis.getExtremes(),
  11865. min = extremes.min,
  11866. max = extremes.max;
  11867.  
  11868. // it's outside current extremes
  11869. if (processedXData[dataLength - 1] < min || processedXData[0] > max) {
  11870. processedXData = [];
  11871. processedYData = [];
  11872.  
  11873. // only crop if it's actually spilling out
  11874. } else if (processedXData[0] < min || processedXData[dataLength - 1] > max) {
  11875.  
  11876. // iterate up to find slice start
  11877. for (i = 0; i < dataLength; i++) {
  11878. if (processedXData[i] >= min) {
  11879. cropStart = mathMax(0, i - 1);
  11880. break;
  11881. }
  11882. }
  11883. // proceed to find slice end
  11884. for (; i < dataLength; i++) {
  11885. if (processedXData[i] > max) {
  11886. cropEnd = i + 1;
  11887. break;
  11888. }
  11889.  
  11890. }
  11891. processedXData = processedXData.slice(cropStart, cropEnd);
  11892. processedYData = processedYData.slice(cropStart, cropEnd);
  11893. cropped = true;
  11894. }
  11895. }
  11896.  
  11897.  
  11898. // Find the closest distance between processed points
  11899. for (i = processedXData.length - 1; i > 0; i--) {
  11900. distance = processedXData[i] - processedXData[i - 1];
  11901. if (distance > 0 && (closestPointRange === UNDEFINED || distance < closestPointRange)) {
  11902. closestPointRange = distance;
  11903. }
  11904. }
  11905.  
  11906. // Record the properties
  11907. series.cropped = cropped; // undefined or true
  11908. series.cropStart = cropStart;
  11909. series.processedXData = processedXData;
  11910. series.processedYData = processedYData;
  11911.  
  11912. if (options.pointRange === null) { // null means auto, as for columns, candlesticks and OHLC
  11913. series.pointRange = closestPointRange || 1;
  11914. }
  11915. series.closestPointRange = closestPointRange;
  11916.  
  11917. },
  11918.  
  11919. /**
  11920. * Generate the data point after the data has been processed by cropping away
  11921. * unused points and optionally grouped in Highcharts Stock.
  11922. */
  11923. generatePoints: function () {
  11924. var series = this,
  11925. options = series.options,
  11926. dataOptions = options.data,
  11927. data = series.data,
  11928. dataLength,
  11929. processedXData = series.processedXData,
  11930. processedYData = series.processedYData,
  11931. pointClass = series.pointClass,
  11932. processedDataLength = processedXData.length,
  11933. cropStart = series.cropStart || 0,
  11934. cursor,
  11935. hasGroupedData = series.hasGroupedData,
  11936. point,
  11937. points = [],
  11938. i;
  11939.  
  11940. if (!data && !hasGroupedData) {
  11941. var arr = [];
  11942. arr.length = dataOptions.length;
  11943. data = series.data = arr;
  11944. }
  11945.  
  11946. for (i = 0; i < processedDataLength; i++) {
  11947. cursor = cropStart + i;
  11948. if (!hasGroupedData) {
  11949. if (data[cursor]) {
  11950. point = data[cursor];
  11951. } else if (dataOptions[cursor] !== UNDEFINED) { // #970
  11952. data[cursor] = point = (new pointClass()).init(series, dataOptions[cursor], processedXData[i]);
  11953. }
  11954. points[i] = point;
  11955. } else {
  11956. // splat the y data in case of ohlc data array
  11957. points[i] = (new pointClass()).init(series, [processedXData[i]].concat(splat(processedYData[i])));
  11958. }
  11959. }
  11960.  
  11961. // Hide cropped-away points - this only runs when the number of points is above cropThreshold, or when
  11962. // swithching view from non-grouped data to grouped data (#637)
  11963. if (data && (processedDataLength !== (dataLength = data.length) || hasGroupedData)) {
  11964. for (i = 0; i < dataLength; i++) {
  11965. if (i === cropStart && !hasGroupedData) { // when has grouped data, clear all points
  11966. i += processedDataLength;
  11967. }
  11968. if (data[i]) {
  11969. data[i].destroyElements();
  11970. data[i].plotX = UNDEFINED; // #1003
  11971. }
  11972. }
  11973. }
  11974.  
  11975. series.data = data;
  11976. series.points = points;
  11977. },
  11978.  
  11979. /**
  11980. * Translate data points from raw data values to chart specific positioning data
  11981. * needed later in drawPoints, drawGraph and drawTracker.
  11982. */
  11983. translate: function () {
  11984. if (!this.processedXData) { // hidden series
  11985. this.processData();
  11986. }
  11987. this.generatePoints();
  11988. var series = this,
  11989. chart = series.chart,
  11990. options = series.options,
  11991. stacking = options.stacking,
  11992. xAxis = series.xAxis,
  11993. categories = xAxis.categories,
  11994. yAxis = series.yAxis,
  11995. points = series.points,
  11996. dataLength = points.length,
  11997. hasModifyValue = !!series.modifyValue,
  11998. isLastSeries,
  11999. allStackSeries = yAxis.series,
  12000. i = allStackSeries.length;
  12001.  
  12002. // Is it the last visible series?
  12003. while (i--) {
  12004. if (allStackSeries[i].visible) {
  12005. if (i === series.index) {
  12006. isLastSeries = true;
  12007. }
  12008. break;
  12009. }
  12010. }
  12011.  
  12012. // Translate each point
  12013. for (i = 0; i < dataLength; i++) {
  12014. var point = points[i],
  12015. xValue = point.x,
  12016. yValue = point.y,
  12017. yBottom = point.low,
  12018. stack = yAxis.stacks[(yValue < options.threshold ? '-' : '') + series.stackKey],
  12019. pointStack,
  12020. pointStackTotal;
  12021.  
  12022. // get the plotX translation
  12023. //point.plotX = mathRound(xAxis.translate(xValue, 0, 0, 0, 1) * 10) / 10; // Math.round fixes #591
  12024. point.plotX = xAxis.translate(xValue, 0, 0, 0, 1); // Math.round fixes #591
  12025.  
  12026. // calculate the bottom y value for stacked series
  12027. if (stacking && series.visible && stack && stack[xValue]) {
  12028. pointStack = stack[xValue];
  12029. pointStackTotal = pointStack.total;
  12030. pointStack.cum = yBottom = pointStack.cum - yValue; // start from top
  12031. yValue = yBottom + yValue;
  12032.  
  12033. if (isLastSeries) {
  12034. yBottom = options.threshold;
  12035. }
  12036.  
  12037. if (stacking === 'percent') {
  12038. yBottom = pointStackTotal ? yBottom * 100 / pointStackTotal : 0;
  12039. yValue = pointStackTotal ? yValue * 100 / pointStackTotal : 0;
  12040. }
  12041.  
  12042. point.percentage = pointStackTotal ? point.y * 100 / pointStackTotal : 0;
  12043. point.stackTotal = pointStackTotal;
  12044. point.stackY = yValue;
  12045. }
  12046.  
  12047. // Set translated yBottom or remove it
  12048. point.yBottom = defined(yBottom) ?
  12049. yAxis.translate(yBottom, 0, 1, 0, 1) :
  12050. null;
  12051.  
  12052. // general hook, used for Highstock compare mode
  12053. if (hasModifyValue) {
  12054. yValue = series.modifyValue(yValue, point);
  12055. }
  12056.  
  12057. // Set the the plotY value, reset it for redraws
  12058. point.plotY = (typeof yValue === 'number') ?
  12059. mathRound(yAxis.translate(yValue, 0, 1, 0, 1) * 10) / 10 : // Math.round fixes #591
  12060. UNDEFINED;
  12061.  
  12062. // set client related positions for mouse tracking
  12063. point.clientX = chart.inverted ?
  12064. chart.plotHeight - point.plotX :
  12065. point.plotX; // for mouse tracking
  12066.  
  12067. // some API data
  12068. point.category = categories && categories[point.x] !== UNDEFINED ?
  12069. categories[point.x] : point.x;
  12070.  
  12071.  
  12072. }
  12073.  
  12074. // now that we have the cropped data, build the segments
  12075. series.getSegments();
  12076. },
  12077. /**
  12078. * Memoize tooltip texts and positions
  12079. */
  12080. setTooltipPoints: function (renew) {
  12081. var series = this,
  12082. chart = series.chart,
  12083. points = [],
  12084. pointsLength,
  12085. plotSize = chart.plotSizeX,
  12086. low,
  12087. high,
  12088. xAxis = series.xAxis,
  12089. point,
  12090. i,
  12091. tooltipPoints = []; // a lookup array for each pixel in the x dimension
  12092.  
  12093. // don't waste resources if tracker is disabled
  12094. if (series.options.enableMouseTracking === false) {
  12095. return;
  12096. }
  12097.  
  12098. // renew
  12099. if (renew) {
  12100. series.tooltipPoints = null;
  12101. }
  12102.  
  12103. // concat segments to overcome null values
  12104. each(series.segments || series.points, function (segment) {
  12105. points = points.concat(segment);
  12106. });
  12107.  
  12108. // loop the concatenated points and apply each point to all the closest
  12109. // pixel positions
  12110. if (xAxis && xAxis.reversed) {
  12111. points = points.reverse();
  12112. }
  12113.  
  12114. // Assign each pixel position to the nearest point
  12115. pointsLength = points.length;
  12116. for (i = 0; i < pointsLength; i++) {
  12117. point = points[i];
  12118. low = points[i - 1] ? points[i - 1]._high + 1 : 0;
  12119. point._high = high = points[i + 1] ?
  12120. mathMax(0, mathFloor((point.plotX + (points[i + 1] ? points[i + 1].plotX : plotSize)) / 2)) :
  12121. plotSize;
  12122.  
  12123. while (low >= 0 && low <= high) {
  12124. tooltipPoints[low++] = point;
  12125. }
  12126. }
  12127. series.tooltipPoints = tooltipPoints;
  12128. },
  12129.  
  12130. /**
  12131. * Format the header of the tooltip
  12132. */
  12133. tooltipHeaderFormatter: function (key) {
  12134. var series = this,
  12135. tooltipOptions = series.tooltipOptions,
  12136. xDateFormat = tooltipOptions.xDateFormat,
  12137. xAxis = series.xAxis,
  12138. isDateTime = xAxis && xAxis.options.type === 'datetime',
  12139. n;
  12140.  
  12141. // Guess the best date format based on the closest point distance (#568)
  12142. if (isDateTime && !xDateFormat) {
  12143. for (n in timeUnits) {
  12144. if (timeUnits[n] >= xAxis.closestPointRange) {
  12145. xDateFormat = tooltipOptions.dateTimeLabelFormats[n];
  12146. break;
  12147. }
  12148. }
  12149. }
  12150.  
  12151. return tooltipOptions.headerFormat
  12152. .replace('{point.key}', isDateTime ? dateFormat(xDateFormat, key) : key)
  12153. .replace('{series.name}', series.name)
  12154. .replace('{series.color}', series.color);
  12155. },
  12156.  
  12157. /**
  12158. * Series mouse over handler
  12159. */
  12160. onMouseOver: function () {
  12161. var series = this,
  12162. chart = series.chart,
  12163. hoverSeries = chart.hoverSeries;
  12164.  
  12165. if (!hasTouch && chart.mouseIsDown) {
  12166. return;
  12167. }
  12168.  
  12169. // set normal state to previous series
  12170. if (hoverSeries && hoverSeries !== series) {
  12171. hoverSeries.onMouseOut();
  12172. }
  12173.  
  12174. // trigger the event, but to save processing time,
  12175. // only if defined
  12176. if (series.options.events.mouseOver) {
  12177. fireEvent(series, 'mouseOver');
  12178. }
  12179.  
  12180. // hover this
  12181. series.setState(HOVER_STATE);
  12182. chart.hoverSeries = series;
  12183. },
  12184.  
  12185. /**
  12186. * Series mouse out handler
  12187. */
  12188. onMouseOut: function () {
  12189. // trigger the event only if listeners exist
  12190. var series = this,
  12191. options = series.options,
  12192. chart = series.chart,
  12193. tooltip = chart.tooltip,
  12194. hoverPoint = chart.hoverPoint;
  12195.  
  12196. // trigger mouse out on the point, which must be in this series
  12197. if (hoverPoint) {
  12198. hoverPoint.onMouseOut();
  12199. }
  12200.  
  12201. // fire the mouse out event
  12202. if (series && options.events.mouseOut) {
  12203. fireEvent(series, 'mouseOut');
  12204. }
  12205.  
  12206.  
  12207. // hide the tooltip
  12208. if (tooltip && !options.stickyTracking && !tooltip.shared) {
  12209. tooltip.hide();
  12210. }
  12211.  
  12212. // set normal state
  12213. series.setState();
  12214. chart.hoverSeries = null;
  12215. },
  12216.  
  12217. /**
  12218. * Animate in the series
  12219. */
  12220. animate: function (init) {
  12221. var series = this,
  12222. chart = series.chart,
  12223. clipRect = series.clipRect,
  12224. animation = series.options.animation;
  12225.  
  12226. if (animation && !isObject(animation)) {
  12227. animation = {};
  12228. }
  12229.  
  12230. if (init) { // initialize the animation
  12231. if (!clipRect.isAnimating) { // apply it only for one of the series
  12232. clipRect.attr('width', 0);
  12233. clipRect.isAnimating = true;
  12234. }
  12235.  
  12236. } else { // run the animation
  12237. clipRect.animate({
  12238. width: chart.plotSizeX
  12239. }, animation);
  12240.  
  12241. // delete this function to allow it only once
  12242. this.animate = null;
  12243. }
  12244. },
  12245.  
  12246.  
  12247. /**
  12248. * Draw the markers
  12249. */
  12250. drawPoints: function () {
  12251. var series = this,
  12252. pointAttr,
  12253. points = series.points,
  12254. chart = series.chart,
  12255. plotX,
  12256. plotY,
  12257. i,
  12258. point,
  12259. radius,
  12260. symbol,
  12261. isImage,
  12262. graphic;
  12263.  
  12264. if (series.options.marker.enabled) {
  12265. i = points.length;
  12266. while (i--) {
  12267. point = points[i];
  12268. plotX = point.plotX;
  12269. plotY = point.plotY;
  12270. graphic = point.graphic;
  12271.  
  12272. // only draw the point if y is defined
  12273. if (plotY !== UNDEFINED && !isNaN(plotY)) {
  12274.  
  12275. // shortcuts
  12276. pointAttr = point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE];
  12277. radius = pointAttr.r;
  12278. symbol = pick(point.marker && point.marker.symbol, series.symbol);
  12279. isImage = symbol.indexOf('url') === 0;
  12280.  
  12281. if (graphic) { // update
  12282. graphic.animate(extend({
  12283. x: plotX - radius,
  12284. y: plotY - radius
  12285. }, graphic.symbolName ? { // don't apply to image symbols #507
  12286. width: 2 * radius,
  12287. height: 2 * radius
  12288. } : {}));
  12289. } else if (radius > 0 || isImage) {
  12290. point.graphic = chart.renderer.symbol(
  12291. symbol,
  12292. plotX - radius,
  12293. plotY - radius,
  12294. 2 * radius,
  12295. 2 * radius
  12296. )
  12297. .attr(pointAttr)
  12298. .add(series.group);
  12299. }
  12300. }
  12301. }
  12302. }
  12303.  
  12304. },
  12305.  
  12306. /**
  12307. * Convert state properties from API naming conventions to SVG attributes
  12308. *
  12309. * @param {Object} options API options object
  12310. * @param {Object} base1 SVG attribute object to inherit from
  12311. * @param {Object} base2 Second level SVG attribute object to inherit from
  12312. */
  12313. convertAttribs: function (options, base1, base2, base3) {
  12314. var conversion = this.pointAttrToOptions,
  12315. attr,
  12316. option,
  12317. obj = {};
  12318.  
  12319. options = options || {};
  12320. base1 = base1 || {};
  12321. base2 = base2 || {};
  12322. base3 = base3 || {};
  12323.  
  12324. for (attr in conversion) {
  12325. option = conversion[attr];
  12326. obj[attr] = pick(options[option], base1[attr], base2[attr], base3[attr]);
  12327. }
  12328. return obj;
  12329. },
  12330.  
  12331. /**
  12332. * Get the state attributes. Each series type has its own set of attributes
  12333. * that are allowed to change on a point's state change. Series wide attributes are stored for
  12334. * all series, and additionally point specific attributes are stored for all
  12335. * points with individual marker options. If such options are not defined for the point,
  12336. * a reference to the series wide attributes is stored in point.pointAttr.
  12337. */
  12338. getAttribs: function () {
  12339. var series = this,
  12340. normalOptions = defaultPlotOptions[series.type].marker ? series.options.marker : series.options,
  12341. stateOptions = normalOptions.states,
  12342. stateOptionsHover = stateOptions[HOVER_STATE],
  12343. pointStateOptionsHover,
  12344. seriesColor = series.color,
  12345. normalDefaults = {
  12346. stroke: seriesColor,
  12347. fill: seriesColor
  12348. },
  12349. points = series.points || [], // #927
  12350. i,
  12351. point,
  12352. seriesPointAttr = [],
  12353. pointAttr,
  12354. pointAttrToOptions = series.pointAttrToOptions,
  12355. hasPointSpecificOptions,
  12356. key;
  12357.  
  12358. // series type specific modifications
  12359. if (series.options.marker) { // line, spline, area, areaspline, scatter
  12360.  
  12361. // if no hover radius is given, default to normal radius + 2
  12362. stateOptionsHover.radius = stateOptionsHover.radius || normalOptions.radius + 2;
  12363. stateOptionsHover.lineWidth = stateOptionsHover.lineWidth || normalOptions.lineWidth + 1;
  12364.  
  12365. } else { // column, bar, pie
  12366.  
  12367. // if no hover color is given, brighten the normal color
  12368. stateOptionsHover.color = stateOptionsHover.color ||
  12369. Color(stateOptionsHover.color || seriesColor)
  12370. .brighten(stateOptionsHover.brightness).get();
  12371. }
  12372.  
  12373. // general point attributes for the series normal state
  12374. seriesPointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, normalDefaults);
  12375.  
  12376. // HOVER_STATE and SELECT_STATE states inherit from normal state except the default radius
  12377. each([HOVER_STATE, SELECT_STATE], function (state) {
  12378. seriesPointAttr[state] =
  12379. series.convertAttribs(stateOptions[state], seriesPointAttr[NORMAL_STATE]);
  12380. });
  12381.  
  12382. // set it
  12383. series.pointAttr = seriesPointAttr;
  12384.  
  12385.  
  12386. // Generate the point-specific attribute collections if specific point
  12387. // options are given. If not, create a referance to the series wide point
  12388. // attributes
  12389. i = points.length;
  12390. while (i--) {
  12391. point = points[i];
  12392. normalOptions = (point.options && point.options.marker) || point.options;
  12393. if (normalOptions && normalOptions.enabled === false) {
  12394. normalOptions.radius = 0;
  12395. }
  12396. hasPointSpecificOptions = false;
  12397.  
  12398. // check if the point has specific visual options
  12399. if (point.options) {
  12400. for (key in pointAttrToOptions) {
  12401. if (defined(normalOptions[pointAttrToOptions[key]])) {
  12402. hasPointSpecificOptions = true;
  12403. }
  12404. }
  12405. }
  12406.  
  12407.  
  12408.  
  12409. // a specific marker config object is defined for the individual point:
  12410. // create it's own attribute collection
  12411. if (hasPointSpecificOptions) {
  12412.  
  12413. pointAttr = [];
  12414. stateOptions = normalOptions.states || {}; // reassign for individual point
  12415. pointStateOptionsHover = stateOptions[HOVER_STATE] = stateOptions[HOVER_STATE] || {};
  12416.  
  12417. // if no hover color is given, brighten the normal color
  12418. if (!series.options.marker) { // column, bar, point
  12419. pointStateOptionsHover.color =
  12420. Color(pointStateOptionsHover.color || point.options.color)
  12421. .brighten(pointStateOptionsHover.brightness ||
  12422. stateOptionsHover.brightness).get();
  12423.  
  12424. }
  12425.  
  12426. // normal point state inherits series wide normal state
  12427. pointAttr[NORMAL_STATE] = series.convertAttribs(normalOptions, seriesPointAttr[NORMAL_STATE]);
  12428.  
  12429. // inherit from point normal and series hover
  12430. pointAttr[HOVER_STATE] = series.convertAttribs(
  12431. stateOptions[HOVER_STATE],
  12432. seriesPointAttr[HOVER_STATE],
  12433. pointAttr[NORMAL_STATE]
  12434. );
  12435. // inherit from point normal and series hover
  12436. pointAttr[SELECT_STATE] = series.convertAttribs(
  12437. stateOptions[SELECT_STATE],
  12438. seriesPointAttr[SELECT_STATE],
  12439. pointAttr[NORMAL_STATE]
  12440. );
  12441.  
  12442.  
  12443.  
  12444. // no marker config object is created: copy a reference to the series-wide
  12445. // attribute collection
  12446. } else {
  12447. pointAttr = seriesPointAttr;
  12448. }
  12449.  
  12450. point.pointAttr = pointAttr;
  12451.  
  12452. }
  12453.  
  12454. },
  12455.  
  12456.  
  12457. /**
  12458. * Clear DOM objects and free up memory
  12459. */
  12460. destroy: function () {
  12461. var series = this,
  12462. chart = series.chart,
  12463. seriesClipRect = series.clipRect,
  12464. issue134 = /AppleWebKit\/533/.test(userAgent),
  12465. destroy,
  12466. i,
  12467. data = series.data || [],
  12468. point,
  12469. prop,
  12470. axis;
  12471.  
  12472. // add event hook
  12473. fireEvent(series, 'destroy');
  12474.  
  12475. // remove all events
  12476. removeEvent(series);
  12477.  
  12478. // erase from axes
  12479. each(['xAxis', 'yAxis'], function (AXIS) {
  12480. axis = series[AXIS];
  12481. if (axis) {
  12482. erase(axis.series, series);
  12483. axis.isDirty = true;
  12484. }
  12485. });
  12486.  
  12487. // remove legend items
  12488. if (series.legendItem) {
  12489. series.chart.legend.destroyItem(series);
  12490. }
  12491.  
  12492. // destroy all points with their elements
  12493. i = data.length;
  12494. while (i--) {
  12495. point = data[i];
  12496. if (point && point.destroy) {
  12497. point.destroy();
  12498. }
  12499. }
  12500. series.points = null;
  12501.  
  12502. // If this series clipRect is not the global one (which is removed on chart.destroy) we
  12503. // destroy it here.
  12504. if (seriesClipRect && seriesClipRect !== chart.clipRect) {
  12505. series.clipRect = seriesClipRect.destroy();
  12506. }
  12507.  
  12508. // destroy all SVGElements associated to the series
  12509. each(['area', 'graph', 'dataLabelsGroup', 'group', 'tracker', 'trackerGroup'], function (prop) {
  12510. if (series[prop]) {
  12511.  
  12512. // issue 134 workaround
  12513. destroy = issue134 && prop === 'group' ?
  12514. 'hide' :
  12515. 'destroy';
  12516.  
  12517. series[prop][destroy]();
  12518. }
  12519. });
  12520.  
  12521. // remove from hoverSeries
  12522. if (chart.hoverSeries === series) {
  12523. chart.hoverSeries = null;
  12524. }
  12525. erase(chart.series, series);
  12526.  
  12527. // clear all members
  12528. for (prop in series) {
  12529. delete series[prop];
  12530. }
  12531. },
  12532.  
  12533. /**
  12534. * Draw the data labels
  12535. */
  12536. drawDataLabels: function () {
  12537.  
  12538. var series = this,
  12539. seriesOptions = series.options,
  12540. options = seriesOptions.dataLabels;
  12541.  
  12542. if (options.enabled || series._hasPointLabels) {
  12543. var x,
  12544. y,
  12545. points = series.points,
  12546. pointOptions,
  12547. generalOptions,
  12548. str,
  12549. dataLabelsGroup = series.dataLabelsGroup,
  12550. chart = series.chart,
  12551. xAxis = series.xAxis,
  12552. groupLeft = xAxis ? xAxis.left : chart.plotLeft,
  12553. yAxis = series.yAxis,
  12554. groupTop = yAxis ? yAxis.top : chart.plotTop,
  12555. renderer = chart.renderer,
  12556. inverted = chart.inverted,
  12557. seriesType = series.type,
  12558. stacking = seriesOptions.stacking,
  12559. isBarLike = seriesType === 'column' || seriesType === 'bar',
  12560. vAlignIsNull = options.verticalAlign === null,
  12561. yIsNull = options.y === null,
  12562. fontMetrics = renderer.fontMetrics(options.style.fontSize), // height and baseline
  12563. fontLineHeight = fontMetrics.h,
  12564. fontBaseline = fontMetrics.b,
  12565. dataLabel,
  12566. enabled;
  12567.  
  12568. if (isBarLike) {
  12569. var defaultYs = {
  12570. top: fontBaseline,
  12571. middle: fontBaseline - fontLineHeight / 2,
  12572. bottom: -fontLineHeight + fontBaseline
  12573. };
  12574. if (stacking) {
  12575. // In stacked series the default label placement is inside the bars
  12576. if (vAlignIsNull) {
  12577. options = merge(options, {verticalAlign: 'middle'});
  12578. }
  12579.  
  12580. // If no y delta is specified, try to create a good default
  12581. if (yIsNull) {
  12582. options = merge(options, { y: defaultYs[options.verticalAlign]});
  12583. }
  12584. } else {
  12585. // In non stacked series the default label placement is on top of the bars
  12586. if (vAlignIsNull) {
  12587. options = merge(options, {verticalAlign: 'top'});
  12588.  
  12589. // If no y delta is specified, try to create a good default (like default bar)
  12590. } else if (yIsNull) {
  12591. options = merge(options, { y: defaultYs[options.verticalAlign]});
  12592. }
  12593.  
  12594. }
  12595. }
  12596.  
  12597.  
  12598. // create a separate group for the data labels to avoid rotation
  12599. if (!dataLabelsGroup) {
  12600. dataLabelsGroup = series.dataLabelsGroup =
  12601. renderer.g('data-labels')
  12602. .attr({
  12603. visibility: series.visible ? VISIBLE : HIDDEN,
  12604. zIndex: 6
  12605. })
  12606. .translate(groupLeft, groupTop)
  12607. .add();
  12608. } else {
  12609. dataLabelsGroup.translate(groupLeft, groupTop);
  12610. }
  12611.  
  12612. // make the labels for each point
  12613. generalOptions = options;
  12614. each(points, function (point) {
  12615.  
  12616. dataLabel = point.dataLabel;
  12617.  
  12618. // Merge in individual options from point
  12619. options = generalOptions; // reset changes from previous points
  12620. pointOptions = point.options;
  12621. if (pointOptions && pointOptions.dataLabels) {
  12622. options = merge(options, pointOptions.dataLabels);
  12623. }
  12624. enabled = options.enabled;
  12625.  
  12626. // Get the positions
  12627. if (enabled) {
  12628. var plotX = (point.barX && point.barX + point.barW / 2) || pick(point.plotX, -999),
  12629. plotY = pick(point.plotY, -999),
  12630.  
  12631. // if options.y is null, which happens by default on column charts, set the position
  12632. // above or below the column depending on the threshold
  12633. individualYDelta = options.y === null ?
  12634. (point.y >= seriesOptions.threshold ?
  12635. -fontLineHeight + fontBaseline : // below the threshold
  12636. fontBaseline) : // above the threshold
  12637. options.y;
  12638.  
  12639. x = (inverted ? chart.plotWidth - plotY : plotX) + options.x;
  12640. y = mathRound((inverted ? chart.plotHeight - plotX : plotY) + individualYDelta);
  12641.  
  12642. }
  12643.  
  12644. // If the point is outside the plot area, destroy it. #678, #820
  12645. if (dataLabel && series.isCartesian && (!chart.isInsidePlot(x, y) || !enabled)) {
  12646. point.dataLabel = dataLabel.destroy();
  12647.  
  12648. // Individual labels are disabled if the are explicitly disabled
  12649. // in the point options, or if they fall outside the plot area.
  12650. } else if (enabled) {
  12651.  
  12652. var align = options.align,
  12653. attr,
  12654. name;
  12655.  
  12656. // Get the string
  12657. str = options.formatter.call(point.getLabelConfig(), options);
  12658.  
  12659. // in columns, align the string to the column
  12660. if (seriesType === 'column') {
  12661. x += { left: -1, right: 1 }[align] * point.barW / 2 || 0;
  12662. }
  12663.  
  12664. if (!stacking && inverted && point.y < 0) {
  12665. align = 'right';
  12666. x -= 10;
  12667. }
  12668.  
  12669. // Determine the color
  12670. options.style.color = pick(options.color, options.style.color, series.color, 'black');
  12671.  
  12672.  
  12673. // update existing label
  12674. if (dataLabel) {
  12675. // vertically centered
  12676. dataLabel
  12677. .attr({
  12678. text: str
  12679. }).animate({
  12680. x: x,
  12681. y: y
  12682. });
  12683. // create new label
  12684. } else if (defined(str)) {
  12685. attr = {
  12686. align: align,
  12687. fill: options.backgroundColor,
  12688. stroke: options.borderColor,
  12689. 'stroke-width': options.borderWidth,
  12690. r: options.borderRadius || 0,
  12691. rotation: options.rotation,
  12692. padding: options.padding,
  12693. zIndex: 1
  12694. };
  12695. // Remove unused attributes (#947)
  12696. for (name in attr) {
  12697. if (attr[name] === UNDEFINED) {
  12698. delete attr[name];
  12699. }
  12700. }
  12701.  
  12702. dataLabel = point.dataLabel = renderer[options.rotation ? 'text' : 'label']( // labels don't support rotation
  12703. str,
  12704. x,
  12705. y,
  12706. null,
  12707. null,
  12708. null,
  12709. options.useHTML,
  12710. true // baseline for backwards compat
  12711. )
  12712. .attr(attr)
  12713. .css(options.style)
  12714. .add(dataLabelsGroup)
  12715. .shadow(options.shadow);
  12716. }
  12717.  
  12718. if (isBarLike && seriesOptions.stacking && dataLabel) {
  12719. var barX = point.barX,
  12720. barY = point.barY,
  12721. barW = point.barW,
  12722. barH = point.barH;
  12723.  
  12724. dataLabel.align(options, null,
  12725. {
  12726. x: inverted ? chart.plotWidth - barY - barH : barX,
  12727. y: inverted ? chart.plotHeight - barX - barW : barY,
  12728. width: inverted ? barH : barW,
  12729. height: inverted ? barW : barH
  12730. });
  12731. }
  12732.  
  12733.  
  12734. }
  12735. });
  12736. }
  12737. },
  12738.  
  12739. /**
  12740. * Return the graph path of a segment
  12741. */
  12742. getSegmentPath: function (segment) {
  12743. var series = this,
  12744. segmentPath = [];
  12745.  
  12746. // build the segment line
  12747. each(segment, function (point, i) {
  12748.  
  12749. if (series.getPointSpline) { // generate the spline as defined in the SplineSeries object
  12750. segmentPath.push.apply(segmentPath, series.getPointSpline(segment, point, i));
  12751.  
  12752. } else {
  12753.  
  12754. // moveTo or lineTo
  12755. segmentPath.push(i ? L : M);
  12756.  
  12757. // step line?
  12758. if (i && series.options.step) {
  12759. var lastPoint = segment[i - 1];
  12760. segmentPath.push(
  12761. point.plotX,
  12762. lastPoint.plotY
  12763. );
  12764. }
  12765.  
  12766. // normal line to next point
  12767. segmentPath.push(
  12768. point.plotX,
  12769. point.plotY
  12770. );
  12771. }
  12772. });
  12773.  
  12774. return segmentPath;
  12775. },
  12776.  
  12777. /**
  12778. * Draw the actual graph
  12779. */
  12780. drawGraph: function () {
  12781. var series = this,
  12782. options = series.options,
  12783. chart = series.chart,
  12784. graph = series.graph,
  12785. graphPath = [],
  12786. group = series.group,
  12787. color = options.lineColor || series.color,
  12788. lineWidth = options.lineWidth,
  12789. dashStyle = options.dashStyle,
  12790. segmentPath,
  12791. renderer = chart.renderer,
  12792. singlePoints = [], // used in drawTracker
  12793. attribs;
  12794.  
  12795. // divide into segments and build graph and area paths
  12796. each(series.segments, function (segment) {
  12797.  
  12798. segmentPath = series.getSegmentPath(segment);
  12799.  
  12800. // add the segment to the graph, or a single point for tracking
  12801. if (segment.length > 1) {
  12802. graphPath = graphPath.concat(segmentPath);
  12803. } else {
  12804. singlePoints.push(segment[0]);
  12805. }
  12806. });
  12807.  
  12808. // used in drawTracker:
  12809. series.graphPath = graphPath;
  12810. series.singlePoints = singlePoints;
  12811.  
  12812. // draw the graph
  12813. if (graph) {
  12814. stop(graph); // cancel running animations, #459
  12815. graph.animate({ d: graphPath });
  12816.  
  12817. } else {
  12818. if (lineWidth) {
  12819. attribs = {
  12820. stroke: color,
  12821. 'stroke-width': lineWidth
  12822. };
  12823. if (dashStyle) {
  12824. attribs.dashstyle = dashStyle;
  12825. }
  12826.  
  12827. series.graph = renderer.path(graphPath)
  12828. .attr(attribs).add(group).shadow(options.shadow);
  12829. }
  12830. }
  12831. },
  12832.  
  12833. /**
  12834. * Initialize and perform group inversion on series.group and series.trackerGroup
  12835. */
  12836. invertGroups: function () {
  12837. var series = this,
  12838. group = series.group,
  12839. trackerGroup = series.trackerGroup,
  12840. chart = series.chart;
  12841.  
  12842. // A fixed size is needed for inversion to work
  12843. function setInvert() {
  12844. var size = {
  12845. width: series.yAxis.len,
  12846. height: series.xAxis.len
  12847. };
  12848.  
  12849. // Set the series.group size
  12850. group.attr(size).invert();
  12851.  
  12852. // Set the tracker group size
  12853. if (trackerGroup) {
  12854. trackerGroup.attr(size).invert();
  12855. }
  12856. }
  12857.  
  12858. addEvent(chart, 'resize', setInvert); // do it on resize
  12859. addEvent(series, 'destroy', function () {
  12860. removeEvent(chart, 'resize', setInvert);
  12861. });
  12862.  
  12863. // Do it now
  12864. setInvert(); // do it now
  12865.  
  12866. // On subsequent render and redraw, just do setInvert without setting up events again
  12867. series.invertGroups = setInvert;
  12868. },
  12869.  
  12870. /**
  12871. * Create the series group
  12872. */
  12873. createGroup: function () {
  12874.  
  12875. var chart = this.chart,
  12876. group = this.group = chart.renderer.g('series');
  12877.  
  12878. group.attr({
  12879. visibility: this.visible ? VISIBLE : HIDDEN,
  12880. zIndex: this.options.zIndex
  12881. })
  12882. .translate(this.xAxis.left, this.yAxis.top)
  12883. .add(chart.seriesGroup);
  12884.  
  12885. // Only run this once
  12886. this.createGroup = noop;
  12887. },
  12888.  
  12889. /**
  12890. * Render the graph and markers
  12891. */
  12892. render: function () {
  12893. var series = this,
  12894. chart = series.chart,
  12895. group,
  12896. options = series.options,
  12897. doClip = options.clip !== false,
  12898. animation = options.animation,
  12899. doAnimation = animation && series.animate,
  12900. duration = doAnimation ? (animation && animation.duration) || 500 : 0,
  12901. clipRect = series.clipRect,
  12902. renderer = chart.renderer;
  12903.  
  12904.  
  12905. // Add plot area clipping rectangle. If this is before chart.hasRendered,
  12906. // create one shared clipRect.
  12907.  
  12908. // Todo: since creating the clip property, the clipRect is created but
  12909. // never used when clip is false. A better way would be that the animation
  12910. // would run, then the clipRect destroyed.
  12911. if (!clipRect) {
  12912. clipRect = series.clipRect = !chart.hasRendered && chart.clipRect ?
  12913. chart.clipRect :
  12914. renderer.clipRect(0, 0, chart.plotSizeX, chart.plotSizeY + 1);
  12915. if (!chart.clipRect) {
  12916. chart.clipRect = clipRect;
  12917. }
  12918. }
  12919.  
  12920.  
  12921. // the group
  12922. series.createGroup();
  12923. group = series.group;
  12924.  
  12925.  
  12926. series.drawDataLabels();
  12927.  
  12928. // initiate the animation
  12929. if (doAnimation) {
  12930. series.animate(true);
  12931. }
  12932.  
  12933. // cache attributes for shapes
  12934. series.getAttribs();
  12935.  
  12936. // draw the graph if any
  12937. if (series.drawGraph) {
  12938. series.drawGraph();
  12939. }
  12940.  
  12941. // draw the points
  12942. series.drawPoints();
  12943.  
  12944. // draw the mouse tracking area
  12945. if (series.options.enableMouseTracking !== false) {
  12946. series.drawTracker();
  12947. }
  12948.  
  12949. // Handle inverted series and tracker groups
  12950. if (chart.inverted) {
  12951. series.invertGroups();
  12952. }
  12953.  
  12954. // Do the initial clipping. This must be done after inverting for VML.
  12955. if (doClip && !series.hasRendered) {
  12956. group.clip(clipRect);
  12957. if (series.trackerGroup) {
  12958. series.trackerGroup.clip(chart.clipRect);
  12959. }
  12960. }
  12961.  
  12962.  
  12963. // run the animation
  12964. if (doAnimation) {
  12965. series.animate();
  12966. }
  12967.  
  12968. // finish the individual clipRect
  12969. setTimeout(function () {
  12970. clipRect.isAnimating = false;
  12971. group = series.group; // can be destroyed during the timeout
  12972. if (group && clipRect !== chart.clipRect && clipRect.renderer) {
  12973. if (doClip) {
  12974. group.clip((series.clipRect = chart.clipRect));
  12975. }
  12976. clipRect.destroy();
  12977. }
  12978. }, duration);
  12979.  
  12980. series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12981. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  12982. series.hasRendered = true;
  12983. },
  12984.  
  12985. /**
  12986. * Redraw the series after an update in the axes.
  12987. */
  12988. redraw: function () {
  12989. var series = this,
  12990. chart = series.chart,
  12991. wasDirtyData = series.isDirtyData, // cache it here as it is set to false in render, but used after
  12992. group = series.group;
  12993.  
  12994. // reposition on resize
  12995. if (group) {
  12996. if (chart.inverted) {
  12997. group.attr({
  12998. width: chart.plotWidth,
  12999. height: chart.plotHeight
  13000. });
  13001. }
  13002.  
  13003. group.animate({
  13004. translateX: series.xAxis.left,
  13005. translateY: series.yAxis.top
  13006. });
  13007. }
  13008.  
  13009. series.translate();
  13010. series.setTooltipPoints(false);
  13011.  
  13012. series.render();
  13013. if (wasDirtyData) {
  13014. fireEvent(series, 'updatedData');
  13015. }
  13016. },
  13017.  
  13018. /**
  13019. * Set the state of the graph
  13020. */
  13021. setState: function (state) {
  13022. var series = this,
  13023. options = series.options,
  13024. graph = series.graph,
  13025. stateOptions = options.states,
  13026. lineWidth = options.lineWidth;
  13027.  
  13028. state = state || NORMAL_STATE;
  13029.  
  13030. if (series.state !== state) {
  13031. series.state = state;
  13032.  
  13033. if (stateOptions[state] && stateOptions[state].enabled === false) {
  13034. return;
  13035. }
  13036.  
  13037. if (state) {
  13038. lineWidth = stateOptions[state].lineWidth || lineWidth + 1;
  13039. }
  13040.  
  13041. if (graph && !graph.dashstyle) { // hover is turned off for dashed lines in VML
  13042. graph.attr({ // use attr because animate will cause any other animation on the graph to stop
  13043. 'stroke-width': lineWidth
  13044. }, state ? 0 : 500);
  13045. }
  13046. }
  13047. },
  13048.  
  13049. /**
  13050. * Set the visibility of the graph
  13051. *
  13052. * @param vis {Boolean} True to show the series, false to hide. If UNDEFINED,
  13053. * the visibility is toggled.
  13054. */
  13055. setVisible: function (vis, redraw) {
  13056. var series = this,
  13057. chart = series.chart,
  13058. legendItem = series.legendItem,
  13059. seriesGroup = series.group,
  13060. seriesTracker = series.tracker,
  13061. dataLabelsGroup = series.dataLabelsGroup,
  13062. showOrHide,
  13063. i,
  13064. points = series.points,
  13065. point,
  13066. ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
  13067. oldVisibility = series.visible;
  13068.  
  13069. // if called without an argument, toggle visibility
  13070. series.visible = vis = vis === UNDEFINED ? !oldVisibility : vis;
  13071. showOrHide = vis ? 'show' : 'hide';
  13072.  
  13073. // show or hide series
  13074. if (seriesGroup) { // pies don't have one
  13075. seriesGroup[showOrHide]();
  13076. }
  13077.  
  13078. // show or hide trackers
  13079. if (seriesTracker) {
  13080. seriesTracker[showOrHide]();
  13081. } else if (points) {
  13082. i = points.length;
  13083. while (i--) {
  13084. point = points[i];
  13085. if (point.tracker) {
  13086. point.tracker[showOrHide]();
  13087. }
  13088. }
  13089. }
  13090.  
  13091.  
  13092. if (dataLabelsGroup) {
  13093. dataLabelsGroup[showOrHide]();
  13094. }
  13095.  
  13096. if (legendItem) {
  13097. chart.legend.colorizeItem(series, vis);
  13098. }
  13099.  
  13100.  
  13101. // rescale or adapt to resized chart
  13102. series.isDirty = true;
  13103. // in a stack, all other series are affected
  13104. if (series.options.stacking) {
  13105. each(chart.series, function (otherSeries) {
  13106. if (otherSeries.options.stacking && otherSeries.visible) {
  13107. otherSeries.isDirty = true;
  13108. }
  13109. });
  13110. }
  13111.  
  13112. if (ignoreHiddenSeries) {
  13113. chart.isDirtyBox = true;
  13114. }
  13115. if (redraw !== false) {
  13116. chart.redraw();
  13117. }
  13118.  
  13119. fireEvent(series, showOrHide);
  13120. },
  13121.  
  13122. /**
  13123. * Show the graph
  13124. */
  13125. show: function () {
  13126. this.setVisible(true);
  13127. },
  13128.  
  13129. /**
  13130. * Hide the graph
  13131. */
  13132. hide: function () {
  13133. this.setVisible(false);
  13134. },
  13135.  
  13136.  
  13137. /**
  13138. * Set the selected state of the graph
  13139. *
  13140. * @param selected {Boolean} True to select the series, false to unselect. If
  13141. * UNDEFINED, the selection state is toggled.
  13142. */
  13143. select: function (selected) {
  13144. var series = this;
  13145. // if called without an argument, toggle
  13146. series.selected = selected = (selected === UNDEFINED) ? !series.selected : selected;
  13147.  
  13148. if (series.checkbox) {
  13149. series.checkbox.checked = selected;
  13150. }
  13151.  
  13152. fireEvent(series, selected ? 'select' : 'unselect');
  13153. },
  13154.  
  13155. /**
  13156. * Create a group that holds the tracking object or objects. This allows for
  13157. * individual clipping and placement of each series tracker.
  13158. */
  13159. drawTrackerGroup: function () {
  13160. var trackerGroup = this.trackerGroup,
  13161. chart = this.chart;
  13162.  
  13163. if (this.isCartesian) {
  13164.  
  13165. // Generate it on first call
  13166. if (!trackerGroup) {
  13167. this.trackerGroup = trackerGroup = chart.renderer.g()
  13168. .attr({
  13169. zIndex: this.options.zIndex || 1
  13170. })
  13171. .add(chart.trackerGroup);
  13172.  
  13173. }
  13174. // Place it on first and subsequent (redraw) calls
  13175. trackerGroup.translate(this.xAxis.left, this.yAxis.top);
  13176.  
  13177. }
  13178.  
  13179. return trackerGroup;
  13180. },
  13181.  
  13182. /**
  13183. * Draw the tracker object that sits above all data labels and markers to
  13184. * track mouse events on the graph or points. For the line type charts
  13185. * the tracker uses the same graphPath, but with a greater stroke width
  13186. * for better control.
  13187. */
  13188. drawTracker: function () {
  13189. var series = this,
  13190. options = series.options,
  13191. trackByArea = options.trackByArea,
  13192. trackerPath = [].concat(trackByArea ? series.areaPath : series.graphPath),
  13193. trackerPathLength = trackerPath.length,
  13194. chart = series.chart,
  13195. renderer = chart.renderer,
  13196. snap = chart.options.tooltip.snap,
  13197. tracker = series.tracker,
  13198. cursor = options.cursor,
  13199. css = cursor && { cursor: cursor },
  13200. singlePoints = series.singlePoints,
  13201. trackerGroup = series.drawTrackerGroup(),
  13202. singlePoint,
  13203. i;
  13204.  
  13205. // Extend end points. A better way would be to use round linecaps,
  13206. // but those are not clickable in VML.
  13207. if (trackerPathLength && !trackByArea) {
  13208. i = trackerPathLength + 1;
  13209. while (i--) {
  13210. if (trackerPath[i] === M) { // extend left side
  13211. trackerPath.splice(i + 1, 0, trackerPath[i + 1] - snap, trackerPath[i + 2], L);
  13212. }
  13213. if ((i && trackerPath[i] === M) || i === trackerPathLength) { // extend right side
  13214. trackerPath.splice(i, 0, L, trackerPath[i - 2] + snap, trackerPath[i - 1]);
  13215. }
  13216. }
  13217. }
  13218.  
  13219. // handle single points
  13220. for (i = 0; i < singlePoints.length; i++) {
  13221. singlePoint = singlePoints[i];
  13222. trackerPath.push(M, singlePoint.plotX - snap, singlePoint.plotY,
  13223. L, singlePoint.plotX + snap, singlePoint.plotY);
  13224. }
  13225.  
  13226.  
  13227.  
  13228. // draw the tracker
  13229. if (tracker) {
  13230. tracker.attr({ d: trackerPath });
  13231.  
  13232. } else { // create
  13233.  
  13234. series.tracker = renderer.path(trackerPath)
  13235. .attr({
  13236. isTracker: true,
  13237. 'stroke-linejoin': 'bevel',
  13238. visibility: series.visible ? VISIBLE : HIDDEN,
  13239. stroke: TRACKER_FILL,
  13240. fill: trackByArea ? TRACKER_FILL : NONE,
  13241. 'stroke-width' : options.lineWidth + (trackByArea ? 0 : 2 * snap)
  13242. })
  13243. .on(hasTouch ? 'touchstart' : 'mouseover', function () {
  13244. if (chart.hoverSeries !== series) {
  13245. series.onMouseOver();
  13246. }
  13247. })
  13248. .on('mouseout', function () {
  13249. if (!options.stickyTracking) {
  13250. series.onMouseOut();
  13251. }
  13252. })
  13253. .css(css)
  13254. .add(trackerGroup);
  13255. }
  13256.  
  13257. }
  13258.  
  13259. }; // end Series prototype
  13260.  
  13261.  
  13262. /**
  13263. * LineSeries object
  13264. */
  13265. var LineSeries = extendClass(Series);
  13266. seriesTypes.line = LineSeries;
  13267.  
  13268. /**
  13269. * Set the default options for area
  13270. */
  13271. defaultPlotOptions.area = merge(defaultSeriesOptions, {
  13272. threshold: 0
  13273. // trackByArea: false,
  13274. // lineColor: null, // overrides color, but lets fillColor be unaltered
  13275. // fillOpacity: 0.75,
  13276. // fillColor: null
  13277. });
  13278.  
  13279. /**
  13280. * AreaSeries object
  13281. */
  13282. var AreaSeries = extendClass(Series, {
  13283. type: 'area',
  13284.  
  13285. /**
  13286. * Extend the base Series getSegmentPath method by adding the path for the area.
  13287. * This path is pushed to the series.areaPath property.
  13288. */
  13289. getSegmentPath: function (segment) {
  13290.  
  13291. var segmentPath = Series.prototype.getSegmentPath.call(this, segment), // call base method
  13292. areaSegmentPath = [].concat(segmentPath), // work on a copy for the area path
  13293. i,
  13294. options = this.options,
  13295. segLength = segmentPath.length,
  13296. translatedThreshold = this.yAxis.getThreshold(options.threshold);
  13297.  
  13298. if (segLength === 3) { // for animation from 1 to two points
  13299. areaSegmentPath.push(L, segmentPath[1], segmentPath[2]);
  13300. }
  13301. if (options.stacking && this.type !== 'areaspline') {
  13302.  
  13303. // Follow stack back. Todo: implement areaspline. A general solution could be to
  13304. // reverse the entire graphPath of the previous series, though may be hard with
  13305. // splines and with series with different extremes
  13306. for (i = segment.length - 1; i >= 0; i--) {
  13307.  
  13308. // step line?
  13309. if (i < segment.length - 1 && options.step) {
  13310. areaSegmentPath.push(segment[i + 1].plotX, segment[i].yBottom);
  13311. }
  13312.  
  13313. areaSegmentPath.push(segment[i].plotX, segment[i].yBottom);
  13314. }
  13315.  
  13316. } else { // follow zero line back
  13317. areaSegmentPath.push(
  13318. L,
  13319. segment[segment.length - 1].plotX,
  13320. translatedThreshold,
  13321. L,
  13322. segment[0].plotX,
  13323. translatedThreshold
  13324. );
  13325. }
  13326. this.areaPath = this.areaPath.concat(areaSegmentPath);
  13327.  
  13328. return segmentPath;
  13329. },
  13330.  
  13331. /**
  13332. * Draw the graph and the underlying area. This method calls the Series base
  13333. * function and adds the area. The areaPath is calculated in the getSegmentPath
  13334. * method called from Series.prototype.drawGraph.
  13335. */
  13336. drawGraph: function () {
  13337.  
  13338. // Define or reset areaPath
  13339. this.areaPath = [];
  13340.  
  13341. // Call the base method
  13342. Series.prototype.drawGraph.apply(this);
  13343.  
  13344. // Define local variables
  13345. var areaPath = this.areaPath,
  13346. options = this.options,
  13347. area = this.area;
  13348.  
  13349. // Create or update the area
  13350. if (area) { // update
  13351. area.animate({ d: areaPath });
  13352.  
  13353. } else { // create
  13354. this.area = this.chart.renderer.path(areaPath)
  13355. .attr({
  13356. fill: pick(
  13357. options.fillColor,
  13358. Color(this.color).setOpacity(options.fillOpacity || 0.75).get()
  13359. )
  13360. }).add(this.group);
  13361. }
  13362. },
  13363.  
  13364. /**
  13365. * Get the series' symbol in the legend
  13366. *
  13367. * @param {Object} legend The legend object
  13368. * @param {Object} item The series (this) or point
  13369. */
  13370. drawLegendSymbol: function (legend, item) {
  13371.  
  13372. item.legendSymbol = this.chart.renderer.rect(
  13373. 0,
  13374. legend.baseline - 11,
  13375. legend.options.symbolWidth,
  13376. 12,
  13377. 2
  13378. ).attr({
  13379. zIndex: 3
  13380. }).add(item.legendGroup);
  13381.  
  13382. }
  13383. });
  13384.  
  13385. seriesTypes.area = AreaSeries;/**
  13386. * Set the default options for spline
  13387. */
  13388. defaultPlotOptions.spline = merge(defaultSeriesOptions);
  13389.  
  13390. /**
  13391. * SplineSeries object
  13392. */
  13393. var SplineSeries = extendClass(Series, {
  13394. type: 'spline',
  13395.  
  13396. /**
  13397. * Draw the actual graph
  13398. */
  13399. getPointSpline: function (segment, point, i) {
  13400. var smoothing = 1.5, // 1 means control points midway between points, 2 means 1/3 from the point, 3 is 1/4 etc
  13401. denom = smoothing + 1,
  13402. plotX = point.plotX,
  13403. plotY = point.plotY,
  13404. lastPoint = segment[i - 1],
  13405. nextPoint = segment[i + 1],
  13406. leftContX,
  13407. leftContY,
  13408. rightContX,
  13409. rightContY,
  13410. ret;
  13411.  
  13412. // find control points
  13413. if (i && i < segment.length - 1) {
  13414. var lastX = lastPoint.plotX,
  13415. lastY = lastPoint.plotY,
  13416. nextX = nextPoint.plotX,
  13417. nextY = nextPoint.plotY,
  13418. correction;
  13419.  
  13420. leftContX = (smoothing * plotX + lastX) / denom;
  13421. leftContY = (smoothing * plotY + lastY) / denom;
  13422. rightContX = (smoothing * plotX + nextX) / denom;
  13423. rightContY = (smoothing * plotY + nextY) / denom;
  13424.  
  13425. // have the two control points make a straight line through main point
  13426. correction = ((rightContY - leftContY) * (rightContX - plotX)) /
  13427. (rightContX - leftContX) + plotY - rightContY;
  13428.  
  13429. leftContY += correction;
  13430. rightContY += correction;
  13431.  
  13432. // to prevent false extremes, check that control points are between
  13433. // neighbouring points' y values
  13434. if (leftContY > lastY && leftContY > plotY) {
  13435. leftContY = mathMax(lastY, plotY);
  13436. rightContY = 2 * plotY - leftContY; // mirror of left control point
  13437. } else if (leftContY < lastY && leftContY < plotY) {
  13438. leftContY = mathMin(lastY, plotY);
  13439. rightContY = 2 * plotY - leftContY;
  13440. }
  13441. if (rightContY > nextY && rightContY > plotY) {
  13442. rightContY = mathMax(nextY, plotY);
  13443. leftContY = 2 * plotY - rightContY;
  13444. } else if (rightContY < nextY && rightContY < plotY) {
  13445. rightContY = mathMin(nextY, plotY);
  13446. leftContY = 2 * plotY - rightContY;
  13447. }
  13448.  
  13449. // record for drawing in next point
  13450. point.rightContX = rightContX;
  13451. point.rightContY = rightContY;
  13452.  
  13453. }
  13454.  
  13455. // moveTo or lineTo
  13456. if (!i) {
  13457. ret = [M, plotX, plotY];
  13458. } else { // curve from last point to this
  13459. ret = [
  13460. 'C',
  13461. lastPoint.rightContX || lastPoint.plotX,
  13462. lastPoint.rightContY || lastPoint.plotY,
  13463. leftContX || plotX,
  13464. leftContY || plotY,
  13465. plotX,
  13466. plotY
  13467. ];
  13468. lastPoint.rightContX = lastPoint.rightContY = null; // reset for updating series later
  13469. }
  13470. return ret;
  13471. }
  13472. });
  13473. seriesTypes.spline = SplineSeries;
  13474.  
  13475. /**
  13476. * Set the default options for areaspline
  13477. */
  13478. defaultPlotOptions.areaspline = merge(defaultPlotOptions.area);
  13479.  
  13480. /**
  13481. * AreaSplineSeries object
  13482. */
  13483. var areaProto = AreaSeries.prototype,
  13484. AreaSplineSeries = extendClass(SplineSeries, {
  13485. type: 'areaspline',
  13486.  
  13487. // Mix in methods from the area series
  13488. getSegmentPath: areaProto.getSegmentPath,
  13489. drawGraph: areaProto.drawGraph
  13490. });
  13491. seriesTypes.areaspline = AreaSplineSeries;
  13492.  
  13493. /**
  13494. * Set the default options for column
  13495. */
  13496. defaultPlotOptions.column = merge(defaultSeriesOptions, {
  13497. borderColor: '#FFFFFF',
  13498. borderWidth: 1,
  13499. borderRadius: 0,
  13500. //colorByPoint: undefined,
  13501. groupPadding: 0.2,
  13502. marker: null, // point options are specified in the base options
  13503. pointPadding: 0.1,
  13504. //pointWidth: null,
  13505. minPointLength: 0,
  13506. cropThreshold: 50, // when there are more points, they will not animate out of the chart on xAxis.setExtremes
  13507. pointRange: null, // null means auto, meaning 1 in a categorized axis and least distance between points if not categories
  13508. states: {
  13509. hover: {
  13510. brightness: 0.1,
  13511. shadow: false
  13512. },
  13513. select: {
  13514. color: '#C0C0C0',
  13515. borderColor: '#000000',
  13516. shadow: false
  13517. }
  13518. },
  13519. dataLabels: {
  13520. y: null,
  13521. verticalAlign: null
  13522. },
  13523. threshold: 0
  13524. });
  13525.  
  13526. /**
  13527. * ColumnSeries object
  13528. */
  13529. var ColumnSeries = extendClass(Series, {
  13530. type: 'column',
  13531. tooltipOutsidePlot: true,
  13532. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  13533. stroke: 'borderColor',
  13534. 'stroke-width': 'borderWidth',
  13535. fill: 'color',
  13536. r: 'borderRadius'
  13537. },
  13538. init: function () {
  13539. Series.prototype.init.apply(this, arguments);
  13540.  
  13541. var series = this,
  13542. chart = series.chart;
  13543.  
  13544. // if the series is added dynamically, force redraw of other
  13545. // series affected by a new column
  13546. if (chart.hasRendered) {
  13547. each(chart.series, function (otherSeries) {
  13548. if (otherSeries.type === series.type) {
  13549. otherSeries.isDirty = true;
  13550. }
  13551. });
  13552. }
  13553. },
  13554.  
  13555. /**
  13556. * Translate each point to the plot area coordinate system and find shape positions
  13557. */
  13558. translate: function () {
  13559. var series = this,
  13560. chart = series.chart,
  13561. options = series.options,
  13562. stacking = options.stacking,
  13563. borderWidth = options.borderWidth,
  13564. columnCount = 0,
  13565. xAxis = series.xAxis,
  13566. reversedXAxis = xAxis.reversed,
  13567. stackGroups = {},
  13568. stackKey,
  13569. columnIndex;
  13570.  
  13571. Series.prototype.translate.apply(series);
  13572.  
  13573. // Get the total number of column type series.
  13574. // This is called on every series. Consider moving this logic to a
  13575. // chart.orderStacks() function and call it on init, addSeries and removeSeries
  13576. each(chart.series, function (otherSeries) {
  13577. if (otherSeries.type === series.type && otherSeries.visible &&
  13578. series.options.group === otherSeries.options.group) { // used in Stock charts navigator series
  13579. if (otherSeries.options.stacking) {
  13580. stackKey = otherSeries.stackKey;
  13581. if (stackGroups[stackKey] === UNDEFINED) {
  13582. stackGroups[stackKey] = columnCount++;
  13583. }
  13584. columnIndex = stackGroups[stackKey];
  13585. } else {
  13586. columnIndex = columnCount++;
  13587. }
  13588. otherSeries.columnIndex = columnIndex;
  13589. }
  13590. });
  13591.  
  13592. // calculate the width and position of each column based on
  13593. // the number of column series in the plot, the groupPadding
  13594. // and the pointPadding options
  13595. var points = series.points,
  13596. categoryWidth = mathAbs(xAxis.transA) * (xAxis.ordinalSlope || options.pointRange || xAxis.closestPointRange || 1),
  13597. groupPadding = categoryWidth * options.groupPadding,
  13598. groupWidth = categoryWidth - 2 * groupPadding,
  13599. pointOffsetWidth = groupWidth / columnCount,
  13600. optionPointWidth = options.pointWidth,
  13601. pointPadding = defined(optionPointWidth) ? (pointOffsetWidth - optionPointWidth) / 2 :
  13602. pointOffsetWidth * options.pointPadding,
  13603. pointWidth = pick(optionPointWidth, pointOffsetWidth - 2 * pointPadding), // exact point width, used in polar charts
  13604. barW = mathCeil(mathMax(pointWidth, 1 + 2 * borderWidth)), // rounded and postprocessed for border width
  13605. colIndex = (reversedXAxis ? columnCount -
  13606. series.columnIndex : series.columnIndex) || 0,
  13607. pointXOffset = pointPadding + (groupPadding + colIndex *
  13608. pointOffsetWidth - (categoryWidth / 2)) *
  13609. (reversedXAxis ? -1 : 1),
  13610. threshold = options.threshold,
  13611. translatedThreshold = series.yAxis.getThreshold(threshold),
  13612. minPointLength = pick(options.minPointLength, 5);
  13613.  
  13614. // record the new values
  13615. each(points, function (point) {
  13616. var plotY = point.plotY,
  13617. yBottom = pick(point.yBottom, translatedThreshold),
  13618. barX = point.plotX + pointXOffset,
  13619. barY = mathCeil(mathMin(plotY, yBottom)),
  13620. barH = mathCeil(mathMax(plotY, yBottom) - barY),
  13621. stack = series.yAxis.stacks[(point.y < 0 ? '-' : '') + series.stackKey],
  13622. shapeArgs;
  13623.  
  13624. // Record the offset'ed position and width of the bar to be able to align the stacking total correctly
  13625. if (stacking && series.visible && stack && stack[point.x]) {
  13626. stack[point.x].setOffset(pointXOffset, barW);
  13627. }
  13628.  
  13629. // handle options.minPointLength
  13630. if (mathAbs(barH) < minPointLength) {
  13631. if (minPointLength) {
  13632. barH = minPointLength;
  13633. barY =
  13634. mathAbs(barY - translatedThreshold) > minPointLength ? // stacked
  13635. yBottom - minPointLength : // keep position
  13636. translatedThreshold - (plotY <= translatedThreshold ? minPointLength : 0);
  13637. }
  13638. }
  13639.  
  13640. extend(point, {
  13641. barX: barX,
  13642. barY: barY,
  13643. barW: barW,
  13644. barH: barH,
  13645. pointWidth: pointWidth
  13646. });
  13647.  
  13648. // create shape type and shape args that are reused in drawPoints and drawTracker
  13649. point.shapeType = 'rect';
  13650. point.shapeArgs = shapeArgs = chart.renderer.Element.prototype.crisp.call(0, borderWidth, barX, barY, barW, barH);
  13651.  
  13652. if (borderWidth % 2) { // correct for shorting in crisp method, visible in stacked columns with 1px border
  13653. shapeArgs.y -= 1;
  13654. shapeArgs.height += 1;
  13655. }
  13656.  
  13657. // make small columns responsive to mouse
  13658. point.trackerArgs = mathAbs(barH) < 3 && merge(point.shapeArgs, {
  13659. height: 6,
  13660. y: barY - 3
  13661. });
  13662. });
  13663.  
  13664. },
  13665.  
  13666. getSymbol: function () {
  13667. },
  13668.  
  13669. /**
  13670. * Use a solid rectangle like the area series types
  13671. */
  13672. drawLegendSymbol: AreaSeries.prototype.drawLegendSymbol,
  13673.  
  13674.  
  13675. /**
  13676. * Columns have no graph
  13677. */
  13678. drawGraph: function () {},
  13679.  
  13680. /**
  13681. * Draw the columns. For bars, the series.group is rotated, so the same coordinates
  13682. * apply for columns and bars. This method is inherited by scatter series.
  13683. *
  13684. */
  13685. drawPoints: function () {
  13686. var series = this,
  13687. options = series.options,
  13688. renderer = series.chart.renderer,
  13689. graphic,
  13690. shapeArgs;
  13691.  
  13692.  
  13693. // draw the columns
  13694. each(series.points, function (point) {
  13695. var plotY = point.plotY;
  13696. if (plotY !== UNDEFINED && !isNaN(plotY) && point.y !== null) {
  13697. graphic = point.graphic;
  13698. shapeArgs = point.shapeArgs;
  13699. if (graphic) { // update
  13700. stop(graphic);
  13701. graphic.animate(merge(shapeArgs));
  13702.  
  13703. } else {
  13704. point.graphic = graphic = renderer[point.shapeType](shapeArgs)
  13705. .attr(point.pointAttr[point.selected ? SELECT_STATE : NORMAL_STATE])
  13706. .add(series.group)
  13707. .shadow(options.shadow, null, options.stacking && !options.borderRadius);
  13708. }
  13709.  
  13710. }
  13711. });
  13712. },
  13713. /**
  13714. * Draw the individual tracker elements.
  13715. * This method is inherited by scatter and pie charts too.
  13716. */
  13717. drawTracker: function () {
  13718. var series = this,
  13719. chart = series.chart,
  13720. renderer = chart.renderer,
  13721. shapeArgs,
  13722. tracker,
  13723. trackerLabel = +new Date(),
  13724. options = series.options,
  13725. cursor = options.cursor,
  13726. css = cursor && { cursor: cursor },
  13727. trackerGroup = series.drawTrackerGroup(),
  13728. rel,
  13729. plotY,
  13730. validPlotY;
  13731.  
  13732. each(series.points, function (point) {
  13733. tracker = point.tracker;
  13734. shapeArgs = point.trackerArgs || point.shapeArgs;
  13735. plotY = point.plotY;
  13736. validPlotY = !series.isCartesian || (plotY !== UNDEFINED && !isNaN(plotY));
  13737. delete shapeArgs.strokeWidth;
  13738. if (point.y !== null && validPlotY) {
  13739. if (tracker) {// update
  13740. tracker.attr(shapeArgs);
  13741.  
  13742. } else {
  13743. point.tracker =
  13744. renderer[point.shapeType](shapeArgs)
  13745. .attr({
  13746. isTracker: trackerLabel,
  13747. fill: TRACKER_FILL,
  13748. visibility: series.visible ? VISIBLE : HIDDEN
  13749. })
  13750. .on(hasTouch ? 'touchstart' : 'mouseover', function (event) {
  13751. rel = event.relatedTarget || event.fromElement;
  13752. if (chart.hoverSeries !== series && attr(rel, 'isTracker') !== trackerLabel) {
  13753. series.onMouseOver();
  13754. }
  13755. point.onMouseOver();
  13756.  
  13757. })
  13758. .on('mouseout', function (event) {
  13759. if (!options.stickyTracking) {
  13760. rel = event.relatedTarget || event.toElement;
  13761. if (attr(rel, 'isTracker') !== trackerLabel) {
  13762. series.onMouseOut();
  13763. }
  13764. }
  13765. })
  13766. .css(css)
  13767. .add(point.group || trackerGroup); // pies have point group - see issue #118
  13768. }
  13769. }
  13770. });
  13771. },
  13772.  
  13773.  
  13774. /**
  13775. * Animate the column heights one by one from zero
  13776. * @param {Boolean} init Whether to initialize the animation or run it
  13777. */
  13778. animate: function (init) {
  13779. var series = this,
  13780. points = series.points,
  13781. options = series.options;
  13782.  
  13783. if (!init) { // run the animation
  13784. /*
  13785. * Note: Ideally the animation should be initialized by calling
  13786. * series.group.hide(), and then calling series.group.show()
  13787. * after the animation was started. But this rendered the shadows
  13788. * invisible in IE8 standards mode. If the columns flicker on large
  13789. * datasets, this is the cause.
  13790. */
  13791.  
  13792. each(points, function (point) {
  13793. var graphic = point.graphic,
  13794. shapeArgs = point.shapeArgs,
  13795. yAxis = series.yAxis,
  13796. threshold = options.threshold;
  13797.  
  13798. if (graphic) {
  13799. // start values
  13800. graphic.attr({
  13801. height: 0,
  13802. y: defined(threshold) ?
  13803. yAxis.getThreshold(threshold) :
  13804. yAxis.translate(yAxis.getExtremes().min, 0, 1, 0, 1)
  13805. });
  13806.  
  13807. // animate
  13808. graphic.animate({
  13809. height: shapeArgs.height,
  13810. y: shapeArgs.y
  13811. }, options.animation);
  13812. }
  13813. });
  13814.  
  13815.  
  13816. // delete this function to allow it only once
  13817. series.animate = null;
  13818. }
  13819.  
  13820. },
  13821. /**
  13822. * Remove this series from the chart
  13823. */
  13824. remove: function () {
  13825. var series = this,
  13826. chart = series.chart;
  13827.  
  13828. // column and bar series affects other series of the same type
  13829. // as they are either stacked or grouped
  13830. if (chart.hasRendered) {
  13831. each(chart.series, function (otherSeries) {
  13832. if (otherSeries.type === series.type) {
  13833. otherSeries.isDirty = true;
  13834. }
  13835. });
  13836. }
  13837.  
  13838. Series.prototype.remove.apply(series, arguments);
  13839. }
  13840. });
  13841. seriesTypes.column = ColumnSeries;
  13842. /**
  13843. * Set the default options for bar
  13844. */
  13845. defaultPlotOptions.bar = merge(defaultPlotOptions.column, {
  13846. dataLabels: {
  13847. align: 'left',
  13848. x: 5,
  13849. y: null,
  13850. verticalAlign: 'middle'
  13851. }
  13852. });
  13853. /**
  13854. * The Bar series class
  13855. */
  13856. var BarSeries = extendClass(ColumnSeries, {
  13857. type: 'bar',
  13858. inverted: true
  13859. });
  13860. seriesTypes.bar = BarSeries;
  13861.  
  13862. /**
  13863. * Set the default options for scatter
  13864. */
  13865. defaultPlotOptions.scatter = merge(defaultSeriesOptions, {
  13866. lineWidth: 0,
  13867. states: {
  13868. hover: {
  13869. lineWidth: 0
  13870. }
  13871. },
  13872. tooltip: {
  13873. headerFormat: '<span style="font-size: 10px; color:{series.color}">{series.name}</span><br/>',
  13874. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>'
  13875. }
  13876. });
  13877.  
  13878. /**
  13879. * The scatter series class
  13880. */
  13881. var ScatterSeries = extendClass(Series, {
  13882. type: 'scatter',
  13883. sorted: false,
  13884. /**
  13885. * Extend the base Series' translate method by adding shape type and
  13886. * arguments for the point trackers
  13887. */
  13888. translate: function () {
  13889. var series = this;
  13890.  
  13891. Series.prototype.translate.apply(series);
  13892.  
  13893. each(series.points, function (point) {
  13894. point.shapeType = 'circle';
  13895. point.shapeArgs = {
  13896. x: point.plotX,
  13897. y: point.plotY,
  13898. r: series.chart.options.tooltip.snap
  13899. };
  13900. });
  13901. },
  13902.  
  13903. /**
  13904. * Add tracking event listener to the series group, so the point graphics
  13905. * themselves act as trackers
  13906. */
  13907. drawTracker: function () {
  13908. var series = this,
  13909. cursor = series.options.cursor,
  13910. css = cursor && { cursor: cursor },
  13911. points = series.points,
  13912. i = points.length,
  13913. graphic;
  13914.  
  13915. // Set an expando property for the point index, used below
  13916. while (i--) {
  13917. graphic = points[i].graphic;
  13918. if (graphic) { // doesn't exist for null points
  13919. graphic.element._i = i;
  13920. }
  13921. }
  13922.  
  13923. // Add the event listeners, we need to do this only once
  13924. if (!series._hasTracking) {
  13925. series.group
  13926. .attr({
  13927. isTracker: true
  13928. })
  13929. .on(hasTouch ? 'touchstart' : 'mouseover', function (e) {
  13930. series.onMouseOver();
  13931. if (e.target._i !== UNDEFINED) { // undefined on graph in scatterchart
  13932. points[e.target._i].onMouseOver();
  13933. }
  13934. })
  13935. .on('mouseout', function () {
  13936. if (!series.options.stickyTracking) {
  13937. series.onMouseOut();
  13938. }
  13939. })
  13940. .css(css);
  13941. } else {
  13942. series._hasTracking = true;
  13943. }
  13944.  
  13945. }
  13946. });
  13947. seriesTypes.scatter = ScatterSeries;
  13948.  
  13949. /**
  13950. * Set the default options for pie
  13951. */
  13952. defaultPlotOptions.pie = merge(defaultSeriesOptions, {
  13953. borderColor: '#FFFFFF',
  13954. borderWidth: 1,
  13955. center: ['50%', '50%'],
  13956. colorByPoint: true, // always true for pies
  13957. dataLabels: {
  13958. // align: null,
  13959. // connectorWidth: 1,
  13960. // connectorColor: point.color,
  13961. // connectorPadding: 5,
  13962. distance: 30,
  13963. enabled: true,
  13964. formatter: function () {
  13965. return this.point.name;
  13966. },
  13967. // softConnector: true,
  13968. y: 5
  13969. },
  13970. //innerSize: 0,
  13971. legendType: 'point',
  13972. marker: null, // point options are specified in the base options
  13973. size: '75%',
  13974. showInLegend: false,
  13975. slicedOffset: 10,
  13976. states: {
  13977. hover: {
  13978. brightness: 0.1,
  13979. shadow: false
  13980. }
  13981. }
  13982. });
  13983.  
  13984. /**
  13985. * Extended point object for pies
  13986. */
  13987. var PiePoint = extendClass(Point, {
  13988. /**
  13989. * Initiate the pie slice
  13990. */
  13991. init: function () {
  13992.  
  13993. Point.prototype.init.apply(this, arguments);
  13994.  
  13995. var point = this,
  13996. toggleSlice;
  13997.  
  13998. //visible: options.visible !== false,
  13999. extend(point, {
  14000. visible: point.visible !== false,
  14001. name: pick(point.name, 'Slice')
  14002. });
  14003.  
  14004. // add event listener for select
  14005. toggleSlice = function () {
  14006. point.slice();
  14007. };
  14008. addEvent(point, 'select', toggleSlice);
  14009. addEvent(point, 'unselect', toggleSlice);
  14010.  
  14011. return point;
  14012. },
  14013.  
  14014. /**
  14015. * Toggle the visibility of the pie slice
  14016. * @param {Boolean} vis Whether to show the slice or not. If undefined, the
  14017. * visibility is toggled
  14018. */
  14019. setVisible: function (vis) {
  14020. var point = this,
  14021. chart = point.series.chart,
  14022. tracker = point.tracker,
  14023. dataLabel = point.dataLabel,
  14024. connector = point.connector,
  14025. shadowGroup = point.shadowGroup,
  14026. method;
  14027.  
  14028. // if called without an argument, toggle visibility
  14029. point.visible = vis = vis === UNDEFINED ? !point.visible : vis;
  14030.  
  14031. method = vis ? 'show' : 'hide';
  14032.  
  14033. point.group[method]();
  14034. if (tracker) {
  14035. tracker[method]();
  14036. }
  14037. if (dataLabel) {
  14038. dataLabel[method]();
  14039. }
  14040. if (connector) {
  14041. connector[method]();
  14042. }
  14043. if (shadowGroup) {
  14044. shadowGroup[method]();
  14045. }
  14046. if (point.legendItem) {
  14047. chart.legend.colorizeItem(point, vis);
  14048. }
  14049. },
  14050.  
  14051. /**
  14052. * Set or toggle whether the slice is cut out from the pie
  14053. * @param {Boolean} sliced When undefined, the slice state is toggled
  14054. * @param {Boolean} redraw Whether to redraw the chart. True by default.
  14055. */
  14056. slice: function (sliced, redraw, animation) {
  14057. var point = this,
  14058. series = point.series,
  14059. chart = series.chart,
  14060. slicedTranslation = point.slicedTranslation,
  14061. translation;
  14062.  
  14063. setAnimation(animation, chart);
  14064.  
  14065. // redraw is true by default
  14066. redraw = pick(redraw, true);
  14067.  
  14068. // if called without an argument, toggle
  14069. sliced = point.sliced = defined(sliced) ? sliced : !point.sliced;
  14070.  
  14071. translation = {
  14072. translateX: (sliced ? slicedTranslation[0] : chart.plotLeft),
  14073. translateY: (sliced ? slicedTranslation[1] : chart.plotTop)
  14074. };
  14075. point.group.animate(translation);
  14076. if (point.shadowGroup) {
  14077. point.shadowGroup.animate(translation);
  14078. }
  14079.  
  14080. }
  14081. });
  14082.  
  14083. /**
  14084. * The Pie series class
  14085. */
  14086. var PieSeries = {
  14087. type: 'pie',
  14088. isCartesian: false,
  14089. pointClass: PiePoint,
  14090. pointAttrToOptions: { // mapping between SVG attributes and the corresponding options
  14091. stroke: 'borderColor',
  14092. 'stroke-width': 'borderWidth',
  14093. fill: 'color'
  14094. },
  14095.  
  14096. /**
  14097. * Pies have one color each point
  14098. */
  14099. getColor: function () {
  14100. // record first color for use in setData
  14101. this.initialColor = this.chart.counters.color;
  14102. },
  14103.  
  14104. /**
  14105. * Animate the pies in
  14106. */
  14107. animate: function () {
  14108. var series = this,
  14109. points = series.points;
  14110.  
  14111. each(points, function (point) {
  14112. var graphic = point.graphic,
  14113. args = point.shapeArgs,
  14114. up = -mathPI / 2;
  14115.  
  14116. if (graphic) {
  14117. // start values
  14118. graphic.attr({
  14119. r: 0,
  14120. start: up,
  14121. end: up
  14122. });
  14123.  
  14124. // animate
  14125. graphic.animate({
  14126. r: args.r,
  14127. start: args.start,
  14128. end: args.end
  14129. }, series.options.animation);
  14130. }
  14131. });
  14132.  
  14133. // delete this function to allow it only once
  14134. series.animate = null;
  14135.  
  14136. },
  14137.  
  14138. /**
  14139. * Extend the basic setData method by running processData and generatePoints immediately,
  14140. * in order to access the points from the legend.
  14141. */
  14142. setData: function (data, redraw) {
  14143. Series.prototype.setData.call(this, data, false);
  14144. this.processData();
  14145. this.generatePoints();
  14146. if (pick(redraw, true)) {
  14147. this.chart.redraw();
  14148. }
  14149. },
  14150.  
  14151. /**
  14152. * Get the center of the pie based on the size and center options relative to the
  14153. * plot area. Borrowed by the polar and gauge series types.
  14154. */
  14155. getCenter: function () {
  14156.  
  14157. var options = this.options,
  14158. chart = this.chart,
  14159. plotWidth = chart.plotWidth,
  14160. plotHeight = chart.plotHeight,
  14161. positions = options.center.concat([options.size, options.innerSize || 0]),
  14162. smallestSize = mathMin(plotWidth, plotHeight),
  14163. isPercent;
  14164.  
  14165. return map(positions, function (length, i) {
  14166.  
  14167. isPercent = /%$/.test(length);
  14168. return isPercent ?
  14169. // i == 0: centerX, relative to width
  14170. // i == 1: centerY, relative to height
  14171. // i == 2: size, relative to smallestSize
  14172. // i == 4: innerSize, relative to smallestSize
  14173. [plotWidth, plotHeight, smallestSize, smallestSize][i] *
  14174. pInt(length) / 100 :
  14175. length;
  14176. });
  14177. },
  14178.  
  14179. /**
  14180. * Do translation for pie slices
  14181. */
  14182. translate: function () {
  14183. this.generatePoints();
  14184.  
  14185. var total = 0,
  14186. series = this,
  14187. cumulative = -0.25, // start at top
  14188. precision = 1000, // issue #172
  14189. options = series.options,
  14190. slicedOffset = options.slicedOffset,
  14191. connectorOffset = slicedOffset + options.borderWidth,
  14192. positions,
  14193. chart = series.chart,
  14194. start,
  14195. end,
  14196. angle,
  14197. points = series.points,
  14198. circ = 2 * mathPI,
  14199. fraction,
  14200. radiusX, // the x component of the radius vector for a given point
  14201. radiusY,
  14202. labelDistance = options.dataLabels.distance;
  14203.  
  14204. // get positions - either an integer or a percentage string must be given
  14205. series.center = positions = series.getCenter();
  14206.  
  14207. // utility for getting the x value from a given y, used for anticollision logic in data labels
  14208. series.getX = function (y, left) {
  14209.  
  14210. angle = math.asin((y - positions[1]) / (positions[2] / 2 + labelDistance));
  14211.  
  14212. return positions[0] +
  14213. (left ? -1 : 1) *
  14214. (mathCos(angle) * (positions[2] / 2 + labelDistance));
  14215. };
  14216.  
  14217. // get the total sum
  14218. each(points, function (point) {
  14219. total += point.y;
  14220. });
  14221.  
  14222. each(points, function (point) {
  14223. // set start and end angle
  14224. fraction = total ? point.y / total : 0;
  14225. start = mathRound(cumulative * circ * precision) / precision;
  14226. cumulative += fraction;
  14227. end = mathRound(cumulative * circ * precision) / precision;
  14228.  
  14229. // set the shape
  14230. point.shapeType = 'arc';
  14231. point.shapeArgs = {
  14232. x: positions[0],
  14233. y: positions[1],
  14234. r: positions[2] / 2,
  14235. innerR: positions[3] / 2,
  14236. start: start,
  14237. end: end
  14238. };
  14239.  
  14240. // center for the sliced out slice
  14241. angle = (end + start) / 2;
  14242. point.slicedTranslation = map([
  14243. mathCos(angle) * slicedOffset + chart.plotLeft,
  14244. mathSin(angle) * slicedOffset + chart.plotTop
  14245. ], mathRound);
  14246.  
  14247. // set the anchor point for tooltips
  14248. radiusX = mathCos(angle) * positions[2] / 2;
  14249. radiusY = mathSin(angle) * positions[2] / 2;
  14250. point.tooltipPos = [
  14251. positions[0] + radiusX * 0.7,
  14252. positions[1] + radiusY * 0.7
  14253. ];
  14254.  
  14255. // set the anchor point for data labels
  14256. point.labelPos = [
  14257. positions[0] + radiusX + mathCos(angle) * labelDistance, // first break of connector
  14258. positions[1] + radiusY + mathSin(angle) * labelDistance, // a/a
  14259. positions[0] + radiusX + mathCos(angle) * connectorOffset, // second break, right outside pie
  14260. positions[1] + radiusY + mathSin(angle) * connectorOffset, // a/a
  14261. positions[0] + radiusX, // landing point for connector
  14262. positions[1] + radiusY, // a/a
  14263. labelDistance < 0 ? // alignment
  14264. 'center' :
  14265. angle < circ / 4 ? 'left' : 'right', // alignment
  14266. angle // center angle
  14267. ];
  14268.  
  14269. // API properties
  14270. point.percentage = fraction * 100;
  14271. point.total = total;
  14272.  
  14273. });
  14274.  
  14275.  
  14276. this.setTooltipPoints();
  14277. },
  14278.  
  14279. /**
  14280. * Render the slices
  14281. */
  14282. render: function () {
  14283. var series = this;
  14284.  
  14285. // cache attributes for shapes
  14286. series.getAttribs();
  14287.  
  14288. this.drawPoints();
  14289.  
  14290. // draw the mouse tracking area
  14291. if (series.options.enableMouseTracking !== false) {
  14292. series.drawTracker();
  14293. }
  14294.  
  14295. this.drawDataLabels();
  14296.  
  14297. if (series.options.animation && series.animate) {
  14298. series.animate();
  14299. }
  14300.  
  14301. // (See #322) series.isDirty = series.isDirtyData = false; // means data is in accordance with what you see
  14302. series.isDirty = false; // means data is in accordance with what you see
  14303. },
  14304.  
  14305. /**
  14306. * Draw the data points
  14307. */
  14308. drawPoints: function () {
  14309. var series = this,
  14310. chart = series.chart,
  14311. renderer = chart.renderer,
  14312. groupTranslation,
  14313. //center,
  14314. graphic,
  14315. group,
  14316. shadow = series.options.shadow,
  14317. shadowGroup,
  14318. shapeArgs;
  14319.  
  14320. // draw the slices
  14321. each(series.points, function (point) {
  14322. graphic = point.graphic;
  14323. shapeArgs = point.shapeArgs;
  14324. group = point.group;
  14325. shadowGroup = point.shadowGroup;
  14326.  
  14327. // put the shadow behind all points
  14328. if (shadow && !shadowGroup) {
  14329. shadowGroup = point.shadowGroup = renderer.g('shadow')
  14330. .attr({ zIndex: 4 })
  14331. .add();
  14332. }
  14333.  
  14334. // create the group the first time
  14335. if (!group) {
  14336. group = point.group = renderer.g('point')
  14337. .attr({ zIndex: 5 })
  14338. .add();
  14339. }
  14340.  
  14341. // if the point is sliced, use special translation, else use plot area traslation
  14342. groupTranslation = point.sliced ? point.slicedTranslation : [chart.plotLeft, chart.plotTop];
  14343. group.translate(groupTranslation[0], groupTranslation[1]);
  14344. if (shadowGroup) {
  14345. shadowGroup.translate(groupTranslation[0], groupTranslation[1]);
  14346. }
  14347.  
  14348. // draw the slice
  14349. if (graphic) {
  14350. graphic.animate(shapeArgs);
  14351. } else {
  14352. point.graphic = graphic = renderer.arc(shapeArgs)
  14353. .setRadialReference(series.center)
  14354. .attr(extend(
  14355. point.pointAttr[NORMAL_STATE],
  14356. { 'stroke-linejoin': 'round' }
  14357. ))
  14358. .add(point.group)
  14359. .shadow(shadow, shadowGroup);
  14360.  
  14361. }
  14362.  
  14363. // detect point specific visibility
  14364. if (point.visible === false) {
  14365. point.setVisible(false);
  14366. }
  14367.  
  14368. });
  14369.  
  14370. },
  14371.  
  14372. /**
  14373. * Override the base drawDataLabels method by pie specific functionality
  14374. */
  14375. drawDataLabels: function () {
  14376. var series = this,
  14377. data = series.data,
  14378. point,
  14379. chart = series.chart,
  14380. options = series.options.dataLabels,
  14381. connectorPadding = pick(options.connectorPadding, 10),
  14382. connectorWidth = pick(options.connectorWidth, 1),
  14383. connector,
  14384. connectorPath,
  14385. softConnector = pick(options.softConnector, true),
  14386. distanceOption = options.distance,
  14387. seriesCenter = series.center,
  14388. radius = seriesCenter[2] / 2,
  14389. centerY = seriesCenter[1],
  14390. outside = distanceOption > 0,
  14391. dataLabel,
  14392. labelPos,
  14393. labelHeight,
  14394. halves = [// divide the points into right and left halves for anti collision
  14395. [], // right
  14396. [] // left
  14397. ],
  14398. x,
  14399. y,
  14400. visibility,
  14401. rankArr,
  14402. sort,
  14403. i = 2,
  14404. j;
  14405.  
  14406. // get out if not enabled
  14407. if (!options.enabled) {
  14408. return;
  14409. }
  14410.  
  14411. // run parent method
  14412. Series.prototype.drawDataLabels.apply(series);
  14413.  
  14414. // arrange points for detection collision
  14415. each(data, function (point) {
  14416. if (point.dataLabel) { // it may have been cancelled in the base method (#407)
  14417. halves[
  14418. point.labelPos[7] < mathPI / 2 ? 0 : 1
  14419. ].push(point);
  14420. }
  14421. });
  14422. halves[1].reverse();
  14423.  
  14424. // define the sorting algorithm
  14425. sort = function (a, b) {
  14426. return b.y - a.y;
  14427. };
  14428.  
  14429. // assume equal label heights
  14430. labelHeight = halves[0][0] && halves[0][0].dataLabel && (halves[0][0].dataLabel.getBBox().height || 21); // 21 is for #968
  14431.  
  14432. /* Loop over the points in each half, starting from the top and bottom
  14433. * of the pie to detect overlapping labels.
  14434. */
  14435. while (i--) {
  14436.  
  14437. var slots = [],
  14438. slotsLength,
  14439. usedSlots = [],
  14440. points = halves[i],
  14441. pos,
  14442. length = points.length,
  14443. slotIndex;
  14444.  
  14445. // Only do anti-collision when we are outside the pie and have connectors (#856)
  14446. if (distanceOption > 0) {
  14447.  
  14448. // build the slots
  14449. for (pos = centerY - radius - distanceOption; pos <= centerY + radius + distanceOption; pos += labelHeight) {
  14450. slots.push(pos);
  14451. // visualize the slot
  14452. /*
  14453. var slotX = series.getX(pos, i) + chart.plotLeft - (i ? 100 : 0),
  14454. slotY = pos + chart.plotTop;
  14455. if (!isNaN(slotX)) {
  14456. chart.renderer.rect(slotX, slotY - 7, 100, labelHeight)
  14457. .attr({
  14458. 'stroke-width': 1,
  14459. stroke: 'silver'
  14460. })
  14461. .add();
  14462. chart.renderer.text('Slot '+ (slots.length - 1), slotX, slotY + 4)
  14463. .attr({
  14464. fill: 'silver'
  14465. }).add();
  14466. }
  14467. // */
  14468. }
  14469. slotsLength = slots.length;
  14470.  
  14471. // if there are more values than available slots, remove lowest values
  14472. if (length > slotsLength) {
  14473. // create an array for sorting and ranking the points within each quarter
  14474. rankArr = [].concat(points);
  14475. rankArr.sort(sort);
  14476. j = length;
  14477. while (j--) {
  14478. rankArr[j].rank = j;
  14479. }
  14480. j = length;
  14481. while (j--) {
  14482. if (points[j].rank >= slotsLength) {
  14483. points.splice(j, 1);
  14484. }
  14485. }
  14486. length = points.length;
  14487. }
  14488.  
  14489. // The label goes to the nearest open slot, but not closer to the edge than
  14490. // the label's index.
  14491. for (j = 0; j < length; j++) {
  14492.  
  14493. point = points[j];
  14494. labelPos = point.labelPos;
  14495.  
  14496. var closest = 9999,
  14497. distance,
  14498. slotI;
  14499.  
  14500. // find the closest slot index
  14501. for (slotI = 0; slotI < slotsLength; slotI++) {
  14502. distance = mathAbs(slots[slotI] - labelPos[1]);
  14503. if (distance < closest) {
  14504. closest = distance;
  14505. slotIndex = slotI;
  14506. }
  14507. }
  14508.  
  14509. // if that slot index is closer to the edges of the slots, move it
  14510. // to the closest appropriate slot
  14511. if (slotIndex < j && slots[j] !== null) { // cluster at the top
  14512. slotIndex = j;
  14513. } else if (slotsLength < length - j + slotIndex && slots[j] !== null) { // cluster at the bottom
  14514. slotIndex = slotsLength - length + j;
  14515. while (slots[slotIndex] === null) { // make sure it is not taken
  14516. slotIndex++;
  14517. }
  14518. } else {
  14519. // Slot is taken, find next free slot below. In the next run, the next slice will find the
  14520. // slot above these, because it is the closest one
  14521. while (slots[slotIndex] === null) { // make sure it is not taken
  14522. slotIndex++;
  14523. }
  14524. }
  14525.  
  14526. usedSlots.push({ i: slotIndex, y: slots[slotIndex] });
  14527. slots[slotIndex] = null; // mark as taken
  14528. }
  14529. // sort them in order to fill in from the top
  14530. usedSlots.sort(sort);
  14531. }
  14532.  
  14533. // now the used slots are sorted, fill them up sequentially
  14534. for (j = 0; j < length; j++) {
  14535.  
  14536. var slot, naturalY;
  14537.  
  14538. point = points[j];
  14539. labelPos = point.labelPos;
  14540. dataLabel = point.dataLabel;
  14541. visibility = point.visible === false ? HIDDEN : VISIBLE;
  14542. naturalY = labelPos[1];
  14543.  
  14544. if (distanceOption > 0) {
  14545. slot = usedSlots.pop();
  14546. slotIndex = slot.i;
  14547.  
  14548. // if the slot next to currrent slot is free, the y value is allowed
  14549. // to fall back to the natural position
  14550. y = slot.y;
  14551. if ((naturalY > y && slots[slotIndex + 1] !== null) ||
  14552. (naturalY < y && slots[slotIndex - 1] !== null)) {
  14553. y = naturalY;
  14554. }
  14555.  
  14556. } else {
  14557. y = naturalY;
  14558. }
  14559.  
  14560. // get the x - use the natural x position for first and last slot, to prevent the top
  14561. // and botton slice connectors from touching each other on either side
  14562. x = options.justify ?
  14563. seriesCenter[0] + (i ? -1 : 1) * (radius + distanceOption) :
  14564. series.getX(slotIndex === 0 || slotIndex === slots.length - 1 ? naturalY : y, i);
  14565.  
  14566. // move or place the data label
  14567. dataLabel
  14568. .attr({
  14569. visibility: visibility,
  14570. align: labelPos[6]
  14571. })[dataLabel.moved ? 'animate' : 'attr']({
  14572. x: x + options.x +
  14573. ({ left: connectorPadding, right: -connectorPadding }[labelPos[6]] || 0),
  14574. y: y + options.y
  14575. });
  14576. dataLabel.moved = true;
  14577.  
  14578. // draw the connector
  14579. if (outside && connectorWidth) {
  14580. connector = point.connector;
  14581.  
  14582. connectorPath = softConnector ? [
  14583. M,
  14584. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14585. 'C',
  14586. x, y, // first break, next to the label
  14587. 2 * labelPos[2] - labelPos[4], 2 * labelPos[3] - labelPos[5],
  14588. labelPos[2], labelPos[3], // second break
  14589. L,
  14590. labelPos[4], labelPos[5] // base
  14591. ] : [
  14592. M,
  14593. x + (labelPos[6] === 'left' ? 5 : -5), y, // end of the string at the label
  14594. L,
  14595. labelPos[2], labelPos[3], // second break
  14596. L,
  14597. labelPos[4], labelPos[5] // base
  14598. ];
  14599.  
  14600. if (connector) {
  14601. connector.animate({ d: connectorPath });
  14602. connector.attr('visibility', visibility);
  14603.  
  14604. } else {
  14605. point.connector = connector = series.chart.renderer.path(connectorPath).attr({
  14606. 'stroke-width': connectorWidth,
  14607. stroke: options.connectorColor || point.color || '#606060',
  14608. visibility: visibility,
  14609. zIndex: 3
  14610. })
  14611. .translate(chart.plotLeft, chart.plotTop)
  14612. .add();
  14613. }
  14614. }
  14615. }
  14616. }
  14617. },
  14618.  
  14619. /**
  14620. * Draw point specific tracker objects. Inherit directly from column series.
  14621. */
  14622. drawTracker: ColumnSeries.prototype.drawTracker,
  14623.  
  14624. /**
  14625. * Use a simple symbol from column prototype
  14626. */
  14627. drawLegendSymbol: AreaSeries.prototype.drawLegendSymbol,
  14628.  
  14629. /**
  14630. * Pies don't have point marker symbols
  14631. */
  14632. getSymbol: function () {}
  14633.  
  14634. };
  14635. PieSeries = extendClass(Series, PieSeries);
  14636. seriesTypes.pie = PieSeries;
  14637.  
  14638.  
  14639. // global variables
  14640. extend(Highcharts, {
  14641.  
  14642. // Constructors
  14643. Axis: Axis,
  14644. CanVGRenderer: CanVGRenderer,
  14645. Chart: Chart,
  14646. Color: Color,
  14647. Legend: Legend,
  14648. Point: Point,
  14649. Tick: Tick,
  14650. Tooltip: Tooltip,
  14651. Renderer: Renderer,
  14652. Series: Series,
  14653. SVGRenderer: SVGRenderer,
  14654. VMLRenderer: VMLRenderer,
  14655.  
  14656. // Various
  14657. dateFormat: dateFormat,
  14658. pathAnim: pathAnim,
  14659. getOptions: getOptions,
  14660. hasBidiBug: hasBidiBug,
  14661. numberFormat: numberFormat,
  14662. seriesTypes: seriesTypes,
  14663. setOptions: setOptions,
  14664. addEvent: addEvent,
  14665. removeEvent: removeEvent,
  14666. createElement: createElement,
  14667. discardElement: discardElement,
  14668. css: css,
  14669. each: each,
  14670. extend: extend,
  14671. map: map,
  14672. merge: merge,
  14673. pick: pick,
  14674. splat: splat,
  14675. extendClass: extendClass,
  14676. pInt: pInt,
  14677. product: 'Highcharts',
  14678. version: '2.2.5'
  14679. });
  14680. }());
Add Comment
Please, Sign In to add comment