【问题标题】:d3 v3 - sync brush zoom and scroll zoomd3 v3 - 同步画笔缩放和滚动缩放
【发布时间】:2021-02-22 17:41:47
【问题描述】:

我正在尝试在同一个图表中添加画笔缩放和滚动缩放,但是因为两者都依赖于 X 和 Y 来同步变得有点困难。如何根据轴域计算平移和缩放值?

这是我写下的示例图表以进行测试:

var margin = {
    top: 30,
    right: 20,
    bottom: 30,
    left: 50
};
var width = 600 - margin.left - margin.right;
var height = 270 - margin.top - margin.bottom;

var parseDate = d3.time.format("%d-%b-%y").parse;

var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);

var fullX = x.copy();
var fullY = y.copy();

var xAxis = d3.svg.axis().scale(x)
    .orient("bottom").ticks(5);

var yAxis = d3.svg.axis().scale(y)
    .orient("left").ticks(5);

var valueline = d3.svg.line()
    .x(function (d) {
      return x(d.date);
    })
    .y(function (d) {
      return y(d.close);
    });

var svg = d3.select("body")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);
    
 var g = svg
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// Get the data
var data = [{
    date: "1-May-12",
    close: "58.13"
}, {
    date: "30-Apr-12",
    close: "53.98"
}, {
    date: "27-Apr-12",
    close: "67.00"
}, {
    date: "26-Apr-12",
    close: "89.70"
}, {
    date: "25-Apr-12",
    close: "99.00"
}];

data.forEach(function (d) {
    d.date = parseDate(d.date);
    d.close = +d.close;
});

// Scale the range of the data
x.domain(d3.extent(data, function (d) {
    return d.date;
    }));
y.domain([0, d3.max(data, function (d) {
    return d.close;
    })]);


var gBrush = g.append('g').classed('brush', true);

var gAxisX = g.append("g") // Add the X Axis
.attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    
var gAxisY = g.append("g") // Add the Y Axis
.attr("class", "y axis");

function drawAxes() {
  gAxisX.call(xAxis);
  gAxisY.call(yAxis);
}

drawAxes();

var brushing = false;

var zoom = d3.behavior.zoom().x(x).y(y).scaleExtent([1, 10000])
.on('zoom', function () {

  if(brushing && d3.event.sourceEvent.type === 'mousemove' && d3.event.sourceEvent.which !== 2) return ;

  drawAxes();
});
svg.call(zoom);

var brush = d3.svg.brush().x(x).y(y)
.on('brushstart', function () {
  d3.select(this).select('rect.extent').style('display', null);
})
.on('brush', function () {
    brushing = true;
})
.on('brushend', function () {
  brushing = false;
  d3.select(this).select('rect.extent').style('display', 'none');
  
  var extent = brush.extent();
  
  x.domain([extent[0][0], extent[1][0]]);
  y.domain([extent[0][1], extent[1][1]]);

    var sx = x.domain(), sy = y.domain();

  var k = Math.min((x.range()[1]-x.range()[0]) / (fullX(sx[1]) - fullX(sx[0])), (y.range()[1]-y.range()[0]) / (fullY(sy[1]) - fullY(sy[0]))),
      tx = fullX.range()[0] - fullX(sx[0]) * k,
      ty = fullY.range()[0] - fullY(sy[0]) * k;

   // zoom.translate([tx, ty]).scale(k);

 // zoom.x(sy).y(sy); - I don't want to do this to respect scaleExtent to original domains

  drawAxes();
});

gBrush.call(brush);

d3.select('#autofit').on('click', function () {
// Scale the range of the data
x.domain(d3.extent(data, function (d) {
    return d.date;
    }));
y.domain([0, d3.max(data, function (d) {
    return d.close;
    })]);
    drawAxes();
})
body {
    font: 12px Arial;
}
path {
    stroke: steelblue;
    stroke-width: 2;
    fill: none;
}
.axis path, .axis line {
    fill: none;
    stroke: grey;
    stroke-width: 1;
    shape-rendering: crispEdges;
}
rect.extent {
  fill: none;
  stroke: #ddd;
}
<script src="https://d3js.org/d3.v3.min.js"></script>


<button id='autofit'>
Autofit
</button>

是的,有几个与此相关的示例,但在 v4 中,我正在尝试在 v3 中完成此操作。

参考 1:D3.js: Combining Zoom/Brush (v4)

参考 2:http://bl.ocks.org/nnattawat/9689303(问题是缩放的 x 被缩放到新的拉丝缩放,这弄乱了 scaleExtent,我想限制缩放到原始域)

我正在努力解决的问题是:

 var k = Math.min((x.range()[1]-x.range()[0]) / (fullX(sx[1]) - fullX(sx[0])), (y.range()[1]-y.range()[0]) / (fullY(sy[1]) - fullY(sy[0]))),
  tx = fullX.range()[0] - fullX(sx[0]) * k,
  ty = fullY.range()[0] - fullY(sy[0]) * k;

// zoom.translate([tx, ty]).scale(k);

拉丝功能内。

基本上尝试根据新域计算平移和缩放。如果 k 是基于 x.range() 计算的,则那里使用的逻辑适用于 x,如果 k 是基于 y.range() 计算的,则适用于 y 但是让他们一起工作似乎是一场斗争(Math.min 没有工作)。谢谢。

【问题讨论】:

    标签: javascript svg d3.js semantic-zoom


    【解决方案1】:

    看起来没有办法同时转换 X 和 Y,正如 Mike 在此处提到的那样:https://github.com/d3/d3-zoom/issues/48

    附:如果您的缩放受限,即水平或垂直,则可以通过应用适当的比例使这两个缩放功能一起工作。

    例如 X 平移和缩放:

    k_x = (x.range()[1]-x.range()[0]) / (fullX(sx[1]) - fullX(sx[0])); 
    t_x = fullX.range()[0] - fullX(sx[0]) * k_x
    

    而对于 Y 轴平移和缩放,

     k_y = (y.range()[1]-y.range()[0]) / (fullY(sy[1]) - fullY(sy[0])));
     ty = fullY.range()[0] - fullY(sy[0]) * k_y;
    

    在这个问题中提到了一些解决方法,我将对其进行研究,如果一切顺利,我会在此处发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 2017-08-22
      • 2014-02-08
      相关资源
      最近更新 更多