list-bar-chart2-home.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <div>
  3. <div
  4. class="chart"
  5. v-for="index of list.length"
  6. :key="index"
  7. :id="id + index"
  8. ></div>
  9. </div>
  10. </template>
  11. <script>
  12. import util from "@/helper/util.js";
  13. import partten from "@/helper/partten.js";
  14. import * as echarts from "echarts";
  15. export default {
  16. name: "percent-pie",
  17. componentName: "percent-pie",
  18. props: {
  19. width: {
  20. type: String,
  21. default: "100%",
  22. },
  23. height: {
  24. type: String,
  25. default: "18.519vh",
  26. },
  27. // 传入数据
  28. list: {
  29. type: Array,
  30. default: () => [
  31. {
  32. name: "当日预测电量",
  33. value: 103.62,
  34. total: 200,
  35. },
  36. {
  37. name: "实际发电量",
  38. value: 98.62,
  39. total: 100,
  40. },
  41. {
  42. name: "当月预测电量",
  43. value: 113.27,
  44. total: 100,
  45. },
  46. {
  47. name: "实际发电量",
  48. value: 136.72,
  49. total: 100,
  50. },
  51. ],
  52. },
  53. total: {
  54. type: Number,
  55. default: 150,
  56. },
  57. colors: {
  58. type: Array,
  59. default: () => ["green", "purple"],
  60. },
  61. },
  62. data() {
  63. return {
  64. id: "",
  65. chart: null,
  66. firstAnimation: true,
  67. };
  68. },
  69. created() {
  70. },
  71. computed: {
  72. datas() {
  73. return this.list.map((t) => {
  74. return t.value;
  75. });
  76. },
  77. },
  78. methods: {
  79. resize() {},
  80. initChart(value, index) {
  81. var currColor = this.colors[ (value.color?value.color % 2 : index % 2) ];
  82. var $dom = document.getElementById(this.id + (index + 1));
  83. $dom.style.width = this.width;
  84. $dom.style.height = `calc(${this.height} / ${this.list.length} - 4px)`;
  85. let chart = echarts.init($dom);
  86. let option = {
  87. xAxis: {
  88. max: value.total,
  89. splitLine: {
  90. show: false,
  91. },
  92. axisLine: {
  93. show: false,
  94. },
  95. axisLabel: {
  96. show: false,
  97. },
  98. axisTick: {
  99. show: false,
  100. },
  101. },
  102. grid: {
  103. left: 16,
  104. top: 0, // 设置条形图的边s距
  105. right: 0,
  106. bottom: 0,
  107. containLabel: true,
  108. },
  109. yAxis: [
  110. {
  111. type: "category",
  112. inverse: true,
  113. data: [value],
  114. axisLine: {
  115. show: false,
  116. },
  117. axisTick: {
  118. show: false,
  119. },
  120. axisLabel: {
  121. show: false,
  122. },
  123. },
  124. ],
  125. series: [
  126. // 内
  127. {
  128. type: "bar",
  129. barWidth: 6,
  130. animation: this.firstAnimation,
  131. // legendHoverLink: false,
  132. // silent: true,
  133. itemStyle: {
  134. normal: {
  135. color: function (params) {
  136. return {
  137. type: "linear",
  138. x: 100,
  139. y: 10,
  140. x2: 1,
  141. y2: 0,
  142. colorStops: [
  143. {
  144. offset: 0,
  145. color: partten.getColor(currColor), // 0% 处的颜色
  146. },
  147. {
  148. offset: 1,
  149. color: partten.getColor(currColor), // 100% 处的颜色
  150. },
  151. ],
  152. };
  153. },
  154. shadowBlur: 10,
  155. shadowColor: "rgba(255, 255, 255, 0.30)",
  156. },
  157. },
  158. label: {
  159. normal: {
  160. show: true,
  161. position: [0, -20],
  162. formatter: function (param) {
  163. return param.data.value;
  164. },
  165. textStyle: {
  166. color: partten.getColor(currColor),
  167. // this.$store.state.themeName === "dark" ? "#ffffff" : "#000",
  168. fontSize: 12,
  169. },
  170. },
  171. },
  172. data: [value],
  173. z: 1,
  174. animationEasing: "elasticOut",
  175. },
  176. // 三角
  177. {
  178. type: "pictorialBar",
  179. symbolPosition: "end",
  180. animation: this.firstAnimation,
  181. data: [value.value],
  182. symbol: "triangle",
  183. symbolOffset: [0, -10],
  184. symbolSize: [5, 5],
  185. symbolRotate: 180,
  186. itemStyle: {
  187. normal: {
  188. borderWidth: 0,
  189. color: function (params) {
  190. return partten.getColor(currColor);
  191. },
  192. // shadowBlur: 2,
  193. // shadowColor: "rgba(255, 255, 255, 0.80)",
  194. },
  195. },
  196. },
  197. // 分隔
  198. {
  199. type: "pictorialBar",
  200. itemStyle: {
  201. normal: {
  202. color:
  203. this.$store.state.themeName === "dark" ? "#20314f" : "#000",
  204. },
  205. },
  206. animation: this.firstAnimation,
  207. symbolRepeat: "fixed",
  208. symbolMargin: 4,
  209. symbol: "rect",
  210. symbolClip: true,
  211. symbolSize: [1, 8],
  212. symbolPosition: "start",
  213. symbolOffset: [8, -1],
  214. symbolBoundingData: value.total,
  215. symbolRotate: -15,
  216. data: [value],
  217. z: 2,
  218. animationEasing: "elasticOut",
  219. },
  220. // 外边框
  221. {
  222. type: "pictorialBar",
  223. animation: this.firstAnimation,
  224. symbol: "rect",
  225. symbolBoundingData: value.total,
  226. itemStyle: {
  227. normal: {
  228. color: "none",
  229. },
  230. },
  231. label: {
  232. normal: {
  233. formatter: (params) => {
  234. return "{gm|}{f| " + params.data + "}";
  235. },
  236. rich: {
  237. f: {
  238. color:
  239. this.$store.state.themeName === "dark" ? "#fff" : "#000",
  240. fontSize: 14,
  241. lineHeight: 20,
  242. fontFamily: "Bicubik",
  243. },
  244. gm: {
  245. backgroundColor: partten.getColor(currColor),
  246. width: 4,
  247. height: 4,
  248. lineHeight: 20,
  249. verticalAlign: "middle",
  250. borderRadius: [50, 50, 50, 50],
  251. },
  252. },
  253. position: "right",
  254. distance: 8, // 向右偏移位置
  255. show: true,
  256. },
  257. },
  258. // data: [+value.total],
  259. },
  260. // 外框
  261. {
  262. type: "bar",
  263. animation: this.firstAnimation,
  264. name: "外框",
  265. barGap: "-120%", // 设置外框粗细
  266. data: [
  267. {
  268. value: value.total,
  269. itemStyle: {
  270. normal: {
  271. color: "transparent",
  272. borderColor: partten.getColor(currColor), // [, "#333"],
  273. borderWidth: 1, // 边框宽度
  274. // barBorderRadius: 0, //圆角半径
  275. opacity: 0.5,
  276. label: {
  277. // 标签显示位置
  278. show: false,
  279. position: "top", // insideTop 或者横向的 insideLeft
  280. },
  281. },
  282. },
  283. },
  284. ],
  285. barWidth: 9,
  286. },
  287. ],
  288. };
  289. chart.clear();
  290. chart.setOption(option);
  291. this.resize = function () {
  292. chart.resize();
  293. };
  294. window.removeEventListener("resize", this.resize);
  295. window.addEventListener("resize", this.resize);
  296. },
  297. handleElectricDetail(){}
  298. },
  299. created() {
  300. this.id = "pie-chart-" + util.newGUID();
  301. },
  302. mounted() {
  303. this.$nextTick(() => {
  304. this.$el.style.width = this.width;
  305. this.$el.style.height = this.height;
  306. this.list.forEach((value, index) => {
  307. this.initChart(value, index);
  308. });
  309. this.firstAnimation = false;
  310. });
  311. },
  312. updated() {
  313. this.$nextTick(() => {
  314. this.list.forEach((value, index) => {
  315. this.initChart(value, index);
  316. });
  317. });
  318. },
  319. unmounted() {
  320. window.removeEventListener("resize", this.resize);
  321. },
  322. watch: {
  323. "$store.state.themeName"() {
  324. this.list.forEach((value, index) => {
  325. this.initChart(value, index);
  326. });
  327. },
  328. },
  329. };
  330. </script>
  331. <style lang="less">
  332. .chart {
  333. width: 100%;
  334. height: 100%;
  335. display: inline-block;
  336. cursor: default;
  337. }
  338. </style>