echartBox.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div class="echartBar">
  3. <div class="bar" ref="bar"></div>
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { defineProps, ref, nextTick, reactive, watch, onMounted } from 'vue';
  8. import * as echarts from 'echarts';
  9. let props = defineProps({
  10. gridV: {
  11. type: Object,
  12. default: () => {
  13. return {
  14. left: '2%',
  15. right: '4%',
  16. bottom: '14%',
  17. top: '16%',
  18. containLabel: true
  19. }
  20. }
  21. },
  22. legendV: {
  23. type: Object,
  24. default: () => {
  25. return {}
  26. }
  27. },
  28. xData: {
  29. type: Array,
  30. default: () => {
  31. return []
  32. }
  33. },
  34. maxY: {
  35. type: String,
  36. default: '100'
  37. },
  38. datazoomShow: {
  39. type: Boolean,
  40. default: false,
  41. },
  42. yData1: {
  43. type: Array,
  44. default: () => {
  45. return []
  46. }
  47. },
  48. yData2: {
  49. type: Array,
  50. default: () => {
  51. return []
  52. }
  53. },
  54. typeV: {
  55. type: String,
  56. default: 'bar',
  57. }
  58. });
  59. //获取dom元素节点
  60. let bar = ref<any>();
  61. let legendD = reactive({})
  62. function getOption() {
  63. nextTick(() => {
  64. let myChart = echarts.init(bar.value);
  65. let option = {
  66. tooltip: {
  67. trigger: 'item',
  68. backgroundColor: 'rgba(0, 0, 0, .6)',
  69. textStyle: {
  70. color: '#fff',
  71. fontSize: 12,
  72. },
  73. },
  74. grid: props.gridV,
  75. legend: legendD || {},
  76. xAxis: {
  77. type: 'category',
  78. data: props.xData || [],
  79. axisLine: {
  80. lineStyle: {
  81. color: 'rgba(36, 71, 76,1)'
  82. }
  83. },
  84. axisLabel: {
  85. interval: 'auto',
  86. fontSize: 14,
  87. rotate: 0,
  88. textStyle: {
  89. color: '#fff',
  90. fontFamily: 'Microsoft YaHei'
  91. }
  92. },
  93. },
  94. yAxis: {
  95. type: 'value',
  96. max: props.maxY,
  97. axisLine: {
  98. show: false,
  99. lineStyle: {
  100. color: 'white'
  101. }
  102. },
  103. splitLine: {
  104. show: true,
  105. lineStyle: {
  106. color: 'rgba(36, 71, 76,.6)'
  107. }
  108. },
  109. axisLabel: {
  110. textStyle: {
  111. color: '#fff',
  112. },
  113. formatter: '{value}',
  114. },
  115. },
  116. // "dataZoom": [{
  117. // "show": props.datazoomShow,
  118. // "height": 12,
  119. // "xAxisIndex": [
  120. // 0
  121. // ],
  122. // bottom: '8%',
  123. // "start": 10,
  124. // "end": 90,
  125. // handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
  126. // handleSize: '110%',
  127. // handleStyle: {
  128. // color: "#d3dee5",
  129. // },
  130. // textStyle: {
  131. // color: "#fff"
  132. // },
  133. // borderColor: "#90979c"
  134. // }, {
  135. // "type": "inside",
  136. // "show": true,
  137. // "height": 15,
  138. // "start": 1,
  139. // "end": 35
  140. // }],
  141. series: [{
  142. name: legendD['data'][0],
  143. type: props.typeV || '',
  144. barWidth: '10%',
  145. itemStyle: {
  146. normal: {
  147. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  148. offset: 0,
  149. color: '#fccb05'
  150. }, {
  151. offset: 1,
  152. color: '#f5804d'
  153. }]),
  154. barBorderRadius: 12,
  155. },
  156. },
  157. data: props.yData1 || []
  158. },
  159. {
  160. name: legendD['data'][1],
  161. type: props.typeV || '',
  162. barWidth: '10%',
  163. itemStyle: {
  164. normal: {
  165. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  166. offset: 0,
  167. color: '#8bd46e'
  168. }, {
  169. offset: 1,
  170. color: '#09bcb7'
  171. }]),
  172. barBorderRadius: 12,
  173. }
  174. },
  175. data: props.yData2 || []
  176. },
  177. ]
  178. };
  179. myChart.setOption(option);
  180. window.onresize = function () {
  181. myChart.resize();
  182. };
  183. });
  184. }
  185. watch(() => props.legendV, (newV, oldV) => {
  186. if (JSON.stringify(newV) != '{}') {
  187. legendD = Object.assign({}, newV)
  188. getOption()
  189. console.log(legendD, 'legendD')
  190. }
  191. }, {
  192. immediate: true,
  193. })
  194. watch(() => props.yData1, (newY, oldY) => {
  195. getOption()
  196. }, { immediate: true })
  197. onMounted(() => {
  198. })
  199. </script>
  200. <style scoped lang="less">
  201. .echartBar {
  202. width: 100%;
  203. height: 100%;
  204. .bar {
  205. width: 100%;
  206. height: 100%;
  207. }
  208. }
  209. </style>