multiple-hover-bar-chart.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. color: {
  194. type: Array,
  195. default: () => ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338"],
  196. },
  197. },
  198. data() {
  199. return {
  200. id: "",
  201. chart: null,
  202. };
  203. },
  204. computed: {
  205. xdata() {
  206. let result = [];
  207. if (this.list && this.list.length > 0 && this.list[0].value.length > 0) {
  208. result = this.list[0].value.map((t) => {
  209. return t.text;
  210. });
  211. }
  212. return result;
  213. },
  214. ydata() {
  215. let result = [];
  216. this.units.forEach((value, index) => {
  217. let data = null;
  218. if (index == 0) {
  219. data = {
  220. type: "value",
  221. name: value,
  222. axisLabel: {
  223. formatter: "{value} ",
  224. fontSize: 14,
  225. },
  226. //分格线
  227. splitLine: {
  228. lineStyle: {
  229. color: "#5a6162",
  230. type: "dashed",
  231. },
  232. },
  233. };
  234. } else {
  235. data = {
  236. type: "value",
  237. name: value,
  238. axisLabel: {
  239. formatter: "{value}",
  240. fontSize: 14,
  241. },
  242. //分格线
  243. splitLine: {
  244. show: false,
  245. },
  246. };
  247. }
  248. result.push(data);
  249. });
  250. return result;
  251. },
  252. series() {
  253. let result = [];
  254. if (this.list && this.list.length > 0) {
  255. this.list.forEach((value, index) => {
  256. result.push({
  257. name: value.title,
  258. type: "bar",
  259. yAxisIndex: value.yAxisIndex,
  260. data: value.value.map((t) => {
  261. return t.value;
  262. }),
  263. itemStyle: {
  264. color: partten.getColor("gray") + 50,
  265. },
  266. emphasis: {
  267. itemStyle: {
  268. color: this.color[index],
  269. },
  270. },
  271. });
  272. });
  273. }
  274. return result;
  275. },
  276. },
  277. emits: {
  278. tooltip: function(param, callback) {
  279. var color = ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"];
  280. var result = param[0].axisValue;
  281. param.forEach((value, index) => {
  282. 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;
  283. });
  284. callback(result);
  285. return true;
  286. },
  287. },
  288. methods: {
  289. initChart() {
  290. let that = this;
  291. let chart = echarts.init(this.$el);
  292. let option = {
  293. color: this.color,
  294. tooltip: {
  295. trigger: "axis",
  296. backgroundColor: "rgba(0,0,0,0.4)",
  297. textStyle: {
  298. color: "#fff",
  299. fontSize: 14,
  300. },
  301. },
  302. grid: {
  303. top: util.vh(32),
  304. left: util.vh(40),
  305. right: this.ydata.length > 1 ? util.vh(40) : util.vh(16),
  306. bottom: util.vh(24),
  307. },
  308. xAxis: [
  309. {
  310. type: "category",
  311. data: this.xdata,
  312. axisPointer: {
  313. type: "shadow",
  314. },
  315. axisLabel: {
  316. fontSize: 14,
  317. },
  318. },
  319. ],
  320. yAxis: this.ydata,
  321. series: this.series,
  322. };
  323. if (that.customerTooltip) {
  324. option.tooltip.formatter = function(param) {
  325. let result = "";
  326. let callback = function(value) {
  327. result = value;
  328. };
  329. that.$emit("tooltip", param, callback);
  330. return result;
  331. };
  332. }
  333. chart.setOption(option);
  334. },
  335. },
  336. created() {
  337. this.id = "pie-chart-" + util.newGUID();
  338. },
  339. mounted() {
  340. this.$nextTick(() => {
  341. this.$el.style.width = this.width;
  342. this.$el.style.height = this.height;
  343. this.initChart();
  344. });
  345. },
  346. updated() {
  347. this.$nextTick(() => {
  348. this.initChart();
  349. });
  350. },
  351. };
  352. </script>
  353. <style lang="less">
  354. .chart {
  355. width: 100%;
  356. height: 100%;
  357. display: inline-block;
  358. }
  359. </style>