bar-line-chart.vue 5.3 KB

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