hover-bar-chart.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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: "13.889vh",
  19. },
  20. // 传入数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. text: "1",
  26. value: 1,
  27. },
  28. {
  29. text: "2",
  30. value: 2,
  31. },
  32. {
  33. text: "3",
  34. value: 1,
  35. },
  36. {
  37. text: "4",
  38. value: 3,
  39. },
  40. {
  41. text: "5",
  42. value: 3,
  43. },
  44. {
  45. text: "6",
  46. value: 3,
  47. },
  48. {
  49. text: "7",
  50. value: 3,
  51. },
  52. {
  53. text: "8",
  54. value: 3,
  55. },
  56. {
  57. text: "9",
  58. value: 3,
  59. },
  60. {
  61. text: "10",
  62. value: 3,
  63. },
  64. {
  65. text: "11",
  66. value: 3,
  67. },
  68. {
  69. text: "12",
  70. value: 3,
  71. },
  72. {
  73. text: "13",
  74. value: 3,
  75. },
  76. {
  77. text: "14",
  78. value: 3,
  79. },
  80. {
  81. text: "15",
  82. value: 3,
  83. },
  84. {
  85. text: "16",
  86. value: 3,
  87. },
  88. {
  89. text: "17",
  90. value: 3,
  91. },
  92. {
  93. text: "18",
  94. value: 3,
  95. },
  96. {
  97. text: "19",
  98. value: 3,
  99. },
  100. {
  101. text: "20",
  102. value: 3,
  103. },
  104. {
  105. text: "21",
  106. value: 3,
  107. },
  108. {
  109. text: "22",
  110. value: 3,
  111. },
  112. {
  113. text: "23",
  114. value: 3,
  115. },
  116. {
  117. text: "24",
  118. value: 3,
  119. },
  120. {
  121. text: "25",
  122. value: 3,
  123. },
  124. {
  125. text: "26",
  126. value: 3,
  127. },
  128. {
  129. text: "27",
  130. value: 3,
  131. },
  132. {
  133. text: "28",
  134. value: 3,
  135. },
  136. {
  137. text: "29",
  138. value: 3,
  139. },
  140. {
  141. text: "30",
  142. value: 3,
  143. },
  144. {
  145. text: "31",
  146. value: 3,
  147. },
  148. ],
  149. },
  150. },
  151. data() {
  152. return {
  153. id: "",
  154. chart: null,
  155. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
  156. };
  157. },
  158. computed: {
  159. colorValue() {
  160. return partten.getColor(this.color);
  161. },
  162. datas() {
  163. return this.list.map((t) => {
  164. return t.value;
  165. });
  166. },
  167. xdata() {
  168. return this.list.map((t) => {
  169. return t.text;
  170. });
  171. },
  172. },
  173. methods: {
  174. initChart() {
  175. let option = {
  176. color: this.color,
  177. tooltip: {
  178. trigger: "item",
  179. backgroundColor: "rgba(0,0,0,0.3)",
  180. textStyle: {
  181. color: "#fff",
  182. fontSize: util.vh(16),
  183. },
  184. formatter: function(param) {
  185. console.log(param);
  186. let marker = '<span style="display:inline-block;margin-right:0.370vh;border-radius:0.926vh;width:0.926vh;height:0.926vh;background-color:' + partten.getColor("green") + ';"></span>';
  187. return param.name + "<br >" + marker + param.seriesName + ":" + param.value;
  188. },
  189. },
  190. grid: {
  191. top: 28,
  192. left: 16,
  193. bottom: 16,
  194. right: 16,
  195. containLabel: true,
  196. },
  197. xAxis: [
  198. {
  199. type: "category",
  200. data: this.xdata,
  201. axisLabel: {
  202. fontSize: util.vh(14),
  203. },
  204. },
  205. ],
  206. yAxis: [
  207. {
  208. type: "value",
  209. name: "(万KWh)",
  210. //分格线
  211. splitLine: {
  212. lineStyle: {
  213. color: "#999",
  214. type: "dashed",
  215. },
  216. },
  217. axisLabel: {
  218. fontSize: util.vh(14),
  219. },
  220. },
  221. ],
  222. series: {
  223. name: "损失电量",
  224. type: "bar",
  225. data: this.datas,
  226. itemStyle: {
  227. color: partten.getColor("gray"),
  228. },
  229. emphasis: {
  230. itemStyle: {
  231. color: partten.getColor("green"),
  232. },
  233. },
  234. },
  235. };
  236. this.chart.setOption(option);
  237. },
  238. },
  239. created() {
  240. this.id = "pie-chart-" + util.newGUID();
  241. },
  242. mounted() {
  243. this.$el.style.width = this.width;
  244. this.$el.style.height = this.height;
  245. this.chart = echarts.init(this.$el);
  246. this.initChart();
  247. },
  248. updated() {
  249. this.initChart();
  250. },
  251. };
  252. </script>
  253. <style lang="less">
  254. .chart {
  255. width: 100%;
  256. height: 100%;
  257. display: inline-block;
  258. }
  259. </style>