normal-line-chart.vue 5.6 KB

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