vertival-bar-line-chart.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // 单位
  45. units: {
  46. type: Array,
  47. default: () => ["", "风机健康状态数量"],
  48. },
  49. // 显示 legend
  50. showLegend: {
  51. type: Boolean,
  52. default: true,
  53. },
  54. // 颜色
  55. color: {
  56. type: Array,
  57. default: () => ["#05bb4c", "#4b55ae", "#e17e23", "#02BB4C", "#EDB32F", "#EDEB2F"],
  58. },
  59. },
  60. data() {
  61. return {
  62. id: "",
  63. chart: null,
  64. newbardata:null
  65. };
  66. },
  67. watch: {
  68. bardata: {
  69. handler(newValue, oldValue) {
  70. console.warn(newValue);
  71. this.newbardata = newValue;
  72. this.initChart();
  73. },
  74. deep: true,
  75. },
  76. },
  77. computed: {
  78. legend() {
  79. return this.newbardata.legend;
  80. },
  81. },
  82. methods: {
  83. initChart() {
  84. let chart = echarts.init(this.$el);
  85. let option = {
  86. color: this.color,
  87. grid: {
  88. left: 16,
  89. right: 16,
  90. bottom: 0,
  91. top: 32,
  92. containLabel: true,
  93. },
  94. tooltip: {
  95. trigger: "axis",
  96. backgroundColor: "rgba(0,0,0,0.4)",
  97. borderColor: partten.getColor("gray"),
  98. textStyle: {
  99. color: "#fff",
  100. fontSize: 14,
  101. },
  102. },
  103. legend: {
  104. show: this.showLegend,
  105. data: this.newbardata.legend,
  106. right: 120,
  107. icon: "ract",
  108. itemWidth: 8,
  109. itemHeight: 8,
  110. inactiveColor: partten.getColor("gray"),
  111. textStyle: {
  112. color: partten.getColor("grayl"),
  113. fontSize: 12,
  114. },
  115. },
  116. xAxis: [
  117. {
  118. type: "category",
  119. axisLabel: {
  120. color: partten.getColor("gray"),
  121. },
  122. axisLine: {
  123. show: false,
  124. },
  125. axisTick: {
  126. show: false,
  127. },
  128. data: this.newbardata.area,
  129. },
  130. ],
  131. yAxis: [
  132. {
  133. type: "value",
  134. name: this.units[0],
  135. axisLabel: {
  136. formatter: "{value} ",
  137. color: partten.getColor("gray"),
  138. },
  139. axisLine: {
  140. type: "dashed",
  141. lineStyle: {
  142. color: partten.getColor("gray"),
  143. },
  144. width: 5,
  145. },
  146. axisTick: {
  147. show: false,
  148. },
  149. splitLine: {
  150. lineStyle: {
  151. type: "dashed",
  152. dashOffset: 10,
  153. color: partten.getColor("gray") + 80,
  154. },
  155. },
  156. },
  157. {
  158. type: "value",
  159. name: this.units[1],
  160. axisLabel: {
  161. formatter: "{value} ",
  162. color: partten.getColor("gray"),
  163. align: "left",
  164. },
  165. axisLine: {
  166. show: false,
  167. },
  168. axisTick: {
  169. show: false,
  170. },
  171. splitLine: {
  172. show: false,
  173. },
  174. },
  175. ],
  176. series: [],
  177. };
  178. // line data
  179. // if (this.lineData.length > 0) {
  180. // option.series.push({
  181. // name: this.units[0],
  182. // type: "line",
  183. // data: this.lineData,
  184. // smooth: false, //平滑展示
  185. // yAxisIndex: 0,
  186. // lineStyle: {
  187. // // color: partten.getColor("green"),
  188. // color: "#323E6F",
  189. // },
  190. // itemStyle: {
  191. // // color: partten.getColor("green"),
  192. // color: "#323E6F",
  193. // },
  194. // });
  195. // }
  196. // bar data
  197. for (var i = 0; i < this.newbardata.legend.length; i++) {
  198. option.series.push({
  199. name: this.newbardata.legend[i],
  200. type: "bar",
  201. stack: "总量",
  202. yAxisIndex: 1,
  203. barWidth: "10%",
  204. label: {
  205. show: false,
  206. position: "insideRight",
  207. },
  208. data: this.newbardata.data[i],
  209. });
  210. }
  211. chart.setOption(option);
  212. },
  213. },
  214. created() {
  215. this.id = "pie-chart-" + util.newGUID();
  216. this.newbardata = this.bardata
  217. },
  218. mounted() {
  219. this.$nextTick(() => {
  220. this.$el.style.width = this.width;
  221. this.$el.style.height = this.height;
  222. this.initChart();
  223. });
  224. },
  225. updated() {
  226. this.$nextTick(() => {
  227. this.initChart();
  228. });
  229. },
  230. };
  231. </script>
  232. <style lang="less">
  233. .chart {
  234. width: 100%;
  235. height: 100%;
  236. display: inline-block;
  237. }
  238. </style>