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 |   88 ++++++++++++++++++++++++++++++++++++++------
 1 files changed, 76 insertions(+), 12 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 1c7f5cc..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'
@@ -41,6 +42,7 @@
     search: null,              // 鎼滅储鏉′欢
     vFields: [],               // 鏁板�煎瓧娈�
     vstFields: null,           // 缁熻鏁版嵁鍊煎瓧娈典俊鎭�
+    chart: null
   }
 
   UNSAFE_componentWillMount () {
@@ -124,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 {
@@ -235,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)
@@ -292,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({
@@ -311,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()
       }
@@ -586,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') {
@@ -603,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])) {
@@ -631,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') {
@@ -647,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])) {
@@ -672,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])) {
@@ -1053,6 +1073,8 @@
     }
     
     chart.render()
+
+    this.setState({chart})
   }
 
   /**
@@ -1304,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)
       
@@ -1329,7 +1351,7 @@
           .tooltip(`${item.name}`, (value) => {
             return {
               name: item.name,
-              value: plot.show === 'percent' ? value + '%' : value
+              value: item.show === 'percent' ? value + '%' : value
             }
           })
 
@@ -1338,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) {
@@ -1380,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 {
@@ -1418,6 +1440,8 @@
     }
 
     chart.render()
+
+    this.setState({chart})
   }
 
   /**
@@ -1691,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() {
@@ -1712,7 +1775,8 @@
         }
         <NormalHeader config={config} BID={BID} menuType={this.props.menuType} refresh={this.refreshSearch} />
         <div className="canvas-wrap" ref={ref => this.wrap = ref}>
-          <div className="chart-action">
+          {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 (

--
Gitblit v1.8.0