const requestURL = 'https://api.coincap.io/v2/candles?exchange=poloniex&interval=h8&baseId=ethereum"eId=bitcoin';
let offset = 0;
const datatt = anychart.data.table(0);
const data = anychart.data.table(0);
async function requestAndTreatment() {
const response = await fetch(requestURL)
.then(response => response.json());
const arr = response.data;
const arrReversed = [...arr].reverse();
const pointA = arrReversed[offset++];
const pointALow = Number(pointA.low);
const pointAHigh = Number(pointA.high);
const pointB = arr[offset++];
const pointBLow = Number(pointB.low);
const pointBHigh = Number(pointB.high);
const averageA = (pointALow + pointAHigh) / 2;
const averageB = (pointBLow + pointBHigh) / 2;
datatt.addData([[response.timestamp, averageA]]);
data.addData([[response.timestamp, averageB]]);
}
window.addEventListener('DOMContentLoaded', () => {
const jccBtnLine = document.querySelector('#jcc_btn_line');
const jccLine = document.querySelector('#jcc_line');
const chBox = document.querySelector('#checkbox');
let intervalId = null;
jccBtnLine.addEventListener('click', () => {
jccLine.style.fill = '#1976d2';
clearInterval(intervalId);
intervalId = setInterval(requestAndTreatment, 5000);
});
let secondSeries = null;
chBox.addEventListener('change', () => {
if (!chart) {
return;
}
if (chBox.checked) {
secondSeries = chart.plot(0).addSeries(data)[0];
secondSeries.name('НАЦБанк');
} else {
if (secondSeries) {
chart.plot(0).removeSeries(secondSeries.id());
secondSeries = null;
}
}
});
anychart.onDocumentReady(() => {
chart = anychart.stock();
chart.interactivity().hoverMode('single');
chart.plot(0).line(datatt);
chart.container('containertt');
chart.draw();
});
});