亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索

向 Python Dash 應(yīng)用的 Plotly 圖表模式欄添加全屏圖標(biāo)

碧海醫(yī)心
發(fā)布: 2025-09-13 15:40:10
原創(chuàng)
643人瀏覽過

向 python dash 應(yīng)用的 plotly 圖表模式欄添加全屏圖標(biāo)

向 Python Dash 應(yīng)用的 Plotly 圖表模式欄添加全屏圖標(biāo)

在 Dash 應(yīng)用中,Plotly 圖表提供了一個模式欄(Modebar),用于控制圖表的交互行為,例如縮放、平移、下載等。有時,我們希望為用戶提供一個更直觀的全屏顯示圖表的選項。雖然 Plotly 本身沒有直接提供全屏按鈕,但我們可以通過自定義 JavaScript 代碼,將其添加到模式欄中。

實現(xiàn)步驟

  1. 創(chuàng)建 assets 文件夾:

    在 Dash 應(yīng)用的根目錄下創(chuàng)建一個名為 assets 的文件夾。Dash 會自動將該文件夾中的 CSS 和 JavaScript 文件加載到應(yīng)用中。

    立即學(xué)習(xí)Python免費(fèi)學(xué)習(xí)筆記(深入)”;

  2. 創(chuàng)建 JavaScript 文件:

    在 assets 文件夾中創(chuàng)建一個 JavaScript 文件,例如 fullscreen.js,并將以下代碼復(fù)制到該文件中:

    //Script to show Plotly graph to fullscreen mode
    //Dependence on Font Awesome icons
    //Author: Dhirendra Kumar
    //Created: 26-Nov-2024
    
    function addToModbar() {
        const modeBars = document.querySelectorAll(".modebar-container");
        for(let i=0; i<modeBars.length; i++) {
            const modeBarGroups = modeBars[i].querySelectorAll(".modebar-group");
            const modeBarBtns = modeBarGroups[modeBarGroups.length - 1].querySelectorAll(".modebar-btn");
    
            if (modeBarBtns[modeBarBtns.length - 1].getAttribute('data-title') !== 'Fullscreen') {
                const aTag = document.createElement('a');
                aTag.className = "modebar-btn";
                aTag.setAttribute("rel", "tooltip");
                aTag.setAttribute("data-title", "Fullscreen");
                aTag.setAttribute("style", "color:gray");
                aTag.setAttribute("onClick", "fullscreen(this);");
                const iTag = document.createElement('i');
                iTag.className = 'fa-solid fa-maximize';
                aTag.appendChild(iTag);
                modeBarGroups[modeBarGroups.length - 1].appendChild(aTag);
            }
        }
    }
    
    function fullscreen(el) {
        elem = el.closest('.dash-graph');
        if (document.fullscreenElement) {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.mozCancelFullScreen) { // Firefox
                document.mozCancelFullScreen();
            } else if (document.webkitExitFullscreen) { // Chrome, Safari and Opera
                document.webkitExitFullscreen();
            } else if (document.msExitFullscreen) { // IE/Edge
                document.msExitFullscreen();
            }
        }
        else {
            if (elem.requestFullscreen) {
                elem.requestFullscreen();
            } else if (elem.mozRequestFullScreen) { // Firefox
                elem.mozRequestFullScreen();
            } else if (elem.webkitRequestFullscreen) { // Chrome, Safari and Opera
                elem.webkitRequestFullscreen();
            } else if (elem.msRequestFullscreen) { // IE/Edge
                elem.msRequestFullscreen();
            }
        }
    }
    
    window.fetch = new Proxy(window.fetch, {
        apply(fetch, that, args) {
            // Forward function call to the original fetch
            const result = fetch.apply(that, args);
    
            // Do whatever you want with the resulting Promise
            result.then((response) => {
                if (args[0] == '/_dash-update-component') {
                    setTimeout(function() {addToModbar()}, 1000)
                }})
            return result
            }
    })
    登錄后復(fù)制

    這段 JavaScript 代碼做了以下幾件事:

    愛圖表
    愛圖表

    AI驅(qū)動的智能化圖表創(chuàng)作平臺

    愛圖表99
    查看詳情 愛圖表
    • addToModbar(): 查找頁面上所有的 Plotly 圖表的模式欄,并在每個模式欄的最后一組按鈕中添加一個全屏按鈕。該按鈕使用 Font Awesome 的全屏圖標(biāo) (fa-solid fa-maximize)。
    • fullscreen(el): 當(dāng)用戶點擊全屏按鈕時,該函數(shù)會被調(diào)用。它會找到包含圖表的 .dash-graph 元素,并使用瀏覽器的全屏 API 將其設(shè)置為全屏顯示。
    • window.fetch = new Proxy(window.fetch, ...): 這段代碼攔截了 Dash 應(yīng)用的 fetch 請求。每當(dāng) Dash 應(yīng)用更新組件時,它會延遲 1 秒后調(diào)用 addToModbar() 函數(shù),以確保全屏按鈕在圖表更新后仍然存在。
  3. 引入 Font Awesome:

    由于代碼使用了 Font Awesome 圖標(biāo),需要在 Dash 應(yīng)用中引入 Font Awesome 的 CSS 文件。 最簡單的方法是在 Dash 應(yīng)用的 HTML 頭部添加以下鏈接:

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" integrity="sha512-9usAa10IRO0HhonpyAIVpjrylPvoDwiPUiKdWk5t3PyolY1cOd4DSE0Ga+ri4AuTroPR5aQvXU9xC6qOPnzFeg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    登錄后復(fù)制

    可以將此鏈接添加到 Dash 應(yīng)用的 index.html 文件中,或者使用 Dash 的 dash.Dash.index_string 屬性進(jìn)行自定義。

注意事項

  • 確保 Font Awesome 的 CSS 文件已正確引入,否則全屏按鈕將無法顯示圖標(biāo)。
  • addToModbar 函數(shù)使用 setTimeout 延遲調(diào)用,以確保在 Dash 應(yīng)用更新組件后,全屏按鈕仍然存在??梢愿鶕?jù)實際情況調(diào)整延遲時間。
  • 該代碼依賴于 Plotly 圖表的 HTML 結(jié)構(gòu)。如果 Plotly 的 HTML 結(jié)構(gòu)發(fā)生變化,可能需要修改代碼才能正常工作。

總結(jié)

通過以上步驟,就可以在 Python Dash 應(yīng)用的 Plotly 圖表模式欄中添加一個全屏按鈕,為用戶提供更方便的全屏顯示體驗。 這種方法利用了 Dash 的 assets 文件夾和自定義 JavaScript 代碼,實現(xiàn)了對 Plotly 圖表的增強(qiáng)。

以上就是向 Python Dash 應(yīng)用的 Plotly 圖表模式欄添加全屏圖標(biāo)的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!

最佳 Windows 性能的頂級免費(fèi)優(yōu)化軟件
最佳 Windows 性能的頂級免費(fèi)優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進(jìn)程會占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場系統(tǒng)廣告
熱門教程
更多>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號