marker-line-chart.vue 6.9 KB

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