class CustomNdvilegend extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
0.0
0.2
0.4
0.6
0.8
1.0
Critical
0.0 - 0.2
No vegetation
Poor
0.2 - 0.3
Sparse vegetation
Moderate
0.3 - 0.5
Moderate growth
Fair
0.5 - 0.6
Healthy growth
Good
0.6 - 0.8
Very healthy
Excellent
0.8 - 1.0
Thriving growth
`;
// Initialize tooltips
this.initializeTooltips();
// Update current marker position
this.updateCurrentMarker();
// Replace feather icons
setTimeout(() => {
if (window.feather) {
window.feather.replace();
}
}, 100);
}
initializeTooltips() {
const healthLevels = this.shadowRoot.querySelectorAll('.health-level');
healthLevels.forEach(level => {
const tooltipText = level.getAttribute('data-tooltip');
level.addEventListener('mouseenter', (e) => {
const tooltip = document.createElement('div');
tooltip.className = 'tooltip';
tooltip.textContent = tooltipText;
const rect = level.getBoundingClientRect();
tooltip.style.left = `${rect.left + rect.width/2}px`;
tooltip.style.top = `${rect.top - 10}px`;
tooltip.style.transform = 'translateX(-50%) translateY(-100%)';
document.body.appendChild(tooltip);
// Show tooltip
setTimeout(() => {
tooltip.style.opacity = '1';
}, 10);
level._tooltip = tooltip;
});
level.addEventListener('mouseleave', () => {
if (level._tooltip) {
level._tooltip.remove();
level._tooltip = null;
}
});
});
}
updateCurrentMarker() {
const currentNDVI = 0.62; // This could be dynamic
const marker = this.shadowRoot.querySelector('.current-marker');
const ndviValue = this.shadowRoot.querySelector('.ndvi-value');
if (marker) {
const position = currentNDVI * 100;
marker.style.left = `${position}%`;
// Update color based on NDVI value
let color;
if (currentNDVI < 0.2) color = '#d32f2f';
else if (currentNDVI < 0.3) color = '#f57c00';
else if (currentNDVI < 0.5) color = '#fbc02d';
else if (currentNDVI < 0.6) color = '#7cb342';
else if (currentNDVI < 0.8) color = '#388e3c';
else color = '#1b5e20';
marker.style.background = color;
marker.style.setProperty('--marker-color', color);
// Update the displayed value
if (ndviValue) {
ndviValue.textContent = currentNDVI.toFixed(2);
}
}
}
}
customElements.define('custom-ndvi-legend', CustomNdvilegend);