basicEchartLine.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <div class="basicEchartLine">
  3. <div class="echart-box" ref="line"></div>
  4. </div>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref, reactive, onMounted, nextTick, defineProps, watch } from 'vue';
  8. import * as echarts from 'echarts';
  9. let props = defineProps({
  10. //距离边缘的距离
  11. gridV: {
  12. type: Object,
  13. default: () => {
  14. return {};
  15. },
  16. },
  17. echartData: {
  18. type: Object,
  19. default: () => {
  20. return {}
  21. }
  22. }
  23. });
  24. //获取don元素节点
  25. let line = ref();
  26. let gridVs = reactive({
  27. top: '',
  28. left: '',
  29. right: '',
  30. bottom: '',
  31. })
  32. let echartDatas = reactive({
  33. xData: [],
  34. yData: [],
  35. yData1: [],
  36. legendName: []
  37. })
  38. function getOption() {
  39. nextTick(() => {
  40. let myChart = echarts.init(line.value);
  41. let option = {
  42. tooltip: {
  43. trigger: 'axis',
  44. backgroundColor: 'rgba(0, 0, 0, .6)',
  45. borderColor: '#2395c8',
  46. textStyle: {
  47. color: '#fff',
  48. fontSize: 12,
  49. },
  50. axisPointer: {
  51. type: 'line',
  52. lineStyle: {
  53. color: {
  54. type: 'linear',
  55. x: 0,
  56. y: 0,
  57. x2: 0,
  58. y2: 1,
  59. colorStops: [
  60. {
  61. offset: 0,
  62. color: 'rgba(31, 179, 247,0)',
  63. },
  64. {
  65. offset: 0.5,
  66. color: 'rgba(31, 179, 247,1)',
  67. },
  68. {
  69. offset: 1,
  70. color: 'rgba(31, 179, 247,0)',
  71. },
  72. ],
  73. global: false,
  74. },
  75. },
  76. },
  77. },
  78. legend: {
  79. align: 'left',
  80. right: 'center',
  81. top: '2%',
  82. type: 'plain',
  83. textStyle: {
  84. color: '#7ec7ff',
  85. fontSize: 14,
  86. },
  87. // icon:'rect',
  88. itemGap: 25,
  89. itemWidth: 18,
  90. 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',
  91. data: [
  92. {
  93. name: echartDatas.legendName.length != 0 ? echartDatas.legendName[0] : '',
  94. },
  95. {
  96. name: echartDatas.legendName.length > 1 ? echartDatas.legendName[1] : '',
  97. },
  98. ],
  99. },
  100. grid: gridVs,
  101. xAxis: [
  102. {
  103. //x轴
  104. type: 'category', //数据类型为不连续数据
  105. boundaryGap: false, //坐标轴两边是否留白
  106. axisLine: {
  107. //坐标轴轴线相关设置。数学上的x轴
  108. show: true,
  109. lineStyle: {
  110. color: 'rgba(100, 99, 99,.5)',
  111. type: 'dashed',
  112. },
  113. },
  114. axisLabel: {
  115. fontSize: 14,
  116. margin: 10,
  117. textStyle: {
  118. color: '#b3b8cc',
  119. },
  120. formatter: function (params) {
  121. let newParamsName = ref('') // 最终拼接成的字符串
  122. let paramsNameNumber = ref(params.length) // 实际标签的个数
  123. let provideNumber = ref(10) // 每行能显示的字的个数
  124. let rowNumber = Math.ceil(paramsNameNumber.value / provideNumber.value) // 换行的话,需要显示几行,向上取整
  125. /**
  126. * 判断标签的个数是否大于规定的个数, 如果大于,则进行换行处理 如果不大于,即等于或小于,就返回原标签
  127. */
  128. // 条件等同于rowNumber>1
  129. if (paramsNameNumber.value > provideNumber.value) {
  130. /** 循环每一行,p表示行 */
  131. for (var p = 0; p < rowNumber; p++) {
  132. var tempStr = '' // 表示每一次截取的字符串
  133. var start = p * provideNumber.value // 开始截取的位置
  134. var end = start + provideNumber.value // 结束截取的位置
  135. // 此处特殊处理最后一行的索引值
  136. if (p == rowNumber - 1) {
  137. // 最后一次不换行
  138. tempStr = params.substring(start, paramsNameNumber.value)
  139. } else {
  140. // 每一次拼接字符串并换行
  141. tempStr = params.substring(start, end) + '\n'
  142. }
  143. newParamsName.value += tempStr // 最终拼成的字符串
  144. }
  145. } else {
  146. // 将旧标签的值赋给新标签
  147. newParamsName.value = params
  148. }
  149. //将最终的字符串返回
  150. return newParamsName.value
  151. }
  152. },
  153. axisTick: { show: true }, //刻度点数轴
  154. splitLine: {
  155. show: false,
  156. },
  157. data: echartDatas.xData.length != 0 ? echartDatas.xData : [],
  158. },
  159. ],
  160. yAxis: [
  161. {
  162. //y轴的相关设置
  163. type: 'value', //y轴数据类型为连续的数据
  164. min: 0, //y轴上的刻度最小值
  165. // splitNumber: 7,//y轴上的刻度段数
  166. splitLine: {
  167. show: true,
  168. lineStyle: {
  169. color: 'rgba(100, 99, 99,.5)',
  170. type: 'dashed',
  171. },
  172. },
  173. axisLine: {
  174. //y轴的相关设置
  175. show: false,
  176. },
  177. axisLabel: {
  178. show: true,
  179. margin: 20,
  180. textStyle: {
  181. color: '#b3b8cc',
  182. },
  183. },
  184. axisTick: { show: true }, //刻度点数轴
  185. },
  186. ],
  187. series: [
  188. {
  189. name: echartDatas.legendName.length != 0 ? echartDatas.legendName[0] : '',
  190. type: 'line', //统计图类型为折线图
  191. smooth: false, //是否平滑曲线显示
  192. symbolSize: 0, //数据点的大小,[0,0]//b表示宽度和高度
  193. lineStyle: {
  194. //线条的相关设置
  195. normal: {
  196. color: '#1faef0', // 线条颜色
  197. },
  198. },
  199. itemStyle: {
  200. color: "#1faef0",
  201. borderColor: "#1faef0",
  202. borderWidth: 1
  203. },
  204. areaStyle: {
  205. //区域填充样式
  206. normal: {
  207. //线性渐变,前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’,则该四个值是绝对像素位置。
  208. color: new echarts.graphic.LinearGradient(
  209. 0,
  210. 0,
  211. 0,
  212. 1,
  213. [
  214. { offset: 0, color: 'rgba(31, 174, 240, 0.7)' },
  215. { offset: 0.7, color: 'rgba(31, 174, 240, 0)' },
  216. ],
  217. false,
  218. ),
  219. shadowColor: 'rgba(31, 174, 240, 0.6)', //阴影颜色
  220. shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
  221. },
  222. },
  223. data: echartDatas.yData.length != 0 ? echartDatas.yData : [],
  224. },
  225. {
  226. name: echartDatas.legendName.length > 1 ? echartDatas.legendName[1] : '',
  227. type: 'line', //统计图类型为折线图
  228. smooth: false, //是否平滑曲线显示
  229. symbolSize: 0, //数据点的大小,[0,0]//b表示宽度和高度
  230. lineStyle: {
  231. //线条的相关设置
  232. normal: {
  233. color: '#1ef49f', // 线条颜色
  234. },
  235. },
  236. itemStyle: {
  237. color: "#1ef49f",
  238. borderColor: "#1ef49f",
  239. borderWidth: 1
  240. },
  241. areaStyle: {
  242. //区域填充样式
  243. normal: {
  244. //线性渐变,前4个参数分别是x0,y0,x2,y2(范围0~1);相当于图形包围盒中的百分比。如果最后一个参数是‘true’,则该四个值是绝对像素位置。
  245. color: new echarts.graphic.LinearGradient(
  246. 0,
  247. 0,
  248. 0,
  249. 1,
  250. [
  251. { offset: 0, color: 'rgba(31, 237, 155, 0.7)' },
  252. { offset: 0.7, color: 'rgba(31, 237, 155, 0)' },
  253. ],
  254. false,
  255. ),
  256. shadowColor: 'rgba(31, 237, 155, 0.6)', //阴影颜色
  257. shadowBlur: 20, //shadowBlur设图形阴影的模糊大小。配合shadowColor,shadowOffsetX/Y, 设置图形的阴影效果。
  258. },
  259. },
  260. data: echartDatas.yData1.length != 0 ? echartDatas.yData1 : [],
  261. },
  262. ],
  263. };
  264. myChart.setOption(option);
  265. window.onresize = function () {
  266. myChart.resize();
  267. };
  268. });
  269. }
  270. watch(() => props.gridV, (newV, oldV) => {
  271. console.log(newV, 'gird--------')
  272. gridVs.top = newV.top
  273. gridVs.left = newV.left
  274. gridVs.right = newV.right
  275. gridVs.bottom = newV.bottom
  276. }, { immediate: true, deep: true })
  277. watch(() => props.echartData, (newE, oldE) => {
  278. console.log(newE, 'echartData---------')
  279. echartDatas.xData = newE.xData
  280. echartDatas.yData = newE.yData
  281. echartDatas.yData1 = newE.yData1
  282. echartDatas.legendName = newE.legendName
  283. getOption();
  284. }, { immediate: true, deep: true })
  285. onMounted(() => {
  286. getOption();
  287. });
  288. </script>
  289. <style lang="less" scoped>
  290. .basicEchartLine {
  291. position: relative;
  292. width: 100%;
  293. height: 100%;
  294. .echart-box {
  295. width: 100%;
  296. height: 100%;
  297. }
  298. }
  299. </style>