How to encode a field using conditions in Vega-Lite #8673
-
OBJECTIVE Hi Folks, I'm trying to add data labels to my chart, however I have multiple bars layered on top of each other, and I need the data labels to hover over different bars depending on if values in a certain field are positive or negative. ATTEMPT This could be achieved by changing the "field" property of the "y" encoding using a condition. I've spent some time exploring Vega-Lite documentation and experimenting with some stuff, but I can't get it to work no matter what I try. Vega seems to ignore by condition. I'm also curious if I'm able to apply conditions to "mark" rather than "encoding". When values are negative, I'd like to change "dy" to 10 from -10. Any suggestions?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For your For the other question, would you mind trying to transform with Here is an example "mark": {
"type":"text",
"dy": {"expr": "datum.longer_bar_value < 0 ? 10 : -10"}
},
"transform": {
{
"calculate": "Math.abs(datum.thin_bar_value) > Math.abs(datum.thick_bar_value) ? datum.thin_bar_value : datum.thick_bar_value",
"as": "longer_bar_value"
}
}
"encoding": {
"text": {
"field": "longer_bar_value"
},
"y": {
"field": "longer_bar_value",
"type": "quantitative"
}....
} Please let us know if you have any question :) |
Beta Was this translation helpful? Give feedback.
For your
dy
question, would you mind checking this example?For the other question, would you mind trying to transform with
calculate
before encoding?Here is an example
Please let us know if you have any question :)