double-line-chart.vue 5.4 KB

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