current-scatter-chart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. that.chartData.forEach((ele) => {
  105. ele.emphasis = {
  106. focus: "none", // 取消高亮
  107. itemStyle: {
  108. color: null, // 不改变颜色
  109. borderColor: null, // 不改变边框颜色
  110. borderWidth: 0, // 不改变边框宽度
  111. shadowBlur: 0, // 不添加阴影
  112. shadowColor: null, // 不改变阴影颜色
  113. opacity: 1, // 不改变透明度
  114. },
  115. };
  116. ele.large = true; // 开启大数据模式
  117. ele.progressive = 300; // 每次渲染的数据点数量
  118. ele.progressiveChunkMode = "sequential"; // 顺序分批渲染
  119. ele.silent = true;
  120. if (ele.name === "偏差上限") {
  121. ele.areaStyle = that.linearGradientColor[0];
  122. ele.silent = true;
  123. } else if (ele.name === "偏差下限") {
  124. ele.areaStyle = that.linearGradientColor[1];
  125. ele.silent = true;
  126. }
  127. if (ele.name === "实发有功" && markItem.res) {
  128. this.markItem = markItem;
  129. ele.markPoint = {
  130. symbolSize: 36,
  131. itemStyle: {
  132. color: this.$store.state.theme ? "#eb4d4b" : "#ff7979", // 特殊颜色
  133. },
  134. emphasis: {
  135. focus: "none", // 取消高亮
  136. silent: true, // 禁用 markPoint 的所有鼠标事件
  137. itemStyle: {
  138. color: "#fff", // 不改变颜色
  139. borderColor: null, // 不改变边框颜色
  140. borderWidth: 0, // 不改变边框宽度
  141. shadowBlur: 0, // 不添加阴影
  142. shadowColor: null, // 不改变阴影颜色
  143. opacity: 1, // 不改变透明度
  144. },
  145. },
  146. blur: {
  147. itemStyle: {
  148. opacity: 1,
  149. },
  150. },
  151. data: [
  152. {
  153. name: `${markItem.date} 异常点`,
  154. value: ele.data[markItem.dataIndex],
  155. xAxis: markItem.dataIndex, // 使用索引作为 x 坐标
  156. yAxis: ele.data[markItem.dataIndex], // 使用实际值作为 y 坐标
  157. label: {
  158. formatter: markItem.res,
  159. position: "top",
  160. },
  161. },
  162. ],
  163. };
  164. }
  165. });
  166. let myChart = echarts.init(document.getElementById(this.id));
  167. that.chart = myChart;
  168. //指定图表的配置项和数据
  169. const option = {
  170. //标题
  171. color: this.color,
  172. title: {
  173. text: that.chartTitle,
  174. right: 440,
  175. top: 4,
  176. textStyle: {
  177. fontSize: 14,
  178. color: theme ? "#000" : "#fff",
  179. },
  180. },
  181. //工具箱
  182. toolbox: {
  183. show: false,
  184. x: "right",
  185. position: [10, 10],
  186. // backgroundColor:'rgba(0,0,0,0.4)',
  187. borderColor: theme ? "#000" : "#fff",
  188. textStyle: {
  189. fontSize: util.vh(16),
  190. color: theme ? "#000" : "#fff",
  191. },
  192. iconStyle: {
  193. borderColor: theme ? "#000" : "#fff",
  194. },
  195. emphasis: {
  196. iconStyle: {
  197. borderColor: theme ? "#000" : "#fff",
  198. },
  199. },
  200. },
  201. tooltip: {
  202. trigger: "axis",
  203. axisPointer: {
  204. type: "cross",
  205. },
  206. formatter(data) {
  207. const dataIndex = data[0].dataIndex;
  208. let label = `<p><span style="display:inline-block;width:100%">${that.xAxisData[dataIndex]}</span>`;
  209. data.forEach((ele, index) => {
  210. label += `<p>
  211. <span style="background:${
  212. that.color[index]
  213. };margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:50%;over-flow:hidden;"></span>
  214. <span>${
  215. ele.seriesName
  216. }:<span style="font-weight: 700;margin-left:10px">${
  217. ele.value || 0
  218. }</span>
  219. </span>
  220. </p>
  221. </p>`;
  222. });
  223. return label;
  224. },
  225. },
  226. brush: {
  227. seriesIndex: [2, 3],
  228. yAxisIndex: 0,
  229. transformable: true,
  230. throttleType: "debounce",
  231. throttleDelay: 1000,
  232. removeOnClick: true,
  233. brushType: "polygon",
  234. brushMode: "multiple",
  235. brushStyle: {
  236. borderWidth: 1,
  237. borderColor: "#ff2424",
  238. },
  239. },
  240. dataZoom: [
  241. {
  242. type: "inside", //图表下方的伸缩条
  243. show: false, //是否显示
  244. realtime: true, //拖动时,是否实时更新系列的视图
  245. start: 0, //伸缩条开始位置(1-100),可以随时更改
  246. end: 100, //伸缩条结束位置(1-100),可以随时更改
  247. },
  248. {
  249. type: "slider", //图表下方的伸缩条
  250. show: false, //是否显示
  251. realtime: true, //拖动时,是否实时更新系列的视图
  252. start: 0, //伸缩条开始位置(1-100),可以随时更改
  253. end: 100, //伸缩条结束位置(1-100),可以随时更改
  254. },
  255. ],
  256. textStyle: {
  257. fontSize: util.vh(16),
  258. color: theme ? "#fff" : "#000",
  259. rich: {
  260. a: {
  261. fontSize: 15,
  262. width: 110,
  263. color: theme ? "#000" : "#fff",
  264. },
  265. },
  266. },
  267. //图例-每一条数据的名字
  268. legend: {
  269. show: that.showLegend,
  270. // right: 170,
  271. right: 70,
  272. type: "scroll",
  273. top: "5",
  274. icon: "circle",
  275. itemWidth: 6,
  276. inactiveColor: theme ? "#000" : "#fff",
  277. textStyle: {
  278. color: theme ? "#000" : "#fff",
  279. fontSize: 12,
  280. },
  281. // formatter(name) {
  282. // return [`{a| ${name}}`].join(" ");
  283. // },
  284. },
  285. grid: {
  286. top: 58,
  287. left: 40,
  288. right: 48,
  289. bottom: 24,
  290. },
  291. //x轴
  292. xAxis: [
  293. {
  294. name: "时间",
  295. nameTextStyle: {
  296. color: theme ? "#000" : "#fff",
  297. },
  298. type: "category",
  299. boundaryGap: true,
  300. data: that.xAxisData || [],
  301. axisLabel: {
  302. formatter: "{value}",
  303. color: theme ? "#000" : "#fff",
  304. },
  305. splitLine: {
  306. show: false,
  307. },
  308. smooth: true,
  309. textStyle: {
  310. color: theme ? "#000" : "#fff",
  311. },
  312. },
  313. ],
  314. //y轴没有显式设置,根据值自动生成y轴
  315. yAxis: [
  316. {
  317. splitLine: {
  318. show: false,
  319. },
  320. position: "left",
  321. min: 0,
  322. name: "MW",
  323. nameTextStyle: {
  324. color: theme ? "#000" : "#fff",
  325. },
  326. axisLabel: {
  327. color: theme ? "#000" : "#fff",
  328. },
  329. },
  330. {
  331. splitLine: {
  332. show: false,
  333. },
  334. position: "left",
  335. min: 0,
  336. name: "m/s",
  337. nameTextStyle: {
  338. color: theme ? "#000" : "#fff",
  339. },
  340. axisLabel: {
  341. color: theme ? "#000" : "#fff",
  342. },
  343. position: "right",
  344. },
  345. {
  346. splitLine: {
  347. show: false,
  348. },
  349. position: "left",
  350. min: 0,
  351. name: "", // 另一条 MW 单位的Y轴,因为数值太大了所以单独用一条Y轴
  352. nameTextStyle: {
  353. color: theme ? "#000" : "#fff",
  354. },
  355. axisLabel: {
  356. color: theme ? "#000" : "#fff",
  357. },
  358. },
  359. ],
  360. animation: true,
  361. dataset: that.dataSet.length ? JSON.parse(that.dataSet) : [],
  362. //数据-data是最终要显示的数据
  363. series: that.chartData,
  364. };
  365. that.resize = function () {
  366. myChart.resize();
  367. };
  368. window.addEventListener("resize", that.resize);
  369. if (markItem.res) {
  370. option.dataZoom = that.handleRowClick(markItem);
  371. }
  372. myChart.setOption(option);
  373. myChart.off("brushSelected");
  374. // myChart.on("brushSelected", (params) => {
  375. // that.$emit("getSelected", params.batch || []);
  376. // });
  377. myChart.off("legendselectchanged");
  378. });
  379. },
  380. changeColor() {
  381. if (this.$store.state.theme) {
  382. this.color = [
  383. "#67c23a",
  384. "rgb(242.5, 208.5, 157.5)",
  385. "#f56c6c",
  386. "rgb(135, 0, 157)",
  387. "rgb(116, 21, 219)",
  388. "#205081", // 添加的深蓝色
  389. "rgb(169, 89, 43)", // 添加的棕色
  390. ];
  391. this.linearGradientColor = [
  392. {
  393. opacity: 0.5,
  394. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  395. {
  396. offset: 0,
  397. color: "rgb(255, 0, 135)",
  398. },
  399. {
  400. offset: 1,
  401. color: "rgb(135, 0, 157)",
  402. },
  403. ]),
  404. },
  405. // {
  406. // opacity: 0.5,
  407. // color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  408. // {
  409. // offset: 0,
  410. // color: "rgb(55, 162, 255)",
  411. // },
  412. // {
  413. // offset: 1,
  414. // color: "rgb(116, 21, 219)",
  415. // },
  416. // ]),
  417. // },
  418. {
  419. opacity: 1,
  420. color: "#fff",
  421. },
  422. ];
  423. } else {
  424. this.color = [
  425. "#67c23a",
  426. "rgb(242.5, 208.5, 157.5)",
  427. "#f56c6c",
  428. "rgb(77, 119, 255)",
  429. "rgb(1, 191, 236)",
  430. "#f2c94c", // 添加的黄色
  431. "rgb(153, 102, 255)", // 添加的紫色
  432. ];
  433. this.linearGradientColor = [
  434. {
  435. opacity: 0.5,
  436. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  437. {
  438. offset: 0,
  439. color: "rgb(0, 221, 255)",
  440. },
  441. {
  442. offset: 1,
  443. color: "rgb(77, 119, 255)",
  444. },
  445. ]),
  446. },
  447. // {
  448. // opacity: 0.5,
  449. // color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  450. // {
  451. // offset: 0,
  452. // color: "rgb(128, 255, 165)",
  453. // },
  454. // {
  455. // offset: 1,
  456. // color: "rgb(1, 191, 236)",
  457. // },
  458. // ]),
  459. // },
  460. {
  461. opacity: 1,
  462. color: "rgb(33,34,35)",
  463. },
  464. ];
  465. }
  466. },
  467. // 处理表格行点击事件
  468. handleRowClick(row) {
  469. const that = this;
  470. const index = row.dataIndex;
  471. const totalDataLength = that.chartData[1].data.length;
  472. // 计算点击点周围的缩放范围
  473. let zoomRange = 300; // 可视区域包含的数据点数量
  474. // let startIndex = Math.max(index - Math.floor(zoomRange / 2), 0);
  475. // let endIndex = Math.min(startIndex + zoomRange, totalDataLength);
  476. let startIndex = index - zoomRange / 2 < 0 ? 0 : index - zoomRange / 2;
  477. let endIndex =
  478. index + zoomRange / 2 > totalDataLength
  479. ? totalDataLength
  480. : index + zoomRange / 2;
  481. // 将百分比转换为相对于数据集的百分比
  482. let startPercent = parseInt((startIndex / totalDataLength) * 100);
  483. let endPercent = parseInt((endIndex / totalDataLength) * 100);
  484. return [
  485. {
  486. // type: "slider",
  487. type: "inside",
  488. show: false,
  489. realtime: true, //拖动时,是否实时更新系列的视图
  490. start: startPercent,
  491. end: endPercent,
  492. },
  493. ];
  494. },
  495. },
  496. created() {
  497. this.id = "chart-" + util.newGUID();
  498. this.changeColor();
  499. this.chartData = JSON.parse(JSON.stringify(this.seriesData));
  500. },
  501. mounted() {
  502. this.$el.style.width = this.width;
  503. this.$el.style.height = this.height;
  504. this.initChart();
  505. },
  506. updated() {
  507. let myChart = echarts.init(document.getElementById(this.id));
  508. myChart.dispose();
  509. this.$nextTick(() => {
  510. this.initChart(this.markItem);
  511. });
  512. },
  513. unmounted() {
  514. this.markItem = {};
  515. window.removeEventListener("resize", this.resize);
  516. },
  517. watch: {
  518. "$store.state.theme"() {
  519. this.changeColor();
  520. this.initChart(this.markItem);
  521. },
  522. seriesData(data) {
  523. this.chartData = JSON.parse(JSON.stringify(this.seriesData));
  524. },
  525. },
  526. };
  527. </script>
  528. <style lang="less">
  529. .chart {
  530. width: 100%;
  531. height: 100%;
  532. display: inline-block;
  533. }
  534. </style>