|
13 | 13 |
|
14 | 14 | <p align="center"><a href="https://apexcharts.com/vue-chart-demos/"><img src="https://apexcharts.com/media/apexcharts-banner.png"></a></p> |
15 | 15 |
|
16 | | -## Download and Installation |
| 16 | +## Features |
| 17 | + |
| 18 | +- Reactive chart updates — change props and the chart re-renders automatically |
| 19 | +- **Tree-shaking** — import only the chart types and features you use via `vue3-apexcharts/core` |
| 20 | +- **Server-Side Rendering (SSR)** — render charts to HTML on the server with `<apexchart-server>` and hydrate on the client with `<apexchart-hydrate>` |
| 21 | +- Full TypeScript support |
17 | 22 |
|
18 | | -##### Installing via npm |
| 23 | +## Download and Installation |
19 | 24 |
|
20 | 25 | ```bash |
21 | | -npm install --save apexcharts |
22 | | -npm install --save vue3-apexcharts |
| 26 | +npm install apexcharts vue3-apexcharts |
23 | 27 | ``` |
24 | 28 |
|
25 | | -If you're looking for Vue 2.x.x compatibile component, check-out <a href="https://github.com/apexcharts/vue-apexcharts">vue-apexcharts</a> |
| 29 | +If you're looking for Vue 2.x.x compatible component, check-out <a href="https://github.com/apexcharts/vue-apexcharts">vue-apexcharts</a> |
26 | 30 |
|
27 | 31 | ## Usage |
28 | 32 |
|
@@ -245,28 +249,190 @@ More info on the `.exec()` method can be found <a href="https://apexcharts.com/d |
245 | 249 |
|
246 | 250 | All other methods of ApexCharts can be called the same way. |
247 | 251 |
|
| 252 | +## Tree-Shaking (Core Entry) |
| 253 | + |
| 254 | +The default entry (`vue3-apexcharts`) bundles the full ApexCharts library. If you want to reduce your bundle size by importing only the chart types and features you need, use the **core** entry point: |
| 255 | + |
| 256 | +```js |
| 257 | +import VueApexCharts from "vue3-apexcharts/core"; |
| 258 | + |
| 259 | +// Import only the chart types you need |
| 260 | +import "apexcharts/bar"; |
| 261 | +import "apexcharts/line"; |
| 262 | + |
| 263 | +// Import only the features you need |
| 264 | +import "apexcharts/features/legend"; |
| 265 | +import "apexcharts/features/toolbar"; |
| 266 | + |
| 267 | +const app = createApp(App); |
| 268 | +app.use(VueApexCharts); |
| 269 | +``` |
| 270 | + |
| 271 | +The core entry imports ApexCharts from `apexcharts/core`, which is the bare class with only tooltip included. You must explicitly import the chart types and optional features your app uses: |
| 272 | + |
| 273 | +**Chart types:** `apexcharts/line`, `apexcharts/bar`, `apexcharts/pie`, `apexcharts/heatmap`, `apexcharts/candlestick`, `apexcharts/radial`, etc. |
| 274 | + |
| 275 | +**Optional features:** |
| 276 | +| Import | Description | |
| 277 | +|--------|-------------| |
| 278 | +| `apexcharts/features/legend` | Chart legend | |
| 279 | +| `apexcharts/features/toolbar` | Toolbar (zoom, pan, download) | |
| 280 | +| `apexcharts/features/annotations` | X/Y-axis and point annotations | |
| 281 | +| `apexcharts/features/exports` | PNG/SVG/CSV export | |
| 282 | +| `apexcharts/features/keyboard` | Keyboard navigation & accessibility | |
| 283 | + |
| 284 | +> **Note:** Tree-shaking requires ApexCharts v5.8.1+. |
| 285 | +
|
| 286 | +## Server-Side Rendering (SSR) |
| 287 | + |
| 288 | +Vue3-ApexCharts includes dedicated components for SSR that work with Nuxt 3 and other Vue SSR frameworks. |
| 289 | + |
| 290 | +> **Note:** SSR support requires ApexCharts v5.6.0+. |
| 291 | +
|
| 292 | +### Nuxt 3 Setup |
| 293 | + |
| 294 | +Register the plugin so that components are available during both server and client render passes: |
| 295 | + |
| 296 | +```ts |
| 297 | +// plugins/apexcharts.ts |
| 298 | +import VueApexCharts from "vue3-apexcharts"; |
| 299 | + |
| 300 | +export default defineNuxtPlugin({ |
| 301 | + name: "apexcharts", |
| 302 | + parallel: true, |
| 303 | + setup(nuxtApp) { |
| 304 | + nuxtApp.vueApp.use(VueApexCharts); |
| 305 | + }, |
| 306 | +}); |
| 307 | +``` |
| 308 | + |
| 309 | +### Server-Side Rendering with `<apexchart-server>` |
| 310 | + |
| 311 | +Render charts to HTML on the server — the chart markup is included in the initial page load for better SEO and faster perceived performance: |
| 312 | + |
| 313 | +```vue |
| 314 | +<template> |
| 315 | + <apexchart-server |
| 316 | + type="bar" |
| 317 | + :series="series" |
| 318 | + :options="options" |
| 319 | + :width="500" |
| 320 | + :height="300" |
| 321 | + /> |
| 322 | +</template> |
| 323 | +
|
| 324 | +<script setup> |
| 325 | +const series = [{ data: [30, 40, 35, 50, 49, 60, 70] }]; |
| 326 | +const options = { |
| 327 | + chart: { type: "bar" }, |
| 328 | + xaxis: { categories: ["A", "B", "C", "D", "E", "F", "G"] }, |
| 329 | +}; |
| 330 | +</script> |
| 331 | +``` |
| 332 | + |
| 333 | +### SSR + Client Hydration |
| 334 | + |
| 335 | +For interactive charts, pair server rendering with client-side hydration. The chart is rendered on the server, then `<apexchart-hydrate>` finds all server-rendered charts on the page and attaches client-side interactivity (tooltips, animations, click handlers): |
| 336 | + |
| 337 | +```vue |
| 338 | +<template> |
| 339 | + <apexchart-server |
| 340 | + type="line" |
| 341 | + :series="series" |
| 342 | + :options="options" |
| 343 | + :width="700" |
| 344 | + :height="350" |
| 345 | + /> |
| 346 | +
|
| 347 | + <!-- Place once per page — hydrates all server-rendered charts --> |
| 348 | + <apexchart-hydrate |
| 349 | + :client-options="{ |
| 350 | + chart: { animations: { enabled: true } }, |
| 351 | + }" |
| 352 | + /> |
| 353 | +</template> |
| 354 | +
|
| 355 | +<script setup> |
| 356 | +const series = [{ name: "Sales", data: [30, 40, 35, 50, 49, 60, 70] }]; |
| 357 | +const options = { |
| 358 | + chart: { type: "line" }, |
| 359 | + xaxis: { categories: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] }, |
| 360 | +}; |
| 361 | +</script> |
| 362 | +``` |
| 363 | + |
| 364 | +### Client-Only Rendering (Traditional) |
| 365 | + |
| 366 | +For purely client-side rendering in Nuxt, wrap the standard component in `<ClientOnly>`: |
| 367 | + |
| 368 | +```vue |
| 369 | +<template> |
| 370 | + <ClientOnly> |
| 371 | + <apexchart |
| 372 | + type="bar" |
| 373 | + :series="series" |
| 374 | + :options="options" |
| 375 | + width="500" |
| 376 | + height="300" |
| 377 | + /> |
| 378 | + </ClientOnly> |
| 379 | +</template> |
| 380 | +``` |
| 381 | + |
| 382 | +### SSR Component Reference |
| 383 | + |
| 384 | +#### `<apexchart-server>` |
| 385 | + |
| 386 | +| Prop | Type | Default | Description | |
| 387 | +| ------------- | ------------- | -------- | --------------------------- | |
| 388 | +| **type** | String | `'line'` | Chart type | |
| 389 | +| **width** | Number/String | `400` | Chart width | |
| 390 | +| **height** | Number/String | `300` | Chart height | |
| 391 | +| **series** | Array | `[]` | Chart data series | |
| 392 | +| **options** | Object | `{}` | ApexCharts configuration | |
| 393 | +| **className** | String | `''` | CSS class for the container | |
| 394 | + |
| 395 | +#### `<apexchart-hydrate>` |
| 396 | + |
| 397 | +| Prop | Type | Default | Description | |
| 398 | +| ----------------- | ------ | ----------------------------- | ------------------------------------------- | |
| 399 | +| **clientOptions** | Object | `{}` | Client-side options merged during hydration | |
| 400 | +| **selector** | String | `'[data-apexcharts-hydrate]'` | CSS selector for charts to hydrate | |
| 401 | + |
248 | 402 | ## Running the examples |
249 | 403 |
|
250 | 404 | Basic Examples are included to show how to get started using ApexCharts with Vue 3 easily. |
251 | 405 |
|
252 | | -To run the examples, |
253 | | - |
254 | 406 | ```bash |
255 | 407 | cd demo |
256 | | -yarn install |
257 | | -yarn start |
| 408 | +npm install |
| 409 | +npm run dev |
| 410 | +``` |
| 411 | + |
| 412 | +An SSR demo using Nuxt 3 is also available: |
| 413 | + |
| 414 | +```bash |
| 415 | +cd demo-ssr |
| 416 | +npm install |
| 417 | +npm run dev |
258 | 418 | ``` |
259 | 419 |
|
260 | 420 | ## Development |
261 | 421 |
|
262 | 422 | #### Install dependencies |
263 | 423 |
|
264 | 424 | ```bash |
265 | | -yarn install |
| 425 | +npm install |
266 | 426 | ``` |
267 | 427 |
|
268 | 428 | #### Bundling |
269 | 429 |
|
270 | 430 | ```bash |
271 | | -yarn build |
| 431 | +npm run build |
| 432 | +``` |
| 433 | + |
| 434 | +#### Running tests |
| 435 | + |
| 436 | +```bash |
| 437 | +npm test |
272 | 438 | ``` |
0 commit comments