echartLine1.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div class="echartLine">
  3. <div class="line" ref="line"></div>
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import * as echarts from 'echarts'
  8. import { ref, nextTick, reactive, watch, defineProps } from 'vue';
  9. let props = defineProps({
  10. echartDataSg: {
  11. type: Object,
  12. }
  13. })
  14. //获取dom元素节点
  15. let line = ref<any>()
  16. let echartDataSgs = reactive({})
  17. watch(() => props.echartDataSg, (data) => {
  18. echartDataSgs = Object.assign({}, data)
  19. getOption()
  20. }, { immediate: true, deep: true })
  21. function getOption() {
  22. nextTick(() => {
  23. const myChart = echarts.init(line.value)
  24. let option = {
  25. grid: {
  26. top: '8%',
  27. left: '5%',
  28. bottom: '14%',
  29. right: '5%',
  30. // containLabel: true,
  31. },
  32. tooltip: {
  33. trigger: 'axis',
  34. backgroundColor: 'rgba(0, 0, 0, .6)',
  35. textStyle: {
  36. color: '#fff',
  37. fontSize: 12,
  38. },
  39. },
  40. xAxis: [
  41. {
  42. type: 'category',
  43. boundaryGap: false,
  44. axisLabel: {
  45. // formatter: '{value}',
  46. fontSize: 14,
  47. margin: 10,
  48. textStyle: {
  49. color: '#b3b8cc',
  50. },
  51. // interval: 0,
  52. formatter: function (params) {
  53. let newParamsName = ref('') // 最终拼接成的字符串
  54. let paramsNameNumber = ref(params.length) // 实际标签的个数
  55. let provideNumber = ref(10) // 每行能显示的字的个数
  56. let rowNumber = Math.ceil(paramsNameNumber.value / provideNumber.value) // 换行的话,需要显示几行,向上取整
  57. /**
  58. * 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
  59. */
  60. // 条件等同于rowNumber>1
  61. if (paramsNameNumber.value > provideNumber.value) {
  62. /** 循环每一行,p表示行 */
  63. for (var p = 0; p < rowNumber; p++) {
  64. var tempStr = '' // 表示每一次截取的字符串
  65. var start = p * provideNumber.value // 开始截取的位置
  66. var end = start + provideNumber.value // 结束截取的位置
  67. // 此处特殊处理最后一行的索引值
  68. if (p == rowNumber - 1) {
  69. // 最后一次不换行
  70. tempStr = params.substring(start, paramsNameNumber.value)
  71. } else {
  72. // 每一次拼接字符串并换行
  73. tempStr = params.substring(start, end) + '\n'
  74. }
  75. newParamsName.value += tempStr // 最终拼成的字符串
  76. }
  77. } else {
  78. // 将旧标签的值赋给新标签
  79. newParamsName.value = params
  80. }
  81. //将最终的字符串返回
  82. return newParamsName.value
  83. }
  84. },
  85. axisLine: {
  86. lineStyle: {
  87. color: '#244a94',
  88. },
  89. },
  90. splitLine: {
  91. show: true,
  92. lineStyle: {
  93. color: '#0d2973',
  94. type: 'dashed',
  95. },
  96. },
  97. axisTick: {
  98. show: false,
  99. },
  100. data: echartDataSgs.xData,
  101. },
  102. ],
  103. yAxis: [
  104. {
  105. boundaryGap: false,
  106. // name: '(%)',
  107. type: 'value',
  108. max: 50,
  109. axisLabel: {
  110. textStyle: {
  111. color: '#b3b8cc',
  112. },
  113. },
  114. nameTextStyle: {
  115. color: '#fff',
  116. fontSize: 12,
  117. lineHeight: 2,
  118. },
  119. splitLine: {
  120. lineStyle: {
  121. color: '#0d2973',
  122. type: 'dashed',
  123. },
  124. },
  125. axisLine: {
  126. show: true,
  127. lineStyle: {
  128. color: '#244a94',
  129. },
  130. },
  131. axisTick: {
  132. show: false,
  133. },
  134. },
  135. ],
  136. series: [
  137. {
  138. name: echartDataSgs.lengedData ? echartDataSgs.lengedData : '',
  139. type: 'line',
  140. smooth: false,
  141. showSymbol: false,
  142. symbolSize: 8,
  143. zlevel: 3,
  144. itemStyle: {
  145. color: '#02f2de',
  146. borderColor: '#a3c8d8',
  147. },
  148. lineStyle: {
  149. normal: {
  150. width: 3,
  151. color: '#02f2de',
  152. },
  153. },
  154. areaStyle: {
  155. normal: {
  156. color: new echarts.graphic.LinearGradient(
  157. 0,
  158. 0,
  159. 0,
  160. 1,
  161. [
  162. {
  163. offset: 0,
  164. color: 'rgba(2, 242, 222,0.8)',
  165. },
  166. {
  167. offset: 0.5,
  168. color: 'rgba(2, 242, 222,0.4)',
  169. },
  170. {
  171. offset: 0.9,
  172. color: 'rgba(2, 242, 222,0)',
  173. },
  174. ],
  175. false
  176. ),
  177. },
  178. },
  179. data: echartDataSgs.yData,
  180. },
  181. ],
  182. }
  183. myChart.setOption(option)
  184. window.onresize = function () {
  185. myChart.resize()
  186. }
  187. })
  188. }
  189. </script>
  190. <style scoped lang='less'>
  191. .echartLine {
  192. width: 100%;
  193. height: 100%;
  194. .line {
  195. width: 100%;
  196. height: 100%;
  197. }
  198. }
  199. </style>