list-bar-chart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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: "percent-pie",
  10. componentName: "percent-pie",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "18.519vh",
  19. },
  20. // 传入数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. name: "当日预测电量",
  26. value: 103.62,
  27. },
  28. {
  29. name: "实际发电量",
  30. value: 98.62,
  31. },
  32. {
  33. name: "当月预测电量",
  34. value: 113.27,
  35. },
  36. {
  37. name: "实际发电量",
  38. value: 136.72,
  39. },
  40. ],
  41. },
  42. total: {
  43. type: Number,
  44. default: 150,
  45. },
  46. colors: {
  47. type: Array,
  48. default: () => ["green", "purple"],
  49. },
  50. },
  51. data() {
  52. return {
  53. id: "",
  54. chart: null,
  55. };
  56. },
  57. computed: {
  58. datas() {
  59. return this.list.map((t) => {
  60. return t.value;
  61. });
  62. },
  63. },
  64. methods: {
  65. initChart() {
  66. let color1 = this.colors[0];
  67. let color2 = this.colors[1];
  68. let option = {
  69. xAxis: {
  70. max: this.total,
  71. splitLine: {
  72. show: false,
  73. },
  74. axisLine: {
  75. show: false,
  76. },
  77. axisLabel: {
  78. show: false,
  79. },
  80. axisTick: {
  81. show: false,
  82. },
  83. },
  84. grid: {
  85. left: util.vh(16),
  86. top: util.vh(16), // 设置条形图的边s距
  87. right: util.vh(80),
  88. bottom: 0,
  89. },
  90. yAxis: [
  91. {
  92. type: "category",
  93. inverse: true,
  94. data: this.list,
  95. axisLine: {
  96. show: false,
  97. },
  98. axisTick: {
  99. show: false,
  100. },
  101. axisLabel: {
  102. show: false,
  103. },
  104. },
  105. ],
  106. series: [
  107. // 内
  108. {
  109. type: "bar",
  110. barWidth: 10,
  111. // legendHoverLink: false,
  112. // silent: true,
  113. itemStyle: {
  114. normal: {
  115. color: function(params) {
  116. var color;
  117. if (params.dataIndex == 0 || params.dataIndex == 2) {
  118. color = {
  119. type: "linear",
  120. x: 0,
  121. y: 0,
  122. x2: 1,
  123. y2: 0,
  124. colorStops: [
  125. {
  126. offset: 0,
  127. color: partten.getColor(color1), // 0% 处的颜色
  128. },
  129. {
  130. offset: 1,
  131. color: partten.getColor(color1), // 100% 处的颜色
  132. },
  133. ],
  134. };
  135. } else if (params.dataIndex == 1 || params.dataIndex == 3) {
  136. color = {
  137. type: "linear",
  138. x: 0,
  139. y: 0,
  140. x2: 1,
  141. y2: 0,
  142. colorStops: [
  143. {
  144. offset: 0,
  145. color: partten.getColor(color2), // 0% 处的颜色
  146. },
  147. {
  148. offset: 1,
  149. color: partten.getColor(color2), // 100% 处的颜色
  150. },
  151. ],
  152. };
  153. } else {
  154. color = {
  155. type: "linear",
  156. x: 0,
  157. y: 0,
  158. x2: 1,
  159. y2: 0,
  160. colorStops: [
  161. {
  162. offset: 0,
  163. color: partten.getColor(color1), // 0% 处的颜色
  164. },
  165. {
  166. offset: 1,
  167. color: partten.getColor(color1), // 100% 处的颜色
  168. },
  169. ],
  170. };
  171. }
  172. return color;
  173. },
  174. shadowBlur: 3,
  175. shadowColor: "rgba(255, 255, 255, 0.70)",
  176. },
  177. },
  178. label: {
  179. normal: {
  180. show: true,
  181. position: [0, util.vh("-20")],
  182. formatter: function(param) {
  183. return param.data.name;
  184. },
  185. textStyle: {
  186. color: "#7a8385",
  187. fontSize: util.vh("14"),
  188. },
  189. rich: {
  190. c1: {
  191. color: partten.getColor(color1),
  192. },
  193. c2: {
  194. color: partten.getColor(color2),
  195. },
  196. },
  197. },
  198. },
  199. data: this.list,
  200. z: 1,
  201. animationEasing: "elasticOut",
  202. },
  203. // 三角
  204. {
  205. type: "pictorialBar",
  206. symbolPosition: "end",
  207. data: this.list,
  208. symbol: "triangle",
  209. symbolOffset: [0, -16],
  210. symbolSize: [10, 10],
  211. symbolRotate: 180,
  212. itemStyle: {
  213. normal: {
  214. borderWidth: 0,
  215. color: function(params) {
  216. var color;
  217. if (params.dataIndex == 0 || params.dataIndex == 2) {
  218. color = partten.getColor(color1);
  219. } else if (params.dataIndex == 1 || params.dataIndex == 3) {
  220. color = partten.getColor(color2);
  221. } else {
  222. color = partten.getColor(color1);
  223. }
  224. return color;
  225. },
  226. shadowBlur: 2,
  227. shadowColor: "rgba(255, 255, 255, 0.80)",
  228. },
  229. },
  230. },
  231. // 分隔
  232. {
  233. type: "pictorialBar",
  234. itemStyle: {
  235. normal: {
  236. color: "#20314f",
  237. },
  238. },
  239. symbolRepeat: "fixed",
  240. symbolMargin: 6,
  241. symbol: "rect",
  242. symbolClip: true,
  243. symbolSize: [1, 10],
  244. symbolPosition: "start",
  245. symbolOffset: [10, -1],
  246. symbolBoundingData: this.total,
  247. symbolRotate: -15,
  248. data: this.list,
  249. z: 2,
  250. animationEasing: "elasticOut",
  251. },
  252. // 外边框
  253. {
  254. type: "pictorialBar",
  255. symbol: "rect",
  256. symbolBoundingData: this.total,
  257. itemStyle: {
  258. normal: {
  259. color: "none",
  260. },
  261. },
  262. label: {
  263. normal: {
  264. formatter: (params) => {
  265. var text;
  266. if (params.dataIndex == 0 || params.dataIndex == 2) {
  267. text = "{gm|}{f| " + params.data + "}";
  268. } else if (params.dataIndex == 1 || params.dataIndex == 3) {
  269. text = "{pm|}{f| " + params.data + "}";
  270. } else {
  271. text = "{gm|}{f| " + params.data + "}";
  272. }
  273. return text;
  274. },
  275. rich: {
  276. f: {
  277. color: "#ffffff",
  278. fontSize: util.vh("14"),
  279. lineHeight: util.vh(20),
  280. },
  281. gm: {
  282. backgroundColor: partten.getColor(color1),
  283. width: util.vh(8),
  284. height: util.vh(8),
  285. lineHeight: util.vh(20),
  286. verticalAlign: "middle",
  287. borderRadius: [50, 50, 50, 50],
  288. },
  289. pm: {
  290. backgroundColor: partten.getColor(color2),
  291. width: util.vh(8),
  292. height: util.vh(8),
  293. lineHeight: util.vh(20),
  294. verticalAlign: "middle",
  295. borderRadius: [50, 50, 50, 50],
  296. },
  297. },
  298. position: "right",
  299. distance: 8, // 向右偏移位置
  300. show: true,
  301. },
  302. },
  303. data: this.datas,
  304. },
  305. // 外框
  306. {
  307. type: "bar",
  308. name: "外框",
  309. barGap: "-120%", // 设置外框粗细
  310. data: [
  311. {
  312. value: this.total,
  313. itemStyle: {
  314. normal: {
  315. color: "transparent",
  316. borderColor: partten.getColor(color1), // [, "#333"],
  317. borderWidth: 1, // 边框宽度
  318. // barBorderRadius: 0, //圆角半径
  319. opacity: 0.5,
  320. label: {
  321. // 标签显示位置
  322. show: false,
  323. position: "top", // insideTop 或者横向的 insideLeft
  324. },
  325. },
  326. },
  327. },
  328. {
  329. value: this.total,
  330. itemStyle: {
  331. normal: {
  332. color: "transparent",
  333. borderColor: partten.getColor(color2), // [, "#333"],
  334. opacity: 0.5,
  335. borderWidth: 1, // 边框宽度
  336. // barBorderRadius: 0, //圆角半径
  337. label: {
  338. // 标签显示位置
  339. show: false,
  340. position: "top", // insideTop 或者横向的 insideLeft
  341. },
  342. },
  343. },
  344. },
  345. {
  346. value: this.total,
  347. itemStyle: {
  348. normal: {
  349. color: "transparent",
  350. borderColor: partten.getColor(color1), // [, "#333"],
  351. borderWidth: 1, // 边框宽度
  352. opacity: 0.5,
  353. // barBorderRadius: 0, //圆角半径
  354. label: {
  355. // 标签显示位置
  356. show: false,
  357. position: "top", // insideTop 或者横向的 insideLeft
  358. },
  359. },
  360. },
  361. },
  362. {
  363. value: this.total,
  364. itemStyle: {
  365. normal: {
  366. color: "transparent",
  367. borderColor: partten.getColor(color2), // [, "#333"],
  368. opacity: 0.7,
  369. borderWidth: 1, // 边框宽度
  370. // barBorderRadius: 0, //圆角半径
  371. label: {
  372. // 标签显示位置
  373. show: false,
  374. position: "top", // insideTop 或者横向的 insideLeft
  375. },
  376. },
  377. },
  378. },
  379. ],
  380. barWidth: 14,
  381. },
  382. ],
  383. };
  384. this.chart.setOption(option);
  385. },
  386. },
  387. created() {
  388. this.id = "pie-chart-" + util.newGUID();
  389. },
  390. mounted() {
  391. this.$nextTick(() => {
  392. this.$el.style.width = this.width;
  393. this.$el.style.height = this.height;
  394. this.chart = echarts.init(this.$el);
  395. this.initChart();
  396. });
  397. },
  398. updated() {
  399. this.initChart();
  400. },
  401. };
  402. </script>
  403. <style lang="less">
  404. .chart {
  405. width: 100%;
  406. height: 100%;
  407. display: inline-block;
  408. }
  409. </style>