list-bar-chart2.vue 7.6 KB

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