【Vue】20.vue项目中引入echarts图表遇到的问题

淡淡的烟草味﹌ 2022-02-14 18:39 678阅读 0赞

最近做一个公众号有需求是要添加一个图表,自己调查了一下,决定使用ECharts,在vue项目中引入ECharts,由于第一次做图表,还要跟UI设计图保持一致,也是遇到了好多样式问题,在这里做一下总结。

效果图如下:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JlbmJlbjUxMzYyNA_size_16_color_FFFFFF_t_70

ECharts官方Docs

ECharts官方Example

ECharts附录一按需引入ECharts图表和组件

一.安装插件

  1. npm install echarts --save

二.在当前页面中按需引入模块

  1. const echarts = require('echarts/lib/echarts');
  2. // require('echarts/lib/chart/bar');
  3. require('echarts/lib/chart/line');
  4. require('echarts/lib/component/tooltip');
  5. require('echarts/lib/component/markPoint');
  6. require('echarts/lib/component/title');
  7. require('echarts/lib/component/axisPointer');

三.在mounted()方法里面加载该折线图

  1. mounted(){
  2. this.drawLine();
  3. }
  4. methods: {
  5. drawLine() {
  6. const that = this;
  7. this.myChat = echarts.init(document.getElementById('myChat'));
  8. this.myChat.setOption({
  9. tooltip: {
  10. show: true,
  11. trigger: 'axis',
  12. axisPointer: {
  13. type: 'line',
  14. axis: 'auto',
  15. lineStyle: {
  16. color: '#F78600',
  17. z: 0,
  18. symbolSize: 2,
  19. },
  20. label: {
  21. show: true,
  22. color: '#F78600',
  23. backgroundColor: '#FFFFFF',
  24. formatter() {
  25. return '';
  26. },
  27. },
  28. },
  29. borderColor: '#FFFFFF',
  30. formatter(params) {
  31. return `<div style="z-index: 10; height: 10px; line-height: 10px; text-align: center; font-size: 12px">${params[0].seriesName} ${params[0].value}%<div/>`;
  32. },
  33. backgroundColor: '#F78600',
  34. showContent: true,
  35. },
  36. legend: {
  37. data: ['平均温度'],
  38. },
  39. grid: {
  40. top: 35,
  41. x2: 30,
  42. y2: 10,
  43. },
  44. calculable: true, // 提示框消失
  45. xAxis: {
  46. position: 'top',
  47. type: 'category',
  48. data: ['-5岁', `${that.info.age}岁`, '+5岁', '+10岁', '+15岁', '+20岁', '+25岁'],
  49. axisPointer: {
  50. type: 'line',
  51. snap: false,
  52. label: {
  53. color: '#F78600',
  54. backgroundColor: '#FFFFFF',
  55. shadowColor: '#FFFFFF',
  56. formatter(params) {
  57. return `${params.value}`;
  58. },
  59. },
  60. lineStyle: {
  61. type: 'solid',
  62. },
  63. triggerTooltip: true,
  64. },
  65. backgroundColor: '#F78600',
  66. boundaryGap: false,
  67. gridIndex: 0,
  68. // 坐标轴刻度标签的相关设置
  69. axisLabel: {
  70. color: '#9EA0AA',
  71. fontSize: 10,
  72. interval: 'auto',
  73. },
  74. // 坐标轴轴线相关设置
  75. axisLine: {
  76. show: false,
  77. },
  78. // 刻度线
  79. axisTick: {
  80. show: false,
  81. alignWithLabel: true,
  82. },
  83. // 网格线
  84. splitLine: {
  85. show: true,
  86. lineStyle: {
  87. // width: 1,
  88. type: 'dotted',
  89. color: '#EEEEEE',
  90. },
  91. },
  92. },
  93. yAxis: [
  94. {
  95. show: false, // 是否显示Y轴
  96. scale: true, // 只在数值轴中有效
  97. // position: 'right', // 坐标轴显示的位置
  98. type: 'value',
  99. name: '温度',
  100. min: that.dataArrMin - 10,
  101. max: that.dataArrMax + 10,
  102. interval: 1,
  103. axisLabel: {
  104. formatter: '{value}%',
  105. },
  106. },
  107. ],
  108. series: [
  109. {
  110. name: '现在',
  111. type: 'line',
  112. stack: '总量',
  113. smooth: true, // 曲线平滑true
  114. // symbol: 'circle',
  115. symbolSize: 4,
  116. showSymbol: false,
  117. hoverAnimation: false,
  118. z: 20,
  119. markPoint: {
  120. z: 1000,
  121. symbol: 'path://m 0,0 h 48 v 20 h -30 l -6,10 l -6,-10 h -6 z',
  122. showSymbol: true,
  123. itemStyle: {
  124. color: '#939393',
  125. opacity: 0.5,
  126. width: 74,
  127. height: 32.4,
  128. borderRadius: 50,
  129. z: 1000,
  130. },
  131. symbolSize: [25, 20], // 容器大小
  132. symbolOffset: [0, -10], // 位置偏移
  133. label: {
  134. position: 'insideTop',
  135. distance: 4,
  136. offset: [0, -2],
  137. color: '#ffffff',
  138. fontSize: 10,
  139. textBorderRadius: 10,
  140. formatter(params) {
  141. return `${params.value}%`;
  142. },
  143. },
  144. data: [
  145. {type: 'max', name: '最大值'},
  146. {type: 'min', name: '最小值'},
  147. ],
  148. },
  149. itemStyle: {
  150. normal: {
  151. areaStyle: {
  152. z: 10,
  153. type: 'default',
  154. // 渐变色实现
  155. color: new echarts.graphic.LinearGradient(0, 0, 0, 1,
  156. // 变化度
  157. // 三种由深及浅的颜色
  158. [{
  159. offset: 0,
  160. color: '#FFE8BF',
  161. },
  162. // {
  163. // offset: 0.5,
  164. // color: '#FFFFFF',
  165. // },
  166. {
  167. offset: 1,
  168. color: '#FFFFFF',
  169. }]),
  170. },
  171. lineStyle: { // 线的颜色
  172. width: 1,
  173. type: 'solid',
  174. color: '#F78600',
  175. },
  176. label: {
  177. show: false,
  178. position: 'top',
  179. textStyle: {
  180. color: '#F78600',
  181. },
  182. },
  183. color: '#FFFFFF',
  184. borderColor: '#F78600',
  185. borderWidth: 1,
  186. },
  187. },
  188. areaStyle: {
  189. },
  190. data: that.dataArr,
  191. },
  192. ],
  193. });
  194. window.addEventListener('resize', this.myChat.resize);
  195. this.myChat.dispatchAction({
  196. type: 'showTip',
  197. seriesIndex: 0,
  198. dataIndex: 1,
  199. z: 0,
  200. });
  201. },
  202. }

四.图表的一些样式问题

(1)ECharts X轴固定在上方

  1. xAxis: {
  2. position: 'top',
  3. }

(2)ECharts line动态刷新数据

  1. data: 动态接口数据

(3)ECharts tooltip提示框

  1. tooltip ={ //提示框组件
  2. trigger: 'item', //触发类型,'item'数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。 'axis'坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表中使用。
  3. triggerOn:"mousemove", //提示框触发的条件,'mousemove'鼠标移动时触发。'click'鼠标点击时触发。'mousemove|click'同时鼠标移动和点击时触发。'none'不在 'mousemove' 或 'click' 时触发
  4. showContent:true, //是否显示提示框浮层
  5. alwaysShowContent:true, //是否永远显示提示框内容
  6. showDelay:0, //浮层显示的延迟,单位为 ms
  7. hideDelay:100, //浮层隐藏的延迟,单位为 ms
  8. enterable:false, //鼠标是否可进入提示框浮层中
  9. confine:false, //是否将 tooltip 框限制在图表的区域内
  10. transitionDuration:0.4, //提示框浮层的移动动画过渡时间,单位是 s,设置为 0 的时候会紧跟着鼠标移动
  11. position:['50%', '50%'], //提示框浮层的位置,默认不设置时位置会跟随鼠标的位置,[10, 10],回掉函数,inside鼠标所在图形的内部中心位置,top、left、bottom、right鼠标所在图形上侧,左侧,下侧,右侧,
  12. formatter:"{b0}: {c0}<br />{b1}: {c1}", //提示框浮层内容格式器,支持字符串模板和回调函数两种形式,模板变量有 {a}, {b},{c},{d},{e},分别表示系列名,数据名,数据值等
  13. backgroundColor:"transparent", //标题背景色
  14. borderColor:"#ccc", //边框颜色
  15. borderWidth:0, //边框线宽
  16. padding:5, //图例内边距,单位px 5 [5, 10] [5,10,5,10]
  17. textStyle:mytextStyle, //文本样式
  18. };

