| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- 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()}.png`
- );
- // 获取 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",
- args: ["--no-sandbox", "--disable-setuid-sandbox"],
- });
- try {
- const page = await browser.newPage();
- const traces = [trace, legendTrace1, legendTrace2, legendTrace3];
- const htmlContent = `
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>机组静态偏航误差值</title>
- <script>${plotlyContent}</script>
- <style>
- body { margin: 0; }
- #chart { width: 800px; height: 600px; }
- </style>
- </head>
- <body>
- <div id="chart"></div>
- <script>
- window.onload = function() {
- Plotly.newPlot('chart', ${JSON.stringify(
- traces
- )}, ${JSON.stringify(layout)}).then(() => {
- window.chartRendered = true;
- });
- };
- </script>
- </body>
- </html>
- `;
- await page.setContent(htmlContent, { waitUntil: "networkidle0" });
- await page.waitForFunction(() => window.chartRendered === true, {
- timeout: 60000,
- });
- // 截图并保存到临时文件
- const chartElement = await page.$("#chart");
- await chartElement.screenshot({ path: tempFilePath, type: "png" });
- // 上传图片到服务器
- const formData = new FormData();
- formData.append("file", fs.createReadStream(tempFilePath));
- const response = await axios.post(
- `${process.env.API_BASE_URL}/examples/upload`,
- {
- filePath: tempFilePath,
- bucketName,
- objectName,
- }
- );
- console.log("上传成功:", response.data);
- return response?.data?.url;
- } catch (error) {
- console.error("生成图表失败:", error);
- } finally {
- await browser.close();
- }
- } catch (error) {
- console.error("发生错误:", error);
- }
- };
|