double-line-chart.vue 6.5 KB

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