1

I want to access getValue method from Chart object, but I get function undefined.

<template>
    <div>
        <canvas width="600" height="400" ref="canvas"></canvas>
    </div>
</template>
<script>
    import Vue from 'vue';
    import Chart from 'chart.js';
    import Axios from 'axios';

    export default {
        mixins: [DateRangeMixin],
        props: {
          // other props...
            callback: false,
        },

        data() {
            return {
                chart: '',
            };
        },

        mounted() {
       // ...
        },

        methods: {

      //other methods...,
               getValue(data) {
                    if (data === 1) {
                        return 'Up'
                    } else if(data === 0) {
                        return 'Down';
                    }
                },
            render(data) {
                this.chart = new Chart(this.$refs.canvas, {
                    type: 'line',
                    data: {

                        labels: Object.keys(data),
                        datasets: [{
                           // a lot of data ....                   
                            data: Object.values(data),

                        }]

                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true,
                                    callback(label, index, labels) {
                                        return this.getValue(label); // <-- Tried this and got: 'this.getValue is not a function'. I understand it bounces to new Chart object, but how to resolve this?
                                    }
                                }
                            }]
                        }
                    }

                });

            },
        },
    };
</script>

I understand that it's because Chart is an object and this is pointing to it, but how do I resolve this and access my method from the callback ?

I imagine if that export default... would be set to a variable, then I could access my method via variable.methods.getValue , but in this scenario How can I achieve my goal ?

4
  • 1
    why don't you just give the canvas an id, do document get element and feed it the data, labels and options. No need to complicate it. otherwise use vue chartjs wrapper github.com/apertureless/vue-chartjs Commented Dec 25, 2017 at 11:34
  • @samayo I have a big project here and reverting back to playing with DOM and even jQuery would be an overkill in then end. The chartjs wrappers seems to be the best choice I have atm - didn't knew it existed - Thanks! Commented Dec 25, 2017 at 12:55
  • 2
    Just assign var self = this; right before you create the Chart, and then you can use self to access your component properties... Commented Dec 25, 2017 at 13:22
  • @DavidHeremans Yup! That solved my problem in a way I was looking for! Thanks!! Commented Dec 25, 2017 at 13:30

1 Answer 1

1

Right before you create the new Chart() assign this to a variable self: var self = this;. You can then access your component properties throughself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.