marker-line-chart.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. isChartClick:{
  13. type: Boolean,
  14. default: false,
  15. },
  16. width: {
  17. type: String,
  18. default: "100%",
  19. },
  20. height: {
  21. type: String,
  22. default: "13.889vh",
  23. },
  24. // 传入数据
  25. list: {
  26. type: Array,
  27. default: () => [
  28. {
  29. title: "绿线",
  30. value: [
  31. {
  32. text: "",
  33. value: 0,
  34. },
  35. {
  36. text: "0:00",
  37. value: 20,
  38. },
  39. {
  40. text: "10:00",
  41. value: 1,
  42. },
  43. {
  44. text: "11:00",
  45. value: 40,
  46. },
  47. {
  48. text: "12:00",
  49. value: 10,
  50. },
  51. {
  52. text: "13:00",
  53. value: 15,
  54. },
  55. {
  56. text: "14:00",
  57. value: 30,
  58. },
  59. {
  60. text: "15:00",
  61. value: 40,
  62. },
  63. {
  64. text: "",
  65. value: 10,
  66. },
  67. ],
  68. },
  69. {
  70. title: "黄线",
  71. value: [
  72. {
  73. text: "",
  74. value: 0,
  75. },
  76. {
  77. text: "0:00",
  78. value: 40,
  79. },
  80. {
  81. text: "10:00",
  82. value: 20,
  83. },
  84. {
  85. text: "11:00",
  86. value: 20,
  87. },
  88. {
  89. text: "12:00",
  90. value: 10,
  91. },
  92. {
  93. text: "13:00",
  94. value: 40,
  95. },
  96. {
  97. text: "14:00",
  98. value: 50,
  99. },
  100. {
  101. text: "15:00",
  102. value: 40,
  103. },
  104. {
  105. text: "",
  106. value: 10,
  107. },
  108. ],
  109. },
  110. ],
  111. },
  112. // 单位
  113. unit: {
  114. type: String,
  115. default: "MW",
  116. },
  117. showLegend: {
  118. type: Boolean,
  119. default: false,
  120. },
  121. },
  122. data() {
  123. return {
  124. id: "",
  125. chart: null,
  126. color: ["#05bb4c", "#f8de5b", "#4b55ae", "#fa8c16"],
  127. };
  128. },
  129. computed: {
  130. colorValue() {
  131. return partten.getColor(this.color);
  132. },
  133. datas() {
  134. return this.list.map((t) => {
  135. return t.value;
  136. });
  137. },
  138. legend() {
  139. return this.list.map((t) => {
  140. return t.title;
  141. });
  142. },
  143. xdata() {
  144. return this.list[0].value.map((t) => {
  145. return t.text;
  146. });
  147. },
  148. series() {
  149. let that = this;
  150. let result = [];
  151. this.list.forEach((value, index) => {
  152. result.push({
  153. name: value.title,
  154. type: "line",
  155. smooth: true,
  156. showSymbol: false,
  157. zlevel: index,
  158. lineStyle: {
  159. normal: {
  160. color: this.color[index],
  161. width: 1,
  162. },
  163. },
  164. markPoint: {},
  165. // index == 0
  166. // ? {
  167. // data: [
  168. // {
  169. // type: "average",
  170. // name: "保证功率",
  171. // symbolSize: 0,
  172. // label: {
  173. // offset: [0, -40],
  174. // formatter: function(param) {
  175. // return `{title|${param.name}}` + "\n" + `{value| ${param.value}${that.unit}}`;
  176. // },
  177. // backgroundColor: partten.getColor("green") + 33,
  178. // borderColor: partten.getColor("green"),
  179. // borderWidth: 0.5,
  180. // borderRadius: 2,
  181. // padding: 8,
  182. // rich: {
  183. // title: {
  184. // color: partten.getColor("green"),
  185. // fontSize: 12,
  186. // },
  187. // value: {
  188. // color: "#fff",
  189. // fontSize: 16,
  190. // padding: [12, 0, 0, -4],
  191. // },
  192. // },
  193. // },
  194. // },
  195. // ],
  196. // }
  197. // : {},
  198. yAxisIndex: value.yAxisIndex,
  199. data: value.value.map((t) => {
  200. return t.value;
  201. }),
  202. });
  203. });
  204. return result;
  205. },
  206. yAxis() {
  207. let result = [];
  208. result.push({
  209. type: "value",
  210. name: this.unit,
  211. axisLabel: {
  212. formatter: "{value}",
  213. fontSize: util.vh(14),
  214. },
  215. boundaryGap: false,
  216. //分格线
  217. splitLine: {
  218. show: true,
  219. lineStyle: {
  220. color: partten.getColor("gray"),
  221. type: "dashed",
  222. },
  223. },
  224. });
  225. return result;
  226. },
  227. },
  228. methods: {
  229. resize() {},
  230. initChart() {
  231. let that = this;
  232. const chart = echarts.init(this.$el);
  233. let option = {
  234. color: this.color,
  235. tooltip: {
  236. trigger: "axis",
  237. backgroundColor: "rgba(0,0,0,0.4)",
  238. borderColor: partten.getColor("gray"),
  239. textStyle: {
  240. color: "#fff",
  241. fontSize: util.vh(16),
  242. },
  243. },
  244. legend: {
  245. show: this.showLegend,
  246. data: this.legend,
  247. right: 56,
  248. icon: "circle",
  249. itemWidth: 6,
  250. inactiveColor: partten.getColor("gray"),
  251. textStyle: {
  252. color: partten.getColor("grayl"),
  253. fontSize: 12,
  254. },
  255. },
  256. grid: {
  257. top: 16,
  258. left: 32,
  259. right: 8,
  260. bottom: 24,
  261. },
  262. xAxis: [
  263. {
  264. type: "category",
  265. boundaryGap: false,
  266. axisLabel: {
  267. formatter: "{value}",
  268. textStyle: {
  269. color: partten.getColor("gray"),
  270. fontSize: util.vh(14),
  271. },
  272. },
  273. data: this.xdata,
  274. },
  275. ],
  276. yAxis: this.yAxis,
  277. series: this.series,
  278. };
  279. chart.clear();
  280. chart.setOption(option);
  281. if(this.isChartClick){
  282. chart.getZr().off("click");
  283. chart.getZr().on("click", (params) => {
  284. that.chartClick();
  285. });
  286. }
  287. this.resize = function() {
  288. chart.resize();
  289. };
  290. window.addEventListener("resize", this.resize);
  291. },
  292. chartClick(){
  293. this.$emit("chartClick");
  294. }
  295. },
  296. created() {
  297. this.id = "pie-chart-" + util.newGUID();
  298. },
  299. mounted() {
  300. this.$nextTick(() => {
  301. this.$el.style.width = this.width;
  302. this.$el.style.height = this.height;
  303. this.initChart();
  304. });
  305. },
  306. updated() {
  307. this.$nextTick(() => {
  308. this.initChart();
  309. });
  310. },
  311. unmounted() {
  312. window.removeEventListener("resize", this.resize);
  313. },
  314. };
  315. </script>
  316. <style lang="less">
  317. .chart {
  318. width: 100%;
  319. height: 100%;
  320. display: inline-block;
  321. }
  322. </style>