在 Deneb 柱状图中,将负值显示为红色。

huangapple go评论90阅读模式
英文:

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”字段,并在我的测试中引用该字段,但它仍然无法调整颜色。

在 Deneb 柱状图中,将负值显示为红色。
在 Deneb 柱状图中,将负值显示为红色。

英文:

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
{
  &quot;data&quot;: {&quot;name&quot;: &quot;dataset&quot;},
  &quot;mark&quot;: &quot;bar&quot;,
  &quot;encoding&quot;: {
    &quot;x&quot;: {
      &quot;field&quot;: &quot;MonthYear&quot;,
      &quot;sort&quot;: {&quot;field&quot;: &quot;Month End&quot;}
    },
    &quot;y&quot;: {
      &quot;field&quot;: &quot;EBITDA&quot;,
      &quot;aggregate&quot;: &quot;sum&quot;
    },
    &quot;color&quot;: {
      &quot;condition&quot;: {
        &quot;test&quot;: &quot;datum[&#39;EBITDA&#39;]&lt;0&quot;,
        &quot;value&quot;: &quot;red&quot;
      },
      &quot;value&quot;: &quot;green&quot;
    }
  }
}

If I adjust the condition to be &quot;test&quot;: &quot;1==1&quot; 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 &quot;tranform:&quot; 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.

在 Deneb 柱状图中,将负值显示为红色。
在 Deneb 柱状图中,将负值显示为红色。

答案1

得分: 0

不喜欢您的聚合。看起来您发送的数据已经由Power BI进行了聚合。如果是这样,这将有效:

在Vega Editor中查看示例

如果您的数据没有被聚合,可以添加一个类似这样的聚合转换:

在Vega Editor中打开图表

英文:

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:

    &quot;y&quot;: {
      &quot;field&quot;: &quot;b&quot;,
      &quot;type&quot;: &quot;quantitative&quot;
    },

View sample in the Vega Editor

If your data isn’t aggregated, add an aggregate transform like this:

  &quot;transform&quot;: [
    {&quot;aggregate&quot;: [{
       &quot;op&quot;: &quot;sum&quot;,
       &quot;field&quot;: &quot;b&quot;,
       &quot;as&quot;: &quot;bsum&quot;
      }],
      &quot;groupby&quot;: [&quot;a&quot;]}
  ],
  &quot;mark&quot;: &quot;bar&quot;,
  &quot;encoding&quot;: {
    &quot;x&quot;: {
      &quot;field&quot;: &quot;a&quot;,
      &quot;sort&quot;: {&quot;field&quot;: &quot;a&quot;}
    },
    &quot;y&quot;: {
      &quot;field&quot;: &quot;bsum&quot;,
      &quot;type&quot;: &quot;quantitative&quot;
    },
    &quot;color&quot;: {
      &quot;condition&quot;: {
        &quot;test&quot;: &quot;datum[&#39;bsum&#39;]&lt;0&quot;,
        &quot;value&quot;: &quot;red&quot;
      },
      &quot;value&quot;: &quot;green&quot;
    }
  }
}

Open the Chart in the Vega Editor

huangapple
  • 本文由 发表于 2023年2月14日 07:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定