current-scatter-chart.vue 14 KB

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