UNPKG

anychart-react

Version:

React component for AnyChart JavaScript charting library

1 lines 4.08 kB
"use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _toConsumableArray(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),_react=require("react"),_react2=_interopRequireDefault(_react);require("anychart");var AnyChart=function(e){function t(e){_classCallCheck(this,t);var n=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return n.instance=null,n.isStage=!1,n.disposeInstance=!1,n.multipleEntities=["xAxis","yAxis","lineMarker","rangeMarker","textMarker","grid","minorGrid"],n.containerId=e.id||"ac-chart-container",n}return _inherits(t,e),_createClass(t,[{key:"removeInstance",value:function(){this.instance&&(this.disposeInstance?this.instance.dispose():this.isStage?this.instance.remove():this.instance.container().getStage().remove())}},{key:"isArray",value:function(e){return"object"==(void 0===e?"undefined":_typeof(e))&&e instanceof Array}},{key:"applyProps",value:function(e){var t=!0,n=!1,r=void 0;try{for(var i,a=Object.keys(e)[Symbol.iterator]();!(t=(i=a.next()).done);t=!0){var s=i.value,o=e[s];if("width"!=s&&"height"!=s||this.isStage||this.instance.container().getStage()[s](o),this.instance[s])if(~this.multipleEntities.indexOf(s)){var c;this.isArray(o)||(o=[o]),(c=this.instance)[s].apply(c,_toConsumableArray(o))}else this.instance[s](o)}}catch(e){n=!0,r=e}finally{try{!t&&a.return&&a.return()}finally{if(n)throw r}}}},{key:"createInstance",value:function(e){e.instance?(this.removeInstance(),this.instance=e.instance,this.isStage="function"!=typeof this.instance.draw,delete e.instance,this.disposeInstance=!1):e.type&&(this.removeInstance(),this.disposeInstance=!0,this.instance=anychart[e.type](e.data),this.isStage=!1,delete e.type,delete e.data),this.instance&&this.instance.container(this.containerId),delete e.id}},{key:"drawInstance",value:function(e){if(this.instance)if(this.isStage){this.instance.suspend();var t=e.charts;delete e.charts,this.applyProps(e);var n=!0,r=!1,i=void 0;try{for(var a,s=t[Symbol.iterator]();!(n=(a=s.next()).done);n=!0){var o=a.value;o.container(this.instance).draw()}}catch(e){r=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(r)throw i}}this.instance.resume()}else this.applyProps(e),this.instance.draw()}},{key:"createAndDraw",value:function(e){var t=Object.assign(e,this.props);this.createInstance(t),this.drawInstance(t)}},{key:"render",value:function(){return _react2.default.createElement("div",{id:this.containerId})}},{key:"componentDidMount",value:function(){this.createAndDraw({})}},{key:"componentWillUpdate",value:function(e,t){this.containerId=e.id||this.containerId}},{key:"componentDidUpdate",value:function(e,t){var n=Object.assign({},e);delete n.type,delete n.instance,this.createAndDraw(n)}},{key:"componentWillUnmount",value:function(){this.removeInstance()}}]),t}(_react2.default.Component);exports.default=AnyChart;