Reputation: 53
Am trying to design a candlestick chart with java script,but have an error,can any one help me? and my error is:
Uncaught Error: "candlestick" is not a chart type.
I was watching a video and did all steps but still have the error the video link: https://www.youtube.com/watch?v=Pru1dPE0ubI
// setup
const starting = luxon.DateTime.fromRFC2822('01 Aug 2021 00:00 GMT')
const date2 = luxon.DateTime.fromRFC2822('02 Aug 2021 00:00 GMT')
const data = {
datasets: [{
data: [{
x: starting.valueOf(),
o: 1,
h: 1.50,
l: 0.75,
c: 1.25,
},
{
x: date2.valueOf(),
o: 1,
h: 1.50,
l: 0.50,
c: 0.90
}
],
}]
};
// config
const config = {
type: 'candlestick',
data,
options: {}
};
// render init block
const myChart = new Chart(
document.getElementById('chartBig2'),
config
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.0.2/luxon.min.js" integrity="sha512-frUCURIeB0OKMPgmDEwT3rC4NH2a4gn06N3Iw6T1z0WfrQZd7gNfJFbHrNsZP38PVXOp6nUiFtBqVvmCj+ARhw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<link href="/static/profile/js/financial.js" />
Upvotes: 3
Views: 354
Reputation: 1
try this in js file. You should inject 'chartjs-chart-financial' package to chartjs registration.
import {CandlestickController, OhlcController, CandlestickElement, OhlcElement} from 'chartjs-chart-financial';
import Chart from 'chart.js/auto';
import 'chartjs-adapter-luxon';
import '../../front/css/chartjs/style.css';
import { DateTime } from 'luxon';
let barData = [];
let labels = [];
if(data.length == 1){
barData.push({x: null, o: 100, c: 100, h: 100, l: 100});
}
barData.push(...data.map(item =>
({
x: DateTime.fromFormat(item.savedAt, 'yyyy-MM-dd').valueOf(),
o: item.openPrice,
c: item.closePrice,
h: item.highPrice,
l: item.lowPrice
})
)
);
let graph = new Chart(ctx, {
type: 'candlestick',
label: 'prix',
data: {
labels: labels,
datasets: [{
label: 'Prix',
data: barData,
}]
},
options: {
legend: {
display: false
},
scales: {
y: {
beginAtZero: false
},
x: {
type: 'time',
time: {
unit: 'day',
tooltipFormat: 'dd/MM/yyyy',
displayFormats: {
week: 'dd/MM/yyyy',
}
},
ticks: {
source: 'data',
display: true,
maxRotation: 0,
minRotation: 0
},
},
},
},
});
})
Upvotes: 0