๐ Enhance your Nuxt 3 projects with interactive data visualizations using nuxt-plotly โ Simplified Plotly.js integration made easy!
๐ nuxt-plotly module is thin Nuxt3 wrapper for plotly.js
nuxt-plotly dependency to your projectnpx nuxi@latest module add nuxt-plotly
nuxt-plotly to the modules section of nuxt.config.ts// nuxt.config.js
export default defineNuxtConfig({
/**
* Add nuxt-plotly module
*/
modules: ["nuxt-plotly"],
/**
* Add nuxt-plotly module with options
* Set the inject option to true to use plotly function via $plotly
*/
// modules: [["nuxt-plotly", { inject: true }]],
});
plotly.js-dist-min to the vite.optimizeDeps.include section of nuxt.config.ts// nuxt.config.js
export default defineNuxtConfig({
vite: {
optimizeDeps: {
include: ["plotly.js-dist-min"],
},
},
});
Thatโs it! You can now use Nuxt Plotly Module in your Nuxt app โจ
There are two ways to use the nuxt-plotly module on the client-side in Nuxt3:
<client-only> tag.<client-only>
<nuxt-plotly
:data="pieChart.data"
:layout="pieChart.layout"
:config="pieChart.config"
style="width: 100%"
></nuxt-plotly>
</client-only>
.client.vue extension, for example, PieChart.client.vue and then you can use the component without the <client-only> tag.You can access Plotly events using the @on-ready directive to receive the PlotlyHTMLElement object from the <nuxt-plotly> component.
<template>
<client-only>
<nuxt-plotly
:data="data"
:layout="layout"
:config="config"
@on-ready="myChartOnReady"
></nuxt-plotly>
</client-only>
</template>
function myChartOnReady(plotlyHTMLElement: NuxtPlotlyHTMLElement) {
console.log({ plotlyHTMLElement });
plotlyHTMLElement.on?.("plotly_afterplot", function () {
console.log("done plotting");
});
plotlyHTMLElement.on?.("plotly_click", function () {
alert("You clicked this Plotly chart!");
});
}
To use the Plotly Function in your nuxt project, follow these steps:
inject option to true in the nuxt-plotly module configuration of your nuxt.config.ts file.// nuxt.config.js
export default defineNuxtConfig({
modules: [["nuxt-plotly", { inject: true }]],
});
$plotly in your nuxt project.// app.vue
const { $plotly } = useNuxtApp();
/**
* Show all plotly functions
*/
console.log($plotly);
/**
* Use downloadImage function
*/
$plotly.downloadImage(plotlyHTMLElement as HTMLElement, {
format: "png",
width: 800,
height: 600,
filename: "newplot",
});
These type aliases simplify the usage of Plotly types in your Nuxt project:
/**
* Represents an array of Plotly data objects.
*/
export type NuxtPlotlyData = Array<Plotly.Data>;
/**
* Represents a partial configuration object for Plotly charts.
*/
export type NuxtPlotlyConfig = Partial<Plotly.Config>;
/**
* Represents a partial layout object for Plotly charts.
*/
export type NuxtPlotlyLayout = Partial<Plotly.Layout>;
/**
* Represents a partial HTML element that holds a rendered Plotly chart.
*/
export type NuxtPlotlyHTMLElement = Partial<Plotly.PlotlyHTMLElement>;
With these type aliases, you can easily work with Plotly data, configurations, layouts, and HTML elements in your Nuxt application, enhancing your experience when creating interactive charts and visualizations.
# Install dependencies
npm install
# Generate type stubs
npm run dev:prepare
# Develop with the playground
npm run dev
# Build the playground
npm run dev:build
# Run ESLint
npm run lint
# Run Vitest
npm run test
npm run test:watch
# Release new version
npm run release
Copyright ยฉ 2023 Supanut Dokmaithong.
This project is MIT licensed.
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.