英文:
Zoom on a Plotly heatmap
问题
目前在Plotly.JS热力图中存在两种“缩放”行为:
- 在这里,您可以采用任何矩形形状进行缩放(单击、拖动和释放)。但像素不是正方形,这对某些应用程序来说不合适(宽高比未保持,有时应该保持宽高比):
const z = Array.from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255)));
Plotly.newPlot('plot', [{type: 'heatmap', z: z}], {});
- 在这里,像素是正方形,多亏了
{'yaxis': {'scaleanchor': 'x'}}
,但然后只能使用特定宽高比的矩形形状进行缩放,这有时对用户体验/UI构成了限制:
const z = Array from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255)));
Plotly.newPlot('plot', [{type: 'heatmap', z: z}], {'yaxis': {'scaleanchor': 'x'}});
问题:如何实现两者兼顾,即您可以绘制任何形状的矩形选择缩放?并保持正方形像素?缩放的对象应该位于图中心(如果需要的话,水平或垂直留有空白)。
英文:
Currently there are 2 "zooming" behaviours in Plotly.JS heatmaps:
-
Here you can take any rectangular shape for the zoom (click, drag and drop). But then the pixels are not square, which is not ok for some applications (the aspect ratio is not preserved, and sometimes it should be preserved):
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const z = Array.from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255))); Plotly.newPlot('plot', [{type: 'heatmap', z: z}], {});
<!-- language: lang-html -->
<script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script> <div id="plot"></div>
<!-- end snippet -->
-
Here the pixels are square thanks to
{'yaxis': {'scaleanchor': 'x'}}
, but then you can zoom only with a certain aspect ratio rectangular shape, which is sometimes a limiting factor for the UX/UI:<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const z = Array.from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255))); Plotly.newPlot('plot', [{type: 'heatmap', z: z}], {'yaxis': {'scaleanchor': 'x'}});
<!-- language: lang-html -->
<script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script> <div id="plot"></div>
<!-- end snippet -->
Question: How to have both, i.e. you can draw a rectangle selection zoom of any shape? and keep square-shape pixels? The zoomed object should be centered in the plot (with horizontal or vertical white space if needed).
答案1
得分: 2
以下是翻译好的内容:
一种实现这一目标的方法是最初使用所需的 scaleratio
设置 scaleanchor
约束,以便在绘制图形后,我们可以计算出产生所需的像素到单位比例的受限制缩放范围比例,而不需要太多麻烦。
然后,我们可以移除约束并附加一个 plotly_relayout 事件处理程序,以在需要时进行调整。由于这些调整是通过调用 Plotly.relayout()
来精确进行的,我们通过条件块和仅考虑合理数量的有效数字来防止无限循环来比较范围比例。
如果重新布局后的比例与目标(受限制)比例不匹配,我们通过扩展一个轴范围(而不是缩小另一个轴)来进行调整,以使用户创建的缩放窗口相对于调整后的范围保持居中。
请注意:代码部分未进行翻译。
英文:
One way to do that is to initially set a scaleanchor
constraint with the desired scaleratio
, so that once the figure is plotted, we can compute the constrained zoom range ratio that produces the desired pixel to unit scaleratio without too much hassle.
Then, we can remove the constraint and attach a plotly_relayout event handler that will do the adjustments when necessary. Since those adjusments are precisely made by calling Plotly.relayout()
, we prevent infinite loops with condition blocks and by considering only a reasonable amount of significant digits to compare the range ratios.
If the ratio after relayout don't match the target (contrained) ratio, we adjust it by expanding one of the axis range (rather than shrinking the other), keeping the user-created zoom window centered relative to the adjusted range.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const z = Array.from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255)));
const data = [{
type: 'heatmap',
z: z
}];
const layout = {
xaxis: {
constrain: 'range',
constraintoward: 'center',
scaleanchor: "y",
scaleratio: 1
}
};
Plotly.newPlot('plot', data, layout).then(afterPlot);
function afterPlot(gd) {
// Reference each axis range
const xrange = gd._fullLayout.xaxis.range;
const yrange = gd._fullLayout.yaxis.range;
// Needed when resetting scale
const xrange_init = [...xrange];
const yrange_init = [...yrange];
// Compute the actual zoom range ratio that produces the desired pixel to unit scaleratio
const zw0 = Math.abs(xrange[1] - xrange[0]);
const zh0 = Math.abs(yrange[1] - yrange[0]);
const r0 = Number((zw0 / zh0).toPrecision(6));
// Now we can remove the scaleanchor constraint
// Nb. the update object references gd._fullLayout.<x|y>axis.range
const update = {
'xaxis.range': xrange,
'yaxis.range': yrange,
'xaxis.scaleanchor': false,
'yaxis.scaleanchor': false
};
Plotly.relayout(gd, update);
// Attach the handler that will do the adjustments after relayout if needed
gd.on('plotly_relayout', relayoutHandler);
function relayoutHandler(e) {
if (e.width || e.height) {
// The layout aspect ratio probably changed, need to reapply the initial
// scaleanchor constraint and reset variables
return unbindAndReset(gd, relayoutHandler);
}
if (e['xaxis.autorange'] || e['yaxis.autorange']) {
// Reset zoom range (dblclick or "autoscale" btn click)
[xrange[0], xrange[1]] = xrange_init;
[yrange[0], yrange[1]] = yrange_init;
return Plotly.relayout(gd, update);
}
// Compute zoom range ratio after relayout
const zw1 = Math.abs(xrange[1] - xrange[0]);
const zh1 = Math.abs(yrange[1] - yrange[0]);
const r1 = Number((zw1 / zh1).toPrecision(6));
if (r1 === r0) {
return; // nothing to do
}
// ratios don't match, expand one of the axis range as necessary
const [xmin, xmax] = getExtremes(gd, 0, 'x');
const [ymin, ymax] = getExtremes(gd, 0, 'y');
if (r1 > r0) {
const extra = (zh1 * r1/r0 - zh1) / 2;
expandAxisRange(yrange, extra, ymin, ymax);
}
if (r1 < r0) {
const extra = (zw1 * r0/r1 - zw1) / 2;
expandAxisRange(xrange, extra, xmin, xmax);
}
Plotly.relayout(gd, update);
}
}
function unbindAndReset(gd, handler) {
gd.removeListener('plotly_relayout', handler);
// Careful here if you want to reuse the original `layout` (eg. could be
// that you set specific ranges initially) because it has been passed by
// reference to newPlot() and been modified since then.
const _layout = {
xaxis: {scaleanchor: 'y', scaleratio: 1, autorange: true},
yaxis: {autorange: true}
};
return Plotly.relayout(gd, _layout).then(afterPlot);
}
function getExtremes(gd, traceIndex, axisId) {
const extremes = gd._fullData[traceIndex]._extremes[axisId];
return [extremes.min[0].val, extremes.max[0].val];
}
function expandAxisRange(range, extra, min, max) {
const reversed = range[0] > range[1];
if (reversed) {
[range[0], range[1]] = [range[1], range[0]];
}
let shift = 0;
if (range[0] - extra < min) {
const out = min - (range[0] - extra);
const room = max - (range[1] + extra);
shift = out <= room ? out : (out + room) / 2;
}
else if (range[1] + extra > max) {
const out = range[1] + extra - max;
const room = range[0] - extra - min;
shift = out <= room ? -out : -(out + room) / 2;
}
range[0] = range[0] - extra + shift;
range[1] = range[1] + extra + shift;
if (reversed) {
[range[0], range[1]] = [range[1], range[0]];
}
}
<!-- language: lang-html -->
<script src="https://cdn.plot.ly/plotly-2.22.0.min.js"></script>
<div id="plot"></div>
<!-- end snippet -->
Nb. In the handler, except when checking if the user just reset the scale, we use references to gd._fullLayout.<x|y>axis.range
rather than checking what contains e
(the passed-in event object), because the references are always up-to-date and their structure never change, unlike the event parameter that only reflects what was updated. Also, because the update
object itself refers these references, it allows to be a bit less verbose and just call Plotly.relayout(gd, update)
after modifying the ranges.
答案2
得分: 1
你可以使用layout.xaxis
和layout.yaxis
属性与scaleanchor
和scaleratio
属性。
这里有一个示例代码片段:
const z = Array.from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255)));
Plotly.newPlot('plot', [{type: 'heatmap', z: z}], {
margin: {t: 50}, // 添加一些顶部边距以使热图居中显示
xaxis: { // 设置x轴属性
scaleanchor: 'y', // 将比例锚定到y轴
scaleratio: 1, // 设置比例系数为1以确保像素是正方形的
},
yaxis: { // 设置y轴属性
scaleanchor: 'x', // 将比例锚定到x轴
scaleratio: 1, // 设置比例系数为1以确保像素是正方形的
},
dragmode: 'select', // 启用矩形选择缩放
});
// 当发生缩放事件时更新图表
document.getElementById('plot').on('plotly_selected', function(eventData) {
const xRange = eventData.range.x;
const yRange = eventData.range.y;
Plotly.relayout('plot', {
'xaxis.range': xRange, // 更新x轴范围
'yaxis.range': yRange, // 更新y轴范围
});
});
首先,为热图定义z
数据,并使用Plotly.newPlot
创建图表。我们使用scaleanchor
属性设置layout.xaxis
和layout.yaxis
属性,将其设置为相反的轴,并将scaleratio
属性设置为1,以确保像素是正方形的。
我们还将dragmode
属性设置为'select'
以启用矩形选择缩放。
最后,我们为plotly_selected
事件添加了事件监听器,以使用Plotly.relayout
更新xaxis.range
和yaxis.range
属性,确保缩放对象在图表中居中显示,如果需要的话会有水平或垂直的空白空间。
希望这有所帮助。
英文:
You can use the layout.xaxis and layout.yaxis properties with the scaleanchor and scaleratio attributes.
Here's an example code snippet:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const z = Array.from({length: 500}, () => Array.from({length: 100}, () => Math.floor(Math.random() * 255)));
Plotly.newPlot('plot', [{type: 'heatmap', z: z}], {
margin: {t: 50}, // Add some top margin to center the heatmap
xaxis: { // Set the x-axis properties
scaleanchor: 'y', // Set the scale anchor to y-axis
scaleratio: 1, // Set the scale ratio to 1 for square pixels
},
yaxis: { // Set the y-axis properties
scaleanchor: 'x', // Set the scale anchor to x-axis
scaleratio: 1, // Set the scale ratio to 1 for square pixels
},
dragmode: 'select', // Enable rectangular selection zoom
});
// Update the plot when a zoom event occurs
document.getElementById('plot').on('plotly_selected', function(eventData) {
const xRange = eventData.range.x;
const yRange = eventData.range.y;
Plotly.relayout('plot', {
'xaxis.range': xRange, // Update the x-axis range
'yaxis.range': yRange, // Update the y-axis range
});
});
<!-- language: lang-html -->
<script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script>
<div id="plot"></div>
<!-- end snippet -->
First define the z data for the heatmap and create the plot using Plotly.newPlot. We set the xaxis and yaxis properties with the scaleanchor attribute set to the opposite axis, and the scaleratio attribute set to 1 to ensure square pixels.
We also set the dragmode property to 'select' to enable rectangular selection zoom.
Finally, we add an event listener to the plotly_selected event that updates the xaxis.range and yaxis.range properties to the selected zoom range using Plotly.relayout. This ensures that the zoomed object is centered in the plot with horizontal or vertical white space if needed.
I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论