项目中引入echarts图表的基本使用方法
参考文档:
在vue-cli项目中使用echarts
echarts x轴 增加滚动条 dataZoom
前端数据可视化echarts.js使用指南
1、安装echarts
npm install echarts -S
2、在项目中引入
main.js文件
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
需要用到echarts的文件中:
<div id="chart" style="width:500px;height:500px" />
const echarts = require('echarts')
export default {
data(){
return{
chartOption: {
// title: { text: '在Vue中使用echarts' },
tooltip: {},
legend: {},
xAxis: {
data: [],
axisLabel: { interval: 0 } // 用于显示or隐藏刻度,为0时表示显示所有刻度,1为间隔一个刻度显示
},
yAxis: {},
series: [
{
name:'',
type:'bar', //柱状图
data:[]
}
]
}
}
}
}
methods:{
initEcharts(){
const myChart = echarts.init(document.getElementById('chart'))
// 绘制图表
// (1)、x轴方向设置滚动条以显示更多数据
this.chartOption={
//关键属性代码
dataZoom: [{
type: 'slider',
show: true,
xAxisIndex: [0],
left: '9%',
bottom: -5,
start: 10, // 设置为0则从第一个刻度数据开始
end: 90 //表示图表可见数据所占百分比,设置为100则无法滑动,
}]
}
// 使echart图表随着浏览器的大小发生响应式变化
window.onresize = myChart.resize
myChart.setOption(this.chartOption)
}
}
}
3、滚动条参考效果图
注意事项:
(1)在使用echarts.js的时候一定要配置xAxis,yAxis,series这三个参数,如果不想设置的话也要在初始化时将其设置为空JSON,否则会报错。
(2)在echarts.init之前的对象是必须设置有宽高的,否则也会出现错误
(上面只大概写了我项目中用到觉得比较重要的一些,想要了解更多,参考文档都写的很好很详细,有需要可自阅)