Mermaid FlowChart not rendering correctly with dynamic data in Vue 3

Viewed 440

I am using the mermaid library to create Flowcharts. When I am using static data to create a sample chart it's working completely fine. But as soon as I try to replace the static data with my fetched data the chart breaks and instead of showing the chart it prints out the graph syntax.

This is my code:

<template>
        <div>
            <VueMermaid
                type="graph TD"
                :nodes="dataChart"
            />
        </div>
</template>

<script>
import axios from "axios";
import VueMermaid from "../../components/FlowchartComponent.vue";

export default {
    components: {
        VueMermaid
    },
    data() {
        return {
            dataChart:[
                {id:'0',text:'Node0', next:['1']},
                {id:'1',text:'Node1',next:['2','3', '4', '5']},
                {id:'2',text:'Node2', next:['6']},
                {id:'3',text:'Node3', next:['6']},
                {id:'4',text:'Node4', next:['6']},
                {id:'5',text:'Node5', next:['6']},
                {id:'6',text:'Node6'}
            ]
        }
    },
    setup() {
        
        let dataList = [];
        
        axios.get(`link_to_API`)
        .then((data) => {
            console.log(data);
            console.log("tasks", data.data.tasks[0]);
            console.log("tasks_id", data.data.tasks[0].task_id);
            console.log("downstream", data.data.tasks[0].downstream_task_ids[0]);
            
            
            let selection1 = data.data.tasks;

            //create for each task an item in dataList with id, name of node and next node name
            for (let z in selection1){
                let item={id: "", text: "", next:""};
                item.id=z;
                item.text=data.data.tasks[z].task_id;
                item.next=data.data.tasks[z].downstream_task_ids;
                dataList.push(item);
                // console.log("item", item);
            }
            console.log("dataList", dataList);

            // replace in dataList for each item the value of next (string) with the id of the node that has the same string as text
            for(let i in dataList){
                console.log("dataList[i].next", dataList[i].next);
                for(let h in dataList[i].next){
                    console.log("dataNext einzeln", dataList[i].next[h]);
                    let obj = dataList.find(o => o.text === dataList[i].next[h]);
                    console.log("obj", obj);
                    // console.log("obj.id", obj.id);
                    dataList[i].next[h] = obj.id;
                }
            }
            console.log("newdataList", dataList);
        });

        return {           
            dataList
        }
    },
    // Component should only be built when fetched data is ready
    async created(){
        await axios.get(`link_to_API`);

        console.log("this.dataChart", this.dataChart);
        //replace values of dataChart with values of dataList
        this.dataChart = this.dataList;
        console.log("this.dataChart", this.dataChart);
    }
}
</script>

<style scoped>
</style>

The FlowchartComponent I am using contains the code that is used in the vue-mermaid library: https://github.com/robin1liu/vue-mermaid/blob/master/src/vue-mermaid.vue and the mermaidjs library itself has been installed via npm.

The expected outcome of the code would be a usual mermaif flowchart. This works as long as I only use the dataChart in the data() return{}. But as soon as I try to change that data like I did it in the mounted(), I get something like this:

graph TD 0[Node0]-->1[Node1] 1[Node1]-->2[Node2] 1[Node1]-->3[Node3] 1[Node1]-->4[Node4] 1[Node1]-->5[Node5] 2[Node2]-->7[Node7] 3[Node3]-->7[Node7] 4[Node4]-->7[Node7] 5[Node5]-->7[Node7] 7[Node7]

Does anyone know why that happens?

0 Answers
Related