Exploring d3.js for the first time, I am attempting to convert the data visualization rules outlined in this guide to work with vue3 and typescript.
Below you will find the code snippet I have been working on:
<template>
<svg
class="line-chart"
:viewBox="viewBox"
>
<g transform="translate(0, 10)">
<path
class="line-chart__line"
:d="path(data)"
/>
</g>
</svg>
</template>
<script lang="ts">
import { ref, defineComponent, computed, toRef } from 'vue';
import * as d3 from 'd3';
export default defineComponent({
name: "StandardMetric",
props: {
data: {
required: true,
type: Array,
},
width: {
default: 500,
type: Number,
},
height: {
default: 270,
type: Number,
},
},
setup(props){
const padding = 60;
const data = [99, 71, 78, 25, 36, 92];
const rangeY = computed(() => {
const defaultHeight: number =props.height?props.height:270;
const height: number = defaultHeight - padding;
return [0, height];
});
const rangeX = computed(() => {
const defaultWidth: number =props.width?props.width:500;
const width: number = defaultWidth - padding;
return [0, width];
});
const path =(linedata: number[])=> {
const maxIndex: number = linedata.length-1;
let maxValue = d3.max(linedata);
maxValue = maxValue?maxValue:0
const x = d3.scaleLinear().range(rangeX.value).domain([0, maxIndex]);
const y = d3.scaleLinear().range(rangeY.value).domain([0, maxValue]);
d3.axisLeft(x);
d3.axisTop(y);
// return d3.line<ChartData>()
// .x((d:ChartData, i) => x(i))
// // .x(d=>x(d))
// .y((d:ChartData) => y(d.p));
return d3.line<number>()
.x((d, i)=>x(i))
.y(d=>y(d))
}
const line = computed(()=> {
return path(data)
})
const viewBox = computed(() =>{
return `0 0 ${props.width} ${props.height}`;
})
return {
line,
viewBox,
path,
data
}
}
}
)
</script>
Upon loading the page, I encountered the following error:
Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "function line(da…". More details...
I have included the package.json content below:
{
"name": "tools-ui",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"serve": "vite preview"
},
"dependencies": {
"@types/d3": "^6.3.0",
"d3": "^6.6.2",
"element-plus": "^1.0.2-beta.36",
"vue": "^3.0.5",
"vue-router": "^4.0.5",
"vuex": "^4.0.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.20.0",
"@typescript-eslint/parser": "^4.20.0",
"@vitejs/plugin-vue": "^1.2.1",
"@vue/compiler-sfc": "^3.0.5",
"eslint": "^7.23.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^7.8.0",
"prettier": "^2.2.1",
"typescript": "^4.1.3",
"vite": "^2.1.5",
"vue-tsc": "^0.0.15"
}
}
I suspect there may be an issue with my path function, but I am unable to pinpoint the exact problem. Any assistance would be greatly appreciated. Thank you!