current-scatter-chart.vue 16 KB

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