### Basic Line Chart Example
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/+page.md
A quick start example demonstrating how to create a line chart using svelte-chartjs. Ensure ChartJS is registered with necessary components.
```svelte
```
--------------------------------
### Quick Setup with Auto Import
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
For rapid prototyping, import 'chart.js/auto' to automatically register all Chart.js components. Note that this increases bundle size.
```svelte
```
--------------------------------
### Install svelte-chartjs
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/+page.md
Install the svelte-chartjs package and chart.js using your preferred package manager.
```bash
pnpm add svelte-chartjs chart.js
# or
yarn add svelte-chartjs chart.js
# or
npm i svelte-chartjs chart.js
```
--------------------------------
### Stacked Bar Chart Setup with svelte-chartjs
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/examples/stacked-bar/+page.md
Configure and render a stacked bar chart. Ensure Chart.js components and scales are registered. Stacking is controlled via the `options.scales` object.
```svelte
```
--------------------------------
### Get dataset at event utility function
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/api/+page.md
Returns all elements belonging to the same dataset as the clicked element.
```ts
function getDatasetAtEvent(chart: Chart, event: PointerEvent): InteractionItem[]
```
--------------------------------
### Lazy Chart.js Auto Import
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/+page.md
Use this import for a simple setup where all Chart.js components are automatically included. This is not recommended for production due to bundle size.
```js
import 'chart.js/auto';
```
--------------------------------
### Get elements at event utility function
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/api/+page.md
Returns all elements at the same data index as the clicked element.
```ts
function getElementsAtEvent(chart: Chart, event: PointerEvent): InteractionItem[]
```
--------------------------------
### Get element at event utility function
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/api/+page.md
Returns the single nearest element to the click point.
```ts
function getElementAtEvent(chart: Chart, event: PointerEvent): InteractionItem[]
```
--------------------------------
### Updating Chart Data with Svelte Reactivity
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/guides/reactivity/+page.md
Use standard Svelte `$state` reactivity to update chart data. Reassigning the `data` object triggers an automatic chart update. This example demonstrates adding a new data point.
```svelte
```
--------------------------------
### Initialize Site Theme
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/app.html
Sets the document theme based on localStorage or system preference.
```javascript
(function () { var saved = localStorage.getItem('theme'); var theme = saved === 'light' || saved === 'dark' ? saved : window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; document.documentElement.setAttribute('data-theme', theme); })();
```
--------------------------------
### Create and Render a Pie Chart
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/pie/+page.md
Import the Pie component and necessary Chart.js modules. Register the modules and define your chart data and options. Use the Pie component, passing the data and options as props.
```svelte
```
--------------------------------
### Initialize and Render Bar Chart
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/bar/+page.md
Import necessary Chart.js modules and register them. Then, define chart data and render the Bar component with the provided data and options.
```svelte
```
--------------------------------
### Enable Tree-Shaking with Auto Registration
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/README.md
Import chart.js/auto to automatically register all Chart.js components.
```javascript
import { Line } from 'svelte-chartjs';
import 'chart.js/auto';
```
--------------------------------
### Import components and utilities
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/api/+page.md
Standard import statement for accessing components and interaction utilities from the library.
```js
import {
Chart,
Line,
Bar,
Pie,
Doughnut,
Radar,
PolarArea,
Bubble,
Scatter,
getDatasetAtEvent,
getElementAtEvent,
getElementsAtEvent,
} from 'svelte-chartjs';
```
--------------------------------
### Event Handling with Event Utilities
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Use getDatasetAtEvent, getElementAtEvent, and getElementsAtEvent to create interactive charts that respond to user clicks. Ensure the chart instance is bound to access these utilities.
```svelte
```
--------------------------------
### Tree-shakable Chart.js Import and Registration
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/+page.md
Import and register only the necessary Chart.js components for better performance and smaller bundle sizes. This is the recommended approach.
```js
import {
Chart as ChartJS,
Title, Tooltip, Legend,
LineElement, LinearScale, PointElement, CategoryScale,
} from 'chart.js';
ChartJS.register(
Title, Tooltip, Legend,
LineElement, LinearScale, PointElement, CategoryScale
);
```
--------------------------------
### Create a Line Chart with svelte-chartjs
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/line/+page.md
Import and register necessary Chart.js modules, then define chart data and options to render a line chart using the Line component. Ensure all required Chart.js components are registered before use.
```svelte
```
--------------------------------
### Pie Chart Component Usage
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/pie/+page.md
Demonstrates how to import and use the Pie component from svelte-chartjs, including necessary Chart.js imports and data configuration.
```APIDOC
## Pie Chart Component Usage
### Description
This section shows how to integrate the Pie chart component into your Svelte application. It includes the necessary imports for both `svelte-chartjs` and `chart.js`, along with an example of how to define the chart data and options.
### Method
Component Usage
### Endpoint
N/A (Component Integration)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```svelte
```
### Response
#### Success Response (200)
N/A (Component Rendering)
#### Response Example
N/A (Component Rendering)
```
--------------------------------
### Implement Gradient Fill in Svelte
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/examples/gradient/+page.md
Register the Filler plugin and use onMount to create a linear gradient once the chart context is available.
```svelte
```
--------------------------------
### Implement Clipboard Copy Functionality
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/app.html
Listens for clicks on elements with the .copy-btn class to copy base64-encoded code to the clipboard.
```javascript
document.addEventListener('click', function (e) { var btn = e.target.closest('.copy-btn'); if (!btn) return; var code = atob(btn.dataset.code); navigator.clipboard.writeText(code).then(function () { btn.textContent = 'Copied!'; setTimeout(function () { btn.textContent = 'Copy'; }, 1500); }); });
```
--------------------------------
### Pie Chart Props
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/pie/+page.md
Lists and describes the available props for the Pie Chart component.
```APIDOC
## Pie Chart Props
### Description
These are the configurable properties available for the Pie Chart component.
### Method
Component Props
### Endpoint
N/A (Component Integration)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Props Table
| Prop | Type | Default | Description |
|---|---|---|---|
| `data` | `ChartData<'pie'>` | required | Chart data |
| `options` | `ChartOptions<'pie'>` | `{}` | Chart.js options |
| `plugins` | `Plugin<'pie'>[]` | `[]` | Chart.js plugins |
| `updateMode` | `UpdateMode` | — | Transition mode |
| `chart` | `Chart` | — | Bindable chart instance |
### Request Example
None
### Response
#### Success Response (200)
N/A (Component Rendering)
#### Response Example
N/A (Component Rendering)
```
--------------------------------
### Implement Pie Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Displays proportional data as slices of a circle, requiring ArcElement registration.
```svelte
```
--------------------------------
### Implement a Line Chart
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/README.md
Register necessary Chart.js components and pass data to the Line component.
```svelte
```
--------------------------------
### Implement Line Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Renders a line chart requiring registration of LineElement, LinearScale, PointElement, and CategoryScale.
```svelte
```
--------------------------------
### Doughnut Chart Usage
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/doughnut/+page.md
Import and register necessary Chart.js modules, then define chart data and render the Doughnut component with responsive options.
```svelte
```
--------------------------------
### Create a Scatter Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Plots individual data points on a 2D grid to show correlations. Requires PointElement and LinearScale for axes.
```svelte
```
--------------------------------
### Scatter Chart Component
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/scatter/+page.md
Demonstrates the basic usage of the Scatter component from svelte-chartjs, including necessary Chart.js imports and data configuration.
```APIDOC
## Scatter Chart Component Usage
### Description
This section shows how to import and use the Scatter component in a Svelte application. It includes the necessary Chart.js imports and registration of required modules, along with a sample data structure for a scatter plot.
### Method
Svelte Component Usage
### Endpoint
N/A (Client-side Component)
### Parameters
#### Path Parameters
N/A
#### Query Parameters
N/A
#### Request Body
N/A
### Request Example
```svelte
```
### Response
#### Success Response (200)
N/A (Client-side rendering)
#### Response Example
N/A
```
--------------------------------
### Create a Bubble Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Extends scatter plots by adding a third dimension (radius). Each data point needs x, y, and r values. Scales must be configured.
```svelte
```
--------------------------------
### Polar Area Chart Component
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/polar/+page.md
Demonstrates how to use the PolarArea component with sample data and options.
```APIDOC
## Polar Area Chart Component Usage
### Description
This section shows how to import and use the `PolarArea` component from `svelte-chartjs`, including necessary Chart.js registrations and example data.
### Method
Component Usage
### Endpoint
N/A (Component Integration)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None (Component Props)
### Props
| Prop | Type | Default | Description |
|---|---|---|---|
| `data` | `ChartData<'polarArea'>` | required | Chart data |
| `options` | `ChartOptions<'polarArea'>` | `{}` | Chart.js options |
| `plugins` | `Plugin<'polarArea'>[]` | `[]` | Chart.js plugins |
| `updateMode` | `UpdateMode` | — | Transition mode |
| `chart` | `Chart` | — | Bindable chart instance |
### Request Example
```svelte
```
### Response
#### Success Response (200)
Component renders the Polar Area Chart.
#### Response Example
(Visual rendering of the chart)
```
--------------------------------
### Implement Bar Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Creates vertical or horizontal bar charts, requiring registration of BarElement, CategoryScale, and LinearScale.
```svelte
```
--------------------------------
### Imports
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/api/+page.md
Import statements for using svelte-chartjs components and utilities.
```APIDOC
## Imports
```js
import {
Chart,
Line,
Bar,
Pie,
Doughnut,
Radar,
PolarArea,
Bubble,
Scatter,
getDatasetAtEvent,
getElementAtEvent,
getElementsAtEvent,
} from 'svelte-chartjs';
```
```js
import type { ChartBaseProps } from 'svelte-chartjs';
```
```
--------------------------------
### Line Component Props
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/line/+page.md
Documentation for the properties available for the Line chart component.
```APIDOC
## Line Component Props
### Description
Configuration properties for the Line chart component.
### Parameters
- **data** (ChartData<'line'>) - Required - Chart data object.
- **options** (ChartOptions<'line'>) - Optional - Chart.js configuration options. Default: {}
- **plugins** (Plugin<'line'>[]) - Optional - Array of Chart.js plugins. Default: []
- **updateMode** (UpdateMode) - Optional - Transition mode for chart updates.
- **chart** (Chart) - Optional - Bindable chart instance.
```
--------------------------------
### Handle Chart Click Events in Svelte
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/examples/events/+page.md
Use this snippet to set up event listeners for chart clicks. It imports necessary Chart.js components and svelte-chartjs utility functions. Ensure Chart.js components are registered before use.
```svelte
```
--------------------------------
### Bubble Component API
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/bubble/+page.md
Documentation for the Bubble component props and usage.
```APIDOC
## Bubble Component
### Description
The Bubble component renders a bubble chart using Chart.js within a Svelte application.
### Props
- **data** (ChartData<'bubble'>) - Required - The dataset for the bubble chart.
- **options** (ChartOptions<'bubble'>) - Optional - Chart.js configuration options. Default: {}
- **plugins** (Plugin<'bubble'>[]) - Optional - Array of Chart.js plugins. Default: []
- **updateMode** (UpdateMode) - Optional - Transition mode for chart updates.
- **chart** (Chart) - Optional - Bindable reference to the underlying Chart.js instance.
### Usage Example
```svelte
```
```
--------------------------------
### Optimize Bundle with Selective Imports
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/README.md
Manually import and register only the specific Chart.js components required for the application.
```javascript
import { Line } from 'svelte-chartjs';
import {
Chart as ChartJS,
Title,
Tooltip,
Legend,
LineElement,
LinearScale,
PointElement,
CategoryScale,
} from 'chart.js';
ChartJS.register(Title, Tooltip, Legend, LineElement, LinearScale, PointElement, CategoryScale);
```
--------------------------------
### Create a Doughnut Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Use this component for proportional data with a hollow center. Ensure ArcElement and CategoryScale are registered.
```svelte
```
--------------------------------
### Scatter Chart Props
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/scatter/+page.md
Details the available properties (props) for the Scatter component, their types, default values, and descriptions.
```APIDOC
## Scatter Chart Props
### Description
This section lists and describes the configurable properties for the Scatter component.
### Method
Component Props
### Endpoint
N/A (Client-side Component)
### Parameters
#### Path Parameters
N/A
#### Query Parameters
N/A
#### Request Body
N/A
### Props Table
| Prop | Type | Default | Description |
|---|---|---|---|
| `data` | `ChartData<'scatter'>` | required | Chart data |
| `options` | `ChartOptions<'scatter'>` | `{}` | Chart.js options |
| `plugins` | `Plugin<'scatter'>[]` | `[]` | Chart.js plugins |
| `updateMode` | `UpdateMode` | — | Transition mode |
| `chart` | `Chart` | — | Bindable chart instance |
### Request Example
N/A
### Response
#### Success Response (200)
N/A
#### Response Example
N/A
```
--------------------------------
### Implement Polar Area Chart
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/polar/+page.md
Import the PolarArea component and ChartJS modules. Register necessary Chart.js components before rendering the chart with your data and options.
```svelte
```
--------------------------------
### Event Utility Functions
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/README.md
Helper functions to retrieve chart datasets or elements based on pointer events.
```APIDOC
## Event Utility Functions
### Description
These functions allow developers to extract specific chart data or elements when a user interacts with the chart via pointer events.
### Functions
- **getDatasetAtEvent(chart, event)**: Returns the dataset at the event point.
- **getElementAtEvent(chart, event)**: Returns the nearest element at the event point.
- **getElementsAtEvent(chart, event)**: Returns all elements at the event point.
### Parameters
- **chart** (Object) - Required - The chart instance.
- **event** (Event) - Required - The pointer event object.
### Usage Example
```javascript
function onClick(event) {
if (!chart) return;
const dataset = getDatasetAtEvent(chart, event);
const element = getElementAtEvent(chart, event);
const elements = getElementsAtEvent(chart, event);
console.log({ dataset, element, elements });
}
```
```
--------------------------------
### Use Generic Chart Component
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/README.md
Utilize the generic Chart component by specifying the chart type via the type prop.
```svelte
```
--------------------------------
### Utility Functions
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/api/+page.md
Utility functions for interacting with chart events and elements.
```APIDOC
## Utility Functions
### `getDatasetAtEvent(chart, event)`
Returns all elements belonging to the same dataset as the clicked element.
```ts
function getDatasetAtEvent(chart: Chart, event: PointerEvent): InteractionItem[];
```
### `getElementAtEvent(chart, event)`
Returns the single nearest element to the click point.
```ts
function getElementAtEvent(chart: Chart, event: PointerEvent): InteractionItem[];
```
### `getElementsAtEvent(chart, event)`
Returns all elements at the same data index as the clicked element.
```ts
function getElementsAtEvent(chart: Chart, event: PointerEvent): InteractionItem[];
```
```
--------------------------------
### Create a Radar Chart Component
Source: https://context7.com/sauravkanchan/svelte-chartjs/llms.txt
Ideal for comparing multiple variables across categories. Requires RadialLinearScale and PointElement to be registered.
```svelte
```
--------------------------------
### Custom Size Line Chart
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/+page.md
Control the dimensions of the chart by setting the `width` and `height` props and disabling `maintainAspectRatio` in the options.
```svelte
```
--------------------------------
### Implement Scatter Chart in Svelte
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/charts/scatter/+page.md
Register necessary Chart.js elements before rendering the Scatter component. Ensure all required scales and elements are imported and registered to avoid runtime errors.
```svelte
```
--------------------------------
### Handle Chart Events in Svelte
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/README.md
Use `getDatasetAtEvent`, `getElementAtEvent`, and `getElementsAtEvent` to retrieve chart data and elements on click events. Ensure the chart instance is available before calling these functions.
```svelte
```
--------------------------------
### Access Chart.js instance with bind:chart
Source: https://github.com/sauravkanchan/svelte-chartjs/blob/master/sites/docs/src/routes/examples/ref/+page.md
Use the bind:chart directive to expose the Chart.js instance to a local variable. This allows direct interaction with the Chart.js API after the component mounts.
```svelte
```