0

I have started with vue and D3. I just want to show my csv data in the console but when I try to do it with D3 CSV function is not working at all. Appears an array of 16 HTML elements which are in index.html, would you mind helping me with this?

This is my project structure:

enter image description here

This is my component code:

<template>

</template>

<script>

import * as d3 from "d3";
import { csv } from 'd3';

export default {
    name: 'mycomponent',
    data() {
        return{
            dataset: [],
        }
    },
    async mounted(){
        const data = await d3.csv('./datasets/dataset.csv')
        this.$data.$dataset = Object.freeze(data)
        console.log(data);
    }
}


</script>

This is home:

<template>
    <mycomponent></mycomponent>
</template>

<script>

import mycomponent from '@/components/mycomponent.vue'

export default {
  name: 'Home',
  components: {
    mycomponent,
  },
}
</script>

And this is what I get in console:

enter image description here

1 Answer 1

1

The d3.csv function will execute at runtime not compile-time so you have to put your csv file in public directory then use it as usual public files.

let data = await d3.csv("/datasets/dataset.csv")

Or if you want to load your csv file at compile-time you can import it as string and use d3.csvParse instead.

import dataset from '@/datasets/dataset.csv'

let data = d3.csvParse(dataset);

I would prefer the first method, in the second method your csv file might cause your script file too big.

Example

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.