I want to create a candelstick charts with node.js and save it as a picture, i am using the following code:
const { JSDOM } = require('jsdom');
const { document } = new JSDOM('').window;
global.document = document;
const Chart = require('chart.js');
async function ChartSpeichern() {
// Erstellen eines neuen Chart-Objekts
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const candlestickData = [
{ open: 100, high: 120, low: 80, close: 110 },
{ open: 110, high: 130, low: 90, close: 120 },
{ open: 120, high: 140, low: 100, close: 130 },
// Weitere Candlesticks hinzufügen...
];
const chart = new Chart(ctx, {
type: 'candlestick',
data: {
datasets: [{
label: 'Candlestick Chart',
data: candlestickData.map((candle) => ({
t: new Date(), // Hier können Sie ein genaues Datum setzen
o: candle.open,
h: candle.high,
l: candle.low,
c: candle.close,
})),
}],
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'day',
},
},
},
},
});
// Zeichnen des Charts
chart.draw();
// Hier können Sie das Bild als Datei speichern (z.B., als PNG)
const fs = require('fs');
const dataUrl = canvas.toDataURL();
const base64Data = dataUrl.replace(/^data:image\/png;base64,/, '');
fs.writeFileSync('candlestick_chart.png', base64Data, 'base64');
}
// Rufen Sie die Funktion auf
ChartSpeichern();
i get then the error
throw new Error(‘This method is not implemented: Check that a complete date adapter is provided.’);
^
Error: This method is not implemented: Check that a complete date adapter is provided.
Then i install this here: npm install chartjs-adapter-date-fns
and i use this code:
const { JSDOM } = require('jsdom');
const { document } = new JSDOM('').window;
global.document = document;
// Hier fügen wir den import für den Datumsadapter hinzu
const Chart = require('chart.js');
const { ChartDate, registerables } = require('chartjs-adapter-date-fns');
Chart.register(...registerables);
async function ChartSpeichern() {
// Erstellen eines neuen Chart-Objekts
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const candlestickData = [
{ open: 100, high: 120, low: 80, close: 110 },
{ open: 110, high: 130, low: 90, close: 120 },
{ open: 120, high: 140, low: 100, close: 130 },
// Weitere Candlesticks hinzufügen...
];
const chart = new Chart(ctx, {
type: 'candlestick',
data: {
datasets: [{
label: 'Candlestick Chart',
data: candlestickData.map((candle) => ({
t: new Date(), // Hier können Sie ein genaues Datum setzen
o: candle.open,
h: candle.high,
l: candle.low,
c: candle.close,
})),
}],
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'day',
},
},
},
},
});
// Zeichnen des Charts
chart.draw();
// Hier können Sie das Bild als Datei speichern (z.B., als PNG)
const fs = require('fs');
const dataUrl = canvas.toDataURL();
const base64Data = dataUrl.replace(/^data:image\/png;base64,/, '');
fs.writeFileSync('candlestick_chart.png', base64Data, 'base64');
}
// Rufen Sie die Funktion auf
ChartSpeichern();
but then i get this error
const { ChartDate, registerables } = require(‘chartjs-adapter-date-fns’);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module
so since few hours i try this with help of ChatGPT but we dont come to a result, just because of this time adapter or so, the normal graphs are working but when i want to create a candelstick chart with node js i just get error, would be cool if somebody know a solution that he can share.