/*
* @Author: your name
* @Date: 2025-04-14 16:09:13
* @LastEditTime: 2025-04-15 13:54:17
* @LastEditors: bogon
* @Description: In User Settings Edit
* @FilePath: /downLoadServer/src/server/utils/chartsCom/3DDrawingChart.js
*/
import puppeteer from "puppeteer";
import fs from "fs-extra";
import path from "path";
import FormData from "form-data";
import { colorSchemes } from "../colors.js";
export const generate3DDrawingChart = async (data) => {
try {
console.log("开始生成热力图...");
console.log("数据:", data);
const colorSchemesItem = colorSchemes[0].colors;
// 创建临时目录
const tempDir = path.join(process.cwd(), "images");
await fs.ensureDir(tempDir);
const tempFilePath = path.join(
tempDir,
`temp_heatmap_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");
// 创建浏览器实例
const browser = await puppeteer.launch({
headless: "new",
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
try {
const page = await browser.newPage();
// 准备图表数据
const chartDataset = data.data[0]; // 修改为 data.chartData
const uniqueColors = [...new Set(chartDataset.color)];
const traces = uniqueColors.map((color, idx) => {
const colorData = chartDataset.color.map((c) => (c === color ? 1 : 0));
return {
x: chartDataset.xData.filter((_, i) => colorData[i] === 1),
y: chartDataset.yData.filter((_, i) => colorData[i] === 1),
z: chartDataset.zData.filter((_, i) => colorData[i] === 1),
mode: "markers", // 根据需要设置模式
type: "scatter3d",
marker: {
size: 1, // 使用动态点大小
color: colorSchemesItem[idx], // 使用配色方案
colorscale: "YlGnBu",
},
name: ` ${color}`,
// hovertemplate: `${data.xaixs}: %{x}
${data.yaixs}: %{y}
${data.zaixs}: %{z}