考虑此jsfiddle上的示例:https://jsfiddle.net/6xbj847x/1/
HTML:
Graph
<div id="graphdiv"></div>
JS:
var g = new Dygraph(document.getElementById("graphdiv"),
[
[0, 1], // Starts at height 1, step width is 2
[2, 2], // step width is 1
[3, 3], // step width is 0.5
[3.5, 4], // step width is 0.25
[3.75, 5], // remainder is at height 5
],
{
stepPlot: true,
labels: ["XX","YY"],
series: {
"YY": {stepPlot: true}
}
});
g.ready(function() {
g.setAnnotations([
{
series: "YY",
x: 0,
shortText: "2"
},
{
series: "YY",
x: 2,
shortText: "1"
},
{
series: "YY",
x: 3,
shortText: ".5"
}
]);
});
当开始显示时,y轴显示0到5的范围:
然后我进行“ X”缩放:在x = 2之前单击鼠标左键;在单击时向右拖动,然后在x = 2之后释放;图形将更新,并且x范围将按预期更新:
...还会自动更新y轴,现在它显示0到3(或附近)的范围!
如何在选择图中缩放时防止y轴自动更新-因此总是显示初始的y轴范围?
确定,知道了:
http://dygraphs.com/options.html
valueRange#
将图形的垂直范围明确设置为[低,高]。可以基于每个轴进行设置,以分别定义每个y轴。如果未指定任何限制,则会自动进行计算(例如[null,30]以自动计算下限)
因此,解决方案是:
var g = new Dygraph(document.getElementById("graphdiv"),
[
[0, 1], // Starts at height 1, step width is 2
[2, 2], // step width is 1
[3, 3], // step width is 0.5
[3.5, 4], // step width is 0.25
[3.75, 5], // remainder is at height 5
],
{
stepPlot: true,
labels: ["XX","YY"],
series: {
"YY": {stepPlot: true}
},
valueRange: [0, 5.5], // fix y range axis here, so it does not autoupdate on zoom
});
g.ready(function() {
g.setAnnotations([
{
series: "YY",
x: 0,
shortText: "2"
},
{
series: "YY",
x: 2,
shortText: "1"
},
{
series: "YY",
x: 3,
shortText: ".5"
}
]);
});