double-line-chart.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. },
  114. data () {
  115. return {
  116. id: "",
  117. chart: null,
  118. color: ["#05bb4c", "#f8de5b", "#4b55ae", "#fa8c16"],
  119. };
  120. },
  121. computed: {
  122. colorValue () {
  123. return partten.getColor(this.color);
  124. },
  125. datas () {
  126. return this.list.map((t) => {
  127. return t.value;
  128. });
  129. },
  130. xdata () {
  131. return this.list[0].value.map((t) => {
  132. return t.text;
  133. });
  134. },
  135. series () {
  136. let result = [];
  137. this.list.forEach((value, index) => {
  138. result.push({
  139. name: value.title,
  140. type: "line",
  141. smooth: true,
  142. showSymbol: false,
  143. zlevel: index,
  144. lineStyle: {
  145. normal: {
  146. color: this.color[index],
  147. },
  148. },
  149. yAxisIndex: value.yAxisIndex,
  150. data: value.value.map((t) => {
  151. return t.value;
  152. }),
  153. });
  154. });
  155. return result;
  156. },
  157. yAxis () {
  158. let result = [];
  159. result.push({
  160. type: "value",
  161. name: this.unit,
  162. axisLabel: {
  163. formatter: "{value}",
  164. fontSize: util.vh(14),
  165. },
  166. boundaryGap: false,
  167. //分格线
  168. splitLine: {
  169. show: false,
  170. },
  171. });
  172. return result;
  173. },
  174. },
  175. methods: {
  176. hexToRgba (hex, opacity) {
  177. let rgbaColor = "";
  178. let reg = /^#[\da-f]{6}$/i;
  179. if (reg.test(hex)) {
  180. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  181. }
  182. return rgbaColor;
  183. },
  184. initChart () {
  185. let option = {
  186. color: this.color,
  187. tooltip: {
  188. trigger: "item",
  189. backgroundColor: "rgba(255,255,255,0.3)",
  190. textStyle: {
  191. color: "#fff",
  192. fontSize: util.vh(16),
  193. },
  194. formatter: function (param) {
  195. return param.name + "<br >" + param.marker + param.seriesName + ":" + param.value;
  196. },
  197. },
  198. grid: {
  199. top: 16,
  200. left: 32,
  201. right: 8,
  202. bottom: 24,
  203. },
  204. xAxis: [
  205. {
  206. type: "category",
  207. boundaryGap: false,
  208. axisLabel: {
  209. formatter: "{value}",
  210. textStyle: {
  211. color: "#333",
  212. fontSize: util.vh(14),
  213. },
  214. },
  215. data: this.xdata,
  216. },
  217. ],
  218. yAxis: this.yAxis,
  219. series: this.series,
  220. };
  221. this.chart.setOption(option);
  222. },
  223. },
  224. created () {
  225. this.$nextTick(() => {
  226. this.id = "pie-chart-" + util.newGUID();
  227. });
  228. },
  229. mounted () {
  230. this.$nextTick(() => {
  231. this.$el.style.width = this.width;
  232. this.$el.style.height = this.height;
  233. this.chart = echarts.init(this.$el);
  234. this.initChart();
  235. });
  236. },
  237. updated () {
  238. this.$nextTick(() => {
  239. this.initChart();
  240. });
  241. },
  242. };
  243. </script>
  244. <style lang="less">
  245. .chart {
  246. width: 100%;
  247. height: 100%;
  248. display: inline-block;
  249. }
  250. </style>