123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /**
- * (c) 2010-2019 Torstein Honsi
- *
- * License: www.highcharts.com/license
- */
- /**
- * A callback function to gain complete control on when the responsive rule
- * applies.
- *
- * @callback Highcharts.ResponsiveCallbackFunction
- *
- * @return {boolean}
- * Return `true` if it applies.
- */
- 'use strict';
- import H from './Globals.js';
- import './Chart.js';
- import './Utilities.js';
- var Chart = H.Chart,
- isArray = H.isArray,
- isObject = H.isObject,
- pick = H.pick,
- splat = H.splat;
- /**
- * Allows setting a set of rules to apply for different screen or chart
- * sizes. Each rule specifies additional chart options.
- *
- * @sample {highstock} stock/demo/responsive/
- * Stock chart
- * @sample highcharts/responsive/axis/
- * Axis
- * @sample highcharts/responsive/legend/
- * Legend
- * @sample highcharts/responsive/classname/
- * Class name
- *
- * @since 5.0.0
- * @apioption responsive
- */
- /**
- * A set of rules for responsive settings. The rules are executed from
- * the top down.
- *
- * @sample {highcharts} highcharts/responsive/axis/
- * Axis changes
- * @sample {highstock} highcharts/responsive/axis/
- * Axis changes
- * @sample {highmaps} highcharts/responsive/axis/
- * Axis changes
- *
- * @type {Array<*>}
- * @since 5.0.0
- * @apioption responsive.rules
- */
- /**
- * A full set of chart options to apply as overrides to the general
- * chart options. The chart options are applied when the given rule
- * is active.
- *
- * A special case is configuration objects that take arrays, for example
- * [xAxis](#xAxis), [yAxis](#yAxis) or [series](#series). For these
- * collections, an `id` option is used to map the new option set to
- * an existing object. If an existing object of the same id is not found,
- * the item of the same indexupdated. So for example, setting `chartOptions`
- * with two series items without an `id`, will cause the existing chart's
- * two series to be updated with respective options.
- *
- * @sample {highstock} stock/demo/responsive/
- * Stock chart
- * @sample highcharts/responsive/axis/
- * Axis
- * @sample highcharts/responsive/legend/
- * Legend
- * @sample highcharts/responsive/classname/
- * Class name
- *
- * @type {Highcharts.Options}
- * @since 5.0.0
- * @apioption responsive.rules.chartOptions
- */
- /**
- * Under which conditions the rule applies.
- *
- * @since 5.0.0
- * @apioption responsive.rules.condition
- */
- /**
- * A callback function to gain complete control on when the responsive
- * rule applies. Return `true` if it applies. This opens for checking
- * against other metrics than the chart size, or example the document
- * size or other elements.
- *
- * @type {Highcharts.ResponsiveCallbackFunction}
- * @since 5.0.0
- * @context Highcharts.Chart
- * @apioption responsive.rules.condition.callback
- */
- /**
- * The responsive rule applies if the chart height is less than this.
- *
- * @type {number}
- * @since 5.0.0
- * @apioption responsive.rules.condition.maxHeight
- */
- /**
- * The responsive rule applies if the chart width is less than this.
- *
- * @sample highcharts/responsive/axis/
- * Max width is 500
- *
- * @type {number}
- * @since 5.0.0
- * @apioption responsive.rules.condition.maxWidth
- */
- /**
- * The responsive rule applies if the chart height is greater than this.
- *
- * @type {number}
- * @default 0
- * @since 5.0.0
- * @apioption responsive.rules.condition.minHeight
- */
- /**
- * The responsive rule applies if the chart width is greater than this.
- *
- * @type {number}
- * @default 0
- * @since 5.0.0
- * @apioption responsive.rules.condition.minWidth
- */
- /**
- * Update the chart based on the current chart/document size and options for
- * responsiveness.
- *
- * @private
- * @function Highcharts.Chart#setResponsive
- *
- * @param {boolean} [redraw=true]
- * @param {Array} [reset=false]
- * Reset by un-applying all rules. Chart.update resets all rules before
- * applying updated options.
- */
- Chart.prototype.setResponsive = function (redraw, reset) {
- var options = this.options.responsive,
- ruleIds = [],
- currentResponsive = this.currentResponsive,
- currentRuleIds,
- undoOptions;
- if (!reset && options && options.rules) {
- options.rules.forEach(function (rule) {
- if (rule._id === undefined) {
- rule._id = H.uniqueKey();
- }
- this.matchResponsiveRule(rule, ruleIds, redraw);
- }, this);
- }
- // Merge matching rules
- var mergedOptions = H.merge.apply(0, ruleIds.map(function (ruleId) {
- return H.find(options.rules, function (rule) {
- return rule._id === ruleId;
- }).chartOptions;
- }));
- mergedOptions.isResponsiveOptions = true;
- // Stringified key for the rules that currently apply.
- ruleIds = ruleIds.toString() || undefined;
- currentRuleIds = currentResponsive && currentResponsive.ruleIds;
- // Changes in what rules apply
- if (ruleIds !== currentRuleIds) {
- // Undo previous rules. Before we apply a new set of rules, we need to
- // roll back completely to base options (#6291).
- if (currentResponsive) {
- this.update(currentResponsive.undoOptions, redraw);
- }
- if (ruleIds) {
- // Get undo-options for matching rules
- undoOptions = this.currentOptions(mergedOptions);
- undoOptions.isResponsiveOptions = true;
- this.currentResponsive = {
- ruleIds: ruleIds,
- mergedOptions: mergedOptions,
- undoOptions: undoOptions
- };
- this.update(mergedOptions, redraw);
- } else {
- this.currentResponsive = undefined;
- }
- }
- };
- /**
- * Handle a single responsiveness rule.
- *
- * @private
- * @function Highcharts.Chart#matchResponsiveRule
- *
- * @param {Highcharts.ResponsiveRulesConditionOptions} rule
- *
- * @param {Array<number>} matches
- */
- Chart.prototype.matchResponsiveRule = function (rule, matches) {
- var condition = rule.condition,
- fn = condition.callback || function () {
- return (
- this.chartWidth <= pick(condition.maxWidth, Number.MAX_VALUE) &&
- this.chartHeight <=
- pick(condition.maxHeight, Number.MAX_VALUE) &&
- this.chartWidth >= pick(condition.minWidth, 0) &&
- this.chartHeight >= pick(condition.minHeight, 0)
- );
- };
- if (fn.call(this)) {
- matches.push(rule._id);
- }
- };
- /**
- * Get the current values for a given set of options. Used before we update
- * the chart with a new responsiveness rule.
- * TODO: Restore axis options (by id?)
- *
- * @private
- * @function Highcharts.Chart#currentOptions
- *
- * @param {Highcharts.Options} options
- *
- * @return {Highcharts.Options}
- */
- Chart.prototype.currentOptions = function (options) {
- var ret = {};
- /**
- * Recurse over a set of options and its current values,
- * and store the current values in the ret object.
- */
- function getCurrent(options, curr, ret, depth) {
- var i;
- H.objectEach(options, function (val, key) {
- if (!depth && ['series', 'xAxis', 'yAxis'].indexOf(key) > -1) {
- val = splat(val);
- ret[key] = [];
- // Iterate over collections like series, xAxis or yAxis and map
- // the items by index.
- for (i = 0; i < val.length; i++) {
- if (curr[key][i]) { // Item exists in current data (#6347)
- ret[key][i] = {};
- getCurrent(
- val[i],
- curr[key][i],
- ret[key][i],
- depth + 1
- );
- }
- }
- } else if (isObject(val)) {
- ret[key] = isArray(val) ? [] : {};
- getCurrent(val, curr[key] || {}, ret[key], depth + 1);
- } else {
- ret[key] = curr[key] || null;
- }
- });
- }
- getCurrent(options, this.options, ret, 0);
- return ret;
- };
|