From 11745b05b3ecdd3a3dc6f64793dc83a095de2b09 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期四, 12 五月 2022 10:19:52 +0800
Subject: [PATCH] 2022-05-12

---
 src/tabviews/custom/components/chart/antv-scatter/index.jsx |  107 ++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 84 insertions(+), 23 deletions(-)

diff --git a/src/tabviews/custom/components/chart/antv-scatter/index.jsx b/src/tabviews/custom/components/chart/antv-scatter/index.jsx
index 742617f..c650f3f 100644
--- a/src/tabviews/custom/components/chart/antv-scatter/index.jsx
+++ b/src/tabviews/custom/components/chart/antv-scatter/index.jsx
@@ -3,17 +3,17 @@
 import { is, fromJS } from 'immutable'
 import { Chart } from '@antv/g2'
 import { Spin, Empty, notification } from 'antd'
+import { DownloadOutlined } from '@ant-design/icons'
 import moment from 'moment'
 
 import Api from '@/api'
 import Utils from '@/utils/utils.js'
-import asyncComponent from '@/utils/asyncComponent'
 import asyncBtnComponent from './asyncButtonComponent'
 import UtilsDM from '@/utils/utils-datamanage.js'
 import MKEmitter from '@/utils/events.js'
+import NormalHeader from '@/tabviews/custom/components/share/normalheader'
 import './index.scss'
 
-const NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader'))
 const ExcelOutButton = asyncBtnComponent(() => import('@/tabviews/zshare/actionList/exceloutbutton'))
 const ExcelInButton = asyncBtnComponent(() => import('@/tabviews/zshare/actionList/excelInbutton'))
 
@@ -36,6 +36,7 @@
     plot: null,                // 鍥捐〃璁剧疆
     data: null,                // 鏁版嵁
     search: null,              // 鎼滅储鏉′欢
+    chart: null
   }
 
   UNSAFE_componentWillMount () {
@@ -52,14 +53,7 @@
       _sync = false
     }
 
-    let height = config.plot.height || 400
-    if (config.plot.title || config.search.length > 0) {
-      _config.plot.height = height - 70
-    } else {
-      _config.plot.height = height - 25
-    }
-
-    _config.style.height = height
+    _config.style.height = config.plot.height || 400
 
     this.setState({
       config: _config,
@@ -72,7 +66,9 @@
       search: Utils.initMainSearch(config.search),
     }, () => {
       if (config.setting.sync !== 'true' && config.setting.onload === 'true') {
-        this.loadData()
+        setTimeout(() => {
+          this.loadData()
+        }, _config.setting.delay || 0)
       } else if (config.setting.sync === 'true' && _data) {
         this.handleData()
       }
@@ -132,7 +128,7 @@
 
     if (!config.timer) return
 
-    const _change = { '15s': 15000, '30s': 30000, '1min': 60000, '5min': 300000, '10min': 600000, '15min': 900000, '30min': 1800000, '1hour': 3600000 }
+    const _change = { '5s': 5000, '15s': 15000, '30s': 30000, '1min': 60000, '5min': 300000, '10min': 600000, '15min': 900000, '30min': 1800000, '1hour': 3600000 }
 
     let timer = _change[config.timer]
 
@@ -188,14 +184,24 @@
 
     if (config.uuid !== menuId) return
 
-    this.loadData()                                                            // 鏁版嵁鍒锋柊
+    let supModule = config.setting.supModule
 
-    if (btn.syncComponentId && btn.syncComponentId !== config.uuid && btn.syncComponentId !== config.setting.supModule) {
-      MKEmitter.emit('reloadData', btn.syncComponentId)                        // 鍚岀骇鏍囩鍒锋柊
+    btn.syncComponentId && MKEmitter.emit('reloadData', btn.syncComponentId)
+
+    if (!btn.syncComponentId || btn.syncComponentId !== supModule) {
+      if (position === 'mainline' || position === 'popclose') { // 鍒锋柊婧愮粍浠舵椂锛岄檮甯﹀埛鏂颁笂绾ц涓庡綋鍓嶇粍浠�
+        if (supModule && BID) {
+          MKEmitter.emit('reloadData', supModule, BID)
+        } else {
+          this.loadData()
+        }
+      } else {
+        this.loadData()
+      }
     }
 
-    if (position === 'mainline' && config.setting.supModule) {                 // 涓昏〃琛屽埛鏂�
-      MKEmitter.emit('reloadData', config.setting.supModule, (BID || 'empty'))
+    if (position === 'popclose') { // 鎵ц鍚姩寮圭獥鐨勬寜閽墍閫夋嫨鐨勫埛鏂伴」
+      btn.$tabId && MKEmitter.emit('refreshPopButton', btn.$tabId)
     }
   }
 
@@ -211,7 +217,7 @@
     const { config } = this.state
 
     if (!config.setting.supModule || config.setting.supModule !== MenuID) return
-    if (id !== this.state.BID) {
+    if (id !== this.state.BID || id !== '') {
       this.setState({ BID: id }, () => {
         this.loadData()
       })
@@ -288,11 +294,18 @@
 
     let result = await Api.genericInterface(param)
     if (result.status) {
+      let reset = true
+
+      if (hastimer && is(fromJS(result.data), fromJS(this.state.data))) {
+        reset = false
+      }
+      
       this.setState({
         data: result.data,
         empty: false,
         loading: false
       }, () => {
+        if (!reset) return
         this.handleData()
       })
     } else {
@@ -327,7 +340,7 @@
     const chart = new Chart({
       container: chartId,
       autoFit: true,
-      height: plot.height
+      height: this.wrap.offsetHeight - 25
     })
 
     chart.data(data);
@@ -375,6 +388,8 @@
       })
     }
     chart.render()
+
+    this.setState({chart})
   }
 
   refreshSearch = (list) => {
@@ -383,11 +398,56 @@
     })
   }
 
