vertival-bar-line-chart.vue 5.4 KB

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