BoxLineCharts.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: "lightgray",
  152. },
  153. boxpoints: false,
  154. boxmean: false,
  155. name: filteredData.title,
  156. fillcolor: "#406DAB", // 设置箱线图填充颜色,带透明度
  157. hovertemplate:
  158. `${this.chartData.xaixs}:` +
  159. ` %{x} <br> ` +
  160. `${this.chartData.yaixs}:` +
  161. "%{y} <br>",
  162. };
  163. let trace2 = {};
  164. if (filteredData.medians && filteredData.medians.x.length > 0) {
  165. trace2 = {
  166. x: filteredData.medians.x,
  167. y: filteredData.medians.y,
  168. mode: "markers",
  169. marker: {
  170. color: "#406DAB",
  171. size: 3,
  172. },
  173. name: `${filteredData.title} - 中位点`,
  174. type: "scatter",
  175. };
  176. }
  177. const layout = {
  178. title: filteredData.title,
  179. xaxis: {
  180. title: this.chartData.xaixs,
  181. tickmode: "array",
  182. },
  183. yaxis: { title: this.chartData.yaixs },
  184. showlegend: true,
  185. };
  186. if (
  187. this.chartData.xaixs === "机组" ||
  188. this.chartData.xaixs === "机组名称"
  189. ) {
  190. layout.xaxis.tickvals = [...new Set(filteredData.xData)];
  191. layout.xaxis.ticktext = [...new Set(filteredData.xData)];
  192. }
  193. const getChartSetUp = (axisTitle) => {
  194. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  195. };
  196. const xChartSetUp = getChartSetUp(layout.xaxis.title);
  197. if (xChartSetUp) {
  198. layout.xaxis.dtick = xChartSetUp.dtick;
  199. layout.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
  200. }
  201. const yChartSetUp = getChartSetUp(layout.yaxis.title);
  202. if (yChartSetUp) {
  203. layout.yaxis.dtick = yChartSetUp.dtick;
  204. layout.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
  205. }
  206. this.$nextTick(() => {
  207. const plotDiv = document.getElementById(`plotDivBox-${this.index}`);
  208. if (plotDiv) {
  209. Plotly.newPlot(plotDiv, [trace, trace2], layout);
  210. }
  211. });
  212. },
  213. },
  214. };
  215. </script>
  216. <style scoped>
  217. /* 可根据需要自定义样式 */
  218. </style>