lineChart.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <script setup>
  2. import util from "@tools/util";
  3. import chartTheme from './../rateAnalysis.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. "#325dab",
  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. axisPointer: {
  92. type: "cross",
  93. },
  94. },
  95. dataset: props.dataset || [],
  96. series: props.series || [],
  97. legend: {
  98. right: "120",
  99. top: "5",
  100. itemWidth: 6,
  101. },
  102. grid: {
  103. top: 80,
  104. left: 40,
  105. right: 40,
  106. bottom: 40,
  107. },
  108. dataZoom: [
  109. {
  110. type: "inside", //图表下方的伸缩条
  111. show: false, //是否显示
  112. realtime: true, //拖动时,是否实时更新系列的视图
  113. start: 0, //伸缩条开始位置(1-100),可以随时更改
  114. end: 100, //伸缩条结束位置(1-100),可以随时更改
  115. },
  116. {
  117. type: "slider", //图表下方的伸缩条
  118. show: false, //是否显示
  119. realtime: true, //拖动时,是否实时更新系列的视图
  120. start: 0, //伸缩条开始位置(1-100),可以随时更改
  121. end: 100, //伸缩条结束位置(1-100),可以随时更改
  122. },
  123. ],
  124. }
  125. },
  126. set(val) { }
  127. })
  128. watch(() => option, (newVal, oldVal) => {
  129. if (chartIns.value) {
  130. const echartIns = toRaw(chartIns.value)
  131. echartIns.setOption(toRaw(newVal.value))
  132. }
  133. }, { deep: true })
  134. watch([() => props.width, () => props.height],(newVal, oldVal) => {
  135. if(chartIns.value){
  136. const echartIns = toRaw(chartIns.value)
  137. nextTick(() => echartIns.resize())
  138. }
  139. })
  140. const funBrushChange = (flag) => {
  141. const echartIns = toRaw(chartIns.value)
  142. echartIns.dispatchAction({
  143. type: "takeGlobalCursor",
  144. // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  145. key: "brush",
  146. brushOption: {
  147. seriesIndex: [1],
  148. yAxisIndex: 0,
  149. transformable: true,
  150. throttleType: "debounce",
  151. throttleDelay: 1000,
  152. removeOnClick: true,
  153. brushType: flag? "polygon" : false,
  154. brushMode: "multiple",
  155. brushStyle: {
  156. borderWidth: 1,
  157. color: "rgba(255,36,36,0.2)",
  158. borderColor: "#ff2424",
  159. },
  160. },
  161. });
  162. echartIns.off("brushSelected");
  163. echartIns.on("brushSelected", (params) => {
  164. emits("getSelected", params.batch || []);
  165. });
  166. }
  167. watch(() => props.brush, (newVal, oldVal) => funBrushChange(newVal))
  168. onMounted(() => {
  169. nextTick(() => {
  170. echarts.registerTheme('chartTheme', chartTheme)
  171. const echartIns = echarts.init(document.getElementById(chartId),'chartTheme')
  172. chartIns.value = echartIns
  173. echartIns.setOption(option.value)
  174. funBrushChange(props.brush)
  175. window.addEventListener('resize', () => {
  176. echartIns.resize()
  177. })
  178. })
  179. })
  180. </script>
  181. <template>
  182. <div :id="chartId" :style="{ height: props.height, width: props.width }"></div>
  183. </template>