-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery_geographic_choropleth.cpp
More file actions
154 lines (142 loc) 路 5.8 KB
/
Copy pathgallery_geographic_choropleth.cpp
File metadata and controls
154 lines (142 loc) 路 5.8 KB
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
/**
* @file gallery_geographic_choropleth.cpp
* @brief Geographic Choropleth Map - World GDP Distribution Visualization
* @author plotly.cpp contributors
* @date 2025
*
* @example gallery_geographic_choropleth.cpp
*
* # Geographic Choropleth Map Example
*
* This example demonstrates creating professional geographic data
* visualizations using choropleth maps to show statistical data across
* countries. It visualizes world GDP per capita distribution using color-coded
* country regions with interactive hover information and detailed geographic
* styling.
*
* ## What You'll Learn
* - Creating choropleth maps for geographic data visualization
* - Working with ISO 3-letter country codes for location mapping
* - Implementing custom color scales for data representation
* - Configuring geographic projections and map styling
* - Adding interactive hover templates with detailed country information
* - Customizing map features (coastlines, oceans, lakes, rivers)
* - Professional map layout design with annotations
*
* ## Sample Output
* The example creates a comprehensive world map featuring:
* - World GDP per capita data for 40+ countries using ISO-3 codes
* - Purple-to-yellow color gradient representing economic data ranges
* - Natural Earth projection for aesthetically pleasing world view
* - Interactive hover showing country name, GDP value, and country code
* - Styled coastlines, oceans, and geographic features
* - Professional layout suitable for economic analysis presentations
*
* @image html geographic_choropleth.png "World GDP per Capita Choropleth Map"
*
* @see plotly::Object For choropleth trace configuration
* @see parseGalleryArgs() For command line argument handling
*/
#include "plotly/plotly.hpp"
#include "utils/arg_parser.hpp"
#include <string>
#include <vector>
auto main(int argc, char *argv[]) -> int {
// Parse command line arguments
auto args = parseGalleryArgs(argc, argv);
plotly::Figure fig;
fig.openBrowser(args.headless);
// Country codes (ISO 3-letter) and corresponding data
std::vector<std::string> countries = {
"USA", "CHN", "JPN", "DEU", "IND", "GBR", "FRA", "ITA", "BRA", "CAN",
"RUS", "KOR", "AUS", "ESP", "MEX", "IDN", "NLD", "SAU", "TUR", "TWN",
"CHE", "BEL", "IRL", "ISR", "AUT", "NGA", "THA", "EGY", "ZAF", "ARG",
"NOR", "PHL", "BGD", "VNM", "CHL", "FIN", "SGP", "MYS", "DNK", "NZL"};
// GDP per capita data (in thousands USD)
std::vector<double> gdpPerCapita = {
63.5, 10.5, 39.3, 46.3, 2.1, 42.3, 38.6, 31.3, 8.7, 46.2,
11.3, 31.8, 54.9, 27.1, 9.9, 4.1, 52.3, 23.1, 9.1, 25.9,
81.9, 47.2, 79.9, 43.6, 48.1, 2.2, 7.8, 3.0, 6.0, 10.0,
75.4, 3.5, 2.5, 3.8, 15.3, 48.8, 65.2, 11.4, 59.8, 44.0};
// Country names for hover display
std::vector<std::string> countryNames = {
"United States", "China", "Japan", "Germany",
"India", "United Kingdom", "France", "Italy",
"Brazil", "Canada", "Russia", "South Korea",
"Australia", "Spain", "Mexico", "Indonesia",
"Netherlands", "Saudi Arabia", "Turkey", "Taiwan",
"Switzerland", "Belgium", "Ireland", "Israel",
"Austria", "Nigeria", "Thailand", "Egypt",
"South Africa", "Argentina", "Norway", "Philippines",
"Bangladesh", "Vietnam", "Chile", "Finland",
"Singapore", "Malaysia", "Denmark", "New Zealand"};
// Create choropleth trace
plotly::Object trace = {
{"type", "choropleth"},
{"locations", countries},
{"z", gdpPerCapita},
{"text", countryNames},
{"locationmode", "ISO-3"},
{"colorscale",
{{0.0, "#0d0887"},
{0.2, "#5b02a3"},
{0.4, "#8b0aa5"},
{0.6, "#b93289"},
{0.8, "#db5c68"},
{1.0, "#f0f921"}}},
{"colorbar",
{{"title", "GDP per Capita<br>(thousands USD)"},
{"titleside", "right"},
{"tickmode", "linear"},
{"tick0", 0},
{"dtick", 20}}},
{"hovertemplate", "<b>%{text}</b><br>" +
std::string("GDP per Capita: $%{z:,.1f}k<br>") +
"Country Code: %{location}<extra></extra>"},
{"marker", {{"line", {{"color", "white"}, {"width", 0.5}}}}}};
// Create layout with geographic projection
plotly::Object layout = {
{"title",
{{"text",
"World GDP per Capita Distribution<br>" +
std::string("<sub>Choropleth Map by Country (2023 data)</sub>")},
{"font", {{"size", 18}}}}},
{"geo",
{{"showframe", false},
{"showcoastlines", true},
{"coastlinecolor", "rgb(204, 204, 204)"},
{"projection", {{"type", "natural earth"}}},
{"showland", true},
{"landcolor", "rgb(243, 243, 243)"},
{"showocean", true},
{"oceancolor", "rgb(230, 245, 255)"},
{"showlakes", true},
{"lakecolor", "rgb(230, 245, 255)"},
{"showrivers", true},
{"rivercolor", "rgb(230, 245, 255)"}}},
{"width", 1200},
{"height", 700},
{"margin", {{"l", 0}, {"r", 0}, {"t", 80}, {"b", 0}}},
{"annotations",
{{{"text", "Hover over countries for detailed information"},
{"x", 0.5},
{"y", -0.02},
{"xref", "paper"},
{"yref", "paper"},
{"showarrow", false},
{"font", {{"size", 12}}}}}}};
// Create the plot
std::vector<plotly::Object> data = {trace};
fig.newPlot(data, layout);
if (!args.headless) {
fig.waitClose();
} else {
// Save image instead of opening browser
plotly::Object imageOpts = {{"format", "png"},
{"width", 1200},
{"height", 700},
{"filename", "geographic_choropleth"}};
fig.downloadImage(imageOpts);
}
return 0;
}