繁体   English   中英

Mpabox GL case 表达式 output,执行数学表达式

[英]Mpabox GL case expression output, perform math expression

嗨,问题不是在满足条件后返回 output,只是应用一些数学运算并将其结果返回到文本字段。 Mapboxgl-js 版本 v 1.11.0

  "text-field": [
     "case",
    ['all', [">=",   ["abs", ["get", "count"]], 1.0e+3]], //if thousands
   
    ["/", ["get", "count"], 1.0e+3], //<--- doesn't work perform division, should return 1 if count is 1000
    
     ["get", "count"] //fallback value 
   ],

问题出在类型上,我尝试在案例表达式 output 中执行数学运算而不将其带入类型编号,在添加“to-string”(用于结果)和“number”(用于操作本身)之后它已解决,格式化效果很好。 这是工作解决方案。

    "text-field": [
    "case",
     // Billions  
    ["all", [">=",   ["abs", ["get", "usersCount"]], 1.0e+9]], 
    // divide usersCount by 1 billion, than strigify("to-string"), and concat result with string "B" same for all
    ["concat", ["to-string", ["round", ["/", ["number", ["get", "usersCount"]], ["number", 1000000000]]]], 'B'],
    
    ["all", [">=",   ["abs", ["get", "usersCount"]], 1.0e+6]],
    ["concat", ["to-string", ["round", ["/", ["number", ["get", "usersCount"]], ["number", 1000000]]]], "M"] ,

    ["all", [">=", ["abs", ["get", "usersCount"]], 1.0e+3]],
    ["concat",["to-string",  ["round", ["/", ["number", ["get", "usersCount"]], ["number", 1.0e+3]]]], "K"],
    //if none of above (1-10) return the value itself 
    ["get", "usersCount"]
  ],

希望这会对某人有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM