英文:
Show negative values in Deneb column chart in red
问题
我一直在PowerBI Desktop中尝试使用Deneb视觉进行实验,(除了其他许多事情之外)一直在尝试创建一个简单的柱形图,以红色显示负值,绿色显示正值,但是无论如何都无法让它正常工作。我相信脚本中的条件/测试是正确的,但当条件为“true”时,它却不会“触发”。
我已阅读了Vega-Lite文档中的条件页面https://vega.github.io/vega-lite/docs/condition.html,并在编码/颜色部分中有一个条件部分。
我已经从我的日历表中添加了“Month End”和“MonthYear”列,还从一个事实表中添加了“EBITDA”度量到Deneb视觉中。
如果我将条件调整为“test”: "1==1",那么“true”路径可以正常工作,因此我认为我的测试行中存在错误,尽管根据许多博客、Stack Overflow问题等来看,这似乎是正确的。
我还尝试使用“transform”通道在Deneb数据集中创建一个新的“Neg”字段,并在我的测试中引用该字段,但它仍然无法调整颜色。
英文:
I've been playing around with the Deneb visual in PowerBI Desktop and (amongst many other things) have been trying to create a simple column chart that shows negative values in red and positive values in green, however can't for the life of me seem to get it working - I believe the condition/test in my script is correct, but it refuses to 'fire' when it's 'true'
I've read through the condition page of the Vega-Lite documentation <https://vega.github.io/vega-lite/docs/condition.html> and have a condition section within the encoding/color
I've added Month End and MonthYear columns from my Calendar table and an EBITDA measure from a fact table to the Deneb visual
Month End | MonthYear | EBITDA |
---|---|---|
31/7/2021 | "Jul-21" | 8277.56 |
31/8/2021 | "Aug-21" | -15123.66 |
30/9/2021 | "Sep-21" | 9502.11 |
31/10/2021 | "Oct-21" | 13090.99 |
{
"data": {"name": "dataset"},
"mark": "bar",
"encoding": {
"x": {
"field": "MonthYear",
"sort": {"field": "Month End"}
},
"y": {
"field": "EBITDA",
"aggregate": "sum"
},
"color": {
"condition": {
"test": "datum['EBITDA']<0",
"value": "red"
},
"value": "green"
}
}
}
If I adjust the condition to be "test": "1==1"
then the 'true' path works, so I assume I've got something wrong with my test line, though this seems to be correct per a lot of blogs, stackoverflow questions etc.
I've also tried using a "tranform:"
channel to create a new Neg field in the Deneb dataset and referring to that field in my test, but it still won't adjust the colour.
答案1
得分: 0
不喜欢您的聚合。看起来您发送的数据已经由Power BI进行了聚合。如果是这样,这将有效:
如果您的数据没有被聚合,可以添加一个类似这样的聚合转换:
英文:
It doesn’t like your aggregation. It looks like the data you are sending in is already aggregated by Power BI. If so, this will work:
"y": {
"field": "b",
"type": "quantitative"
},
View sample in the Vega Editor
If your data isn’t aggregated, add an aggregate transform like this:
"transform": [
{"aggregate": [{
"op": "sum",
"field": "b",
"as": "bsum"
}],
"groupby": ["a"]}
],
"mark": "bar",
"encoding": {
"x": {
"field": "a",
"sort": {"field": "a"}
},
"y": {
"field": "bsum",
"type": "quantitative"
},
"color": {
"condition": {
"test": "datum['bsum']<0",
"value": "red"
},
"value": "green"
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论