Responsive.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Chart from './Chart/Chart.js';
  12. import U from './Utilities.js';
  13. var find = U.find, isArray = U.isArray, isObject = U.isObject, merge = U.merge, objectEach = U.objectEach, pick = U.pick, splat = U.splat, uniqueKey = U.uniqueKey;
  14. /**
  15. * A callback function to gain complete control on when the responsive rule
  16. * applies.
  17. *
  18. * @callback Highcharts.ResponsiveCallbackFunction
  19. *
  20. * @param {Highcharts.Chart} this
  21. * Chart context.
  22. *
  23. * @return {boolean}
  24. * Return `true` if it applies.
  25. */
  26. /**
  27. * Allows setting a set of rules to apply for different screen or chart
  28. * sizes. Each rule specifies additional chart options.
  29. *
  30. * @sample {highstock} stock/demo/responsive/
  31. * Stock chart
  32. * @sample highcharts/responsive/axis/
  33. * Axis
  34. * @sample highcharts/responsive/legend/
  35. * Legend
  36. * @sample highcharts/responsive/classname/
  37. * Class name
  38. *
  39. * @since 5.0.0
  40. * @apioption responsive
  41. */
  42. /**
  43. * A set of rules for responsive settings. The rules are executed from
  44. * the top down.
  45. *
  46. * @sample {highcharts} highcharts/responsive/axis/
  47. * Axis changes
  48. * @sample {highstock} highcharts/responsive/axis/
  49. * Axis changes
  50. * @sample {highmaps} highcharts/responsive/axis/
  51. * Axis changes
  52. *
  53. * @type {Array<*>}
  54. * @since 5.0.0
  55. * @apioption responsive.rules
  56. */
  57. /**
  58. * A full set of chart options to apply as overrides to the general
  59. * chart options. The chart options are applied when the given rule
  60. * is active.
  61. *
  62. * A special case is configuration objects that take arrays, for example
  63. * [xAxis](#xAxis), [yAxis](#yAxis) or [series](#series). For these
  64. * collections, an `id` option is used to map the new option set to
  65. * an existing object. If an existing object of the same id is not found,
  66. * the item of the same indexupdated. So for example, setting `chartOptions`
  67. * with two series items without an `id`, will cause the existing chart's
  68. * two series to be updated with respective options.
  69. *
  70. * @sample {highstock} stock/demo/responsive/
  71. * Stock chart
  72. * @sample highcharts/responsive/axis/
  73. * Axis
  74. * @sample highcharts/responsive/legend/
  75. * Legend
  76. * @sample highcharts/responsive/classname/
  77. * Class name
  78. *
  79. * @type {Highcharts.Options}
  80. * @since 5.0.0
  81. * @apioption responsive.rules.chartOptions
  82. */
  83. /**
  84. * Under which conditions the rule applies.
  85. *
  86. * @since 5.0.0
  87. * @apioption responsive.rules.condition
  88. */
  89. /**
  90. * A callback function to gain complete control on when the responsive
  91. * rule applies. Return `true` if it applies. This opens for checking
  92. * against other metrics than the chart size, for example the document
  93. * size or other elements.
  94. *
  95. * @type {Highcharts.ResponsiveCallbackFunction}
  96. * @since 5.0.0
  97. * @context Highcharts.Chart
  98. * @apioption responsive.rules.condition.callback
  99. */
  100. /**
  101. * The responsive rule applies if the chart height is less than this.
  102. *
  103. * @type {number}
  104. * @since 5.0.0
  105. * @apioption responsive.rules.condition.maxHeight
  106. */
  107. /**
  108. * The responsive rule applies if the chart width is less than this.
  109. *
  110. * @sample highcharts/responsive/axis/
  111. * Max width is 500
  112. *
  113. * @type {number}
  114. * @since 5.0.0
  115. * @apioption responsive.rules.condition.maxWidth
  116. */
  117. /**
  118. * The responsive rule applies if the chart height is greater than this.
  119. *
  120. * @type {number}
  121. * @default 0
  122. * @since 5.0.0
  123. * @apioption responsive.rules.condition.minHeight
  124. */
  125. /**
  126. * The responsive rule applies if the chart width is greater than this.
  127. *
  128. * @type {number}
  129. * @default 0
  130. * @since 5.0.0
  131. * @apioption responsive.rules.condition.minWidth
  132. */
  133. /* eslint-disable no-invalid-this, valid-jsdoc */
  134. /**
  135. * Update the chart based on the current chart/document size and options for
  136. * responsiveness.
  137. *
  138. * @private
  139. * @function Highcharts.Chart#setResponsive
  140. * @param {boolean} [redraw=true]
  141. * @param {boolean} [reset=false]
  142. * Reset by un-applying all rules. Chart.update resets all rules before applying
  143. * updated options.
  144. */
  145. Chart.prototype.setResponsive = function (redraw, reset) {
  146. var options = this.options.responsive, ruleIds = [], currentResponsive = this.currentResponsive, currentRuleIds, undoOptions;
  147. if (!reset && options && options.rules) {
  148. options.rules.forEach(function (rule) {
  149. if (typeof rule._id === 'undefined') {
  150. rule._id = uniqueKey();
  151. }
  152. this.matchResponsiveRule(rule, ruleIds /* , redraw */);
  153. }, this);
  154. }
  155. // Merge matching rules
  156. var mergedOptions = merge.apply(0, ruleIds.map(function (ruleId) {
  157. return find(options.rules, function (rule) {
  158. return rule._id === ruleId;
  159. }).chartOptions;
  160. }));
  161. mergedOptions.isResponsiveOptions = true;
  162. // Stringified key for the rules that currently apply.
  163. ruleIds = (ruleIds.toString() || void 0);
  164. currentRuleIds = currentResponsive && currentResponsive.ruleIds;
  165. // Changes in what rules apply
  166. if (ruleIds !== currentRuleIds) {
  167. // Undo previous rules. Before we apply a new set of rules, we need to
  168. // roll back completely to base options (#6291).
  169. if (currentResponsive) {
  170. this.update(currentResponsive.undoOptions, redraw, true);
  171. }
  172. if (ruleIds) {
  173. // Get undo-options for matching rules
  174. undoOptions = this.currentOptions(mergedOptions);
  175. undoOptions.isResponsiveOptions = true;
  176. this.currentResponsive = {
  177. ruleIds: ruleIds,
  178. mergedOptions: mergedOptions,
  179. undoOptions: undoOptions
  180. };
  181. this.update(mergedOptions, redraw, true);
  182. }
  183. else {
  184. this.currentResponsive = void 0;
  185. }
  186. }
  187. };
  188. /**
  189. * Handle a single responsiveness rule.
  190. *
  191. * @private
  192. * @function Highcharts.Chart#matchResponsiveRule
  193. * @param {Highcharts.ResponsiveRulesOptions} rule
  194. * @param {Array<string>} matches
  195. */
  196. Chart.prototype.matchResponsiveRule = function (rule, matches) {
  197. var condition = rule.condition, fn = condition.callback || function () {
  198. return (this.chartWidth <= pick(condition.maxWidth, Number.MAX_VALUE) &&
  199. this.chartHeight <=
  200. pick(condition.maxHeight, Number.MAX_VALUE) &&
  201. this.chartWidth >= pick(condition.minWidth, 0) &&
  202. this.chartHeight >= pick(condition.minHeight, 0));
  203. };
  204. if (fn.call(this)) {
  205. matches.push(rule._id);
  206. }
  207. };
  208. /**
  209. * Get the current values for a given set of options. Used before we update
  210. * the chart with a new responsiveness rule.
  211. *
  212. * @todo Restore axis options (by id?). The matching of items in collections
  213. * bears resemblance to the oneToOne matching in Chart.update. Probably we can
  214. * refactor out that matching and reuse it in both functions.
  215. *
  216. * @private
  217. * @function Highcharts.Chart#currentOptions
  218. * @param {Highcharts.Options} options
  219. * @return {Highcharts.Options}
  220. */
  221. Chart.prototype.currentOptions = function (options) {
  222. var chart = this, ret = {};
  223. /**
  224. * Recurse over a set of options and its current values,
  225. * and store the current values in the ret object.
  226. */
  227. function getCurrent(options, curr, ret, depth) {
  228. var i;
  229. objectEach(options, function (val, key) {
  230. if (!depth &&
  231. chart.collectionsWithUpdate.indexOf(key) > -1) {
  232. val = splat(val);
  233. ret[key] = [];
  234. // Iterate over collections like series, xAxis or yAxis and map
  235. // the items by index.
  236. for (i = 0; i < Math.max(val.length, curr[key].length); i++) {
  237. // Item exists in current data (#6347)
  238. if (curr[key][i]) {
  239. // If the item is missing from the new data, we need to
  240. // save the whole config structure. Like when
  241. // responsively updating from a dual axis layout to a
  242. // single axis and back (#13544).
  243. if (val[i] === void 0) {
  244. ret[key][i] = curr[key][i];
  245. // Otherwise, proceed
  246. }
  247. else {
  248. ret[key][i] = {};
  249. getCurrent(val[i], curr[key][i], ret[key][i], depth + 1);
  250. }
  251. }
  252. }
  253. }
  254. else if (isObject(val)) {
  255. ret[key] = isArray(val) ? [] : {};
  256. getCurrent(val, curr[key] || {}, ret[key], depth + 1);
  257. }
  258. else if (typeof curr[key] === 'undefined') { // #10286
  259. ret[key] = null;
  260. }
  261. else {
  262. ret[key] = curr[key];
  263. }
  264. });
  265. }
  266. getCurrent(options, this.options, ret, 0);
  267. return ret;
  268. };