簡體   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