multiple-hover-bar-chart.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. title: "日发电量",
  26. yAxisIndex: 0,
  27. value: [
  28. {
  29. text: "1日",
  30. value: 1,
  31. },
  32. {
  33. text: "2日",
  34. value: 1,
  35. },
  36. {
  37. text: "3日",
  38. value: 1,
  39. },
  40. {
  41. text: "4日",
  42. value: 1,
  43. },
  44. {
  45. text: "5日",
  46. value: 1,
  47. },
  48. {
  49. text: "6日",
  50. value: 1,
  51. },
  52. {
  53. text: "7日",
  54. value: 1,
  55. },
  56. {
  57. text: "8日",
  58. value: 1,
  59. },
  60. {
  61. text: "9日",
  62. value: 1,
  63. },
  64. {
  65. text: "10日",
  66. value: 1,
  67. },
  68. {
  69. text: "11日",
  70. value: 1,
  71. },
  72. {
  73. text: "12日",
  74. value: 1,
  75. },
  76. ],
  77. },
  78. {
  79. title: "上网电量",
  80. yAxisIndex: 0,
  81. value: [
  82. {
  83. text: "1日",
  84. value: 1,
  85. },
  86. {
  87. text: "2日",
  88. value: 2,
  89. },
  90. {
  91. text: "3日",
  92. value: 1,
  93. },
  94. {
  95. text: "4日",
  96. value: 3,
  97. },
  98. {
  99. text: "5日",
  100. value: 3,
  101. },
  102. {
  103. text: "6日",
  104. value: 3,
  105. },
  106. {
  107. text: "7日",
  108. value: 3,
  109. },
  110. ],
  111. },
  112. {
  113. title: "购网电量",
  114. yAxisIndex: 0,
  115. value: [
  116. {
  117. text: "1日",
  118. value: 1,
  119. },
  120. {
  121. text: "2日",
  122. value: 2,
  123. },
  124. {
  125. text: "3日",
  126. value: 1,
  127. },
  128. {
  129. text: "4日",
  130. value: 3,
  131. },
  132. {
  133. text: "5日",
  134. value: 3,
  135. },
  136. {
  137. text: "6日",
  138. value: 3,
  139. },
  140. {
  141. text: "7日",
  142. value: 3,
  143. },
  144. ],
  145. },
  146. {
  147. title: "风速",
  148. yAxisIndex: 1,
  149. value: [
  150. {
  151. text: "1日",
  152. value: 1,
  153. },
  154. {
  155. text: "2日",
  156. value: 2,
  157. },
  158. {
  159. text: "3日",
  160. value: 1,
  161. },
  162. {
  163. text: "4日",
  164. value: 3,
  165. },
  166. {
  167. text: "5日",
  168. value: 3,
  169. },
  170. {
  171. text: "6日",
  172. value: 3,
  173. },
  174. {
  175. text: "7日",
  176. value: 3,
  177. },
  178. ],
  179. },
  180. ],
  181. },
  182. // 单位
  183. units: {
  184. type: Array,
  185. default: () => ["(万KWh)", "(风速)"],
  186. },
  187. // 自定义tooltip 显示内容
  188. customerTooltip: {
  189. type: Boolean,
  190. default: true,
  191. },
  192. },
  193. data() {
  194. return {
  195. id: "",
  196. chart: null,
  197. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
  198. };
  199. },
  200. computed: {
  201. xdata() {
  202. let result = [];
  203. if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
  204. result = this.list[0].value.map((t) => {
  205. return t.text;
  206. });
  207. }
  208. return result;
  209. },
  210. ydata() {
  211. let result = [];
  212. this.units.forEach((value, index) => {
  213. let data = null;
  214. if (index == 0) {
  215. data = {
  216. type: "value",
  217. name: value,
  218. axisLabel: {
  219. formatter: "{value} ",
  220. fontSize: util.vh(14),
  221. },
  222. //分格线
  223. splitLine: {
  224. lineStyle: {
  225. color: "#5a6162",
  226. type: "dashed",
  227. },
  228. },
  229. };
  230. } else {
  231. data = {
  232. type: "value",
  233. name: value,
  234. axisLabel: {
  235. formatter: "{value}",
  236. fontSize: util.vh(14),
  237. },
  238. //分格线
  239. splitLine: {
  240. show: false,
  241. },
  242. };
  243. }
  244. result.push(data);
  245. });
  246. return result;
  247. },
  248. series() {
  249. let result = [];
  250. if (this.list && this.list.length > 0) {
  251. this.list.forEach((value, index) => {
  252. result.push({
  253. name: value.title,
  254. type: "bar",
  255. yAxisIndex: value.yAxisIndex,
  256. data: value.value.map((t) => {
  257. return t.value;
  258. }),
  259. itemStyle: {
  260. color: partten.getColor("gray") + 50,
  261. },
  262. emphasis: {
  263. itemStyle: {
  264. color: this.color[index],
  265. },
  266. },
  267. });
  268. });
  269. }
  270. return result;
  271. },
  272. },
  273. emits: {
  274. tooltip: function(param, callback) {
  275. var color = ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"];
  276. var result = param[0].axisValue;
  277. param.forEach((value, index) => {
  278. result += "<br />" + `<span style="display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px;background-color:${color[index]};"></span>` + value.seriesName + ":" + value.value;
  279. });
  280. callback(result);
  281. return true;
  282. },
  283. },
  284. methods: {
  285. initChart() {
  286. let that = this;
  287. let chart = echarts.init(this.$el);
  288. let option = {
  289. color: this.color,
  290. tooltip: {
  291. trigger: "axis",
  292. backgroundColor: partten.getColor("gray") + "99",
  293. textStyle: {
  294. color: "#fff",
  295. fontSize: util.vh(16),
  296. },
  297. },
  298. grid: {
  299. top: util.vh(32),
  300. left: util.vh(40),
  301. right: this.ydata.length > 1 ? util.vh(40) : util.vh(16),
  302. bottom: util.vh(24),
  303. },
  304. xAxis: [
  305. {
  306. type: "category",
  307. data: this.xdata,
  308. axisPointer: {
  309. type: "shadow",
  310. },
  311. axisLabel: {
  312. fontSize: util.vh(14),
  313. },
  314. },
  315. ],
  316. yAxis: this.ydata,
  317. series: this.series,
  318. };
  319. if (that.customerTooltip) {
  320. option.tooltip.formatter = function(param) {
  321. let result = "";
  322. let callback = function(value) {
  323. result = value;
  324. };
  325. that.$emit("tooltip", param, callback);
  326. return result;
  327. };
  328. }
  329. chart.setOption(option);
  330. },
  331. },
  332. created() {
  333. this.id = "pie-chart-" + util.newGUID();
  334. },
  335. mounted() {
  336. this.$el.style.width = this.width;
  337. this.$el.style.height = this.height;
  338. this.initChart();
  339. },
  340. updated() {
  341. this.initChart();
  342. },
  343. };
  344. </script>
  345. <style lang="less">
  346. .chart {
  347. width: 100%;
  348. height: 100%;
  349. display: inline-block;
  350. }
  351. </style>