Nuxt3.0中使用EChart可视化图表📊
在Nuxt3.0项目中用到了可视化图表,于是我用了EChart可视化图表库。但是在官网我没有找到针对在Nuxt3.0中使用EChart的方法,于是在这里记录我的引入EChart并简单使用的步骤。需要声明的是,本文只针对在Nuxt3.0项目中使用EChart.js库的可视化图表进行讲解,不针对EChart图表的详细配置进行讲解,如需了解EChart的可视化图表详细配置参数,请查看官网手册Documentation - Apache ECharts
第一步:下载安装vue-echarts和echarts
安装vue-echarts包:npm i vue-echarts
安装echarts包:npm i echarts
tips:如果下载安装报错,可替换尝试使用:npm i vue-echarts --force和npm i echarts --force
第二步:配置项目nuxt-config.ts文件
nuxt-config.ts文件
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
build: {
transpile: [/echarts/],
}
})
第三步:新建plugins目录,并在目录下新建chart.js文件

chart.js文件:
import { use } from 'echarts/core';
// 手动导入ECharts模块以减小包的大小
import { CanvasRenderer } from 'echarts/renderers';
import { BarChart } from 'echarts/charts';
import { GridComponent, TooltipComponent } from 'echarts/components';
export default defineNuxtPlugin(() => {
use([CanvasRenderer, BarChart, GridComponent, TooltipComponent]);
});
第四步:在Test.vue页面中使用
Test.vue页面文件
<template>
<div>
<client-only>
<v-chart class="chart" :option="option" />
</client-only>
</div>
</template>
<script setup lang="ts">
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { LabelLayout } from 'echarts/features';
import { PieChart } from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
LegendComponent,
} from 'echarts/components';
import VChart, { THEME_KEY } from 'vue-echarts';
import { ref, defineComponent } from 'vue';
use([
CanvasRenderer,
PieChart,
TitleComponent,
TooltipComponent,
LegendComponent,
LabelLayout
]);
const option = ref({
title: {
text: '测试图表',
subtext: 'nuxt3.0中的EChart初探',
left: 'center',
textStyle: { //主标题样式
color: '#DC143C'
},
subtextStyle: { //副标题样式
color: '#008000'
}
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'horizontal', //图例方向
bottom: 'bottom', //图例距离底部位置
textStyle: { color: "#FFFDFE" }, //图例字体颜色
},
series: [
{
name: '技术量',
type: 'pie',
radius: '50%',
label: {
color: '#FFA500'
},
data: [
{ value: 1048, name: '前端技术' },
{ value: 735, name: '后端技术' },
{ value: 580, name: '服务器技术' },
{ value: 484, name: '运维技术' },
{ value: 300, name: '测试技术' }
]
}
]
});
</script>
<style scoped>
.chart {
height: 800px;
}
</style>
至此,我们在Nuxt3.0项目中使用EChart图表的需求就实现啦~
tips:我使用的是Vue3.0的 setup语法糖的写法,如果没有用语法糖写法的小伙伴可以参考如下代码,其中唯一的区别就是在Test.vue页面文件中的用法不同:
<template>
<div>
<client-only>
<v-chart class="chart" :option="option" />
</client-only>
</div>
</template>
<script> //注意这里没有使用setup语法糖
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { PieChart } from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
LegendComponent,
} from 'echarts/components';
import VChart, { THEME_KEY } from 'vue-echarts';
import { ref, defineComponent } from 'vue';
use([
CanvasRenderer,
PieChart,
TitleComponent,
TooltipComponent,
LegendComponent,
]);
export default defineComponent({
name: 'HelloWorld',
components: {
VChart,
},
provide: {
[THEME_KEY]: 'dark',
},
setup() {
const option = ref({
title: {
text: '测试图表',
subtext: 'nuxt3.0中的EChart初探',
left: 'center',
textStyle: {
//主标题样式
color: '#DC143C',
},
subtextStyle: {
//副标题样式
color: '#008000',
},
},
tooltip: {
trigger: 'item',
},
legend: {
orient: 'horizontal', //图例方向
bottom: 'bottom', //图例距离底部位置
textStyle: { color: '#FFFDFE' }, //图例字体颜色
},
series: [
{
name: '技术量',
type: 'pie',
radius: '50%',
label: {
color: '#FFA500',
},
data: [
{ value: 1048, name: '前端技术' },
{ value: 735, name: '后端技术' },
{ value: 580, name: '服务器技术' },
{ value: 484, name: '运维技术' },
{ value: 300, name: '测试技术' },
],
},
],
});
return { option };
},
});
</script>
<style scoped>
.chart {
height: 800px;
}
</style>
效果图:

