|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
<title>World Map Dashboard</title> |
|
<script src="https://d3js.org/d3.v7.min.js"></script> |
|
<script src="https://d3js.org/topojson.v3.min.js"></script> |
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> |
|
<style> |
|
* { |
|
margin: 0; |
|
padding: 0; |
|
box-sizing: border-box; |
|
} |
|
|
|
body { |
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; |
|
background-color: #f5f5f5; |
|
} |
|
|
|
.container { |
|
max-width: 1400px; |
|
margin: 20px auto; |
|
padding: 20px; |
|
display: grid; |
|
grid-template-columns: 2fr 1fr; |
|
gap: 20px; |
|
} |
|
|
|
.map-container { |
|
background: white; |
|
padding: 20px; |
|
border-radius: 10px; |
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
|
} |
|
|
|
.chart-container { |
|
background: white; |
|
padding: 20px; |
|
border-radius: 10px; |
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
|
} |
|
|
|
.country { |
|
fill: #ccc; |
|
stroke: #fff; |
|
stroke-width: 0.5; |
|
cursor: pointer; |
|
transition: fill 0.3s; |
|
} |
|
|
|
.country:hover { |
|
fill: #666; |
|
} |
|
|
|
.selected { |
|
fill: #4CAF50 !important; |
|
} |
|
|
|
h2 { |
|
margin-bottom: 20px; |
|
color: #333; |
|
} |
|
|
|
.tooltip { |
|
position: absolute; |
|
background: rgba(0,0,0,0.8); |
|
color: white; |
|
padding: 5px 10px; |
|
border-radius: 4px; |
|
font-size: 12px; |
|
pointer-events: none; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<div class="container"> |
|
<div class="map-container"> |
|
<h2>World Map</h2> |
|
<div id="map"></div> |
|
</div> |
|
<div class="chart-container"> |
|
<h2>Country Statistics</h2> |
|
<canvas id="statsChart"></canvas> |
|
</div> |
|
</div> |
|
|
|
<script> |
|
|
|
const countryStats = { |
|
'United States': { gdp: 21400, population: 331000000 }, |
|
'China': { gdp: 14700, population: 1402000000 }, |
|
'Japan': { gdp: 5100, population: 125360000 }, |
|
'Germany': { gdp: 3800, population: 83200000 }, |
|
'United Kingdom': { gdp: 2700, population: 67220000 } |
|
}; |
|
|
|
|
|
const width = 800; |
|
const height = 500; |
|
|
|
const svg = d3.select('#map') |
|
.append('svg') |
|
.attr('width', width) |
|
.attr('height', height); |
|
|
|
const projection = d3.geoMercator() |
|
.scale(130) |
|
.translate([width / 2, height / 1.5]); |
|
|
|
const path = d3.geoPath() |
|
.projection(projection); |
|
|
|
const tooltip = d3.select('body') |
|
.append('div') |
|
.attr('class', 'tooltip') |
|
.style('opacity', 0); |
|
|
|
|
|
d3.json('https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json') |
|
.then(data => { |
|
const countries = topojson.feature(data, data.objects.countries); |
|
|
|
svg.selectAll('path') |
|
.data(countries.features) |
|
.enter() |
|
.append('path') |
|
.attr('class', 'country') |
|
.attr('d', path) |
|
.on('mouseover', function(event, d) { |
|
tooltip.transition() |
|
.duration(200) |
|
.style('opacity', .9); |
|
tooltip.html(d.properties.name) |
|
.style('left', (event.pageX + 5) + 'px') |
|
.style('top', (event.pageY - 28) + 'px'); |
|
}) |
|
.on('mouseout', function() { |
|
tooltip.transition() |
|
.duration(500) |
|
.style('opacity', 0); |
|
}) |
|
.on('click', function(event, d) { |
|
updateChart(d.properties.name); |
|
d3.selectAll('.country').classed('selected', false); |
|
d3.select(this).classed('selected', true); |
|
}); |
|
}); |
|
|
|
|
|
let statsChart; |
|
|
|
function initChart() { |
|
const ctx = document.getElementById('statsChart').getContext('2d'); |
|
statsChart = new Chart(ctx, { |
|
type: 'bar', |
|
data: { |
|
labels: ['GDP (Billion $)', 'Population (Million)'], |
|
datasets: [{ |
|
label: 'Country Statistics', |
|
data: [0, 0], |
|
backgroundColor: [ |
|
'rgba(75, 192, 192, 0.6)', |
|
'rgba(153, 102, 255, 0.6)' |
|
], |
|
borderColor: [ |
|
'rgba(75, 192, 192, 1)', |
|
'rgba(153, 102, 255, 1)' |
|
], |
|
borderWidth: 1 |
|
}] |
|
}, |
|
options: { |
|
responsive: true, |
|
scales: { |
|
y: { |
|
beginAtZero: true |
|
} |
|
} |
|
} |
|
}); |
|
} |
|
|
|
function updateChart(countryName) { |
|
if (countryStats[countryName]) { |
|
const stats = countryStats[countryName]; |
|
statsChart.data.datasets[0].data = [ |
|
stats.gdp, |
|
stats.population / 1000000 |
|
]; |
|
statsChart.data.datasets[0].label = countryName; |
|
statsChart.update(); |
|
} |
|
} |
|
|
|
initChart(); |
|
</script> |
|
</body> |
|
</html> |