123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- import {showAlertWin, withDateFormatLength} from "@/utils/cqcy";
- import { parseString } from "cron-parser";
- import html2canvas from "html2canvas";
- function setCellSize(l, e, t, o) {
- const {
- cs: r,
- rs: c
- } = l[e][t].mc, n = (l.config && l.config.rowhidden) ? Object.keys(l.config.rowhidden) : [], s = [];
- for (let l = 0; l < c; l++) {
- let t = e + l;
- n.indexOf(String(t)) < 0 && s.push(t)
- }
- const i = [];
- for (let l = 0; l < r; l++) i.push(t + l);
- const u = Object.values(o.getRowHeight(s))
- .reduce((l, e) => l + e);
- return {
- w: Object.values(o.getColumnWidth(i))
- .reduce((l, e) => l + e),
- h: u
- }
- }
- export function insertLuckysheetEChart({
- selector: l,
- info: e,
- sheet: t,
- optionData: o,
- echarts: r,
- luckysheet: c,
- $: n,
- _self: _self,
- flag: flag
- }) {
- if (!e.option || !t) return;
- const [s, i] = e.pos, u = e.className, f = e.option, {
- data: _,
- visibledatacolumn: d,
- visibledatarow: m
- } = t, a = _[s][i];
- let b = 0,
- y = 0,
- g = [],
- h = [];
- if (a.mc) {
- if (!String(a.mc.cs) || !String(a.mc.rs)) {
- if (_self) {
- _self.$message({
- message: '无效单元!',
- type: 'error'
- })
- }
- return void console.error("无效单元");
- }
- for (; b < a.mc.rs;) g.push(s + b++);
- for (; y < a.mc.cs;) h.push(i + y++)
- } else g.push(s), h.push(i);
- const p = 0 == i ? 0 : d[i - 1],
- v = 0 == s ? 0 : m[s - 1],
- j = Object.values(c.getColumnWidth(h))
- .reduce((l, e) => l + e, 0),
- w = Object.values(c.getRowHeight(g))
- .reduce((l, e) => l + e, 0);
- if (j < 80 || w < 80) {
- if (_self) {
- luckysheet.cancelRangeMerge()
- showAlertWin(_self, null, '所选单元格不满足宽度大于80(默认情况下至少3格)或者高度大于80(默认情况下至少5格),请调整大小后重试!')
- }
- return void console.error(`单元[${s},${i}]不满足: width >= 80 && height >= 80,调整大小后重试!`);
- }
- let x = `<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);
- n('.'+u).mousedown(function(){return false});
- let S = r.init(document.getElementsByClassName(u)[0]);
- f.animation = false;
- const height = getLegendHeight(j, f.legend.data)
- console.log(height)
- f.grid.bottom = height + 40
- S.on('finished', function() {
- // const imgBase = n(`.${u} canvas`)[0].toDataURL('image/png');
- // console.log(imgBase);
- html2canvas(n(`.${u}`)[0]).then(function(canvas) {
- // 将 Canvas 转换为图像(Base64 数据)
- const imageBase64 = canvas.toDataURL('image/png');
- sessionStorage.setItem(`print_${u}`, JSON.stringify({
- src: imageBase64,
- addr: toExcelColumn(h[0] + 1) + (g[0] + 1) + ":" + toExcelColumn(h[h.length - 1] + 1) + (g[g.length - 1] + 1)
- }));
- });
- })
- S.setOption(f);
- let k = S.getConnectedDataURL({
- type: "png",
- pixelRatio: 1,
- backgroundColor: "#ffffff"
- });
- if (flag) {
- // n('.' + u).css('display', 'none')
- $('.img-list').css('display', 'none');
- c.insertImage(k, {
- rowIndex: s,
- colIndex: i,
- cellSize: setCellSize(o, s, i, c),
- success: function () {
- console.log("插入成功")
- }
- })
- }
- }
- export function cellValueFormat(html) {
- const d = document.createElement('div')
- d.innerHTML = html
- const tr = d.getElementsByTagName('tr')
- let txt = ''
- for (let i = 0; i < tr.length; i++) {
- const td = tr[i].getElementsByTagName('td')
- for (let j = 0; j < td.length; j++) {
- txt += td[j].textContent
- if (j < td.length - 1) {
- txt += '\t'
- }
- }
- txt += '\r\n'
- }
- return txt
- }
- export function toExcelColumn(num) {
- let result = '';
- while (num > 0) {
- const remainder = (num - 1) % 26;
- result = String.fromCharCode(65 + remainder) + result;
- num = Math.floor((num - 1) / 26);
- }
- return result;
- }
- export function toLuckySheetJson(luckySheet) {
- let sheet = luckySheet.getSheetData(0);
- console.log(sheet)
- for (let i = 0; i < sheet.length; i++) {
- let row = sheet[i];
- for (let j = 0; j < row.length; j++) {
- if (!row[j]) {
- continue;
- }
- if (row[j].tb) {
- luckysheet.setCellFormat(i, j, 'tb', '0');
- }
- }
- }
- }
- export 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.insertColumn(arr[0].length)
- // luckysheet.insertRow(arr.length)
- 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 calculateMinutes(time1, time2) {
- // 将时间字符串转换为Date对象
- let date1 = new Date(time1);
- let date2 = new Date(time2);
- // 计算时间差(以毫秒为单位)
- let diffInMilliseconds = Math.abs(date2.getTime() - date1.getTime());
- // 将时间差转换为分钟
- let minutes = Math.floor(diffInMilliseconds / 1000 / 60);
- return minutes;
- }
- function changeValStr(str) {
- if (str) {
- if ((str + "").startsWith("${")) {
- return "";
- } else {
- if (str.v && str.v.startsWith("${")) {
- return "";
- } else {
- if (JSON.stringify(str).startsWith("${")) {
- return "";
- }
- }
- }
- return str;
- } else {
- return str;
- }
- }
- 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;
- }
|