barChart.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <script setup>
  2. import util from "@tools/util";
  3. import chartTheme from './barChart.json'
  4. import { ref, toRaw, computed, onMounted, watch, nextTick } from 'vue';
  5. import {useStore} from 'vuex'
  6. import * as echarts from 'echarts'
  7. const chartId = 'chart-' + util.newGUID(); //chartId
  8. const chartIns = ref(null) //chart 实例
  9. const emits = defineEmits(['getSelected'])
  10. const props = defineProps({
  11. xAxis: {
  12. type: Object,
  13. required: true,
  14. default: () => ({})
  15. },
  16. yAxis: {
  17. type: Array,
  18. required: false
  19. },
  20. series: {
  21. type: Array,
  22. required: true
  23. },
  24. dataset: {
  25. type: Array,
  26. required: false,
  27. default: () => ([])
  28. },
  29. height: {
  30. type: String,
  31. required: false,
  32. default: '500px'
  33. },
  34. width: {
  35. type: String,
  36. required: false,
  37. default: '500px'
  38. },
  39. title: {
  40. type: String,
  41. required: false
  42. },
  43. subtext: {
  44. type: String,
  45. required: false
  46. },
  47. brush: {
  48. type: Boolean,
  49. required: false,
  50. default: false
  51. }
  52. })
  53. /**定义option */
  54. const option = computed({
  55. get() {
  56. return {
  57. color:[
  58. "rgb(50,93,171)",
  59. "#0098d980",
  60. "#626c91",
  61. "#a0a7e6",
  62. "#c4ebad",
  63. "#96dee8"
  64. ],
  65. title: {
  66. text: props.title || '',
  67. subtext: props.subtext || '',
  68. top: 6,
  69. left: '5%',
  70. },
  71. xAxis: props.xAxis || {},
  72. yAxis: props.yAxis || {},
  73. brush: {
  74. seriesIndex: [1],
  75. yAxisIndex: 0,
  76. transformable: true,
  77. throttleType: "debounce",
  78. throttleDelay: 1000,
  79. removeOnClick: true,
  80. brushType: props.brush? "polygon" : false,
  81. brushMode: "multiple",
  82. brushStyle: {
  83. borderWidth: 1,
  84. borderColor: "#ff2424",
  85. },
  86. },
  87. toolbox:{
  88. show: props.brush,
  89. },
  90. tooltip: {
  91. confine: true,
  92. trigger: "axis",
  93. },
  94. dataset: props.dataset || [],
  95. series: props.series || [],
  96. legend: {
  97. right: "120",
  98. top: "5",
  99. itemWidth: 6,
  100. },
  101. grid: {
  102. top: 80,
  103. left: 40,
  104. right: 40,
  105. bottom: 40,
  106. },
  107. dataZoom: [
  108. {
  109. type: "inside", //图表下方的伸缩条
  110. show: false, //是否显示
  111. realtime: true, //拖动时,是否实时更新系列的视图
  112. start: 0, //伸缩条开始位置(1-100),可以随时更改
  113. end: 100, //伸缩条结束位置(1-100),可以随时更改
  114. },
  115. {
  116. type: "slider", //图表下方的伸缩条
  117. show: false, //是否显示
  118. realtime: true, //拖动时,是否实时更新系列的视图
  119. start: 0, //伸缩条开始位置(1-100),可以随时更改
  120. end: 100, //伸缩条结束位置(1-100),可以随时更改
  121. },
  122. ],
  123. }
  124. },
  125. set(val) { }
  126. })
  127. watch(() => option, (newVal, oldVal) => {
  128. if (chartIns.value) {
  129. const echartIns = toRaw(chartIns.value)
  130. echartIns.setOption(toRaw(newVal.value))
  131. }
  132. }, { deep: true })
  133. watch([() => props.width, () => props.height],(newVal, oldVal) => {
  134. if(chartIns.value){
  135. const echartIns = toRaw(chartIns.value)
  136. nextTick(() => echartIns.resize())
  137. }
  138. })
  139. const store = useStore()
  140. const collapse = computed({
  141. get(){
  142. return store.state.collapse
  143. },
  144. set(val){}
  145. })
  146. watch(collapse, (val) => {
  147. if(chartIns.value){
  148. setTimeout(() => {
  149. chartIns.value?.resize()
  150. },300)
  151. }
  152. })
  153. const funBrushChange = (flag) => {
  154. const echartIns = toRaw(chartIns.value)
  155. echartIns.dispatchAction({
  156. type: "takeGlobalCursor",
  157. // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  158. key: "brush",
  159. brushOption: {
  160. seriesIndex: [1],
  161. yAxisIndex: 0,
  162. transformable: true,
  163. throttleType: "debounce",
  164. throttleDelay: 1000,
  165. removeOnClick: true,
  166. brushType: flag? "polygon" : false,
  167. brushMode: "multiple",
  168. brushStyle: {
  169. borderWidth: 1,
  170. color: "rgba(255,36,36,0.2)",
  171. borderColor: "#ff2424",
  172. },
  173. },
  174. });
  175. echartIns.off("brushSelected");
  176. echartIns.on("brushSelected", (params) => {
  177. emits("getSelected", params.batch || []);
  178. });
  179. }
  180. watch(() => props.brush, (newVal, oldVal) => funBrushChange(newVal))
  181. onMounted(() => {
  182. nextTick(() => {
  183. echarts.registerTheme('chartTheme', chartTheme)
  184. const echartIns = echarts.init(document.getElementById(chartId),'chartTheme')
  185. chartIns.value = echartIns
  186. echartIns.setOption(option.value)
  187. funBrushChange(props.brush)
  188. window.addEventListener('resize', () => {
  189. echartIns.resize()
  190. })
  191. })
  192. })
  193. </script>
  194. <template>
  195. <div :id="chartId" :style="{ height: props.height, width: props.width }"></div>
  196. </template>