double-line-chart.vue 6.0 KB

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