BoxLineCharts.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div style="width: 100%; height: 500px">
  3. <!-- 条件渲染日期范围选择器 -->
  4. <el-date-picker
  5. v-if="chartData.xaixs === '时间'"
  6. v-model="dateRange"
  7. type="daterange"
  8. align="right"
  9. unlink-panels
  10. range-separator="至"
  11. start-placeholder="开始日期"
  12. end-placeholder="结束日期"
  13. @change="onDateRangeChange"
  14. size="mini"
  15. style="margin: 20px 0 0 0"
  16. ></el-date-picker>
  17. <div
  18. v-loading="loading"
  19. :id="`plotDivBox-${index}`"
  20. style="width: 100%; height: 450px"
  21. >
  22. <el-empty v-if="isError" description="请求失败"></el-empty>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import Plotly from "plotly.js-dist";
  28. import axios from "axios";
  29. import { myMixin } from "@/mixins/chartRequestMixin";
  30. export default {
  31. mixins: [myMixin],
  32. props: {
  33. fileAddr: {
  34. default: "",
  35. type: String,
  36. },
  37. index: {
  38. type: String,
  39. },
  40. setUpImgData: {
  41. default: () => [],
  42. type: Array,
  43. },
  44. },
  45. data() {
  46. return {
  47. chartData: {},
  48. dateRange: [], // 用于存储选中的日期范围
  49. loading: false,
  50. isError: false,
  51. };
  52. },
  53. watch: {
  54. setUpImgData: {
  55. handler(newType) {
  56. this.drawBoxPlot();
  57. },
  58. deep: true,
  59. },
  60. dateRange: {
  61. handler(newRange) {
  62. this.drawBoxPlot(); // 日期范围变化时重新绘制图表
  63. },
  64. deep: true,
  65. },
  66. },
  67. mounted() {
  68. console.log(this.fileAddr, "fileAddr 连接");
  69. this.getData();
  70. },
  71. methods: {
  72. async getData() {
  73. if (this.fileAddr !== "") {
  74. try {
  75. this.loading = true;
  76. this.cancelToken = axios.CancelToken.source();
  77. const resultChartsData = await axios.get(this.fileAddr, {
  78. cancelToken: this.cancelToken.token,
  79. });
  80. this.chartData = resultChartsData.data;
  81. this.loading = false;
  82. this.isError = false;
  83. this.drawBoxPlot(); // 数据加载完成后绘制图表
  84. } catch (error) {
  85. this.loading = false;
  86. this.isError = true;
  87. }
  88. }
  89. },
  90. // 处理日期范围变化
  91. onDateRangeChange() {
  92. this.drawBoxPlot(); // 日期变化时重新绘制图表
  93. },
  94. // 判断时间戳是否在选择的日期范围内
  95. isInDateRange(timestamp) {
  96. const [startDate, endDate] = this.dateRange;
  97. if (!startDate || !endDate) return true;
  98. const date = new Date(timestamp);
  99. return date >= new Date(startDate) && date <= new Date(endDate);
  100. },
  101. // 判断xData是否为日期格式
  102. isDateType(xData) {
  103. if (xData) {
  104. const firstTimestamp = xData[0];
  105. return !isNaN(Date.parse(firstTimestamp)); // 判断是否是有效日期
  106. } else {
  107. return false;
  108. }
  109. },
  110. // 过滤数据
  111. filterData(group) {
  112. const filteredXData = [];
  113. const filteredYData = [];
  114. const filteredMedians = group.medians ? { x: [], y: [] } : null;
  115. if (this.isDateType(group.xData)) {
  116. group.xData.forEach((timestamp, index) => {
  117. if (this.isInDateRange(timestamp)) {
  118. filteredXData.push(timestamp);
  119. filteredYData.push(group.yData[index]);
  120. if (filteredMedians && this.isInDateRange(group.medians.x[index])) {
  121. filteredMedians.x.push(group.medians.x[index]);
  122. filteredMedians.y.push(group.medians.y[index]);
  123. }
  124. }
  125. });
  126. } else {
  127. filteredXData.push(...group.xData);
  128. filteredYData.push(...group.yData);
  129. if (group.medians) {
  130. filteredMedians.x.push(...group.medians.x);
  131. filteredMedians.y.push(...group.medians.y);
  132. }
  133. }
  134. return {
  135. ...group,
  136. xData: filteredXData,
  137. yData: filteredYData,
  138. medians: filteredMedians,
  139. };
  140. },
  141. // 绘制箱线图
  142. drawBoxPlot() {
  143. if (!this.chartData.data || this.loading) return; // 如果数据为空或正在加载则不绘制
  144. const chartData = this.chartData.data[0];
  145. const filteredData = this.filterData(chartData);
  146. const trace = {
  147. x: filteredData.xData,
  148. y: filteredData.yData,
  149. type: "box",
  150. marker: {
  151. color: "#263649",
  152. },
  153. line: {
  154. width: 0.8, // 这里设置线条粗细(默认是 2)
  155. },
  156. boxpoints: false,
  157. boxmean: false,
  158. name: filteredData.title,
  159. fillcolor: "#458EF7", // 设置箱线图填充颜色,带透明度
  160. hovertemplate:
  161. `${this.chartData.xaixs}:` +
  162. ` %{x} <br> ` +
  163. `${this.chartData.yaixs}:` +
  164. "%{y} <br>",
  165. };
  166. let trace2 = {};
  167. if (filteredData.medians && filteredData.medians.x.length > 0) {
  168. trace2 = {
  169. x: filteredData.medians.x,
  170. y: filteredData.medians.y,
  171. mode: "markers",
  172. marker: {
  173. color: "#406DAB",
  174. size: 3,
  175. },
  176. name: `${filteredData.title} - 中位点`,
  177. type: "scatter",
  178. };
  179. }
  180. const layout = {
  181. title: {
  182. text: filteredData.title,
  183. font: {
  184. size: 16,
  185. weight: "bold",
  186. },
  187. },
  188. xaxis: {
  189. title: this.chartData.xaixs,
  190. tickmode: "array",
  191. type: "category", // 让 Y 轴按类别均匀分布
  192. gridcolor: "rgb(255,255,255)",
  193. tickcolor: "rgb(255,255,255)",
  194. backgroundcolor: "#e5ecf6",
  195. },
  196. yaxis: {
  197. title: this.chartData.yaixs,
  198. gridcolor: "rgb(255,255,255)",
  199. tickcolor: "rgb(255,255,255)",
  200. backgroundcolor: "#e5ecf6",
  201. },
  202. showlegend: false,
  203. plot_bgcolor: "#e5ecf6",
  204. gridcolor: "#fff",
  205. bgcolor: "#e5ecf6", // 设置背景颜色
  206. };
  207. if (
  208. this.chartData.xaixs === "机组" ||
  209. this.chartData.xaixs === "机组名称"
  210. ) {
  211. layout.xaxis.tickvals = [...new Set(filteredData.xData)];
  212. layout.xaxis.ticktext = [...new Set(filteredData.xData)];
  213. }
  214. const getChartSetUp = (axisTitle) => {
  215. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  216. };
  217. const xChartSetUp = getChartSetUp(layout.xaxis.title);
  218. if (xChartSetUp) {
  219. layout.xaxis.dtick = xChartSetUp.dtick;
  220. layout.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
  221. }
  222. const yChartSetUp = getChartSetUp(layout.yaxis.title);
  223. if (yChartSetUp) {
  224. layout.yaxis.dtick = yChartSetUp.dtick;
  225. layout.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
  226. }
  227. this.$nextTick(() => {
  228. const plotDiv = document.getElementById(`plotDivBox-${this.index}`);
  229. if (plotDiv) {
  230. Plotly.newPlot(plotDiv, [trace, trace2], layout);
  231. }
  232. });
  233. },
  234. },
  235. };
  236. </script>
  237. <style scoped>
  238. /* 可根据需要自定义样式 */
  239. </style>