总结一下近几天根据UI图实现echarts图表中遇到的问题。
根据商家选择的时间范围在图表展示不同时间的销售额,毛利润等情况,一开始很顺利地在H5上运行起来,打包到手机上调试的时候问题来了,formatter展示不生效,查了一些资料,基本都是在图表update时给formatter赋值,我也试了一下,没有达到预期效果,后来试验,formatter是字符串的时候展示没问题,是方法时手机端未展示。然后,我通过每次echarts线的点击事件,得到类似于formatter方法的返回值,进行回显。
<view id="echarts" class="echarts_data" :prop="option" :change:prop="echarts.updateEcharts" @click="echarts.onClick"></view>
复制
<script module="echarts" lang="renderjs"> let mychart export default { data() { return { clickData: { chenbenPrice: 0, difference: 0, goodsNum: 0, mont: '', payPrice: 0, value: 0 } } }, mounted() { if (typeof window.echarts === 'function') { this.initEcharts() } else { const script = document.createElement('script') script.src = '*******/echarts.js' // 我这是上传到云端的地址,自行修改 script.onload = this.initEcharts.bind(this) document.head.appendChild(script) } }, methods: { initEcharts() { mychart = echarts.init(document.getElementById('echarts')) //mychart.setOption(this.option) this.updateEcharts(this.option) // 点击事件获取数据 mychart.getZr().on('click', params => { let pointInPixel = [params.offsetX, params.offsetY] if (mychart.containPixel('grid', pointInPixel)) { let xIndex = mychart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0] this.clickData = mychart.getOption().series[0].data[xIndex] this.updateEcharts(this.option) } }) }, updateEcharts(newValue, oldValue, ownerInstance, instance) { if (mychart) { if (newValue) { if (newValue.tooltip) { if (newValue.tooltip.formatterStatus) { newValue.tooltip.formatter = ` <div style="font-size:12px;">${this.clickData.mont}<br/> <div style="height:10px;"></div> 销售额:${this.clickData.payPrice}<br/> 成<span style="margin: 0 6px"></span>本:${this.clickData.chenbenPrice}<br/> 毛利润:${this.clickData.value}<br/></div>` newValue.tooltip.extraCssText = 'z-index: 2;' } } } mychart.setOption(newValue, newValue.notMerge) } }, onClick(event, ownerInstance) { ownerInstance.callMethod('onViewClick', { data: this.clickData }) } } } </script>
复制
最后实现效果(手机端截图):

第一次写博客记,记录下开发中遇到的小问题,也希望能够帮到阅读的人,thank you~
ok,the ending~~~~