current-scatter-chart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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><span>${
  219. ele.seriesName === "平均风速" ? "m/s" : "MW"
  220. }</span>
  221. </span>
  222. </p>
  223. </p>`;
  224. });
  225. return label;
  226. },
  227. },
  228. brush: {
  229. seriesIndex: [2, 3],
  230. yAxisIndex: 0,
  231. transformable: true,
  232. throttleType: "debounce",
  233. throttleDelay: 1000,
  234. removeOnClick: true,
  235. brushType: "polygon",
  236. brushMode: "multiple",
  237. brushStyle: {
  238. borderWidth: 1,
  239. borderColor: "#ff2424",
  240. },
  241. },
  242. dataZoom: [
  243. {
  244. type: "inside", //图表下方的伸缩条
  245. show: false, //是否显示
  246. realtime: true, //拖动时,是否实时更新系列的视图
  247. start: 0, //伸缩条开始位置(1-100),可以随时更改
  248. end: 100, //伸缩条结束位置(1-100),可以随时更改
  249. },
  250. {
  251. type: "slider", //图表下方的伸缩条
  252. show: false, //是否显示
  253. realtime: true, //拖动时,是否实时更新系列的视图
  254. start: 0, //伸缩条开始位置(1-100),可以随时更改
  255. end: 100, //伸缩条结束位置(1-100),可以随时更改
  256. },
  257. ],
  258. textStyle: {
  259. fontSize: util.vh(16),
  260. color: theme ? "#fff" : "#000",
  261. rich: {
  262. a: {
  263. fontSize: 15,
  264. width: 110,
  265. color: theme ? "#000" : "#fff",
  266. },
  267. },
  268. },
  269. //图例-每一条数据的名字
  270. legend: {
  271. show: that.showLegend,
  272. // right: 170,
  273. right: 70,
  274. type: "scroll",
  275. top: "5",
  276. icon: "circle",
  277. itemWidth: 6,
  278. inactiveColor: theme ? "#000" : "#fff",
  279. textStyle: {
  280. color: theme ? "#000" : "#fff",
  281. fontSize: 12,
  282. },
  283. // formatter(name) {
  284. // return [`{a| ${name}}`].join(" ");
  285. // },
  286. },
  287. grid: {
  288. top: 58,
  289. left: 40,
  290. right: 48,
  291. bottom: 24,
  292. },
  293. //x轴
  294. xAxis: [
  295. {
  296. name: "时间",
  297. nameTextStyle: {
  298. color: theme ? "#000" : "#fff",
  299. },
  300. type: "category",
  301. boundaryGap: true,
  302. data: that.xAxisData || [],
  303. axisLabel: {
  304. formatter: "{value}",
  305. color: theme ? "#000" : "#fff",
  306. },
  307. splitLine: {
  308. show: false,
  309. },
  310. smooth: true,
  311. textStyle: {
  312. color: theme ? "#000" : "#fff",
  313. },
  314. },
  315. ],
  316. //y轴没有显式设置,根据值自动生成y轴
  317. yAxis: [
  318. {
  319. splitLine: {
  320. show: false,
  321. },
  322. position: "left",
  323. min: 0,
  324. name: "MW",
  325. nameTextStyle: {
  326. color: theme ? "#000" : "#fff",
  327. },
  328. axisLabel: {
  329. color: theme ? "#000" : "#fff",
  330. },
  331. },
  332. {
  333. splitLine: {
  334. show: false,
  335. },
  336. position: "left",
  337. min: 0,
  338. name: "m/s",
  339. nameTextStyle: {
  340. color: theme ? "#000" : "#fff",
  341. },
  342. axisLabel: {
  343. color: theme ? "#000" : "#fff",
  344. },
  345. position: "right",
  346. },
  347. {
  348. splitLine: {
  349. show: false,
  350. },
  351. position: "left",
  352. min: 0,
  353. name: "", // 另一条 MW 单位的Y轴,因为数值太大了所以单独用一条Y轴
  354. nameTextStyle: {
  355. color: theme ? "#000" : "#fff",
  356. },
  357. axisLabel: {
  358. color: theme ? "#000" : "#fff",
  359. },
  360. },
  361. ],
  362. animation: true,
  363. dataset: that.dataSet.length ? JSON.parse(that.dataSet) : [],
  364. //数据-data是最终要显示的数据
  365. series: that.chartData,
  366. };
  367. that.resize = function () {
  368. myChart.resize();
  369. };
  370. window.addEventListener("resize", that.resize);
  371. if (markItem.res) {
  372. option.dataZoom = that.handleRowClick(markItem);
  373. }
  374. myChart.setOption(option);
  375. myChart.off("brushSelected");
  376. // myChart.on("brushSelected", (params) => {
  377. // that.$emit("getSelected", params.batch || []);
  378. // });
  379. myChart.off("legendselectchanged");
  380. });
  381. },
  382. changeColor() {
  383. if (this.$store.state.theme) {
  384. this.color = [
  385. "#67c23a",
  386. "rgb(242.5, 208.5, 157.5)",
  387. "#f56c6c",
  388. "rgb(135, 0, 157)",
  389. "rgb(116, 21, 219)",
  390. "#205081", // 添加的深蓝色
  391. "rgb(169, 89, 43)", // 添加的棕色
  392. ];
  393. this.linearGradientColor = [
  394. {
  395. opacity: 0.5,
  396. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  397. {
  398. offset: 0,
  399. color: "rgb(255, 0, 135)",
  400. },
  401. {
  402. offset: 1,
  403. color: "rgb(135, 0, 157)",
  404. },
  405. ]),
  406. },
  407. // {
  408. // opacity: 0.5,
  409. // color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  410. // {
  411. // offset: 0,
  412. // color: "rgb(55, 162, 255)",
  413. // },
  414. // {
  415. // offset: 1,
  416. // color: "rgb(116, 21, 219)",
  417. // },
  418. // ]),
  419. // },
  420. {
  421. opacity: 1,
  422. color: "#fff",
  423. },
  424. ];
  425. } else {
  426. this.color = [
  427. "#67c23a",
  428. "rgb(242.5, 208.5, 157.5)",
  429. "#f56c6c",
  430. "rgb(77, 119, 255)",
  431. "rgb(1, 191, 236)",
  432. "#f2c94c", // 添加的黄色
  433. "rgb(153, 102, 255)", // 添加的紫色
  434. ];
  435. this.linearGradientColor = [
  436. {
  437. opacity: 0.5,
  438. color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  439. {
  440. offset: 0,
  441. color: "rgb(0, 221, 255)",
  442. },
  443. {
  444. offset: 1,
  445. color: "rgb(77, 119, 255)",
  446. },
  447. ]),
  448. },
  449. // {
  450. // opacity: 0.5,
  451. // color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
  452. // {
  453. // offset: 0,
  454. // color: "rgb(128, 255, 165)",
  455. // },
  456. // {
  457. // offset: 1,
  458. // color: "rgb(1, 191, 236)",
  459. // },
  460. // ]),
  461. // },
  462. {
  463. opacity: 1,
  464. color: "rgb(33,34,35)",
  465. },
  466. ];
  467. }
  468. },
  469. // 处理表格行点击事件
  470. handleRowClick(row) {
  471. const that = this;
  472. const index = row.dataIndex;
  473. const totalDataLength = that.chartData[1].data.length;
  474. // 计算点击点周围的缩放范围
  475. let zoomRange = 300; // 可视区域包含的数据点数量
  476. // let startIndex = Math.max(index - Math.floor(zoomRange / 2), 0);
  477. // let endIndex = Math.min(startIndex + zoomRange, totalDataLength);
  478. let startIndex = index - zoomRange / 2 < 0 ? 0 : index - zoomRange / 2;
  479. let endIndex =
  480. index + zoomRange / 2 > totalDataLength
  481. ? totalDataLength
  482. : index + zoomRange / 2;
  483. // 将百分比转换为相对于数据集的百分比
  484. let startPercent = parseInt((startIndex / totalDataLength) * 100);
  485. let endPercent = parseInt((endIndex / totalDataLength) * 100);
  486. return [
  487. {
  488. // type: "slider",
  489. type: "inside",
  490. show: false,
  491. realtime: true, //拖动时,是否实时更新系列的视图
  492. start: startPercent,
  493. end: endPercent,
  494. },
  495. ];
  496. },
  497. },
  498. created() {
  499. this.id = "chart-" + util.newGUID();
  500. this.changeColor();
  501. this.chartData = JSON.parse(JSON.stringify(this.seriesData));
  502. },
  503. mounted() {
  504. this.$el.style.width = this.width;
  505. this.$el.style.height = this.height;
  506. this.initChart();
  507. },
  508. updated() {
  509. let myChart = echarts.init(document.getElementById(this.id));
  510. myChart.dispose();
  511. this.$nextTick(() => {
  512. this.initChart(this.markItem);
  513. });
  514. },
  515. unmounted() {
  516. this.markItem = {};
  517. window.removeEventListener("resize", this.resize);
  518. },
  519. watch: {
  520. "$store.state.theme"() {
  521. this.changeColor();
  522. this.initChart(this.markItem);
  523. },
  524. seriesData(data) {
  525. this.chartData = JSON.parse(JSON.stringify(this.seriesData));
  526. },
  527. },
  528. };
  529. </script>
  530. <style lang="less">
  531. .chart {
  532. width: 100%;
  533. height: 100%;
  534. display: inline-block;
  535. }
  536. </style>