I have a problem with import data using chart.js. I am making aplications in node.js and i would like to import data from another file (use require).
When i import data then my chart not loading and console log 'require is not defined'.
const data= require('../db/controllers/data')
const ctx = document.querySelector("#myChart").getContext("2d")
let delayed
let gradient = ctx.createLinearGradient(0, 0, 0, 400)
gradient.addColorStop(0, "rgba(58,123,213,1")
gradient.addColorStop(1, "rgba(0,210,255, 0.3")
const labels = [
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
"2018",
"2019",
"2020",
]
const data = {
labels,
datasets: [
{
data: data,
label: "Test",
fill: true,
backgroundColor: gradient,
borderColor: '#fff',
pointBackgroundColor: 'rgb(189, 195, 199'
},
],
}
const config = {
type: "line",
data: data,
options: {
radius: 5,
hitRadius: 30,
hoverRadius: 12,
responsive: true,
animation: {
onComplete: () => {
delayed = true
},
delay: (context) => {
let delay = 0
if( context.type === 'data' && context.mode === 'default' && !delayed) {
delay = context.dataIndex * 300 + context.datasetIndex * 100
}
return delay
}
},
scales: {
y: {
ticks: {
callback: function (value) {
return "$" + value + "m"
},
},
},
},
},
}
const myChart = new Chart(ctx, config)
I try a many soultions but i don't know how i can import data from another file in Module format. Thanks you very much for your help! :)