echartLine1.vue 6.5 KB

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