BarChart1.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!--
  2. * @Author: your name
  3. * @Date: 2025-08-15 16:31:43
  4. * @LastEditTime: 2025-08-20 14:14:19
  5. * @LastEditors: bogon
  6. * @Description: In User Settings Edit
  7. * @FilePath: /performance-test/src/views/performance/components/chartsCom/BarChart1.vue
  8. -->
  9. <template>
  10. <div ref="chart" style="width: 100%; height: 500px"></div>
  11. </template>
  12. <script>
  13. import * as echarts from "echarts";
  14. import chartDataJson from "./json/production_indicator11.json"; // 你的数据文件
  15. import yaw_error1 from "./json/yaw_error1.json";
  16. import { colorSchemes } from "@/views/overview/js/colors";
  17. export default {
  18. props: {
  19. charts: {
  20. type: String,
  21. default() {
  22. return "";
  23. },
  24. },
  25. },
  26. data() {
  27. return {
  28. chart: null,
  29. };
  30. },
  31. mounted() {
  32. this.chart = echarts.init(this.$refs.chart);
  33. if (this.charts === "prd") {
  34. this.renderChart(chartDataJson);
  35. } else {
  36. this.renderChart(yaw_error1);
  37. // this.renderChart(chartDataJson);
  38. }
  39. window.addEventListener("resize", this.resizeChart);
  40. },
  41. beforeDestroy() {
  42. window.removeEventListener("resize", this.resizeChart);
  43. if (this.chart) this.chart.dispose();
  44. },
  45. methods: {
  46. resizeChart() {
  47. if (this.chart) this.chart.resize();
  48. },
  49. renderChart(dataObj) {
  50. if (!dataObj || !dataObj.data || dataObj.data.length === 0) return;
  51. // X 轴(风机编号)
  52. const xData = dataObj.data[0].xData;
  53. // 系列名称
  54. const seriesNames = dataObj.data.map((item) => item.enginName);
  55. // 构造 series
  56. const series = dataObj.data.map((item) => ({
  57. name: item.enginName,
  58. type: "bar",
  59. stack: "lossStack", // 设置堆叠
  60. data: item.yData,
  61. emphasis: { focus: "series" },
  62. }));
  63. const option = {
  64. backgroundColor: "#e5ecf6", // 整个图表背景色
  65. grid: {
  66. left: "10%", // 左边距
  67. right: "10%", // 右边距
  68. top: "15%", // 上边距
  69. // bottom: "15%", // 下边距
  70. containLabel: true, // 保证坐标轴标签显示完整
  71. },
  72. tooltip: {
  73. trigger: "axis",
  74. axisPointer: { type: "shadow" },
  75. },
  76. color: [
  77. "#8ECAC1",
  78. "#77BDC2",
  79. "#64ADC2",
  80. "#559ABE",
  81. "#4884B7",
  82. "#406DAB",
  83. ],
  84. legend: {
  85. data: seriesNames,
  86. },
  87. xAxis: {
  88. type: "category",
  89. data: xData,
  90. name: dataObj.xaixs || "",
  91. splitLine: {
  92. show: true,
  93. lineStyle: {
  94. color: "#fff",
  95. },
  96. },
  97. axisTick: { alignWithLabel: true },
  98. // nameLocation: "middle", // 设置名称位置
  99. // nameGap: 30, // 与轴线的距离
  100. // nameRotate: 0, // 旋转角度(可选)
  101. },
  102. yAxis: {
  103. type: "value",
  104. name: dataObj.yaixs || "",
  105. nameLocation: "middle", // 设置名称位置
  106. nameGap: 30, // 与轴线的距离
  107. nameRotate: 90, // 旋转角度(可选)
  108. splitLine: {
  109. show: true,
  110. lineStyle: {
  111. color: "#fff",
  112. },
  113. },
  114. },
  115. series: series,
  116. };
  117. this.chart.setOption(option);
  118. },
  119. },
  120. };
  121. </script>