vertival-bar-line-chart.vue 5.5 KB

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