lineCharts.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. import dayjs from "dayjs";
  9. export default {
  10. name: "double-line-chart",
  11. componentName: "double-line-chart",
  12. props: {
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. height: {
  18. type: String,
  19. default: "13.889vh",
  20. },
  21. // 传入数据
  22. list: {
  23. type: Array,
  24. default: () => [
  25. {
  26. title: "绿线",
  27. smooth: true,
  28. value: [
  29. {
  30. text: "",
  31. value: 0,
  32. },
  33. {
  34. text: "0:00",
  35. value: 20,
  36. },
  37. {
  38. text: "10:00",
  39. value: 1,
  40. },
  41. {
  42. text: "11:00",
  43. value: 40,
  44. },
  45. {
  46. text: "12:00",
  47. value: 10,
  48. },
  49. {
  50. text: "13:00",
  51. value: 15,
  52. },
  53. {
  54. text: "14:00",
  55. value: 30,
  56. },
  57. {
  58. text: "15:00",
  59. value: 40,
  60. },
  61. {
  62. text: "",
  63. value: 10,
  64. },
  65. ],
  66. },
  67. {
  68. title: "黄线",
  69. smooth: true,
  70. value: [
  71. {
  72. text: "",
  73. value: 0,
  74. },
  75. {
  76. text: "0:00",
  77. value: 40,
  78. },
  79. {
  80. text: "10:00",
  81. value: 20,
  82. },
  83. {
  84. text: "11:00",
  85. value: 20,
  86. },
  87. {
  88. text: "12:00",
  89. value: 10,
  90. },
  91. {
  92. text: "13:00",
  93. value: 40,
  94. },
  95. {
  96. text: "14:00",
  97. value: 50,
  98. },
  99. {
  100. text: "15:00",
  101. value: 40,
  102. },
  103. {
  104. text: "",
  105. value: 10,
  106. },
  107. ],
  108. },
  109. ],
  110. },
  111. // 单位
  112. unit: {
  113. type: String,
  114. default: "",
  115. },
  116. showLegend: {
  117. type: Boolean,
  118. default: false,
  119. },
  120. },
  121. data() {
  122. return {
  123. id: "",
  124. chart: null,
  125. color: ["#1C99FF", "#FF8700", "#3D54BE", "#fa8c16", "#1DA0D7", "#DD5044"],
  126. newlist: null,
  127. };
  128. },
  129. watch: {
  130. list: {
  131. handler(newValue, oldValue) {
  132. this.newlist = newValue;
  133. this.$nextTick(() => {
  134. this.newlist = this.list;
  135. this.initChart();
  136. });
  137. },
  138. deep: true,
  139. },
  140. "$store.state.themeName"() {
  141. this.initChart();
  142. },
  143. },
  144. computed: {
  145. colorValue() {
  146. return partten.getColor(this.color);
  147. },
  148. datas() {
  149. return this.newlist.map((t) => {
  150. return t.value;
  151. });
  152. },
  153. legend() {
  154. if (this.newlist) {
  155. return this.newlist.map((t) => {
  156. return t.title;
  157. });
  158. } else {
  159. return [];
  160. }
  161. },
  162. xdata() {
  163. let result = [];
  164. if (this.list && this.list.length > 0) {
  165. result = this.list[0].date.map((t) => {
  166. return dayjs(t).format("MM-DD");
  167. });
  168. }
  169. return result;
  170. },
  171. series() {
  172. let result = [];
  173. this.list.forEach((value, index) => {
  174. result.push({
  175. name: value.name || value.title,
  176. type: "line",
  177. smooth: value.smooth,
  178. showSymbol: true,
  179. zlevel: index,
  180. lineStyle: {
  181. normal: {
  182. color: this.color[index],
  183. width: 1,
  184. },
  185. },
  186. yAxisIndex: value.yAxisIndex || 0,
  187. data: value.children,
  188. });
  189. });
  190. return result;
  191. },
  192. yAxis() {
  193. let result = [];
  194. result.push({
  195. type: "value",
  196. name: this.unit,
  197. axisLabel: {
  198. formatter: "{value}",
  199. fontSize: util.vh(14),
  200. },
  201. boundaryGap: false,
  202. //分格线
  203. splitLine: {
  204. show: false,
  205. },
  206. });
  207. return result;
  208. },
  209. },
  210. methods: {
  211. resize() {},
  212. initChart() {
  213. const chart = echarts.init(this.$el);
  214. let option = {
  215. color: this.color,
  216. tooltip: {
  217. trigger: "axis",
  218. backgroundColor: "rgba(0,0,0,0.4)",
  219. borderColor: partten.getColor("gray"),
  220. textStyle: {
  221. color: "#fff",
  222. fontSize: util.vh(16),
  223. },
  224. },
  225. legend: {
  226. show: this.showLegend,
  227. data: this.legend,
  228. padding: [10, 10],
  229. right: 56,
  230. icon: "circle",
  231. itemWidth: 6,
  232. inactiveColor: partten.getColor("gray"),
  233. textStyle: {
  234. color: partten.getColor("grayl"),
  235. fontSize: 12,
  236. },
  237. },
  238. grid: {
  239. top: 56,
  240. left: 40,
  241. right: 15,
  242. bottom: 24,
  243. },
  244. xAxis: [
  245. {
  246. type: "category",
  247. boundaryGap: false,
  248. axisLabel: {
  249. formatter: "{value}",
  250. textStyle: {
  251. color: partten.getColor("gray"),
  252. fontSize: util.vh(10),
  253. },
  254. },
  255. data: this.xdata,
  256. },
  257. ],
  258. yAxis: this.yAxis,
  259. series: this.series,
  260. };
  261. chart.clear();
  262. chart.setOption(option);
  263. this.resize = function () {
  264. chart.resize();
  265. };
  266. window.addEventListener("resize", this.resize);
  267. },
  268. },
  269. created() {
  270. this.$nextTick(() => {
  271. this.id = "pie-chart-" + util.newGUID();
  272. });
  273. this.newlist = this.list;
  274. },
  275. mounted() {
  276. this.$nextTick(() => {
  277. this.$el.style.width = this.width;
  278. this.$el.style.height = this.height;
  279. this.initChart();
  280. });
  281. },
  282. updated() {
  283. this.$nextTick(() => {
  284. this.initChart();
  285. });
  286. },
  287. unmounted() {
  288. window.removeEventListener("resize", this.resize);
  289. },
  290. };
  291. </script>
  292. <style lang="less">
  293. .chart {
  294. width: 100%;
  295. height: 100%;
  296. display: inline-block;
  297. }
  298. </style>