Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chartjs-plugin-annotation does not work with vue-chartjs #673

Closed
sshiling opened this issue Dec 15, 2020 · 3 comments
Closed

chartjs-plugin-annotation does not work with vue-chartjs #673

sshiling opened this issue Dec 15, 2020 · 3 comments

Comments

@sshiling
Copy link

sshiling commented Dec 15, 2020

Expected Behavior

Annotation on the chart is displayed.

Actual Behavior

Annotation on the chart is not displayed.

Environment

  • vue.js version: 2.6.12
  • vue-chart.js version: 3.5.1
  • chart.js version: 2.9.4
  • npm version: 6.14.8
  • chartjs-plugin-annotation: 0.5.7

ChartBar.vue

<script>
import { Bar, mixins } from 'vue-chartjs'
import chartjsPluginAnnotation from 'chartjs-plugin-annotation'

const { reactiveProp } = mixins

export default {
  name: 'ChartBar',
  extends: Bar,
  mixins: [reactiveProp],
  props: {
    chartData: { type: Object, required: true, default: () => null },
    options: { type: Object, required: false, default: () => null }
  },
  mounted () {
    this.addPlugin([chartjsPluginAnnotation])
    this.renderChart(this.chartData, this.options)
  }
}
</script>

<style scoped></style>

Page.vue

<template>
    <chart-bar
      :chart-data="chartBarData"
      :options="chartBarData.options"
      :styles="chartBarStyles"
    />
</template>

<script>
import ChartBar from 'components/charts/ChartBar'

export default {
  name: 'Page',
  meta: {
    title: 'Page Title'
  },
  components: {
    ChartBar
  },
  data: () => ({
    chartBarData: {
      labels: [
        '2020-12-01',
        '2020-12-02',
        '2020-12-03',
        '2020-12-04',
        '2020-12-05'
      ],
      datasets: [
        {
          label: 'Container 1',
          borderColor: '#e57373',
          pointBackgroundColor: 'black',
          borderWidth: 1,
          pointBorderColor: 'black',
          backgroundColor: '#e57373',
          data: [19, 31, 3, 5, 2]
        },
        {
          label: 'Container 2',
          borderColor: '#81c784',
          pointBackgroundColor: 'black',
          borderWidth: 1,
          pointBorderColor: 'black',
          backgroundColor: '#81c784',
          data: [13, 8, 21, 3, 4]
        }
      ],
      options: {
        responsive: true,
        maintainAspectRatio: false,
        legend: {
          display: true
        },
        scales: {
          xAxes: [{
            display: true,
            ticks: {
              beginAtZero: true
            }
          }],
          yAxes: [{
            id: 'yAxis',
            display: true,
            ticks: {
              beginAtZero: true
            }
          }]
        },
        annotation: {
          drawTime: 'afterDatasetsDraw',
          annotations: [{
            id: 'maxLine',
            type: 'line',
            mode: 'horizontal',
            scaleID: 'yAxis',
            value: '30',
            borderColor: '#ff00ff',
            borderWidth: 5,
            label: {
              enabled: true,
              position: 'center',
              content: '30'
            }
          }]
        }
      }
    },
    chartBarStyles: {
      height: '300px',
      position: 'relative'
    }
  })
}
</script>

<style lang="stylus" scoped></style>

I've checked these links:
https://stackoverflow.com/questions/56273630/attach-a-custom-plugin-to-vue-chart
vue-chartjs GitHub issue #86

It works on CodeSandbox
https://codesandbox.io/s/wandering-tdd-h8h0m?file=/src/App.vue
But doesn't work in my actual vue project.
I saw the same issue on the internet:
https://www.gitmemory.com/issue/apertureless/vue-chartjs/666/734989814
But can't figure out why it so.

Please could you give me a hint?
I definitely missed something.

@sshiling
Copy link
Author

I found out that it's working with chart.js: 2.9.3, but does not with 2.9.4.

@muitdebos
Copy link

muitdebos commented Jan 6, 2021

For me moving to 2.9.3 didn't work yet, leaving the rest of the code as is. However, the example code I see everywhere (including in your code here) says to do .addPlugin([plugin]), note the Array structure surrounding the plugin. When I check the code in vue-chartjs as shown here https://github.com/apertureless/vue-chartjs/blob/develop/src/BaseCharts.js#L62 it actually uses a .push to add new plugins.

So for me the fix was this.addPlugin(chartjsPluginAnnotation) (note the lack of [ ] surrounding the plugin)

@jastax
Copy link

jastax commented Sep 11, 2021

Same for me, 2.9.3 works, 2.9.4 does not work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants