构建 Vue 2 项目并安装 ECharts 5.2.2
下面是从零开始构建 Vue 2 项目并安装指定版本 ECharts 的完整步骤:
1. 创建 Vue 2 项目
# 安装 Vue CLI(如果尚未安装)
npm install -g @vue/cli
# 创建 Vue 2 项目
vue create vue2-echarts-demo
# 选择 Vue 2 预设
# 手动选择特性时确保选择 Vue 2.x
2. 进入项目目录并安装 ECharts 5.2.2
cd vue2-echarts-demo
npm install echarts@5.2.2 --save
3. 项目结构准备
src/
├── components/
│ └── ECharts.vue # ECharts 封装组件
├── views/
│ └── Home.vue # 示例页面
├── App.vue
└── main.js
4. 封装 ECharts 组件
创建 src/components/ECharts.vue
:
<template>
<div ref="chart" :style="{ width: width, height: height }"></div>
</template>
<script>
import * as echarts from 'echarts';
export default {
props: {
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '400px'
},
option: {
type: Object,
required: true
}
},
data() {
return {
chart: null
};
},
watch: {
option: {
handler(newVal) {
if (this.chart) {
this.chart.setOption(newVal, true);
}
},
deep: true
}
},
mounted() {
this.initChart();
},
beforeDestroy() {
if (this.chart) {
this.chart.dispose();
this.chart = null;
}
},
methods: {
initChart() {
this.chart = echarts.init(this.$refs.chart);
this.chart.setOption(this.option);
// 响应式调整
window.addEventListener('resize', this.handleResize);
},
handleResize() {
this.chart && this.chart.resize();
}
}
};
</script>
5. 创建示例页面
创建 src/views/Home.vue
:
<template>
<div class="home">
<h1>Vue 2 + ECharts 5.2.2 示例</h1>
<ECharts :option="chartOption" />
</div>
</template>
<script>
import ECharts from '@/components/ECharts.vue';
export default {
name: 'Home',
components: {
ECharts
},
data() {
return {
chartOption: {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
}
};
}
};
</script>
<style scoped>
.home {
padding: 20px;
}
</style>
6. 修改 App.vue
<template>
<div id="app">
<Home />
</div>
</template>
<script>
import Home from './views/Home.vue';
export default {
name: 'App',
components: {
Home
}
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
margin-top: 60px;
}
</style>
7. 修改 main.js
import Vue from 'vue';
import App from './App.vue';
Vue.config.productionTip = false;
new Vue({
render: h => h(App)
}).$mount('#app');
8. 运行项目
npm run serve
9. 按需引入 ECharts(可选)
如果只需要部分 ECharts 功能,可以按需引入:
// 在需要使用的地方按需引入
import * as echarts from 'echarts/core';
import { BarChart } from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
echarts.use([
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
BarChart,
CanvasRenderer
]);
// 然后使用 echarts
10. 项目依赖确认
确保 package.json
中包含:
"dependencies": {
"echarts": "^5.2.2",
"vue": "^2.6.14"
}
常见问题解决
版本冲突:
- 如果安装时出现版本冲突,可以尝试:
npm install echarts@5.2.2 --save --legacy-peer-deps
全局引入:
- 如果需要全局引入 ECharts,可以在
main.js
中添加:
import echarts from 'echarts'; Vue.prototype.$echarts = echarts;
- 如果需要全局引入 ECharts,可以在
主题使用:
- 要使用自定义主题:
import theme from './theme.json'; echarts.registerTheme('myTheme', theme); // 初始化时使用主题 this.chart = echarts.init(this.$refs.chart, 'myTheme');
现在您已经成功创建了一个 Vue 2 项目并集成了 ECharts 5.2.2 版本,可以开始开发各种图表应用了。