Vega-Lite在堆叠柱状图的y轴上不正确地显示数字。

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

Vega-Lite doesn't display numbers correctly in stacked bar chart on y axis

问题

由于某种原因,我的堆叠条形图在Y轴上没有正确显示数字。我检查了数据格式,尝试了不同的格式,但它们都被四舍五入为0.0、0.1等。我的最小值是500,000,最大值是272M。

在哪里寻找错误?

以下是我的代码:

vegalite({
  width: 600,
  height: 300,
  data: { values: TrafficAp },
  mark: {
    type: "bar",
    size: 35,
    cornerRadiusEnd: 4
  },
  encoding: {
    x: {
      timeUnit: "year_month_day",
      field: "month",
      type: "temporal",
      title: "Month",
      sort: {
        field: "month",
        order: [
          "2022-01",
          "2022-02",
          "2022-03",
          "2022-04",
          "2022-05",
          "2022-06",
          "2023-07",
          "2023-08",
          "2023-09",
          "2023-10",
          "2023-11",
          "2023-12"
        ]
      },
      scale: {
        bandPaddingInner: 0.2 // 调整bandPaddingInner值以更改条形之间的间距
      }
    },
    y: {
      aggregate: "sum",
      type: "quantitative",
      field: "sessions",
      title: "Sessions",
      axis: {
        format: "s",
        labelExpr:
          "datum.value === 0 ? '0' : format(datum.value, '.1s').replace(/G/,'B')"
      }
    },
    color: {
      field: "traffic_type",
      type: "nominal",
      title: "Traffic Type"
    }
  },
  config: {
    stack: "normalize" // 垂直堆叠条形并标准化值
  }
})

附上柱状图的外观如下:

[![enter image description here][1]][1]

英文:

For some reason, my stacked bar chart doesn't display the numbers correctly on Y axis. I checked data format, tries different ones, but they are all rounded up as 0.0, 0.1 etc My min value is 500,000 and max value is 272M.

Where to look for a bug?

Here is my code:

vegalite({
  width: 600,
  height: 300,
  data: { values: TrafficAp },
  mark: {
    type: "bar",
    size: 35,
    cornerRadiusEnd: 4
  },
  encoding: {
    x: {
      timeUnit: "year_month_day",
      field: "month",
      type: "temporal",
      title: "Month",
      sort: {
        field: "month",
        order: [
          "2022-01",
          "2022-02",
          "2022-03",
          "2022-04",
          "2022-05",
          "2022-06",
          "2023-07",
          "2023-08",
          "2023-09",
          "2023-10",
          "2023-11",
          "2023-12"
        ]
      },
      scale: {
        bandPaddingInner: 0.2 // Adjust the bandPaddingInner value to change the spacing between bars
      }
    },
    y: {
      aggregate: "sum",
      type: "quantitative",
      field: "sessions",
      title: "Sessions",
      axis: {
        format: "s",
        labelExpr:
          "datum.value === 0 ? '0' : format(datum.value, '.1s').replace(/G/,'B')"
      }
    },
    color: {
      field: "traffic_type",
      type: "nominal",
      title: "Traffic Type"
    }
  },
  config: {
    stack: "normalize" // Stack bars vertically and normalize the values
  }
})```
Attached is how the bar chart looks like [![enter image description here][1]][1]


</details>


# 答案1
**得分**: 1

这些百分比是由以下代码行引起的:

    config: {
        stack: "normalize" // 垂直堆叠条形图并对值进行归一化
    }

更多信息请查看:https://vega.github.io/vega-lite/docs/stack.html

<details>
<summary>英文:</summary>

Those are percentages caused by this line:

    config: {
        stack: &quot;normalize&quot; // Stack bars vertically and normalize the values
      }

More info: https://vega.github.io/vega-lite/docs/stack.html

</details>



huangapple
  • 本文由 发表于 2023年7月14日 02:22:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682261.html
匿名

发表评论

匿名网友

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

确定