How to use mapbox case expression within case expression in icon-color

639 views Asked by At

I am trying to have a case expression within case expression in icon-color. My icon-color depends on a combination of to variables. I tryed the following syntax without any success. What is wrong? Is it possible?

"icon-color": [
  "case", 
  ["==",["get","status"],"s1"],
  ["case", ["==",["get","priority"],"p1"], "#111111", ["==",["get","priority"],"p2"], "#222222", "#DDDDDD"]      
  ["==",["get","status"],"s2"],
  ["case", ["==",["get","priority"],"p1"], "#333333", ["==",["get","priority"],"p2"], "#444444", "#DDDDDD"]
  "#777777"]
2

There are 2 answers

3
Steve Bennett On

For starters, you need to put double quotes around your colors ("#111111") and around property names with hyphens ("icon-color").

0
Stephen Taylor On

Try this:

"icon-color": [
  "case", 
  ["all",["==",["get","status"],"s1"],["==",["get","priority"],"p1"]], // is status == s1 AND priority == p1?
  "#111111", // result of all on the above line being true
  ["all",["==",["get","status"],"s1"],["==",["get","priority"],"p2"]], // after failing the first test, is status == s1 AND priority == p2?
  "#222222", // result of all on the above line being true
  ["all",["==",["get","status"],"s2"],["==",["get","priority"],"p1"]], // after failing the second test, is status == s2 AND priority == p1?
  "#333333", // result of all on the above line being true
  ["all",["==",["get","status"],"s2"],["==",["get","priority"],"p2"]], // after failing the third test, is status == s2 AND priority == p2?
  "#444444",  // result of all on the above line being true
  "#777777" // result of all tests failing
]