current-scatter-chart.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <template>
  2. <div class="chart" :id="id" :style="`width:${width}; height:${height}`"></div>
  3. </template>
  4. <script>
  5. import util from "@tools/util";
  6. import partten from "@tools/partten";
  7. import * as echarts from "echarts";
  8. import chartTheme from "./current-scatter-chart.json";
  9. export default {
  10. name: "currentScatterChart",
  11. props: {
  12. // 图表宽度
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. // 图表高度
  18. height: {
  19. type: String,
  20. default: "350px",
  21. },
  22. // 图表主标题
  23. chartTitle: {
  24. type: String,
  25. default: "自定义图表组件",
  26. },
  27. // X 轴配置项
  28. xAxisData: {
  29. type: Array,
  30. default: () => {
  31. return [];
  32. },
  33. },
  34. // Y 轴配置项
  35. yAxisData: {
  36. type: Array,
  37. default: () => {
  38. return [];
  39. },
  40. },
  41. dataSet: {
  42. type: String,
  43. default: "",
  44. },
  45. // 图表核心数据
  46. seriesData: {
  47. type: Array,
  48. default: () => {
  49. return [];
  50. },
  51. },
  52. // 是否显示图表图例
  53. showLegend: {
  54. type: Boolean,
  55. default: true,
  56. },
  57. // 是否默认采用笔刷模式
  58. brushSelected: {
  59. type: Boolean,
  60. default: false,
  61. },
  62. },
  63. data() {
  64. return {
  65. id: "",
  66. chart: null,
  67. theme: "light",
  68. color: [],
  69. linearGradientColor: [],
  70. chartData: [],
  71. markItem: {},
  72. };
  73. },
  74. computed: {
  75. collapse() {
  76. return this.$store.state.collapse;
  77. },
  78. },
  79. watch: {
  80. height() {
  81. if (this.chart) {
  82. this.chart.resize();
  83. }
  84. },
  85. collapse(val) {
  86. if (this.chart) {
  87. setTimeout(() => {
  88. this.chart.resize();
  89. }, 300);
  90. }
  91. },
  92. },
  93. methods: {
  94. resize() {},
  95. setMarkItem(markItem) {
  96. this.markItem = markItem;
  97. },
  98. initChart(markItem = {}) {
  99. const that = this;
  100. // document.getElementById(this.id).removeAttribute("_echarts_instance_");
  101. that.chart = null;
  102. const theme = sessionStorage.getItem("theme") === "true" ? true : false;
  103. this.$nextTick(() => {
  104. let charYMin = 0;
  105. let charDataConcatArray = [];
  106. const splitNumber = 5;
  107. let leftPowerMax = 0;
  108. let fsMax = 0;
  109. let rightPowerMax = 0;
  110. that.chartData.forEach((ele) => {
  111. ele.emphasis = {
  112. focus: "none", // 取消高亮
  113. itemStyle: {
  114. color: null, // 不改变颜色
  115. borderColor: null, // 不改变边框颜色
  116. borderWidth: 0, // 不改变边框宽度
  117. shadowBlur: 0, // 不添加阴影
  118. shadowColor: null, // 不改变阴影颜色
  119. opacity: 1, // 不改变透明度
  120. },
  121. };
  122. ele.large = true; // 开启大数据模式
  123. ele.progressive = 300; // 每次渲染的数据点数量
  124. ele.progressiveChunkMode = "sequential"; // 顺序分批渲染
  125. ele.silent = true;
  126. if (ele.name === "偏差上限") {
  127. ele.areaStyle = that.linearGradientColor[0];
  128. ele.silent = true;
  129. } else if (ele.name === "偏差下限") {
  130. ele.areaStyle = that.linearGradientColor[1];
  131. ele.silent = true;
  132. }
  133. if (ele.name === "实发有功" && markItem.res) {
  134. this.markItem = markItem;
  135. ele.markPoint = {
  136. symbolSize: 36,
  137. itemStyle: {
  138. color: this.$store.state.theme ? "#eb4d4b" : "#ff7979", // 特殊颜色
  139. },
  140. emphasis: {
  141. focus: "none", // 取消高亮
  142. silent: true, // 禁用 markPoint 的所有鼠标事件
  143. itemStyle: {
  144. color: "#fff", // 不改变颜色
  145. borderColor: null, // 不改变边框颜色
  146. borderWidth: 0, // 不改变边框宽度
  147. shadowBlur: 0, // 不添加阴影
  148. shadowColor: null, // 不改变阴影颜色
  149. opacity: 1, // 不改变透明度
  150. },
  151. },
  152. blur: {
  153. itemStyle: {
  154. opacity: 1,
  155. },
  156. },
  157. data: [
  158. {
  159. name: `${markItem.date} 异常点`,
  160. value: ele.data[markItem.dataIndex],
  161. xAxis: markItem.dataIndex, // 使用索引作为 x 坐标
  162. yAxis: ele.data[markItem.dataIndex], // 使用实际值作为 y 坐标
  163. label: {
  164. formatter: markItem.res,
  165. position: "top",
  166. },
  167. },
  168. ],
  169. };
  170. }
  171. charDataConcatArray = charDataConcatArray.concat(ele.data);
  172. });
  173. if (that.chartData?.length) {
  174. for (let i = 0; i < charDataConcatArray.length; i++) {
  175. if (Number(charDataConcatArray[i]) < charYMin) {
  176. charYMin = Number(charDataConcatArray[i]);
  177. }
  178. }
  179. if (charYMin < 0) {
  180. charYMin = parseInt(charYMin + -1);
  181. } else {
  182. charYMin = 0;
  183. }
  184. let leftPowerData = [].concat(
  185. that.chartData[0].data,
  186. that.chartData[1].data,
  187. that.chartData[2].data,
  188. that.chartData[3].data,
  189. that.chartData[4].data,
  190. that.chartData[5].data
  191. );
  192. let fsData = that.chartData[6].data;
  193. let rightPowerData = that.chartData[5].data;
  194. for (let i = 0; i < leftPowerData.length; i++) {
  195. if (Number(leftPowerData[i]) > leftPowerMax) {
  196. leftPowerMax = Number(leftPowerData[i]);
  197. }
  198. }
  199. for (let i = 0; i < fsData.length; i++) {
  200. if (Number(fsData[i]) > fsMax) {
  201. fsMax = Number(fsData[i]);
  202. }
  203. }
  204. for (let i = 0; i < rightPowerData.length; i++) {
  205. if (Number(rightPowerData[i]) > rightPowerMax) {
  206. rightPowerMax = Number(rightPowerData[i]);
  207. }
  208. }
  209. }
  210. let myChart = echarts.init(document.getElementById(this.id));
  211. that.chart = myChart;
  212. //指定图表的配置项和数据
  213. const option = {
  214. //标题
  215. color: that.color,
  216. title: {
  217. text: that.chartTitle,
  218. right: 440,
  219. top: 4,
  220. textStyle: {
  221. fontSize: 14,
  222. color: theme ? "#000" : "#fff",
  223. },
  224. },
  225. //工具箱
  226. toolbox: {
  227. show: false,
  228. x: "right",
  229. position: [10, 10],
  230. // backgroundColor:'rgba(0,0,0,0.4)',
  231. borderColor: theme ? "#000" : "#fff",
  232. textStyle: {
  233. fontSize: util.vh(16),
  234. color: theme ? "#000" : "#fff",
  235. },
  236. iconStyle: {
  237. borderColor: theme ? "#000" : "#fff",
  238. },
  239. emphasis: {
  240. iconStyle: {
  241. borderColor: theme ? "#000" : "#fff",
  242. },
  243. },
  244. },
  245. tooltip: {
  246. trigger: "axis",
  247. axisPointer: {
  248. type: "cross",
  249. },
  250. formatter(data) {
  251. const dataIndex = data[0].dataIndex;
  252. let label = `<p><span style="display:inline-block;width:100%">${that.xAxisData[dataIndex]}</span>`;
  253. data.forEach((ele, index) => {
  254. label += `<p>
  255. <span style="background:${
  256. // that.color[ele.componentIndex]
  257. ele.color
  258. };margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:50%;over-flow:hidden;"></span>
  259. <span>${
  260. ele.seriesName
  261. }:<span style="font-weight: 700;margin-left:10px">${
  262. ele.value || 0
  263. }</span><span>${
  264. ele.seriesName === "平均风速" ? "m/s" : "MW"
  265. }</span>
  266. </span>
  267. </p>
  268. </p>`;
  269. });
  270. return label;
  271. },
  272. },
  273. brush: {
  274. seriesIndex: [2, 3],
  275. yAxisIndex: 0,
  276. transformable: true,
  277. throttleType: "debounce",
  278. throttleDelay: 1000,
  279. removeOnClick: true,
  280. brushType: "polygon",
  281. brushMode: "multiple",
  282. brushStyle: {
  283. borderWidth: 1,
  284. borderColor: "#ff2424",
  285. },
  286. },
  287. dataZoom: [
  288. {
  289. type: "inside", //图表下方的伸缩条
  290. show: false, //是否显示
  291. realtime: true, //拖动时,是否实时更新系列的视图
  292. start: 0, //伸缩条开始位置(1-100),可以随时更改
  293. end: 100, //伸缩条结束位置(1-100),可以随时更改
  294. },
  295. {
  296. type: "slider", //图表下方的伸缩条
  297. show: false, //是否显示
  298. realtime: true, //拖动时,是否实时更新系列的视图
  299. start: 0, //伸缩条开始位置(1-100),可以随时更改
  300. end: 100, //伸缩条结束位置(1-100),可以随时更改
  301. },
  302. ],
  303. textStyle: {
  304. fontSize: util.vh(16),
  305. color: theme ? "#fff" : "#000",
  306. rich: {
  307. a: {
  308. fontSize: 15,
  309. width: 110,
  310. color: theme ? "#000" : "#fff",
  311. },
  312. },
  313. },
  314. //图例-每一条数据的名字
  315. legend: {
  316. show: that.showLegend,
  317. // right: 170,
  318. right: 70,
  319. type: "scroll",
  320. top: "5",
  321. icon: "circle",
  322. itemWidth: 6,
  323. inactiveColor: theme ? "#000" : "#fff",
  324. textStyle: {
  325. color: theme ? "#000" : "#fff",
  326. fontSize: 12,
  327. },
  328. // formatter(name) {
  329. // return [`{a| ${name}}`].join(" ");
  330. // },
  331. },
  332. grid: {
  333. top: 58,
  334. left: 40,
  335. right: 48,
  336. bottom: 24,
  337. },
  338. //x轴
  339. xAxis: [
  340. {
  341. name: "",
  342. nameTextStyle: {
  343. color: theme ? "#000" : "#fff",
  344. },
  345. type: "category",
  346. boundaryGap: true,
  347. data: that.xAxisData || [],
  348. axisLabel: {
  349. formatter: "{value}",
  350. color: theme ? "#000" : "#fff",
  351. },
  352. splitLine: {
  353. show: false,
  354. },
  355. smooth: true,
  356. textStyle: {
  357. color: theme ? "#000" : "#fff",
  358. },
  359. },
  360. ],
  361. //y轴没有显式设置,根据值自动生成y轴
  362. yAxis: [
  363. {
  364. splitLine: {
  365. show: false,
  366. },
  367. position: "left",
  368. min: charYMin,
  369. max: leftPowerMax, // 动态设置最大值
  370. interval: parseInt((leftPowerMax - charYMin) / splitNumber),
  371. splitNumber,
  372. name: "MW",
  373. nameTextStyle: {
  374. color: theme ? "#000" : "#fff",
  375. },
  376. axisLabel: {
  377. color: theme ? "#000" : "#fff",
  378. },
  379. },
  380. {
  381. splitLine: {
  382. show: false,
  383. },
  384. min: 0,
  385. max: fsMax, // 动态设置最大值
  386. interval: parseInt(fsMax / splitNumber),
  387. splitNumber,
  388. name: "m/s",
  389. nameTextStyle: {
  390. color: theme ? "#000" : "#fff",
  391. },
  392. axisLabel: {
  393. color: theme ? "#000" : "#fff",
  394. // formatter(value) {
  395. // if (String(value).indexOf(".") !== -1) {
  396. // return "";
  397. // } else {
  398. // return parseInt(value);
  399. // }
  400. // },
  401. },
  402. position: "right",
  403. },
  404. // {
  405. // splitLine: {
  406. // show: false,
  407. // },
  408. // position: "left",
  409. // min: charYMin,
  410. // max: rightPowerMax, // 动态设置最大值
  411. // interval: parseInt((rightPowerMax - rightPowerMin) / splitNumber),
  412. // splitNumber,
  413. // name: "", // 另一条 MW 单位的Y轴,因为数值太大了所以单独用一条Y轴
  414. // nameTextStyle: {
  415. // color: theme ? "#000" : "#fff",
  416. // },
  417. // axisLabel: {
  418. // color: theme ? "#000" : "#fff",
  419. // },
  420. // },
  421. ],
  422. animation: true,
  423. dataset: that.dataSet.length ? JSON.parse(that.dataSet) : [],
  424. //数据-data是最终要显示的数据
  425. series: that.chartData,
  426. };
  427. that.resize = function () {
  428. myChart.resize();
  429. };
  430. window.addEventListener("resize", that.resize);
  431. if (markItem.res) {
  432. option.dataZoom = that.handleRowClick(markItem);
  433. }
  434. myChart.setOption(option);
  435. myChart.off("brushSelected");
  436. // myChart.on("brushSelected", (params) => {
  437. // that.$emit("getSelected", params.batch || []);
  438. // });
  439. myChart.off("legendselectchanged");
  440. });
  441. },
  442. changeColor() {
  443. if (this.$store.state.theme) {
  444. this.color = [
  445. "#67c23a",
  446. "rgb(242.5, 208.5, 157.5)",
  447. "#f56c6c",
  448. "rgb(135, 0, 157)",
  449. "rgb(116, 21, 219)",
  450. "#205081", // 添加的深蓝色
  451. "rgb(169, 89, 43)", // 添加的棕色
  452. ];
  453. this.linearGradientColor = [
  454. {
  455. opacity: 0.5,
  456. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  457. {
  458. offset: 0,
  459. color: "rgb(255, 0, 135)",
  460. },
  461. {
  462. offset: 1,
  463. color: "rgb(135, 0, 157)",
  464. },
  465. ]),
  466. },
  467. // {
  468. // opacity: 0.5,
  469. // color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  470. // {
  471. // offset: 0,
  472. // color: "rgb(55, 162, 255)",
  473. // },
  474. // {
  475. // offset: 1,
  476. // color: "rgb(116, 21, 219)",
  477. // },
  478. // ]),
  479. // },
  480. {
  481. opacity: 1,
  482. color: "#fff",
  483. },
  484. ];
  485. } else {
  486. this.color = [
  487. "#205081",
  488. "#e96767", //
  489. "#41a273", //
  490. "rgb(77, 119, 255)",
  491. "rgb(1, 191, 236)",
  492. "#f2c94c",
  493. "#7bbfdd", //
  494. ];
  495. this.linearGradientColor = [
  496. {
  497. opacity: 0.5,
  498. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  499. {
  500. offset: 0,
  501. color: "rgb(0, 221, 255)",
  502. },
  503. {
  504. offset: 1,
  505. color: "rgb(77, 119, 255)",
  506. },
  507. ]),
  508. },
  509. // {
  510. // opacity: 0.5,
  511. // color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  512. // {
  513. // offset: 0,
  514. // color: "rgb(128, 255, 165)",
  515. // },
  516. // {
  517. // offset: 1,
  518. // color: "rgb(1, 191, 236)",
  519. // },
  520. // ]),
  521. // },
  522. {
  523. opacity: 1,
  524. color: "#142446",
  525. },
  526. ];
  527. }
  528. },
  529. // 处理表格行点击事件
  530. handleRowClick(row) {
  531. const that = this;
  532. const index = row.dataIndex;
  533. const totalDataLength = that.chartData[1].data.length;
  534. // 计算点击点周围的缩放范围
  535. let zoomRange = 300; // 可视区域包含的数据点数量
  536. // let startIndex = Math.max(index - Math.floor(zoomRange / 2), 0);
  537. // let endIndex = Math.min(startIndex + zoomRange, totalDataLength);
  538. let startIndex = index - zoomRange / 2 < 0 ? 0 : index - zoomRange / 2;
  539. let endIndex =
  540. index + zoomRange / 2 > totalDataLength
  541. ? totalDataLength
  542. : index + zoomRange / 2;
  543. // 将百分比转换为相对于数据集的百分比
  544. let startPercent = parseInt((startIndex / totalDataLength) * 100);
  545. let endPercent = parseInt((endIndex / totalDataLength) * 100);
  546. return [
  547. {
  548. // type: "slider",
  549. type: "inside",
  550. show: false,
  551. realtime: true, //拖动时,是否实时更新系列的视图
  552. start: startPercent,
  553. end: endPercent,
  554. },
  555. ];
  556. },
  557. },
  558. created() {
  559. this.id = "chart-" + util.newGUID();
  560. this.changeColor();
  561. this.chartData = JSON.parse(JSON.stringify(this.seriesData));
  562. },
  563. mounted() {
  564. this.$el.style.width = this.width;
  565. this.$el.style.height = this.height;
  566. this.$nextTick(() => {
  567. this.initChart();
  568. });
  569. },
  570. updated() {
  571. let myChart = echarts.init(document.getElementById(this.id));
  572. myChart.dispose();
  573. this.$nextTick(() => {
  574. this.initChart(this.markItem);
  575. });
  576. },
  577. unmounted() {
  578. this.markItem = {};
  579. window.removeEventListener("resize", this.resize);
  580. },
  581. watch: {
  582. "$store.state.theme"() {
  583. this.changeColor();
  584. this.initChart(this.markItem);
  585. },
  586. seriesData(data) {
  587. this.chartData = JSON.parse(JSON.stringify(this.seriesData));
  588. },
  589. },
  590. };
  591. </script>
  592. <style lang="less">
  593. .chart {
  594. width: 100%;
  595. height: 100%;
  596. display: inline-block;
  597. }
  598. </style>