D3缩放设置

时间:2017-11-17 17:10:46

标签: javascript d3.js svg event-handling

我在理解D3中缩放的工作方式时遇到了一些麻烦,特别是如何调用它以及初始化设置。我有以下代码片段(仅包含基本位),这是来自D3 V4上的Manning教科书并且工作正常:

ActivityItemSource *itemSource = [[ActivityItemSource alloc] initWithTitle:title url:url thumbnail:thumbnail];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[title, itemSource] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

ActivityItemSource.m

@interface ActivityItemSource() <UIActivityItemSource>

@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSURL *url;
@property (strong, nonatomic) UIImage *thumbnail;
@end

@implementation ActivityItemSource

- (instancetype)initWithTitle:(NSString *)title url:(NSURL *)url thumbnail:(UIImage *)thumbnail {
    self = [self init];
    if (self) {
        self.title = title;
        self.url = url;
        self.thumbnail = thumbnail;
    }

    return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return self.url;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    return self.url;
}

- (NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return self.title;
}

- (UIImage *) activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size {
    return self.thumbnail;
}

@end

为什么有必要在svg上多次调用缩放功能? 第一次调用它时,我们不会通过&#34; zoomSettings&# 34;,第二次我们这样做。这有什么意义?这与缩放事件在SVG上运行的事实有关,而不是一个包含所有路径的组吗?我习惯了更简单的缩放示例,这些示例在SVG上调用,元素绑定到一个组:

var svg = d3.select("body").append("svg")
    .attr("width",width)
    .attr("height",height)

function createMap(countries, cities) {
    var projection = d3.geoMercator()
        .scale(scale)
        .translate([width/2,height/2])

var mapZoom = d3.zoom()
    .on("zoom", zoomed)

var zoomSettings = d3.zoomIdentity
    .translate(width/2, height/2)
    .scale(scale)

svg.call(mapZoom).call(mapZoom.transform, zoomSettings) // ?!!!

function zoomed() {
        var e = d3.event
        .translate([e.transform.x, e.transform.y])
        .scale(e.transform.k)

    // I didn't include the drawing of the paths, but 
       they are appended to the SVG, and this updates their data.

    d3.selectAll("path.graticule").attr("d", geoPath)
    d3.selectAll("path.countries").attr("d", geoPath)
    d3.selectAll("circle.cities")
    .attr("cx", d => projection([d.x,d.y])[0])
    .attr("cy", d => projection([d.x,d.y])[1])
}

任何有关此人可以提供的澄清将不胜感激。我发现D3中的zoom()令人难以置信的混乱,尽管有API。

1 个答案:

答案 0 :(得分:2)

这一行:

svg.call(mapZoom).call(mapZoom.transform, zoomSettings)

正在做两件事。

首先,svg.call(mapZoom) svg.call(mapZoom.transform, zoomSettings)。第二个电话var zoom = d3.zoom()applying the zoom behavior给svg。这是在用户对其做任何事情之前设置它的初始状态。

所以想想这样:

  1. 创建缩放行为(svg.call(mapZoom)
  2. 将其应用于svg元素(.call(mapZoom.transform, zoomSettings)
  3. 设置其初始状态(Language