Can you dynamically set a color scheme and/or range in Vega-lite?

爱⌒轻易说出口 提交于 2021-01-05 06:29:38

问题


Is it possible dynamically define the color scheme range. For example, I have the below Vega-lite JSON for my color configuration. I would like to input the range below or an entirely different range depending on some condition on the data passed in. Is it possible to set a color range for a variable and dynamically select which one to use?

Also if possible, how would you define the logic to select the appropriate color scheme. The logic is whether some condition on the data (value is present in passed in data) is true. Something like: IF VALUE IN datum.COL THEN SCHEME1 ELSE SCHEME2

What I am trying to do (maybe there is another way around this) is fix the colors for the FIRST (when sorted) one or two values of the Symbol field (which may or may not be present) and then have the rest default to a standard color range.

Color Scheme 1:

"color": {
          "field": "Symbol",
          "scale": {
             "range": [
                   "#0b9ddb",
                   "#c00000",
                   "#41527a"
                      ]
                   }
         },

Color Scheme 2:

"color": {
          "field": "Symbol",
          "scale": {
             "range": [
                   "#c00000",
                   "#41527a",
                   "#90ee90"
                      ]
                   }
         },

Note, I can get this to work using the following condition but the color in the legend does not change. It sticks to the value in the scale range. Is there a way to get the legend to update to use the color from the condition?

"color": {
          "field": "Symbol",
          "condition": {
            "test": "datum.Symbol == 'Value_0b9ddb'",
            "value": "red"
          },
          "scale": {
             "range": [
                   "#c00000",
                   "#41527a",
                   "#90ee90"
                      ]
                   }
         },

来源:https://stackoverflow.com/questions/64837114/can-you-dynamically-set-a-color-scheme-and-or-range-in-vega-lite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!