health-bar-line-chart.vue 6.1 KB

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