area-line-chart.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. },
  203. // 显示 legend
  204. showLegend: {
  205. type: Boolean,
  206. default: true,
  207. },
  208. // 颜色
  209. color: {
  210. type: Array,
  211. default: () => ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
  212. },
  213. },
  214. data () {
  215. return {
  216. id: "",
  217. chart: null,
  218. };
  219. },
  220. computed: {
  221. legend () {
  222. let data = [];
  223. this.lineData.forEach((value, index) => {
  224. data.push(value.text);
  225. });
  226. return data;
  227. },
  228. xAxisData () {
  229. let data = [];
  230. if (this.lineData.length > 0)
  231. this.lineData[0].value.forEach((value, index) => {
  232. data.push(value.text);
  233. });
  234. return data;
  235. },
  236. areaChartData () {
  237. let data = [];
  238. for (var i = 0; i < this.areaData.length; i++) {
  239. let item = this.areaData[i];
  240. var color = item.color || item.state;
  241. data.push({
  242. name: item.name,
  243. value: [item.start, item.end, item.end - item.start],
  244. itemStyle: {
  245. normal: {
  246. color: color,
  247. },
  248. },
  249. exData: item,
  250. });
  251. }
  252. return data;
  253. },
  254. areaMax () {
  255. let max = 0;
  256. this.areaData.forEach((value) => {
  257. if (max < value.end) max = value.end;
  258. });
  259. return max;
  260. },
  261. },
  262. methods: {
  263. renderItem (params, api) {
  264. var start = api.coord([api.value(0)]);
  265. var end = api.coord([api.value(1)]);
  266. var height = api.size([0, 1])[1];
  267. var rectShape = echarts.graphic.clipRectByRect(
  268. {
  269. x: start[0],
  270. y: start[1] - height / 2,
  271. width: end[0] - start[0],
  272. height: height,
  273. },
  274. {
  275. x: params.coordSys.x,
  276. y: params.coordSys.y,
  277. width: params.coordSys.width,
  278. height: params.coordSys.height,
  279. }
  280. );
  281. return (
  282. rectShape && {
  283. type: "rect",
  284. transition: ["shape"],
  285. shape: rectShape,
  286. style: api.style(),
  287. }
  288. );
  289. },
  290. initChart () {
  291. console.log("units:",this.units)
  292. let that = this;
  293. let chart = echarts.init(this.$el);
  294. let option = {
  295. color: this.color,
  296. grid: {
  297. left: 40,
  298. right: 40,
  299. bottom: 40,
  300. top: 32,
  301. containLabel: true,
  302. },
  303. tooltip: {
  304. show: true,
  305. trigger: "axis",
  306. axisPointer: {
  307. type: "cross",
  308. },
  309. backgroundColor: "rgba(0,0,0,0.4)",
  310. borderColor: partten.getColor("gray"),
  311. textStyle: {
  312. color: "#fff",
  313. fontSize: 14,
  314. },
  315. },
  316. legend: {
  317. show: this.showLegend,
  318. data: this.legend,
  319. right: 120,
  320. icon: "ract",
  321. itemWidth: 8,
  322. itemHeight: 8,
  323. inactiveColor: partten.getColor("gray"),
  324. textStyle: {
  325. color: partten.getColor("grayl"),
  326. fontSize: 12,
  327. },
  328. },
  329. xAxis: [
  330. {
  331. type: "category",
  332. axisLabel: {
  333. color: partten.getColor("gray"),
  334. },
  335. axisLine: {
  336. show: false,
  337. },
  338. axisTick: {
  339. show: false,
  340. },
  341. data: this.xAxisData,
  342. },
  343. {
  344. show: false,
  345. min: 0,
  346. max: this.areaMax,
  347. axisLabel: {
  348. show: false,
  349. formatter: function (val) {
  350. return Math.max(0, val - 0) + " ms";
  351. },
  352. },
  353. },
  354. ],
  355. yAxis: [
  356. {
  357. type: "value",
  358. name: this.units[0],
  359. axisLabel: {
  360. formatter: "{value} ",
  361. color: partten.getColor("gray"),
  362. },
  363. axisLine: {
  364. type: "dashed",
  365. lineStyle: {
  366. color: partten.getColor("gray"),
  367. },
  368. width: 5,
  369. },
  370. axisTick: {
  371. show: false,
  372. },
  373. splitLine: {
  374. lineStyle: {
  375. type: "dashed",
  376. dashOffset: 10,
  377. color: partten.getColor("gray") + 80,
  378. },
  379. },
  380. },
  381. {
  382. data: [this.areaData[0].name],
  383. axisLabel: { show: false },
  384. },
  385. ],
  386. series: [],
  387. };
  388. // line data
  389. if (this.lineData.length > 0) {
  390. this.lineData.forEach((value, index) => {
  391. option.series.push({
  392. name: value.text,
  393. type: "line",
  394. data: value.value,
  395. smooth: true, //平滑展示
  396. yAxisIndex: 0,
  397. // lineStyle: {
  398. // color: partten.getColor("green"),
  399. // },
  400. // itemStyle: {
  401. // color: partten.getColor("green"),
  402. // },
  403. });
  404. });
  405. }
  406. // 区域
  407. if (this.areaData && this.areaData.length > 0) {
  408. option.series.push({
  409. type: "custom",
  410. renderItem: this.renderItem,
  411. yAxisIndex: 1,
  412. xAxisIndex: 1,
  413. itemStyle: {
  414. opacity: 0.2,
  415. },
  416. tooltip: {
  417. show: false,
  418. formatter: function (params) {
  419. return params.marker + params.name + ": " + params.value[2] + "s";
  420. },
  421. },
  422. encode: {
  423. x: [1, 2],
  424. y: 0,
  425. },
  426. data: this.areaChartData,
  427. });
  428. }
  429. chart.setOption(option);
  430. return chart;
  431. },
  432. },
  433. emits: {
  434. areaClick: null,
  435. },
  436. created () {
  437. this.id = "pie-chart-" + util.newGUID();
  438. },
  439. mounted () {
  440. this.$nextTick(() => {
  441. this.$el.style.width = this.width;
  442. this.$el.style.height = this.height;
  443. let that = this;
  444. let chart = this.initChart();
  445. chart.on("click", function(e, p) {
  446. if (e.seriesType == "custom") {
  447. that.$emit("areaClick", { data: e.data.exData });
  448. }
  449. });
  450. });
  451. },
  452. updated () {
  453. this.$nextTick(() => {
  454. this.initChart();
  455. });
  456. },
  457. };
  458. </script>
  459. <style lang="less">
  460. .chart {
  461. width: 100%;
  462. height: 100%;
  463. display: inline-block;
  464. }
  465. </style>