@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
81 lines • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// (C) 2020 GoodData Corporation
var get = require("lodash/get");
var geoChart_1 = require("../../constants/geoChart");
function getViewportOptions(data, config) {
var center = get(config, "center");
var zoom = get(config, "zoom", geoChart_1.DEFAULT_ZOOM);
var area = get(config, "viewport", {}).area;
// use `center` config if it exists
if (!center) {
if (geoChart_1.VIEWPORTS[area]) {
return {
bounds: geoChart_1.VIEWPORTS[area],
};
}
else {
var lngLatBounds = getLngLatBounds(data);
if (lngLatBounds) {
return {
bounds: [lngLatBounds.northEast, lngLatBounds.southWest],
};
}
return {
center: geoChart_1.DEFAULT_CENTER,
zoom: zoom,
};
}
}
return {
center: center,
zoom: zoom,
};
}
exports.getViewportOptions = getViewportOptions;
/*
* @method getLngLatBounds: IGeoLngLatBounds
* Represents a rectangular geographical area on a map.
*
* @example
*
* ```js
* const corner1 = [40.712, -74.227],
* const corner2 = [40.774, -74.125],
* const bounds = getLngLatBounds([corner1, corner2]);
*
* bounds && map.fitBounds([bounds.northEast, bounds.southWest], { padding: 60 });
* ```
*/
function getLngLatBounds(lnglats) {
if (!lnglats || !lnglats.length) {
return;
}
return lnglats.reduce(extendLngLatBounds, undefined) || geoChart_1.DEFAULT_WORLD_BOUNDS;
}
exports.getLngLatBounds = getLngLatBounds;
// @method extendLngLatBounds: IGeoLngLatBounds
// Extend the bounds to contain the given point
function extendLngLatBounds(bounds, lnglat) {
if (!lnglat) {
return bounds;
}
if (!bounds) {
return {
northEast: lnglat,
southWest: lnglat,
};
}
var northEast = bounds.northEast, southWest = bounds.southWest;
return {
northEast: {
lat: Math.max(lnglat.lat, northEast.lat),
lng: Math.max(lnglat.lng, northEast.lng),
},
southWest: {
lat: Math.min(lnglat.lat, southWest.lat),
lng: Math.min(lnglat.lng, southWest.lng),
},
};
}
//# sourceMappingURL=viewport.js.map