area-line-chart.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "multiple-bar-chart",
  10. componentName: "multiple-bar-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "800px",
  19. },
  20. lineData: {
  21. type: Array,
  22. default: () => [
  23. {
  24. text: "日发电量",
  25. value: [
  26. {
  27. text: "1",
  28. value: 1,
  29. },
  30. {
  31. text: "2",
  32. value: 2,
  33. },
  34. {
  35. text: "3",
  36. value: 1,
  37. },
  38. {
  39. text: "4",
  40. value: 3,
  41. },
  42. {
  43. text: "5",
  44. value: 3,
  45. },
  46. {
  47. text: "6",
  48. value: 3,
  49. },
  50. {
  51. text: "7",
  52. value: 3,
  53. },
  54. {
  55. text: "8",
  56. value: 3,
  57. },
  58. {
  59. text: "9",
  60. value: 3,
  61. },
  62. {
  63. text: "10",
  64. value: 3,
  65. },
  66. {
  67. text: "11",
  68. value: 3,
  69. },
  70. {
  71. text: "12",
  72. value: 3,
  73. },
  74. {
  75. text: "13",
  76. value: 3,
  77. },
  78. {
  79. text: "14",
  80. value: 3,
  81. },
  82. {
  83. text: "15",
  84. value: 3,
  85. },
  86. {
  87. text: "16",
  88. value: 3,
  89. },
  90. ],
  91. },
  92. {
  93. text: "上网电量",
  94. value: [
  95. {
  96. text: "1",
  97. value: 1,
  98. },
  99. {
  100. text: "2",
  101. value: 2,
  102. },
  103. {
  104. text: "3",
  105. value: 1,
  106. },
  107. {
  108. text: "4",
  109. value: 3,
  110. },
  111. {
  112. text: "5",
  113. value: 4,
  114. },
  115. {
  116. text: "6",
  117. value: 5,
  118. },
  119. {
  120. text: "7",
  121. value: 6,
  122. },
  123. {
  124. text: "8",
  125. value: 7,
  126. },
  127. {
  128. text: "9",
  129. value: 8,
  130. },
  131. {
  132. text: "10",
  133. value: 7,
  134. },
  135. {
  136. text: "11",
  137. value: 9,
  138. },
  139. {
  140. text: "12",
  141. value: 2,
  142. },
  143. {
  144. text: "13",
  145. value: 3,
  146. },
  147. {
  148. text: "14",
  149. value: 5,
  150. },
  151. {
  152. text: "15",
  153. value: 12,
  154. },
  155. {
  156. text: "16",
  157. value: 11,
  158. },
  159. ],
  160. },
  161. ],
  162. },
  163. areaData: {
  164. type: Array,
  165. default: () => [
  166. {
  167. name: "1",
  168. start: 0,
  169. end: 100,
  170. state: "green",
  171. },
  172. {
  173. name: "1",
  174. start: 100,
  175. end: 200,
  176. state: "red",
  177. },
  178. {
  179. name: "1",
  180. start: 200,
  181. end: 300,
  182. state: "yellow",
  183. },
  184. {
  185. name: "2",
  186. start: 300,
  187. end: 800,
  188. state: "green",
  189. },
  190. {
  191. name: "3",
  192. start: 800,
  193. end: 9000,
  194. state: "green",
  195. },
  196. ],
  197. },
  198. // 单位
  199. units: {
  200. type: Array,
  201. // default: () => ["健康趋势", "风机健康状态数量"],
  202. default: () => ["(实时功率)", "(风速)"],
  203. },
  204. // 显示 legend
  205. showLegend: {
  206. type: Boolean,
  207. default: true,
  208. },
  209. // 颜色
  210. color: {
  211. type: Array,
  212. default: () => [
  213. "#323E6F",
  214. "#1DA0D7",
  215. "#02BB4C",
  216. "#DB5520",
  217. "#EDB32F",
  218. "#EDEB2F",
  219. ],
  220. },
  221. },
  222. data() {
  223. return {
  224. id: "",
  225. chart: null,
  226. };
  227. },
  228. computed: {
  229. legend() {
  230. let data = [];
  231. this.lineData.forEach((value, index) => {
  232. data.push(value.text);
  233. });
  234. return data;
  235. },
  236. xAxisData() {
  237. let data = [];
  238. if (this.lineData.length > 0)
  239. this.lineData[0].value.forEach((value, index) => {
  240. data.push(value.text);
  241. });
  242. return data;
  243. },
  244. areaChartData() {
  245. let data = [];
  246. for (var i = 0; i < this.areaData.length; i++) {
  247. let item = this.areaData[i];
  248. var color = item.color || item.state;
  249. data.push({
  250. name: item.name,
  251. value: [item.start, item.end, item.end - item.start],
  252. itemStyle: {
  253. normal: {
  254. color: color,
  255. },
  256. },
  257. exData: item,
  258. });
  259. }
  260. return data;
  261. },
  262. areaMax() {
  263. let max = 0;
  264. this.areaData.forEach((value) => {
  265. if (max < value.end) max = value.end;
  266. });
  267. return max;
  268. },
  269. },
  270. methods: {
  271. renderItem(params, api) {
  272. var start = api.coord([api.value(0)]);
  273. var end = api.coord([api.value(1)]);
  274. var height = api.size([0, 1])[1];
  275. var rectShape = echarts.graphic.clipRectByRect(
  276. {
  277. x: start[0],
  278. y: start[1] - height / 2,
  279. width: end[0] - start[0],
  280. height: height,
  281. },
  282. {
  283. x: params.coordSys.x,
  284. y: params.coordSys.y,
  285. width: params.coordSys.width,
  286. height: params.coordSys.height,
  287. }
  288. );
  289. return (
  290. rectShape && {
  291. type: "rect",
  292. transition: ["shape"],
  293. shape: rectShape,
  294. style: api.style(),
  295. }
  296. );
  297. },
  298. initChart() {
  299. let that = this;
  300. let chart = echarts.init(this.$el);
  301. let option = {
  302. color: this.color,
  303. grid: {
  304. left: 40,
  305. right: 40,
  306. bottom: 40,
  307. top: 32,
  308. containLabel: true,
  309. },
  310. tooltip: {
  311. show: true,
  312. trigger: "axis",
  313. axisPointer: {
  314. type: "cross",
  315. },
  316. backgroundColor: "rgba(0,0,0,0.4)",
  317. borderColor: partten.getColor("gray"),
  318. textStyle: {
  319. color: "#fff",
  320. fontSize: 14,
  321. },
  322. },
  323. legend: {
  324. show: this.showLegend,
  325. data: this.legend,
  326. right: 120,
  327. icon: "ract",
  328. itemWidth: 8,
  329. itemHeight: 8,
  330. inactiveColor: partten.getColor("gray"),
  331. textStyle: {
  332. color: partten.getColor("grayl"),
  333. fontSize: 12,
  334. },
  335. },
  336. xAxis: [
  337. {
  338. type: "category",
  339. name: this.units[2],
  340. axisLabel: {
  341. color: partten.getColor("gray"),
  342. },
  343. // axisLine: {
  344. // show: false,
  345. // },
  346. axisLine: {
  347. type: "dashed",
  348. lineStyle: {
  349. color: partten.getColor("gray"),
  350. },
  351. width: 5,
  352. },
  353. axisTick: {
  354. show: false,
  355. },
  356. data: this.xAxisData,
  357. },
  358. {
  359. show: false,
  360. min: 0,
  361. max: this.areaMax,
  362. axisLabel: {
  363. show: false,
  364. formatter: function (val) {
  365. return Math.max(0, val - 0) + " ms";
  366. },
  367. },
  368. },
  369. ],
  370. yAxis: [
  371. {
  372. type: "value",
  373. },
  374. {
  375. type: "value",
  376. name: this.units[0],
  377. axisLabel: {
  378. formatter: "{value} ",
  379. color: partten.getColor("gray"),
  380. },
  381. axisLine: {
  382. type: "dashed",
  383. lineStyle: {
  384. color: partten.getColor("gray"),
  385. },
  386. width: 5,
  387. },
  388. axisTick: {
  389. show: false,
  390. },
  391. splitLine: {
  392. lineStyle: {
  393. type: "dashed",
  394. dashOffset: 10,
  395. color: partten.getColor("gray") + 80,
  396. },
  397. },
  398. },
  399. {
  400. data: [this.areaData[0].name],
  401. axisLabel: { show: false },
  402. },
  403. {
  404. name: "风速",
  405. type: "value",
  406. min: 0,
  407. max: 30,
  408. },
  409. ],
  410. series: [],
  411. };
  412. // line data
  413. if (this.lineData.length > 0) {
  414. this.lineData.forEach((value, index) => {
  415. option.series.push({
  416. name: value.text,
  417. type: "line",
  418. data: value.value,
  419. smooth: true, //平滑展示
  420. yAxisIndex: value.yAxisIndex,
  421. // lineStyle: {
  422. // color: partten.getColor("green"),
  423. // },
  424. // itemStyle: {
  425. // color: partten.getColor("green"),
  426. // },
  427. });
  428. });
  429. }
  430. // 区域
  431. if (this.areaData && this.areaData.length > 0) {
  432. option.series.push({
  433. type: "custom",
  434. renderItem: this.renderItem,
  435. yAxisIndex: 1,
  436. xAxisIndex: 1,
  437. itemStyle: {
  438. opacity: 0.2,
  439. },
  440. tooltip: {
  441. show: false,
  442. formatter: function (params) {
  443. return params.marker + params.name + ": " + params.value[2] + "s";
  444. },
  445. },
  446. encode: {
  447. x: [1, 2],
  448. y: 0,
  449. },
  450. data: this.areaChartData,
  451. });
  452. }
  453. chart.setOption(option);
  454. return chart;
  455. },
  456. },
  457. emits: {
  458. areaClick: null,
  459. },
  460. created() {
  461. this.id = "pie-chart-" + util.newGUID();
  462. },
  463. mounted() {
  464. this.$nextTick(() => {
  465. this.$el.style.width = this.width;
  466. this.$el.style.height = this.height;
  467. let that = this;
  468. let chart = this.initChart();
  469. chart.on("click", function (e, p) {
  470. if (e.seriesType == "custom") {
  471. that.$emit("areaClick", { data: e.data.exData });
  472. }
  473. });
  474. });
  475. },
  476. updated() {
  477. this.$nextTick(() => {
  478. this.initChart();
  479. });
  480. },
  481. };
  482. </script>
  483. <style lang="less">
  484. .chart {
  485. width: 100%;
  486. height: 100%;
  487. display: inline-block;
  488. }
  489. </style>