arrow-line-chart.vue 6.0 KB

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