Vue+Echarts可视化详解(统计图)
<template>
<div>
<el-row :gutter="20">
<el-col :span="12">
<div id="chart-pie" style="width: 100%; height: 400px;"></div>
</el-col>
<el-col :span="12">
<div id="chart-bar" style="width: 100%; height: 400px;"></div>
</el-col>
</el-row>
</div>
</template>
<script>
import * as echarts from 'echarts';
export default {
name: 'StatisticsChart',
mounted() {
this.initCharts();
},
methods: {
initCharts() {
const chartPie = echarts.init(document.getElementById('chart-pie'));
const chartBar = echarts.init(document.getElementById('chart-bar'));
const optionPie = {
title: {
text: '访问来源'
},
tooltip: {
trigger: 'item'
},
legend: {
top: '5%',
left: 'center'
},
series: [
{
name: '访问来源',
type: 'pie',
radius: '50%',
data: [
{ value: 1048, name: '搜索引擎' },
{ value: 735, name: '直接访问' },
{ value: 580, name: '邮件营销' },
{ value: 484, name: '联盟广告' },
{ value: 300, name: '视频广告' }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
const optionBar = {
title: {
text: '销售分析'
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
xAxis: {
type: 'category',
data: ['2011', '2012', '2013', '2014', '2015', '2016', '2017']
},
yAxis: {
type: 'value'
},
series: [
{
name: '销售额',
type: 'bar',
data: [320, 332, 301, 334, 390, 330, 320]
}
]
};
chartPie.setOption(optionPie);
chartBar.setOption(optionBar);
window.addEventListener('resize', function() {
chartPie.resize();
chartBar.resize();
});
}
}
};
</script>
<style scoped>
/* 样式按需定制 */
</style>
这个代码实例展示了如何在Vue组件中初始化两个ECharts图表,一个是饼图,另一个是柱状图。它使用了Element UI的布局组件<el-row>
和<el-col>
来构建响应式的布局,并在组件的mounted
生命周期钩子中初始化图表。图表的配置项采用了ECharts官方文档中
评论已关闭