赞
踩
在Vue项目中使用Echats,可以极大程度的方便完成很多Canvas功能。
npm install echarts --save
- main.js 全局引入形式
- // main.js
- import * as echarts from "echarts"
- Vue.prototype.$echarts = echarts
- // 页面文件中使用,如index.vue中
- <template>
- <div>
- <!-- echarts -->
- <div id="main" class="main_container"></div>
- </div>
- </template>
- <script>
- export default {
- data() {
- return {}
- },
- mounted() {
- this.initCharts()
- },
- methods: {
- initCharts() {
- // 初始化echarts实例
- var myChart = this.$echarts.init(document.getElementById("main"))
- // 绘制图表
- myChart.setOption({
- title: {
- text: "ECharts 入门示例",
- },
- tooltip: {},
- xAxis: {
- data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
- },
- yAxis: {},
- series: [
- {
- name: "销量",
- type: "bar",
- data: [5, 20, 36, 10, 10, 20],
- },
- ],
- })
- },
- },
- }
- </script>
- <style lang="scss" scoped>
- .main_container {
- width: 100%;
- height: 200px;
- overflow: hidden;
- }
- </style>
需要注意一点,挂载echarts实例的dom节点,需要给定宽高。
- 页面中全局引入
- <template>
- <div>
- <!-- echarts -->
- <div id="main" class="main_container"></div>
- </div>
- </template>
- <script>
- import * as echarts from "echarts"
- export default {
- data() {
- return {}
- },
- mounted() {
- this.initCharts()
- },
- methods: {
- initCharts() {
- // 初始化echarts实例
- var myChart = echarts.init(document.getElementById("main"))
- // 绘制图表
- myChart.setOption({
- title: {
- text: "ECharts 入门示例",
- },
- tooltip: {},
- xAxis: {
- data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
- },
- yAxis: {},
- series: [
- {
- name: "销量",
- type: "bar",
- data: [5, 20, 36, 10, 10, 20],
- },
- ],
- })
- },
- },
- }
- </script>
- <style lang="scss" scoped>
- .main_container {
- width: 100%;
- height: 200px;
- overflow: hidden;
- }
- </style>
- // 页面文件,如index.vue
- <template>
- <div>
- <!-- echarts -->
- <div id="main" class="main_container"></div>
- </div>
- </template>
- <script>
- import * as echarts from "echarts/core"
- import { TitleComponent, GridComponent } from "echarts/components"
- import { BarChart } from "echarts/charts"
- import { CanvasRenderer } from "echarts/renderers"
- echarts.use([TitleComponent, GridComponent, BarChart, CanvasRenderer])
-
- export default {
- data() {
- return {}
- },
- mounted() {
- this.initCharts()
- },
- methods: {
- initCharts() {
- // 初始化echarts实例
- var myChart = echarts.init(document.getElementById("main"))
- // 绘制图表
- myChart.setOption({
- title: {
- text: "ECharts 入门示例",
- },
- tooltip: {},
- xAxis: {
- data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
- },
- yAxis: {},
- series: [
- {
- name: "销量",
- type: "bar",
- data: [5, 20, 36, 10, 10, 20],
- },
- ],
- })
- },
- },
- }
- </script>
- <style lang="scss" scoped>
- .main_container {
- width: 100%;
- height: 200px;
- overflow: hidden;
- }
- </style>
显示的效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。