npm获取echarts
npm install echarts --save
main.js文件下引入echarts,新版本引入方式与以往不同
import * as echarts from 'echarts'
Vue.prototype.$echarts=echarts
创建vue文件,准备dom ,定义id名称,样式大小
<template>
<div id="chart" style="width: 100%;height: 400px;"></div>
</template>
基于准备好的dom,初始化echarts实例
// 基于准备好的dom,初始化echarts实例
var myChart = this.$echarts.init(document.getElementById('chart'));
绘制图表 ,在echarts官网选择需要的图表,复制option
var option = {
title: {
text: 'SC'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['game1', 'game2', 'game3', 'game4', 'game5']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'game1',
type: 'line',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'game2',
type: 'line',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'game3',
type: 'line',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'game4',
type: 'line',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'game5',
type: 'line',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
}
绘制图表
myChart.setOption(option);
钩子函数 mounted() 调用图表所在的方法,在页面挂载之前会被调用
mounted() {
this.chart();
}
全部代码,如下
<template>
<div id="chart" style="width: 100%;height: 400px;"></div>
</template>
<script>
export default{
name:'historyChart',
data(){
return{
}
},
methods:{
chart(){
var myChart = this.$echarts.init(document.getElementById('chart'));
var option = {
title: {
text: 'SC'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['game1', 'game2', 'game3', 'game4', 'game5']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
name: 'game1',
type: 'line',
stack: 'Total',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'game2',
type: 'line',
stack: 'Total',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'game3',
type: 'line',
stack: 'Total',
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: 'game4',
type: 'line',
stack: 'Total',
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: 'game5',
type: 'line',
stack: 'Total',
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
}
myChart.setOption(option);
}
},
mounted() {
this.chart();
}
}
</script>
<style>
</style>
版权声明:本文为qq_63312957原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。