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