+  downloadImage = () => {
+    const { chart, config } = this.state
+    const link = document.createElement('a');
+    const filename = `${config.name}${moment().format('YYYY-MM-DD HH_mm_ss')}.png`;
+    const canvas = chart.getCanvas();
+    canvas.get('timeline').stopAllAnimations();
+  
+    setTimeout(() => {
+      const canvas = chart.getCanvas();
+      const canvasDom = canvas.get('el');
+      const dataURL = canvasDom.toDataURL('image/png');
+
+      if (window.Blob && window.URL) {
+        const arr = dataURL.split(',');
+        const mime = arr[0].match(/:(.*?);/)[1];
+        const bstr = atob(arr[1]);
+        let n = bstr.length;
+        const u8arr = new Uint8Array(n);
+        while (n--) {
+          u8arr[n] = bstr.charCodeAt(n);
+        }
+        const blobObj = new Blob([u8arr], { type: mime });
+        if (window.navigator.msSaveBlob) {
+          window.navigator.msSaveBlob(blobObj, filename);
+        } else {
+          link.addEventListener('click', () => {
+            link.download = filename;
+            link.href = window.URL.createObjectURL(blobObj);
+          });
+        }
+      }
+      const e = document.createEvent('MouseEvents');
+      e.initEvent('click', false, false);
+      link.dispatchEvent(e);
+    }, 16);
+  }
+
   render() {
     const { config, loading, empty, BID } = this.state
 
+    let style = {...config.style}
+    if (empty && config.plot.empty === 'hidden') {
+      style.opacity = 0
+      style.position = 'absolute'
+      style.zIndex = -1
+      style.width = '100%'
+    }
+
     return (
-      <div className="custom-scatter-plot-box" style={config.style}>
+      <div className="custom-scatter-plot-box" id={'anchor' + config.uuid} style={style}>
         {loading ?
           <div className="loading-mask">
             <div className="ant-spin-blur"></div>
@@ -395,8 +455,9 @@
           </div> : null
         }
         <NormalHeader config={config} BID={BID} menuType={this.props.menuType} refresh={this.refreshSearch} />
-        <div className="canvas-wrap">
-          <div className="chart-action">
+        <div className="canvas-wrap" ref={ref => this.wrap = ref}>
+          {config.plot.download === 'enable' && this.state.chart && !empty ? <DownloadOutlined onClick={this.downloadImage} className="system-color download"/> : null}
+          <div className={'chart-action' + (config.plot.download === 'enable' ? ' downable' : '')}>
             {config.action.map(item => {
               if (item.OpenType === 'excelOut') {
                 return (
@@ -421,7 +482,7 @@
               }
             })}
           </div>
-          <div className={'canvas' + (empty ? ' empty' : '')} style={{height: config.plot.height + 25}} id={this.state.chartId}></div>
+          <div className={'canvas' + (empty ? ' empty' : '')} id={this.state.chartId}></div>
         </div>
         {empty ? <Empty description={false}/> : null}
       </div>

--
Gitblit v1.8.0