Time3DChart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <template>
  2. <div>
  3. <!-- 配色方案选择和图表类型切换 -->
  4. <div style="display: flex; align-items: center; padding-top: 20px">
  5. <div style="margin-right: 20px; display: flex; align-items: center">
  6. <el-select
  7. size="small"
  8. v-model="color1"
  9. @change="updateChartColor"
  10. placeholder="选择配色方案"
  11. style="width: 200px"
  12. >
  13. <el-option
  14. v-for="(scheme, index) in colorSchemes"
  15. :key="index"
  16. :label="scheme.label"
  17. :value="scheme.colors"
  18. >
  19. <span
  20. v-for="color in scheme.colors.slice(0, 8)"
  21. :style="{
  22. background: color,
  23. width: '20px',
  24. height: '20px',
  25. display: 'inline-block',
  26. }"
  27. ></span>
  28. </el-option>
  29. </el-select>
  30. </div>
  31. <!-- 点大小控制 -->
  32. <div style="display: flex; align-items: center">
  33. <el-slider
  34. v-model="pointSize"
  35. :min="1"
  36. :max="15"
  37. :step="1"
  38. label="点的大小"
  39. show-stops
  40. style="width: 150px"
  41. @change="updateChartColor"
  42. ></el-slider>
  43. </div>
  44. </div>
  45. <div
  46. v-loading="loading"
  47. :id="`plotly-3d-chart-` + index"
  48. ref="plotlyChart"
  49. style="height: 600px; background-color: #e5ecf6"
  50. ></div>
  51. </div>
  52. </template>
  53. <script>
  54. import Plotly, { doCamera } from "plotly.js-dist";
  55. import axios from "axios";
  56. import { myMixin } from "@/mixins/chartRequestMixin";
  57. import { colorSchemes } from "@/views/overview/js/colors";
  58. import { mapState } from "vuex";
  59. export default {
  60. props: {
  61. fileAddr: {
  62. default: "",
  63. type: String,
  64. },
  65. index: {
  66. default: "",
  67. type: String,
  68. },
  69. setUpImgData: {
  70. default: () => [],
  71. type: Array,
  72. },
  73. },
  74. data() {
  75. return {
  76. color1: [], // 默认颜色
  77. chartData: {},
  78. chartType: "scatter", // 当前图表类型(默认是散点图)
  79. colorSchemes: [...colorSchemes],
  80. pointSize: 1, // 默认点大小
  81. };
  82. },
  83. mixins: [myMixin],
  84. computed: {
  85. ...mapState("themes", {
  86. themeColor: "themeColor",
  87. }),
  88. },
  89. watch: {
  90. themeColor: {
  91. handler(newval) {
  92. if (newval.length === 0) {
  93. this.color1 = colorSchemes[0].colors;
  94. } else {
  95. this.color1 = newval;
  96. }
  97. this.updateChartColor();
  98. },
  99. deep: true,
  100. },
  101. setUpImgData: {
  102. handler(newType) {
  103. this.renderChart();
  104. },
  105. deep: true,
  106. },
  107. },
  108. async mounted() {
  109. this.getData();
  110. if (this.themeColor.length === 0) {
  111. this.color1 = colorSchemes[0].colors;
  112. } else {
  113. this.color1 = this.themeColor;
  114. }
  115. },
  116. methods: {
  117. async getData() {
  118. if (this.fileAddr !== "") {
  119. try {
  120. this.loading = true;
  121. this.cancelToken = axios.CancelToken.source();
  122. const resultChartsData = await axios.get(this.fileAddr, {
  123. cancelToken: this.cancelToken.token,
  124. });
  125. if (typeof resultChartsData.data === "string") {
  126. let dataString = resultChartsData.data;
  127. dataString = dataString.trim(); // 去除前后空格
  128. dataString = dataString.replace(/Infinity/g, '"Infinity"'); // 处理无效字符
  129. try {
  130. const parsedData = JSON.parse(dataString);
  131. this.chartData = parsedData;
  132. } catch (error) {
  133. console.error("JSON 解析失败:", error);
  134. }
  135. } else {
  136. this.chartData = resultChartsData.data;
  137. }
  138. this.renderChart();
  139. this.isError = false;
  140. this.loading = false;
  141. } catch (error) {
  142. this.isError = true;
  143. this.loading = false;
  144. }
  145. }
  146. },
  147. // 格式化日期为 YY-MM 格式
  148. formatDate(dateString) {
  149. const date = new Date(dateString);
  150. const year = date.getFullYear(); // 获取年份后两位
  151. const month = ("0" + (date.getMonth() + 1)).slice(-2); // 获取月份并确保两位数
  152. return `${year}-${month}`;
  153. },
  154. renderChart() {
  155. // 提取 Y 轴数据中的月份,并去重
  156. const uniqueMonths = Array.from(
  157. new Set(
  158. this.chartData.data[0].yData.map((date) => this.formatDate(date))
  159. )
  160. );
  161. if (!this.color1) {
  162. this.color1 = colorSchemes[0].colors;
  163. }
  164. // 设置每个月份对应的颜色
  165. const monthColors = this.color1;
  166. // 为每个月份生成独立的 trace,每个 trace 对应一个月份
  167. const traces = uniqueMonths.map((month, monthIndex) => {
  168. const monthData = this.chartData.data[0].yData
  169. .map((date, index) => (this.formatDate(date) === month ? index : -1))
  170. .filter((index) => index !== -1);
  171. const trace = {
  172. x: monthData.map((index) => this.chartData.data[0].xData[index]), // 发电机转速
  173. y: monthData.map((index) => this.chartData.data[0].yData[index]), // 时间
  174. z: monthData.map((index) => this.chartData.data[0].zData[index]), // 有功功率
  175. mode: "markers",
  176. type: "scatter3d", // 3D 散点图
  177. marker: {
  178. size: this.pointSize,
  179. color: monthColors[monthIndex],
  180. opacity: 0.8,
  181. lighting: {
  182. ambient: 0.3, // 环境光(影响整体亮度)
  183. diffuse: 1, // 漫反射光(增加真实感)
  184. specular: 0.5, // 镜面反射(增强高光)
  185. roughness: 0.5, // 低值=光滑表面,高值=粗糙表面
  186. fresnel: 0.2, // 边缘高光强度
  187. },
  188. },
  189. name: month, // 图例项名称,格式为 YY-MM
  190. legendgroup: `month-${monthIndex}`, // 图例分组
  191. hovertemplate:
  192. `${this.chartData.xaixs}:` +
  193. ` %{x} <br> ` +
  194. `${this.chartData.yaixs}:` +
  195. "%{y} <br>" +
  196. `${this.chartData.zaixs}:` +
  197. "%{z} <br><extra></extra>",
  198. };
  199. return trace;
  200. });
  201. const layout = {
  202. title: {
  203. text: this.chartData.data[0].title,
  204. font: {
  205. size: 16,
  206. weight: "bold",
  207. },
  208. },
  209. scene: {
  210. xaxis: {
  211. title: {
  212. text: this.chartData.xaixs,
  213. standoff: 100,
  214. },
  215. gridcolor: "#fff",
  216. // backgroundcolor: "#CFD4DC",
  217. backgroundcolor: "#e0e7f1",
  218. showbackground: true,
  219. linecolor: "black",
  220. ticks: "outside",
  221. ticklen: 10,
  222. tickcolor: "black",
  223. zeroline: false,
  224. tickangle: -10,
  225. },
  226. // 对 Y 轴不显示默认标题,只保留 tick 标签,并适当加大 standoff 以防止标签挤在一起
  227. yaxis: {
  228. title: {
  229. text: this.chartData.yaixs, // 隐藏默认标题
  230. },
  231. type: "category", // 让 Y 轴按类别均匀分布
  232. categoryorder: "category ascending", // 按类别字母顺序排列
  233. type: "date",
  234. tickformat: "%Y-%m",
  235. dtick: "M3",
  236. gridcolor: "#fff",
  237. tickcolor: "#e5ecf6",
  238. // backgroundcolor: "#CFD4DC",
  239. backgroundcolor: "#e0e7f1",
  240. showbackground: true,
  241. linecolor: "black",
  242. ticks: "outside",
  243. tickcolor: "black",
  244. zeroline: false,
  245. tickangle: 25,
  246. },
  247. zaxis: {
  248. title: {
  249. text: this.chartData.zaixs,
  250. },
  251. gridcolor: "#fff",
  252. tickcolor: "#fff",
  253. // backgroundcolor: "#CFD4DC",
  254. backgroundcolor: "#e0e7f1",
  255. showbackground: true,
  256. linecolor: "black",
  257. ticks: "outside",
  258. tickcolor: "black",
  259. zeroline: false,
  260. tickangle: -90,
  261. },
  262. bgcolor: "#e5ecf6",
  263. aspectratio: {
  264. x: 2.2,
  265. y: 1.7,
  266. z: 1,
  267. },
  268. aspectmode: "manual",
  269. gridcolor: "#fff",
  270. camera: {
  271. up: {
  272. x: 0.200292643688136,
  273. y: 0.2488259353493132,
  274. z: 0.947612004346693,
  275. },
  276. center: {
  277. x: -0.052807476121180814,
  278. y: 0.02451796399554085,
  279. z: -0.022911006648570736,
  280. },
  281. eye: {
  282. x: -2.126379643342493,
  283. y: -2.551422475965373,
  284. z: 1.0917667684145647,
  285. },
  286. projection: {
  287. type: "orthographic",
  288. },
  289. },
  290. // 添加 annotation 模拟 Y 轴标题
  291. // annotations: [
  292. // {
  293. // x: -0.15, // 让标题靠左
  294. // y: 1, // 让标题在 Y 轴顶部
  295. // xref: "paper",
  296. // yref: "paper",
  297. // text: this.chartData.xaixs, // Y 轴标题
  298. // showarrow: false,
  299. // textangle: 90, // 旋转 90°
  300. // font: {
  301. // size: 16,
  302. // color: "black",
  303. // },
  304. // xanchor: "right",
  305. // yanchor: "top", // 避免重叠
  306. // },
  307. // ],
  308. },
  309. margin: { t: 50, b: 50, l: 50, r: 50 },
  310. staticPlot: false,
  311. showlegend: true,
  312. legend: {
  313. marker: {
  314. size: 10,
  315. },
  316. },
  317. };
  318. const config = {
  319. modeBarButtonsToAdd: [
  320. {
  321. name: "还原", // 自定义按钮
  322. icon: Plotly.Icons.home,
  323. click: () => this.resetCamera(),
  324. },
  325. ],
  326. modeBarButtonsToRemove: [
  327. "sendDataToCloud",
  328. "autoScale2d",
  329. "hoverClosest3d",
  330. "resetCameraLastSave3d",
  331. "resetCameraDefault3d",
  332. ],
  333. displaylogo: false, // 可选:隐藏 Plotly logo
  334. };
  335. // 获取x轴和y轴的设置
  336. const getChartSetUp = (axisTitle) => {
  337. return this.setUpImgData.find((item) => item.text.includes(axisTitle));
  338. };
  339. // 更新x轴和y轴的范围与步长
  340. const xChartSetUp = getChartSetUp(layout.scene.xaxis.title);
  341. if (xChartSetUp) {
  342. layout.scene.xaxis.dtick = xChartSetUp.dtick;
  343. layout.scene.xaxis.range = [xChartSetUp.min, xChartSetUp.max];
  344. }
  345. const yChartSetUp = getChartSetUp(layout.scene.yaxis.title);
  346. if (yChartSetUp) {
  347. layout.scene.yaxis.dtick = yChartSetUp.dtick;
  348. layout.scene.yaxis.range = [yChartSetUp.min, yChartSetUp.max];
  349. }
  350. const zChartSetUp = getChartSetUp(layout.scene.zaxis.title);
  351. if (zChartSetUp) {
  352. layout.scene.zaxis.dtick = zChartSetUp.dtick;
  353. layout.scene.zaxis.range = [zChartSetUp.min, zChartSetUp.max];
  354. }
  355. Plotly.newPlot(`plotly-3d-chart-` + this.index, traces, layout, config);
  356. // 监听图表的 relayout 事件,获取并输出相机视角
  357. const plotElement = document.getElementById(
  358. `plotly-3d-chart-` + this.index
  359. );
  360. plotElement.on("plotly_relayout", function (eventData) {
  361. // 在每次布局变更时,打印当前相机视角
  362. if (eventData["scene.camera"]) {
  363. console.log(
  364. "当前相机视角:",
  365. eventData["scene.camera"],
  366. eventData["scene.aspectratio"]
  367. );
  368. }
  369. });
  370. },
  371. // 还原视角
  372. resetCamera() {
  373. Plotly.relayout(`plotly-3d-chart-` + this.index, {
  374. "scene.camera": {
  375. up: {
  376. x: 0.200292643688136,
  377. y: 0.2488259353493132,
  378. z: 0.947612004346693,
  379. },
  380. center: {
  381. x: -0.052807476121180814,
  382. y: 0.02451796399554085,
  383. z: -0.022911006648570736,
  384. },
  385. eye: {
  386. x: -2.126379643342493,
  387. y: -2.551422475965373,
  388. z: 1.0917667684145647,
  389. },
  390. projection: {
  391. type: "orthographic",
  392. },
  393. },
  394. "scene.aspectratio": {
  395. x: 2.2,
  396. y: 1.7,
  397. z: 1,
  398. },
  399. });
  400. },
  401. updateChartColor() {
  402. this.renderChart(); // 当配色方案或点大小发生变化时重新渲染图表
  403. },
  404. // 获取配色选项样式
  405. getOptionStyle(scheme) {
  406. return {
  407. background: `linear-gradient(to right, ${scheme
  408. .slice(0, 8)
  409. .join(", ")})`,
  410. color: "#fff",
  411. height: "30px",
  412. lineHeight: "30px",
  413. borderRadius: "0px",
  414. };
  415. },
  416. },
  417. };
  418. </script>
  419. <style scoped>
  420. /* 样式可以根据需求自定义 */
  421. #plotly-3d-chart {
  422. width: 100%;
  423. height: 600px;
  424. }
  425. ::v-deep canvas {
  426. /* height: 400px !important; */
  427. }
  428. /* .js-plotly-plot .plotly,
  429. .js-plotly-plot .plotly div {
  430. background: #e5ecf6 !important;
  431. }
  432. #scene {
  433. background: #e5ecf6 !important;
  434. } */
  435. </style>