我正在使用Dygraphs来实现一些图形,并且在使用interactiveModel选项自定义缩放选项时遇到了麻烦。按照网站关于如何使用它的建议,似乎没有指定downV3
,moveV3
等,并且我的程序无法识别它们。我使用jsFiddle中的选项复制并粘贴了Dygraphs的示例。有人知道如何有效地使用它和/或正确的语法吗?谢谢!
var g3 = new Dygraph(document.getElementById("div_g3"),
NoisyData, { errorBars : true, interactionModel : {
'mousedown' : downV3,
'mousemove' : moveV3,
'mouseup' : upV3,
'click' : clickV3,
'dblclick' : dblClickV3,
'mousewheel' : scrollV3
}});
答案 0 :(得分:0)
downV3
等是Dygraphs将在命名事件上调用的函数的名称。因此,您必须执行这些功能。 downV3
示例函数为:
function downV3(event, g, context) {
context.initializeMouseDown(event, g, context);
if (event.altKey || event.shiftKey) {
Dygraph.startZoom(event, g, context);
} else {
Dygraph.startPan(event, g, context);
}
}
event
是mousedown
事件,g
是图形对象,context
是图形画布。
您可以在Dygraphs示例页面中的interaction-api.js
中找到所有示例函数。
当然,您可以(并且应该)将这些函数命名为更有意义的东西。
答案 1 :(得分:0)
交互方面也存在相同的问题,并且关于interactionModel
属性的文档不存在...希望此答案将有助于将来的用户...
如果您只想修改这些功能中的一个或多个,则可以使用defaultInteractionModel
:
interactionModel: {
'dblclick': function (event, g, context) { Dygraph.defaultInteractionModel.dblclick(event, g, context); },
'mousedown': function (event, g, context) { Dygraph.defaultInteractionModel.mousedown(event, g, context); },
'touchend': function (event, g, context) { Dygraph.defaultInteractionModel.touchend(event, g, context); },
'touchmove': function (event, g, context) { Dygraph.defaultInteractionModel.touchmove(event, g, context); },
'touchstart': function (event, g, context) { Dygraph.defaultInteractionModel.touchstart(event, g, context); },
willDestroyContextMyself: true
}
这是Dygraphs interaction example中6个功能的基本代码,您可以根据需要对其进行修改:
// Code for a variety of interaction models. Used in interaction.html, but split out from
// that file so they can be tested in isolation.
//
function downV3(event, g, context) {
context.initializeMouseDown(event, g, context);
if (event.altKey || event.shiftKey) {
Dygraph.startZoom(event, g, context);
} else {
Dygraph.startPan(event, g, context);
}
}
function moveV3(event, g, context) {
if (context.isPanning) {
Dygraph.movePan(event, g, context);
} else if (context.isZooming) {
Dygraph.moveZoom(event, g, context);
}
}
function upV3(event, g, context) {
if (context.isPanning) {
Dygraph.endPan(event, g, context);
} else if (context.isZooming) {
Dygraph.endZoom(event, g, context);
}
}
// Take the offset of a mouse event on the dygraph canvas and
// convert it to a pair of percentages from the bottom left.
// (Not top left, bottom is where the lower value is.)
function offsetToPercentage(g, offsetX, offsetY) {
// This is calculating the pixel offset of the leftmost date.
var xOffset = g.toDomCoords(g.xAxisRange()[0], null)[0];
var yar0 = g.yAxisRange(0);
// This is calculating the pixel of the higest value. (Top pixel)
var yOffset = g.toDomCoords(null, yar0[1])[1];
// x y w and h are relative to the corner of the drawing area,
// so that the upper corner of the drawing area is (0, 0).
var x = offsetX - xOffset;
var y = offsetY - yOffset;
// This is computing the rightmost pixel, effectively defining the
// width.
var w = g.toDomCoords(g.xAxisRange()[1], null)[0] - xOffset;
// This is computing the lowest pixel, effectively defining the height.
var h = g.toDomCoords(null, yar0[0])[1] - yOffset;
// Percentage from the left.
var xPct = w == 0 ? 0 : (x / w);
// Percentage from the top.
var yPct = h == 0 ? 0 : (y / h);
// The (1-) part below changes it from "% distance down from the top"
// to "% distance up from the bottom".
return [xPct, (1-yPct)];
}
function dblClickV3(event, g, context) {
// Reducing by 20% makes it 80% the original size, which means
// to restore to original size it must grow by 25%
if (!(event.offsetX && event.offsetY)){
event.offsetX = event.layerX - event.target.offsetLeft;
event.offsetY = event.layerY - event.target.offsetTop;
}
var percentages = offsetToPercentage(g, event.offsetX, event.offsetY);
var xPct = percentages[0];
var yPct = percentages[1];
if (event.ctrlKey) {
zoom(g, -.25, xPct, yPct);
} else {
zoom(g, +.2, xPct, yPct);
}
}
var lastClickedGraph = null;
function clickV3(event, g, context) {
lastClickedGraph = g;
event.preventDefault();
event.stopPropagation();
}
function scrollV3(event, g, context) {
if (lastClickedGraph != g) {
return;
}
var normal = event.detail ? event.detail * -1 : event.wheelDelta / 40;
// For me the normalized value shows 0.075 for one click. If I took
// that verbatim, it would be a 7.5%.
var percentage = normal / 50;
if (!(event.offsetX && event.offsetY)){
event.offsetX = event.layerX - event.target.offsetLeft;
event.offsetY = event.layerY - event.target.offsetTop;
}
var percentages = offsetToPercentage(g, event.offsetX, event.offsetY);
var xPct = percentages[0];
var yPct = percentages[1];
zoom(g, percentage, xPct, yPct);
event.preventDefault();
event.stopPropagation();
}
// Adjusts [x, y] toward each other by zoomInPercentage%
// Split it so the left/bottom axis gets xBias/yBias of that change and
// tight/top gets (1-xBias)/(1-yBias) of that change.
//
// If a bias is missing it splits it down the middle.
function zoom(g, zoomInPercentage, xBias, yBias) {
xBias = xBias || 0.5;
yBias = yBias || 0.5;
function adjustAxis(axis, zoomInPercentage, bias) {
var delta = axis[1] - axis[0];
var increment = delta * zoomInPercentage;
var foo = [increment * bias, increment * (1-bias)];
return [ axis[0] + foo[0], axis[1] - foo[1] ];
}
var yAxes = g.yAxisRanges();
var newYAxes = [];
for (var i = 0; i < yAxes.length; i++) {
newYAxes[i] = adjustAxis(yAxes[i], zoomInPercentage, yBias);
}
g.updateOptions({
dateWindow: adjustAxis(g.xAxisRange(), zoomInPercentage, xBias),
valueRange: newYAxes[0]
});
}
可以找到源文件here