barChart.vue 4.2 KB

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