echartLine1.vue 8.1 KB

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