double-line-chart.vue 5.4 KB

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