From 26d0fa42ea8c63a87e8ef93d0915f75f46fb1f9c Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期四, 24 三月 2022 11:09:53 +0800
Subject: [PATCH] 2022-03-24

---
 src/tabviews/custom/components/chart/antv-bar-line/index.jsx |  133 +++++++++++++++++++++++++++++++------------
 1 files changed, 95 insertions(+), 38 deletions(-)

diff --git a/src/tabviews/custom/components/chart/antv-bar-line/index.jsx b/src/tabviews/custom/components/chart/antv-bar-line/index.jsx
index f279ed3..2680b42 100644
--- a/src/tabviews/custom/components/chart/antv-bar-line/index.jsx
+++ b/src/tabviews/custom/components/chart/antv-bar-line/index.jsx
@@ -4,6 +4,7 @@
 import { Chart } from '@antv/g2'
 import DataSet from '@antv/data-set'
 import { Spin, Empty, notification } from 'antd'
+import { DownloadOutlined } from '@ant-design/icons'
 import moment from 'moment'
 
 import asyncComponent from './asyncButtonComponent'
@@ -12,9 +13,10 @@
 import Utils from '@/utils/utils.js'
 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 NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader'))
 const ExcelOutButton = asyncComponent(() => import('@/tabviews/zshare/actionList/exceloutbutton'))
 const ExcelInButton = asyncComponent(() => import('@/tabviews/zshare/actionList/excelInbutton'))
 
@@ -40,6 +42,7 @@
     search: null,              // 鎼滅储鏉′欢
     vFields: [],               // 鏁板�煎瓧娈�
     vstFields: null,           // 缁熻鏁版嵁鍊煎瓧娈典俊鎭�
+    chart: null
   }
 
   UNSAFE_componentWillMount () {
@@ -94,22 +97,7 @@
       })
     }
 
-    let padding = 0
-    if (_config.style.paddingTop && !isNaN(parseInt(_config.style.paddingTop))) {
-      padding += parseInt(_config.style.paddingTop)
-    }
-    if (_config.style.paddingBottom && !isNaN(parseInt(_config.style.paddingBottom))) {
-      padding += parseInt(_config.style.paddingBottom)
-    }
-
-    let height = config.plot.height || 400
-    if (config.plot.title || config.search.length > 0) {
-      _config.plot.height = height - 70 - padding
-    } else {
-      _config.plot.height = height - 25 - padding
-    }
-
-    _config.style.height = height
+    _config.style.height = config.plot.height || 400
 
     let transfield = {}
     _config.columns.forEach(col => {
@@ -138,7 +126,7 @@
 
       _config.plot.hasBar = Bar_axis.length > 0
 
-      if (_config.plot.mutilBar !== 'overlap') {
+      if (_config.plot.mutilBar !== 'overlap' && Bar_axis.length > 1) {
         _config.plot.Bar_axis = Bar_axis
       }
     } else {
@@ -249,7 +237,8 @@
       let axisIndex = 0
       let fields = []
       let legends = []
-  
+      let vFieldsShow = {}
+
       _config.plot.customs.forEach(item => {
         if (colors.has(item.name)) {
           item.color = colors.get(item.name)
@@ -306,10 +295,19 @@
           name: item.name,
           marker: { symbol: item.chartType === 'bar' ? 'square' : 'hyphen', style: { stroke: item.color,fill: item.color, r: 5, lineWidth: 2 } }
         })
+
+        if ((!_config.plot.Bar_axis || item.chartType !== 'bar') && item.show) { // 鎶樼嚎鍥炬垨閲嶅彔涓嬬殑鏌辩姸鍥惧彲鍗曠嫭璁剧疆鏄剧ず绫诲瀷
+          vFieldsShow[item.type] = item.show
+        }
       })
       _config.plot.customs = fields
       _config.plot.legends = legends
       _config.plot.axisIndex = axisIndex
+      vFields.forEach(item => {
+        if (vFieldsShow[item.field]) {
+          item.show = vFieldsShow[item.field]
+        }
+      })
     }
 
     this.setState({
@@ -325,7 +323,9 @@
       transfield
     }, () => {
       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()
       }
@@ -385,7 +385,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]
 
@@ -467,7 +467,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()
       })
