simple-line-chart.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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: "double-line-chart",
  10. componentName: "double-line-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "50px",
  19. },
  20. title: {
  21. type: String,
  22. default: "风机",
  23. },
  24. // 传入数据
  25. data: {
  26. type: Array,
  27. default: () => {
  28. return [
  29. 0,
  30. 0,
  31. 0,
  32. 0,
  33. 0,
  34. 0,
  35. 0,
  36. 1,
  37. 0,
  38. 0,
  39. 0,
  40. 0,
  41. 0,
  42. 1,
  43. 0,
  44. 0,
  45. 0,
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. 0,
  51. 0,
  52. 0,
  53. 1,
  54. 0,
  55. 0,
  56. 0,
  57. 0,
  58. 0,
  59. 0,
  60. 0,
  61. 0,
  62. 0,
  63. 0,
  64. 0,
  65. 0,
  66. 0,
  67. 0,
  68. 0,
  69. 0,
  70. 0,
  71. 0,
  72. 0,
  73. 0,
  74. 0,
  75. 0,
  76. 0,
  77. 0,
  78. 0,
  79. 0,
  80. 0,
  81. 0,
  82. 0,
  83. 0,
  84. 0,
  85. 0,
  86. 0,
  87. 0,
  88. 0,
  89. 0,
  90. 0,
  91. 0,
  92. 0,
  93. 0,
  94. 0,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. 0,
  100. 0,
  101. 0,
  102. 0,
  103. 0,
  104. 0,
  105. 0,
  106. 0,
  107. 0,
  108. 0,
  109. 0,
  110. 0,
  111. 0,
  112. 0,
  113. 0,
  114. 0,
  115. 0,
  116. 0,
  117. 0,
  118. 0,
  119. 0,
  120. 0,
  121. 0,
  122. 0,
  123. 0,
  124. 0,
  125. ];
  126. },
  127. },
  128. // 单位
  129. color: {
  130. type: String,
  131. default: "green",
  132. },
  133. lineTitle:{
  134. type: Array,
  135. default: () => [],
  136. }
  137. },
  138. data() {
  139. return {
  140. id: "",
  141. chart: null,
  142. // 时间轴
  143. xData: [
  144. "00:00",
  145. "00:15",
  146. "00:30",
  147. "00:45",
  148. "01:00",
  149. "01:15",
  150. "01:30",
  151. "01:45",
  152. "02:00",
  153. "02:15",
  154. "02:30",
  155. "02:45",
  156. "03:00",
  157. "03:15",
  158. "03:30",
  159. "03:45",
  160. "04:00",
  161. "04:15",
  162. "04:30",
  163. "04:45",
  164. "05:00",
  165. "05:15",
  166. "05:30",
  167. "05:45",
  168. "06:00",
  169. "06:15",
  170. "06:30",
  171. "06:45",
  172. "07:00",
  173. "07:15",
  174. "07:30",
  175. "07:45",
  176. "08:00",
  177. "08:15",
  178. "08:30",
  179. "08:45",
  180. "09:00",
  181. "09:15",
  182. "09:30",
  183. "09:45",
  184. "10:00",
  185. "10:15",
  186. "10:30",
  187. "10:45",
  188. "11:00",
  189. "11:15",
  190. "11:30",
  191. "11:45",
  192. "12:00",
  193. "12:15",
  194. "12:30",
  195. "12:45",
  196. "13:00",
  197. "13:15",
  198. "13:30",
  199. "13:45",
  200. "14:00",
  201. "14:15",
  202. "14:30",
  203. "14:45",
  204. "15:00",
  205. "15:15",
  206. "15:30",
  207. "15:45",
  208. "16:00",
  209. "16:15",
  210. "16:30",
  211. "16:45",
  212. "17:00",
  213. "17:15",
  214. "17:30",
  215. "17:45",
  216. "18:00",
  217. "18:15",
  218. "18:30",
  219. "18:45",
  220. "19:00",
  221. "19:15",
  222. "19:30",
  223. "19:45",
  224. "20:00",
  225. "20:15",
  226. "20:30",
  227. "20:45",
  228. "21:00",
  229. "21:15",
  230. "21:30",
  231. "21:45",
  232. "22:00",
  233. "22:15",
  234. "22:30",
  235. "22:45",
  236. "23:00",
  237. "23:15",
  238. "23:30",
  239. "23:45",
  240. ],
  241. };
  242. },
  243. computed: {
  244. colorValue() {
  245. return partten.getColor(this.color);
  246. },
  247. },
  248. methods: {
  249. resize() {},
  250. initChart() {
  251. const chart = echarts.init(this.$el);
  252. let option = {
  253. title: {
  254. text: this.title,
  255. textStyle: {
  256. color: "#ffffffcc",
  257. fontSize: 14,
  258. },
  259. left: 16,
  260. top: 0,
  261. },
  262. tooltip: {
  263. trigger: "axis",
  264. backgroundColor: "rgba(0,0,0,0.4)",
  265. borderColor: partten.getColor("gray"),
  266. textStyle: {
  267. color: "#fff",
  268. fontSize: util.vh(16),
  269. },
  270. },
  271. grid: {
  272. top: 40,
  273. left: 16,
  274. right: 16,
  275. bottom: 32,
  276. },
  277. xAxis: [
  278. {
  279. type: "category",
  280. data: this.xData,
  281. axisLine: {
  282. show: false,
  283. lineStyle: {
  284. color: partten.getColor("gray"),
  285. },
  286. },
  287. axisLabel: {
  288. show: true,
  289. margin: 16,
  290. interval: 7,
  291. },
  292. axisTick: {
  293. show: false,
  294. lineStyle: {
  295. color: partten.getColor("gray"),
  296. },
  297. },
  298. },
  299. ],
  300. yAxis: [
  301. {
  302. type: "value",
  303. splitNumber: 1,
  304. min: 0,
  305. max: 1,
  306. axisLine: {
  307. show: false,
  308. },
  309. axisLabel: {
  310. show: false,
  311. },
  312. axisTick: {
  313. show: false,
  314. },
  315. splitLine: {
  316. lineStyle: {
  317. color: partten.getColor("gray"),
  318. },
  319. },
  320. },
  321. ],
  322. series: [
  323. {
  324. type: "line",
  325. data: this.data,
  326. showAllSymbol: true,
  327. symbol: "circle",
  328. symbolSize: 10,
  329. lineStyle: {
  330. normal: {
  331. color: this.colorValue,
  332. width: 1,
  333. },
  334. },
  335. label: {
  336. show: false,
  337. },
  338. itemStyle: {
  339. color: this.colorValue,
  340. shadowColor: this.colorValue,
  341. shadowBlur: 4,
  342. },
  343. },
  344. ],
  345. };
  346. chart.clear();
  347. chart.setOption(option);
  348. this.resize = function() {
  349. chart.resize();
  350. };
  351. window.addEventListener("resize", this.resize);
  352. },
  353. },
  354. created() {
  355. this.xData = this.lineTitle;
  356. this.$nextTick(()=>{
  357. this.id = "pie-chart-" + util.newGUID();
  358. });
  359. },
  360. mounted() {
  361. this.xData = this.lineTitle;
  362. this.$nextTick(() => {
  363. this.$el.style.width = this.width;
  364. this.$el.style.height = this.height;
  365. this.initChart();
  366. });
  367. },
  368. updated() {
  369. this.xData = this.lineTitle;
  370. this.$nextTick(() => {
  371. this.initChart();
  372. });
  373. },
  374. unmounted() {
  375. window.removeEventListener("resize", this.resize);
  376. },
  377. };
  378. </script>
  379. <style lang="less">
  380. .chart {
  381. width: 100%;
  382. height: 100%;
  383. display: inline-block;
  384. }
  385. </style>