normal-line-chart.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: "normal-line-chart",
  10. componentName: "normal-line-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "150px",
  19. },
  20. // 数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. title: "日发电量",
  26. yAxisIndex: 0,
  27. value: [
  28. {
  29. text: "1",
  30. value: 1,
  31. },
  32. {
  33. text: "2",
  34. value: 2,
  35. },
  36. {
  37. text: "3",
  38. value: 1,
  39. },
  40. {
  41. text: "4",
  42. value: 3,
  43. },
  44. {
  45. text: "5",
  46. value: 3,
  47. },
  48. {
  49. text: "6",
  50. value: 3,
  51. },
  52. {
  53. text: "7",
  54. value: 3,
  55. },
  56. {
  57. text: "8",
  58. value: 3,
  59. },
  60. {
  61. text: "9",
  62. value: 3,
  63. },
  64. {
  65. text: "10",
  66. value: 3,
  67. },
  68. {
  69. text: "11",
  70. value: 3,
  71. },
  72. {
  73. text: "12",
  74. value: 3,
  75. },
  76. {
  77. text: "13",
  78. value: 3,
  79. },
  80. {
  81. text: "14",
  82. value: 3,
  83. },
  84. {
  85. text: "15",
  86. value: 3,
  87. },
  88. {
  89. text: "16",
  90. value: 3,
  91. },
  92. ],
  93. },
  94. {
  95. title: "上网电量",
  96. yAxisIndex: 0,
  97. value: [
  98. {
  99. text: "1",
  100. value: 1,
  101. },
  102. {
  103. text: "2",
  104. value: 2,
  105. },
  106. {
  107. text: "3",
  108. value: 1,
  109. },
  110. {
  111. text: "4",
  112. value: 3,
  113. },
  114. ],
  115. },
  116. ],
  117. },
  118. // 单位
  119. units: {
  120. type: Array,
  121. default: () => ["(MW)"],
  122. },
  123. },
  124. data() {
  125. return {
  126. id: "",
  127. chart: null,
  128. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
  129. };
  130. },
  131. computed: {
  132. xdata() {
  133. return this.list[0].value.map((t) => {
  134. return t.text;
  135. });
  136. },
  137. yAxis() {
  138. let result = [];
  139. this.units.forEach((value, index) => {
  140. result.push({
  141. type: "value",
  142. name: value,
  143. axisLabel: {
  144. formatter: "{value}",
  145. fontSize: util.vh(12),
  146. },
  147. //分格线
  148. splitLine: {
  149. lineStyle: {
  150. color: "#999",
  151. type: "dashed",
  152. },
  153. },
  154. });
  155. });
  156. return result;
  157. },
  158. series() {
  159. let result = [];
  160. this.list.forEach((value, index) => {
  161. result.push({
  162. name: value.title,
  163. type: "line",
  164. smooth: true,
  165. zlevel: index,
  166. lineStyle: {
  167. normal: {
  168. color: this.color[index],
  169. },
  170. },
  171. yAxisIndex: value.yAxisIndex,
  172. data: value.value.map((t) => {
  173. return t.value;
  174. }),
  175. });
  176. });
  177. return result;
  178. },
  179. },
  180. methods: {
  181. hexToRgba(hex, opacity) {
  182. let rgbaColor = "";
  183. let reg = /^#[\da-f]{6}$/i;
  184. if (reg.test(hex)) {
  185. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  186. }
  187. return rgbaColor;
  188. },
  189. initChart() {
  190. let option = {
  191. color: this.color,
  192. tooltip: {
  193. trigger: "item",
  194. backgroundColor: "rgba(255,255,255,0.3)",
  195. textStyle: {
  196. color: "#fff",
  197. fontSize: util.vh(16),
  198. },
  199. formatter: function(param) {
  200. console.log(param);
  201. return param.name + "<br >" + param.marker + param.seriesName + ":" + param.value;
  202. },
  203. },
  204. grid: {
  205. top: util.vh(32),
  206. left: util.vh(40),
  207. right: util.vh(40),
  208. bottom: util.vh(24),
  209. },
  210. xAxis: [
  211. {
  212. type: "category",
  213. boundaryGap: false,
  214. axisLabel: {
  215. formatter: "{value}",
  216. fontSize: util.vh(12),
  217. textStyle: {
  218. color: partten.getColor("gray"),
  219. },
  220. },
  221. data: this.xdata,
  222. },
  223. ],
  224. yAxis: this.yAxis,
  225. series: this.series,
  226. };
  227. this.chart.setOption(option);
  228. },
  229. },
  230. created() {
  231. this.id = "pie-chart-" + util.newGUID();
  232. },
  233. mounted() {
  234. this.$el.style.width = this.width;
  235. this.$el.style.height = this.height;
  236. this.chart = echarts.init(this.$el);
  237. this.initChart();
  238. },
  239. updated() {
  240. this.initChart();
  241. },
  242. };
  243. </script>
  244. <style lang="less">
  245. .chart {
  246. width: 100%;
  247. height: 100%;
  248. display: inline-block;
  249. }
  250. </style>