@@ -543,10 +543,17 @@
 
     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,
         loading: false
       }, () => {
+        if (!reset) return
         this.handleData()
       })
     } else {
@@ -593,7 +600,7 @@
     if (plot.repeat === 'average') {
       let _mdata = new Map()
       _cdata.forEach(item => {
-        if (!item[plot.Xaxis]) return
+        if (!item[plot.Xaxis] && item[plot.Xaxis] !== 0) return
 
         vFields.forEach(col => {
           if (typeof(item[col.field]) !== 'number') {
@@ -610,6 +617,8 @@
         // dodge is not support linear attribute, please use category attribute! 鏃堕棿鏍煎紡
         if (/^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}:\d{2})?/.test(item[plot.Xaxis])) {
           item[plot.Xaxis] = ' ' + item[plot.Xaxis]
+        } else {
+          item[plot.Xaxis] = '' + item[plot.Xaxis]
         }
 
         if (!_mdata.has(item[plot.Xaxis])) {
@@ -638,7 +647,7 @@
     } else if (plot.repeat === 'cumsum') {
       let _mdata = new Map()
       _cdata.forEach(item => {
-        if (!item[plot.Xaxis]) return
+        if (!item[plot.Xaxis] && item[plot.Xaxis] !== 0) return
 
         vFields.forEach(col => {
           if (typeof(item[col.field]) !== 'number') {
@@ -654,6 +663,8 @@
 
         if (/^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}:\d{2})?/.test(item[plot.Xaxis])) {
           item[plot.Xaxis] = ' ' + item[plot.Xaxis]
+        } else {
+          item[plot.Xaxis] = '' + item[plot.Xaxis]
         }
 
         if (!_mdata.has(item[plot.Xaxis])) {
@@ -679,10 +690,12 @@
     } else { // plot.repeat === 'unrepeat'
       let _mdata = new Map()
       _cdata.forEach(item => {
-        if (!item[plot.Xaxis]) return
+        if (!item[plot.Xaxis] && item[plot.Xaxis] !== 0) return
 
         if (/^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}:\d{2})?/.test(item[plot.Xaxis])) {
           item[plot.Xaxis] = ' ' + item[plot.Xaxis]
+        } else {
+          item[plot.Xaxis] = '' + item[plot.Xaxis]
         }
 
         if (!_mdata.has(item[plot.Xaxis])) {
@@ -906,11 +919,11 @@
   
       _data = dv.rows
     }
-    
+
     const chart = new Chart({
       container: this.state.chartId,
       autoFit: true,
-      height: plot.height
+      height: this.wrap.offsetHeight - 25
     })
 
     chart.data(_data)
@@ -1060,6 +1073,8 @@
     }
     
     chart.render()
+
+    this.setState({chart})
   }
 
   /**
@@ -1089,7 +1104,7 @@
     const chart = new Chart({
       container: this.state.chartId,
       autoFit: true,
-      height: plot.height
+      height: this.wrap.offsetHeight - 25
     })
     
     // 鍧愭爣杞存牸寮忓寲
@@ -1311,7 +1326,7 @@
     view2.data(dv.rows)
     view2.legend(false)
 
-    plot.customs.forEach((item, i) => {
+    plot.customs.forEach(item => {
       if (item.chartType === 'bar' && !plot.Bar_axis) {
         view2.axis(item.name, item.axis)
       
@@ -1336,7 +1351,7 @@
           .tooltip(`${item.name}`, (value) => {
             return {
               name: item.name,
-              value: plot.show === 'percent' ? value + '%' : value
+              value: item.show === 'percent' ? value + '%' : value
             }
           })
 
@@ -1345,7 +1360,7 @@
         }
         if (item.label !== 'false') {
           _chart.label(item.name, (value) => {
-            if (plot.show === 'percent') {
+            if (item.show === 'percent') {
               value = value + '%'
             }
             if (plot.label === 'true' && plot.labelColor === 'custom' && item.color) {
@@ -1387,13 +1402,13 @@
           .tooltip(`${item.name}`, (value) => {
             return {
               name: item.name,
-              value: plot.show === 'percent' ? value + '%' : value
+              value: item.show === 'percent' ? value + '%' : value
             }
           })
 
         if (item.label === 'true') {
           _chart.label(item.name, (value) => {
-            if (plot.show === 'percent') {
+            if (item.show === 'percent') {
               value = value + '%'
             }
             return {
@@ -1425,6 +1440,8 @@
     }
 
     chart.render()
+
+    this.setState({chart})
   }
 
   /**
@@ -1477,7 +1494,7 @@
     const chart = new Chart({
       container: this.state.chartId,
       autoFit: true,
-      height: plot.height
+      height: this.wrap.offsetHeight - 25
     })
 
     chart.data(_data)
@@ -1688,7 +1705,7 @@
 
     chart.on('element:click', (ev) => {
       let data = ev.data.data
-      MKEmitter.emit('resetSelectLine', config.uuid, (data ? data.$$uuid : ''), null)
+      MKEmitter.emit('resetSelectLine', config.uuid, (data ? data.$$uuid : ''), data)
     })
 
     if (plot.interaction && plot.interaction.length) {
@@ -1698,12 +1715,51 @@
     }
 
     chart.render()
+
+    this.setState({chart})
   }
 
   refreshSearch = (list) => {
     this.setState({search: list}, () => {
       this.loadData()
     })
+  }
+
+  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() {
@@ -1718,8 +1774,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 (
@@ -1744,7 +1801,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