import puppeteer from "puppeteer"; import fs from "fs-extra"; import path from "path"; import FormData from "form-data"; import axios from "axios"; // 导入 axios import { colorSchemes } from "../colors.js"; export const getYewErrorBarChart = async ( data, bucketName, objectName, analysisTypeCode ) => { try { // 提取机组编号和偏航误差值 const xData = data.map((item) => item.engine_name); // 机组编号 const yData = data.map((item) => item.yaw_error1); // 偏航误差值 // 为每个数据点分配颜色 const colors = yData.map((value) => { if (value <= 3) { return "#8AC8BE"; // (0, 3] 蓝色 } else if (value <= 5) { return "#407DB3"; // (3, 5] 绿色 } else { return "#1B2973"; // (5, ∞] 红色 } }); const trace = { x: xData, // 横坐标数据 y: yData, // 纵坐标数据 type: "bar", // 当前图表类型 marker: { color: colors, // 每个点的颜色 }, name: "偏航误差值", // 图例名称 }; // 创建虚拟的 trace 以便显示图例 const legendTrace1 = { x: [null], y: [null], name: "(0, 3]", mode: "markers", marker: { color: "#8AC8BE", size: 10 }, }; const legendTrace2 = { x: [null], y: [null], name: "(3, 5]", mode: "markers", marker: { color: "#407DB3", size: 10 }, }; const legendTrace3 = { x: [null], y: [null], name: "(5, ∞]", mode: "markers", marker: { color: "#1B2973", size: 10 }, }; const layout = { title: { text: "机组静态偏航误差值", // 图表标题 font: { size: 16, // 设置标题字体大小(默认 16) weight: "bold", }, }, xaxis: { title: { text: "机组编号", }, // 横坐标标题 tickmode: "array", tickvals: xData, // 设置刻度值(机组编号) ticktext: xData, // 设置刻度文本(机组编号) gridcolor: "rgb(255,255,255)", tickcolor: "rgb(255,255,255)", backgroundcolor: "#e5ecf6", }, yaxis: { title: { text: "静态偏航误差值(度)", }, // 纵坐标标题 gridcolor: "rgb(255,255,255)", tickcolor: "rgb(255,255,255)", backgroundcolor: "#e5ecf6", }, margin: { l: 50, r: 50, t: 50, b: 50, }, plot_bgcolor: "#e5ecf6", gridcolor: "#fff", bgcolor: "#e5ecf6", // 设置背景颜色 autosize: true, // 开启自适应 showlegend: true, // 显示图例 }; // 创建临时目录 const tempDir = path.join(process.cwd(), "images"); await fs.ensureDir(tempDir); const tempFilePath = path.join( tempDir, `temp_yew_error_bar_chart_${Date.now()}.jpeg` ); // 获取 plotly.js 的绝对路径 const plotlyPath = path.join( process.cwd(), "src", "public", "js", "plotly-3.0.1.min.js" ); const plotlyContent = await fs.readFile(plotlyPath, "utf-8"); // 使用 Puppeteer 生成图表的截图 const browser = await puppeteer.launch({ headless: "new", executablePath: `${process.env.CHROME_PATH}`, // 根据系统改路径 args: ["--no-sandbox", "--disable-setuid-sandbox"], }); try { const page = await browser.newPage(); const traces = [trace, legendTrace1, legendTrace2, legendTrace3]; const htmlContent = `