简体   繁体   English

保存和恢复“画笔和缩放”? (程序缩放)

[英]saving and restoring “brush & zoom”? (programatic zoom)

I've been learning d3 through working with the Brush & Zoom example to make a custom timeline. 我一直在通过使用“ 画笔和缩放”示例来制作自定义时间轴来学习d3 I would like to be able to allow the user to save "views" of the timeline: zoomed and panned to particular points, save them out to JSON and then restore them later. 我希望能够允许用户保存时间轴的“视图”:缩放和平移到特定点,将它们保存为JSON ,然后稍后将其还原。

Can anyone give me a nudge in the right direction on this: 任何人都可以在正确的方向上向我推一下:

  1. I'm thinking I need to save both the current d3.event.selection and the current d3.event.transform 我在想我需要保存当前的d3.event.selection和当前的d3.event.transform
  2. serialize them to JSON 将它们序列化为JSON
  3. write a function which will take the JSON and recreate the selection and the transform. 编写一个将使用JSON并重新创建选择和转换的函数。

Do I have the steps right? 我有正确的步骤吗? Are there any working (d3 v4) examples you can point me to? 您是否可以向我指出任何有效的(d3 v4)示例? Most of the programmatic zooming examples & tutorials I've found are for earlier versions of d3 (pre v4). 我发现的大多数程序化缩放示例和教程都是针对d3(pre v4)的早期版本的。


functions from the "Brush & Zoom" example which handle interaction: “画笔和缩放”示例中的函数可处理交互:

function brushed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
  var s = d3.event.selection || x2.range();
  x.domain(s.map(x2.invert, x2));
  focus.select(".area").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
}

function zoomed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
  var t = d3.event.transform;
  x.domain(t.rescaleX(x2).domain());
  focus.select(".area").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}

在此处输入图片说明

There's actually an example of a programmatic zoom right in the code you are looking at: 实际上,您正在查看的代码中确实有一个示例性缩放示例:

svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
  .scale(width / (s[1] - s[0]))
  .translate(-s[0], 0));

So, say we stored our "view" JSON in terms of zoomed to dates: 因此,假设我们以缩放至日期的方式存储了“视图” JSON:

["2003-06-12T19:24:00.000Z","2006-05-20T19:24:00.000Z"]

Loading it would simply be: 加载它只是:

d3.json("viewData.json", function(error, data) {
  var d = data.map(function(d){
    return new Date(d);
  })
  svg.select(".zoom")
    .transition()
    .call(zoom.transform, d3.zoomIdentity
      .scale(width / (x(d[1]) - x(d[0])))
      .translate(-x(d[0]), 0)
    );
});

Here's a running example . 这是一个正在运行的示例


 <!DOCTYPE html> <meta charset="utf-8"> <style> .area { fill: steelblue; clip-path: url(#clip); } .zoom { cursor: move; fill: none; pointer-events: all; } </style> <svg width="700" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 110, left: 40}, margin2 = {top: 430, right: 20, bottom: 30, left: 40}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, height2 = +svg.attr("height") - margin2.top - margin2.bottom; var parseDate = d3.timeParse("%b %Y"); var x = d3.scaleTime().range([0, width]), x2 = d3.scaleTime().range([0, width]), y = d3.scaleLinear().range([height, 0]), y2 = d3.scaleLinear().range([height2, 0]); var xAxis = d3.axisBottom(x), xAxis2 = d3.axisBottom(x2), yAxis = d3.axisLeft(y); var brush = d3.brushX() .extent([[0, 0], [width, height2]]) .on("brush end", brushed); var zoom = d3.zoom() .scaleExtent([1, Infinity]) .translateExtent([[0, 0], [width, height]]) .extent([[0, 0], [width, height]]) .on("zoom", zoomed); var area = d3.area() .curve(d3.curveMonotoneX) .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return y(d.price); }); var area2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d) { return x2(d.date); }) .y0(height2) .y1(function(d) { return y2(d.price); }); svg.append("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); var focus = svg.append("g") .attr("class", "focus") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var context = svg.append("g") .attr("class", "context") .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")"); d3.json("https://jsonblob.com/api/8737d329-6f32-11e7-9e0d-efd5dcec6244", function(error, data) { if (error) throw error; data.forEach(type); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return d.price; })]); x2.domain(x.domain()); y2.domain(y.domain()); focus.append("path") .datum(data) .attr("class", "area") .attr("d", area); focus.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(xAxis); focus.append("g") .attr("class", "axis axis--y") .call(yAxis); context.append("path") .datum(data) .attr("class", "area") .attr("d", area2); context.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height2 + ")") .call(xAxis2); context.append("g") .attr("class", "brush") .call(brush) .call(brush.move, x.range()); svg.append("rect") .attr("class", "zoom") .attr("width", width) .attr("height", height) .attr("transform", "translate(" + margin.left + "," + margin.top + ")") .call(zoom); }); function brushed() { if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom var s = d3.event.selection || x2.range(); x.domain(s.map(x2.invert, x2)); focus.select(".area").attr("d", area); focus.select(".axis--x").call(xAxis); svg.select(".zoom").call(zoom.transform, d3.zoomIdentity .scale(width / (s[1] - s[0])) .translate(-s[0], 0)); } function zoomed() { if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush var t = d3.event.transform; x.domain(t.rescaleX(x2).domain()); focus.select(".area").attr("d", area); focus.select(".axis--x").call(xAxis); context.select(".brush").call(brush.move, x.range().map(t.invertX, t)); } function type(d) { d.date = new Date(d.date); d.price = +d.price; return d; } d3.select('body') .append('button') .style('margin', '20px') .text('Load View') .on('click', function(){ d3.json("https://jsonblob.com/api/b580e2b9-6f32-11e7-9e0d-97f11fab6446", function(error, data) { var d = data.map(function(d){ return new Date(d); }) svg.select(".zoom").transition().call(zoom.transform, d3.zoomIdentity .scale(width / (x(d[1]) - x(d[0]))) .translate(-x(d[0]), 0)); }); }) </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM