irpas技术客

Vue中使用echarts_Soul_guys_echarts 如何使用

网络 7432

使用步骤 在项目下使用命令行,初始化工程的 npm 环境并安装 echarts(这里前提是您已经安装了 npm): npm init npm install echarts --save

2.导入模块到main.js(如果你只有一个组件用echarts,那么就导入局部)

全局

main.js:

import Vue from 'vue' import echarts from 'echarts' //需要挂载到Vue原型上 Vue.prototype.$echarts = echarts

之后的组件内部使用:

<template> <div style="width: auto;height: 400px" id="main"> </div> </template> <script> //通过this.$echarts来使用 export default { name: "page", mounted(){ // 在通过mounted调用即可 this.echartsInit() }, methods: { //初始化echarts echartsInit() { //柱形图 //因为初始化echarts 的时候,需要指定的容器 id='main' this.$echarts.init(document.getElementById('main')).setOption({ xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar', showBackground: true, backgroundStyle: { color: 'rgba(220, 220, 220, 0.8)' } }] }) } } } </script>

之后运行即可

局部

局部使用:

<template> <div style="width: auto;height: 400px" id="main"> </div> </template> <script> import echarts from 'echarts' export default { name: "echarts", data() { return {} }, mounted() { this.echartsInit() }, methods:{ echartsInit() { echarts.init(document.getElementById('main')).setOption({ xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [120, 200, 150, 80, 70, 110, 130], type: 'bar', showBackground: true, backgroundStyle: { color: 'rgba(220, 220, 220, 0.8)' } }] }) } } } </script> <style scoped> </style>

echarts官网教程: https://echarts.apache.org/zh/tutorial.html#%E5%9C%A8%20webpack%20%E4%B8%AD%E4%BD%BF%E7%94%A8%20ECharts echarts官网还有更多的实例: https://echarts.apache.org/examples/zh/index.html#chart-type-bar

1.进入官网 2.找到你想要的效果 3.点击进入 4.复制代码粘贴


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #echarts #如何使用 #npm #环境并安装 #npmnpm #install