Vue-chartjs: onClick event in Pie chart

Created on 25 Jul 2018  路  3Comments  路  Source: apertureless/vue-chartjs

Hi, I'm trying to get the click event when the user click in a pie chart, I try with this code, but I can't get the chart instance in the line "this.$data._chart.getElementsAtEvent(evt)", because this.$data is undefined.

<script>
import { Pie } from 'vue-chartjs'

export default {
  props: ['data', 'title'],

  data() {
      return {
          options: {
            responsive: true, 
            maintainAspectRatio: false,

            title: {
                display: true,
                text: this.title,
                fontSize: 18,
                fontFamily: 'Zil Semi Slab',
                padding: 20
            }

        }
      }
  },
  extends: Pie,

  mounted () {
    this.renderChart(this.data, this.options);

    this.$refs.canvas.onclick = function(evt) {
        this.$data._chart.getElementsAtEvent(evt);
    }

  }
}

</script>

Most helpful comment

Probably a scope issue. Anyway, instead of using this.$refs.canvas.onclick you should use the onClick function provided by chartjs, and use a temp variable to target one of your custom method:

var _this = this;
var options = {
    onClick: function(evt) {
        _this.myCustomMethod(evt);
    }
};
this.renderChart(this.data, this.options);

All 3 comments

Probably a scope issue. Anyway, instead of using this.$refs.canvas.onclick you should use the onClick function provided by chartjs, and use a temp variable to target one of your custom method:

var _this = this;
var options = {
    onClick: function(evt) {
        _this.myCustomMethod(evt);
    }
};
this.renderChart(this.data, this.options);

Yes, I resolved this with the onClick function. Thank you.

Probably a scope issue. Anyway, instead of using this.$refs.canvas.onclick you should use the onClick function provided by chartjs, and use a temp variable to target one of your custom method:

var _this = this;
var options = {
  onClick: function(evt) {
      _this.myCustomMethod(evt);
  }
};
this.renderChart(this.data, this.options);

esto no me funciona

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kurbar picture kurbar  路  4Comments

LeeLenaleee picture LeeLenaleee  路  3Comments

aido179 picture aido179  路  3Comments

Scalpel78 picture Scalpel78  路  4Comments

jcalonso picture jcalonso  路  4Comments