(4)ECharts 初始化的时候显示tooltip提示框

  1. window.addEventListener('resize', this.myChat.resize);
  2. this.myChat.dispatchAction({
  3. type: 'showTip',
  4. seriesIndex: 0,
  5. dataIndex: 1,
  6. z: 0,
  7. });

(5)图表高度调整

动态获取的数据,如果数组高度不一样,就会导致图表的高度不一致,怎么调整呢?

  1. //直角坐标系内绘图网格
  2. grid : {
  3. left : '3%', //组件离容器左侧的距离
  4. right : '4%',
  5. bottom : '0%',
  6. containLabel : true //grid 区域是否包含坐标轴的刻度标签
  7. },

(6) tooltip提示框 自定义小圆点(颜色,形状和大小等等),同理,要改变小圆点儿形状,重写html片段即可。

  1. formatter: function(params) {
  2.   var result = ''
  3.   var dotHtml = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:#1197b8"></span>'
  4.   params.forEach(function (item) {
  5.     result += item.axisValue + "</br>" + dotHtml + item.data
  6.   })
  7.   return result
  8. }

(7)ECharts 折线图美化(颜色渐变,背景透明,隐藏坐标轴)

参考:https://blog.csdn.net/Changeable0127/article/details/81333559

(8)ECharts 折线图折线平滑以及实现填充背景色渐变

参考:https://blog.csdn.net/u012998306/article/details/88689370

(9)ECharts 柱状图或者折线图顶端显示数据

在series如果已经有itemStyle就在她里面添加position: ‘top’

  1. series : [ {
  2. name : '',
  3. type : 'bar',
  4. barWidth : '50%',
  5. itemStyle : {
  6. normal : {
  7. label: {
  8. show: true,
  9. position: 'top',
  10. textStyle: {
  11. color: 'white'
  12. }
  13. }
  14. },
  15. },
  16. data : seriesDataArray
  17. } ]

(10)实现折线图的拐点为空心圆圈

我们看官网提供的是拐点处都是实心的圆,而且圆内部填充的颜色与legend颜色一致,只要把圆的背景颜色填充成背景色,圆圈的border跟折线的颜色一致

  1. series[{
  2. symbol: 'circle',
  3. itemStyle: {
  4. normal: {
  5. color: "#000",
  6. borderColor: '#f58f23',
  7. borderWidth: 1,
  8. }
  9. },
  10. }]

(11)修改折线的颜色和折线的点的大小的方法

  1. series: [{
  2. type: 'line',
  3. showSymbol: true,
  4. symbol: 'circle', //设定为实心点
  5. symbolSize: 20, //设定实心点的大小
  6. hoverAnimation: false,
  7. animation: false,
  8. data: dataa,
  9. markPoint: { //显示一定区域内的最大值和最小值
  10. data: [
  11. { type: 'max', name: '最大值' },
  12. { type: 'min', name: '最小值' }
  13. ]
  14. }]

(12)ECharts折线图取消折线上圆点,设置线条为虚线,series—>symbol:’none’取消折线上的圆点

  1. series: [{
  2. itemStyle:{
  3. normal:{
  4. lineStyle:{
  5. width:2,
  6. type:'dotted' //设置线条为虚线
  7. }
  8. }
  9. },
  10. data: data,
  11. type: 'line'
  12. }]

(13)ECharts markPoint显示问题

参考:https://blog.csdn.net/m0_38069630/article/details/80921801

(14)markPoint气泡大小及修改气泡中字体颜色

  1. markPoint : {
  2. // symbolSize: 80,控制气泡大小
  3. itemStyle:{
  4. normal:{
  5. label:{
  6. show: true,
  7. color: '#000000',//气泡中字体颜色
  8. }
  9. }
  10. },
  11. data : [
  12. {type : 'max', name: '最大值'},
  13. {type : 'min', name: '最小值'}
  14. ]
  15. }

(15)关于移动端如何实现ECharts折线图默认展示高亮数据点的解决办法

参考:https://my.oschina.net/u/3983353/blog/2218887

发表评论

表情:
评论列表 (有 0 条评论,678人围观)

还没有评论,来说两句吧...

相关阅读