Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data point in Chart js line chart not positioned along y axis correctly when retrieve data from firestore

I am having an issues where my data points from data retrieved from firestore are not correctly positioned for the y-axis though the tooltip data is correct. For example, the data below is suppose to be at 23 but its positioned at 100+.

When I manually type in the data, it is positioned correctly so I thought that it might be an issue with the data type after retrieval from the database but I checked and it is appropriately a ‘number’ data type.

I am not so sure how this issue came about and how to rectify it. I would greatly appreciate your help on this problem! Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

<script>
    import {
        auth,
        db
    } from "../../src/main";
    import Sidebar from "../components/Navigation/Sidebar.vue";
    import Topbar from "../components/Navigation/Topbar.vue";
    import ChartTest from "../components/ProgressPage/ChartTest.vue";
    import Chart from 'chart.js/auto';
    import {
        getFirestore,
        doc,
        updateDoc,
        getDoc,
        setDoc,
        collection,
        addDoc,
        deleteDoc,
        deleteField,
        arrayUnion,
        arrayRemove
    } from "firebase/firestore";
    export default {
        name: "Progress",
        components: {
            Sidebar,
            Topbar,
            ChartTest
        },
        mounted() {
            const progressChart = new Chart(document.getElementById("progress-chart"), {
                type: 'line',
                data: {
                    labels: ['CA1', 'SA1', 'CA2', 'SA2'],
                    datasets: [

                    ]
                },
                options: {
                    plugins: {
                        title: {
                            display: true,
                            text: this.title
                        }
                    },
                    scales: {

                        y: {
                            display: true,
                            stacked: true,
                            // max: 0,
                            // min: 200,
                            title: {
                                display: true,
                                text: 'Your Score (%)'
                            }
                        }
                    }
                }
            });

            this.getData().then((data) => {
                progressChart.data.datasets=data
                console.log(typeof(data[0].data[0]))//number data type
                this.getCount(data)
                progressChart.update()  //updating graph with new set of data her

            })


        },
    methods: {
        getCount(data) {
                 this.count= data.length
            },
            async getData() {
                var email = localStorage.getItem("email");
                var ref = doc(db, 'users', email);
                const docSnap = await getDoc(ref)
                if (docSnap.exists()) {
                    var data = docSnap.data().progressResults
                    return data

                } else {
                    console.log('does not exist')
                }


            },
            async addResult() {

                let count = this.existingSubjects.length
                var email = localStorage.getItem("email");
                var ref = doc(db, 'users', email);

                if (!this.existingSubjects.includes(this.subject)) {
                    this.existingSubjects.push(this.subject)
                  
                    const newData = {
                        data: [{ x: this.examType,y:this.score}],
                        label: this.subject,
                        borderColor: this.colors[this.count],
                        fill: false
                    }
                    await updateDoc(
                        ref, {
                            progressResults: arrayUnion(newData)

                        }
                    )
                    console.log(newData)
                } else {
                    //TBC
                }


            }
        },

        data() {
            return {
                score: '',
                examType: '',
                subject: '',
                count:0,
                existingSubjects: [],
                colors: ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850","#21c095","#bbc021","#1a993a","##904b23","#a01359","#a04913","#534270"],
                title: '',
                data: {
                    labels: ['CA1', 'SA1', 'CA2', 'SA2'],
                    datasets: [
                    ]
                },
                tabs: [{
                        link: '',
                        name: "subject",
                        dropdown: true,
                        dropdownTabs: [{
                                name: 'math',
                                link: '#'
                            },
                            {
                                name: 'science',
                                link: '#'
                            },
                        ]

                    },
                    {
                        link: '#',
                        name: "test",
                        dropdown: false,
                        dropdownTabs: []

                    },
                ]

            }
        }
    }
</script>

>Solution :

This is because you set your y axis to stacked so it puts all values on top of each other where the space between the lines is the value you provide.

To get the behaviour you want you need to remove the stacked or set it to false:

y: {
  stacked: false
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading