123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <!--
- * @Author: your name
- * @Date: 2025-01-21 11:18:49
- * @LastEditTime: 2025-02-19 10:06:39
- * @LastEditors: bogon
- * @Description: In User Settings Edit
- * @FilePath: /performance-test/src/views/performance/components/chartsCom/GeneratorTemperature.vue
- -->
- <template>
- <div>
- <!-- 图表控制面板 总图-->
- <div style="display: flex; align-items: center">
- <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"
- :style="getOptionStyle(scheme.colors)"
- ></el-option>
- </el-select>
- <!-- <span style="margin-left: 10px">自定义颜色</span> -->
- </div>
- <div>
- <el-button size="small" @click="toggleChartType">
- 切换为{{ chartType === "line" ? "面积图" : "折线图" }}
- </el-button>
- </div>
- </div>
- <!-- 图表容器 -->
- <div
- v-loading="loading"
- :id="`bar-chart${index}`"
- style="width: 100%; height: 400px"
- >
- <el-empty v-if="isError" description="请求失败"></el-empty>
- </div>
- </div>
- </template>
- <script>
- import { nextTick } from "vue"; // 导入 nextTick
- import Plotly from "plotly.js-dist";
- import axios from "axios";
- import { colorSchemesLine } from "@/views/overview/js/colors";
- import { myMixin } from "@/mixins/chartRequestMixin"; // 假设你需要的 mixin
- export default {
- props: {
- fileAddr: {
- type: String,
- default: "",
- },
- index: {
- type: String,
- default() {
- return "";
- },
- },
- },
- mixins: [myMixin],
- data() {
- return {
- chartData: {},
- color1: [], // 默认颜色
- // 配色方案列表(每个方案是一个颜色数组)
- colorSchemes: [...colorSchemesLine],
- chartType: "line", // 默认图表类型是折线图
- loading: false,
- isError: false,
- colors: [...colorSchemesLine[0].colors],
- typeLine: ["solid", "dot", "dash", "longdash", "dashdot", "longdashdot"],
- };
- },
- mounted() {
- if (this.fileAddr) {
- this.getData();
- }
- },
- methods: {
- // 获取数据
- 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;
- // 使用 nextTick 来确保 DOM 渲染完成后绘制图表
- nextTick(() => {
- this.drawChart();
- this.isError = false;
- this.loading = false;
- });
- } catch (error) {
- console.error("Error loading data:", error);
- this.isError = true;
- this.loading = false;
- }
- }
- },
- // 绘制图表
- drawChart() {
- const data = [];
- this.chartData.data.forEach((turbine, index) => {
- // 判断图表类型,根据类型调整绘制方式
- const chartConfig = {
- x: turbine.xData, // X 数据
- y: turbine.yData, // Y 数据
- name: turbine.Name, // 使用机组名称
- line: {
- dash: this.typeLine[index % this.colors.length],
- color:
- this.color1.length > 0
- ? this.color1[index % this.color1.length]
- : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
- },
- marker: {
- color:
- this.color1.length > 0
- ? this.color1[index % this.color1.length]
- : this.colors[index % this.colors.length], // 为每个机组分配不同的颜色
- },
- };
- if (this.chartType === "line") {
- chartConfig.fill = "none";
- chartConfig.mode = "lines"; // 如果是折线图
- } else if (this.chartType === "bar") {
- chartConfig.fill = "tonexty";
- // chartConfig.type = "bar"; // 如果是柱状图
- }
- data.push(chartConfig);
- });
- const layout = {
- title: this.chartData.title,
- xaxis: {
- title: this.chartData.xaixs || "X轴", // 横坐标标题
- },
- yaxis: {
- title: this.chartData.yaixs || "Y轴", // 纵坐标标题
- },
- margin: {
- l: 50,
- r: 50,
- t: 50,
- b: 50,
- },
- autosize: true, // 开启自适应
- barmode: this.chartType === "bar" ? "stack" : "group", // 如果是柱状图则启用堆叠
- };
- // 使用 Plotly.react 来更新图表
- Plotly.react(`bar-chart${this.index}`, data, layout, {
- responsive: true,
- });
- },
- // 切换图表类型
- toggleChartType() {
- this.chartType = this.chartType === "line" ? "bar" : "line"; // 切换图表类型
- this.drawChart(); // 重新绘制图表
- },
- // 更新图表颜色
- updateChartColor() {
- this.drawChart(); // 更新颜色后重新绘制图表
- },
- // 根据配色方案设置每个选项的样式
- getOptionStyle(scheme) {
- return {
- background: `linear-gradient(to right, ${scheme.join(", ")})`,
- color: "#fff",
- height: "30px",
- lineHeight: "30px",
- borderRadius: "4px",
- };
- },
- },
- };
- </script>
- <style scoped>
- /* 样式可以根据需求自定义 */
- </style>
|