area-bar-chart.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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: "multiple-bar-chart",
  10. componentName: "multiple-bar-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "800px",
  19. },
  20. // 传入数据
  21. bardata: {
  22. type: Object,
  23. default: () => {
  24. return {
  25. area: ["风场1", "风场2", "风场3", "风场4", "风场5", "风场6", "风场7", "风场8", "风场9"],
  26. legend: ["实际电量", "计划检修损失", "非计划检修损失", "限电损失", "受累损失", "性能损失"],
  27. data: [
  28. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  29. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  30. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  31. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  32. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  33. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  34. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  35. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  36. ],
  37. };
  38. },
  39. },
  40. lineData: {
  41. type: Array,
  42. default: () => [[200, 350, 400, 500, 600, 700, 800, 900, 1200]],
  43. },
  44. areaData: {
  45. type: Array,
  46. default: () => [
  47. {
  48. name: "1",
  49. start: 0,
  50. end: 100,
  51. state: "green",
  52. },
  53. {
  54. name: "1",
  55. start: 100,
  56. end: 200,
  57. state: "red",
  58. },
  59. {
  60. name: "1",
  61. start: 200,
  62. end: 300,
  63. state: "yellow",
  64. },
  65. {
  66. name: "",
  67. start: 300,
  68. end: 400,
  69. state: "green",
  70. },
  71. ],
  72. },
  73. // 单位
  74. units: {
  75. type: Array,
  76. default: () => ["健康趋势", "风机健康状态数量"],
  77. },
  78. // 显示 legend
  79. showLegend: {
  80. type: Boolean,
  81. default: true,
  82. },
  83. // 颜色
  84. color: {
  85. type: Array,
  86. default: () => ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
  87. },
  88. },
  89. data() {
  90. return {
  91. id: "",
  92. chart: null,
  93. };
  94. },
  95. computed: {
  96. legend() {
  97. let data = this.bardata.legend;
  98. data.push(this.units[0]);
  99. return data;
  100. },
  101. areaChartData() {
  102. let data = [];
  103. for (var i = 0; i < this.areaData.length; i++) {
  104. let item = this.areaData[i];
  105. var color = item.color ? item.color : partten.getColor(item.state);
  106. data.push({
  107. name: item.name,
  108. value: [item.start, item.end, item.end - item.start],
  109. itemStyle: {
  110. normal: {
  111. color: color,
  112. },
  113. },
  114. exData: item,
  115. });
  116. }
  117. return data;
  118. },
  119. areaMax() {
  120. let max = 0;
  121. this.areaData.forEach((value) => {
  122. if (max < value.end) max = value.end;
  123. });
  124. return max;
  125. },
  126. },
  127. methods: {
  128. renderItem(params, api) {
  129. var start = api.coord([api.value(0)]);
  130. var end = api.coord([api.value(1)]);
  131. var height = api.size([0, 1])[1];
  132. var rectShape = echarts.graphic.clipRectByRect(
  133. {
  134. x: start[0],
  135. y: start[1] - height / 2,
  136. width: end[0] - start[0],
  137. height: height,
  138. },
  139. {
  140. x: params.coordSys.x,
  141. y: params.coordSys.y,
  142. width: params.coordSys.width,
  143. height: params.coordSys.height,
  144. }
  145. );
  146. return (
  147. rectShape && {
  148. type: "rect",
  149. transition: ["shape"],
  150. shape: rectShape,
  151. style: api.style(),
  152. }
  153. );
  154. },
  155. initChart() {
  156. let that = this;
  157. let chart = echarts.init(this.$el);
  158. let option = {
  159. color: this.color,
  160. grid: {
  161. left: 16,
  162. right: 16,
  163. bottom: 0,
  164. top: 32,
  165. containLabel: true,
  166. },
  167. tooltip: {
  168. show: true,
  169. trigger: "axis",
  170. axisPointer: {
  171. type: "line",
  172. },
  173. backgroundColor: "rgba(0,0,0,0.4)",
  174. borderColor: partten.getColor("gray"),
  175. textStyle: {
  176. color: "#fff",
  177. fontSize: 14,
  178. },
  179. },
  180. legend: {
  181. show: this.showLegend,
  182. data: this.bardata.legend,
  183. right: 120,
  184. icon: "ract",
  185. itemWidth: 8,
  186. itemHeight: 8,
  187. inactiveColor: partten.getColor("gray"),
  188. textStyle: {
  189. color: partten.getColor("grayl"),
  190. fontSize: 12,
  191. },
  192. },
  193. xAxis: [
  194. {
  195. type: "category",
  196. axisLabel: {
  197. color: partten.getColor("gray"),
  198. },
  199. axisLine: {
  200. show: false,
  201. },
  202. axisTick: {
  203. show: false,
  204. },
  205. data: this.bardata.area,
  206. },
  207. {
  208. show: false,
  209. min: 0,
  210. max: this.areaMax,
  211. axisLabel: {
  212. show: false,
  213. formatter: function(val) {
  214. return Math.max(0, val - 0) + " ms";
  215. },
  216. },
  217. },
  218. ],
  219. yAxis: [
  220. {
  221. type: "value",
  222. name: this.units[0],
  223. axisLabel: {
  224. formatter: "{value} ",
  225. color: partten.getColor("gray"),
  226. },
  227. axisLine: {
  228. type: "dashed",
  229. lineStyle: {
  230. color: partten.getColor("gray"),
  231. },
  232. width: 5,
  233. },
  234. axisTick: {
  235. show: false,
  236. },
  237. splitLine: {
  238. lineStyle: {
  239. type: "dashed",
  240. dashOffset: 10,
  241. color: partten.getColor("gray") + 80,
  242. },
  243. },
  244. },
  245. {
  246. type: "value",
  247. name: this.units[1],
  248. axisLabel: {
  249. formatter: "{value} ",
  250. color: partten.getColor("gray"),
  251. align: "left",
  252. },
  253. axisLine: {
  254. show: false,
  255. },
  256. axisTick: {
  257. show: false,
  258. },
  259. splitLine: {
  260. show: false,
  261. },
  262. },
  263. {
  264. data: [(this.areaData && this.areaData[0] && this.areaData[0].name) || ""],
  265. axisLabel: { show: false },
  266. splitLine: { show: false },
  267. },
  268. ],
  269. series: [],
  270. };
  271. // line data
  272. if (this.lineData.length) {
  273. this.lineData.forEach((ele, index) => {
  274. option.series.push({
  275. name: this.units[index] || this.units[0] || "",
  276. type: "line",
  277. data: ele,
  278. smooth: true, //平滑展示
  279. yAxisIndex: 0,
  280. lineStyle: {
  281. color: this.color[index],
  282. },
  283. itemStyle: {
  284. color: this.color[index],
  285. },
  286. });
  287. });
  288. }
  289. // bar data
  290. for (var i = 0; i < this.bardata.legend.length; i++) {
  291. option.series.push({
  292. name: this.bardata.legend[i],
  293. type: "bar",
  294. stack: "总量",
  295. yAxisIndex: 1,
  296. barWidth: "10%",
  297. label: {
  298. show: false,
  299. position: "insideRight",
  300. },
  301. data: this.bardata.data[i],
  302. });
  303. }
  304. // 区域
  305. if (this.areaData && this.areaData.length > 0) {
  306. option.series.push({
  307. type: "custom",
  308. renderItem: this.renderItem,
  309. yAxisIndex: 2,
  310. xAxisIndex: 1,
  311. itemStyle: {
  312. opacity: 0.2,
  313. },
  314. tooltip: {
  315. show: false,
  316. formatter: function(params) {
  317. return params.marker + params.name + ": " + params.value[2] + "s";
  318. },
  319. },
  320. encode: {
  321. x: [1, 2],
  322. y: 0,
  323. },
  324. data: this.areaChartData,
  325. });
  326. }
  327. chart.setOption(option);
  328. chart.on("click", function(e, p) {
  329. if (e.seriesType == "custom") {
  330. that.$emit("areaClick", { data: e.data.exData });
  331. }
  332. });
  333. },
  334. },
  335. emits: {
  336. areaClick: null,
  337. },
  338. created() {
  339. this.id = "pie-chart-" + util.newGUID();
  340. },
  341. mounted() {
  342. this.$nextTick(() => {
  343. this.$el.style.width = this.width;
  344. this.$el.style.height = this.height;
  345. this.initChart();
  346. });
  347. },
  348. updated() {
  349. this.$nextTick(() => {
  350. this.initChart();
  351. });
  352. },
  353. };
  354. </script>
  355. <style lang="less">
  356. .chart {
  357. width: 100%;
  358. height: 100%;
  359. display: inline-block;
  360. }
  361. </style>