Nuxt3.0中使用EChart可视化图表📊的更多相关文章
- vue3.0+echart可视化
vue3.0 + echart可视化 案例1: 案例代码 <template> <div ref="test" style="width:800px;h ...
- 解决echart在IE中使用时,在div中加入postion后图表不显示问题
<!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="height:400px;width:1 ...
- JFreeChart与AJAX+JSON+ECharts两种处理方式生成热词统计可视化图表
本篇的思想:对HDFS获取的数据进行两种不同的可视化图表处理方式.第一种JFreeChar可视化处理生成图片文件查看.第二种AJAX+JSON+ECharts实现可视化图表,并呈现于浏览器上. 对 ...
- Webstorm+Webpack+echarts构建个性化定制的数据可视化图表&&两个echarts详细教程(柱状图,南丁格尔图)
Webstorm+Webpack+echarts ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(I ...
- Webpack 2 视频教程 018 - 使用可视化图表进行统计分析打包过程
原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...
- vue可视化图表 基于Echarts封装好的v-charts简介
**vue可视化图表 基于Echarts封装好的v-charts** 近期公司又一个新的需求,要做一个订单和销售额统计的项目,需要用到可视化图表来更直观的展示数据.首先我想到的是Echarts,众所周 ...
- vue-cli+v-charts实现移动端可视化图表
v-charts是饿了么团队开源的一款基于Vue和Echarts的图表工具,在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出现正是为了解决这个 ...
- 可视化图表库--goJS
GoJS是Northwoods Software的产品.Northwoods Software创立于1995年,专注于交互图控件和类库.旗下四款产品: GoJS:用于在HTML上创建交互图的纯java ...
- Python 数据分析中常用的可视化工具
Python 数据分析中常用的可视化工具 1 Matplotlib 用于创建出版质量图表的绘图工具库,目的是为 Python 构建一个 Matlab 式的绘图接口. 1.1 安装 Anaconada ...
- ECharts-基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表
ECharts http://ecomfe.github.com/echarts 基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表.创新的拖拽重计算 ...
随机推荐
- Navicat Premium 16 永久破解激活
Navicat premium是一款数据库管理工具,是一个可多重连线资料库的管理工具,它可以让你以单一程式同时连线到 MySQL.SQLite.Oracle 及 PostgreSQL 资料库,让管理不 ...
- kafka-集群搭建及启动脚本
集群搭建: [root@localhost kafka_2.11-0.10.2.1]# cat config/server.properties | grep -v ^$ | grep -v ^# b ...
- windows下创建虚拟环境
创建虚拟环境依赖以下两个模块 virtualenv 和virtualenvwrapper-win 1.下载 2.修改环境变量,增加一条 WORKON_HOME:路径 3. 同步配置 去向Pytho ...
- Python库之os库和logging库的基本使用说明
使用os库操作目录及文件 使用os.sep() 方法获取系统分隔符 print(os.sep) 使用os.name()方法获取操作系统的平台类型 print(os.name) 使用os.getcwd( ...
- 【建造者设计模式详解】Java/JS/Go/Python/TS不同语言实现
简介 建造者模式(Builder Pattern),也叫生成器模式,属于创建型模式.它使用多个简单的对象一步一步构建成一个复杂的对象.它允许你使用相同的创建代码生成不同类型和形式的对象. 当你希望使用 ...
- Android studio软件的安装过程详解
步骤详解 进入官网,下载相关软件 官网地址:https://developer.android.google.cn/studio/ 点击该页面里面的这个按钮,就能够很轻松地完成下载操作: 弹出弹窗,继 ...
- ABAC框架-casbin
参考文档:https://www.kancloud.cn/oldlei/casbin/1289455 参考博客:https://www.cnblogs.com/studyzy/p/11380736.h ...
- springboot多环境开发兼容问题(Maven和boot)
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...
- 20个值得收藏的实用JavaScript技巧
1.确定对象的数据类型 function myType(type) { return Object.prototype.toString.call(type).slice(8, -1); 使用Obje ...
- Trie树结构
PrefixTree 208. 实现 Trie (前缀树) Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键.这一数据结构 ...