javascript - Flot 导航插件 - 将缩放限制在一个轴上

标签 javascript jquery zooming flot pan

我试图在我的图表中仅在 x 轴上启用缩放和平移,我希望 yaxis 保持在同一范围内。我正在使用 jQuery Flot 库的导航插件。

我找不到相关文档或其他已解决的问题。所以我尝试通过将 yaxis zoomRange 和 panRange 设置为 [0, 0] 来做到这一点,但它仍然无法正常工作。

图表和“放大”功能正常,但当我“缩小”或在图表中平移时,它会损坏。

这是我目前得到的:http://jsfiddle.net/alxer/heL6uwgj/

$(function() {

  //example data
  var data = [{
    label: 'Velocity',
    color: '#93cc67',
    data: [
      [1415165113000, 0],
      [1415165202000, 13],
      [1415165221000, 19],
      [1415165239000, 22],
      [1415165254000, 23],
      [1415165271000, 24]
    ]
  }];

  //non data-dependent options
  var options = {
    canvas: true,
    series: {
      lines: {
        show: true
      },
      points: {
        show: true
      }
    },
    xaxis: {
      mode: "time",
      timezone: "browser"
    },
    yaxis: {},
    legend: {
      type: "canvas",
      position: "ne"
    },
    grid: {
      clickable: true,
      hoverable: true
    },
    zoom: {
      interactive: true
    },
    pan: {
      interactive: true,
      cursor: "move"
    }
  };

  //calculate the min/max and ranges to set the zoom and pan limits
  var minx = null;
  var maxx = null;
  var miny = null;
  var maxy = null;
  var numvals = 0;
  //calculate min, max and max num of values
  for (var a in data) {
    for (var d in data[a].data) {
      if ((minx === null) || (data[a].data[d][0] < minx)) minx = parseFloat(data[a].data[d][0]);
      if ((maxx === null) || (data[a].data[d][0] > maxx)) maxx = parseFloat(data[a].data[d][0]);
      if ((miny === null) || (data[a].data[d][1] < miny)) miny = parseFloat(data[a].data[d][1]);
      if ((maxy === null) || (data[a].data[d][1] > maxy)) maxy = parseFloat(data[a].data[d][1]);
    }
    if (data[a].data.length > numvals) numvals = data[a].data.length;
  }
  if (minx === null) minx = 0;
  if (maxx === null) maxx = 0;
  if (miny === null) miny = 0;
  if (maxy === null) maxy = 0;
  //calculate ranges
  var xrange = maxx - minx;
  var yrange = maxy - miny;
  //apply small margin
  var auxxmargin = xrange * 0.02;
  var auxymargin = yrange * 0.02;
  options.xaxis.min = minx - auxxmargin;
  options.xaxis.max = maxx + auxxmargin;
  options.yaxis.min = miny;
  options.yaxis.max = maxy + auxymargin;
  //xaxis zoom range from 1 value to all values
  options.xaxis.zoomRange = [xrange / numvals, xrange + (auxxmargin * 2)];
  //xaxis pan range from min value to max value
  options.xaxis.panRange = [options.xaxis.min, options.xaxis.max];
  //trying to disable the yaxis zoom and pan
  options.yaxis.zoomRange = [0, 0];
  options.yaxis.panRange = [0, 0];

  var plot = $.plot("#placeholder", data, options);
});

最佳答案

要禁用一个轴的缩放和平移,请将该轴的 panRangezoomRange 选项设置为 false 而不是 [0, 0]:

options.yaxis.zoomRange = false;
options.yaxis.panRange = false;

查看更新的 fiddle .

关于javascript - Flot 导航插件 - 将缩放限制在一个轴上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37863722/

相关文章:

javascript - 如何将网页分成三个垂直部分?

JavaScript:允许使用正则表达式的字符,但排除这些字符的某些模式

javascript - 鼠标悬停时突出显示文本行

javascript - 停止对选择框中的 HTML 选项进行自动排序

javascript - 使用 jQuery 进行表过滤

传单:平移、居中、缩放 map 到标记位置

javascript - 在纯 js 中 move (拖动/平移)和缩放对象(图像或 div)

javascript - 我可以从我的 nuxt.js 项目中获取所有潜在路线的列表吗?

javascript - 如何使用 jQuery/Ajax 将数据传递到 URL

java - Android OSM 缩放级别 19 及以上