echarts 柱状图案例(屏幕自适应、元素自适应、动态显示、配置逻辑)

╰+哭是因爲堅強的太久メ 2021-07-24 20:14 996阅读 0赞
  1. 自适应:
  2. (1)window.onresize监听调用echarts.init对象.resize()方法实现图表自适应
  3. (2)获取图表容器的宽度/100,分成100份,然后根据份数来调整元素合适的大小
  4. (3)在第一次加载、刷新和窗口改变时,都需要实现(2)的步骤
  5. 配置逻辑:
  6. (1)第一次option进行自适应元素高宽的配置,即在监听中进行
  7. (2)第二次option进行样式的配置
  8. (3)第三次option进行数据的配置
  9. 组件卸载时需要卸载监听

效果图:
在这里插入图片描述在这里插入图片描述

代码示例:

  1. <template>
  2. <!-- 横向柱状图 -->
  3. <div class='s-c'>
  4. <div class='s-chart' ref='sell'>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. import chalk from '../../static/theme/chalk'
  10. export default {
  11. name:'sell',
  12. data(){
  13. return{
  14. myMap:null,
  15. data:[],
  16. currentPage:1,
  17. totalPage:0,
  18. timer:null,
  19. first:true
  20. }
  21. },
  22. mounted()
  23. {
  24. this._initChart();
  25. this._getData();
  26. //适配化配置调用
  27. this._resize();
  28. this._mouse();
  29. },
  30. //组件销毁前卸载监听
  31. beforeDestroy()
  32. {
  33. window.onresize=null;
  34. },
  35. methods:{
  36. async _getData()
  37. {
  38. let res=await this.$axios.get('/api/seller');
  39. this.data=res.data;
  40. //数据进行排序
  41. this.data.sort((a,b)=>{
  42. return a.value-b.value
  43. })
  44. //每五个显示一页,计算总页数
  45. this.totalPage=this.data.length%5==0? this.data.length/5:Math.ceil(this.data.length/5);
  46. this._updateChart();
  47. },
  48. _initChart()
  49. {
  50. this.myMap = this.$echarts.init(this.$refs.sell,'chalk');
  51. //完成除数据和宽度适配以外的一些初始配置
  52. let option={
  53. title:{
  54. text:'| 商家销售统计',
  55. left: 20,
  56. top: 20,
  57. },
  58. grid: {
  59. top: '15%',
  60. left: '3%',
  61. right: '6%',
  62. bottom: '3%',
  63. containLabel: true // 距离是包含坐标轴上的文字
  64. },
  65. xAxis:{
  66. type:'value',
  67. splitLine:{ //去掉坐标轴的水平虚线指示线
  68. show:false
  69. },
  70. scale:true,
  71. },
  72. yAxis:{
  73. type:'category',
  74. },
  75. tooltip:{
  76. trigger:'axis',
  77. axisPointer:{
  78. type:'line',
  79. z:0,
  80. lineStyle:{
  81. color:'#2d3443',
  82. }
  83. },
  84. },
  85. series:[
  86. {
  87. type:'bar',
  88. barWidth:66,
  89. itemStyle:{
  90. color:{
  91. type: 'linear',
  92. x: 0,
  93. y: 0,
  94. x2: 1,
  95. y2: 0,
  96. colorStops: [{
  97. offset: 0, color: '#5052EE' // 0% 处的颜色
  98. }, {
  99. offset: 1, color: '#AB6EE5' // 100% 处的颜色
  100. }],
  101. global: false,
  102. },
  103. },
  104. label:{
  105. show:true,
  106. position: 'right',
  107. color:'white'
  108. }
  109. }
  110. ],
  111. }
  112. this.myMap.setOption(option);
  113. },
  114. //根据数据配置图表
  115. _updateChart()
  116. {
  117. //根据当前页数获取对应数据
  118. let start=(this.currentPage-1)*5;
  119. let end=start+5;
  120. const showData=this.data.slice(start,end);
  121. const seriesName=showData.map((item,index)=>{
  122. return item.name;
  123. })
  124. const seriesValue=showData.map((item,index)=>{
  125. return item.value
  126. })
  127. //数据改变时配置
  128. let option={
  129. yAxis:{
  130. data:seriesName,
  131. },
  132. series:[
  133. {
  134. data:seriesValue,
  135. }
  136. ],
  137. }
  138. this.myMap.setOption(option);
  139. this._setInterval();
  140. },
  141. //定时器刷新当前数据
  142. _setInterval()
  143. {
  144. // console.log(this.currentPage);
  145. if(this.timer)
  146. {
  147. clearInterval(this.timer);
  148. }
  149. let that=this;
  150. this.timer=setInterval(() => {
  151. if(this.currentPage>=this.totalPage)
  152. {
  153. this.currentPage=1;
  154. }else{
  155. this.currentPage++;
  156. }
  157. this._updateChart();
  158. }, 3000);
  159. },
  160. //适配监听,整体图表,已经图表中的元素适配
  161. _resize()
  162. {
  163. //第一次打开和刷新不改变窗口时,也进行适配
  164. if(this.first)
  165. {
  166. this._resizeCode();
  167. this.first=false;
  168. }
  169. //标题大小适配,根据容器大小分成100份,根据份数来试
  170. window.onresize=()=>{
  171. this._resizeCode();
  172. }
  173. },
  174. //适配代码
  175. _resizeCode()
  176. {
  177. let container=this.$refs.sell.offsetWidth;
  178. let titleFontSize=container/100*3.6;
  179. //适配图表
  180. let option={
  181. title:{
  182. textStyle:{
  183. fontSize:titleFontSize
  184. }
  185. },
  186. tooltip:{
  187. axisPointer:{
  188. lineStyle:{
  189. width:titleFontSize
  190. }
  191. }
  192. },
  193. series:{
  194. barWidth:titleFontSize,
  195. itemStyle: {
  196. barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0]
  197. }
  198. }
  199. }
  200. this.myMap.setOption(option);
  201. this.myMap.resize();
  202. },
  203. //鼠标监听,鼠标放上停止,移开播放
  204. _mouse()
  205. {
  206. this.myMap.on('mouseover',()=>{
  207. clearInterval(this.timer);
  208. })
  209. this.myMap.on('mouseout',()=>{
  210. this._updateChart();
  211. })
  212. }
  213. }
  214. }
  215. </script>
  216. <style lang='less' scoped>
  217. .s-c{
  218. width: 100%;
  219. height:100%;
  220. .s-chart{
  221. width: 100%;
  222. height:100%;
  223. }
  224. }
  225. </style>

发表评论

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

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

相关阅读

    相关 NGUI屏幕适应

    屏幕自适应 NGUI可以比较方便的实现屏幕自适应,但是它的官方教程里面针对这个问题没有详细的教程,所以可能在实现的时候会走比较多的弯路。以下是我在开发过程中找到的一个比较