├── .github
└── workflows
│ └── run-tests.yml
├── .gitignore
├── .prettierrc.json
├── LICENSE
├── README.md
├── dist
├── index.d.ts
├── types
│ ├── Vue3ChartJs.vue.d.ts
│ ├── Vue3ChartJs.vue.d.ts.map
│ ├── includes.d.ts
│ ├── includes.d.ts.map
│ ├── main.d.ts
│ └── main.d.ts.map
├── vue3-chartjs.es.js
└── vue3-chartjs.umd.js
├── eslint.config.mjs
├── index.html
├── jest.config.js
├── lib
├── Vue3ChartJs.vue
├── includes.ts
├── main.ts
└── shims-vue.d.ts
├── package.json
├── src
├── App.vue
├── charts.js
├── direct.html
└── main.js
├── test
├── chart.props.ts
└── wrapper.test.ts
├── tsconfig.json
├── vite.config.js
└── yarn.lock
/.github/workflows/run-tests.yml:
--------------------------------------------------------------------------------
1 | name: Tests
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ '*' ]
8 |
9 | jobs:
10 | test:
11 | runs-on: ubuntu-22.04
12 | strategy:
13 | matrix:
14 | node-version: [ 20 ]
15 | steps:
16 | - uses: actions/checkout@v4
17 |
18 | - name: Use Node.js ${{ matrix.node-version }}
19 | uses: actions/setup-node@v4
20 | with:
21 | node-version: ${{ matrix.node-version }}
22 |
23 | - name: Install
24 | run: npm install --legacy-peer-deps
25 |
26 | - name: Run tests
27 | run: npm test
28 |
29 | - name: Upload coverage to Coveralls
30 | run: cat ./coverage/lcov.info | npx coveralls
31 | env:
32 | COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | coverage
3 | .DS_Store
4 | dist-ssr
5 | *.local
6 |
7 | # Log files
8 | npm-debug.log*
9 | yarn-debug.log*
10 | yarn-error.log*
11 | pnpm-debug.log*
12 |
13 | # Editor directories and files
14 | .idea
15 | .vscode
16 | *.suo
17 | *.ntvs*
18 | *.njsproj
19 | *.sln
20 | *.sw?
--------------------------------------------------------------------------------
/.prettierrc.json:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/J-T-McC/vue3-chartjs/35765dbaaf1cb51ab81004bb0d821ae8aea3ba34/.prettierrc.json
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Tyson McCarney
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Vue3 ChartJS Wrapper
2 |
3 | [](https://coveralls.io/github/J-T-McC/vue3-chartjs?branch=main)
4 | [](https://github.com/J-T-McC/vue3-chartjs/actions/workflows/run-tests.yml)
5 | [](https://github.com/J-T-McC/vue3-chartjs/pulls)
6 | 
7 |
8 | Basic [ChartJS 4](https://www.chartjs.org/) wrapper for [Vue3](https://v3.vuejs.org/)
9 |
10 | For ChartJS 2, see [v0.3.0](https://github.com/J-T-McC/vue3-chartjs/tree/0.3.0)
11 |
12 | For ChartJS 3.1, see [v1.3.0](https://github.com/J-T-McC/vue3-chartjs/tree/v1.3.0)
13 |
14 | ## Requirements
15 |
16 | - Vue 3
17 | - ChartJS 4
18 |
19 | ## Installation
20 |
21 | ```shell script
22 | yarn add chart.js @j-t-mcc/vue3-chartjs
23 |
24 | npm install chart.js @j-t-mcc/vue3-chartjs
25 | ```
26 |
27 | ## Configuration
28 |
29 | Component props use the same structure as ChartJS arguments and are passed as-is to the instance of ChartJS.
30 |
31 | ChartJS charts are responsive by default. If you wish to have a fixed sized chart, you can set the optional `height` and `width` properties, paired with setting responsive to `false` in the `options` property.
32 |
33 | ```js
34 | props: {
35 | type: {
36 | type: String,
37 | required: true
38 | },
39 | height: {
40 | type: Number,
41 | required: false,
42 | default: null
43 | },
44 | width: {
45 | type: Number,
46 | required: false,
47 | default: null
48 | },
49 | data: {
50 | type: Object,
51 | required: true
52 | },
53 | options: {
54 | type: Object,
55 | default: () => ({})
56 | },
57 | plugins: {
58 | type: Array,
59 | default: () => []
60 | }
61 | }
62 | ```
63 |
64 | ## Sandbox Examples
65 |
66 | * [Pie Chart](https://codesandbox.io/s/pie-chart-848by?file=/src/App.vue)
67 | * [Doughnut Chart](https://codesandbox.io/s/doughnut-chart-g7il4?file=/src/App.vue)
68 | * [Bar Chart](https://codesandbox.io/s/bar-chart-kog87?file=/src/App.vue)
69 | * [Radar Chart](https://codesandbox.io/s/radar-chart-j2dyp?file=/src/App.vue)
70 | * [Line Chart with Plugins](https://codesandbox.io/s/plugin-example-o4y3q?file=/src/App.vue)
71 | * [Events & Exports](https://codesandbox.io/s/events-and-exports-q5g9k?file=/src/App.vue)
72 |
73 | View the [ChartJS Docs](https://www.chartjs.org/docs/latest/samples/bar/vertical.html) for more examples.
74 |
75 | ## Events
76 |
77 | A default event hook plugin is injected into each chart object and emits the following events:
78 | [ChartJS events](https://www.chartjs.org/docs/latest/developers/plugins.html#plugin-core-api)
79 |
80 | Event listeners are converted to camelcase in Vue 3. Events marked as "cancellable" in the ChartJS documentation can be "
81 | canceled" by calling the preventDefault method on the event parameter available in your event function.
82 |
83 | ## Methods
84 |
85 | This library only implements a few ChartJS methods for some common interactions and are available by reference:
86 |
87 | ```javascript
88 | chartRef.value.update(animationSpeed = 750)
89 | chartRef.value.resize()
90 | chartRef.value.destroy()
91 | ```
92 |
93 | If you require additional access to ChartJS functionality, you can interact directly with the ChartJS object via the
94 | chartJSState attribute by reference:
95 |
96 | ```javascript
97 | const base64Image = chartRef.value.chartJSState.chart.toBase64Image()
98 | ```
99 |
100 | See the [ChartJS Docs](https://www.chartjs.org/docs/latest/developers/api.html) for more
101 |
102 | ## Examples
103 |
104 | Below are some basic chart examples to get started.
105 |
106 | ### Simple Chart
107 |
108 | ```vue
109 |
110 |
111 |
117 |
118 |
119 |
120 |
162 | ```
163 |
164 | ### Updating chart
165 |
166 | Here is an example of updating the data, labels and title in a chart.
167 |
168 | See the [ChartJS docs](https://www.chartjs.org/docs/latest/developers/updates.html) for more details on updating charts.
169 |
170 | ```vue
171 |
172 |
264 |
265 |
266 |
312 | ```
313 |
314 | ### Adding a plugin
315 |
316 | ChartJS has two different types of plugins: Global & Inline.
317 |
318 | Inline plugins can be passed directly to the chart via the plugins array prop and will be available for that chart only.
319 |
320 | Global plugins require registration with ChartJS and will be available for all charts. Some plugins must be registered.
321 |
322 | Here is an example of adding a global plugin, in this case [`chartjs-plugin-zoom`](https://github.com/chartjs/chartjs-plugin-zoom).
323 |
324 | Global plugins can be registered one of two ways:
325 |
326 | #### Via Component Install
327 |
328 | ```javascript
329 | import Vue3ChartJs from '@j-t-mcc/vue3-chartjs'
330 | import zoomPlugin from 'chartjs-plugin-zoom'
331 |
332 | const Vue = createApp(App)
333 |
334 | Vue.use(Vue3ChartJs, {
335 | plugins: [
336 | zoomPlugin
337 | ]
338 | })
339 |
340 | Vue.mount('#app')
341 | ````
342 |
343 | #### Via Helper Function
344 |
345 | ```javascript
346 | import Vue3ChartJs from '../lib/main'
347 | import zoomPlugin from 'chartjs-plugin-zoom'
348 |
349 | Vue3ChartJs.registerGlobalPlugins([zoomPlugin])
350 | ```
351 |
352 | Example usage with locally imported chart component:
353 |
354 | ```vue
355 |
356 |