ouhenio commited on
Commit
5de44fc
·
verified ·
1 Parent(s): dbf76b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
app.py CHANGED
@@ -69,6 +69,19 @@ HTML_TEMPLATE = """
69
  border: 1px solid rgba(255, 255, 255, 0.2);
70
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
71
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  </style>
73
  </head>
74
  <body>
@@ -84,7 +97,7 @@ HTML_TEMPLATE = """
84
  // Set up dimensions
85
  const container = document.getElementById('map-container');
86
  const width = container.clientWidth;
87
- const height = container.clientHeight;
88
 
89
  // Create SVG
90
  const svg = d3.select('#map-container')
@@ -93,6 +106,12 @@ HTML_TEMPLATE = """
93
  .attr('height', height)
94
  .attr('viewBox', '0 0 ' + width + ' ' + height);
95
 
 
 
 
 
 
 
96
  // Create color scale
97
  const colorScale = d3.scaleLinear()
98
  .domain([0, 100])
@@ -112,22 +131,27 @@ HTML_TEMPLATE = """
112
  // Load GeoJSON data
113
  d3.json('https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world.geojson')
114
  .then(function(data) {
115
- // Draw countries
116
- svg.selectAll('path')
 
 
 
117
  .data(data.features)
118
  .enter()
119
  .append('path')
 
 
 
 
 
 
 
 
 
120
  .attr('d', path)
121
- .attr('stroke', '#f32b7b')
122
- .attr('stroke-width', 1)
123
  .attr('fill', function(d) {
124
- // Get the ISO code from the properties
125
  const iso = d.properties.iso_a2;
126
-
127
- if (countryData[iso]) {
128
- return colorScale(countryData[iso].percent);
129
- }
130
- return '#2d3748'; // Default gray for other countries
131
  })
132
  .on('mouseover', function(event, d) {
133
  const iso = d.properties.iso_a2;
@@ -136,17 +160,15 @@ HTML_TEMPLATE = """
136
  .attr('stroke', '#4a1942')
137
  .attr('stroke-width', 2);
138
 
139
- if (countryData[iso]) {
140
- tooltip.style('opacity', 1)
141
- .style('left', (event.pageX + 15) + 'px')
142
- .style('top', (event.pageY + 15) + 'px')
143
- .html('<strong>' + countryData[iso].name + '</strong><br/>' +
144
- 'Progress: ' + countryData[iso].percent + '%');
145
- }
146
  })
147
  .on('mousemove', function(event) {
148
  tooltip.style('left', (event.pageX + 15) + 'px')
149
- .style('top', (event.pageY + 15) + 'px');
150
  })
151
  .on('mouseout', function() {
152
  d3.select(this)
 
69
  border: 1px solid rgba(255, 255, 255, 0.2);
70
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
71
  }
72
+ .ocean {
73
+ fill: #111;
74
+ }
75
+ .country {
76
+ stroke: #444;
77
+ stroke-width: 0.5;
78
+ fill: #222;
79
+ }
80
+ .selected-country {
81
+ stroke: #f32b7b;
82
+ stroke-width: 1;
83
+ cursor: pointer;
84
+ }
85
  </style>
86
  </head>
87
  <body>
 
97
  // Set up dimensions
98
  const container = document.getElementById('map-container');
99
  const width = container.clientWidth;
100
+ const height = container.clientHeight || 600;
101
 
102
  // Create SVG
103
  const svg = d3.select('#map-container')
 
106
  .attr('height', height)
107
  .attr('viewBox', '0 0 ' + width + ' ' + height);
108
 
109
+ // Add ocean background
110
+ svg.append('rect')
111
+ .attr('class', 'ocean')
112
+ .attr('width', width)
113
+ .attr('height', height);
114
+
115
  // Create color scale
116
  const colorScale = d3.scaleLinear()
117
  .domain([0, 100])
 
131
  // Load GeoJSON data
132
  d3.json('https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world.geojson')
133
  .then(function(data) {
134
+ // Filter out non-relevant countries
135
+ const relevantCountryCodes = Object.keys(countryData);
136
+
137
+ // First draw all countries with a neutral background
138
+ svg.selectAll('.country')
139
  .data(data.features)
140
  .enter()
141
  .append('path')
142
+ .attr('class', 'country')
143
+ .attr('d', path);
144
+
145
+ // Then draw only our target countries on top
146
+ svg.selectAll('.selected-country')
147
+ .data(data.features.filter(d => relevantCountryCodes.includes(d.properties.iso_a2)))
148
+ .enter()
149
+ .append('path')
150
+ .attr('class', 'selected-country')
151
  .attr('d', path)
 
 
152
  .attr('fill', function(d) {
 
153
  const iso = d.properties.iso_a2;
154
+ return colorScale(countryData[iso].percent);
 
 
 
 
155
  })
156
  .on('mouseover', function(event, d) {
157
  const iso = d.properties.iso_a2;
 
160
  .attr('stroke', '#4a1942')
161
  .attr('stroke-width', 2);
162
 
163
+ tooltip.style('opacity', 1)
164
+ .style('left', (event.pageX + 15) + 'px')
165
+ .style('top', (event.pageY + 15) + 'px')
166
+ .html('<strong>' + countryData[iso].name + '</strong><br/>' +
167
+ 'Progress: ' + countryData[iso].percent + '%');
 
 
168
  })
169
  .on('mousemove', function(event) {
170
  tooltip.style('left', (event.pageX + 15) + 'px')
171
+ .style('top', (event.pageY + 15) + 'px');
172
  })
173
  .on('mouseout', function() {
174
  d3.select(this)