Jelajahi Sumber

打印图例配置

ws 1 tahun lalu
induk
melakukan
0bd4935eda

+ 172 - 90
PrintServer/src/main/resources/static/cy-sheet/index.js

@@ -32,12 +32,11 @@ const getSheetApi = () => {
 						for (let i in reportChartList) {
 							insertEChartInfo(reportChartList[i]);
 						}
-
 						// 绘制基础数据项
 						drawBaseInfo(baseItem)
 						// 绘制数据值
 						drawTableData(reportTableItemList,
-							reportTableType, json.data)
+							reportTableType, json.data, tableData.data[0].data)
 						task = setInterval(function() {
 							if (taskStatus) {
 								let status = true;
@@ -149,96 +148,179 @@ function drawBaseInfo(baseData) {
 	})
 }
 
-function drawTableData(tableItemList, type, tableInfo) {
-	if (!tableItemList || tableItemList.length == 0) {
-		updateLocalExcelContent()
-		return
-	}
-	// 事件驱动报表
-	if (type == 2 || type == 4) {
-		tableItemList.forEach((tableItem, i) => {
-			if (i == 0) {
-				let valueTimeList = tableItem.valueTimeList ? tableItem.valueTimeList.split(',') : []
-				valueTimeList = withDateFormatLength(valueTimeList)
-				let xAxis = tableItem.xaxis
-				let yAxis = tableItem.yaxis - 1
-				if (valueTimeList.length == 0) {
-					luckysheet.setCellValue(xAxis, yAxis, '')
-				} else {
-					valueTimeList.forEach((v, j) => {
-						luckysheet.setCellValue(xAxis + j, yAxis, v)
-					})
-				}
-			}
-			let valueList = tableItem.valueList ? tableItem.valueList
-				.split(',') : []
-			let xAxis = tableItem.xaxis
-			let yAxis = tableItem.yaxis
-			if (valueList.length == 0) {
-				luckysheet.setCellValue(xAxis, yAxis, '')
-			} else {
-				valueList.forEach((v, j) => {
-					luckysheet.setCellValue(xAxis + j, yAxis,
-						v)
-				})
-			}
-		})
-		updateLocalExcelContent()
-		return
-	}
-	// 设备报表
-	if (type == 5 || type == 6) {
-		tableItemList.forEach((tableItem, i) => {
-			let standby = tableItem.standby ? JSON.parse(tableItem.standby) : {}
-			let dataIndex = standby.index != null ? standby.index : -1
-			let valueList = tableItem.valueList ? tableItem.valueList.split(',') : []
-			let xAxis = tableItem.xaxis
-			let yAxis = tableItem.yaxis
-			// 序号、时间处理
-			if (tableItem.timeItemType == 0) { // 序号、开始时间
-				let valueIndexList = tableItem.valueIndexList ? tableItem.valueIndexList.split(',') : []
-				deviceReportUpdateData('${index}', valueIndexList, false)
-				let valueTimeList = tableItem.valueTimeList ? tableItem.valueTimeList.split(',') : []
-				valueTimeList = withDateFormatLength(valueTimeList)
-				deviceReportUpdateData('${startTime}', valueTimeList, false)
-			}
-			if (tableItem.timeItemType == 1) { // 结束时间
-				let valueTimeList = tableItem.valueTimeList ? tableItem.valueTimeList.split(',') : []
-				valueTimeList = withDateFormatLength(valueTimeList)
-				deviceReportUpdateData('${stopTime}', valueTimeList, tableInfo.isGenCountTime == 1)
-			}
-			if (valueList.length == 0) {
-				luckysheet.setCellValue(xAxis, yAxis, '')
-			} else {
-				valueList.forEach((v, j) => {
-					luckysheet.setCellValue(xAxis + j, yAxis, v)
-				})
-			}
-		})
-		updateLocalExcelContent()
-		return
-	}
-	// 其余报表信息
-	tableItemList.forEach((tableItem, i) => {
-		// let standby = tableItem.standby ? JSON.parse(tableItem.standby) :
-		// {}
-		// let dataIndex = standby.index != null ? standby.index : -1
-		let valueList = tableItem.valueList ? tableItem.valueList.split(
-			',') : []
-		// let val = (dataIndex == -1 || (dataIndex + 1) > valueList
-		// 		.length) ?
-		// 	cqcyCode['invalidData'] : valueList[dataIndex]
-		let xAxis = tableItem.xaxis
-		let yAxis = tableItem.yaxis
-		if (valueList.length == 0) {
-			luckysheet.setCellValue(xAxis, yAxis, '')
+function drawTableData(tableItemList, type, tableInfo, sheData) {
+	// if (!tableItemList || tableItemList.length == 0) {
+	//         // this.updateLocalExcelContent();
+	//         return;
+	//       }
+	      // 事件驱动报表
+	      if (type == 2 || type == 4) {
+	          setSheetDatas(tableItemList, luckysheet, type, null, sheData);
+	        return;
+	      }
+	      // 设备报表
+	      if (type == 5 || type == 6) {
+	          setSheetDatas(tableItemList, luckysheet, type, tableInfo.isGenCountTime, sheData);
+	        return;
+	      }
+	        setSheetDatas(tableItemList, luckysheet, null, null, sheData);
+}
+
+function setSheetDatas(tableItemList, luckysheet, type, isGenCountTime, sheData) {
+    console.log(type)
+    let arr = initArrays(tableItemList, type, isGenCountTime, sheData)
+
+    console.log("A1:" + toExcelColumn(arr[0].length) + arr.length)
+    setTimeout(function () {
+        
+        luckysheet.setRangeValue(arr, {range: "A1:" + toExcelColumn(arr[0].length) + arr.length})
+
+    },100)
+}
+
+Array.min = function(array) {
+    return Math.min.apply(Math, array)
+}
+// 最大值
+Array.max = function (array) {
+    return Math.max.apply(Math, array)
+}
+function initArrays(tableItemList, type, isGenCountTime, sheData) {
+    // 纵向 reportTableType = 1
+    let arr = {}
+    let yAdd = 0;
+    let xAdd = 0;
+    let valueTimeList = null;
+    let startTimes = [];
+    let endTimes = [];
+    for (let i = 0; i < tableItemList.length; i++) {
+        let tableItem = tableItemList[i];
+        let standby = JSON.parse(tableItem.standby)
+        let fieldType = standby.fieldType ? standby.fieldType: 2
+        let valueList = tableItem.valueList ?
+            tableItem.valueList.split(",") : [];
+        let xAxis = tableItem.xaxis;
+        let yAxis = tableItem.yaxis;
+        let xAxis2 = tableItem.yaxis;
+        let yAxis2 = tableItem.xaxis;
+        if (fieldType == 1) {
+            for (var j = 0; j < valueList.length; j++) {
+                arr[(yAxis2 + j)+ "_" + xAxis2] = valueList[j]
+            }
+        } else {
+            for (var j = 0; j < valueList.length; j++) {
+                arr[xAxis + "_" + (yAxis + j)] = valueList[j]
+            }
+        }
+        if (i == 0 && (type == 2 || type == 4)) {
+            valueTimeList = tableItem.valueTimeList
+                ? tableItem.valueTimeList.split(",")
+                : [];
+            valueTimeList = withDateFormatLength(valueTimeList);
+            yAdd = yAxis2
+        }
+        if (type == 5 || type == 6) {
+            yAdd = yAxis2
+            if (i == 0) {
+                xAdd = xAxis2
+            }
+            let valueTimeList = tableItem.valueTimeList
+                ? tableItem.valueTimeList.split(",")
+                : [];
+            valueTimeList = withDateFormatLength(valueTimeList);
+            if (tableItem.timeItemType == 0) {
+                // 开始时间
+                startTimes = withDateFormatLength(valueTimeList);
+
+            }
+            if (tableItem.timeItemType == 1) {
+                // 结束时间
+                endTimes = withDateFormatLength(valueTimeList);
+            }
+        }
+    }
+
+    let x = 0;
+    let y = 0;
+    for (let key in arr) {
+        let str = key.split("_");
+        if (y < parseInt(str[0])) {
+            y = parseInt(str[0])
+        }
+        if (x < parseInt(str[1])) {
+            x = parseInt(str[1])
+        }
+    }
+    let aIndex = isGenCountTime == 1 ? 1: 0;
+    let array = []
+    console.log(sheData)
+    for (var i = 0; i < sheData.length; i++) {
+        let row = sheData[i]
+        for (var j = 0; j < row.length; j++) {
+            if (arr[i + "_" + j]) {
+                row[j] = (arr[i + "_" + j])
+            } else {
+                if (j == yAdd - 1 && (type == 2 || type == 4)) {
+                    if (i >= yAdd) {
+
+                        row[j] = (valueTimeList[i - yAdd])
+                    } else {
+                        row[j] = changeValStr(row[j])
+                    }
+                } else if (type == 5 || type == 6) {
+                    if (i >= yAdd) {
+                       if (j == xAdd - 3 - aIndex) {
+                           // 序号列
+                           row[j] = (i - yAdd < startTimes.length ? i - yAdd + 1 : "")
+                       } else if (j == xAdd - 2 - aIndex) {
+                           // 开始时间
+                           row[j] = (i - yAdd < startTimes.length ? startTimes[i - yAdd] : "")
+                       } else if (j == xAdd - 1 - aIndex) {
+                           // 开始时间
+                           row[j] = (i - yAdd < endTimes.length ? endTimes[i - yAdd] : "")
+                       } else if(isGenCountTime == 1 && j == xAdd - 1 && row[j - 2] && row[j - 1]) {
+                           // 计算时间
+                          const time = calculateMinutes(row[j - 2], row[j - 1]);
+                           row[j] = (time)
+                       } else {
+                           row[j] = changeValStr(row[j])
+                       }
+
+                    } else {
+                        row[j] = changeValStr(row[j])
+                    }
+                } else {
+
+                    row[j] = changeValStr(row[j])
+                }
+            }
+        }
+        array.push(row)
+    }
+
+    console.log(array)
+    return array
+
+}
+
+
+function changeValStr(str) {
+    if (str) {
+		if ((str + "").startsWith("${")) {
+			return "";
 		} else {
-			valueList.forEach((v, j) => {
-				luckysheet.setCellValue(xAxis + j, yAxis, v)
-			})
+			if (str.v && str.v.startsWith("${")) {
+			    return "";
+			} else  {
+			    if (JSON.stringify(str).startsWith("${")) {
+			        return "";
+			    }
+			}
 		}
-	})
-	updateLocalExcelContent()
+        return str;
+    } else {
+        return str;
+    }
 }
 
 function insertEChartInfo(chart) {

+ 77 - 1
PrintServer/src/main/resources/static/cy-sheet/lib/chart.js

@@ -71,6 +71,16 @@ function insertLuckysheetEChart({
 		`<div class="${u} luckysheet-modal-dialog luckysheet-modal-dialog-chart luckysheet-data-visualization-chart" style="width: ${j}px;height: ${w}px;position: absolute;z-index: 1000;left: ${p}px;top: ${v}px;"></div>`;
 	n(l + " #luckysheet-cell-main").append(x);
 	let S = r.init(document.getElementsByClassName(u)[0]);
+	if (!f.legend.data || f.legend.data.length == 0 || !f.legend.data[0]) {
+		let arr = []
+		 for (let nn = 0; nn < f.series.length; nn++) {
+		 	arr.push(f.series[nn].name)
+		 }
+		 f.legend.data = arr
+	}
+	const height = getLegendHeight(j, f.legend.data)
+	console.log(height)
+	f.grid.bottom = height + 80
 	S.on('finished', function() {
 		const imgBase = n(`.${u} canvas`)[0].toDataURL('image/png');
 		console.log(imgBase);
@@ -111,4 +121,70 @@ function toExcelColumn(num) {
     num = Math.floor((num - 1) / 26);
   }
   return result;
-}
+}
+
+function getLegendHeight(clazzw, data){
+        var height =0;
+
+        var legend = [];
+        console.log('legend',legend);
+        //主算法,将legend中的设置渲染为DOM元素,用dom元素的宽高来模拟legend组件在echarts内部的高度
+        var icongap = 5;//legend图形左右两边各有5个px的间隔
+        var left = 10,right = 10;
+        //计算legend组件的可用宽度
+        var chartWidth = legend.width||clazzw-left-right;
+		console.log(chartWidth)
+        //legend的padding
+        var padding = legend.padding || 0;
+        if($.isArray(padding)) padding = padding.join('px ')+'px';
+        else padding+='px';
+        //每个legend item之间的间隙(包括水平和垂直)
+        var itemGap = 5;
+        //创建一个不可见的模拟盒子
+        var $legendbox = $('<div class="legend-simulate-box"></div>').css({
+            width: (chartWidth+itemGap) +'px',
+            padding: padding,
+            'line-height': '1',
+            'box-sizing': 'border-box',
+            overflow: 'hidden',
+            'position': 'absolute',
+            'z-index': '-1',
+            'opacity': '0',
+            'filter': 'alpha(opacity=0)',
+            '-ms-filter': 'alpha(opacity=0)'
+        }).appendTo('body');
+        //模拟绘制单个legend item
+        var itemHeight = 14,itemWidth = 25;
+        console.log('itemHeight',itemHeight);
+        if(itemHeight%2!=0) itemHeight++;
+        if(itemWidth%2!=0) itemWidth++;
+        var fontSize = 12;
+        var fontWeight = 'normal';
+        $.each(data,function(i,name){
+            var $icon = $('<span></span>').css({
+                display: 'inline-block',
+                padding: '0 '+icongap+'px',
+                'box-sizing': 'content-box',
+                'width': itemWidth+'px',
+                'height': itemHeight+'px'
+            });
+            var $item = $('<div></div>').css({
+                'display': 'inline-block',
+                'float': 'left',
+                'margin-right': itemGap+'px',
+                'margin-bottom': itemGap+'px',
+                'font-size': fontSize+'px',
+                'font-weight': fontWeight,
+				'max-width': chartWidth - 30,
+				'white-space': 'nowrap',
+				'text-overflow': 'clip'
+            }).append($icon).append(name).appendTo($legendbox);
+        });
+        //得到模拟高度
+        console.log('legendbox',$legendbox.innerHeight());
+        console.log('itemGap',itemGap);
+        height = $legendbox.innerHeight()-itemGap;
+        //善后工作
+        $legendbox.remove();
+        return height;
+    }