1、安装
npm install echarts --save
在main.js中全局引入
import * as echarts from 'echarts'Vue.prototype.$echarts = echarts
2、快速上手
引入折线图
<div id="echarts-assessment" style="width: 600px;height:400px;"></div>mounted(){this.getAssessment()}methods:{getAssessment(){let myChart = this.$echarts.init(document.getElementById('echarts-assessment'));let option = {xAxis: {type: 'category',data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']},yAxis: {type: 'value'},series: [{//data可以是对象形式 [{name:"学业压力", value:"150"}]data: [150, 230, 224, 218, 135, 147, 260],type: 'line'}]};option && myChart.setOption(option);}}
更换其他图形只需要更换实例中的option代码即可
官网实例:https://echarts.apache.org/next/examples/zh/index.html
3、遇见问题汇总
3.1数据更新后视图未更新
解决方案:
将setOption的第二个参数设置为true
myChart.setOption(‘配置项’,true);
【setOption中3个参数的含义】
option—— 图表的配置项和数据
notMerge—— 可选,是否不跟之前设置的 option 进行合并,默认为 false,即合并。
lazyUpdate—— 可选,在设置完 option 后是否不立即更新图表,默认为 false,即立即更新。
3.2柱状图,正数显示在柱上方,负数则显示在柱下方
解决方案:使用map遍历data,要分清楚data是数组还是数组对象
xArr.map((item, idx) => {newData.push({name: item,type: 'bar',barWidth: 11,barGap: '130%',label: { //此处被每一个data覆盖show: true,position: "top",fontSize: 11,color: '#000',formatter: function(value) {return parseFloat(value.value.toFixed(3))}},data: data[idx].map(item=>{return {name:item.name,value:item.value,label: {show: true, //开启显示position: item.value > 0 ? "top" : "bottom", //判断正负值上方显示还是下方}}}),itemStyle: {color: this.abnormalColor[idx],}})})
