本文將詳細介紹如何在 Plotly Dash 應用中為 Plotly 圖表添加全屏圖標。實現(xiàn)這一功能的核心在于利用 Dash 提供的 assets 文件夾,將自定義 JavaScript 代碼嵌入到應用中,從而擴展 Plotly 圖表的交互能力。
創(chuàng)建 assets 文件夾:
在 Dash 應用的根目錄下創(chuàng)建一個名為 assets 的文件夾。Dash 會自動將該文件夾下的 CSS、JavaScript 和圖片等靜態(tài)資源加載到應用中。
創(chuàng)建 JavaScript 文件:
在 assets 文件夾中創(chuàng)建一個 JavaScript 文件,例如 fullscreen.js,并將以下代碼復制到該文件中。這段代碼的主要功能是:
//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 } })
引入 Font Awesome CSS:
為了顯示全屏圖標,需要在 Dash 應用中引入 Font Awesome CSS??梢酝ㄟ^多種方式實現(xiàn),例如:
import dash import dash_html_components as html import dash_core_components as dcc import plotly.graph_objects as go app = dash.Dash(__name__) app.layout = html.Div([ html.Link( rel='stylesheet', ), dcc.Graph( id='example-graph', figure={ 'data': [ {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'}, {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'}, ], 'layout': { 'title': 'Dash Data Visualization' } } ) ]) if __name__ == '__main__': app.run_server(debug=True)
運行 Dash 應用:
運行 Dash 應用,即可看到 Plotly 圖表的工具欄中添加了全屏圖標。點擊該圖標,即可將圖表切換到全屏模式。
通過以上步驟,我們可以輕松地為 Plotly Dash 應用中的圖表添加全屏圖標,從而提升用戶體驗。該方案的核心在于利用 Dash 提供的 assets 文件夾,將自定義 JavaScript 代碼嵌入到應用中,從而擴展 Plotly 圖表的交互能力。 這種方法可以應用于其他 Plotly 圖表定制需求,例如添加自定義按鈕、修改工具欄樣式等。
以上就是向 Plotly Dash 應用圖表工具欄添加全屏圖標的詳細內(nèi)容,更多請關注php中文網(wǎng)其它相關文章!
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號