single-bar-chart.vue 11 KB

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