I’m trying to put three functions that collect data and draw a chart.js when a circle on the map is clicked in leaflet.
This is my circle(marker):
const station = L.circle([X, Y], {
color: color,
fillColor: fillColor,
fillOpacity: 0.5,
radius: 2000,
}).on('click', function() {
showChart();
listDataDates(mikeDates);
mikeChart(mikeData);
}).addTo(map);
station.bindPopup(`${nameBg} (Max level Mike11: ${maxLevel11}) (Max level SURFEX-RAPID: ${maxLevelFW})`);
Next function showChart() get the name and send it to the api url like parameter to get dates from the query:
//Create variables for clicked names and dates
let ulNames = document.getElementById('clickedNames');
let ulDates = document.getElementById('clickedDates');
ulNames.addEventListener('click', showNames);
ulDates.addEventListener('click', showDates);
async function showChart() {
document.getElementById("nameStation").innerHTML = nameBg;
document.getElementById("clicked").innerHTML = nameBg;
const apiUrl = "192.168.0.1:3010/dates?id=" + nameBg;
const response = await fetch(apiUrl);
const mikeDates = await response.json();
dataAPI.dateStation = mikeDates;
dataAPI.nameStation = nameBg;
listDataDates(mikeDates);
}
This function put the dates on the dropdown and put the last date like parameter to the url to get more data for the chart.js
function listDataDates(mikeDates) {
const formatDate = "YYYY-MM-DD HH:mm";
//console.log(JSON.stringify(data, null, 2))
// Loop through each result and append the data.
mikeDates.floodguard_dates.rows.map((clickedDates) => {
const fillDates = `
<li><a class="dropdown-item" href="#">${moment(clickedDates.datprog).format(formatDate)}</a></li>`;
const item = document.createElement('li');
item.addEventListener('click', async (e) => {
if (!e || !e.target || !e.target.innerHTML) {
alert('Възникна неочаквана грешка !');
//dataAPI.dateStation = null;
return;
}
if (!dataAPI.nameStation) {
alert('Моля, изберете местоположение.');
//dataAPI.dateStation = null;
return;
}
dataAPI.dateStation = e.target.innerHTML;
const apiUrl = "192.168.0.1:3010/mikealldata?id=" + moment(mikeDates.floodguard_dates.rows[0].datprog).format(formatDate) + "&id2=" + dataAPI.nameStation;
const response = await fetch(apiUrl)
const mikedata = await response.json()
const mike11date = mikedata.floodguard_mikealldata.rows.map((x) => x.datechar)
//console.log(mike11date)
const mike11value = mikedata.floodguard_mikealldata.rows.map((x) => x.level_11)
//console.log(mike11value)
const mikefwdate = mikedata.floodguard_mikealldata.rows.map((x) => x.datechar)
//console.log(mikefwdate)
const mikefwvalue = mikedata.floodguard_mikealldata.rows.map((x) => x.Discharge)
//console.log(mikefwvalue)
const mikeFirstCodeWarning = mikedata.floodguard_mikealldata.rows.map((x) => x.ALERT_yellow);
const mikeSecondCodeWarning = mikedata.floodguard_mikealldata.rows.map((x) => x.ALERT_orange);
const mikeThirdCodeWarning = mikedata.floodguard_mikealldata.rows.map((x) => x.ALERT_red);
mike11LabelChart = mike11date;
mike11DataChart = mike11value;
mikeFWLabelChart = mikefwdate;
mikeFWDataChart = mikefwvalue;
mikeYellowWarning = mikeFirstCodeWarning;
mikeOrangeWarning = mikeSecondCodeWarning;
mikeRedWarning = mikeThirdCodeWarning;
let showCurrChart = document.querySelector(".chart-container");
showCurrChart.style.display = 'inline';
mikeChart();
//document.getElementById("nameStation").innerHTML = "Избрали сте: " + dataAPI.nameStation;
document.getElementById("dateStation").innerHTML = "Дата на изчесление на прогнозата: " + moment(dataAPI.dateStation).format(formatDate);
});
item.innerHTML = fillDates;
list.appendChild(item);
});
// Finally append all the data to the UL.
ulDates.appendChild(list);
const fillDate = moment(mikeDates.floodguard_dates.rows[0].datprog).format(formatDate);
document.getElementById("clicked2").innerHTML = fillDate;
}
This function destroy and create new chart:
//Create array for Mike11 and MikeFW dates on labels and values for chart
var mike11LabelChart = [], mike11DataChart = [];
var mikeFWLabelChart = [], mikeFWDataChart = [];
var mikeYellowWarning = [], mikeOrangeWarning = [], mikeRedWarning = [];
async function mikeChart() {
//Calling function who fetch the data for charts
//await getMikeFWData();
//Create a instance of html chart
const ctx = document.getElementById('chartVisualizeContainerMike11');
let child = ctx.lastElementChild;
if (child) {
ctx.removeChild(child)
}
let canvasElement = document.createElement('canvas');
canvasElement.id = 'mikeChart';
ctx.appendChild(canvasElement);
canvasElement.height = 380;
//canvasElement.width = 680;
//canvasElement.aspectRatio = 1;
const a = new Chart(canvasElement, {
data: {
labels: mikeFWLabelChart,
datasets: [{
type: 'line',
label: 'водно количество [куб. м./сек.]',
data: mikeFWDataChart,
pointRadius: 1,
borderWidth: 3.5,
borderColor: '#00b3ff',
backgroundColor: "#00b3ff",
pointBackgroundColor: "#00b3ff",
pointBorderColor: "rgba(255, 255, 255, 0)",
pointHoverBackgroundColor: "#00b3ff",
pointHoverBorderColor: "#00b3ff",
showToolTips: false,
}, {
type: 'line',
label: 'надморска височина [метри]',
data: mike11DataChart,
pointRadius: 1,
borderWidth: 3.5,
borderColor: '#754617',
backgroundColor: "#754617",
pointBackgroundColor: "#754617",
pointBorderColor: "rgba(255, 255, 255, 0)",
pointHoverBackgroundColor: "#754617",
pointHoverBorderColor: "#754617",
//pointHoverBorderColor: "#0022ff",
showToolTips: false,
}, {
type: 'line',
label: 'предупреждение I',
data: mikeYellowWarning,
pointRadius: 1,
borderWidth: 3,
borderColor: '#eeff00',
backgroundColor: "#eeff00",
pointBackgroundColor: "#eeff00",
pointBorderColor: "rgba(255, 255, 255, 0)",
pointHoverBackgroundColor: "#eeff00",
pointHoverBorderColor: "#eeff00",
showToolTips: false,
}, {
type: 'line',
label: 'предупреждение II',
data: mikeOrangeWarning,
pointRadius: 1,
borderWidth: 3,
borderColor: '#ff8400',
backgroundColor: "#ff8400",
pointBackgroundColor: "#ff8400",
pointBorderColor: "rgba(255, 255, 255, 0)",
pointHoverBackgroundColor: "#ff8400",
pointHoverBorderColor: "#ff8400",
showToolTips: false,
}, {
type: 'line',
label: 'предупреждение III',
data: mikeRedWarning,
pointRadius: 1,
borderWidth: 3,
borderColor: '#ff0000',
backgroundColor: "#ff0000",
pointBackgroundColor: "#ff0000",
pointBorderColor: "rgba(255, 255, 255, 0)",
pointHoverBackgroundColor: "#ff0000",
pointHoverBorderColor: "#ff0000",
showToolTips: false,
},]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
ticks: {
maxRotation: 90,
minRotation: 90
}
}, y: {
ticks: {
// Include a dollar sign in the ticks
callback: function (value, index, ticks) {
return value;
}
}
}
}
}
})
};
For now when I using this code I receive error: Uncaught ReferenceError: mikeDates is not defined
when I trying to call listDataDates(mikeDates); in the circle
Is there a way to draw a chart when I click on the circle ?