area-bar-chart.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. },
  120. methods: {
  121. renderItem (params, api) {
  122. var start = api.coord([api.value(0)]);
  123. var end = api.coord([api.value(1)]);
  124. var height = api.size([0, 1])[1];
  125. var rectShape = echarts.graphic.clipRectByRect(
  126. {
  127. x: start[0],
  128. y: start[1] - height / 2,
  129. width: end[0] - start[0],
  130. height: height,
  131. },
  132. {
  133. x: params.coordSys.x,
  134. y: params.coordSys.y,
  135. width: params.coordSys.width,
  136. height: params.coordSys.height,
  137. }
  138. );
  139. return (
  140. rectShape && {
  141. type: "rect",
  142. transition: ["shape"],
  143. shape: rectShape,
  144. style: api.style(),
  145. }
  146. );
  147. },
  148. initChart () {
  149. let that = this;
  150. let chart = echarts.init(this.$el);
  151. let option = {
  152. color: this.color,
  153. grid: {
  154. left: 16,
  155. right: 16,
  156. bottom: 0,
  157. top: 32,
  158. containLabel: true,
  159. },
  160. tooltip: {
  161. show: true,
  162. trigger: "item",
  163. backgroundColor: "rgba(0,0,0,0.4)",
  164. borderColor: partten.getColor("gray"),
  165. textStyle: {
  166. color: "#fff",
  167. fontSize: 14,
  168. },
  169. },
  170. legend: {
  171. show: this.showLegend,
  172. data: this.bardata.legend,
  173. right: 120,
  174. icon: "ract",
  175. itemWidth: 8,
  176. itemHeight: 8,
  177. inactiveColor: partten.getColor("gray"),
  178. textStyle: {
  179. color: partten.getColor("grayl"),
  180. fontSize: 12,
  181. },
  182. },
  183. xAxis: [
  184. {
  185. type: "category",
  186. axisLabel: {
  187. color: partten.getColor("gray"),
  188. },
  189. axisLine: {
  190. show: false,
  191. },
  192. axisTick: {
  193. show: false,
  194. },
  195. data: this.bardata.area,
  196. },
  197. {
  198. min: 0,
  199. axisLabel: {
  200. show: false,
  201. formatter: function (val) {
  202. return Math.max(0, val - 0) + " ms";
  203. },
  204. },
  205. },
  206. ],
  207. yAxis: [
  208. {
  209. type: "value",
  210. name: this.units[0],
  211. axisLabel: {
  212. formatter: "{value} ",
  213. color: partten.getColor("gray"),
  214. },
  215. axisLine: {
  216. type: "dashed",
  217. lineStyle: {
  218. color: partten.getColor("gray"),
  219. },
  220. width: 5,
  221. },
  222. axisTick: {
  223. show: false,
  224. },
  225. splitLine: {
  226. lineStyle: {
  227. type: "dashed",
  228. dashOffset: 10,
  229. color: partten.getColor("gray") + 80,
  230. },
  231. },
  232. },
  233. {
  234. type: "value",
  235. name: this.units[1],
  236. axisLabel: {
  237. formatter: "{value} ",
  238. color: partten.getColor("gray"),
  239. align: "left",
  240. },
  241. axisLine: {
  242. show: false,
  243. },
  244. axisTick: {
  245. show: false,
  246. },
  247. splitLine: {
  248. show: false,
  249. },
  250. },
  251. {
  252. data: [(this.areaData && this.areaData[0] && this.areaData[0].name || "")],
  253. axisLabel: { show: false },
  254. },
  255. ],
  256. series: [],
  257. };
  258. // line data
  259. if (this.lineData.length) {
  260. this.lineData.forEach((ele, index) => {
  261. option.series.push({
  262. name: this.units[index] || this.units[0] || "",
  263. type: "line",
  264. data: ele,
  265. smooth: true, //平滑展示
  266. yAxisIndex: 0,
  267. lineStyle: {
  268. color: this.color[index]
  269. },
  270. itemStyle: {
  271. color: this.color[index]
  272. }
  273. });
  274. });
  275. }
  276. // bar data
  277. for (var i = 0; i < this.bardata.legend.length; i++) {
  278. option.series.push({
  279. name: this.bardata.legend[i],
  280. type: "bar",
  281. stack: "总量",
  282. yAxisIndex: 1,
  283. barWidth: "10%",
  284. label: {
  285. show: false,
  286. position: "insideRight",
  287. },
  288. data: this.bardata.data[i],
  289. });
  290. }
  291. // 区域
  292. if (this.areaData && this.areaData.length > 0) {
  293. option.series.push({
  294. type: "custom",
  295. renderItem: this.renderItem,
  296. yAxisIndex: 2,
  297. xAxisIndex: 1,
  298. itemStyle: {
  299. opacity: 0.2,
  300. },
  301. tooltip: {
  302. show: true,
  303. formatter: function (params) {
  304. return params.marker + params.name + ": " + params.value[2] + "s";
  305. },
  306. },
  307. encode: {
  308. x: [1, 2],
  309. y: 0,
  310. },
  311. data: this.areaChartData,
  312. });
  313. }
  314. chart.setOption(option);
  315. chart.on("click", function (e, p) {
  316. if (e.seriesType == "custom") {
  317. that.$emit("areaClick", { data: e.data.exData });
  318. }
  319. });
  320. },
  321. },
  322. emits: {
  323. areaClick: null,
  324. },
  325. created () {
  326. this.id = "pie-chart-" + util.newGUID();
  327. },
  328. mounted () {
  329. this.$nextTick(() => {
  330. this.$el.style.width = this.width;
  331. this.$el.style.height = this.height;
  332. this.initChart();
  333. });
  334. },
  335. updated () {
  336. this.$nextTick(() => {
  337. this.initChart();
  338. });
  339. },
  340. };
  341. </script>
  342. <style lang="less">
  343. .chart {
  344. width: 100%;
  345. height: 100%;
  346. display: inline-block;
  347. }
  348. </style>