double-line-chartold.vue 5.7 KB

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