Responsive Web Design
透過 resize 事件與 rendering 函式來達成
<div id="linechartContainer" style="width: 100%; height: 100%;">
<div id="linechartBody" style="width: 100%; height: 100%;">
</div>
</div>
function rendering() {
if(window.jQuery && $('#linechartContainer').length > 0 && $('#linechartBody').length > 0) {
Plotly.d3.select('#linechartBody svg').html('');
layout["height"] = parseInt(Plotly.d3.select("#linechartContainer").style("height"), 10);
layout["width"] = parseInt(Plotly.d3.select("#linechartContainer").style("width"), 10);
layout["autosize"] = false;
Plotly.newPlot('linechartBody', data, layout);
}
}
Plotly.d3.select(window).on('resize', rendering);
allow multiple resize events
- 透過 namespace 來達成,需要注意的是需要每個 resize event 都有 namespace 定義,如下 API 說明及範例
註解 |
If an event listener was already registered for the same type on the selected element, the existing listener is removed before the new listener is added. To register multiple listeners for the same event type, the type may be followed by an optional namespace, such as "click.foo" and "click.bar". |
function renderingEvent1() {
}
Plotly.d3.select(window).on('resize.event1', renderingEvent1);
function renderingEvent2() {
}
Plotly.d3.select(window).on('resize.event2', renderingEvent2);