From 46f79b491173d284a4900d19e7aecf7509481438 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期五, 21 一月 2022 17:21:25 +0800
Subject: [PATCH] 2022-01-21

---
 src/tabviews/custom/components/chart/antv-bar-line/index.jsx |   72 ++++++++++++++++++++++++++++++++----
 1 files changed, 64 insertions(+), 8 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 cb6fb7f..b83a695 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({
@@ -1059,6 +1071,8 @@
     }
     
     chart.render()
+
+    this.setState({chart})
   }
 
   /**
@@ -1310,7 +1324,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)
       
@@ -1335,7 +1349,7 @@
           .tooltip(`${item.name}`, (value) => {
             return {
               name: item.name,
-              value: plot.show === 'percent' ? value + '%' : value
+              value: item.show === 'percent' ? value + '%' : value
             }
           })
 
@@ -1344,7 +1358,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) {
@@ -1386,13 +1400,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 {
@@ -1424,6 +1438,8 @@
     }
 
     chart.render()
+
+    this.setState({chart})
   }
 
   /**
@@ -1697,12 +1713,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,7 +1773,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