marker-line-chart.vue 7.2 KB

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