如何避免在Vega-Lite中显示未使用的传奇条目?

发布于 2025-02-11 17:12:45 字数 1849 浏览 4 评论 0原文

我在仪表板上有多个图(饼图,散点图,barcharts),所有这些图都由第三个属性上色。因此,我创建了一个自定义的颜色标度,以在每个图中使用。 现在,在每个图中的每个图例中都显示了整个秤,即使实际上只有3种颜色,因为没有其他颜色的数据点。 如何使Vega-Lite仅显示现有数据的传奇条目。 我需要一个全局解决方案,因为我有不同的数据集,当然要应用相同的代码。预先感谢您的回答!

   EEKData = {
        "values": [
        {EEK: "A+", Energieträger: "Wärmepumpe", "frequency": 1},
        {EEK: "A", Energieträger: "Gas", "frequency": 16},
        {EEK: "A", Energieträger: "Wärmepumpe", "frequency": 5},
        {EEK: "A", Energieträger: "Fernwärme", "frequency": 7},
        {EEK: "B", Energieträger: "Fernwärme", "frequency": 14},
        {EEK: "C", Energieträger: "Wärmepumpe", "frequency": 201},
        {EEK: "D", Energieträger: "Wärmepumpe", "frequency": 248},
        {EEK: "E", Energieträger: "Wärmepumpe", "frequency": 210},
        {EEK: "F", Energieträger: "Wärmepumpe", "frequency": 200},
        {EEK: "H", Energieträger: "Gas", "frequency": 28}
        ]
    };

 custom_scale = {
  "domain": ["Fernwärme", "Gas", "Gas dezentral", "BHKW", "Pellet",
        "Öl", "Wärmepumpe", "Elektro-Nachtspeicher", "Sonstiges"],
   "range": ["#0070C0", "#ED6011", "#FF0900", "#C00025", "#843C0C",
       "#000000", "#70AD47", "#6C7406", "#595959"]
};

const spec = {
    data: {name: 'values'},
        mark: {type: 'bar', size: 20, opacity: 1},
        encoding: {
            x: {field: 'EEK', type: 'nominal', sort: ["A+", "A", "B", "C", "D", "E", "F", "G", "H"], title: null, axis: {labelAngle: 0}},
            y: {field: 'frequency', type: 'quantitative', title: null},
            color: {field: 'Energieträger', type: 'nominal', scale: customScale},

        }
    },
}

return <VegaLite
    spec={spec}
    data={EEKData}
/>

I have multiple plots (pie charts, scatterplots, barcharts) in a dashboard, all of them are colored by a third attribute. Therefore I created a custom color scale to use in every plot.
Now, the whole scale is shown in every legend in every plot, even if only 3 of the colors are in fact necessary, as there are no data points for the other colors.
How can I make vega-lite showing only those legend entries with existing data.
I need a global solution, as I have different data sets to which I want to apply the same code of course. Thanks in advance for your answers!

Unused legend entries

   EEKData = {
        "values": [
        {EEK: "A+", Energieträger: "Wärmepumpe", "frequency": 1},
        {EEK: "A", Energieträger: "Gas", "frequency": 16},
        {EEK: "A", Energieträger: "Wärmepumpe", "frequency": 5},
        {EEK: "A", Energieträger: "Fernwärme", "frequency": 7},
        {EEK: "B", Energieträger: "Fernwärme", "frequency": 14},
        {EEK: "C", Energieträger: "Wärmepumpe", "frequency": 201},
        {EEK: "D", Energieträger: "Wärmepumpe", "frequency": 248},
        {EEK: "E", Energieträger: "Wärmepumpe", "frequency": 210},
        {EEK: "F", Energieträger: "Wärmepumpe", "frequency": 200},
        {EEK: "H", Energieträger: "Gas", "frequency": 28}
        ]
    };

 custom_scale = {
  "domain": ["Fernwärme", "Gas", "Gas dezentral", "BHKW", "Pellet",
        "Öl", "Wärmepumpe", "Elektro-Nachtspeicher", "Sonstiges"],
   "range": ["#0070C0", "#ED6011", "#FF0900", "#C00025", "#843C0C",
       "#000000", "#70AD47", "#6C7406", "#595959"]
};

const spec = {
    data: {name: 'values'},
        mark: {type: 'bar', size: 20, opacity: 1},
        encoding: {
            x: {field: 'EEK', type: 'nominal', sort: ["A+", "A", "B", "C", "D", "E", "F", "G", "H"], title: null, axis: {labelAngle: 0}},
            y: {field: 'frequency', type: 'quantitative', title: null},
            color: {field: 'Energieträger', type: 'nominal', scale: customScale},

        }
    },
}

return <VegaLite
    spec={spec}
    data={EEKData}
/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2025-02-18 17:12:45

Vega-Lite将使用您在秤中提供的所有颜色。您需要设置自定义域和范围吗?如果您需要描述的功能,则需要让VL自动计算颜色,然后仅使用数据中的颜色。

另一个选择是如果您需要特定类别的某些颜色,则在数据中提供颜色。

Vega-Lite will use all the colours you provide in your scale. Do you need to set a custom domain and range? If you want the functionality you describe, you need to let VL calculate the colours for you automatically and then it will use only the ones in the data.

Another option is to provide the color in a field in your data if you need certain colours for specific categories.

清醇 2025-02-18 17:12:45

我通过根据出现的类别过滤每个数据集的自定义颜色数组,然后将子数组用作颜色尺度来解决问题。

I solved the issue by filtering the array of custom colors for every dataset according to the occurring categories and then using the sub array as color scale.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文