app模板、应用模板、组件模板、widget模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
13 KiB

4 years ago
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _echarts = require('echarts/lib/echarts');
  4. var _echarts2 = _interopRequireDefault(_echarts);
  5. require('echarts/lib/component/tooltip');
  6. require('echarts/lib/component/legend');
  7. var _numerify = require('numerify');
  8. var _numerify2 = _interopRequireDefault(_numerify);
  9. var _utilsLite = require('utils-lite');
  10. var _loading = require('./components/loading');
  11. var _loading2 = _interopRequireDefault(_loading);
  12. var _dataEmpty = require('./components/data-empty');
  13. var _dataEmpty2 = _interopRequireDefault(_dataEmpty);
  14. var _constants = require('./constants');
  15. var _extend = require('./modules/extend');
  16. var _extend2 = _interopRequireDefault(_extend);
  17. var _mark = require('./modules/mark');
  18. var _mark2 = _interopRequireDefault(_mark);
  19. var _animation = require('./modules/animation');
  20. var _animation2 = _interopRequireDefault(_animation);
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. exports.default = {
  23. render: function render(h) {
  24. return h('div', {
  25. class: [(0, _utilsLite.camelToKebab)(this.$options.name || this.$options._componentTag)],
  26. style: this.canvasStyle
  27. }, [h('div', {
  28. style: this.canvasStyle,
  29. class: { 'v-charts-mask-status': this.dataEmpty || this.loading },
  30. ref: 'canvas'
  31. }), h(_dataEmpty2.default, {
  32. style: { display: this.dataEmpty ? '' : 'none' }
  33. }), h(_loading2.default, {
  34. style: { display: this.loading ? '' : 'none' }
  35. }), this.$slots.default]);
  36. },
  37. props: {
  38. data: { type: [Object, Array], default: function _default() {
  39. return {};
  40. }
  41. },
  42. settings: { type: Object, default: function _default() {
  43. return {};
  44. }
  45. },
  46. width: { type: String, default: 'auto' },
  47. height: { type: String, default: '400px' },
  48. beforeConfig: { type: Function },
  49. afterConfig: { type: Function },
  50. afterSetOption: { type: Function },
  51. afterSetOptionOnce: { type: Function },
  52. events: { type: Object },
  53. grid: { type: [Object, Array] },
  54. colors: { type: Array },
  55. tooltipVisible: { type: Boolean, default: true },
  56. legendVisible: { type: Boolean, default: true },
  57. legendPosition: { type: String },
  58. markLine: { type: Object },
  59. markArea: { type: Object },
  60. markPoint: { type: Object },
  61. visualMap: { type: [Object, Array] },
  62. dataZoom: { type: [Object, Array] },
  63. toolbox: { type: [Object, Array] },
  64. initOptions: { type: Object, default: function _default() {
  65. return {};
  66. }
  67. },
  68. title: [Object, Array],
  69. legend: [Object, Array],
  70. xAxis: [Object, Array],
  71. yAxis: [Object, Array],
  72. radar: Object,
  73. tooltip: Object,
  74. axisPointer: [Object, Array],
  75. brush: [Object, Array],
  76. geo: [Object, Array],
  77. timeline: [Object, Array],
  78. graphic: [Object, Array],
  79. series: [Object, Array],
  80. backgroundColor: [Object, String],
  81. textStyle: [Object, Array],
  82. animation: Object,
  83. theme: Object,
  84. themeName: String,
  85. loading: Boolean,
  86. dataEmpty: Boolean,
  87. extend: Object,
  88. judgeWidth: { type: Boolean, default: false },
  89. widthChangeDelay: { type: Number, default: 300 },
  90. tooltipFormatter: { type: Function },
  91. resizeable: { type: Boolean, default: true },
  92. resizeDelay: { type: Number, default: 200 },
  93. changeDelay: { type: Number, default: 0 },
  94. setOptionOpts: { type: [Boolean, Object], default: true },
  95. cancelResizeCheck: Boolean,
  96. notSetUnchange: Array,
  97. log: Boolean
  98. },
  99. watch: {
  100. data: {
  101. deep: true,
  102. handler: function handler(v) {
  103. if (v) {
  104. this.changeHandler();
  105. }
  106. }
  107. },
  108. settings: {
  109. deep: true,
  110. handler: function handler(v) {
  111. if (v.type && this.chartLib) this.chartHandler = this.chartLib[v.type];
  112. this.changeHandler();
  113. }
  114. },
  115. width: 'nextTickResize',
  116. height: 'nextTickResize',
  117. events: {
  118. deep: true,
  119. handler: 'createEventProxy'
  120. },
  121. theme: {
  122. deep: true,
  123. handler: 'themeChange'
  124. },
  125. themeName: 'themeChange',
  126. resizeable: 'resizeableHandler'
  127. },
  128. computed: {
  129. canvasStyle: function canvasStyle() {
  130. return {
  131. width: this.width,
  132. height: this.height,
  133. position: 'relative'
  134. };
  135. },
  136. chartColor: function chartColor() {
  137. return this.colors || this.theme && this.theme.color || _constants.DEFAULT_COLORS;
  138. }
  139. },
  140. methods: {
  141. dataHandler: function dataHandler() {
  142. if (!this.chartHandler) return;
  143. var data = this.data;
  144. var _data = data,
  145. _data$columns = _data.columns,
  146. columns = _data$columns === undefined ? [] : _data$columns,
  147. _data$rows = _data.rows,
  148. rows = _data$rows === undefined ? [] : _data$rows;
  149. var extra = {
  150. tooltipVisible: this.tooltipVisible,
  151. legendVisible: this.legendVisible,
  152. echarts: this.echarts,
  153. color: this.chartColor,
  154. tooltipFormatter: this.tooltipFormatter,
  155. _once: this._once
  156. };
  157. if (this.beforeConfig) data = this.beforeConfig(data);
  158. var options = this.chartHandler(columns, rows, this.settings, extra);
  159. if (options) {
  160. if (typeof options.then === 'function') {
  161. options.then(this.optionsHandler);
  162. } else {
  163. this.optionsHandler(options);
  164. }
  165. }
  166. },
  167. nextTickResize: function nextTickResize() {
  168. this.$nextTick(this.resize);
  169. },
  170. resize: function resize() {
  171. if (!this.cancelResizeCheck) {
  172. if (this.$el && this.$el.clientWidth && this.$el.clientHeight) {
  173. this.echartsResize();
  174. }
  175. } else {
  176. this.echartsResize();
  177. }
  178. },
  179. echartsResize: function echartsResize() {
  180. this.echarts && this.echarts.resize();
  181. },
  182. optionsHandler: function optionsHandler(options) {
  183. var _this = this;
  184. // legend
  185. if (this.legendPosition && options.legend) {
  186. options.legend[this.legendPosition] = 10;
  187. if (~['left', 'right'].indexOf(this.legendPosition)) {
  188. options.legend.top = 'middle';
  189. options.legend.orient = 'vertical';
  190. }
  191. }
  192. // color
  193. options.color = this.chartColor;
  194. // echarts self settings
  195. _constants.ECHARTS_SETTINGS.forEach(function (setting) {
  196. if (_this[setting]) options[setting] = _this[setting];
  197. });
  198. // animation
  199. if (this.animation) (0, _animation2.default)(options, this.animation);
  200. // marks
  201. if (this.markArea || this.markLine || this.markPoint) {
  202. var marks = {
  203. markArea: this.markArea,
  204. markLine: this.markLine,
  205. markPoint: this.markPoint
  206. };
  207. var series = options.series;
  208. if ((0, _utilsLite.isArray)(series)) {
  209. series.forEach(function (item) {
  210. (0, _mark2.default)(item, marks);
  211. });
  212. } else if ((0, _utilsLite.isObject)(series)) {
  213. (0, _mark2.default)(series, marks);
  214. }
  215. }
  216. // change inited echarts settings
  217. if (this.extend) (0, _extend2.default)(options, this.extend);
  218. if (this.afterConfig) options = this.afterConfig(options);
  219. var setOptionOpts = this.setOptionOpts;
  220. // map chart not merge
  221. if ((this.settings.bmap || this.settings.amap) && !(0, _utilsLite.isObject)(setOptionOpts)) {
  222. setOptionOpts = false;
  223. }
  224. // exclude unchange options
  225. if (this.notSetUnchange && this.notSetUnchange.length) {
  226. this.notSetUnchange.forEach(function (item) {
  227. var value = options[item];
  228. if (value) {
  229. if ((0, _utilsLite.isEqual)(value, _this._store[item])) {
  230. options[item] = undefined;
  231. } else {
  232. _this._store[item] = (0, _utilsLite.cloneDeep)(value);
  233. }
  234. }
  235. });
  236. if ((0, _utilsLite.isObject)(setOptionOpts)) {
  237. setOptionOpts.notMerge = false;
  238. } else {
  239. setOptionOpts = false;
  240. }
  241. }
  242. if (this._isDestroyed) return;
  243. if (this.log) console.log(options);
  244. console.log('options------->', options);
  245. console.log('setOptions----->', setOptionOpts);
  246. // 设置一些基本参数
  247. options.legend.textStyle = {
  248. color: '#FFF'
  249. };
  250. this.echarts.setOption(options, setOptionOpts);
  251. this.$emit('ready', this.echarts, options, _echarts2.default);
  252. if (!this._once['ready-once']) {
  253. this._once['ready-once'] = true;
  254. this.$emit('ready-once', this.echarts, options, _echarts2.default);
  255. }
  256. if (this.judgeWidth) this.judgeWidthHandler(options);
  257. if (this.afterSetOption) this.afterSetOption(this.echarts, options, _echarts2.default);
  258. if (this.afterSetOptionOnce && !this._once['afterSetOptionOnce']) {
  259. this._once['afterSetOptionOnce'] = true;
  260. this.afterSetOptionOnce(this.echarts, options, _echarts2.default);
  261. }
  262. },
  263. judgeWidthHandler: function judgeWidthHandler(options) {
  264. var _this2 = this;
  265. var widthChangeDelay = this.widthChangeDelay,
  266. resize = this.resize;
  267. if (this.$el.clientWidth || this.$el.clientHeight) {
  268. resize();
  269. } else {
  270. this.$nextTick(function (_) {
  271. if (_this2.$el.clientWidth || _this2.$el.clientHeight) {
  272. resize();
  273. } else {
  274. setTimeout(function (_) {
  275. resize();
  276. if (!_this2.$el.clientWidth || !_this2.$el.clientHeight) {
  277. console.warn(' Can\'t get dom width or height ');
  278. }
  279. }, widthChangeDelay);
  280. }
  281. });
  282. }
  283. },
  284. resizeableHandler: function resizeableHandler(resizeable) {
  285. if (resizeable && !this._once.onresize) this.addResizeListener();
  286. if (!resizeable && this._once.onresize) this.removeResizeListener();
  287. },
  288. init: function init() {
  289. if (this.echarts) return;
  290. var themeName = this.themeName || this.theme || _constants.DEFAULT_THEME;
  291. this.echarts = _echarts2.default.init(this.$refs.canvas, themeName, this.initOptions);
  292. if (this.data) this.changeHandler();
  293. this.createEventProxy();
  294. if (this.resizeable) this.addResizeListener();
  295. },
  296. addResizeListener: function addResizeListener() {
  297. window.addEventListener('resize', this.resizeHandler);
  298. this._once.onresize = true;
  299. },
  300. removeResizeListener: function removeResizeListener() {
  301. window.removeEventListener('resize', this.resizeHandler);
  302. this._once.onresize = false;
  303. },
  304. addWatchToProps: function addWatchToProps() {
  305. var _this3 = this;
  306. var watchedVariable = this._watchers.map(function (watcher) {
  307. return watcher.expression;
  308. });
  309. Object.keys(this.$props).forEach(function (prop) {
  310. if (!~watchedVariable.indexOf(prop) && !~_constants.STATIC_PROPS.indexOf(prop)) {
  311. var opts = {};
  312. if (~['[object Object]', '[object Array]'].indexOf((0, _utilsLite.getType)(_this3.$props[prop]))) {
  313. opts.deep = true;
  314. }
  315. _this3.$watch(prop, function () {
  316. _this3.changeHandler();
  317. }, opts);
  318. }
  319. });
  320. },
  321. createEventProxy: function createEventProxy() {
  322. var _this4 = this;
  323. // 只要用户使用 on 方法绑定的事件都做一层代理,
  324. // 是否真正执行相应的事件方法取决于该方法是否仍然存在 events 中
  325. // 实现 events 的动态响应
  326. var self = this;
  327. var keys = Object.keys(this.events || {});
  328. keys.length && keys.forEach(function (ev) {
  329. if (_this4.registeredEvents.indexOf(ev) === -1) {
  330. _this4.registeredEvents.push(ev);
  331. _this4.echarts.on(ev, function (ev) {
  332. return function () {
  333. if (ev in self.events) {
  334. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  335. args[_key] = arguments[_key];
  336. }
  337. self.events[ev].apply(null, args);
  338. }
  339. };
  340. }(ev));
  341. }
  342. });
  343. },
  344. themeChange: function themeChange(theme) {
  345. this.clean();
  346. this.echarts = null;
  347. this.init();
  348. },
  349. clean: function clean() {
  350. if (this.resizeable) this.removeResizeListener();
  351. this.echarts.dispose();
  352. }
  353. },
  354. created: function created() {
  355. this.echarts = null;
  356. this.registeredEvents = [];
  357. this._once = {};
  358. this._store = {};
  359. this.resizeHandler = (0, _utilsLite.debounce)(this.resize, this.resizeDelay);
  360. this.changeHandler = (0, _utilsLite.debounce)(this.dataHandler, this.changeDelay);
  361. this.addWatchToProps();
  362. },
  363. mounted: function mounted() {
  364. this.init();
  365. },
  366. beforeDestroy: function beforeDestroy() {
  367. this.clean();
  368. },
  369. _numerify: _numerify2.default
  370. };