网站首页 > 技术文章 正文
首先我们初始化一个 vue 项目,执行 vue init webpack echart,接着我们进入初始化的项目下。安装 echarts,
npm install echarts -S //或
cnpm install echarts -S
安装完成之后,我们就可以开始引入我们需要的 echarts 了,接下来介绍几种使用 echarts 的方式。
全局引用:
首先在 main.js 中引入 echarts,将其绑定到 vue 原型上:
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;
接着,我们就可以在任何一个组件中使用 echarts 了。
局部使用:
当然,很多时候没必要在全局引入 ecahrts,那么我们只在单个组件内使用即可,代码更加简单:
<template>
<div>
<div style="width:500px;height:500px" ref="chart"></div>
</div>
</template>
<script>
const echarts = require('echarts');
export default{
data () {
return {};
},
methods: {
initCharts () {
let myChart = echarts.init(this.$refs.chart);
// 绘制图表
myChart.setOption({
title: { text: '在 Vue 中使用 echarts' },
tooltip: {},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
},
mounted () {
this.initCharts();
}
}
</script>
可以看到,我们直接在组件内引入 echarts,接下来跟全局引入的使用一样。区别在于,这种方式如果你想在其他组件内用 echarts,则必须重新引入了。
猜你喜欢
- 2024-10-12 安详解在vue中使用echarts图表实例代码
- 2024-10-12 echarts两点连线,中间没有数值情况下实现连线,x轴标题超长解决
- 2024-10-12 ECharts—百度可视化图表插件(echarts数据可视化网站)
- 2024-10-12 vue之将echart封装为组件(vue echart 组件)
- 2024-10-12 百度echarts使用(百度 echarts)
- 2024-10-12 「ECharts 术语速查手册」简介(echarts emphasis)
- 2024-10-12 分享7个常用的echarts示例(echarts介绍)
- 2024-10-12 Vue实战090:Vue使用ECharts图表详解
- 2024-10-12 Spring Boot 2.x基础教程:使用 ECharts 绘制各种华丽的数据图表
- 2024-10-12 Echart可视化学习笔记(echarts数据可视化 html模板)
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)