You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The chart will still render without client-only however the client side DOM tree will no longer match the server render and a console error will be shown.
Multi chart example
You can add multiple Chart.js types to your chart.js plugin file like so:
import Vue from 'vue'
import { Line } from 'vue-chartjs'
import { Pie } from 'vue-chartjs'
Vue.component('line-chart', {
extends: Line,
props: ['data', 'options'],
mounted () {
this.renderChart(this.data, this.options)
}
})
Vue.component('pie-chart', {
extends: Pie,
props: ['data', 'options'],
mounted () {
this.renderChart(this.data, this.options)
}
})
@WesThorburn Thanks for sharing the instruction. They are great starting point, especially for front-end newbie like me :)
I encountered few issues and below is how they got resolved. Perhaps these issues are resulted from changes / new requirement introduced in recent versions, just my guess.
During runtime I was getting error complaining that required props chartData is missing.
My solution: rename the data props as chartData.
The chart background and axis (incl. axis scale) were displayed properly, but the data line was not coming.
My solution: within the chartData object, in addition to datasets, add a labels array, each element is the label for a data point.
Voila!
I still need to explore more especially customisation of the chart.
BTW, may I know why it has to be implemented as Nuxt plugin rather than standard Vue component?
Initially I tried to implement the chart as Vue component, but the import statement (from vue-chartjs/legacy) seems to cause runtime error Cannot use import statement outside a module. When I search this error message in google, some responses in StackOverflow suggest to implement thing as Nuxt plugin instead of component. Eventually I landed on this page.
This worked well for me with everyone's tips. I had to remove <client-only>...</client-only> .. it was causing some screwy page rendering behavior. My charts were showing up only after saving my Nuxt project, but not on a page refresh. Removing that resolved the issue for me and I haven't noticed any problems or console errors without it
I'm using chartsjs in markdown files with embedded Vue components.
I'm getting this error:
ERROR in ./node_modules/vue-chartjs/dist/index.js 216:37
Module parse failed: Unexpected token (216:37)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const ref = shallowRef(null);
| const reforwardRef = (chartRef)=>{
I'm getting this error: ERROR in ./node_modules/vue-chartjs/dist/index.js 216:37 Module parse failed: Unexpected token (216:37) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders | const ref = shallowRef(null); | const reforwardRef = (chartRef)=>{
ref.value = chartRef?.chart;
| }; | expose({
package.json
"nuxt": "^2.15.8", "vue": "^2.7.10",
you have to use this versions
{
...
"dependencies": {
"chart.js": "2.9.4",
"vue-chartjs": "^3.5.1"
}
...
}
@WesThorburn Thanks for sharing the instruction. They are great starting point, especially for front-end newbie like me :)
I encountered few issues and below is how they got resolved. Perhaps these issues are resulted from changes / new requirement introduced in recent versions, just my guess.
My versions:
For Vue 2, import the chart component from
vue-chartjs/legacy
instead of from'vue-chartjs'
.https://vue-chartjs.org/guide/#creating-your-first-chart
Regardless of Vue 2 or 3, import and register few more elements from
chart.js
. Refer theLine.vue
code in the official example:https://codesandbox.io/s/github/apertureless/vue-chartjs/tree/main/legacy/sandboxes/line?file=/src/components/Line.vue
During runtime I was getting error complaining that required props
chartData
is missing.My solution: rename the
data
props aschartData
.The chart background and axis (incl. axis scale) were displayed properly, but the data line was not coming.
My solution: within the
chartData
object, in addition todatasets
, add alabels
array, each element is the label for a data point.Voila!
I still need to explore more especially customisation of the chart.