パッケージの詳細

svelte-chartjs

SauravKanchan198.3kMIT3.1.5

svelte-chartjs logo

svelte, chartjs

readme

svelte-chartjs

svelte-chartjs logo

Svelte wrapper for chart.js Open for PRs and contributions!

npm version FOSSA Status npm


Install   •   Usage   •   Migration guide   •   Examples   •   Slack   •   Stack Overflow


Install

Install this library with peer dependencies:

pnpm add svelte-chartjs chart.js
# or
yarn add svelte-chartjs chart.js
# or
npm i svelte-chartjs chart.js

Need an API to fetch data? Consider Cube, an open-source API for data apps.


supported by Cube

Usage

<script>
  import { Line } from 'svelte-chartjs'
</script>

<Line data={...} />

Custom Size

In order for Chart.js to obey the custom size you need to set maintainAspectRatio to false, example:

<Line
  data={data}
  width={100}
  height={50}
  options={{ maintainAspectRatio: false }}
/>

Migration from v1 to v2

With v2, this library introduces a number of breaking changes. In order to improve performance, offer new features, and improve maintainability, it was necessary to break backwards compatibility, but we aimed to do so only when worth the benefit.

Change component import path

v1:

import Line from 'svelte-chartjs/src/Line.svelte'

v2:

import { Line } from 'svelte-chartjs'

Tree-shaking

v2 of this library, just like Chart.js v3, is tree-shakable. It means that you need to import and register the controllers, elements, scales, and plugins you want to use.

For a list of all the available items to import, see Chart.js docs.

v1:

import Line from 'svelte-chartjs/src/Line.svelte'

v2 — lazy way:

import { Line } from 'svelte-chartjs'
import 'chart.js/auto';

v2 — tree-shakable way:

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);

Using the "lazy way" is okay to simplify the migration, but please consider using the tree-shakable way to decrease the bundle size.

Please note that typed chart components register their controllers by default, so you don't need to register them by yourself. For example, when using the Pie component, you don't need to register PieController explicitly.

import { Pie } from 'svelte-chartjs';
import { Chart as ChartJS, Title, Tooltip, Legend, ArcElement, CategoryScale } from 'chart.js'

ChartJS.register(Title, Tooltip, Legend, ArcElement, CategoryScale)

Examples

Docs for v1

Full Documentation and demo for v1 here

License

FOSSA Status

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.1.5 (2024-02-06)

Bug Fixes

  • exports: update types path to use the correct type def (539e2c7)

3.1.4 (2024-01-30)

3.1.3 (2024-01-30)

Bug Fixes

  • deps: update dependency svelte to v4 (c8a12cc)
  • size: update size test to use the new build (c850b88)

3.1.2 (2023-01-18)

Bug Fixes

  • upgrade chart.js with fixed types (3bd69dc)

3.1.1 (2023-01-18)

Bug Fixes

3.1.0 (2022-11-17)

Features

Bug Fixes

  • deps: update dependency chart.js to v4 (#84) (112b2fa)

3.0.1 (2022-09-20)

Bug Fixes

3.0.0 (2022-09-19)

⚠ BREAKING CHANGES

  • now package is module type, new exports in package.json

build

2.1.1 (2022-08-29)

Bug Fixes

2.1.0 (2022-08-17)

Features

  • events forwarding, getDatasetAtEvent getElementAtEvent getElementsAtEvent events utils (#63) (e84bd77)
  • new prop types, expose chart instance (#62) (d59111a)
  • update mode prop (#65) (d6e5506)

Bug Fixes

  • rename components Base -> Chart, Polar -> PolarArea (#64) (ec905cc)

2.0.1 (2022-07-20)

Bug Fixes

2.0.0 (2022-07-20)

⚠ BREAKING CHANGES

  • svelte files were removed from the package, now you should use named exports
  • add typescript and tree-shaking for chart.js imports

Features

Bug Fixes

  • base: Add chart.destroy() to fix memory leak. Fixes #25 (d49ffa1)
  • remove svelte files from package (#54) (82b871b)