double-line-chart.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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"],
  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. },
  140. computed: {
  141. colorValue() {
  142. return partten.getColor(this.color);
  143. },
  144. datas() {
  145. return this.newlist.map((t) => {
  146. return t.value;
  147. });
  148. },
  149. legend() {
  150. return this.newlist.map((t) => {
  151. return t.title;
  152. });
  153. },
  154. xdata() {
  155. return this.newlist[0].value.map((t) => {
  156. return t.text;
  157. });
  158. },
  159. series() {
  160. let result = [];
  161. this.newlist.forEach((value, index) => {
  162. result.push({
  163. name: value.title,
  164. type: "line",
  165. smooth: value.smooth,
  166. showSymbol: false,
  167. zlevel: index,
  168. lineStyle: {
  169. normal: {
  170. color: this.color[index],
  171. width: 1,
  172. },
  173. },
  174. yAxisIndex: value.yAxisIndex,
  175. data: value.value.map((t) => {
  176. return t.value;
  177. }),
  178. });
  179. });
  180. return result;
  181. },
  182. yAxis() {
  183. let result = [];
  184. result.push({
  185. type: "value",
  186. name: this.unit,
  187. axisLabel: {
  188. formatter: "{value}",
  189. fontSize: util.vh(14),
  190. },
  191. boundaryGap: false,
  192. //分格线
  193. splitLine: {
  194. show: false,
  195. },
  196. });
  197. return result;
  198. },
  199. },
  200. methods: {
  201. resize() {},
  202. initChart() {
  203. const chart = echarts.init(this.$el);
  204. let option = {
  205. color: this.color,
  206. tooltip: {
  207. trigger: "axis",
  208. backgroundColor: "rgba(0,0,0,0.4)",
  209. borderColor: partten.getColor("gray"),
  210. textStyle: {
  211. color: "#fff",
  212. fontSize: util.vh(16),
  213. },
  214. },
  215. legend: {
  216. show: this.showLegend,
  217. data: this.legend,
  218. right: 56,
  219. icon: "circle",
  220. itemWidth: 6,
  221. inactiveColor: partten.getColor("gray"),
  222. textStyle: {
  223. color: partten.getColor("grayl"),
  224. fontSize: 12,
  225. },
  226. },
  227. grid: {
  228. top: 16,
  229. left: 32,
  230. right: 8,
  231. bottom: 24,
  232. },
  233. xAxis: [
  234. {
  235. type: "category",
  236. boundaryGap: false,
  237. axisLabel: {
  238. formatter: "{value}",
  239. textStyle: {
  240. color: partten.getColor("gray"),
  241. fontSize: util.vh(14),
  242. },
  243. },
  244. data: this.xdata,
  245. },
  246. ],
  247. yAxis: this.yAxis,
  248. series: this.series,
  249. };
  250. chart.clear();
  251. chart.setOption(option);
  252. this.resize = function () {
  253. chart.resize();
  254. };
  255. window.addEventListener("resize", this.resize);
  256. },
  257. },
  258. created() {
  259. this.$nextTick(() => {
  260. this.id = "pie-chart-" + util.newGUID();
  261. });
  262. this.newlist = this.list;
  263. console.warn(this.list);
  264. console.warn(this.newlist);
  265. },
  266. mounted() {
  267. this.$nextTick(() => {
  268. this.$el.style.width = this.width;
  269. this.$el.style.height = this.height;
  270. this.initChart();
  271. });
  272. },
  273. updated() {
  274. this.$nextTick(() => {
  275. this.initChart();
  276. });
  277. },
  278. unmounted() {
  279. window.removeEventListener("resize", this.resize);
  280. },
  281. };
  282. </script>
  283. <style lang="less">
  284. .chart {
  285. width: 100%;
  286. height: 100%;
  287. display: inline-block;
  288. }
  289. </style>