dz-chart.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <div class="dz_chart">
  3. <div ref="chartRef" class="chartRef"></div>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import { ref, reactive, watch, onMounted, nextTick } from 'vue'
  8. import * as echarts from 'echarts';
  9. import warnLevel1 from '@/assets/images/home-green/300.png'
  10. import warnLevel2 from '@/assets/images/home-green/400.png'
  11. import warnLevel3 from '@/assets/images/home-green/500.png'
  12. import warnLevel4 from '@/assets/images/home-green/600.png'
  13. import warnLevel5 from '@/assets/images/home-green/700.png'
  14. let props = defineProps({
  15. echartOption: {
  16. type: Object,
  17. default: () => {
  18. return {}
  19. }
  20. },
  21. echartData: {
  22. type: Array,
  23. default: () => {
  24. return []
  25. }
  26. }
  27. })
  28. //获取dom元素节点
  29. let chartRef = ref<any>();
  30. // 类别
  31. let category = ref<any[]>([])
  32. let echartY=ref<any[]>([])
  33. function getOption() {
  34. let myChart = echarts.init(chartRef.value);
  35. let option = {
  36. xAxis: {
  37. max: 10,
  38. splitLine: {
  39. show: false
  40. },
  41. axisLine: {
  42. show: false
  43. },
  44. axisLabel: {
  45. show: false
  46. },
  47. axisTick: {
  48. show: false
  49. },
  50. },
  51. grid: {
  52. left: 10,
  53. top: 10, // 设置条形图的边距
  54. right: 10,
  55. bottom: 0
  56. },
  57. yAxis: [{
  58. type: "category",
  59. inverse: false,
  60. data: category.value,
  61. axisLine: {
  62. show: false
  63. },
  64. axisTick: {
  65. show: false
  66. },
  67. axisLabel: {
  68. show: false
  69. },
  70. }],
  71. series: [
  72. {
  73. // 内
  74. type: "bar",
  75. barWidth: 18,
  76. silent: true,
  77. itemStyle: {
  78. normal: {
  79. color: function (param) {
  80. const colors = ['#ff0000', '#ff7700', '#d8b66f', '#dbbf2e', '#61b4c5',];
  81. return colors[param.dataIndex % colors.length];
  82. }
  83. }
  84. },
  85. label: {
  86. normal: {
  87. formatter: (params) => {
  88. let text;
  89. if (params.dataIndex == 0) {
  90. text = `{warnLevel5|\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0${params.data.name}}`;
  91. } else if (params.dataIndex == 1) {
  92. text = `{warnLevel4|\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0${params.data.name}}`;
  93. } else if (params.dataIndex == 2) {
  94. text = `{warnLevel3|\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0${params.data.name}}`;
  95. } else if (params.dataIndex == 3) {
  96. text = `{warnLevel2|\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0${params.data.name}}`;
  97. } else if (params.dataIndex == 4) {
  98. text = `{warnLevel1|\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0${params.data.name}}`;
  99. }
  100. return text;
  101. },
  102. textStyle: {
  103. color: "#fff",
  104. fontSize: 12
  105. },
  106. position: [0, -22],
  107. show: true,
  108. rich: { //富文本
  109. warnLevel1: {
  110. backgroundColor: {
  111. image: warnLevel1,
  112. },
  113. height: 16,
  114. width: 16,
  115. },
  116. warnLevel2: {
  117. backgroundColor: { //这里可以添加你想自定义的图片
  118. image: warnLevel2,
  119. },
  120. height: 16,
  121. width: 16,
  122. },
  123. warnLevel3: {
  124. backgroundColor: {
  125. image: warnLevel3,
  126. },
  127. height: 16,
  128. width: 16,
  129. },
  130. warnLevel4: {
  131. backgroundColor: { //这里可以添加你想自定义的图片
  132. image: warnLevel4,
  133. },
  134. height: 16,
  135. width: 16,
  136. },
  137. warnLevel5: {
  138. backgroundColor: { //这里可以添加你想自定义的图片
  139. image: warnLevel5,
  140. },
  141. height: 16,
  142. width: 16,
  143. },
  144. },
  145. }
  146. },
  147. data: category.value,
  148. z: 1,
  149. animationEasing: "elasticOut"
  150. },
  151. {
  152. // 分隔
  153. type: "pictorialBar",
  154. itemStyle: {
  155. normal: {
  156. color: "rgba(28, 48, 52)"
  157. }
  158. },
  159. symbolRepeat: "fixed",
  160. symbolMargin: 3,
  161. symbol: "rect",
  162. symbolClip: true,
  163. symbolSize: [10, 24],
  164. symbolPosition: "start",
  165. symbolOffset: [1, -4],
  166. symbolBoundingData: 10,
  167. data: [10, 10, 10, 10, 10],
  168. z: 2,
  169. animationEasing: "elasticOut",
  170. },
  171. {
  172. // label
  173. type: "pictorialBar",
  174. symbolBoundingData: 10,
  175. itemStyle: {
  176. normal: {
  177. color: "none"
  178. }
  179. },
  180. label: {
  181. normal: {
  182. formatter: (params) => {
  183. let text;
  184. if (params.dataIndex == 0) {
  185. text = '{a| ' + params.data + '个}';
  186. } else if (params.dataIndex == 1) {
  187. text = '{b| ' + params.data + '个}';
  188. } else if (params.dataIndex == 2) {
  189. text = '{c| ' + params.data + '个}';
  190. } else if (params.dataIndex == 3) {
  191. text = '{d| ' + params.data + '个}';
  192. } else if (params.dataIndex == 4) {
  193. text = '{e| ' + params.data + '个}';
  194. } else {
  195. text = '{f| ' + params.data + '个}';
  196. }
  197. return text;
  198. },
  199. rich: {
  200. a: {
  201. color: "#ff0000"
  202. },
  203. b: {
  204. color: "#ff7700"
  205. },
  206. c: {
  207. color: "#d8b66f"
  208. },
  209. d: {
  210. color: "#dbbf2e"
  211. },
  212. e: {
  213. color: "#61b4c5"
  214. },
  215. f: {
  216. color: "#fff"
  217. },
  218. },
  219. position: [350, -12],
  220. show: true
  221. }
  222. },
  223. data: echartY.value,
  224. z: 0,
  225. },
  226. {
  227. name: "外框",
  228. type: "bar",
  229. barGap: "-130%", // 设置外框粗细
  230. data: [10, 10, 10, 10, 10],
  231. barWidth: 29,
  232. itemStyle: {
  233. normal: {
  234. barBorderRadius: [0, 0, 0, 0],
  235. color: "transparent", // 填充色
  236. barBorderColor: "#3ecca7", // 边框色
  237. barBorderWidth: 1, // 边框宽度
  238. }
  239. },
  240. z: 2
  241. },
  242. {
  243. type: 'scatter',
  244. name: '条形',
  245. symbol: 'roundRect',
  246. symbolSize: [6, 14],
  247. symbolOffset: [3, -3],
  248. symbolKeepAspect: true,
  249. itemStyle: {
  250. normal: {
  251. color: "#3ecca7"
  252. }
  253. },
  254. data: [10, 10, 10, 10, 10],
  255. }
  256. ]
  257. }
  258. myChart.setOption(option);
  259. window.onresize = function () {
  260. myChart.resize();
  261. };
  262. }
  263. watch(() => props.echartData, (newV, oldV) => {
  264. if(newV.length){
  265. category.value=newV
  266. echartY.value=newV.map((el:any)=>el.value)
  267. getOption()
  268. }
  269. }, { immediate: true, })
  270. </script>
  271. <style lang="less" scoped>
  272. .dz_chart {
  273. position: relative;
  274. padding-top: 10px;
  275. height: 100%;
  276. .chartRef {
  277. width: 100%;
  278. height: 100%;
  279. }
  280. }
  281. </style>