CSNET's picture
ลองสร้าง html5 ที่ใช้ NDVI กับไร่จริงในประเทศไทยมาซัก 1 แปลงให้ดูให้เห็นภาพหน่อย
b263139 verified
Raw History Blame Contribute Delete
14.3 kB
class CustomNdvilegend extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
.legend-container {
background: white;
border-radius: 0.75rem;
padding: 1.5rem;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
border: 1px solid #e5e7eb;
}
.legend-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 1.25rem;
}
.legend-title {
font-size: 1.125rem;
font-weight: 600;
color: #111827;
display: flex;
align-items: center;
gap: 0.5rem;
}
.legend-title i {
color: #10b981;
}
.ndvi-value {
font-size: 1.5rem;
font-weight: 700;
color: #059669;
background: #d1fae5;
padding: 0.25rem 0.75rem;
border-radius: 0.5rem;
}
.gradient-container {
position: relative;
height: 2rem;
border-radius: 0.5rem;
overflow: hidden;
margin-bottom: 1rem;
background: linear-gradient(90deg,
#d32f2f 0%,
#f57c00 20%,
#fbc02d 40%,
#7cb342 60%,
#388e3c 80%,
#1b5e20 100%);
}
.gradient-labels {
display: flex;
justify-content: space-between;
margin-bottom: 1.5rem;
}
.gradient-label {
font-size: 0.75rem;
color: #6b7280;
font-weight: 500;
}
.health-levels {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
@media (min-width: 640px) {
.health-levels {
grid-template-columns: repeat(6, 1fr);
}
}
.health-level {
text-align: center;
padding: 0.75rem;
border-radius: 0.5rem;
transition: all 0.2s ease;
cursor: help;
}
.health-level:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.health-level.critical {
background: #fee2e2;
border: 1px solid #fecaca;
}
.health-level.poor {
background: #ffedd5;
border: 1px solid #fed7aa;
}
.health-level.moderate {
background: #fef3c7;
border: 1px solid #fde68a;
}
.health-level.fair {
background: #dcfce7;
border: 1px solid #bbf7d0;
}
.health-level.good {
background: #bbf7d0;
border: 1px solid #86efac;
}
.health-level.excellent {
background: #86efac;
border: 1px solid #4ade80;
}
.health-color {
width: 1.5rem;
height: 1.5rem;
border-radius: 50%;
margin: 0 auto 0.5rem;
}
.critical .health-color {
background: #d32f2f;
}
.poor .health-color {
background: #f57c00;
}
.moderate .health-color {
background: #fbc02d;
}
.fair .health-color {
background: #7cb342;
}
.good .health-color {
background: #388e3c;
}
.excellent .health-color {
background: #1b5e20;
}
.health-name {
font-size: 0.875rem;
font-weight: 600;
color: #111827;
margin-bottom: 0.25rem;
}
.health-range {
font-size: 0.75rem;
color: #6b7280;
}
.health-description {
font-size: 0.75rem;
color: #6b7280;
margin-top: 0.5rem;
line-height: 1.4;
}
.current-marker {
position: absolute;
top: -0.5rem;
width: 2px;
height: 3rem;
background: #111827;
transform: translateX(-50%);
}
.current-marker::after {
content: 'Current';
position: absolute;
top: -1.5rem;
left: 50%;
transform: translateX(-50%);
font-size: 0.75rem;
font-weight: 600;
color: #111827;
white-space: nowrap;
}
.current-marker::before {
content: '';
position: absolute;
top: -0.5rem;
left: 50%;
transform: translateX(-50%);
width: 0;
height: 0;
border-left: 0.5rem solid transparent;
border-right: 0.5rem solid transparent;
border-top: 0.5rem solid #111827;
}
.tooltip {
position: absolute;
background: #111827;
color: white;
padding: 0.5rem 0.75rem;
border-radius: 0.375rem;
font-size: 0.75rem;
white-space: nowrap;
z-index: 1000;
pointer-events: none;
opacity: 0;
transition: opacity 0.2s ease;
}
.tooltip::after {
content: '';
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
border-width: 0.375rem;
border-style: solid;
border-color: #111827 transparent transparent transparent;
}
</style>
<div class="legend-container">
<div class="legend-header">
<div class="legend-title">
<i data-feather="layers"></i>
NDVI Health Legend
</div>
<div class="ndvi-value">0.62</div>
</div>
<div class="gradient-container" id="gradientBar">
<div class="current-marker" style="left: 62%;"></div>
</div>
<div class="gradient-labels">
<span class="gradient-label">0.0</span>
<span class="gradient-label">0.2</span>
<span class="gradient-label">0.4</span>
<span class="gradient-label">0.6</span>
<span class="gradient-label">0.8</span>
<span class="gradient-label">1.0</span>
</div>
<div class="health-levels">
<div class="health-level critical" data-tooltip="No vegetation, bare soil">
<div class="health-color"></div>
<div class="health-name">Critical</div>
<div class="health-range">0.0 - 0.2</div>
<div class="health-description">No vegetation</div>
</div>
<div class="health-level poor" data-tooltip="Sparse vegetation, stressed">
<div class="health-color"></div>
<div class="health-name">Poor</div>
<div class="health-range">0.2 - 0.3</div>
<div class="health-description">Sparse vegetation</div>
</div>
<div class="health-level moderate" data-tooltip="Moderate vegetation">
<div class="health-color"></div>
<div class="health-name">Moderate</div>
<div class="health-range">0.3 - 0.5</div>
<div class="health-description">Moderate growth</div>
</div>
<div class="health-level fair" data-tooltip="Healthy vegetation">
<div class="health-color"></div>
<div class="health-name">Fair</div>
<div class="health-range">0.5 - 0.6</div>
<div class="health-description">Healthy growth</div>
</div>
<div class="health-level good" data-tooltip="Very healthy vegetation">
<div class="health-color"></div>
<div class="health-name">Good</div>
<div class="health-range">0.6 - 0.8</div>
<div class="health-description">Very healthy</div>
</div>
<div class="health-level excellent" data-tooltip="Dense, thriving vegetation">
<div class="health-color"></div>
<div class="health-name">Excellent</div>
<div class="health-range">0.8 - 1.0</div>
<div class="health-description">Thriving growth</div>
</div>
</div>
</div>
`;
// 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);