list-bar-chart.vue 11 KB

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