繁体   English   中英

如何在 C3.js 中隐藏堆积条形图上的条形

[英]How to hide bars on a stacked bar chart in C3.js

我有一个用C3.js制作的堆积条形图,它使用以下代码生成:

stacked_bar_chart = c3.generate({
        bindto: '#stacked_bar_chart_container',
        data: {
            columns: [
               ["Critical", 446, 863], 
               ["High", 1160, 2301],
               ["Medium", 3106, 8258], 
               ["Low", 277, 119], 
               ["Informational", 7374, 23240]
            ],
            type: 'bar',
            groups: [
                ['Low', 'Medium', 'Informational', 'High', 'Critical', 'Unknown']
            ],
        },
        grid: {
            y: {
                lines: [{ value: 0 }]
            }
        },
        axis: {
            x: {
                type: 'category',
                categories: ["Remediated", "Unconfirmed"] // Notice the x-axis has categories
            },
            y: {
                label: 'Number of Findings'
            }
        },       
    });

我试图做到这一点,以便在单击按钮时,我能够从图表中隐藏名为Remediated的栏。 我尝试通过执行以下操作来卸载它:

stacked_bar_chart.unload("Remediated");

但这没有任何影响,我很确定这是因为我使用type: 'category'作为 x 轴。 我宁愿不必卸载数据,以便稍后我可以根据需要重新显示栏,而无需再次检索数据。

C3.js参考页面中进行了一些研究后,我认为没有简单的 API 函数可以实现这一点,所以我想出了我自己目前正在使用的这个功能的测试实现。

首先,通过我这样做的方式,我跟踪三个单独的全局变量,这些变量将保存当前图表中的数据,也将保存我们从中删除的数据。 这是我决定选择的方式,因为我的图表数据来自网络资源,因此每次添加或删除类别时继续进行 AJAX 调用并刷新数据是低效的。

// Our three new variables
var removed_from_stacked_bar = {};
var stacked_bar_categories = ["Remediated", "Unconfirmed"];
var stacked_bar_data = [
               ["Critical", 446, 863], 
               ["High", 1160, 2301],
               ["Medium", 3106, 8258], 
               ["Low", 277, 119], 
               ["Informational", 7374, 23240]
            ];

function initialize_stacked_bar_chart(data, categories) {
    stacked_bar_chart = c3.generate({
        bindto: '#stacked_bar_chart_container',
        data: {
            columns: data, // Coming from the parameter
            type: 'bar',
            groups: [
                ['Low', 'Medium', 'Informational', 'High', 'Critical', 'Unknown']
            ],
        },
        grid: {
            y: {
                lines: [{ value: 0 }]
            }
        },
        axis: {
            x: {
                type: 'category',
                categories: categories // Coming from the parameter
            },
            y: {
                label: 'Number of Findings'
            }
        },       
    });
}

initialize_stacked_bar_chart(stacked_bar_data, stacked_bar_categories);

现在我编写了一个名为update_stacked_bar_chart()的函数,它有一个category参数,以便在调用时删除/添加从图表传入的category

function update_stacked_bar_chart(category) {
    var categoryIndex = stacked_bar_categories.indexOf(category);
    var removed_values = [];
    if (categoryIndex != -1) { // Removing the item since it exists in the bar chart's categories
        stacked_bar_categories.splice(categoryIndex, 1); // Removing the category name from the bar chart's category list
        stacked_bar_data.forEach(function (item, index) {
            var temp = item.splice(categoryIndex + 1, 1); // Removing the value this category held (in-place) in the sublist for each severity
            removed_values.push(temp); // Pushing each removed value into the array of removed values (in order from Critical, High, Medium, Low, Informational).
        });
        removed_from_stacked_bar[category] = removed_values;
    } else { // Re-adding the item if it was not found in the current chart's categories
        stacked_bar_categories.push(category); // Adding the category name to the bar chart's category list
        removed_from_stacked_bar[category].forEach(function (item, index) {
            stacked_bar_data[index].push(item); // Adding the value for each severity into the respective severity list 
        });
        delete removed_from_stacked_bar[category];
    }
    initialize_stacked_bar_chart(stacked_bar_data, stacked_bar_categories); // Remaking the bar chart with the new data and categories.
}

此函数将允许您在每次调用时从条形图中切换任何类别。 您可以将其附加到事件侦听器,以便在需要时调用它。

这是一个如何使用它来切换条的示例,因为它被称为:

update_stacked_bar_chart("Remediated"); // Removes the "Remediated" bar
update_stacked_bar_chart("Remediated"); // Re-adds the "Remediated" bar
update_stacked_bar_chart("Remediated"); // Removes the "Remediated" bar
update_stacked_bar_chart("Unconfirmed"); // Removes the "Unconfirmed" bar
update_stacked_bar_chart("Remediated"); // Re-adds the "Remediated" bar
update_stacked_bar_chart("Unconfirmed"); // Re-adds the "Unconfirmed" bar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM