{"id":221,"date":"2013-05-10T10:57:20","date_gmt":"2013-05-10T10:57:20","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=221"},"modified":"2021-01-08T15:16:01","modified_gmt":"2021-01-08T15:16:01","slug":"custom-colors-in-maps","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/custom-colors-in-maps\/","title":{"rendered":"Custom colors in maps"},"content":{"rendered":"<p>In a series of blog posts, we will explore various usage scenarios for MindFusion software components, based mostly on technical support questions we are frequently asked. Today\u2019s post shows how to assign colors to map regions, depending on the value of a field in the map\u2019s associated DBF database. This particular example assigns darker shades of red to countries with larger populations.<\/p>\n<p>Let\u2019s load an ESRI map file and its database into the map view\u2019s BaseMap property, which provides a shortcut for setting a map in the first layer of the view:<\/p>\n<pre>\/\/ load the map file and dbf database\nvar map = mapView.BaseMap = Map.FromFile(\n\t\"ne_50m_admin_0_countries.shp\", true, \"NAME\");\nvar db = map.Database;\nvar layer = mapView.Layers[0];<\/pre>\n<p>Next, create an array of population threshold values, which will correspond to different values in the layer\u2019s color palette:<\/p>\n<pre>\/\/ specify threshold values that trigger more saturated colors\nvar m = 1000000;\nvar populationThreshold = new[]\n{\n\t0, 1*m, 5*m, 10*m, 50*m, 100*m, 1000*m\n};\n\nint numColors = populationThreshold.Length;<\/pre>\n<p>Define the map colors as shades of red:<\/p>\n<pre>\/\/ set the layer palette with a color for each threshold value\nlayer.FillColors = new Color[numColors];\nlayer.FillColors[0] = Color.WhiteSmoke;\nfor (int i = 1; i &lt; numColors; i++)\n{\n\tlayer.FillColors[i] = Color.FromArgb(\n\t\t255,\n\t\t255 - 255 \/ (numColors - i),\n\t\t255 - 255 \/ (numColors - i));\n}<\/pre>\n<p>Iterate over the database records, find their corresponding shape from the ESRI .shp file, and read the population field:<\/p>\n<pre>\/\/ for each database record, read population field and set color\nfor (int i = 0; i &lt; db.Rows.Count; i++)\n{\n\tvar countryShape = map.Shapes[i];\n\tvar population = float.Parse(db.Rows[i][\"POP_EST\"]);\n\t\u2026\n}<\/pre>\n<p>Finally, determine the shape\u2019s color from the maximal threshold value smaller than the country\u2019s population:<\/p>\n<pre>\tfor (int c = numColors - 1; c &gt;= 0; c--)\n\t{\n\t\tif (population &gt; populationThreshold[c])\n\t\t{\n\t\t\tcountryShape.Color = c;\n\t\t\tbreak;\n\t\t}\n\t}<\/pre>\n<p>The resulting custom-colored map is shown in this screenshot:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/mindfusion.dev\/_samples\/map_colors.png\" alt=\"Colored map\" width=\"853\" height=\"426\" vspace=\"5\" \/><br \/>\n<em>Labels for countries are displayed dynamically e.g. they are hidden if space is not enough. In the sample pictured above you will see all labels if you zoom in the map.<\/em><\/p>\n<p>A VS2008 solution that includes the full source code and map files can be downloaded here:<br \/>\n<a href=\"https:\/\/mindfusion.dev\/_samples\/ColorCoding.zip\">https:\/\/mindfusion.dev\/_samples\/ColorCoding.zip<\/a><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a series of blog posts, we will explore various usage scenarios for MindFusion software components, based mostly on technical support questions we are frequently asked. Today\u2019s post shows how to assign colors to map regions, depending on the value &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/custom-colors-in-maps\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[73,74],"tags":[],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-mapping","category-sample-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-3z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":8,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":2428,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions\/2428"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}