123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- <!--
- * @Author: your name
- * @Date: 2024-09-11 14:32:12
- * @LastEditTime: 2025-07-14 13:39:59
- * @LastEditors: bogon
- * @Description: In User Settings Edit
- * @FilePath: /performance-test/src/views/performance/components/chartsCom/powerMarkers2DCharts.vue
- -->
- <!-- <h1>逐月有功功率散点2D分析</h1>
- <h1>偏航控制策略异常检测 2D</h1> 目前没找到这个分析模型-->
- <template>
- <div style="min-height: 600px">
- <!-- 2D散点图 -->
- <template>
- <div style="display: flex; align-items: center; margin-top: 20px">
- <div style="margin-right: 20px; display: flex; align-items: center">
- <el-select
- size="small"
- v-model="color1"
- @change="updateChartColor"
- placeholder="选择配色方案"
- style="width: 200px"
- >
- <el-option
- v-for="(scheme, index) in colorSchemes"
- :key="index"
- :label="scheme.label"
- :value="scheme.colors"
- >
- <span
- v-for="color in scheme.colors.slice(0, 8)"
- :style="{
- background: color,
- width: '20px',
- height: '20px',
- display: 'inline-block',
- }"
- ></span>
- </el-option>
- </el-select>
- </div>
- <!-- 点大小控制 -->
- <div style="display: flex; align-items: center">
- <!-- <span style="margin-right: 10px">点大小</span> -->
- <el-slider
- v-model="pointSize"
- :min="1"
- :max="15"
- :step="1"
- label="点的大小"
- show-stops
- style="width: 150px"
- @change="updateChartColor"
- ></el-slider>
- </div>
- </div>
- <div
- v-loading="loading"
- :ref="`plotlyChart-${index}`"
- style="height: 500px"
- >
- <el-empty v-if="isError" description="请求失败"></el-empty>
- </div>
- </template>
- </div>
- </template>
- <script>
- import Plotly from "plotly.js-dist";
- import axios from "axios";
- import { myMixin } from "@/mixins/chartRequestMixin";
- import { colorSchemes } from "@/views/overview/js/colors";
- import { mapState } from "vuex";
- export default {
- mixins: [myMixin],
- props: {
- fileAddr: {
- default: "",
- type: String,
- },
- index: {
- type: String,
- },
- setUpImgData: {
- default: () => [],
- type: Array,
- },
- },
- data() {
- return {
- pointSize: 5, // 默认点大小
- chartData: {},
- chartType: "scatter", // 默认显示散点图
- color1: colorSchemes[5].colors, // 默认颜色
- selectedPoints: [],
- originalColors: [],
- originalSizes: [],
- // 配色方案列表(每个方案是一个颜色数组)
- colorSchemes: [...colorSchemes],
- };
- },
- computed: {
- ...mapState("themes", {
- themeColor: "themeColor",
- }),
- },
- watch: {
- themeColor: {
- handler(newval) {
- if (newval.length === 0) {
- this.color1 = this.colorSchemes[5].colors;
- } else {
- this.color1 = newval;
- }
- this.updateChartColor();
- },
- deep: true,
- },
- setUpImgData: {
- handler(newType) {
- this.drawChart();
- },
- deep: true,
- },
- },
- async mounted() {
- this.$nextTick(() => {
- this.getData();
- // if (this.themeColor.length === 0) {
- this.color1 = this.colorSchemes[5].colors;
- // } else {
- // this.color1 = this.themeColor;
- // }
- });
- // console.log(this.color1, colorSchemes[0].colors, "color1");
- },
- methods: {
- // 根据配色方案设置每个选项的样式
- getOptionStyle(scheme) {
- return {
- background: `linear-gradient(to right, ${scheme
- .slice(0, 8)
- .join(", ")})`,
- color: "#fff",
- height: "30px",
- lineHeight: "30px",
- borderRadius: "0px",
- };
- },
- async getData() {
- if (this.fileAddr !== "") {
- try {
- this.loading = true;
- this.cancelToken = axios.CancelToken.source();
- const resultChartsData = await axios.get(this.fileAddr, {
- cancelToken: this.cancelToken.token,
- });
- this.chartData = resultChartsData.data;
- this.drawChart();
- this.isError = false;
- this.loading = false;
- } catch (error) {
- this.isError = true;
- this.loading = false;
- }
- }
- },
- drawChart() {
- // 提取散点数据和线数据
- const data =
- this.chartData.data &&
- this.chartData.data.filter(
- (item) => item.enginName !== "合同功率曲线"
- )[0]; // 点数据
- const lineData =
- this.chartData.data &&
- this.chartData.data.filter(
- (item) => item.enginName === "合同功率曲线"
- )[0]; // 线数据
- // 提取唯一时间标签,并计算 tickvals 和 ticktext
- const uniqueTimeLabels = data.colorbar
- ? [...new Set(data.colorbar)]
- : [...new Set(data.color)]; // 从 colorbar 中提取唯一的时间标签
- // console.log(data.colorbar, "data.colorbar 1");
- const tickvals = uniqueTimeLabels.map((label, index) => index + 1); // 根据时间标签生成 tickvals
- const ticktext = uniqueTimeLabels.map((dateStr) => {
- const date = new Date(dateStr);
- return date.toLocaleDateString("en-CA", {
- year: "numeric",
- month: "2-digit",
- }); // 格式化为 'yyyy-MM'
- }); // 使用格式化后的时间作为 ticktext
- const timeMapping = uniqueTimeLabels.reduce((acc, curr, index) => {
- acc[curr] = index + 1;
- return acc;
- }, {});
- // 获取 yData 的最小值和最大值来做比例值的计算
- const minValue = Math.min(...tickvals);
- const maxValue = Math.max(...tickvals);
- // 计算比例值
- const colors = ticktext.map((item, ind) => {
- // 计算比例值(可以根据需要调整映射的数据范围)
- const proportion = (tickvals[ind] - minValue) / (maxValue - minValue);
- return [
- proportion, // 比例值
- this.color1[ind], // 对应的颜色
- ];
- });
- // 将时间字符串映射为数字
- let colorValues = [];
- if (data.colorbar) {
- colorValues = data.colorbar.map((date, index) => timeMapping[date]);
- } else {
- colorValues = data.color.map((date, index) => timeMapping[date]);
- }
- let scatterTrace = {}; // 用于存放散点图的 trace
- let lineTrace = {}; // 用于存放折线图的 trace
- // 保存原始颜色和大小
- this.originalColors = [...data.yData];
- this.originalSizes = new Array(data.xData.length).fill(6); // 初始点大小
- // 绘制散点图
- if (this.chartType === "scatter") {
- scatterTrace = {
- x: data.xData,
- y: data.yData,
- mode: "markers", // 散点
- type: "scattergl", // 使用散点图
- text: data.engineName, // 提示文本
- marker: {
- color: colorValues, // 使用时间数据来映射颜色
- colorscale: this.color1
- ? [...colors]
- : [
- [0, "#F9FDD2"],
- [0.15, "#E9F6BD"],
- [0.3, "#C2E3B9"],
- [0.45, "#8AC8BE"],
- [0.6, "#5CA8BF"],
- [0.75, "#407DB3"],
- [0.9, "#2E4C9A"],
- [1, "#1B2973"],
- ], // 默认颜色渐变
- colorbar: {
- title: data.colorbartitle, // 色标标题
- tickvals: tickvals, // 设置刻度值
- ticktext: ticktext, // 设置刻度文本
- tickmode: "array", // 使用数组模式、
- x: 1.05, // x 控制横向位置(1 是右边,>1 就更靠右)
- y: 1.04, // y=1 是顶部
- // len: 0.4, // 高度(0~1之间)
- xanchor: "left", // x锚点从左对齐
- yanchor: "top", // y锚点从顶部对齐
- },
- size: new Array(data.xData.length).fill(this.pointSize), // 点的大小
- },
- hovertemplate:
- `${this.chartData.xaixs}:` +
- ` %{x} <br> ` +
- `${this.chartData.yaixs}:` +
- "%{y} <br>" +
- `时间: %{customdata}<extra></extra>`, // 在 hover 中显示格式化后的时间
- customdata: data.colorbar || data.color, // 将格式化后的时间存入 customdata
- timedata: data.timeData,
- };
- }
- if (lineData) {
- // 绘制折线图
- lineTrace = {
- x: lineData.xData, // 线数据的 xData
- y: lineData.yData, // 线数据的 yData
- mode: "lines+markers", // 线和点同时显示
- type: "scattergl", // 使用 scattergl 类型
- text: lineData.engineName, // 提示文本
- line: {
- color: "red", // 线条颜色
- },
- };
- }
- // 图表布局
- const layout = {
- title: {
- text: data.title,
- font: {
- size: 16, // 设置标题字体大小(默认 16)
- weight: "bold",
- },
- },
- xaxis: {
- title: this.chartData.xaixs,
- tickmode: "linear",
- gridcolor: "rgb(255,255,255)",
- showgrid: true,
- zeroline: false,
- tickcolor: "rgb(255,255,255)",
- backgroundcolor: "#e5ecf6",
- showbackground: true, // 显示背景
- dtick: 1,
- },
- yaxis: {
- title: this.chartData.yaixs,
- gridcolor: "rgb(255,255,255)", // 网格线颜色
- tickcolor: "rgb(255,255,255)",
- backgroundcolor: "#e5ecf6",
- showbackground: true, // 显示背景
- },
- showlegend: false,
- plot_bgcolor: "#e5ecf6",
- gridcolor: "#fff", // 设置网格线颜色
- };
- const getChartSetUp = (axisTitle) => {
- return this.setUpImgData.find((item) => item.text.includes(axisTitle));
- };
- const xChartSetUp = getChartSetUp(layout.xaxis.title);
- if (xChartSetUp) {
- layout.xaxis.dtick = xChartSetUp.dtick;
- layout.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
- }
- const yChartSetUp = getChartSetUp(layout.yaxis.title);
- if (yChartSetUp) {
- layout.yaxis.dtick = yChartSetUp.dtick;
- layout.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
- }
- // 配置工具栏按钮
- const config = {
- modeBarButtonsToAdd: [
- {
- name: "选择",
- icon: Plotly.Icons.pencil,
- click: (gd) => this.handleSelectClick(gd),
- },
- {
- name: "清除选中",
- icon: Plotly.Icons.undo,
- click: (gd) => this.handleClearSelect(gd),
- },
- {
- name: "下载CSV文件",
- icon: Plotly.Icons.disk,
- click: (gd) => this.handleDownloadCSV(gd),
- },
- ],
- modeBarButtonsToRemove: [
- "lasso2d", // 移除不需要的工具按钮
- // 移除不需要的工具按钮
- "lasso2d",
- "sendDataToCloud",
- "resetCameraLastSave3d",
- "resetCameraDefault3d",
- "resetCameraLastSave",
- "sendDataToCloud",
- "zoom2d", // 缩放按钮
- "zoom3d",
- "plotlylogo2D",
- "plotlylogo3D",
- ],
- displaylogo: false,
- editable: true,
- scrollZoom: false,
- };
- // 合并散点和折线图的数据
- const traces = [];
- if (scatterTrace) traces.push(scatterTrace); // 如果有散点数据
- if (lineTrace) traces.push(lineTrace); // 如果有线图数据
- this.$nextTick(() => {
- // 使用 Plotly 绘制图表
- Plotly.react(
- this.$refs[`plotlyChart-${this.index}`], // 这里是对 DOM 元素的引用
- traces,
- layout,
- config
- ).then(() => {
- // 确保图表加载完成后设置工具栏按钮
- const plotElement = this.$refs[`plotlyChart-${this.index}`];
- Plotly.relayout(plotElement, layout).then(function (gd) {
- // 获取工具栏按钮
- const toolbar = gd.querySelector(".modebar");
- const buttons = toolbar.querySelectorAll(".modebar-btn");
- // 定义一个映射对象,方便修改按钮提示
- const titleMap = {
- "Download plot as a png": "保存图片",
- Autoscale: "缩放",
- Pan: "平移",
- "Zoom out": "放大",
- "Zoom in": "缩小",
- "Box Select": "选择框操作",
- "Lasso Select": "套索选择操作",
- "Reset axes": "重置操作",
- "Reset camera to default": "重置相机视角",
- "Turntable rotation": "转台式旋转",
- "Orbital rotation": "轨道式旋转",
- };
- // 遍历所有按钮,修改它们的 title
- buttons.forEach(function (button) {
- const dataTitle = button.getAttribute("data-title");
- // 如果标题匹配,修改属性值
- if (titleMap[dataTitle]) {
- button.setAttribute("data-title", titleMap[dataTitle]);
- }
- });
- }); // 使用 relayout 来确保自定义按钮应用
- });
- });
- },
- handleSelectClick(gd) {
- // 绑定 plotly_click 事件
- gd.on("plotly_click", (data) => {
- const pointIndex = data.points[0].pointIndex;
- const xClick = data.points[0].x;
- const yClick = data.points[0].y;
- // 将点击的点添加到选中的点数组
- this.selectedPoints.push({
- x: xClick, // 点击点的 x 坐标
- y: yClick, // 点击点的 y 坐标
- index: pointIndex, // 点击点的索引
- time: data.points[0]?.data?.timedata[data.points[0].pointIndex], // 点击点的时间信息
- });
- // 初始化颜色和大小数组
- let newColors = [...this.originalColors];
- let newSize = [...this.originalSizes];
- // 如果选中的点数大于等于3,进行多边形选择区域的处理
- if (this.selectedPoints.length >= 3) {
- const xv = this.selectedPoints.map((p) => p.x);
- const yv = this.selectedPoints.map((p) => p.y);
- // 判断点是否在多边形内
- function inPolygon(x, y, xv, yv) {
- let inside = false;
- for (let i = 0, j = xv.length - 1; i < xv.length; j = i++) {
- const intersect =
- yv[i] > y !== yv[j] > y &&
- x < ((xv[j] - xv[i]) * (y - yv[i])) / (yv[j] - yv[i]) + xv[i];
- if (intersect) inside = !inside;
- }
- return inside;
- }
- // 用于跟踪已添加的 (x, y) 组合
- const addedPoints = {};
- // 遍历图表数据中的所有点,检查是否在多边形内
- gd.data[0].x.forEach((xVal, i) => {
- const yVal = gd.data[0].y[i];
- if (inPolygon(xVal, yVal, xv, yv)) {
- const pointKey = `${xVal}-${yVal}`;
- if (!addedPoints[pointKey]) {
- this.selectedPoints.push({
- x: gd.data[0].x[i],
- y: gd.data[0].y[i],
- time: gd.data[0].text[i],
- });
- newColors[i] = "red"; // 高亮选择的点
- newSize[i] = 10; // 设置点的大小
- addedPoints[pointKey] = true;
- }
- }
- });
- }
- // // 更新选中点的颜色和大小
- this.selectedPoints.forEach((point) => {
- newColors[point.index] = "red";
- newSize[point.index] = 10;
- });
- Plotly.addTraces(gd, {
- x: this.selectedPoints.map((p) => p.x),
- y: this.selectedPoints.map((p) => p.y),
- mode: "markers",
- type: "scattergl",
- marker: {
- color: "red",
- size: 10,
- },
- name: "选中点",
- showlegend: false,
- });
- // 处理选中的数据
- this.getSelectData(this.selectedPoints, gd.layout);
- });
- },
- handleClearSelect(gd) {
- this.selectedPoints = [];
- Plotly.restyle(gd, {
- "marker.color": [this.originalColors],
- "marker.size": [this.originalSizes],
- });
- },
- getSelectData(selectedPoints, layout) {
- // 在这里处理选中的数据,您可以将其展示或导出等
- console.log("选中的点数据:", selectedPoints);
- console.log("布局信息:", layout);
- },
- handleDownloadCSV(gd) {
- if (this.selectedPoints.length === 0) {
- alert("没有选中的数据,请选择数据后进行文件下载。");
- return;
- }
- this.downloadCSV();
- },
- async downloadCSV() {
- const paramValueTypes = await axios.get("/ETLapi/sysConf/getByType", {
- params: { type: "en_cn_mapping" },
- });
- console.log(paramValueTypes, "paramValueTypes");
- const headers = [this.chartData.xaixs, this.chartData.yaixs];
- const csvRows = [headers]; // 保存标头
- // 使用 Set 或 Map 去重
- const uniquePoints = [];
- // this.selectedPoints.forEach((point) => {
- // if (!uniquePoints.some((p) => p.x === point.x && p.y === point.y)) {
- // uniquePoints.push(point);
- // }
- // });
- // // 将去重后的点加入 CSV 数据
- // uniquePoints.forEach((point) => {
- // csvRows.push(`${point.x},${point.y}`);
- // });
- // const csvString = csvRows.join("\n");
- // const blob = new Blob([csvString], { type: "text/csv; charset=utf-8" });
- // const url = URL.createObjectURL(blob);
- // const a = document.createElement("a");
- // a.href = url;
- // a.download = "selected_data.csv";
- // a.click();
- // URL.revokeObjectURL(url);
- },
- updateChartColor(color) {
- // 更新图表颜色
- // this.color1 = color;
- this.drawChart();
- },
- },
- };
- </script>
- <style scoped>
- /* 自定义样式 */
- </style>
|