polestar-ui-kit
Version:
## Install
219 lines (155 loc) • 9.09 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
require("core-js/modules/es.array.for-each");
require("core-js/modules/es.array.map");
require("core-js/modules/es.date.to-string");
require("core-js/modules/es.object.get-prototype-of");
require("core-js/modules/es.object.set-prototype-of");
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.reflect.construct");
require("core-js/modules/es.regexp.to-string");
require("core-js/modules/web.dom-collections.for-each");
require("core-js/modules/web.timers");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _highcharts = _interopRequireDefault(require("highcharts"));
var _highchartsMore = _interopRequireDefault(require("highcharts/highcharts-more"));
var _boost = _interopRequireDefault(require("highcharts/modules/boost"));
var _heatmap = _interopRequireDefault(require("highcharts/modules/heatmap"));
var _defaultOptions = require("./helpers/defaultOptions");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var ReactHighcharts = function (_PureComponent) {
_inherits(ReactHighcharts, _PureComponent);
var _super = _createSuper(ReactHighcharts);
function ReactHighcharts(props) {
var _this;
_classCallCheck(this, ReactHighcharts);
_this = _super.call(this, props);
_this.getChart = function () {
return _this.chart;
};
_this.destroyChart = function () {
_this.destroyTimer();
_this.getChart().destroy();
};
_this.destroyTimer = function () {
clearInterval(_this.timer);
_this.timer = null;
};
_this.startLiveTimer = function (interval, period) {
_this.timer = setInterval(function () {
var currentTime = new Date().valueOf();
_this.chart.xAxis[0].setExtremes(currentTime - period, currentTime, true, false);
_this.chart.redraw();
}, interval);
};
_this.redrawChart = function () {
_this.chart.redraw();
};
_this.addPoint = function (key, data) {
var seriesList = _this.chart.series;
seriesList.forEach(function (series) {
if (series.options.id === key) {
series.addPoint(data, false, false);
}
});
};
_this.updatePoint = function (key, data) {
var seriesData = _this.chart.series[0].data;
var newData = seriesData.map(function (d) {
if (d.id === key) {
d.y = data;
}
return d;
});
_this.chart.series[0].setData(newData);
};
_this.renderChart = function (propConfig, callback) {
var config = _extends({}, _defaultOptions.defaultOptions, propConfig);
_this.destroyTimer();
if (config.live && config.live.enabled) {
_this.startLiveTimer(config.live.interval, config.live.period);
}
_this.chart = _highcharts["default"].chart(_this.chartEl, config, callback);
};
var _config = _this.props.config;
if (_config && _config.more) {
(0, _highchartsMore["default"])(_highcharts["default"]);
}
if (_config && _config.boost && _config.boost.enabled) {
(0, _boost["default"])(_highcharts["default"]);
}
if (_config && _config.chart && _config.chart.type === 'heatmap') {
(0, _heatmap["default"])(_highcharts["default"]);
}
_highcharts["default"].setOptions({
global: {
useUTC: false
}
});
return _this;
}
_createClass(ReactHighcharts, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props = this.props,
config = _this$props.config,
callback = _this$props.callback;
this.renderChart(config, callback);
}
}, {
key: "componentWillUpdate",
value: function componentWillUpdate(nextProps) {
var config = nextProps.config,
callback = nextProps.callback;
this.renderChart(config, callback);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.destroyChart();
}
}, {
key: "render",
value: function render() {
var _this2 = this;
return _react["default"].createElement("div", _extends({}, this.props.domConfig, {
ref: function ref(el) {
_this2.chartEl = el;
}
}));
}
}]);
return ReactHighcharts;
}(_react.PureComponent);
ReactHighcharts.propTypes = {
config: _propTypes["default"].object,
domConfig: _propTypes["default"].object,
callback: _propTypes["default"].func
};
ReactHighcharts.defaultProps = {
config: {},
domConfig: {},
callback: function callback() {
console.log('Highchart init.');
}
};
var _default = ReactHighcharts;
exports["default"] = _default;