File size: 6,142 Bytes
320a79f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
<!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>
// Mock data for country statistics
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 }
};
// Set up the map
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);
// Load world map data
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);
});
});
// Initialize Chart.js
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> |